Skip to content

[AST] Add OriginalDC argument to ExternalASTSource::FindExternalVisibleDeclsByName #123152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/include/clang/AST/DeclBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -2722,6 +2722,9 @@ class DeclContext {
bool Deserialize = false) const;

private:
lookup_result lookupImpl(DeclarationName Name,
const DeclContext *OriginalLookupDC) const;

/// Whether this declaration context has had externally visible
/// storage added since the last lookup. In this case, \c LookupPtr's
/// invariant may not hold and needs to be fixed before we perform
Expand Down
3 changes: 2 additions & 1 deletion clang/include/clang/AST/ExternalASTMerger.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ class ExternalASTMerger : public ExternalASTSource {

/// Implementation of the ExternalASTSource API.
bool FindExternalVisibleDeclsByName(const DeclContext *DC,
DeclarationName Name) override;
DeclarationName Name,
const DeclContext *OriginalDC) override;

/// Implementation of the ExternalASTSource API.
void
Expand Down
12 changes: 10 additions & 2 deletions clang/include/clang/AST/ExternalASTSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,20 @@ class ExternalASTSource : public RefCountedBase<ExternalASTSource> {
/// Find all declarations with the given name in the given context,
/// and add them to the context by calling SetExternalVisibleDeclsForName
/// or SetNoExternalVisibleDeclsForName.
/// \param DC The context for lookup in. \c DC should be a primary context.
/// \param Name The name to look for.
/// \param OriginalDC The original context for lookup. \c OriginalDC can
/// provide more information than \c DC. e.g., The same namespace can appear
/// in multiple module units. So we need the \c OriginalDC to tell us what
/// the module the lookup come from.
///
/// \return \c true if any declarations might have been found, \c false if
/// we definitely have no declarations with tbis name.
///
/// The default implementation of this method is a no-op returning \c false.
virtual bool
FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name);
virtual bool FindExternalVisibleDeclsByName(const DeclContext *DC,
DeclarationName Name,
const DeclContext *OriginalDC);

/// Load all the external specializations for the Decl \param D if \param
/// OnlyPartial is false. Otherwise, load all the external **partial**
Expand Down
3 changes: 2 additions & 1 deletion clang/include/clang/Sema/MultiplexExternalSemaSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ class MultiplexExternalSemaSource : public ExternalSemaSource {
/// Find all declarations with the given name in the
/// given context.
bool FindExternalVisibleDeclsByName(const DeclContext *DC,
DeclarationName Name) override;
DeclarationName Name,
const DeclContext *OriginalDC) override;

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

Expand Down
3 changes: 2 additions & 1 deletion clang/include/clang/Serialization/ASTReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -2119,7 +2119,8 @@ class ASTReader
/// The current implementation of this method just loads the entire
/// lookup table as unmaterialized references.
bool FindExternalVisibleDeclsByName(const DeclContext *DC,
DeclarationName Name) override;
DeclarationName Name,
const DeclContext *OriginalDC) override;

/// Read all of the declarations lexically stored in a
/// declaration context.
Expand Down
19 changes: 14 additions & 5 deletions clang/lib/AST/DeclBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1856,9 +1856,16 @@ DeclContext::lookup(DeclarationName Name) const {
if (getDeclKind() == Decl::LinkageSpec || getDeclKind() == Decl::Export)
return getParent()->lookup(Name);

const DeclContext *PrimaryContext = getPrimaryContext();
if (PrimaryContext != this)
return PrimaryContext->lookup(Name);
return getPrimaryContext()->lookupImpl(Name, this);
}

