diff options
author | Chuanqi Xu <yedeng.yd@linux.alibaba.com> | 2023-05-10 11:54:04 +0800 |
---|---|---|
committer | Chuanqi Xu <yedeng.yd@linux.alibaba.com> | 2023-05-23 10:52:22 +0800 |
commit | 52bc4b16cb68d6d64c0d9499b2e6c1d719e78085 (patch) | |
tree | 69dce43acf8cef1d724a9d99de2f9f898ae63140 /clang/lib | |
parent | acd84fb6389ce8b1efa13c9bfdcb42fde9c9bdba (diff) | |
download | llvm-52bc4b16cb68d6d64c0d9499b2e6c1d719e78085.zip llvm-52bc4b16cb68d6d64c0d9499b2e6c1d719e78085.tar.gz llvm-52bc4b16cb68d6d64c0d9499b2e6c1d719e78085.tar.bz2 |
[NFC] [C++20] [Modules] Refactor Sema::isModuleUnitOfCurrentTU into
Decl::isInAnotherModuleUnit
Refactor `Sema::isModuleUnitOfCurrentTU` to `Decl::isInAnotherModuleUnit`
to make code simpler a little bit. Note that although this patch
introduces a FIXME, this is an existing issue and this patch just tries
to describe it explicitly.
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/AST/ASTContext.cpp | 6 | ||||
-rw-r--r-- | clang/lib/AST/DeclBase.cpp | 23 | ||||
-rw-r--r-- | clang/lib/Sema/SemaLookup.cpp | 11 | ||||
-rw-r--r-- | clang/lib/Sema/SemaModule.cpp | 13 | ||||
-rw-r--r-- | clang/lib/Sema/SemaOverload.cpp | 31 | ||||
-rw-r--r-- | clang/lib/Sema/SemaTemplate.cpp | 9 |
6 files changed, 46 insertions, 47 deletions
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index ad1e940..2307c33 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -1155,7 +1155,7 @@ ArrayRef<Decl *> ASTContext::getModuleInitializers(Module *M) { void ASTContext::setCurrentNamedModule(Module *M) { assert(M->isModulePurview()); assert(!CurrentCXXNamedModule && - "We should set named module for ASTContext for only once"); + "We should set named module for ASTContext for only once"); CurrentCXXNamedModule = M; } @@ -11935,9 +11935,7 @@ bool ASTContext::DeclMustBeEmitted(const Decl *D) { return false; // Variables in other module units shouldn't be forced to be emitted. - auto *VM = VD->getOwningModule(); - if (VM && VM->getTopLevelModule()->isModulePurview() && - VM->getTopLevelModule() != getCurrentNamedModule()) + if (VD->isInAnotherModuleUnit()) return false; // Variables that can be needed in other TUs are required. diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp index f49945f..834beef 100644 --- a/clang/lib/AST/DeclBase.cpp +++ b/clang/lib/AST/DeclBase.cpp @@ -30,6 +30,7 @@ #include "clang/Basic/IdentifierTable.h" #include "clang/Basic/LLVM.h" #include "clang/Basic/LangOptions.h" +#include "clang/Basic/Module.h" #include "clang/Basic/ObjCRuntime.h" #include "clang/Basic/PartialDiagnostic.h" #include "clang/Basic/SourceLocation.h" @@ -1022,6 +1023,28 @@ bool Decl::isInExportDeclContext() const { return DC && isa<ExportDecl>(DC); } +bool Decl::isInAnotherModuleUnit() const { + auto *M = getOwningModule(); + + if (!M) + return false; + + M = M->getTopLevelModule(); + // FIXME: It is problematic if the header module lives in another module + // unit. Consider to fix this by techniques like + // ExternalASTSource::hasExternalDefinitions. + if (M->isHeaderLikeModule()) + return false; + + // A global module without parent implies that we're parsing the global + // module. So it can't be in another module unit. + if (M->isGlobalModule()) + return false; + + assert(M->isModulePurview() && "New module kind?"); + return M != getASTContext().getCurrentNamedModule(); +} + static Decl::Kind getKind(const Decl *D) { return D->getKind(); } static Decl::Kind getKind(const DeclContext *DC) { return DC->getDeclKind(); } diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp index e3a4733..5a2a361 100644 --- a/clang/lib/Sema/SemaLookup.cpp +++ b/clang/lib/Sema/SemaLookup.cpp @@ -1905,14 +1905,11 @@ bool LookupResult::isReachableSlow(Sema &SemaRef, NamedDecl *D) { Module *DeclModule = SemaRef.getOwningModule(D); assert(DeclModule && "hidden decl has no owning module"); - // Entities in module map modules are reachable only if they're visible. - if (DeclModule->isModuleMapModule()) + // Entities in header like modules are reachable only if they're visible. + if (DeclModule->isHeaderLikeModule()) return false; - // If D comes from a module and SemaRef doesn't own a module, it implies D - // comes from another TU. In case SemaRef owns a module, we could judge if D - // comes from another TU by comparing the module unit. - if (SemaRef.isModuleUnitOfCurrentTU(DeclModule)) + if (!D->isInAnotherModuleUnit()) return true; // [module.reach]/p3: @@ -3893,7 +3890,7 @@ void Sema::ArgumentDependentLookup(DeclarationName Name, SourceLocation Loc, "bad export context"); // .. are attached to a named module M, do not appear in the // translation unit containing the point of the lookup.. - if (!isModuleUnitOfCurrentTU(FM) && + if (D->isInAnotherModuleUnit() && llvm::any_of(AssociatedClasses, [&](auto *E) { // ... and have the same innermost enclosing non-inline // namespace scope as a declaration of an associated entity diff --git a/clang/lib/Sema/SemaModule.cpp b/clang/lib/Sema/SemaModule.cpp index a1ab013..67c6556 100644 --- a/clang/lib/Sema/SemaModule.cpp +++ b/clang/lib/Sema/SemaModule.cpp @@ -1024,16 +1024,3 @@ void Sema::PopImplicitGlobalModuleFragment() { "left the wrong module scope, which is not global module fragment"); ModuleScopes.pop_back(); } - -bool Sema::isModuleUnitOfCurrentTU(const Module *M) const { - assert(M); - - Module *CurrentModuleUnit = getCurrentModule(); - - // If we are not in a module currently, M must not be the module unit of - // current TU. - if (!CurrentModuleUnit) - return false; - - return M->isSubModuleOf(CurrentModuleUnit->getTopLevelModule()); -} diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 135bf80..7f3e78c 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -6543,23 +6543,20 @@ void Sema::AddOverloadCandidate( } // Functions with internal linkage are only viable in the same module unit. - if (auto *MF = Function->getOwningModule()) { - if (getLangOpts().CPlusPlusModules && !MF->isModuleMapModule() && - !isModuleUnitOfCurrentTU(MF)) { - /// FIXME: Currently, the semantics of linkage in clang is slightly - /// different from the semantics in C++ spec. In C++ spec, only names - /// have linkage. So that all entities of the same should share one - /// linkage. But in clang, different entities of the same could have - /// different linkage. - NamedDecl *ND = Function; - if (auto *SpecInfo = Function->getTemplateSpecializationInfo()) - ND = SpecInfo->getTemplate(); - - if (ND->getFormalLinkage() == Linkage::InternalLinkage) { - Candidate.Viable = false; - Candidate.FailureKind = ovl_fail_module_mismatched; - return; - } + if (getLangOpts().CPlusPlusModules && Function->isInAnotherModuleUnit()) { + /// FIXME: Currently, the semantics of linkage in clang is slightly + /// different from the semantics in C++ spec. In C++ spec, only names + /// have linkage. So that all entities of the same should share one + /// linkage. But in clang, different entities of the same could have + /// different linkage. + NamedDecl *ND = Function; + if (auto *SpecInfo = Function->getTemplateSpecializationInfo()) + ND = SpecInfo->getTemplate(); + + if (ND->getFormalLinkage() == Linkage::InternalLinkage) { + Candidate.Viable = false; + Candidate.FailureKind = ovl_fail_module_mismatched; + return; } } diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp index c6ba587..7a54521 100644 --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -2851,8 +2851,7 @@ bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, NewDefaultLoc = NewTypeParm->getDefaultArgumentLoc(); SawDefaultArgument = true; - if (!OldTypeParm->getOwningModule() || - isModuleUnitOfCurrentTU(OldTypeParm->getOwningModule())) + if (!OldTypeParm->getOwningModule()) RedundantDefaultArg = true; else if (!getASTContext().isSameDefaultTemplateArgument(OldTypeParm, NewTypeParm)) { @@ -2904,8 +2903,7 @@ bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, OldDefaultLoc = OldNonTypeParm->getDefaultArgumentLoc(); NewDefaultLoc = NewNonTypeParm->getDefaultArgumentLoc(); SawDefaultArgument = true; - if (!OldNonTypeParm->getOwningModule() || - isModuleUnitOfCurrentTU(OldNonTypeParm->getOwningModule())) + if (!OldNonTypeParm->getOwningModule()) RedundantDefaultArg = true; else if (!getASTContext().isSameDefaultTemplateArgument( OldNonTypeParm, NewNonTypeParm)) { @@ -2956,8 +2954,7 @@ bool Sema::CheckTemplateParameterList(TemplateParameterList *NewParams, OldDefaultLoc = OldTemplateParm->getDefaultArgument().getLocation(); NewDefaultLoc = NewTemplateParm->getDefaultArgument().getLocation(); SawDefaultArgument = true; - if (!OldTemplateParm->getOwningModule() || - isModuleUnitOfCurrentTU(OldTemplateParm->getOwningModule())) + if (!OldTemplateParm->getOwningModule()) RedundantDefaultArg = true; else if (!getASTContext().isSameDefaultTemplateArgument( OldTemplateParm, NewTemplateParm)) { |