Skip to content

Commit 263fed7

Browse files
authored
[AST] Add OriginalDC argument to ExternalASTSource::FindExternalVisibleDeclsByName (#123152)
Part for relanding #122887. I split this to test where the performance regession comes from if modules are not used.
1 parent a4e87da commit 263fed7

18 files changed

+82
-43
lines changed

clang/include/clang/AST/DeclBase.h

+3
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

+2-1
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

+10-2
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

+2-1
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

+2-1
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

+14-5
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

+3-2
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

+3-3
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

+5-3
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

+5-3
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

clang/lib/Serialization/ASTReader.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -8366,9 +8366,9 @@ void ASTReader::FindFileRegionDecls(FileID File,
83668366
*DInfo.Mod, LocalDeclID::get(*this, *DInfo.Mod, *DIt))));
83678367
}
83688368

8369-
bool
8370-
ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
8371-
DeclarationName Name) {
8369+
bool ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
8370+
DeclarationName Name,
8371+
const DeclContext *OriginalDC) {
83728372
assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
83738373
"DeclContext has no visible decls in storage");
83748374
if (!Name)

clang/unittests/AST/ExternalASTSourceTest.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ TEST(ExternalASTSourceTest, FailedLookupOccursOnce) {
6767
struct TestSource : ExternalASTSource {
6868
TestSource(unsigned &Calls) : Calls(Calls) {}
6969

70-
bool FindExternalVisibleDeclsByName(const DeclContext *,
71-
DeclarationName Name) override {
70+
bool
71+
FindExternalVisibleDeclsByName(const DeclContext *, DeclarationName Name,
72+
const DeclContext *OriginalDC) override {
7273
if (Name.getAsString() == "j")
7374
++Calls;
7475
return false;

lldb/source/Plugins/ExpressionParser/Clang/ASTUtils.h

+8-6
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ class ExternalASTSourceWrapper : public clang::ExternalSemaSource {
7070
m_Source->updateOutOfDateIdentifier(II);
7171
}
7272

73-
bool FindExternalVisibleDeclsByName(const clang::DeclContext *DC,
74-
clang::DeclarationName Name) override {
75-
return m_Source->FindExternalVisibleDeclsByName(DC, Name);
73+
bool FindExternalVisibleDeclsByName(
74+
const clang::DeclContext *DC, clang::DeclarationName Name,
75+
const clang::DeclContext *OriginalDC) override {
76+
return m_Source->FindExternalVisibleDeclsByName(DC, Name, OriginalDC);
7677
}
7778

7879
bool LoadExternalSpecializations(const clang::Decl *D,
@@ -387,10 +388,11 @@ class SemaSourceWithPriorities : public clang::ExternalSemaSource {
387388
return EK_ReplyHazy;
388389
}
389390

390-
bool FindExternalVisibleDeclsByName(const clang::DeclContext *DC,
391-
clang::DeclarationName Name) override {
391+
bool FindExternalVisibleDeclsByName(
392+
const clang::DeclContext *DC, clang::DeclarationName Name,
393+
const clang::DeclContext *OriginalDC) override {
392394
for (size_t i = 0; i < Sources.size(); ++i)
393-
if (Sources[i]->FindExternalVisibleDeclsByName(DC, Name))
395+
if (Sources[i]->FindExternalVisibleDeclsByName(DC, Name, OriginalDC))
394396
return true;
395397
return false;
396398
}

lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ void ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) {
9999

100100
// The core lookup interface.
101101
bool ClangASTSource::FindExternalVisibleDeclsByName(
102-
const DeclContext *decl_ctx, DeclarationName clang_decl_name) {
102+
const DeclContext *decl_ctx, DeclarationName clang_decl_name,
103+
const clang::DeclContext *original_dc) {
103104
if (!m_ast_context) {
104105
SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
105106
return false;

lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.h

+8-5
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,10 @@ class ClangASTSource : public clang::ExternalASTSource,
8383
///
8484
/// \return
8585
/// Whatever SetExternalVisibleDeclsForName returns.
86-
bool FindExternalVisibleDeclsByName(const clang::DeclContext *DC,
87-
clang::DeclarationName Name) override;
86+
bool
87+
FindExternalVisibleDeclsByName(const clang::DeclContext *DC,
88+
clang::DeclarationName Name,
89+
const clang::DeclContext *OriginalDC) override;
8890

8991
/// Enumerate all Decls in a given lexical context.
9092
///
@@ -211,9 +213,10 @@ class ClangASTSource : public clang::ExternalASTSource,
211213
public:
212214
ClangASTSourceProxy(ClangASTSource &original) : m_original(original) {}
213215

214-
bool FindExternalVisibleDeclsByName(const clang::DeclContext *DC,
215-
clang::DeclarationName Name) override {
216-
return m_original.FindExternalVisibleDeclsByName(DC, Name);
216+
bool FindExternalVisibleDeclsByName(
217+
const clang::DeclContext *DC, clang::DeclarationName Name,
218+
const clang::DeclContext *OriginalDC) override {
219+
return m_original.FindExternalVisibleDeclsByName(DC, Name, OriginalDC);
217220
}
218221

219222
void FindExternalLexicalDecls(

lldb/source/Plugins/ExpressionParser/Clang/ClangExternalASTSourceCallbacks.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ void ClangExternalASTSourceCallbacks::FindExternalLexicalDecls(
5050
}
5151

5252
bool ClangExternalASTSourceCallbacks::FindExternalVisibleDeclsByName(
53-
const clang::DeclContext *DC, clang::DeclarationName Name) {
53+
const clang::DeclContext *DC, clang::DeclarationName Name,
54+
const clang::DeclContext *OriginalDC) {
5455
llvm::SmallVector<clang::NamedDecl *, 4> decls;
5556
// Objective-C methods are not added into the LookupPtr when they originate
5657
// from an external source. SetExternalVisibleDeclsForName() adds them.

lldb/source/Plugins/ExpressionParser/Clang/ClangExternalASTSourceCallbacks.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ class ClangExternalASTSourceCallbacks : public clang::ExternalASTSource {
3737
llvm::function_ref<bool(clang::Decl::Kind)> IsKindWeWant,
3838
llvm::SmallVectorImpl<clang::Decl *> &Result) override;
3939

40-
bool FindExternalVisibleDeclsByName(const clang::DeclContext *DC,
41-
clang::DeclarationName Name) override;
40+
bool
41+
FindExternalVisibleDeclsByName(const clang::DeclContext *DC,
42+
clang::DeclarationName Name,
43+
const clang::DeclContext *OriginalDC) override;
4244

4345
void CompleteType(clang::TagDecl *tag_decl) override;
4446

lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCDeclVendor.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ class lldb_private::AppleObjCExternalASTSource
2929
AppleObjCExternalASTSource(AppleObjCDeclVendor &decl_vendor)
3030
: m_decl_vendor(decl_vendor) {}
3131

32-
bool FindExternalVisibleDeclsByName(const clang::DeclContext *decl_ctx,
33-
clang::DeclarationName name) override {
32+
bool FindExternalVisibleDeclsByName(
33+
const clang::DeclContext *decl_ctx, clang::DeclarationName name,
34+
const clang::DeclContext *original_dc) override {
3435

3536
Log *log(GetLog(
3637
LLDBLog::Expressions)); // FIXME - a more appropriate log channel?

0 commit comments

Comments
 (0)