[PATCH] D68458: [clangd] Collect missing macro references.

Haojian Wu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 7 22:08:06 PDT 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rL373889: [clangd] Collect missing macro references. (authored by hokein, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D68458?vs=223191&id=223730#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D68458/new/

https://reviews.llvm.org/D68458

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


Index: clang-tools-extra/trunk/clangd/CollectMacros.h
===================================================================
--- clang-tools-extra/trunk/clangd/CollectMacros.h
+++ clang-tools-extra/trunk/clangd/CollectMacros.h
@@ -25,7 +25,8 @@
   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 @@
     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 @@
     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);
     }
Index: clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp
===================================================================
--- clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp
+++ clang-tools-extra/trunk/clangd/unittests/SemanticHighlightingTests.cpp
@@ -476,6 +476,20 @@
         $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]];


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D68458.223730.patch
Type: text/x-patch
Size: 2636 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191008/789450f0/attachment.bin>


More information about the llvm-commits mailing list