Skip to content

Commit 651cb8a

Browse files
SergejSalnikovMichael137
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 (cherry picked from commit beadb9e)
1 parent 81388c1 commit 651cb8a

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
@@ -330,7 +330,7 @@ void CGDebugInfo::setLocation(SourceLocation Loc) {
330330
if (Loc.isInvalid())
331331
return;
332332

333-
CurLoc = CGM.getContext().getSourceManager().getExpansionLoc(Loc);
333+
CurLoc = CGM.getContext().getSourceManager().getFileLoc(Loc);
334334

335335
// If we've changed files in the middle of a lexical scope go ahead
336336
// and create a new lexical scope with file node if it's different
@@ -557,7 +557,7 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
557557
FileName = TheCU->getFile()->getFilename();
558558
CSInfo = TheCU->getFile()->getChecksum();
559559
} else {
560-
PresumedLoc PLoc = SM.getPresumedLoc(Loc);
560+
PresumedLoc PLoc = SM.getPresumedLoc(SM.getFileLoc(Loc));
561561
FileName = PLoc.getFilename();
562562

563563
if (FileName.empty()) {
@@ -584,7 +584,8 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
584584
if (CSKind)
585585
CSInfo.emplace(*CSKind, Checksum);
586586
}
587-
return createFile(FileName, CSInfo, getSource(SM, SM.getFileID(Loc)));
587+
return createFile(FileName, CSInfo,
588+
getSource(SM, SM.getFileID(SM.getFileLoc(Loc))));
588589
}
589590

590591
llvm::DIFile *CGDebugInfo::createFile(
@@ -639,7 +640,7 @@ unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
639640
if (Loc.isInvalid())
640641
return 0;
641642
SourceManager &SM = CGM.getContext().getSourceManager();
642-
return SM.getPresumedLoc(Loc).getLine();
643+
return SM.getPresumedLoc(SM.getFileLoc(Loc)).getLine();
643644
}
644645

645646
unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
@@ -651,7 +652,8 @@ unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc, bool Force) {
651652
if (Loc.isInvalid() && CurLoc.isInvalid())
652653
return 0;
653654
SourceManager &SM = CGM.getContext().getSourceManager();
654-
PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
655+
PresumedLoc PLoc =
656+
SM.getPresumedLoc(Loc.isValid() ? SM.getFileLoc(Loc) : CurLoc);
655657
return PLoc.isValid() ? PLoc.getColumn() : 0;
656658
}
657659

@@ -4975,7 +4977,7 @@ void CGDebugInfo::EmitLocation(CGBuilderTy &Builder, SourceLocation Loc) {
49754977
// Update our current location
49764978
setLocation(Loc);
49774979

4978-
if (CurLoc.isInvalid() || CurLoc.isMacroID() || LexicalBlockStack.empty())
4980+
if (CurLoc.isInvalid() || LexicalBlockStack.empty())
49794981
return;
49804982

49814983
llvm::MDNode *Scope = LexicalBlockStack.back();
@@ -6246,7 +6248,8 @@ void CGDebugInfo::EmitGlobalAlias(const llvm::GlobalValue *GV,
62466248
void CGDebugInfo::AddStringLiteralDebugInfo(llvm::GlobalVariable *GV,
62476249
const StringLiteral *S) {
62486250
SourceLocation Loc = S->getStrTokenLoc(0);
6249-
PresumedLoc PLoc = CGM.getContext().getSourceManager().getPresumedLoc(Loc);
6251+
SourceManager &SM = CGM.getContext().getSourceManager();
6252+
PresumedLoc PLoc = SM.getPresumedLoc(SM.getFileLoc(Loc));
62506253
if (!PLoc.isValid())
62516254
return;
62526255

clang/test/CodeGen/macro-info.c

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)