https://github.com/anonymouspc updated https://github.com/llvm/llvm-project/pull/175495
>From feaf0fd9816e3126a3e9e42328ec75ff268da315 Mon Sep 17 00:00:00 2001 From: anonymous <[email protected]> Date: Mon, 12 Jan 2026 15:24:13 +0800 Subject: [PATCH 1/6] Add new modifier `CommandLineDefined`. Macros which defined in command line `-D` (rather than `#define`) have new modifier `commandLineDefined`. --- clang-tools-extra/clangd/CollectMacros.cpp | 15 +++++++++++++-- clang-tools-extra/clangd/CollectMacros.h | 3 +++ clang-tools-extra/clangd/SemanticHighlighting.cpp | 5 +++++ clang-tools-extra/clangd/SemanticHighlighting.h | 1 + 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clangd/CollectMacros.cpp b/clang-tools-extra/clangd/CollectMacros.cpp index 1e7d765f0b6f1..2704dbc869ca8 100644 --- a/clang-tools-extra/clangd/CollectMacros.cpp +++ b/clang-tools-extra/clangd/CollectMacros.cpp @@ -11,6 +11,7 @@ #include "Protocol.h" #include "SourceCode.h" #include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" #include "clang/Tooling/Syntax/Tokens.h" #include "llvm/ADT/STLExtras.h" #include <cstddef> @@ -40,10 +41,20 @@ 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 IsCommandLineDefined = false; + if (MI) { + const auto DefLoc =/* */ MI->getDefinitionLoc(); + if (DefLoc.isValid()) + IsCommandLineDefined = SM.isWrittenInCommandLineFile(DefLoc); + } + 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, IsCommandLineDefined}); else - Out.UnknownMacros.push_back({Start, End, IsDefinition, InIfCondition}); + Out.UnknownMacros.push_back( + {Start, End, IsDefinition, InIfCondition, IsCommandLineDefined}); } void CollectMainFileMacros::FileChanged(SourceLocation Loc, FileChangeReason, diff --git a/clang-tools-extra/clangd/CollectMacros.h b/clang-tools-extra/clangd/CollectMacros.h index 20a3fc24d759c..b88d9e8ad0443 100644 --- a/clang-tools-extra/clangd/CollectMacros.h +++ b/clang-tools-extra/clangd/CollectMacros.h @@ -30,6 +30,9 @@ struct MacroOccurrence { bool IsDefinition; // True if the occurence is used in a conditional directive, e.g. #ifdef MACRO bool InConditionalDirective; + // True if the macro is defined via command line options (e.g. -D...) + // rather than in a source/header file. + bool IsCommandLineDefined; 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 ab720ebe6b47f..79e342f5143b9 100644 --- a/clang-tools-extra/clangd/SemanticHighlighting.cpp +++ b/clang-tools-extra/clangd/SemanticHighlighting.cpp @@ -1190,6 +1190,8 @@ getSemanticHighlightings(ParsedAST &AST, bool IncludeInactiveRegionTokens) { auto &T = Builder.addToken(M.toRange(C.getSourceManager()), HighlightingKind::Macro); T.addModifier(HighlightingModifier::GlobalScope); + if (M.IsCommandLineDefined) + T.addModifier(HighlightingModifier::CommandLineDefined); if (M.IsDefinition) T.addModifier(HighlightingModifier::Declaration); }; @@ -1319,6 +1321,7 @@ highlightingModifierFromString(llvm::StringRef Name) { {"ConstructorOrDestructor", HighlightingModifier::ConstructorOrDestructor}, {"UserDefined", HighlightingModifier::UserDefined}, + {"CommandLineDefined", HighlightingModifier::CommandLineDefined}, {"FunctionScope", HighlightingModifier::FunctionScope}, {"ClassScope", HighlightingModifier::ClassScope}, {"FileScope", HighlightingModifier::FileScope}, @@ -1489,6 +1492,8 @@ llvm::StringRef toSemanticTokenModifier(HighlightingModifier Modifier) { return "constructorOrDestructor"; // nonstandard case HighlightingModifier::UserDefined: return "userDefined"; // nonstandard + case HighlightingModifier::CommandLineDefined: + return "commandLine"; // 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..1ecfe41fc600a 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, + CommandLineDefined, FunctionScope, ClassScope, >From 99d902774ffd5e917b9ef85756d01f82e3ba6519 Mon Sep 17 00:00:00 2001 From: anonymous <[email protected]> Date: Mon, 12 Jan 2026 15:24:28 +0800 Subject: [PATCH 2/6] Update tests to the new modifier `CommandLineDefined`. --- clang-tools-extra/clangd/test/initialize-params.test | 1 + .../clangd/unittests/SemanticHighlightingTests.cpp | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/clang-tools-extra/clangd/test/initialize-params.test b/clang-tools-extra/clangd/test/initialize-params.test index d976b7d19fd0e..9f7ab485d4514 100644 --- a/clang-tools-extra/clangd/test/initialize-params.test +++ b/clang-tools-extra/clangd/test/initialize-params.test @@ -75,6 +75,7 @@ # CHECK-NEXT: "usedAsMutablePointer", # CHECK-NEXT: "constructorOrDestructor", # CHECK-NEXT: "userDefined", +# CHECK-NEXT: "commandLineDefined", # CHECK-NEXT: "functionScope", # CHECK-NEXT: "classScope", # CHECK-NEXT: "fileScope", diff --git a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp index 94cecce1f038c..b6cdd8be277a5 100644 --- a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp +++ b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp @@ -1201,6 +1201,15 @@ TEST(SemanticHighlighting, ScopeModifiers) { checkHighlightings(Test, {}, ScopeModifierMask); } +TEST(SemanticHighlighting, CommandLineMacros) { + checkHighlightings(R"cpp( + int $Variable_def_globalScope[[x]] = $Macro_globalScope[[CMD_MACRO]]; + )cpp", + /*AdditionalFiles=*/{}, + /*ModifierMask=*/~(1 << unsigned(HighlightingModifier::CommandLineDefined)), + /*AdditionalArgs=*/{"-DCMD_MACRO=1"}); +} + // Ranges are highlighted as variables, unless highlighted as $Function etc. std::vector<HighlightingToken> tokens(llvm::StringRef MarkedText) { Annotations A(MarkedText); >From 8ca8873a6b7aa011af6be79467bc46ca7821ffc5 Mon Sep 17 00:00:00 2001 From: anonymous <[email protected]> Date: Mon, 12 Jan 2026 15:27:59 +0800 Subject: [PATCH 3/6] Update release notes. --- clang-tools-extra/docs/ReleaseNotes.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 7747c5d0e96a7..5c2259c611920 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -105,6 +105,11 @@ Diagnostics Semantic Highlighting ^^^^^^^^^^^^^^^^^^^^^ +- ``clangd`` now exposes a ``commandLineDefined`` semantic token modifier + for macros that are defined via compiler command-line options + (for example [-D] flags), allowing clients to distinguish them + from macros defined in source files or headers. + Compile flags ^^^^^^^^^^^^^ >From f9a7c99f0cf7e63d6a87cbf888e10f99799f76a0 Mon Sep 17 00:00:00 2001 From: anonymous <[email protected]> Date: Mon, 12 Jan 2026 15:29:12 +0800 Subject: [PATCH 4/6] clang-format --- clang-tools-extra/clangd/CollectMacros.cpp | 2 +- .../clangd/unittests/SemanticHighlightingTests.cpp | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/clangd/CollectMacros.cpp b/clang-tools-extra/clangd/CollectMacros.cpp index 2704dbc869ca8..172bdb87f4667 100644 --- a/clang-tools-extra/clangd/CollectMacros.cpp +++ b/clang-tools-extra/clangd/CollectMacros.cpp @@ -44,7 +44,7 @@ void CollectMainFileMacros::add(const Token &MacroNameTok, const MacroInfo *MI, bool IsCommandLineDefined = false; if (MI) { - const auto DefLoc =/* */ MI->getDefinitionLoc(); + const auto DefLoc = MI->getDefinitionLoc(); if (DefLoc.isValid()) IsCommandLineDefined = SM.isWrittenInCommandLineFile(DefLoc); } diff --git a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp index b6cdd8be277a5..eab729d94df96 100644 --- a/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp +++ b/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp @@ -1205,9 +1205,10 @@ TEST(SemanticHighlighting, CommandLineMacros) { checkHighlightings(R"cpp( int $Variable_def_globalScope[[x]] = $Macro_globalScope[[CMD_MACRO]]; )cpp", - /*AdditionalFiles=*/{}, - /*ModifierMask=*/~(1 << unsigned(HighlightingModifier::CommandLineDefined)), - /*AdditionalArgs=*/{"-DCMD_MACRO=1"}); + /*AdditionalFiles=*/{}, + /*ModifierMask=*/ + ~(1 << unsigned(HighlightingModifier::CommandLineDefined)), + /*AdditionalArgs=*/{"-DCMD_MACRO=1"}); } // Ranges are highlighted as variables, unless highlighted as $Function etc. >From 97d2c2a7801bb8bebc58b46350d3a8c21f67a668 Mon Sep 17 00:00:00 2001 From: anonymous <[email protected]> Date: Mon, 12 Jan 2026 15:50:58 +0800 Subject: [PATCH 5/6] Typo fix --- clang-tools-extra/clangd/SemanticHighlighting.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang-tools-extra/clangd/SemanticHighlighting.cpp b/clang-tools-extra/clangd/SemanticHighlighting.cpp index 79e342f5143b9..f27c6bd3e282b 100644 --- a/clang-tools-extra/clangd/SemanticHighlighting.cpp +++ b/clang-tools-extra/clangd/SemanticHighlighting.cpp @@ -1493,7 +1493,7 @@ llvm::StringRef toSemanticTokenModifier(HighlightingModifier Modifier) { case HighlightingModifier::UserDefined: return "userDefined"; // nonstandard case HighlightingModifier::CommandLineDefined: - return "commandLine"; // nonstandard + return "commandLineDefined"; // nonstandard case HighlightingModifier::FunctionScope: return "functionScope"; // nonstandard case HighlightingModifier::ClassScope: >From 6fc87e9791a75114827656b79b8d69b5229d658b Mon Sep 17 00:00:00 2001 From: anonymous <[email protected]> Date: Mon, 12 Jan 2026 16:06:09 +0800 Subject: [PATCH 6/6] Update tests that should have a new modifier-mask number. --- clang-tools-extra/clangd/test/semantic-tokens.test | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/clang-tools-extra/clangd/test/semantic-tokens.test b/clang-tools-extra/clangd/test/semantic-tokens.test index e5e8b749caeb3..8a5d4a6ab8b26 100644 --- a/clang-tools-extra/clangd/test/semantic-tokens.test +++ b/clang-tools-extra/clangd/test/semantic-tokens.test @@ -23,7 +23,7 @@ # CHECK-NEXT: 4, # CHECK-NEXT: 1, # CHECK-NEXT: 0, -# CHECK-NEXT: 131075 +# CHECK-NEXT: 262147 # CHECK-NEXT: ], # CHECK-NEXT: "resultId": "1" # CHECK-NEXT: } @@ -49,7 +49,7 @@ # CHECK-NEXT: 4, # CHECK-NEXT: 1, # CHECK-NEXT: 0, -# CHECK-NEXT: 131075 +# CHECK-NEXT: 262147 # CHECK-NEXT: ], # Inserted at position 1 # CHECK-NEXT: "deleteCount": 0, @@ -72,12 +72,12 @@ # CHECK-NEXT: 4, # CHECK-NEXT: 1, # CHECK-NEXT: 0, -# CHECK-NEXT: 131075, +# CHECK-NEXT: 262147, # CHECK-NEXT: 1, # CHECK-NEXT: 4, # CHECK-NEXT: 1, # CHECK-NEXT: 0, -# CHECK-NEXT: 131075 +# CHECK-NEXT: 262147 # CHECK-NEXT: ], # CHECK-NEXT: "resultId": "3" # CHECK-NEXT: } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
