diff options
author | Chuanqi Xu <yedeng.yd@linux.alibaba.com> | 2024-06-24 15:47:30 +0800 |
---|---|---|
committer | Chuanqi Xu <yedeng.yd@linux.alibaba.com> | 2024-06-24 15:58:46 +0800 |
commit | 790f931886a03324714f31a626eef7e9c609ae97 (patch) | |
tree | 8689a3e94f5995fb28d076a04246d17566d72835 /clang/lib/Sema/SemaLookup.cpp | |
parent | 57f79371a5c08e1328e85b68b757cd5547f2bf62 (diff) | |
download | llvm-790f931886a03324714f31a626eef7e9c609ae97.zip llvm-790f931886a03324714f31a626eef7e9c609ae97.tar.gz llvm-790f931886a03324714f31a626eef7e9c609ae97.tar.bz2 |
[NFC] [Modules] Extract the logic to decide whether the module units belongs to the same module
This patch extracts the logci to decide how we decide the module units
belongs to the same module into a member function of ASTContext. This is
helpful to refactor the implementation in the future.
Diffstat (limited to 'clang/lib/Sema/SemaLookup.cpp')
-rw-r--r-- | clang/lib/Sema/SemaLookup.cpp | 50 |
1 files changed, 27 insertions, 23 deletions
diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp index be6ea20..12b13eb 100644 --- a/clang/lib/Sema/SemaLookup.cpp +++ b/clang/lib/Sema/SemaLookup.cpp @@ -1606,22 +1606,32 @@ bool Sema::isUsableModule(const Module *M) { // [module.global.frag]p1: // The global module fragment can be used to provide declarations that are // attached to the global module and usable within the module unit. - if (M == TheGlobalModuleFragment || M == TheImplicitGlobalModuleFragment || - // If M is the module we're parsing, it should be usable. This covers the - // private module fragment. The private module fragment is usable only if - // it is within the current module unit. And it must be the current - // parsing module unit if it is within the current module unit according - // to the grammar of the private module fragment. NOTE: This is covered by - // the following condition. The intention of the check is to avoid string - // comparison as much as possible. - M == getCurrentModule() || - // The module unit which is in the same module with the current module - // unit is usable. - // - // FIXME: Here we judge if they are in the same module by comparing the - // string. Is there any better solution? - M->getPrimaryModuleInterfaceName() == - llvm::StringRef(getLangOpts().CurrentModule).split(':').first) { + if (M == TheGlobalModuleFragment || M == TheImplicitGlobalModuleFragment) { + UsableModuleUnitsCache.insert(M); + return true; + } + + // Otherwise, the global module fragment from other translation unit is not + // directly usable. + if (M->isGlobalModule()) + return false; + + Module *Current = getCurrentModule(); + + // If we're not parsing a module, we can't use all the declarations from + // another module easily. + if (!Current) + return false; + + // If M is the module we're parsing or M and the current module unit lives in + // the same module, M should be usable. + // + // Note: It should be fine to search the vector `ModuleScopes` linearly since + // it should be generally small enough. There should be rare module fragments + // in a named module unit. + if (llvm::count_if(ModuleScopes, + [&M](const ModuleScope &MS) { return MS.Module == M; }) || + getASTContext().isInSameModule(M, Current)) { UsableModuleUnitsCache.insert(M); return true; } @@ -5816,19 +5826,13 @@ void Sema::diagnoseMissingImport(SourceLocation UseLoc, const NamedDecl *Decl, if (M->isModuleMapModule()) return M->getFullModuleName(); - Module *CurrentModule = getCurrentModule(); - if (M->isImplicitGlobalModule()) M = M->getTopLevelModule(); - bool IsInTheSameModule = - CurrentModule && CurrentModule->getPrimaryModuleInterfaceName() == - M->getPrimaryModuleInterfaceName(); - // If the current module unit is in the same module with M, it is OK to show // the partition name. Otherwise, it'll be sufficient to show the primary // module name. - if (IsInTheSameModule) + if (getASTContext().isInSameModule(M, getCurrentModule())) return M->getTopLevelModuleName().str(); else return M->getPrimaryModuleInterfaceName().str(); |