From d384267ad0d5494832f7f53b888c3968b7e688a8 Mon Sep 17 00:00:00 2001 From: Chuanqi Xu Date: Fri, 12 Jul 2024 13:32:40 +0800 Subject: [PATCH] [NFC] [Modules] Introduce 'DeclBase::isInNamedModule' interface This patch introduces DeclBase::isInNamedModule API to ease the use of modules slightly. --- clang/include/clang/AST/DeclBase.h | 3 +++ clang/lib/AST/Decl.cpp | 11 ++--------- clang/lib/AST/DeclBase.cpp | 4 ++++ clang/lib/Sema/SemaDecl.cpp | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h index 06ffc2ce09b8..6c711cfe7927 100644 --- a/clang/include/clang/AST/DeclBase.h +++ b/clang/include/clang/AST/DeclBase.h @@ -673,6 +673,9 @@ public: /// Whether this declaration comes from explicit global module. bool isFromExplicitGlobalModule() const; + /// Whether this declaration comes from a named module. + bool isInNamedModule() const; + /// Return true if this declaration has an attribute which acts as /// definition of the entity, such as 'alias' or 'ifunc'. bool hasDefiningAttr() const; diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index ecccab08cbaa..490c4a2fc525 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -1181,13 +1181,6 @@ Linkage NamedDecl::getLinkageInternal() const { .getLinkage(); } -/// Determine whether D is attached to a named module. -static bool isInNamedModule(const NamedDecl *D) { - if (auto *M = D->getOwningModule()) - return M->isNamedModule(); - return false; -} - static bool isExportedFromModuleInterfaceUnit(const NamedDecl *D) { // FIXME: Handle isModulePrivate. switch (D->getModuleOwnershipKind()) { @@ -1197,7 +1190,7 @@ static bool isExportedFromModuleInterfaceUnit(const NamedDecl *D) { return false; case Decl::ModuleOwnershipKind::Visible: case Decl::ModuleOwnershipKind::VisibleWhenImported: - return isInNamedModule(D); + return D->isInNamedModule(); } llvm_unreachable("unexpected module ownership kind"); } @@ -1215,7 +1208,7 @@ Linkage NamedDecl::getFormalLinkage() const { // [basic.namespace.general]/p2 // A namespace is never attached to a named module and never has a name with // module linkage. - if (isInNamedModule(this) && InternalLinkage == Linkage::External && + if (isInNamedModule() && InternalLinkage == Linkage::External && !isExportedFromModuleInterfaceUnit( cast(this->getCanonicalDecl())) && !isa(this)) diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp index eef946e3aea2..ef2c57e6204d 100644 --- a/clang/lib/AST/DeclBase.cpp +++ b/clang/lib/AST/DeclBase.cpp @@ -1145,6 +1145,10 @@ bool Decl::isFromExplicitGlobalModule() const { return getOwningModule() && getOwningModule()->isExplicitGlobalModule(); } +bool Decl::isInNamedModule() const { + return getOwningModule() && getOwningModule()->isNamedModule(); +} + 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/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 66eeaa8e6f77..e3377bef2ade 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -10094,7 +10094,7 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, // check at the end of the TU (or when the PMF starts) to see that we // have a definition at that point. if (isInline && !D.isFunctionDefinition() && getLangOpts().CPlusPlus20 && - NewFD->hasOwningModule() && NewFD->getOwningModule()->isNamedModule()) { + NewFD->isInNamedModule()) { PendingInlineFuncDecls.insert(NewFD); } }