[clang-tools-extra] [clangd] Omit C++ access specifiers from C hovers (PR #239877)

James Cowsert via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 7 14:42:30 PST 2026


https://github.com/J-Cowsert updated https://github.com/llvm/llvm-project/pull/239877

>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 1/2] [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 b7e851e805e1fb..1b62fe907dd642 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 95e0889b228a38..d72536704fa5c8 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 {};

>From 55485733ec51d2f9e0d010174c87da45dc2026ef Mon Sep 17 00:00:00 2001
From: J-Cowsert <cowsert.jsec at proton.me>
Date: Sat, 7 Nov 2026 15:58:16 -0600
Subject: [PATCH 2/2] [clangd] Extend hover access tests and remove unused
 ranges

Add Objective-C and Objective-C++ test coverage for struct fields and
native ivars. Remove unused range annotations from the test cases.
---
 .../clangd/unittests/HoverTests.cpp           | 36 +++++++++++++++++--
 1 file changed, 33 insertions(+), 3 deletions(-)

diff --git a/clang-tools-extra/clangd/unittests/HoverTests.cpp b/clang-tools-extra/clangd/unittests/HoverTests.cpp
index d72536704fa5c8..c694959dc8b304 100644
--- a/clang-tools-extra/clangd/unittests/HoverTests.cpp
+++ b/clang-tools-extra/clangd/unittests/HoverTests.cpp
@@ -1653,24 +1653,54 @@ TEST(Hover, AccessSpecifierByLanguage) {
   } Cases[] = {
       {R"cpp(
         struct Waldo {
-          int [[loca^tion]];
+          int loca^tion;
         };
       )cpp",
        "-xc", ""},
       {R"cpp(
         struct Waldo {
-          int [[loca^tion]];
+          int loca^tion;
         };
       )cpp",
        "-xc++", "public"},
       {R"cpp(
         struct Waldo {
           struct {
-            int [[loca^tion]];
+            int loca^tion;
           } inner;
         };
       )cpp",
        "-xc", ""},
+      {R"cpp(
+        struct Waldo {
+          int loca^tion;
+        };
+      )cpp",
+       "-xobjective-c", ""},
+      {R"cpp(
+        struct Waldo {
+          int loca^tion;
+        };
+      )cpp",
+       "-xobjective-c++", "public"},
+      // Hover currently does not retrieve ivar access specifiers in either
+      // Objective-C or Objective-C++, so no label is expected.
+      {R"cpp(
+        @interface Waldo {
+        @public
+          int loca^tion;
+        }
+        @end
+      )cpp",
+       "-xobjective-c", ""},
+      {R"cpp(
+        @interface Waldo {
+        @public
+          int loca^tion;
+        }
+        @end
+      )cpp",
+       "-xobjective-c++", ""},
   };
   for (const auto &Case : Cases) {
     SCOPED_TRACE(Case.Code);



More information about the cfe-commits mailing list