Skip to content

Commit 3ac6dbf

Browse files
committed
Address reviewer feedback.
Fix --line-numbers with --macho and add test. Use std::optional to defer initialization of SourcePrinter/LiveElementPrinter.
1 parent d02ad93 commit 3ac6dbf

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

llvm/test/tools/llvm-objdump/MachO/disassemble-source-dsym.test

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# RUN: dsymutil -f -oso-prepend-path=%p/../../dsymutil/ %t3 -o %t3.dSYM
1414
# RUN: llvm-objdump --source --prefix=%p/../../dsymutil %t3 | FileCheck --check-prefix=SOURCE %s
1515

16-
## Test that --source works with --macho flag
16+
## Test that --source works with --macho flag.
1717

1818
## --macho w/ explicit .dSYM
1919
# RUN: llvm-objdump < %p/../../dsymutil/Inputs/basic.macho.x86_64 - --source --macho --dsym=%t1.dSYM --prefix=%p/../../dsymutil | \
@@ -26,3 +26,22 @@
2626
# RUN: llvm-objdump --source --macho --prefix=%p/../../dsymutil %t3 | FileCheck --check-prefix=SOURCE %s
2727

2828
# SOURCE: ; int bar(int arg) {
29+
30+
## Test that --line-numbers works with --macho flag.
31+
32+
## --macho -l w/ explicit .dSYM
33+
# RUN: llvm-objdump -d -l --macho --dsym=%t1.dSYM %p/../../dsymutil/Inputs/basic.macho.x86_64 | FileCheck --check-prefix=LINE %s
34+
35+
## --macho -l w/ object file (embedded debug info)
36+
# RUN: llvm-objdump -d -l --macho %p/../../dsymutil/Inputs/basic1.macho.x86_64.o | FileCheck --check-prefix=LINE_OBJ %s
37+
38+
# LINE: (__TEXT,__text) section
39+
# LINE: _bar:
40+
# LINE: ; bar():
41+
# LINE: ; {{.*}}basic3.c:
42+
43+
# LINE_OBJ: (__TEXT,__text) section
44+
# LINE_OBJ: _main:
45+
# LINE_OBJ: ; main():
46+
# LINE_OBJ: ; {{.*}}basic1.c:23
47+
# LINE_OBJ: pushq %rbp ## basic1.c:23:0

llvm/tools/llvm-objdump/MachODump.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7425,14 +7425,18 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
74257425
if (!DSym)
74267426
return;
74277427
DbgObj = DSym;
7428-
if (UseDbg) {
7428+
if (UseDbg || PrintLines) {
74297429
// Setup the DIContext
74307430
diContext = DWARFContext::create(*DbgObj);
74317431
}
74327432
}
74337433

7434-
SourcePrinter SP(DbgObj, TheTarget->getName());
7435-
LiveElementPrinter LEP(*MRI, *STI);
7434+
std::optional<SourcePrinter> SP;
7435+
std::optional<LiveElementPrinter> LEP;
7436+
if (PrintSource || PrintLines) {
7437+
SP.emplace(DbgObj, TheTarget->getName());
7438+
LEP.emplace(*MRI, *STI);
7439+
}
74367440

74377441
if (FilterSections.empty())
74387442
outs() << "(" << DisSegName << "," << DisSectName << ") section\n";
@@ -7615,7 +7619,7 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
76157619

76167620
if (PrintSource || PrintLines) {
76177621
formatted_raw_ostream FOS(outs());
7618-
SP.printSourceLine(FOS, {PC, SectIdx}, Filename, LEP);
7622+
SP->printSourceLine(FOS, {PC, SectIdx}, Filename, *LEP);
76197623
}
76207624

76217625
if (LeadingAddr) {
@@ -7711,7 +7715,7 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
77117715

77127716
if (PrintSource || PrintLines) {
77137717
formatted_raw_ostream FOS(outs());
7714-
SP.printSourceLine(FOS, {PC, SectIdx}, Filename, LEP);
7718+
SP->printSourceLine(FOS, {PC, SectIdx}, Filename, *LEP);
77157719
}
77167720

77177721
if (DumpAndSkipDataInCode(PC, Bytes.data() + Index, Dices, InstSize))

0 commit comments

Comments
 (0)