[clang] Add a flag to preserve the old macro behaviour. (PR #174895)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 7 16:54:14 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Snehasish Kumar (snehasish)
<details>
<summary>Changes</summary>
[Clang][DebugInfo] Add a flag to use expansion loc for macro params.
This patch adds a flag to allow users to preserve the old behaviour - use the macro expansion location for parameters. This is useful for wider testing of sample profile driven PGO which relies on debug information based mapping.
---
Full diff: https://github.com/llvm/llvm-project/pull/174895.diff
2 Files Affected:
- (modified) clang/lib/CodeGen/CGDebugInfo.cpp (+24-9)
- (modified) clang/test/DebugInfo/Generic/macro-info.c (+12-6)
``````````diff
diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp
index 9dcf8ccdec275..22fc6704de96d 100644
--- a/clang/lib/CodeGen/CGDebugInfo.cpp
+++ b/clang/lib/CodeGen/CGDebugInfo.cpp
@@ -54,11 +54,17 @@
#include "llvm/Support/SHA1.h"
#include "llvm/Support/SHA256.h"
#include "llvm/Support/TimeProfiler.h"
+#include "llvm/Support/CommandLine.h"
#include <cstdint>
#include <optional>
using namespace clang;
using namespace clang::CodeGen;
+static llvm::cl::opt<bool>
+ DebugInfoMacroExpansionLoc("debug-info-macro-expansion-loc",
+ llvm::cl::desc("Use expansion location for debug info"),
+ llvm::cl::init(false));
+
static uint32_t getTypeAlignIfRequired(const Type *Ty, const ASTContext &Ctx) {
auto TI = Ctx.getTypeInfo(Ty);
if (TI.isAlignRequired())
@@ -345,7 +351,10 @@ void CGDebugInfo::setLocation(SourceLocation Loc) {
if (Loc.isInvalid())
return;
- CurLoc = CGM.getContext().getSourceManager().getFileLoc(Loc);
+ if (DebugInfoMacroExpansionLoc)
+ CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
+ else
+ CurLoc = CGM.getContext().getSourceManager().getFileLoc(Loc);
// If we've changed files in the middle of a lexical scope go ahead
// and create a new lexical scope with file node if it's different
@@ -572,7 +581,8 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
FileName = TheCU->getFile()->getFilename();
CSInfo = TheCU->getFile()->getChecksum();
} else {
- PresumedLoc PLoc = SM.getPresumedLoc(SM.getFileLoc(Loc));
+ PresumedLoc PLoc =
+ SM.getPresumedLoc(DebugInfoMacroExpansionLoc ? Loc : SM.getFileLoc(Loc));
FileName = PLoc.getFilename();
if (FileName.empty()) {
@@ -599,8 +609,9 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
if (CSKind)
CSInfo.emplace(*CSKind, Checksum);
}
- return createFile(FileName, CSInfo,
- getSource(SM, SM.getFileID(SM.getFileLoc(Loc))));
+ return createFile(
+ FileName, CSInfo,
+ getSource(SM, SM.getFileID(DebugInfoMacroExpansionLoc ? Loc : SM.getFileLoc(Loc))));
}
llvm::DIFile *CGDebugInfo::createFile(
@@ -655,7 +666,8 @@ unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
if (Loc.isInvalid())
return 0;
SourceManager &SM = CGM.getContext().getSourceManager();
- return SM.getPresumedLoc(SM.getFileLoc(Loc)).getLine();
+ return SM.getPresumedLoc(DebugInfoMacroExpansionLoc ? Loc : SM.getFileLoc(Loc))
+ .getLine();
}
unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
@@ -667,8 +679,9 @@ unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
if (Loc.isInvalid() && CurLoc.isInvalid())
return 0;
SourceManager &SM = CGM.getContext().getSourceManager();
- PresumedLoc PLoc =
- SM.getPresumedLoc(Loc.isValid() ? SM.getFileLoc(Loc) : CurLoc);
+ PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid()
+ ? (DebugInfoMacroExpansionLoc ? Loc : SM.getFileLoc(Loc))
+ : CurLoc);
return PLoc.isValid() ? PLoc.getColumn() : 0;
}
@@ -5014,7 +5027,8 @@ void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
// Update our current location
setLocation(Loc);
- if (CurLoc.isInvalid() || LexicalBlockStack.empty())
+ if (CurLoc.isInvalid() || (DebugInfoMacroExpansionLoc && CurLoc.isMacroID()) ||
+ LexicalBlockStack.empty())
return;
llvm::MDNode *Scope = LexicalBlockStack.back();
@@ -6282,7 +6296,8 @@ void CGDebugInfo::AddStringLiteralDebugInfo(llvm::GlobalVariable *GV,
const StringLiteral *S) {
SourceLocation Loc = S->getStrTokenLoc(0);
SourceManager &SM = CGM.getContext().getSourceManager();
- PresumedLoc PLoc = SM.getPresumedLoc(SM.getFileLoc(Loc));
+ PresumedLoc PLoc =
+ SM.getPresumedLoc(DebugInfoMacroExpansionLoc ? Loc : SM.getFileLoc(Loc));
if (!PLoc.isValid())
return;
diff --git a/clang/test/DebugInfo/Generic/macro-info.c b/clang/test/DebugInfo/Generic/macro-info.c
index ec49eb5d65f9c..3f47050e60317 100644
--- a/clang/test/DebugInfo/Generic/macro-info.c
+++ b/clang/test/DebugInfo/Generic/macro-info.c
@@ -1,4 +1,5 @@
-// 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 -mllvm -debug-info-macro-expansion-loc -o - | FileCheck %s --check-prefix=OLD
#define GLOBAL(num) global## num
#define DECL_GLOBAL(x) int x
@@ -9,11 +10,13 @@
SAME_ORDER(
int
-// CHECK: DIGlobalVariable(name: "global",{{.*}} line: [[@LINE+1]]
+// NEW: DIGlobalVariable(name: "global",{{.*}} line: [[@LINE+2]]
+// OLD: DIGlobalVariable(name: "global",{{.*}} line: [[@LINE-3]]
GLOBAL // <- global
() = 42,
const char* s() {
-// CHECK: DIGlobalVariable({{.*}}line: [[@LINE+1]],{{.*}} 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 +24,10 @@ SAME_ORDER(
SWAP_ORDER(
int GLOBAL( // <- global2
2) = 43,
-// CHECK: DIGlobalVariable(name: "global3",{{.*}} line: [[@LINE+3]]
-// CHECK: DIGlobalVariable(name: "global2",{{.*}} line: [[@LINE-3]]
+// 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,6 +35,7 @@ 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));
``````````
</details>
https://github.com/llvm/llvm-project/pull/174895
More information about the cfe-commits
mailing list