Skip to content

Commit af2196c

Browse files
SergejSalnikovaokblast
authored andcommitted
[clang] Use File Location for debug info resolution. (llvm#163982)
To improve debuggability, the macro arguments should be resolved to their original location rather than macro expansion location. [PR in cation](https://github.com/user-attachments/assets/994fb89f-83be-4c21-a79c-f8e51d818f7b) fixes llvm#160667
1 parent ec7f87d commit af2196c

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ void CGDebugInfo::setLocation(SourceLocation Loc) {
345345
if (Loc.isInvalid())
346346
return;
347347

348-
CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
348+
CurLoc = CGM.getContext().getSourceManager().getFileLoc(Loc);
349349

350350
// If we've changed files in the middle of a lexical scope go ahead
351351
// and create a new lexical scope with file node if it's different
@@ -572,7 +572,7 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
572572
FileName = TheCU->getFile()->getFilename();
573573
CSInfo = TheCU->getFile()->getChecksum();
574574
} else {
575-
PresumedLoc PLoc = SM.getPresumedLoc(Loc);
575+
PresumedLoc PLoc = SM.getPresumedLoc(SM.getFileLoc(Loc));
576576
FileName = PLoc.getFilename();
577577

578578
if (FileName.empty()) {
@@ -599,7 +599,8 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
599599
if (CSKind)
600600
CSInfo.emplace(*CSKind, Checksum);
601601
}
602-
return createFile(FileName, CSInfo, getSource(SM, SM.getFileID(Loc)));
602+
return createFile(FileName, CSInfo,
603+
getSource(SM, SM.getFileID(SM.getFileLoc(Loc))));
603604
}
604605

605606
llvm::DIFile *CGDebugInfo::createFile(
@@ -654,7 +655,7 @@ unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
654655
if (Loc.isInvalid())
655656
return 0;
656657
SourceManager &SM = CGM.getContext().getSourceManager();
657-
return SM.getPresumedLoc(Loc).getLine();
658+
return SM.getPresumedLoc(SM.getFileLoc(Loc)).getLine();
658659
}
659660

660661
unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
@@ -666,7 +667,8 @@ unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
666667
if (Loc.isInvalid() && CurLoc.isInvalid())
667668
return 0;
668669
SourceManager &SM = CGM.getContext().getSourceManager();
669-
PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
670+
PresumedLoc PLoc =
671+
SM.getPresumedLoc(Loc.isValid() ? SM.getFileLoc(Loc) : CurLoc);
670672
return PLoc.isValid() ? PLoc.getColumn() : 0;
671673
}
672674

@@ -5002,7 +5004,7 @@ void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
50025004
// Update our current location
50035005
setLocation(Loc);
50045006

5005-
if (CurLoc.isInvalid() || CurLoc.isMacroID() || LexicalBlockStack.empty())
5007+
if (CurLoc.isInvalid() || LexicalBlockStack.empty())
50065008
return;
50075009

50085010
llvm::MDNode *Scope = LexicalBlockStack.back();
@@ -6278,7 +6280,8 @@ void CGDebugInfo::EmitGlobalAlias(const llvm::GlobalValue *GV,
62786280
void CGDebugInfo::AddStringLiteralDebugInfo(llvm::GlobalVariable *GV,
62796281
const StringLiteral *S) {
62806282
SourceLocation Loc = S->getStrTokenLoc(0);
6281-
PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
6283+
SourceManager &SM = CGM.getContext().getSourceManager();
6284+
PresumedLoc PLoc = SM.getPresumedLoc(SM.getFileLoc(Loc));
62826285
if (!PLoc.isValid())
62836286
return;
62846287

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// RUN: %clang_cc1 %s -debug-info-kind=standalone -emit-llvm -o - | FileCheck %s
2+
3+
#define GLOBAL(num) global## num
4+
#define DECL_GLOBAL(x) int x
5+
#define SAME_ORDER(x, y) x; y
6+
#define SWAP_ORDER(x,y) y; x
7+
8+
9+
10+
SAME_ORDER(
11+
int
12+
// CHECK: DIGlobalVariable(name: "global",{{.*}} line: [[@LINE+1]]
13+
GLOBAL // <- global
14+
() = 42,
15+
const char* s() {
16+
// CHECK: DIGlobalVariable({{.*}}line: [[@LINE+1]],{{.*}} type: [[TYPEID:![0-9]+]]
17+
return "1234567890";
18+
}
19+
)
20+
21+
SWAP_ORDER(
22+
int GLOBAL( // <- global2
23+
2) = 43,
24+
// CHECK: DIGlobalVariable(name: "global3",{{.*}} line: [[@LINE+3]]
25+
// CHECK: DIGlobalVariable(name: "global2",{{.*}} line: [[@LINE-3]]
26+
DECL_GLOBAL(
27+
GLOBAL( // <- global3
28+
3)) = 44
29+
);
30+
31+
32+
DECL_GLOBAL(
33+
// CHECK: DIGlobalVariable(name: "global4",{{.*}} line: [[@LINE+1]]
34+
GLOBAL( // <- global4
35+
4));

0 commit comments

Comments
 (0)