DeclContext::lookup_result
DeclContext::lookupImpl(DeclarationName Name,
const DeclContext *OriginalLookupDC) const {
assert(this == getPrimaryContext() &&
"lookupImpl should only be called with primary DC!");
assert(getDeclKind() != Decl::LinkageSpec && getDeclKind() != Decl::Export &&
"We shouldn't lookup in transparent DC.");

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

if (Source->FindExternalVisibleDeclsByName(this, Name) || !R.second) {
if (Source->FindExternalVisibleDeclsByName(this, Name, OriginalLookupDC) ||
!R.second) {
if (StoredDeclsMap *Map = LookupPtr) {
StoredDeclsMap::iterator I = Map->find(Name);
if (I != Map->end())
Expand Down Expand Up @@ -2115,7 +2123,8 @@ void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal) {
if (ExternalASTSource *Source = getParentASTContext().getExternalSource())
if (hasExternalVisibleStorage() &&
Map->find(D->getDeclName()) == Map->end())
Source->FindExternalVisibleDeclsByName(this, D->getDeclName());
Source->FindExternalVisibleDeclsByName(this, D->getDeclName(),
D->getDeclContext());

// Insert this declaration into the map.
StoredDeclsList &DeclNameEntries = (*Map)[D->getDeclName()];
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/AST/ExternalASTMerger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,9 @@ static bool importSpecializationsIfNeeded(Decl *D, ASTImporter *Importer) {
return false;
}

bool ExternalASTMerger::FindExternalVisibleDeclsByName(const DeclContext *DC,
DeclarationName Name) {
bool ExternalASTMerger::FindExternalVisibleDeclsByName(
const DeclContext *DC, DeclarationName Name,
const DeclContext *OriginalDC) {
llvm::SmallVector<NamedDecl *, 1> Decls;
llvm::SmallVector<Candidate, 4> Candidates;

Expand Down
6 changes: 3 additions & 3 deletions clang/lib/AST/ExternalASTSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ ExternalASTSource::GetExternalCXXBaseSpecifiers(uint64_t Offset) {
return nullptr;
}

bool
ExternalASTSource::FindExternalVisibleDeclsByName(const DeclContext *DC,
DeclarationName Name) {
bool ExternalASTSource::FindExternalVisibleDeclsByName(
const DeclContext *DC, DeclarationName Name,
const DeclContext *OriginalDC) {
return false;
}

Expand Down
8 changes: 5 additions & 3 deletions clang/lib/Interpreter/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ class ExternalSource : public clang::ExternalASTSource {
ExternalSource(ASTContext &ChildASTCtxt, FileManager &ChildFM,
ASTContext &ParentASTCtxt, FileManager &ParentFM);
bool FindExternalVisibleDeclsByName(const DeclContext *DC,
DeclarationName Name) override;
DeclarationName Name,
const DeclContext *OriginalDC) override;
void
completeVisibleDeclsMap(const clang::DeclContext *childDeclContext) override;
};
Expand Down Expand Up @@ -270,8 +271,9 @@ ExternalSource::ExternalSource(ASTContext &ChildASTCtxt, FileManager &ChildFM,
Importer.reset(importer);
}

bool ExternalSource::FindExternalVisibleDeclsByName(const DeclContext *DC,
DeclarationName Name) {
bool ExternalSource::FindExternalVisibleDeclsByName(
const DeclContext *DC, DeclarationName Name,
const DeclContext *OriginalDC) {

IdentifierTable &ParentIdTable = ParentASTCtxt.Idents;

Expand Down
8 changes: 5 additions & 3 deletions clang/lib/Sema/MultiplexExternalSemaSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ MultiplexExternalSemaSource::hasExternalDefinitions(const Decl *D) {
return EK_ReplyHazy;
}

bool MultiplexExternalSemaSource::
FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name) {
bool MultiplexExternalSemaSource::FindExternalVisibleDeclsByName(
const DeclContext *DC, DeclarationName Name,
const DeclContext *OriginalDC) {
bool AnyDeclsFound = false;
for (size_t i = 0; i < Sources.size(); ++i)
AnyDeclsFound |= Sources[i]->FindExternalVisibleDeclsByName(DC, Name);
AnyDeclsFound |=
Sources[i]->FindExternalVisibleDeclsByName(DC, Name, OriginalDC);
return AnyDeclsFound;
}

Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Serialization/ASTReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8366,9 +8366,9 @@ void ASTReader::FindFileRegionDecls(FileID File,
*DInfo.Mod, LocalDeclID::get(*this, *DInfo.Mod, *DIt))));
}

bool
ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
DeclarationName Name) {
bool ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC,
DeclarationName Name,
const DeclContext *OriginalDC) {
assert(DC->hasExternalVisibleStorage() && DC == DC->getPrimaryContext() &&
"DeclContext has no visible decls in storage");
if (!Name)
Expand Down
5 changes: 3 additions & 2 deletions clang/unittests/AST/ExternalASTSourceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ TEST(ExternalASTSourceTest, FailedLookupOccursOnce) {
struct TestSource : ExternalASTSource {
TestSource(unsigned &Calls) : Calls(Calls) {}

bool FindExternalVisibleDeclsByName(const DeclContext *,
DeclarationName Name) override {
bool
FindExternalVisibleDeclsByName(const DeclContext *, DeclarationName Name,
const DeclContext *OriginalDC) override {
if (Name.getAsString() == "j")
++Calls;
return false;
Expand Down
14 changes: 8 additions & 6 deletions lldb/source/Plugins/ExpressionParser/Clang/ASTUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ class ExternalASTSourceWrapper : public clang::ExternalSemaSource {
m_Source->updateOutOfDateIdentifier(II);
}

bool FindExternalVisibleDeclsByName(const clang::DeclContext *DC,
clang::DeclarationName Name) override {
return m_Source->FindExternalVisibleDeclsByName(DC, Name);
bool FindExternalVisibleDeclsByName(
const clang::DeclContext *DC, clang::DeclarationName Name,
const clang::DeclContext *OriginalDC) override {
return m_Source->FindExternalVisibleDeclsByName(DC, Name, OriginalDC);
}

bool LoadExternalSpecializations(const clang::Decl *D,
Expand Down Expand Up @@ -387,10 +388,11 @@ class SemaSourceWithPriorities : public clang::ExternalSemaSource {
return EK_ReplyHazy;
}

bool FindExternalVisibleDeclsByName(const clang::DeclContext *DC,
clang::DeclarationName Name) override {
bool FindExternalVisibleDeclsByName(
const clang::DeclContext *DC, clang::DeclarationName Name,
const clang::DeclContext *OriginalDC) override {
for (size_t i = 0; i < Sources.size(); ++i)
if (Sources[i]->FindExternalVisibleDeclsByName(DC, Name))
if (Sources[i]->FindExternalVisibleDeclsByName(DC, Name, OriginalDC))
return true;
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ void ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) {

// The core lookup interface.
bool ClangASTSource::FindExternalVisibleDeclsByName(
const DeclContext *decl_ctx, DeclarationName clang_decl_name) {
const DeclContext *decl_ctx, DeclarationName clang_decl_name,
const clang::DeclContext *original_dc) {
if (!m_ast_context) {
SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
return false;
Expand Down
13 changes: 8 additions & 5 deletions lldb/source/Plugins/ExpressionParser/Clang/ClangASTSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ class ClangASTSource : public clang::ExternalASTSource,
///
/// \return
/// Whatever SetExternalVisibleDeclsForName returns.
bool FindExternalVisibleDeclsByName(const clang::DeclContext *DC,
clang::DeclarationName Name) override;
bool
FindExternalVisibleDeclsByName(const clang::DeclContext *DC,
clang::DeclarationName Name,
const clang::DeclContext *OriginalDC) override;

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

bool FindExternalVisibleDeclsByName(const clang::DeclContext *DC,
clang::DeclarationName Name) override {
return m_original.FindExternalVisibleDeclsByName(DC, Name);
bool FindExternalVisibleDeclsByName(
const clang::DeclContext *DC, clang::DeclarationName Name,
const clang::DeclContext *OriginalDC) override {
return m_original.FindExternalVisibleDeclsByName(DC, Name, OriginalDC);
}

void FindExternalLexicalDecls(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ void ClangExternalASTSourceCallbacks::FindExternalLexicalDecls(
}

bool ClangExternalASTSourceCallbacks::FindExternalVisibleDeclsByName(
const clang::DeclContext *DC, clang::DeclarationName Name) {
const clang::DeclContext *DC, clang::DeclarationName Name,
const clang::DeclContext *OriginalDC) {
llvm::SmallVector<clang::NamedDecl *, 4> decls;
// Objective-C methods are not added into the LookupPtr when they originate
// from an external source. SetExternalVisibleDeclsForName() adds them.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ class ClangExternalASTSourceCallbacks : public clang::ExternalASTSource {
llvm::function_ref<bool(clang::Decl::Kind)> IsKindWeWant,
llvm::SmallVectorImpl<clang::Decl *> &Result) override;

bool FindExternalVisibleDeclsByName(const clang::DeclContext *DC,
clang::DeclarationName Name) override;
bool
FindExternalVisibleDeclsByName(const clang::DeclContext *DC,
clang::DeclarationName Name,
const clang::DeclContext *OriginalDC) override;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ class lldb_private::AppleObjCExternalASTSource
AppleObjCExternalASTSource(AppleObjCDeclVendor &decl_vendor)
: m_decl_vendor(decl_vendor) {}

bool FindExternalVisibleDeclsByName(const clang::DeclContext *decl_ctx,
clang::DeclarationName name) override {
bool FindExternalVisibleDeclsByName(
const clang::DeclContext *decl_ctx, clang::DeclarationName name,
const clang::DeclContext *original_dc) override {

Log *log(GetLog(
LLDBLog::Expressions)); // FIXME - a more appropriate log channel?
Expand Down
Loading