Skip to content

Commit 5041d06

Browse files
authored
[MC] Fix DWARF file table for files with empty DWARF (#119020) (#119229)
Update root file in DWARF file/line table as soon as we see the first "#line" directive. This was moved from "enabledGenDwarfForAssembly", which is called right before we emit DWARF information. But if the file is empty or contains expressions that doesn't need DWARF, it is never called, leaving an original root file and not the file in the "#line" directive. Add a test checking for this case. Fixes: #119020
1 parent df3397b commit 5041d06

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

llvm/lib/MC/MCParser/AsmParser.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ class AsmParser : public MCAsmParser {
162162
};
163163
CppHashInfoTy CppHashInfo;
164164

165-
/// The filename from the first cpp hash file line comment, if any.
166-
StringRef FirstCppHashFilename;
165+
/// Have we seen any file line comment.
166+
bool HadCppHashFilename = false;
167167

168168
/// List of forward directional labels for diagnosis at the end.
169169
SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels;
@@ -952,12 +952,6 @@ bool AsmParser::enabledGenDwarfForAssembly() {
952952
// the assembler source was produced with debug info already) then emit one
953953
// describing the assembler source file itself.
954954
if (getContext().getGenDwarfFileNumber() == 0) {
955-
// Use the first #line directive for this, if any. It's preprocessed, so
956-
// there is no checksum, and of course no source directive.
957-
if (!FirstCppHashFilename.empty())
958-
getContext().setMCLineTableRootFile(
959-
/*CUID=*/0, getContext().getCompilationDir(), FirstCppHashFilename,
960-
/*Cksum=*/std::nullopt, /*Source=*/std::nullopt);
961955
const MCDwarfFile &RootFile =
962956
getContext().getMCDwarfLineTable(/*CUID=*/0).getRootFile();
963957
getContext().setGenDwarfFileNumber(getStreamer().emitDwarfFileDirective(
@@ -2440,8 +2434,19 @@ bool AsmParser::parseCppHashLineFilenameComment(SMLoc L, bool SaveLocInfo) {
24402434
CppHashInfo.Filename = Filename;
24412435
CppHashInfo.LineNumber = LineNumber;
24422436
CppHashInfo.Buf = CurBuffer;
2443-
if (FirstCppHashFilename.empty())
2444-
FirstCppHashFilename = Filename;
2437+
if (!HadCppHashFilename) {
2438+
HadCppHashFilename = true;
2439+
// If we haven't encountered any .file directives, then the first #line
2440+
// directive describes the "root" file and directory of the compilation
2441+
// unit.
2442+
if (getContext().getGenDwarfFileNumber() == 0) {
2443+
// It's preprocessed, so there is no checksum, and of course no source
2444+
// directive.
2445+
getContext().setMCLineTableRootFile(
2446+
/*CUID=*/0, getContext().getCompilationDir(), Filename,
2447+
/*Cksum=*/std::nullopt, /*Source=*/std::nullopt);
2448+
}
2449+
}
24452450
return false;
24462451
}
24472452

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: llvm-mc -triple x86_64-unknown-linux-gnu -filetype obj -g -dwarf-version 5 -o %t %s
2+
// RUN: llvm-dwarfdump -debug-info -debug-line %t | FileCheck %s
3+
4+
// CHECK-NOT: DW_TAG_
5+
6+
// CHECK: include_directories[ 0] =
7+
// CHECK-NOT: include_directories[ 1] =
8+
// CHECK: file_names[ 0]:
9+
// CHECK-NEXT: name: "/MyTest/Inputs/other.S"
10+
// CHECK-NEXT: dir_index: 0
11+
// CHECK-NOT: file_names[ 1]:
12+
13+
// RUN: llvm-mc -triple=x86_64 -filetype=obj -g -dwarf-version=5 -fdebug-prefix-map=/MyTest=/src_root %s -o %t.5.o
14+
// RUN: llvm-dwarfdump -debug-info -debug-line %t.5.o | FileCheck %s --check-prefixes=MAP
15+
16+
// MAP-NOT: DW_TAG_
17+
18+
// MAP: include_directories[ 0] = "{{.*}}"
19+
// MAP-NEXT: file_names[ 0]:
20+
// MAP-NEXT: name: "/src_root/Inputs/other.S"
21+
// MAP-NEXT: dir_index: 0
22+
23+
# 1 "/MyTest/Inputs/other.S"
24+
25+
.section .data
26+
.asciz "data"

0 commit comments

Comments
 (0)