[clang] [Clang][DebugInfo] Use macro expansion locations by default (PR #235205)

via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 24 10:32:02 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Snehasish Kumar (snehasish)

<details>
<summary>Changes</summary>

Sample PGO testing of -debug-info-macro-expansion-loc is complete. Make its expansion-location behavior unconditional and remove the temporary cc1 flag, code generation option, and location helper.

Update the macro debug-info regression test to check the default behavior, including function line and column locations, and document the removed flag in the release notes.

Fixes #<!-- -->175249

Assisted-by: Argon

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


5 Files Affected:

- (modified) clang/docs/ReleaseNotes.md (+4) 
- (modified) clang/include/clang/Basic/DebugOptions.def (-8) 
- (modified) clang/include/clang/Options/Options.td (-10) 
- (modified) clang/lib/CodeGen/CGDebugInfo.cpp (+7-24) 
- (modified) clang/test/DebugInfo/Generic/macro-info.c (+11-20) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index adcdd911beb09c..9628bc86248d38 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -402,6 +402,10 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 
 ### Removed Compiler Flags
 
+- Removed the experimental `-cc1` flag `-debug-info-macro-expansion-loc`.
+  Debug information now always uses macro expansion locations for macro arguments.
+  (#GH175249)
+
 ### Attribute Changes in Clang
 
 - Clang now properly propagates attributes on class and variable templates to their redeclarations, which will result in redeclarations not interfering with diagnostics. (#GH209812)
diff --git a/clang/include/clang/Basic/DebugOptions.def b/clang/include/clang/Basic/DebugOptions.def
index 91cef13ffac467..480f1f41fad082 100644
--- a/clang/include/clang/Basic/DebugOptions.def
+++ b/clang/include/clang/Basic/DebugOptions.def
@@ -107,14 +107,6 @@ 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 b760c3a1213460..851ce158b2cb8b 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -8132,16 +8132,6 @@ 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 96cda4555e49db..f1dec38668af07 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -59,18 +59,6 @@
 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())
@@ -358,7 +346,7 @@ void CGDebugInfo::setLocation(SourceLocation Loc) {
     return;
 
   SourceManager &SM = CGM.getContext().getSourceManager();
-  SourceLocation NewLoc = SM.getExpansionLoc(getMacroDebugLoc(CGM, Loc));
+  SourceLocation NewLoc = SM.getExpansionLoc(Loc);
   if (CurLoc != NewLoc) {
     CurLoc = NewLoc;
     CurLocFile = nullptr;
@@ -601,7 +589,6 @@ 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;
 
@@ -632,8 +619,7 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
     if (CSKind)
       CSInfo.emplace(*CSKind, Checksum);
   }
-  return createFile(FileName, CSInfo,
-                    getSource(SM, SM.getFileID(getMacroDebugLoc(CGM, Loc))));
+  return createFile(FileName, CSInfo, getSource(SM, SM.getFileID(Loc)));
 }
 
 llvm::DIFile *CGDebugInfo::createFile(
@@ -684,10 +670,9 @@ unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
   if (Loc.isInvalid())
     return 0;
   SourceManager &SM = CGM.getContext().getSourceManager();
-  SourceLocation DebugLoc = getMacroDebugLoc(CGM, Loc);
-  if (DebugLoc == CurLoc)
+  if (Loc == CurLoc)
     return CurLocLine;
-  return SM.getPresumedLoc(DebugLoc).getLine();
+  return SM.getPresumedLoc(Loc).getLine();
 }
 
 unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
@@ -699,7 +684,7 @@ unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
   if (Loc.isInvalid() && CurLoc.isInvalid())
     return 0;
   SourceManager &SM = CGM.getContext().getSourceManager();
-  SourceLocation DebugLoc = Loc.isValid() ? getMacroDebugLoc(CGM, Loc) : CurLoc;
+  SourceLocation DebugLoc = Loc.isValid() ? Loc : CurLoc;
   if (DebugLoc == CurLoc)
     return CurLocColumn;
   PresumedLoc PLoc = SM.getPresumedLoc(DebugLoc);
@@ -5346,9 +5331,7 @@ void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
   // Update our current location
   setLocation(Loc);
 
-  if (CurLoc.isInvalid() ||
-      (CGM.getCodeGenOpts().DebugInfoMacroExpansionLoc && CurLoc.isMacroID()) ||
-      LexicalBlockStack.empty())
+  if (CurLoc.isInvalid() || LexicalBlockStack.empty())
     return;
 
   llvm::MDNode *Scope = LexicalBlockStack.back();
@@ -6621,7 +6604,7 @@ void CGDebugInfo::AddStringLiteralDebugInfo(llvm::GlobalVariable *GV,
                                             const StringLiteral *S) {
   SourceLocation Loc = S->getStrTokenLoc(0);
   SourceManager &SM = CGM.getContext().getSourceManager();
-  PresumedLoc PLoc = SM.getPresumedLoc(getMacroDebugLoc(CGM, Loc));
+  PresumedLoc PLoc = SM.getPresumedLoc(Loc);
   if (!PLoc.isValid())
     return;
 
diff --git a/clang/test/DebugInfo/Generic/macro-info.c b/clang/test/DebugInfo/Generic/macro-info.c
index ee040ec5f83617..887842fe4f2164 100644
--- a/clang/test/DebugInfo/Generic/macro-info.c
+++ b/clang/test/DebugInfo/Generic/macro-info.c
@@ -1,7 +1,4 @@
-// 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
+// RUN: %clang_cc1 %s -debug-info-kind=standalone -emit-llvm -o - | FileCheck %s
 
 #define GLOBAL(num) global## num
 #define DECL_GLOBAL(x) int x
@@ -12,13 +9,11 @@
 
 SAME_ORDER(
   int
-// NEW: DIGlobalVariable(name: "global",{{.*}} line: [[@LINE+2]]
-// OLD: DIGlobalVariable(name: "global",{{.*}} line: [[@LINE-3]]
+// CHECK: DIGlobalVariable(name: "global",{{.*}} line: [[@LINE-2]]
     GLOBAL  // <- global
       () = 42,
   const char* s() {
-// NEW: DIGlobalVariable({{.*}}line: [[@LINE+2]],{{.*}} type: [[TYPEID:![0-9]+]]
-// OLD: DIGlobalVariable({{.*}}line: [[@LINE-8]],{{.*}} type: [[TYPEID:![0-9]+]]
+// CHECK: DIGlobalVariable({{.*}}line: [[@LINE-6]],{{.*}} type: [[TYPEID:![0-9]+]]
     return "1234567890";
   }
 )
@@ -26,10 +21,8 @@ SAME_ORDER(
 SWAP_ORDER(
   int GLOBAL(  // <- global2
     2) = 43,
-// 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]]
+// CHECK: DIGlobalVariable(name: "global3",{{.*}} line: [[@LINE-3]]
+// CHECK: DIGlobalVariable(name: "global2",{{.*}} line: [[@LINE-4]]
   DECL_GLOBAL(
     GLOBAL(  // <- global3
       3)) = 44
@@ -37,8 +30,7 @@ SWAP_ORDER(
 
 
 DECL_GLOBAL(
-// NEW: DIGlobalVariable(name: "global4",{{.*}} line: [[@LINE+2]]
-// OLD: DIGlobalVariable(name: "global4",{{.*}} line: [[@LINE-2]]
+// CHECK: DIGlobalVariable(name: "global4",{{.*}} line: [[@LINE-1]]
   GLOBAL(  // <- global4
     4));
 
@@ -55,13 +47,12 @@ DECL_GLOBAL(
 #define ID512(x) ID256(ID256(x))
 
 ID8(
-// 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]]
+// CHECK: DIGlobalVariable(name: "global5",{{.*}} line: [[@LINE-1]]
   int global5);
 
 ID512(
-// NEW: DIGlobalVariable(name: "global6",{{.*}} line: [[@LINE-1]]
-// OLD: DIGlobalVariable(name: "global6",{{.*}} line: [[@LINE-2]]
+// CHECK: DIGlobalVariable(name: "global6",{{.*}} line: [[@LINE-1]]
   int global6);
+
+// CHECK: ![[SP:[0-9]+]] = distinct !DISubprogram(name: "s",{{.*}} line: [[@LINE-47]],
+// CHECK: !DILocation(line: [[@LINE-48]], column: 1, scope: ![[SP]])

``````````

</details>


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


More information about the cfe-commits mailing list