Skip to content

Commit ea3eb44

Browse files
committed
[AST] Add OriginalDC argument to
ExternalASTSource::FindExternalVisibleDeclsByName Part of #122887. I split this to test where the performance regession comes from if modules are not used.
1 parent 731db2a commit ea3eb44

File tree

18 files changed

+82
-43
lines changed

18 files changed

+82
-43
lines changed

clang/include/clang/AST/DeclBase.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2722,6 +2722,9 @@ class DeclContext {
27222722
bool Deserialize = false) const;
27232723

27242724
private:
2725+
lookup_result lookupImpl(DeclarationName Name,
2726+
const DeclContext *OriginalLookupDC) const;
2727+
27252728
/// Whether this declaration context has had externally visible
27262729
/// storage added since the last lookup. In this case, \c LookupPtr's
27272730
/// invariant may not hold and needs to be fixed before we perform

clang/include/clang/AST/ExternalASTMerger.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ class ExternalASTMerger : public ExternalASTSource {
141141

142142
/// Implementation of the ExternalASTSource API.
143143
bool FindExternalVisibleDeclsByName(const DeclContext *DC,
144-
DeclarationName Name) override;
144+
DeclarationName Name,
145+
const DeclContext *OriginalDC) override;
145146

146147
/// Implementation of the ExternalASTSource API.
147148
void

clang/include/clang/AST/ExternalASTSource.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,20 @@ class ExternalASTSource : public RefCountedBase<ExternalASTSource> {
145145
/// Find all declarations with the given name in the given context,
146146
/// and add them to the context by calling SetExternalVisibleDeclsForName
147147
/// or SetNoExternalVisibleDeclsForName.
148+
/// \param DC The context for lookup in. \c DC should be a primary context.
149+
/// \param Name The name to look for.
150+
/// \param OriginalDC The original context for lookup. \c OriginalDC can
151+
/// provide more information than \c DC. e.g., The same namespace can appear
152+
/// in multiple module units. So we need the \c OriginalDC to tell us what
153+
/// the module the lookup come from.
154+
///
148155
/// \return \c true if any declarations might have been found, \c false if
149156
/// we definitely have no declarations with tbis name.
150157
///
151158
/// The default implementation of this method is a no-op returning \c false.
152-
virtual bool
153-
FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name);
159+
virtual bool FindExternalVisibleDeclsByName(const DeclContext *DC,
160+
DeclarationName Name,
161+
const DeclContext *OriginalDC);
154162

155163
/// Load all the external specializations for the Decl \param D if \param
156164
/// OnlyPartial is false. Otherwise, load all the external **partial**

clang/include/clang/Sema/MultiplexExternalSemaSource.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ class MultiplexExternalSemaSource : public ExternalSemaSource {
9595
/// Find all declarations with the given name in the
9696
/// given context.
9797
bool FindExternalVisibleDeclsByName(const DeclContext *DC,
98-
DeclarationName Name) override;
98+
DeclarationName Name,
99+
const DeclContext *OriginalDC) override;
99100

100101
bool LoadExternalSpecializations(const Decl *D, bool OnlyPartial) override;
101102

clang/include/clang/Serialization/ASTReader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2119,7 +2119,8 @@ class ASTReader
21192119
/// The current implementation of this method just loads the entire
21202120
/// lookup table as unmaterialized references.
21212121
bool FindExternalVisibleDeclsByName(const DeclContext *DC,
2122-
DeclarationName Name) override;
2122+
DeclarationName Name,
2123+
const DeclContext *OriginalDC) override;
21232124

21242125
/// Read all of the declarations lexically stored in a
21252126
/// declaration context.

