Skip to content

Commit ce0ca7e

Browse files
committed
[ClangImporter] Clean up top-level filtering logic for redeclarations
No functionality change.
1 parent f96ef9a commit ce0ca7e

File tree

1 file changed

+49
-51
lines changed

1 file changed

+49
-51
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,18 +2011,25 @@ getClangOwningModule(ClangNode Node, const clang::ASTContext &ClangCtx) {
20112011
return nullptr;
20122012
}
20132013

2014+
static const clang::Module *
2015+
getClangTopLevelOwningModule(ClangNode Node,
2016+
const clang::ASTContext &ClangCtx) {
2017+
const clang::Module *OwningModule = getClangOwningModule(Node, ClangCtx);
2018+
if (!OwningModule)
2019+
return nullptr;
2020+
return OwningModule->getTopLevelModule();
2021+
}
2022+
20142023
static bool isVisibleFromModule(const ClangModuleUnit *ModuleFilter,
20152024
const ValueDecl *VD) {
2016-
// Include a value from module X if:
2017-
// * no particular module was requested, or
2018-
// * module X was specifically requested.
2019-
if (!ModuleFilter)
2020-
return true;
2025+
assert(ModuleFilter);
20212026

20222027
auto ContainingUnit = VD->getDeclContext()->getModuleScopeContext();
20232028
if (ModuleFilter == ContainingUnit)
20242029
return true;
20252030

2031+
// The rest of this function is looking to see if the Clang entity that
2032+
// caused VD to be imported has redeclarations in the filter module.
20262033
auto Wrapper = dyn_cast<ClangModuleUnit>(ContainingUnit);
20272034
if (!Wrapper)
20282035
return false;
@@ -2049,53 +2056,44 @@ static bool isVisibleFromModule(const ClangModuleUnit *ModuleFilter,
20492056
return false;
20502057
}
20512058

2059+
// Macros can be "redeclared" by putting an equivalent definition in two
2060+
// different modules. (We don't actually check the equivalence.)
2061+
// FIXME: We're also not checking if the redeclaration is in /this/ module.
2062+
if (ClangNode.getAsMacro())
2063+
return true;
2064+
2065+
const clang::Decl *D = ClangNode.castAsDecl();
20522066
auto &ClangASTContext = ModuleFilter->getClangASTContext();
2053-
auto OwningClangModule = getClangOwningModule(ClangNode, ClangASTContext);
20542067

20552068
// We don't handle Clang submodules; pop everything up to the top-level
20562069
// module.
2057-
if (OwningClangModule)
2058-
OwningClangModule = OwningClangModule->getTopLevelModule();
2059-
2070+
auto OwningClangModule = getClangTopLevelOwningModule(ClangNode,
2071+
ClangASTContext);
20602072
if (OwningClangModule == ModuleFilter->getClangModule())
20612073
return true;
20622074

2063-
if (auto D = ClangNode.getAsDecl()) {
2064-
// Handle redeclared decls.
2065-
if (isa<clang::FunctionDecl>(D) || isa<clang::VarDecl>(D) ||
2066-
isa<clang::TypedefNameDecl>(D)) {
2067-
for (auto Redeclaration : D->redecls()) {
2068-
if (Redeclaration == D)
2069-
continue;
2070-
auto OwningClangModule = getClangOwningModule(Redeclaration,
2071-
ClangASTContext);
2072-
if (OwningClangModule)
2073-
OwningClangModule = OwningClangModule->getTopLevelModule();
2075+
// Handle redeclarable Clang decls by checking each redeclaration.
2076+
bool IsTagDecl = isa<clang::TagDecl>(D);
2077+
if (!(IsTagDecl || isa<clang::FunctionDecl>(D) || isa<clang::VarDecl>(D) ||
2078+
isa<clang::TypedefNameDecl>(D))) {
2079+
return false;
2080+
}
20742081

2075-
if (OwningClangModule == ModuleFilter->getClangModule())
2076-
return true;
2077-
}
2078-
} else if (isa<clang::TagDecl>(D)) {
2079-
for (auto Redeclaration : D->redecls()) {
2080-
if (Redeclaration == D)
2081-
continue;
2082-
if (!cast<clang::TagDecl>(Redeclaration)->isCompleteDefinition())
2083-
continue;
2084-
auto OwningClangModule = getClangOwningModule(Redeclaration,
2085-
ClangASTContext);
2086-
if (OwningClangModule)
2087-
OwningClangModule = OwningClangModule->getTopLevelModule();
2082+
for (auto Redeclaration : D->redecls()) {
2083+
if (Redeclaration == D)
2084+
continue;
20882085

2089-
if (OwningClangModule == ModuleFilter->getClangModule())
2090-
return true;
2091-
}
2092-
}
2093-
}
2086+
// For enums, structs, and unions, only count definitions when looking to
2087+
// see what other modules they appear in.
2088+
if (IsTagDecl)
2089+
if (!cast<clang::TagDecl>(Redeclaration)->isCompleteDefinition())
2090+
continue;
20942091

2095-
// Macros can be "redeclared" too, by putting an equivalent definition in two
2096-
// different modules.
2097-
if (ClangNode.getAsMacro())
2098-
return true;
2092+
auto OwningClangModule = getClangTopLevelOwningModule(Redeclaration,
2093+
ClangASTContext);
2094+
if (OwningClangModule == ModuleFilter->getClangModule())
2095+
return true;
2096+
}
20992097

21002098
return false;
21012099
}
@@ -2125,12 +2123,14 @@ class ClangVectorDeclConsumer : public clang::VisibleDeclConsumer {
21252123

21262124
class FilteringVisibleDeclConsumer : public swift::VisibleDeclConsumer {
21272125
swift::VisibleDeclConsumer &NextConsumer;
2128-
const ClangModuleUnit *ModuleFilter = nullptr;
2126+
const ClangModuleUnit *ModuleFilter;
21292127

21302128
public:
21312129
FilteringVisibleDeclConsumer(swift::VisibleDeclConsumer &consumer,
21322130
const ClangModuleUnit *CMU)
2133-
: NextConsumer(consumer), ModuleFilter(CMU) {}
2131+
: NextConsumer(consumer), ModuleFilter(CMU) {
2132+
assert(CMU);
2133+
}
21342134

21352135
void foundDecl(ValueDecl *VD, DeclVisibilityKind Reason) override {
21362136
if (isVisibleFromModule(ModuleFilter, VD))
@@ -2140,13 +2140,14 @@ class FilteringVisibleDeclConsumer : public swift::VisibleDeclConsumer {
21402140

21412141
class FilteringDeclaredDeclConsumer : public swift::VisibleDeclConsumer {
21422142
swift::VisibleDeclConsumer &NextConsumer;
2143-
const ClangModuleUnit *ModuleFilter = nullptr;
2143+
const ClangModuleUnit *ModuleFilter;
21442144

21452145
public:
21462146
FilteringDeclaredDeclConsumer(swift::VisibleDeclConsumer &consumer,
21472147
const ClangModuleUnit *CMU)
2148-
: NextConsumer(consumer),
2149-
ModuleFilter(CMU) {}
2148+
: NextConsumer(consumer), ModuleFilter(CMU) {
2149+
assert(CMU);
2150+
}
21502151

21512152
void foundDecl(ValueDecl *VD, DeclVisibilityKind Reason) override {
21522153
if (isDeclaredInModule(ModuleFilter, VD))
@@ -2887,10 +2888,7 @@ void ClangModuleUnit::lookupObjCMethods(
28872888
auto &clangCtx = clangSema.getASTContext();
28882889
for (auto objcMethod : objcMethods) {
28892890
// Verify that this method came from this module.
2890-
auto owningClangModule = getClangOwningModule(objcMethod, clangCtx);
2891-
if (owningClangModule)
2892-
owningClangModule = owningClangModule->getTopLevelModule();
2893-
2891+
auto owningClangModule = getClangTopLevelOwningModule(objcMethod, clangCtx);
28942892
if (owningClangModule != clangModule) continue;
28952893

28962894
// If we found a property accessor, import the property.

0 commit comments

Comments
 (0)