[llvm-branch-commits] [clang-tools-extra] 02bb1ee - [clangd] Index symbols declared inside export declarations (#234755)
Tobias Hieta via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sun Nov 15 12:05:41 PST 2026
Author: R. Aoto
Date: 2026-11-15T21:04:29+01:00
New Revision: 02bb1ee675b68fc095bf9514bab3d07db3c428e3
URL: https://github.com/llvm/llvm-project/commit/02bb1ee675b68fc095bf9514bab3d07db3c428e3
DIFF: https://github.com/llvm/llvm-project/commit/02bb1ee675b68fc095bf9514bab3d07db3c428e3.diff
LOG: [clangd] Index symbols declared inside export declarations (#234755)
SymbolCollector::shouldCollectSymbol only accepts symbols whose semantic
parent is a translation unit, namespace, linkage spec, enum, record or
one of a few ObjC containers. ExportDecl is a DeclContext and is
returned by getDeclContext(), so every entity declared with `export` or
inside an `export { ... }` block was dropped from the index. Members of
exported classes were still indexed, since their parent is the record.
In C++20 modules code this hides most of the exported interface from the
index: Go to Definition on an exported function stops at the declaration
in the module interface instead of reaching the definition in the
implementation unit, and workspace/symbol does not find it. Rename uses
the same check, so it rejects exported symbols as "not eligible for
indexing" (reported in clangd/clangd#2569).
Use getNonTransparentDeclContext() instead, so declarations inside
transparent contexts (export, extern "C", unscoped enums) are judged by
their enclosing context. Do the same in isIndexedForCodeCompletion, so
these symbols are also offered in code completion. This also removes the
FIXME describing the gap. USRGenerator likewise skips any transparent
context, not only linkage specs, so exported entities keep their
namespace in their USR. Otherwise same-named entities in different
namespaces would share a USR and be merged into one clangd symbol.
(cherry picked from commit 39a19f710ddc054201b0af80a215fb008199731b)
Added:
clang/test/Index/USR/export.cpp
Modified:
clang-tools-extra/clangd/CodeComplete.cpp
clang-tools-extra/clangd/index/SymbolCollector.cpp
clang-tools-extra/clangd/unittests/RenameTests.cpp
clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
clang/lib/UnifiedSymbolResolution/USRGeneration.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clangd/CodeComplete.cpp b/clang-tools-extra/clangd/CodeComplete.cpp
index 1ed97ed4fe4432..53b557f5179894 100644
--- a/clang-tools-extra/clangd/CodeComplete.cpp
+++ b/clang-tools-extra/clangd/CodeComplete.cpp
@@ -2413,10 +2413,9 @@ SignatureHelp signatureHelp(PathRef FileName, Position Pos,
bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx) {
auto InTopLevelScope = [](const NamedDecl &ND) {
- switch (ND.getDeclContext()->getDeclKind()) {
+ switch (ND.getNonTransparentDeclContext()->getDeclKind()) {
case Decl::TranslationUnit:
case Decl::Namespace:
- case Decl::LinkageSpec:
return true;
default:
break;
diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp
index 244fb6f6875514..743ba3a41f2a2a 100644
--- a/clang-tools-extra/clangd/index/SymbolCollector.cpp
+++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp
@@ -523,13 +523,12 @@ bool SymbolCollector::shouldCollectSymbol(const NamedDecl &ND,
// We want most things but not "local" symbols such as symbols inside
// FunctionDecl, BlockDecl, ObjCMethodDecl and OMPDeclareReductionDecl.
- // FIXME: Need a matcher for ExportDecl in order to include symbols declared
- // within an export.
- const auto *DeclCtx = ND.getDeclContext();
+ // Transparent contexts (export, extern "C", unscoped enums) are skipped, as
+ // their declarations belong to the enclosing context.
+ const auto *DeclCtx = ND.getNonTransparentDeclContext();
switch (DeclCtx->getDeclKind()) {
case Decl::TranslationUnit:
case Decl::Namespace:
- case Decl::LinkageSpec:
case Decl::Enum:
case Decl::ObjCProtocol:
case Decl::ObjCInterface:
diff --git a/clang-tools-extra/clangd/unittests/RenameTests.cpp b/clang-tools-extra/clangd/unittests/RenameTests.cpp
index 42279b51230e7c..635f8e53f0a6a7 100644
--- a/clang-tools-extra/clangd/unittests/RenameTests.cpp
+++ b/clang-tools-extra/clangd/unittests/RenameTests.cpp
@@ -1538,6 +1538,46 @@ TEST(RenameTest, ProtobufSymbolIsExcluded) {
testing::HasSubstr("not a supported kind"));
}
+TEST(RenameTest, ExportedSymbols) {
+ // Symbols whose parent is an ExportDecl must be indexable, or rename rejects
+ // them as "not eligible for indexing".
+ const char *Tests[] = {
+ R"cpp(
+ export module m;
+ export int [[f^un]]() { return 32; }
+ int x = [[fun]]();
+ )cpp",
+ R"cpp(
+ export module m;
+ export {
+ struct [[S^truct]] {};
+ }
+ [[Struct]] s;
+ )cpp",
+ R"cpp(
+ export module m;
+ namespace ns {
+ export int [[V^ar]];
+ }
+ int y = ns::[[Var]];
+ )cpp",
+ };
+
+ for (const char *Test : Tests) {
+ SCOPED_TRACE(Test);
+ Annotations Code(Test);
+ TestTU TU = TestTU::withCode(Code.code());
+ TU.Filename = "test.cppm";
+ TU.ExtraArgs = {"-std=c++20", "-xc++-module"};
+ auto AST = TU.build();
+ auto Results =
+ rename({Code.point(), "newName", AST, testPath(TU.Filename)});
+
+ ASSERT_TRUE(bool(Results)) << llvm::toString(Results.takeError());
+ EXPECT_EQ(Results->LocalChanges, Code.ranges());
+ }
+}
+
TEST(RenameTest, PrepareRename) {
Annotations FooH("void func();");
Annotations FooCC(R"cpp(
diff --git a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
index 94116fca3cbb29..0c32e6d7fa8611 100644
--- a/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
+++ b/clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp
@@ -2172,6 +2172,79 @@ TEST_F(SymbolCollectorTest, Concepts) {
qName("A"), hasKind(clang::index::SymbolKind::Concept))));
}
+TEST_F(SymbolCollectorTest, ExportedDeclarations) {
+ const char *Main = R"cpp(
+ export module m;
+ export void f();
+ export int v;
+ export {
+ void g();
+ struct S { void m(); };
+ }
+ namespace ns {
+ export void h();
+ void not_exported();
+ }
+ )cpp";
+
+ runSymbolCollector("", Main, {"-std=c++20", "-xc++-module"});
+ EXPECT_THAT(Symbols,
+ UnorderedElementsAre(qName("f"), qName("v"), qName("g"),
+ qName("S"), qName("S::m"), qName("ns"),
+ qName("ns::h"), qName("ns::not_exported")));
+}
+
+TEST_F(SymbolCollectorTest, ExportedDeclarationsForCodeCompletion) {
+ // Symbols declared in the main file are never used for code completion, so
+ // the exported declarations live in a header included into the purview.
+ InMemoryFileSystem->addFile(testPath("exports.h"), 0,
+ llvm::MemoryBuffer::getMemBuffer(R"cpp(
+ export void f();
+ export {
+ struct S { void m(); };
+ enum E { A };
+ }
+ namespace ns {
+ export void h();
+ }
+ )cpp"));
+ const char *Main = R"cpp(
+ export module m;
+ #include "exports.h"
+ )cpp";
+
+ runSymbolCollector("", Main,
+ {"-std=c++20", "-xc++-module", "-I", testRoot()});
+ EXPECT_THAT(Symbols, UnorderedElementsAre(
+ AllOf(qName("f"), forCodeCompletion(true)),
+ AllOf(qName("S"), forCodeCompletion(true)),
+ AllOf(qName("S::m"), forCodeCompletion(false)),
+ AllOf(qName("E"), forCodeCompletion(true)),
+ AllOf(qName("A"), forCodeCompletion(true)),
+ AllOf(qName("ns"), forCodeCompletion(true)),
+ AllOf(qName("ns::h"), forCodeCompletion(true))));
+}
+
+TEST_F(SymbolCollectorTest, ExportedDeclarationsHaveDistinctIDs) {
+ // The enclosing namespace must be part of an exported symbol's USR, or these
+ // functions would share one SymbolID and be merged.
+ const char *Main = R"cpp(
+ export module m;
+ void f();
+ namespace a {
+ export void f();
+ }
+ namespace b {
+ export void f();
+ }
+ )cpp";
+
+ runSymbolCollector("", Main, {"-std=c++20", "-xc++-module"});
+ EXPECT_THAT(Symbols,
+ UnorderedElementsAre(qName("f"), qName("a"), qName("a::f"),
+ qName("b"), qName("b::f")));
+}
+
TEST_F(SymbolCollectorTest, IncludeHeaderForwardDecls) {
CollectorOpts.CollectIncludePath = true;
const std::string Header = R"cpp(#pragma once
diff --git a/clang/lib/UnifiedSymbolResolution/USRGeneration.cpp b/clang/lib/UnifiedSymbolResolution/USRGeneration.cpp
index 5f689ad32159b2..b39a36d21a766e 100644
--- a/clang/lib/UnifiedSymbolResolution/USRGeneration.cpp
+++ b/clang/lib/UnifiedSymbolResolution/USRGeneration.cpp
@@ -227,7 +227,7 @@ bool USRGenerator::ShouldGenerateLocation(const NamedDecl *D) {
void USRGenerator::VisitDeclContext(const DeclContext *DC) {
if (const NamedDecl *D = dyn_cast<NamedDecl>(DC))
Visit(D);
- else if (isa<LinkageSpecDecl>(DC)) // Linkage specs are transparent in USRs.
+ else if (DC->isTransparentContext()) // e.g. linkage specs and exports.
VisitDeclContext(DC->getParent());
}
diff --git a/clang/test/Index/USR/export.cpp b/clang/test/Index/USR/export.cpp
new file mode 100644
index 00000000000000..1d1983c789bf65
--- /dev/null
+++ b/clang/test/Index/USR/export.cpp
@@ -0,0 +1,18 @@
+// RUN: c-index-test core -print-source-symbols -- -std=c++20 -xc++-module %s | FileCheck %s
+
+// Export declarations are skipped in USRs for enclosed items.
+export module m;
+
+namespace ns {
+export void f();
+// CHECK: [[@LINE-1]]:13 | function/C | f | c:@N at ns@F at f# |
+export {
+int X;
+// CHECK: [[@LINE-1]]:5 | variable/C | X | c:@N at ns@X |
+}
+export namespace inner {
+// CHECK: [[@LINE-1]]:18 | namespace/C++ | inner | c:@N at ns@N at inner |
+void g();
+// CHECK: [[@LINE-1]]:6 | function/C | g | c:@N at ns@N at inner@F at g# |
+}
+}
More information about the llvm-branch-commits
mailing list