https://github.com/Quelfth created 
https://github.com/llvm/llvm-project/pull/208951

I've implemented a semantic token modifier in Clangd to tell whether a macro is 
ObjectStyle or FunctionStyle.  It doesn't pass all the tests though when I ran 
them locally.  I was told to create a draft PR to trigger a CI run.

>From 4f8aca277aec39fe252da3ff2942f86b6ddbf188 Mon Sep 17 00:00:00 2001
From: Quelfth <[email protected]>
Date: Wed, 29 Apr 2026 19:27:11 -0700
Subject: [PATCH] initial implementation

---
 clang-tools-extra/clangd/CollectMacros.cpp        | 5 +++--
 clang-tools-extra/clangd/CollectMacros.h          | 1 +
 clang-tools-extra/clangd/SemanticHighlighting.cpp | 5 +++++
 clang-tools-extra/clangd/SemanticHighlighting.h   | 1 +
 4 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clangd/CollectMacros.cpp 
b/clang-tools-extra/clangd/CollectMacros.cpp
index 1e7d765f0b6f1..9bb09da7ac2c9 100644
--- a/clang-tools-extra/clangd/CollectMacros.cpp
+++ b/clang-tools-extra/clangd/CollectMacros.cpp
@@ -40,10 +40,11 @@ void CollectMainFileMacros::add(const Token &MacroNameTok, 
const MacroInfo *MI,
   Out.Names.insert(Name);
   size_t Start = SM.getFileOffset(Loc);
   size_t End = SM.getFileOffset(MacroNameTok.getEndLoc());
+  bool IsFunctionLike = MI->isFunctionLike();
   if (auto SID = getSymbolID(Name, MI, SM))
-    Out.MacroRefs[SID].push_back({Start, End, IsDefinition, InIfCondition});
+    Out.MacroRefs[SID].push_back({Start, End, IsDefinition, InIfCondition, 
IsFunctionLike});
   else
-    Out.UnknownMacros.push_back({Start, End, IsDefinition, InIfCondition});
+    Out.UnknownMacros.push_back({Start, End, IsDefinition, InIfCondition, 
IsFunctionLike});
 }
 
 void CollectMainFileMacros::FileChanged(SourceLocation Loc, FileChangeReason,
diff --git a/clang-tools-extra/clangd/CollectMacros.h 
b/clang-tools-extra/clangd/CollectMacros.h
index 20a3fc24d759c..94d0ce2d54182 100644
--- a/clang-tools-extra/clangd/CollectMacros.h
+++ b/clang-tools-extra/clangd/CollectMacros.h
@@ -30,6 +30,7 @@ struct MacroOccurrence {
   bool IsDefinition;
   // True if the occurence is used in a conditional directive, e.g. #ifdef 
MACRO
   bool InConditionalDirective;
+  bool IsFunctionLike;
 
   CharSourceRange toSourceRange(const SourceManager &SM) const;
   Range toRange(const SourceManager &SM) const;
diff --git a/clang-tools-extra/clangd/SemanticHighlighting.cpp 
b/clang-tools-extra/clangd/SemanticHighlighting.cpp
index 79446a166d0d8..3a7707d1bb95c 100644
--- a/clang-tools-extra/clangd/SemanticHighlighting.cpp
+++ b/clang-tools-extra/clangd/SemanticHighlighting.cpp
@@ -1100,6 +1100,8 @@ getSemanticHighlightings(ParsedAST &AST, bool 
IncludeInactiveRegionTokens) {
     T.addModifier(HighlightingModifier::GlobalScope);
     if (M.IsDefinition)
       T.addModifier(HighlightingModifier::Declaration);
+    if (M.IsFunctionLike)
+      T.addModifier(HighlightingModifier::FunctionLike);
   };
   for (const auto &SIDToRefs : AST.getMacros().MacroRefs)
     for (const auto &M : SIDToRefs.second)
@@ -1227,6 +1229,7 @@ highlightingModifierFromString(llvm::StringRef Name) {
       {"ConstructorOrDestructor",
        HighlightingModifier::ConstructorOrDestructor},
       {"UserDefined", HighlightingModifier::UserDefined},
+      {"FunctionLike", HighlightingModifier::FunctionLike},
       {"FunctionScope", HighlightingModifier::FunctionScope},
       {"ClassScope", HighlightingModifier::ClassScope},
       {"FileScope", HighlightingModifier::FileScope},
@@ -1397,6 +1400,8 @@ llvm::StringRef 
toSemanticTokenModifier(HighlightingModifier Modifier) {
     return "constructorOrDestructor"; // nonstandard
   case HighlightingModifier::UserDefined:
     return "userDefined"; // nonstandard
+  case HighlightingModifier::FunctionLike:
+    return "functionLike"; // nonstandard
   case HighlightingModifier::FunctionScope:
     return "functionScope"; // nonstandard
   case HighlightingModifier::ClassScope:
diff --git a/clang-tools-extra/clangd/SemanticHighlighting.h 
b/clang-tools-extra/clangd/SemanticHighlighting.h
index 59d742b83ee52..45019c98a91b7 100644
--- a/clang-tools-extra/clangd/SemanticHighlighting.h
+++ b/clang-tools-extra/clangd/SemanticHighlighting.h
@@ -80,6 +80,7 @@ enum class HighlightingModifier {
   UsedAsMutablePointer,
   ConstructorOrDestructor,
   UserDefined,
+  FunctionLike,
 
   FunctionScope,
   ClassScope,

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to