[llvm-branch-commits] [clang] [clang] Include source locations in macro directive dumps (PR #240172)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sun Nov 8 10:29:36 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Chen Miao (ChenMiaoi)

<details>
<summary>Changes</summary>

`#pragma clang __debug macro` dumps macro directive history without showing where each directive occurred.

Pass the `SourceManager` to `MacroDirective::dump()` and print each directive's location alongside its address, making definitions and undefinitions easier to trace.

---
Full diff: https://github.com/llvm/llvm-project/pull/240172.diff


4 Files Affected:

- (modified) clang/include/clang/Lex/MacroInfo.h (+1-1) 
- (modified) clang/lib/Lex/MacroInfo.cpp (+4-3) 
- (modified) clang/lib/Lex/PPMacroExpansion.cpp (+1-1) 
- (added) clang/test/Preprocessor/macro-directive-dump.c (+9) 


``````````diff
diff --git a/clang/include/clang/Lex/MacroInfo.h b/clang/include/clang/Lex/MacroInfo.h
index 53888e3d960e5c..332c0628fdaa98 100644
--- a/clang/include/clang/Lex/MacroInfo.h
+++ b/clang/include/clang/Lex/MacroInfo.h
@@ -415,7 +415,7 @@ class MacroDirective {
   const DefInfo findDirectiveAtLoc(SourceLocation L,
                                    const SourceManager &SM) const;
 
-  void dump() const;
+  void dump(const SourceManager &SM) const;
 
   static bool classof(const MacroDirective *) { return true; }
 };
diff --git a/clang/lib/Lex/MacroInfo.cpp b/clang/lib/Lex/MacroInfo.cpp
index 471f911cddd12e..04572fb319e12d 100644
--- a/clang/lib/Lex/MacroInfo.cpp
+++ b/clang/lib/Lex/MacroInfo.cpp
@@ -225,7 +225,7 @@ MacroDirective::findDirectiveAtLoc(SourceLocation L,
   return DefInfo();
 }
 
-LLVM_DUMP_METHOD void MacroDirective::dump() const {
+LLVM_DUMP_METHOD void MacroDirective::dump(const SourceManager &SM) const {
   llvm::raw_ostream &Out = llvm::errs();
 
   switch (getKind()) {
@@ -233,8 +233,9 @@ LLVM_DUMP_METHOD void MacroDirective::dump() const {
   case MD_Undefine: Out << "UndefMacroDirective"; break;
   case MD_Visibility: Out << "VisibilityMacroDirective"; break;
   }
-  Out << " " << this;
-  // FIXME: Dump SourceLocation.
+  Out << " " << this << " <";
+  Loc.print(Out, SM);
+  Out << ">";
   if (auto *Prev = getPrevious())
     Out << " prev " << Prev;
 
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
index f55e9196bcd616..25f9a775831e60 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -273,7 +273,7 @@ void Preprocessor::dumpMacroInfo(const IdentifierInfo *II) {
   for (auto *MD = State ? State->getLatest() : nullptr; MD;
        MD = MD->getPrevious()) {
     llvm::errs() << " ";
-    MD->dump();
+    MD->dump(SourceMgr);
   }
 
   // Dump module macros.
diff --git a/clang/test/Preprocessor/macro-directive-dump.c b/clang/test/Preprocessor/macro-directive-dump.c
new file mode 100644
index 00000000000000..2dc2b0dbebbc55
--- /dev/null
+++ b/clang/test/Preprocessor/macro-directive-dump.c
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -fsyntax-only %s 2>&1 | FileCheck %s
+
+#line 10 "macro-directive-dump.c"
+#define MACRO 1
+#undef MACRO
+#pragma clang __debug macro MACRO
+
+// CHECK: UndefMacroDirective {{.*}} <macro-directive-dump.c:11:8>
+// CHECK: DefMacroDirective {{.*}} <macro-directive-dump.c:10:9>

``````````

</details>


https://github.com/llvm/llvm-project/pull/240172


More information about the llvm-branch-commits mailing list