Skip to content

Commit 2fa81d2

Browse files
committed
[clangd] Collect missing macro references.
Summary: Semantic highlghting is missing a few macro references. Reviewers: ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D68458 llvm-svn: 373889
1 parent 6942327 commit 2fa81d2

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

clang-tools-extra/clangd/CollectMacros.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ struct MainFileMacros {
2525
std::vector<Range> Ranges;
2626
};
2727

28-
/// Collects macro definitions and expansions in the main file. It is used to:
28+
/// Collects macro references (e.g. definitions, expansions) in the main file.
29+
/// It is used to:
2930
/// - collect macros in the preamble section of the main file (in Preamble.cpp)
3031
/// - collect macros after the preamble of the main file (in ParsedAST.cpp)
3132
class CollectMainFileMacros : public PPCallbacks {
@@ -49,6 +50,27 @@ class CollectMainFileMacros : public PPCallbacks {
4950
add(MacroName, MD.getMacroInfo());
5051
}
5152

53+
void MacroUndefined(const clang::Token &MacroName,
54+
const clang::MacroDefinition &MD,
55+
const clang::MacroDirective *Undef) override {
56+
add(MacroName, MD.getMacroInfo());
57+
}
58+
59+
void Ifdef(SourceLocation Loc, const Token &MacroName,
60+
const MacroDefinition &MD) override {
61+
add(MacroName, MD.getMacroInfo());
62+
}
63+
64+
void Ifndef(SourceLocation Loc, const Token &MacroName,
65+
const MacroDefinition &MD) override {
66+
add(MacroName, MD.getMacroInfo());
67+
}
68+
69+
void Defined(const Token &MacroName, const MacroDefinition &MD,
70+
SourceRange Range) override {
71+
add(MacroName, MD.getMacroInfo());
72+
}
73+
5274
private:
5375
void add(const Token &MacroNameTok, const MacroInfo *MI) {
5476
if (!InMainFile)
@@ -57,7 +79,7 @@ class CollectMainFileMacros : public PPCallbacks {
5779
if (Loc.isMacroID())
5880
return;
5981

60-
if (auto Range = getTokenRange(SM, LangOpts, MacroNameTok.getLocation())) {
82+
if (auto Range = getTokenRange(SM, LangOpts, Loc)) {
6183
Out.Names.insert(MacroNameTok.getIdentifierInfo()->getName());
6284
Out.Ranges.push_back(*Range);
6385
}

clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,20 @@ TEST(SemanticHighlighting, GetsCorrectTokens) {
475475
$Macro[[assert]]($Variable[[x]] != $Variable[[y]]);
476476
$Macro[[assert]]($Variable[[x]] != $Function[[f]]());
477477
}
478+
)cpp",
479+
// highlighting all macro references
480+
R"cpp(
481+
#ifndef $Macro[[name]]
482+
#define $Macro[[name]]
483+
#endif
484+
485+
#define $Macro[[test]]
486+
#undef $Macro[[test]]
487+
#ifdef $Macro[[test]]
488+
#endif
489+
490+
#if defined($Macro[[test]])
491+
#endif
478492
)cpp",
479493
R"cpp(
480494
struct $Class[[S]] {

0 commit comments

Comments
 (0)