llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Mahmoud Ahmed (anondeveg) <details> <summary>Changes</summary> Problem: In C, `buildLookupImpl` recurses into the unscoped enum with noload_decls(), only seeing what is in memory. Enumerators of an enum from a PCH or module that haven't been deserialized yet are skipped. subsequently `TU->lookup()` dosen't find them. fix: make `DeclContext::buildLookupImpl` load from disk using `InnerCtx->LoadLexicalDeclsFromExternalStorage()`, given that it is a TU that is not c++ and `InnerCtx` still has external lexical storage, before recursing into the `InnerCtx`, deserializing `InnerCtx` children. added a new `LoadExternalTransparent` parameter, default `false`, so `noload_lookup()` still dosen't touch disk. `buildLookup()` is the only caller that turns it on. performance: Each context is loaded at most once. Normal C compilation never builds the TU lookup table, so it is unaffected. only `TU->lookup` callers pay the cost once. On a PCH of 15 glibc headers the first `TU->lookup()` deserializes 119 more declarations (+2%). Fixes #<!-- -->246166 --- Full diff: https://github.com/llvm/llvm-project/pull/246625.diff 4 Files Affected: - (modified) clang/docs/ReleaseNotes.md (+4-1) - (modified) clang/include/clang/AST/DeclBase.h (+2-1) - (modified) clang/lib/AST/DeclBase.cpp (+14-4) - (added) clang/test/PCH/c-enum-tu-lookup.c (+16) ``````````diff diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index dce8b7992b72f3..0aee6c303ee7ed 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -1341,7 +1341,10 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and - Fixed incorrect comparisons between `ObjCProtocolLoc` nodes stored in a `DynTypedNode`. This subsequently fixes incorrect nodes found by `ASTContext::getParents()` for an `ObjCProtocolLoc`. - + +- Fixed ``DeclContext::lookup`` on a C translation unit not finding enumerators + declared in a precompiled header or module. (#GH246166) + #### Miscellaneous Bug Fixes - Fixed `clang-cl` optimization options such as `/O1`, `/O2`, and diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h index 44caeae238d282..5f205a2d8a8426 100644 --- a/clang/include/clang/AST/DeclBase.h +++ b/clang/include/clang/AST/DeclBase.h @@ -2796,7 +2796,8 @@ class DeclContext { StoredDeclsMap *CreateStoredDeclsMap(ASTContext &C) const; void loadLazyLocalLexicalLookups(); - void buildLookupImpl(DeclContext *DCtx, bool Internal); + void buildLookupImpl(DeclContext *DCtx, bool Internal, + bool LoadExternalTransparent = false); void makeDeclVisibleInContextWithFlags(NamedDecl *D, bool Internal, bool Rediscoverable); void makeDeclVisibleInContextImpl(NamedDecl *D, bool Internal); diff --git a/clang/lib/AST/DeclBase.cpp b/clang/lib/AST/DeclBase.cpp index aac5beec9a741c..edc26efbdef51f 100644 --- a/clang/lib/AST/DeclBase.cpp +++ b/clang/lib/AST/DeclBase.cpp @@ -1881,8 +1881,14 @@ StoredDeclsMap *DeclContext::buildLookup() { return LookupPtr; } + // In C, the TU has no serialized visible-decls table (see + // ASTWriter::GenerateNameLookupTable), so declarations inside transparent + // contexts loaded from an AST file can only be found by loading those + // contexts' lexical decls here from disk. + bool LoadExternalTransparent = + isTranslationUnit() && !getParentASTContext().getLangOpts().CPlusPlus; for (auto *DC : Contexts) - buildLookupImpl(DC, hasExternalVisibleStorage()); + buildLookupImpl(DC, hasExternalVisibleStorage(), LoadExternalTransparent); // We no longer have any lazy decls. setHasLazyLocalLexicalLookups(false); @@ -1893,7 +1899,8 @@ StoredDeclsMap *DeclContext::buildLookup() { /// declarations contained within DCtx, which will either be this /// DeclContext, a DeclContext linked to it, or a transparent context /// nested within it. -void DeclContext::buildLookupImpl(DeclContext *DCtx, bool Internal) { +void DeclContext::buildLookupImpl(DeclContext *DCtx, bool Internal, + bool LoadExternalTransparent) { for (auto *D : DCtx->noload_decls()) { // Insert this declaration into the lookup structure, but only if // it's semantically within its decl context. Any other decls which @@ -1914,8 +1921,11 @@ void DeclContext::buildLookupImpl(DeclContext *DCtx, bool Internal) { // or inline namespace, add the members of this declaration of that // context (recursively). if (auto *InnerCtx = dyn_cast<DeclContext>(D)) - if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace()) - buildLookupImpl(InnerCtx, Internal); + if (InnerCtx->isTransparentContext() || InnerCtx->isInlineNamespace()) { + if (LoadExternalTransparent && InnerCtx->hasExternalLexicalStorage()) + InnerCtx->LoadLexicalDeclsFromExternalStorage(); + buildLookupImpl(InnerCtx, Internal, LoadExternalTransparent); + } } } diff --git a/clang/test/PCH/c-enum-tu-lookup.c b/clang/test/PCH/c-enum-tu-lookup.c new file mode 100644 index 00000000000000..b9faeae437ec98 --- /dev/null +++ b/clang/test/PCH/c-enum-tu-lookup.c @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 -x c-header -emit-pch -o %t.pch %s +// RUN: %clang_cc1 -include-pch %t.pch -ast-dump-all -ast-dump-lookups %s | FileCheck %s + +#ifndef HEADER +#define HEADER +enum { thrd_success = 4 }; +enum E { A = 1 }; +extern int v; +#else +int x; +// CHECK-DAG: DeclarationName 'thrd_success' +// CHECK-DAG: DeclarationName 'A' +// CHECK-DAG: DeclarationName 'E' +// CHECK-DAG: DeclarationName 'v' +// CHECK-DAG: DeclarationName 'x' +#endif `````````` </details> https://github.com/llvm/llvm-project/pull/246625 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
