[clang-tools-extra] [clangd] Omit C++ access specifiers from C hovers (PR #239877)
James Cowsert via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 6 17:58:56 PST 2026
https://github.com/J-Cowsert created https://github.com/llvm/llvm-project/pull/239877
This PR addresses the access-specifier portion of clangd/clangd#2700.
Hovering over a C struct member currently shows C++ syntax such as
`public: int location`.
Only populate `HoverInfo::AccessSpecifier` in C++ mode so C hovers
show `int location` while preserving C++ access labels.
The issue discusses `::` in the `// In ...` scope annotation,
but the discussion leaves open whether that notation should change.
This PR addresses only the access-specifier behavior and
leaves scope formatting unchanged.
Validation:
- Added regression coverage for ordinary C structs, anonymous nested
C structs, and a C++ control case.
- All 37 `Hover.*` tests passed after rebasing and rebuilding.
>From 7c0fad71268080434df7900a1dcd54f0d824b862 Mon Sep 17 00:00:00 2001
From: J-Cowsert <cowsert.jsec at proton.me>
Date: Fri, 6 Nov 2026 18:57:52 -0600
Subject: [PATCH] [clangd] Omit C++ access specifiers from C hovers
---
clang-tools-extra/clangd/Hover.cpp | 3 +-
.../clangd/unittests/HoverTests.cpp | 44 +++++++++++++++++++
2 files changed, 46 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clangd/Hover.cpp b/clang-tools-extra/clangd/Hover.cpp
index b7e851e805e1f..1b62fe907dd64 100644
--- a/clang-tools-extra/clangd/Hover.cpp
+++ b/clang-tools-extra/clangd/Hover.cpp
@@ -666,7 +666,8 @@ HoverInfo getHoverContents(const NamedDecl *D, const PrintingPolicy &PP,
HoverInfo HI;
auto &Ctx = D->getASTContext();
- HI.AccessSpecifier = getAccessSpelling(D->getAccess()).str();
+ if (Ctx.getLangOpts().CPlusPlus)
+ HI.AccessSpecifier = getAccessSpelling(D->getAccess()).str();
HI.NamespaceScope = getNamespaceScope(D);
if (!HI.NamespaceScope->empty())
HI.NamespaceScope->append("::");
diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp
index 95e0889b228a3..d72536704fa5c 100644
--- a/clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -1645,6 +1645,50 @@ TEST(Hover, DefinitionLanuage) {
}
}
+TEST(Hover, AccessSpecifierByLanguage) {
+ struct {
+ const char *const Code;
+ const std::string ClangLanguageFlag;
+ const char *const ExpectedAccessSpecifier;
+ } Cases[] = {
+ {R"cpp(
+ struct Waldo {
+ int [[loca^tion]];
+ };
+ )cpp",
+ "-xc", ""},
+ {R"cpp(
+ struct Waldo {
+ int [[loca^tion]];
+ };
+ )cpp",
+ "-xc++", "public"},
+ {R"cpp(
+ struct Waldo {
+ struct {
+ int [[loca^tion]];
+ } inner;
+ };
+ )cpp",
+ "-xc", ""},
+ };
+ for (const auto &Case : Cases) {
+ SCOPED_TRACE(Case.Code);
+ SCOPED_TRACE(Case.ClangLanguageFlag);
+
+ Annotations T(Case.Code);
+ TestTU TU = TestTU::withCode(T.code());
+ if (!Case.ClangLanguageFlag.empty())
+ TU.ExtraArgs.push_back(Case.ClangLanguageFlag);
+ auto AST = TU.build();
+
+ auto H = getHover(AST, T.point(), format::getLLVMStyle(), nullptr);
+ ASSERT_TRUE(H);
+
+ EXPECT_EQ(H->AccessSpecifier, Case.ExpectedAccessSpecifier);
+ }
+}
+
TEST(Hover, CallPassType) {
const llvm::StringRef CodePrefix = R"cpp(
class Base {};
More information about the cfe-commits
mailing list