Skip to content

Commit 6dbe82f

Browse files
authored
[NFC][DebugInfo] Wrap DILineInfo return type with std::optional to handle missing debug info. (#129792)
Currently, `DIContext::getLineInfoForAddress` and `DIContext::getLineInfoForDataAddress` returns empty DILineInfo when the debug info is missing for the given address. This is not differentiable with the case when debug info is found for the given address but the debug info is default value (filename:linenum is <invalid>:0). This change wraps the return types of `DIContext::getLineInfoForAddress` and `DIContext::getLineInfoForDataAddress` with `std::optional`.
1 parent 2e6402c commit 6dbe82f

File tree

13 files changed

+61
-45
lines changed

13 files changed

+61
-45
lines changed

llvm/include/llvm/DebugInfo/BTF/BTFContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ class BTFContext final : public DIContext {
3030
// BTF is no DWARF, so ignore this operation for now.
3131
}
3232

33-
DILineInfo getLineInfoForAddress(
33+
std::optional<DILineInfo> getLineInfoForAddress(
3434
object::SectionedAddress Address,
3535
DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
3636

37-
DILineInfo
37+
std::optional<DILineInfo>
3838
getLineInfoForDataAddress(object::SectionedAddress Address) override;
3939

4040
DILineInfoTable getLineInfoForAddressRange(

llvm/include/llvm/DebugInfo/DIContext.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,12 @@ class DIContext {
252252
return true;
253253
}
254254

255-
virtual DILineInfo getLineInfoForAddress(
255+
// For getLineInfoForAddress and getLineInfoForDataAddress, std::nullopt is
256+
// returned when debug info is missing for the given address.
257+
virtual std::optional<DILineInfo> getLineInfoForAddress(
256258
object::SectionedAddress Address,
257259
DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
258-
virtual DILineInfo
260+
virtual std::optional<DILineInfo>
259261
getLineInfoForDataAddress(object::SectionedAddress Address) = 0;
260262
virtual DILineInfoTable getLineInfoForAddressRange(
261263
object::SectionedAddress Address, uint64_t Size,

llvm/include/llvm/DebugInfo/DWARF/DWARFContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,10 @@ class DWARFContext : public DIContext {
386386
/// executable's debug info.
387387
DIEsForAddress getDIEsForAddress(uint64_t Address, bool CheckDWO = false);
388388

389-
DILineInfo getLineInfoForAddress(
389+
std::optional<DILineInfo> getLineInfoForAddress(
390390
object::SectionedAddress Address,
391391
DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
392-
DILineInfo
392+
std::optional<DILineInfo>
393393
getLineInfoForDataAddress(object::SectionedAddress Address) override;
394394
DILineInfoTable getLineInfoForAddressRange(
395395
object::SectionedAddress Address, uint64_t Size,

llvm/include/llvm/DebugInfo/PDB/PDBContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ namespace pdb {
4242

4343
void dump(raw_ostream &OS, DIDumpOptions DIDumpOpts) override;
4444

45-
DILineInfo getLineInfoForAddress(
45+
std::optional<DILineInfo> getLineInfoForAddress(
4646
object::SectionedAddress Address,
4747
DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
48-
DILineInfo
48+
std::optional<DILineInfo>
4949
getLineInfoForDataAddress(object::SectionedAddress Address) override;
5050
DILineInfoTable getLineInfoForAddressRange(
5151
object::SectionedAddress Address, uint64_t Size,

llvm/lib/DebugInfo/BTF/BTFContext.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ using namespace llvm;
2020
using object::ObjectFile;
2121
using object::SectionedAddress;
2222

23-
DILineInfo BTFContext::getLineInfoForAddress(SectionedAddress Address,
24-
DILineInfoSpecifier Specifier) {
23+
std::optional<DILineInfo>
24+
BTFContext::getLineInfoForAddress(SectionedAddress Address,
25+
DILineInfoSpecifier Specifier) {
2526
const BTF::BPFLineInfo *LineInfo = BTF.findLineInfo(Address);
2627
DILineInfo Result;
2728
if (!LineInfo)
28-
return Result;
29+
return std::nullopt;
2930

3031
Result.LineSource = BTF.findString(LineInfo->LineOff);
3132
Result.FileName = BTF.findString(LineInfo->FileNameOff);
@@ -34,9 +35,10 @@ DILineInfo BTFContext::getLineInfoForAddress(SectionedAddress Address,
3435
return Result;
3536
}
3637

37-
DILineInfo BTFContext::getLineInfoForDataAddress(SectionedAddress Address) {
38+
std::optional<DILineInfo>
39+
BTFContext::getLineInfoForDataAddress(SectionedAddress Address) {
3840
// BTF does not convey such information.
39-
return {};
41+
return std::nullopt;
4042
}
4143

4244
DILineInfoTable

llvm/lib/DebugInfo/DWARF/DWARFContext.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,8 +1730,9 @@ DWARFContext::getLocalsForAddress(object::SectionedAddress Address) {
17301730
return Result;
17311731
}
17321732

1733-
DILineInfo DWARFContext::getLineInfoForAddress(object::SectionedAddress Address,
1734-
DILineInfoSpecifier Spec) {
1733+
std::optional<DILineInfo>
1734+
DWARFContext::getLineInfoForAddress(object::SectionedAddress Address,
1735+
DILineInfoSpecifier Spec) {
17351736
DILineInfo Result;
17361737
DWARFCompileUnit *CU = getCompileUnitForCodeAddress(Address.Address);
17371738
if (!CU)
@@ -1751,7 +1752,7 @@ DILineInfo DWARFContext::getLineInfoForAddress(object::SectionedAddress Address,
17511752
return Result;
17521753
}
17531754

1754-
DILineInfo
1755+
std::optional<DILineInfo>
17551756
DWARFContext::getLineInfoForDataAddress(object::SectionedAddress Address) {
17561757
DILineInfo Result;
17571758
DWARFCompileUnit *CU = getCompileUnitForDataAddress(Address.Address);

llvm/lib/DebugInfo/GSYM/DwarfTransformer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ llvm::Error DwarfTransformer::verify(StringRef GsymPath,
741741
uint32_t NumDwarfInlineInfos = DwarfInlineInfos.getNumberOfFrames();
742742
if (NumDwarfInlineInfos == 0) {
743743
DwarfInlineInfos.addFrame(
744-
DICtx.getLineInfoForAddress(SectAddr, DLIS));
744+
DICtx.getLineInfoForAddress(SectAddr, DLIS).value_or(DILineInfo()));
745745
}
746746

747747
// Check for 1 entry that has no file and line info

llvm/lib/DebugInfo/PDB/PDBContext.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ PDBContext::PDBContext(const COFFObjectFile &Object,
3232

3333
void PDBContext::dump(raw_ostream &OS, DIDumpOptions DumpOpts){}
3434

35-
DILineInfo PDBContext::getLineInfoForAddress(object::SectionedAddress Address,
36-
DILineInfoSpecifier Specifier) {
35+
std::optional<DILineInfo>
36+
PDBContext::getLineInfoForAddress(object::SectionedAddress Address,
37+
DILineInfoSpecifier Specifier) {
3738
DILineInfo Result;
3839
Result.FunctionName = getFunctionName(Address.Address, Specifier.FNKind);
3940

@@ -64,7 +65,7 @@ DILineInfo PDBContext::getLineInfoForAddress(object::SectionedAddress Address,
6465
return Result;
6566
}
6667

67-
DILineInfo
68+
std::optional<DILineInfo>
6869
PDBContext::getLineInfoForDataAddress(object::SectionedAddress Address) {
6970
// Unimplemented. S_GDATA and S_LDATA in CodeView (used to describe global
7071
// variables) aren't capable of carrying line information.
@@ -84,9 +85,10 @@ PDBContext::getLineInfoForAddressRange(object::SectionedAddress Address,
8485
return Table;
8586

8687
while (auto LineInfo = LineNumbers->getNext()) {
87-
DILineInfo LineEntry = getLineInfoForAddress(
88-
{LineInfo->getVirtualAddress(), Address.SectionIndex}, Specifier);
89-
Table.push_back(std::make_pair(LineInfo->getVirtualAddress(), LineEntry));
88+
if (std::optional<DILineInfo> LineEntry = getLineInfoForAddress(
89+
{LineInfo->getVirtualAddress(), Address.SectionIndex}, Specifier))
90+
Table.push_back(
91+
std::make_pair(LineInfo->getVirtualAddress(), *LineEntry));
9092
}
9193
return Table;
9294
}
@@ -95,7 +97,8 @@ DIInliningInfo
9597
PDBContext::getInliningInfoForAddress(object::SectionedAddress Address,
9698
DILineInfoSpecifier Specifier) {
9799
DIInliningInfo InlineInfo;
98-
DILineInfo CurrentLine = getLineInfoForAddress(Address, Specifier);
100+
DILineInfo CurrentLine =
101+
getLineInfoForAddress(Address, Specifier).value_or(DILineInfo());
99102

100103
// Find the function at this address.
101104
std::unique_ptr<PDBSymbol> ParentFunc =

llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,11 @@ SymbolizableObjectFile::symbolizeCode(object::SectionedAddress ModuleOffset,
276276
if (ModuleOffset.SectionIndex == object::SectionedAddress::UndefSection)
277277
ModuleOffset.SectionIndex =
278278
getModuleSectionIndexForAddress(ModuleOffset.Address);
279-
DILineInfo LineInfo =
280-
DebugInfoContext->getLineInfoForAddress(ModuleOffset, LineInfoSpecifier);
279+
DILineInfo LineInfo;
280+
if (std::optional<DILineInfo> DBGLineInfo =
281+
DebugInfoContext->getLineInfoForAddress(ModuleOffset,
282+
LineInfoSpecifier))
283+
LineInfo = *DBGLineInfo;
281284

282285
// Override function name from symbol table if necessary.
283286
if (shouldOverrideWithSymbolTable(LineInfoSpecifier.FNKind, UseSymbolTable)) {
@@ -334,10 +337,11 @@ DIGlobal SymbolizableObjectFile::symbolizeData(
334337
Res.DeclFile = FileName;
335338

336339
// Try and get a better filename:lineno pair from the debuginfo, if present.
337-
DILineInfo DL = DebugInfoContext->getLineInfoForDataAddress(ModuleOffset);
338-
if (DL.Line != 0) {
339-
Res.DeclFile = DL.FileName;
340-
Res.DeclLine = DL.Line;
340+
std::optional<DILineInfo> DL =
341+
DebugInfoContext->getLineInfoForDataAddress(ModuleOffset);
342+
if (DL && DL->Line != 0) {
343+
Res.DeclFile = DL->FileName;
344+
Res.DeclLine = DL->Line;
341345
}
342346
return Res;
343347
}

llvm/lib/ExecutionEngine/Orc/Debugging/VTuneSupportPlugin.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ static VTuneMethodBatch getMethodBatch(LinkGraph &G, bool EmitDebugInfo) {
7171
SAddr, Sym->getSize(),
7272
DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath);
7373
Method.SourceFileSI = Batch.Strings.size();
74-
Batch.Strings.push_back(DC->getLineInfoForAddress(SAddr).FileName);
74+
Batch.Strings.push_back(
75+
DC->getLineInfoForAddress(SAddr).value_or(DILineInfo()).FileName);
7576
for (auto &LInfo : LinesInfo) {
7677
Method.LineTable.push_back(
7778
std::pair<unsigned, unsigned>{/*unsigned*/ Sym->getOffset(),

llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,9 +565,13 @@ static bool lookup(ObjectFile &Obj, DWARFContext &DICtx, uint64_t Address,
565565

566566
// TODO: it is neccessary to set proper SectionIndex here.
567567
// object::SectionedAddress::UndefSection works for only absolute addresses.
568-
if (DILineInfo LineInfo = DICtx.getLineInfoForAddress(
569-
{Lookup, object::SectionedAddress::UndefSection}))
568+
if (DILineInfo LineInfo =
569+
DICtx
570+
.getLineInfoForAddress(
571+
{Lookup, object::SectionedAddress::UndefSection})
572+
.value_or(DILineInfo())) {
570573
LineInfo.dump(OS);
574+
}
571575

572576
return true;
573577
}

llvm/tools/llvm-objdump/MachODump.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7646,7 +7646,8 @@ static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF,
76467646

76477647
// Print debug info.
76487648
if (diContext) {
7649-
DILineInfo dli = diContext->getLineInfoForAddress({PC, SectIdx});
7649+
DILineInfo dli = diContext->getLineInfoForAddress({PC, SectIdx})
7650+
.value_or(DILineInfo());
76507651
// Print valid line info if it changed.
76517652
if (dli != lastLine && dli.Line != 0)
76527653
outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'

llvm/unittests/DebugInfo/BTF/BTFParserTest.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -343,17 +343,15 @@ TEST(BTFParserTest, btfContext) {
343343
BTFParser BTF;
344344
std::unique_ptr<BTFContext> Ctx = BTFContext::create(Mock.makeObj());
345345

346-
DILineInfo I1 = Ctx->getLineInfoForAddress({16, 1});
347-
EXPECT_EQ(I1.Line, 7u);
348-
EXPECT_EQ(I1.Column, 1u);
349-
EXPECT_EQ(I1.FileName, "a.c");
350-
EXPECT_EQ(I1.LineSource, "first line");
351-
352-
DILineInfo I2 = Ctx->getLineInfoForAddress({24, 1});
353-
EXPECT_EQ(I2.Line, 0u);
354-
EXPECT_EQ(I2.Column, 0u);
355-
EXPECT_EQ(I2.FileName, DILineInfo::BadString);
356-
EXPECT_EQ(I2.LineSource, std::nullopt);
346+
std::optional<DILineInfo> I1 = Ctx->getLineInfoForAddress({16, 1});
347+
EXPECT_TRUE(I1.has_value());
348+
EXPECT_EQ(I1->Line, 7u);
349+
EXPECT_EQ(I1->Column, 1u);
350+
EXPECT_EQ(I1->FileName, "a.c");
351+
EXPECT_EQ(I1->LineSource, "first line");
352+
353+
std::optional<DILineInfo> I2 = Ctx->getLineInfoForAddress({24, 1});
354+
EXPECT_FALSE(I2.has_value());
357355
}
358356

359357
static uint32_t mkInfo(uint32_t Kind) { return Kind << 24; }

0 commit comments

Comments
 (0)