[llvm-branch-commits] [clang] [clang-tools-extra] release/23.x: [clangd] Index symbols declared inside export declarations (#234755) (PR #239936)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sat Nov 7 00:51:55 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clangd
Author: llvmbot
<details>
<summary>Changes</summary>
Backport 39a19f710ddc054201b0af80a215fb008199731b
Requested by: @<!-- -->aotodev
---
Full diff: https://github.com/llvm/llvm-project/pull/239936.diff
6 Files Affected:
- (modified) clang-tools-extra/clangd/CodeComplete.cpp (+1-2)
- (modified) clang-tools-extra/clangd/index/SymbolCollector.cpp (+3-4)
- (modified) clang-tools-extra/clangd/unittests/RenameTests.cpp (+40)
- (modified) clang-tools-extra/clangd/unittests/SymbolCollectorTests.cpp (+73)
- (modified) clang/lib/UnifiedSymbolResolution/USRGeneration.cpp (+1-1)
- (added) clang/test/Index/USR/export.cpp (+18)
``````````diff
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# |
+}
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/239936
More information about the llvm-branch-commits
mailing list