[clang] b1e4d49 - Revert "[Clang][DebugInfo] Use macro expansion locations by default" (#240671)

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 9 19:36:53 PST 2026


Author: Snehasish Kumar
Date: 2026-11-10T03:36:47Z
New Revision: b1e4d4916030ea9beabfa3693b5801304dd8e00f

URL: https://github.com/llvm/llvm-project/commit/b1e4d4916030ea9beabfa3693b5801304dd8e00f
DIFF: https://github.com/llvm/llvm-project/commit/b1e4d4916030ea9beabfa3693b5801304dd8e00f.diff

LOG: Revert "[Clang][DebugInfo] Use macro expansion locations by default" (#240671)

Reverts llvm/llvm-project#235205

This is buggy, we should still call getMacroDebugLoc in e.g.

``` 
SourceLocation NewLoc = SM.getExpansionLoc(getMacroDebugLoc(CGM, Loc));
```

and update `getMacroDebugLoc` to drop

```
  if (CGM.getCodeGenOpts().DebugInfoMacroExpansionLoc)
    return Loc;
```

and keep the depth fix flags.

Added: 
    

Modified: 
    clang/include/clang/Basic/DebugOptions.def
    clang/include/clang/Options/Options.td
    clang/lib/CodeGen/CGDebugInfo.cpp
    clang/test/DebugInfo/Generic/macro-info.c

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/DebugOptions.def b/clang/include/clang/Basic/DebugOptions.def
index 87d9d611b6a391..7774514f87e858 100644
--- a/clang/include/clang/Basic/DebugOptions.def
+++ b/clang/include/clang/Basic/DebugOptions.def
@@ -107,6 +107,14 @@ ENUM_DEBUGOPT(DebugInfo, DebugInfoKind, 4,
 /// Whether to generate macro debug info.
 DEBUGOPT(MacroDebugInfo, 1, 0, Compatible)
 
+/// Whether to use expansion location for debug info.
+/// TODO: #175249: Remove once testing is complete for sample pgo users.
+DEBUGOPT(DebugInfoMacroExpansionLoc, 1, 0, Compatible)
+
+/// Macro expansion chains deeper than this use the expansion location instead
+/// of the file location for debug info.
+VALUE_DEBUGOPT(DebugInfoMacroMaxDepth, 32, 64, Compatible)
+
 /// Tune the debug info for this debugger.
 ENUM_DEBUGOPT(DebuggerTuning, DebuggerKind, 3,
               DebuggerKind::Default, Compatible)

diff  --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index 81f26bb4791416..d281f6552b4dbf 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -8152,6 +8152,16 @@ let Visibility = [CC1Option, CC1AsOption] in {
 def debug_info_macro : Flag<["-"], "debug-info-macro">,
   HelpText<"Emit macro debug information">,
   MarshallingInfoFlag<CodeGenOpts<"MacroDebugInfo">>;
+def debug_info_macro_expansion_loc
+    : Flag<["-"], "debug-info-macro-expansion-loc">,
+      HelpText<"Use expansion location for debug info for multi line macros">,
+      MarshallingInfoFlag<CodeGenOpts<"DebugInfoMacroExpansionLoc">>;
+def debug_info_macro_max_depth_EQ
+    : Joined<["-"], "debug-info-macro-max-depth=">,
+      MetaVarName<"<N>">,
+      HelpText<"Use expansion location for debug info for macro arguments "
+               "nested deeper than <N> macro expansions (default 64)">,
+      MarshallingInfoInt<CodeGenOpts<"DebugInfoMacroMaxDepth">, "64">;
 def default_function_attr : Separate<["-"], "default-function-attr">,
   HelpText<"Apply given attribute to all functions">,
   MarshallingInfoStringVector<CodeGenOpts<"DefaultFunctionAttrs">>;

diff  --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 3aa7eccb433cca..bc516406269c2c 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -59,6 +59,18 @@
 using namespace clang;
 using namespace clang::CodeGen;
 
+static SourceLocation getMacroDebugLoc(const CodeGenModule &CGM,
+                                       SourceLocation Loc) {
+  if (CGM.getCodeGenOpts().DebugInfoMacroExpansionLoc)
+    return Loc;
+  // Resolving the file location of very deep macro expansion chains is
+  // expensive, so fall back to the expansion location for them.
+  const SourceManager &SM = CGM.getContext().getSourceManager();
+  SourceLocation FileLoc =
+      SM.getFileLoc(Loc, CGM.getCodeGenOpts().DebugInfoMacroMaxDepth);
+  return FileLoc.isValid() ? FileLoc : SM.getExpansionLoc(Loc);
+}
+
 static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) {
   auto TI = Ctx.getTypeInfo(Ty);
   if (TI.isAlignRequired())
@@ -346,7 +358,7 @@ void CGDebugInfo::setLocation(SourceLocation Loc) {
     return;
 
   SourceManager &SM = CGM.getContext().getSourceManager();
-  SourceLocation NewLoc = SM.getExpansionLoc(Loc);
+  SourceLocation NewLoc = SM.getExpansionLoc(getMacroDebugLoc(CGM, Loc));
   if (CurLoc != NewLoc) {
     CurLoc = NewLoc;
     CurLocFile = nullptr;
@@ -589,6 +601,7 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
     FileName = TheCU->getFile()->getFilename();
     CSInfo = TheCU->getFile()->getChecksum();
   } else {
+    Loc = getMacroDebugLoc(CGM, Loc);
     if (Loc == CurLoc && CurLocFile)
       return CurLocFile;
 
@@ -622,7 +635,8 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
     if (CSKind)
       CSInfo.emplace(*CSKind, Checksum);
   }
-  return createFile(FileName, CSInfo, getSource(SM, SM.getFileID(Loc)));
+  return createFile(FileName, CSInfo,
+                    getSource(SM, SM.getFileID(getMacroDebugLoc(CGM, Loc))));
 }
 
 llvm::DIFile *CGDebugInfo::createFile(
@@ -673,9 +687,10 @@ unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
   if (Loc.isInvalid())
     return 0;
   SourceManager &SM = CGM.getContext().getSourceManager();
-  if (Loc == CurLoc)
+  SourceLocation DebugLoc = getMacroDebugLoc(CGM, Loc);
+  if (DebugLoc == CurLoc)
     return CurLocLine;
-  return SM.getPresumedLoc(Loc).getLine();
+  return SM.getPresumedLoc(DebugLoc).getLine();
 }
 
 unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
@@ -687,7 +702,7 @@ unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
   if (Loc.isInvalid() && CurLoc.isInvalid())
     return 0;
   SourceManager &SM = CGM.getContext().getSourceManager();
-  SourceLocation DebugLoc = Loc.isValid() ? Loc : CurLoc;
+  SourceLocation DebugLoc = Loc.isValid() ? getMacroDebugLoc(CGM, Loc) : CurLoc;
   if (DebugLoc == CurLoc)
     return CurLocColumn;
   PresumedLoc PLoc = SM.getPresumedLoc(DebugLoc);
@@ -5339,7 +5354,9 @@ void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
   // Update our current location
   setLocation(Loc);
 
-  if (CurLoc.isInvalid() || LexicalBlockStack.empty())
+  if (CurLoc.isInvalid() ||
+      (CGM.getCodeGenOpts().DebugInfoMacroExpansionLoc && CurLoc.isMacroID()) ||
+      LexicalBlockStack.empty())
     return;
 
   llvm::MDNode *Scope = LexicalBlockStack.back();
@@ -6612,7 +6629,7 @@ void CGDebugInfo::AddStringLiteralDebugInfo(llvm::GlobalVariable *GV,
                                             const StringLiteral *S) {
   SourceLocation Loc = S->getStrTokenLoc(0);
   SourceManager &SM = CGM.getContext().getSourceManager();
-  PresumedLoc PLoc = SM.getPresumedLoc(Loc);
+  PresumedLoc PLoc = SM.getPresumedLoc(getMacroDebugLoc(CGM, Loc));
   if (!PLoc.isValid())
     return;
 

diff  --git a/clang/test/DebugInfo/Generic/macro-info.c b/clang/test/DebugInfo/Generic/macro-info.c
index 887842fe4f2164..ee040ec5f83617 100644
--- a/clang/test/DebugInfo/Generic/macro-info.c
+++ b/clang/test/DebugInfo/Generic/macro-info.c
@@ -1,4 +1,7 @@
-// RUN: %clang_cc1 %s -debug-info-kind=standalone -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 %s -debug-info-kind=standalone -emit-llvm -o - | FileCheck %s --check-prefix=NEW
+// RUN: %clang_cc1 %s -debug-info-kind=standalone -emit-llvm -debug-info-macro-expansion-loc -o - | FileCheck %s --check-prefix=OLD
+// RUN: %clang_cc1 %s -debug-info-kind=standalone -emit-llvm -debug-info-macro-max-depth=15 -o - | FileCheck %s --check-prefix=DEPTH15
+// RUN: %clang_cc1 %s -debug-info-kind=standalone -emit-llvm -debug-info-macro-max-depth=14 -o - | FileCheck %s --check-prefix=DEPTH14
 
 #define GLOBAL(num) global## num
 #define DECL_GLOBAL(x) int x
@@ -9,11 +12,13 @@
 
 SAME_ORDER(
   int
-// CHECK: DIGlobalVariable(name: "global",{{.*}} line: [[@LINE-2]]
+// NEW: DIGlobalVariable(name: "global",{{.*}} line: [[@LINE+2]]
+// OLD: DIGlobalVariable(name: "global",{{.*}} line: [[@LINE-3]]
     GLOBAL  // <- global
       () = 42,
   const char* s() {
-// CHECK: DIGlobalVariable({{.*}}line: [[@LINE-6]],{{.*}} type: [[TYPEID:![0-9]+]]
+// NEW: DIGlobalVariable({{.*}}line: [[@LINE+2]],{{.*}} type: [[TYPEID:![0-9]+]]
+// OLD: DIGlobalVariable({{.*}}line: [[@LINE-8]],{{.*}} type: [[TYPEID:![0-9]+]]
     return "1234567890";
   }
 )
@@ -21,8 +26,10 @@ SAME_ORDER(
 SWAP_ORDER(
   int GLOBAL(  // <- global2
     2) = 43,
-// CHECK: DIGlobalVariable(name: "global3",{{.*}} line: [[@LINE-3]]
-// CHECK: DIGlobalVariable(name: "global2",{{.*}} line: [[@LINE-4]]
+// NEW: DIGlobalVariable(name: "global3",{{.*}} line: [[@LINE+5]]
+// NEW: DIGlobalVariable(name: "global2",{{.*}} line: [[@LINE-3]]
+// OLD: DIGlobalVariable(name: "global3",{{.*}} line: [[@LINE-5]]
+// OLD: DIGlobalVariable(name: "global2",{{.*}} line: [[@LINE-6]]
   DECL_GLOBAL(
     GLOBAL(  // <- global3
       3)) = 44
@@ -30,7 +37,8 @@ SWAP_ORDER(
 
 
 DECL_GLOBAL(
-// CHECK: DIGlobalVariable(name: "global4",{{.*}} line: [[@LINE-1]]
+// NEW: DIGlobalVariable(name: "global4",{{.*}} line: [[@LINE+2]]
+// OLD: DIGlobalVariable(name: "global4",{{.*}} line: [[@LINE-2]]
   GLOBAL(  // <- global4
     4));
 
@@ -47,12 +55,13 @@ DECL_GLOBAL(
 #define ID512(x) ID256(ID256(x))
 
 ID8(
-// CHECK: DIGlobalVariable(name: "global5",{{.*}} line: [[@LINE-1]]
+// NEW: DIGlobalVariable(name: "global5",{{.*}} line: [[@LINE+4]]
+// OLD: DIGlobalVariable(name: "global5",{{.*}} line: [[@LINE-2]]
+// DEPTH15: DIGlobalVariable(name: "global5",{{.*}} line: [[@LINE+2]]
+// DEPTH14: DIGlobalVariable(name: "global5",{{.*}} line: [[@LINE-4]]
   int global5);
 
 ID512(
-// CHECK: DIGlobalVariable(name: "global6",{{.*}} line: [[@LINE-1]]
+// NEW: DIGlobalVariable(name: "global6",{{.*}} line: [[@LINE-1]]
+// OLD: DIGlobalVariable(name: "global6",{{.*}} line: [[@LINE-2]]
   int global6);
-
-// CHECK: ![[SP:[0-9]+]] = distinct !DISubprogram(name: "s",{{.*}} line: [[@LINE-47]],
-// CHECK: !DILocation(line: [[@LINE-48]], column: 1, scope: ![[SP]])


        


More information about the cfe-commits mailing list