https://github.com/anonymouspc created https://github.com/llvm/llvm-project/pull/175495
### Feature This PR added a new enum `HighlightingModifier::CommandLineDefined`. ### Reason Macros can be either - defined in header/source `#define`, or - defined via command line `-Dxxx=yyy` Before this PR, the macros above are all only labelled as `GlobalScope`. After this PR, macros who were defined via `-Dxxx=yyy` have an additional label `CommandLineDefined` for users to distinguish them from *in-file-defined* ones. >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/4] 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/4] 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/4] 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/4] 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. _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
