[PATCH] D91025: [clangd] Fix locateMacroAt() for macro definition outside preamble

Nathan Ridge via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 7 23:59:24 PST 2020


nridge created this revision.
Herald added subscribers: cfe-commits, usaxena95, kadircet, arphaman.
Herald added a project: clang.
nridge requested review of this revision.
Herald added subscribers: MaskRay, ilya-biryukov.

Fixes https://github.com/clangd/clangd/issues/577


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D91025

Files:
  clang-tools-extra/clangd/SourceCode.cpp
  clang-tools-extra/clangd/unittests/XRefsTests.cpp


Index: clang-tools-extra/clangd/unittests/XRefsTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -1563,6 +1563,14 @@
         }
       )cpp",
 
+      R"cpp(// Macro outside preamble
+        int breakPreamble;
+        #define [[MA^CRO]](X) (X+1)
+        void test() {
+          int x = [[MACRO]]([[MACRO]](1));
+        }
+      )cpp",
+
       R"cpp(
         int [[v^ar]] = 0;
         void foo(int s = [[var]]);
Index: clang-tools-extra/clangd/SourceCode.cpp
===================================================================
--- clang-tools-extra/clangd/SourceCode.cpp
+++ clang-tools-extra/clangd/SourceCode.cpp
@@ -975,17 +975,31 @@
   if (!IdentifierInfo || !IdentifierInfo->hadMacroDefinition())
     return None;
 
-  // Get the definition just before the searched location so that a macro
-  // referenced in a '#undef MACRO' can still be found. Note that we only do
-  // that if Loc is not pointing at start of file.
-  if (SM.getLocForStartOfFile(SM.getFileID(Loc)) != Loc)
-    Loc = Loc.getLocWithOffset(-1);
-  MacroDefinition MacroDef = PP.getMacroDefinitionAtLoc(IdentifierInfo, Loc);
-  if (auto *MI = MacroDef.getMacroInfo())
-    return DefinedMacro{
-        IdentifierInfo->getName(), MI,
-        translatePreamblePatchLocation(MI->getDefinitionLoc(), SM)};
-  return None;
+  // We need to take special case to handle #define and #undef.
+  // Preprocessor::getMacroDefinitionAtLoc() only considers a macro
+  // definition to be in scope *after* the location of the macro name in a
+  // #define that introduces it, and *before* the location of the macro name
+  // in an #undef that undefines it. To handle these cases, we check for
+  // the macro being in scope either just after or just before the location
+  // of the token. In getting the locations before and after, we also take
+  // care to check for start-of-file and end-of-file.
+  FileID FID = SM.getFileID(Loc);
+  auto JustAfterToken =
+      SM.getLocForEndOfFile(FID) == Loc ? Loc : Loc.getLocWithOffset(1);
+  auto *MI =
+      PP.getMacroDefinitionAtLoc(IdentifierInfo, JustAfterToken).getMacroInfo();
+  if (!MI) {
+    auto JustBeforeToken =
+        SM.getLocForStartOfFile(FID) == Loc ? Loc : Loc.getLocWithOffset(-1);
+    MI = PP.getMacroDefinitionAtLoc(IdentifierInfo, JustBeforeToken)
+             .getMacroInfo();
+  }
+  if (!MI) {
+    return None;
+  }
+  return DefinedMacro{
+      IdentifierInfo->getName(), MI,
+      translatePreamblePatchLocation(MI->getDefinitionLoc(), SM)};
 }
 
 llvm::Expected<std::string> Edit::apply() const {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91025.303696.patch
Type: text/x-patch
Size: 2692 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201108/c120a277/attachment.bin>


More information about the cfe-commits mailing list