clang/lib/AST/DeclBase.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,9 +1856,16 @@ DeclContext::lookup(DeclarationName Name) const {
18561856
if (getDeclKind() == Decl::LinkageSpec || getDeclKind() == Decl::Export)
18571857
return getParent()->lookup(Name);
18581858

1859-
const DeclContext *PrimaryContext = getPrimaryContext();
1860-
if (PrimaryContext != this)
1861-
return PrimaryContext->lookup(Name);
1859+
return getPrimaryContext()->lookupImpl(Name, this);
1860+
}
1861+
1862+
DeclContext::lookup_result
1863+
DeclContext::lookupImpl(DeclarationName Name,
1864+
const DeclContext *OriginalLookupDC) const {
1865+
assert(this == getPrimaryContext() &&
1866+
"lookupImpl should only be called with primary DC!");
1867+
assert(getDeclKind() != Decl::LinkageSpec && getDeclKind() != Decl::Export &&
1868+
"We shouldn't lookup in transparent DC.");
18621869

18631870
// If we have an external source, ensure that any later redeclarations of this
18641871
// context have been loaded, since they may add names to the result of this
@@ -1889,7 +1896,8 @@ DeclContext::lookup(DeclarationName Name) const {
18891896
if (!R.second && !R.first->second.hasExternalDecls())
18901897
return R.first->second.getLookupResult();
18911898

1892-
if (Source->FindExternalVisibleDeclsByName(this, Name) || !R.second) {
1899+
if (Source->FindExternalVisibleDeclsByName(this, Name, OriginalLookupDC) ||
1900+
!R.second) {
18931901
if (StoredDeclsMap *Map = LookupPtr) {
18941902
StoredDeclsMap::iterator I = Map->find(Name);
18951903
if (I != Map->end())
@@ -2115,7 +2123,8 @@ void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
21152123
if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
21162124
if (hasExternalVisibleStorage() &&
21172125
Map->find(D->getDeclName()) == Map->end())
2118-
Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
2126+
Source->FindExternalVisibleDeclsByName(this, D->getDeclName(),
2127+
D->getDeclContext());
21192128

21202129
// Insert this declaration into the map.
21212130
StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];

clang/lib/AST/ExternalASTMerger.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,8 +471,9 @@ static bool importSpecializationsIfNeeded(Decl *D, ASTImporter *Importer) {
471471
return false;
472472
}
473473

474-
bool ExternalASTMerger::FindExternalVisibleDeclsByName(const DeclContext *DC,
475-
DeclarationName Name) {
474+
bool ExternalASTMerger::FindExternalVisibleDeclsByName(
475+
const DeclContext *DC, DeclarationName Name,
476+
const DeclContext *OriginalDC) {
476477
llvm::SmallVector<NamedDecl *, 1> Decls;
477478
llvm::SmallVector<Candidate, 4> Candidates;
478479

clang/lib/AST/ExternalASTSource.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ ExternalASTSource::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
9090
return nullptr;
9191
}
9292

93-
bool
94-
ExternalASTSource::FindExternalVisibleDeclsByName(const DeclContext *DC,
95-
DeclarationName Name) {
93+
bool ExternalASTSource::FindExternalVisibleDeclsByName(
94+
const DeclContext *DC, DeclarationName Name,
95+
const DeclContext *OriginalDC) {
9696
return false;
9797
}
9898

clang/lib/Interpreter/CodeCompletion.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ class ExternalSource : public clang::ExternalASTSource {
228228
ExternalSource(ASTContext &ChildASTCtxt, FileManager &ChildFM,
229229
ASTContext &ParentASTCtxt, FileManager &ParentFM);
230230
bool FindExternalVisibleDeclsByName(const DeclContext *DC,
231-
DeclarationName Name) override;
231+
DeclarationName Name,
232+
const DeclContext *OriginalDC) override;
232233
void
233234
completeVisibleDeclsMap(const clang::DeclContext *childDeclContext) override;
234235
};
@@ -270,8 +271,9 @@ ExternalSource::ExternalSource(ASTContext &ChildASTCtxt, FileManager &ChildFM,
270271
Importer.reset(importer);
271272
}
272273

273-
bool ExternalSource::FindExternalVisibleDeclsByName(const DeclContext *DC,
274-
DeclarationName Name) {
274+
bool ExternalSource::FindExternalVisibleDeclsByName(
275+
const DeclContext *DC, DeclarationName Name,
276+
const DeclContext *OriginalDC) {
275277

276278
IdentifierTable &ParentIdTable = ParentASTCtxt.Idents;
277279

clang/lib/Sema/MultiplexExternalSemaSource.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,13 @@ MultiplexExternalSemaSource::hasExternalDefinitions(const Decl *D) {
107107
return EK_ReplyHazy;
108108
}
109109

110-
bool MultiplexExternalSemaSource::
111-
FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name) {
110+
bool MultiplexExternalSemaSource::FindExternalVisibleDeclsByName(
111+
const DeclContext *DC, DeclarationName Name,
112+
const DeclContext *OriginalDC) {
112113
bool AnyDeclsFound = false;
113114
for (size_t i = 0; i < Sources.size(); ++i)
114-
AnyDeclsFound |= Sources[i]->FindExternalVisibleDeclsByName(DC, Name);
115+
AnyDeclsFound |=
116+
Sources[i]->FindExternalVisibleDeclsByName(DC, Name, OriginalDC);
115117
return AnyDeclsFound;
116118
}
117119

0 commit comments

Comments
 (0)