[clang-tools-extra] r373889 - [clangd] Collect missing macro references.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 7 03:10:31 PDT 2019


Author: hokein
Date: Mon Oct  7 03:10:31 2019
New Revision: 373889

URL: http://llvm.org/viewvc/llvm-project?rev=373889&view=rev
Log:
[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

Modified:
    clang-tools-extra/trunk/clangd/CollectMacros.h
    clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp

Modified: clang-tools-extra/trunk/clangd/CollectMacros.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/CollectMacros.h?rev=373889&r1=373888&r2=373889&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/CollectMacros.h (original)
+++ clang-tools-extra/trunk/clangd/CollectMacros.h Mon Oct  7 03:10:31 2019
@@ -25,7 +25,8 @@ struct MainFileMacros {
   std::vector<Range> Ranges;
 };
 
-/// Collects macro definitions and expansions in the main file. It is used to:
+/// Collects macro references (e.g. definitions, expansions) in the main file.
+/// It is used to:
 ///  - collect macros in the preamble section of the main file (in Preamble.cpp)
 ///  - collect macros after the preamble of the main file (in ParsedAST.cpp)
 class CollectMainFileMacros : public PPCallbacks {
@@ -49,6 +50,27 @@ public:
     add(MacroName, MD.getMacroInfo());
   }
 
+  void MacroUndefined(const clang::Token &MacroName,
+                      const clang::MacroDefinition &MD,
+                      const clang::MacroDirective *Undef) override {
+    add(MacroName, MD.getMacroInfo());
+  }
+
+  void Ifdef(SourceLocation Loc, const Token &MacroName,
+             const MacroDefinition &MD) override {
+    add(MacroName, MD.getMacroInfo());
+  }
+
+  void Ifndef(SourceLocation Loc, const Token &MacroName,
+              const MacroDefinition &MD) override {
+    add(MacroName, MD.getMacroInfo());
+  }
+
+  void Defined(const Token &MacroName, const MacroDefinition &MD,
+               SourceRange Range) override {
+    add(MacroName, MD.getMacroInfo());
+  }
+
 private:
   void add(const Token &MacroNameTok, const MacroInfo *MI) {
     if (!InMainFile)
@@ -57,7 +79,7 @@ private:
     if (Loc.isMacroID())
       return;
 
-    if (auto Range = getTokenRange(SM, LangOpts, MacroNameTok.getLocation())) {
+    if (auto Range = getTokenRange(SM, LangOpts, Loc)) {
       Out.Names.insert(MacroNameTok.getIdentifierInfo()->getName());
       Out.Ranges.push_back(*Range);
     }

Modified: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp?rev=373889&r1=373888&r2=373889&view=diff
==============================================================================
--- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp (original)
+++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp Mon Oct  7 03:10:31 2019
@@ -476,6 +476,20 @@ TEST(SemanticHighlighting, GetsCorrectTo
         $Macro[[assert]]($Variable[[x]] != $Function[[f]]());
       }
     )cpp",
+      // highlighting all macro references
+      R"cpp(
+      #ifndef $Macro[[name]]
+      #define $Macro[[name]]
+      #endif
+
+      #define $Macro[[test]]
+      #undef $Macro[[test]]
+      #ifdef $Macro[[test]]
+      #endif
+
+      #if defined($Macro[[test]])
+      #endif
+    )cpp",
       R"cpp(
       struct $Class[[S]] {
         $Primitive[[float]] $Field[[Value]];




More information about the cfe-commits mailing list