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

Chen Miao via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Nov 11 01:02:04 PST 2026


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

>From 80adb6760d50371e9d687a06b71cfe79157dff37 Mon Sep 17 00:00:00 2001
From: Chen Miao <chenmiao.ku at gmail.com>
Date: Mon, 9 Nov 2026 02:22:59 +0800
Subject: [PATCH 1/3] [clang] Include source locations in macro directive dumps

`#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.
---
 clang/include/clang/Lex/MacroInfo.h            | 2 +-
 clang/lib/Lex/MacroInfo.cpp                    | 7 ++++---
 clang/lib/Lex/PPMacroExpansion.cpp             | 2 +-
 clang/test/Preprocessor/macro-directive-dump.c | 9 +++++++++
 4 files changed, 15 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/Preprocessor/macro-directive-dump.c

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>

>From b3d4682514dfb07614fcda2b0c80922f2c48a32f Mon Sep 17 00:00:00 2001
From: Chen Miao <chenmiao.ku at gmail.com>
Date: Tue, 10 Nov 2026 08:57:13 +0800
Subject: [PATCH 2/3] [clang][test] Cover macro directive locations after
 pop_macro

---
 clang/test/Preprocessor/macro-directive-dump.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/clang/test/Preprocessor/macro-directive-dump.c b/clang/test/Preprocessor/macro-directive-dump.c
index 2dc2b0dbebbc55..d35aa5f1d16133 100644
--- a/clang/test/Preprocessor/macro-directive-dump.c
+++ b/clang/test/Preprocessor/macro-directive-dump.c
@@ -4,6 +4,16 @@
 #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>
+
+#line 20 "macro-directive-dump.c"
+#define MACRO 1
+#pragma push_macro("MACRO")
+#define MACRO 2
+#pragma pop_macro("MACRO")
+#pragma clang __debug macro MACRO
+// CHECK: DefMacroDirective {{.*}} <macro-directive-dump.c:23:9>
+// CHECK: #define <macro> 1
+// CHECK: UndefMacroDirective {{.*}} <macro-directive-dump.c:23:9>
+// CHECK: DefMacroDirective {{.*}} <macro-directive-dump.c:20:9>

>From 6949da0f53f51e91103e74fce04f2b79b7b21cf2 Mon Sep 17 00:00:00 2001
From: Chen Miao <chenmiao.ku at gmail.com>
Date: Wed, 11 Nov 2026 17:01:12 +0800
Subject: [PATCH 3/3] [clang] Preserve the no-argument MacroDirective::dump
 overload

---
 clang/include/clang/Lex/MacroInfo.h |  3 ++-
 clang/lib/Lex/MacroInfo.cpp         | 13 +++++++++----
 clang/lib/Lex/PPMacroExpansion.cpp  |  2 +-
 3 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/clang/include/clang/Lex/MacroInfo.h b/clang/include/clang/Lex/MacroInfo.h
index 332c0628fdaa98..982bf71ebc7371 100644
--- a/clang/include/clang/Lex/MacroInfo.h
+++ b/clang/include/clang/Lex/MacroInfo.h
@@ -415,7 +415,8 @@ class MacroDirective {
   const DefInfo findDirectiveAtLoc(SourceLocation L,
                                    const SourceManager &SM) const;
 
-  void dump(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 04572fb319e12d..3127dbb45f0ca5 100644
--- a/clang/lib/Lex/MacroInfo.cpp
+++ b/clang/lib/Lex/MacroInfo.cpp
@@ -225,7 +225,9 @@ MacroDirective::findDirectiveAtLoc(SourceLocation L,
   return DefInfo();
 }
 
-LLVM_DUMP_METHOD void MacroDirective::dump(const SourceManager &SM) const {
+LLVM_DUMP_METHOD void MacroDirective::dump() const { dump(/*SM=*/nullptr); }
+
+LLVM_DUMP_METHOD void MacroDirective::dump(const SourceManager *SM) const {
   llvm::raw_ostream &Out = llvm::errs();
 
   switch (getKind()) {
@@ -233,9 +235,12 @@ LLVM_DUMP_METHOD void MacroDirective::dump(const SourceManager &SM) const {
   case MD_Undefine: Out << "UndefMacroDirective"; break;
   case MD_Visibility: Out << "VisibilityMacroDirective"; break;
   }
-  Out << " " << this << " <";
-  Loc.print(Out, SM);
-  Out << ">";
+  Out << " " << this;
+  if (SM) {
+    Out << " <";
+    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 25f9a775831e60..6aaa2d959ed219 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(SourceMgr);
+    MD->dump(&SourceMgr);
   }
 
   // Dump module macros.



More information about the llvm-branch-commits mailing list