[clang/Lexer] Enhance `Lexer::getImmediateMacroNameForDiagnostics` to return a result...
authorArgyrios Kyrtzidis <kyrtzidis@apple.com>
Thu, 15 Dec 2022 21:33:17 +0000 (13:33 -0800)
committerArgyrios Kyrtzidis <kyrtzidis@apple.com>
Fri, 16 Dec 2022 06:46:41 +0000 (22:46 -0800)
Use `SourceManager::isWrittenInScratchSpace()` to specifically check for token paste or stringization, instead of
excluding all non-file buffers. This allows diagnostics to mention macro names that were defined from the command-line.

Differential Revision: https://reviews.llvm.org/D140164

clang/lib/Lex/Lexer.cpp
clang/test/Modules/build-fail-notes.m
clang/test/Modules/epic-fail.m

index 3866c2c..4a9c5b5 100644 (file)
@@ -1047,9 +1047,11 @@ StringRef Lexer::getImmediateMacroNameForDiagnostics(
   while (SM.isMacroArgExpansion(Loc))
     Loc = SM.getImmediateExpansionRange(Loc).getBegin();
 
-  // If the macro's spelling has no FileID, then it's actually a token paste
-  // or stringization (or similar) and not a macro at all.
-  if (!SM.getFileEntryForID(SM.getFileID(SM.getSpellingLoc(Loc))))
+  // If the macro's spelling isn't FileID or from scratch space, then it's
+  // actually a token paste or stringization (or similar) and not a macro at
+  // all.
+  SourceLocation SpellLoc = SM.getSpellingLoc(Loc);
+  if (!SpellLoc.isFileID() || SM.isWrittenInScratchSpace(SpellLoc))
     return {};
 
   // Find the spelling location of the start of the non-argument expansion
index 47bdbc7..929e1f1 100644 (file)
@@ -6,7 +6,7 @@
 // CHECK: While building module 'DependsOnModule' imported from
 // CHECK: While building module 'Module' imported from
 // CHECK: error: expected ';' after top level declarator
-// CHECK: note: expanded from here
+// CHECK: note: expanded from macro 'getModuleVersion'
 // CHECK: fatal error: could not build module 'Module'
 // CHECK: fatal error: could not build module 'DependsOnModule'
 // CHECK-NOT: error:
@@ -24,7 +24,7 @@ extern int Module;
 // CHECK-SDIAG: Module.h:9:13: error: expected ';' after top level declarator
 // CHECK-SDIAG: build-fail-notes.m:4:9: note: while building module 'DependsOnModule' imported from
 // CHECK-SDIAG: DependsOnModule.h:1:10: note: while building module 'Module' imported from
-// CHECK-SDIAG: note: expanded from here
+// CHECK-SDIAG: note: expanded from macro 'getModuleVersion'
 // CHECK-SDIAG: warning: umbrella header for module 'Module' does not include header 'NotInModule.h' [-Wincomplete-umbrella]
 // CHECK-SDIAG: DependsOnModule.h:1:10: fatal: could not build module 'Module'
 // CHECK-SDIAG: build-fail-notes.m:4:9: note: while building module 'DependsOnModule' imported from
index b368cea..efa397b 100644 (file)
@@ -6,7 +6,7 @@
 
 // CHECK: While building module 'Module' imported from
 // CHECK: error: expected ';' after top level declarator
-// CHECK: note: expanded from here
+// CHECK: note: expanded from macro 'getModuleVersion'
 // CHECK: fatal error: could not build module 'Module'
 // CHECK: While building module 'DependsOnModule' imported from
 // CHECK: fatal error: could not build module 'Module'