Skip to content

Commit 71e5488

Browse files
committed
DebugInfo: Migrate callers from getAsCString to dwarf::toString
This makes a bunch of these call sites independent of a follow-up change I'm making to have getAsCString return Expected<const char*> for more descriptive error messages so that the failures there can be communicated up to DWARFVerifier (or other callers who want to provide more verbose diagnostics) so DWARFVerifier doesn't have to re-implement the string lookup logic and error checking.
1 parent 5740bb8 commit 71e5488

File tree

9 files changed

+41
-46
lines changed

9 files changed

+41
-46
lines changed

llvm/include/llvm/DebugInfo/DWARF/DWARFFormValue.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ inline StringRef toStringRef(const Optional<DWARFFormValue> &V,
199199
/// form value's encoding wasn't a string.
200200
inline const char *toString(const Optional<DWARFFormValue> &V,
201201
const char *Default) {
202-
return toString(V).getValueOr(Default);
202+
if (auto E = toString(V))
203+
return *E;
204+
return Default;
203205
}
204206

205207
/// Take an optional DWARFFormValue and try to extract an unsigned constant.

llvm/lib/DWARFLinker/DWARFLinker.cpp

+16-17
Original file line numberDiff line numberDiff line change
@@ -223,22 +223,21 @@ static void analyzeImportedModule(
223223
SysRoot = CU.getSysRoot();
224224
if (!SysRoot.empty() && Path.startswith(SysRoot))
225225
return;
226-
if (Optional<DWARFFormValue> Val = DIE.find(dwarf::DW_AT_name))
227-
if (Optional<const char *> Name = Val->getAsCString()) {
228-
auto &Entry = (*ParseableSwiftInterfaces)[*Name];
229-
// The prepend path is applied later when copying.
230-
DWARFDie CUDie = CU.getOrigUnit().getUnitDIE();
231-
SmallString<128> ResolvedPath;
232-
if (sys::path::is_relative(Path))
233-
resolveRelativeObjectPath(ResolvedPath, CUDie);
234-
sys::path::append(ResolvedPath, Path);
235-
if (!Entry.empty() && Entry != ResolvedPath)
236-
ReportWarning(
237-
Twine("Conflicting parseable interfaces for Swift Module ") +
238-
*Name + ": " + Entry + " and " + Path,
239-
DIE);
240-
Entry = std::string(ResolvedPath.str());
241-
}
226+
Optional<const char*> Name = dwarf::toString(DIE.find(dwarf::DW_AT_name));
227+
if (!Name)
228+
return;
229+
auto &Entry = (*ParseableSwiftInterfaces)[*Name];
230+
// The prepend path is applied later when copying.
231+
DWARFDie CUDie = CU.getOrigUnit().getUnitDIE();
232+
SmallString<128> ResolvedPath;
233+
if (sys::path::is_relative(Path))
234+
resolveRelativeObjectPath(ResolvedPath, CUDie);
235+
sys::path::append(ResolvedPath, Path);
236+
if (!Entry.empty() && Entry != ResolvedPath)
237+
ReportWarning(Twine("Conflicting parseable interfaces for Swift Module ") +
238+
*Name + ": " + Entry + " and " + Path,
239+
DIE);
240+
Entry = std::string(ResolvedPath.str());
242241
}
243242

244243
/// The distinct types of work performed by the work loop in
@@ -846,7 +845,7 @@ void DWARFLinker::assignAbbrev(DIEAbbrev &Abbrev) {
846845
unsigned DWARFLinker::DIECloner::cloneStringAttribute(
847846
DIE &Die, AttributeSpec AttrSpec, const DWARFFormValue &Val,
848847
const DWARFUnit &U, OffsetsStringPool &StringPool, AttributesInfo &Info) {
849-
Optional<const char *> String = Val.getAsCString();
848+
Optional<const char *> String = dwarf::toString(Val);
850849
if (!String)
851850
return 0;
852851

llvm/lib/DebugInfo/DWARF/DWARFContext.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,7 @@ void DWARFContext::addLocalsForDie(DWARFCompileUnit *CU, DWARFDie Subprogram,
11951195
Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
11961196
Die = Origin;
11971197
if (auto NameAttr = Die.find(DW_AT_name))
1198-
if (Optional<const char *> Name = NameAttr->getAsCString())
1198+
if (Optional<const char *> Name = dwarf::toString(*NameAttr))
11991199
Local.Name = *Name;
12001200
if (auto Type = Die.getAttributeValueAsReferencedDie(DW_AT_type))
12011201
Local.Size = getTypeSize(Type, getCUAddrSize());

llvm/lib/DebugInfo/DWARF/DWARFDebugLine.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -1331,8 +1331,8 @@ Optional<StringRef> DWARFDebugLine::LineTable::getSourceByIndex(uint64_t FileInd
13311331
if (Kind == FileLineInfoKind::None || !Prologue.hasFileAtIndex(FileIndex))
13321332
return None;
13331333
const FileNameEntry &Entry = Prologue.getFileNameEntry(FileIndex);
1334-
if (Optional<const char *> source = Entry.Source.getAsCString())
1335-
return StringRef(*source);
1334+
if (auto E = dwarf::toString(Entry.Source))
1335+
return StringRef(*E);
13361336
return None;
13371337
}
13381338

@@ -1350,10 +1350,10 @@ bool DWARFDebugLine::Prologue::getFileNameByIndex(
13501350
if (Kind == FileLineInfoKind::None || !hasFileAtIndex(FileIndex))
13511351
return false;
13521352
const FileNameEntry &Entry = getFileNameEntry(FileIndex);
1353-
Optional<const char *> Name = Entry.Name.getAsCString();
1354-
if (!Name)
1353+
auto E = dwarf::toString(Entry.Name);
1354+
if (!E)
13551355
return false;
1356-
StringRef FileName = *Name;
1356+
StringRef FileName = *E;
13571357
if (Kind == FileLineInfoKind::RawValue ||
13581358
isPathAbsoluteOnWindowsOrPosix(FileName)) {
13591359
Result = std::string(FileName);
@@ -1372,11 +1372,10 @@ bool DWARFDebugLine::Prologue::getFileNameByIndex(
13721372
// relative names.
13731373
if ((Entry.DirIdx != 0 || Kind != FileLineInfoKind::RelativeFilePath) &&
13741374
Entry.DirIdx < IncludeDirectories.size())
1375-
IncludeDir = IncludeDirectories[Entry.DirIdx].getAsCString().getValue();
1375+
IncludeDir = dwarf::toStringRef(IncludeDirectories[Entry.DirIdx]);
13761376
} else {
13771377
if (0 < Entry.DirIdx && Entry.DirIdx <= IncludeDirectories.size())
1378-
IncludeDir =
1379-
IncludeDirectories[Entry.DirIdx - 1].getAsCString().getValue();
1378+
IncludeDir = dwarf::toStringRef(IncludeDirectories[Entry.DirIdx - 1]);
13801379
}
13811380

13821381
// For absolute paths only, include the compilation directory of compile unit.

llvm/lib/DebugInfo/DWARF/DWARFExpression.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ static void prettyPrintBaseTypeRef(DWARFUnit *U, raw_ostream &OS,
217217
if (DumpOpts.Verbose)
218218
OS << format("0x%08" PRIx64 " -> ", Operands[Operand]);
219219
OS << format("0x%08" PRIx64 ")", U->getOffset() + Operands[Operand]);
220-
if (auto Name = Die.find(dwarf::DW_AT_name))
221-
OS << " \"" << Name->getAsCString() << "\"";
220+
if (auto Name = dwarf::toString(Die.find(dwarf::DW_AT_name)))
221+
OS << " \"" << *Name << "\"";
222222
} else {
223223
OS << format(" <invalid base_type ref: 0x%" PRIx64 ">",
224224
Operands[Operand]);

llvm/tools/obj2yaml/dwarf2yaml.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ void dumpDebugInfo(DWARFContext &DCtx, DWARFYAML::Data &Y) {
291291
NewValue.Value = Val.getValue();
292292
break;
293293
case dwarf::DW_FORM_string:
294-
if (auto Val = FormValue.getValue().getAsCString())
295-
NewValue.CStr = Val.getValue();
294+
if (auto Val = dwarf::toString(FormValue))
295+
NewValue.CStr = *Val;
296296
break;
297297
case dwarf::DW_FORM_indirect:
298298
indirect = true;

llvm/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,7 @@ TEST(DWARFDebugInfo, TestAttributeIterators) {
15041504

15051505
ASSERT_NE(E, I);
15061506
EXPECT_EQ(I->Attr, DW_AT_name);
1507-
auto ActualCUPath = I->Value.getAsCString();
1507+
auto ActualCUPath = toString(I->Value);
15081508
EXPECT_EQ(CUPath, *ActualCUPath);
15091509

15101510
ASSERT_NE(E, ++I);

llvm/unittests/DebugInfo/DWARF/DWARFDebugLineTest.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ void checkDefaultPrologue(uint16_t Version, DwarfFormat Format,
170170
EXPECT_EQ(Prologue.StandardOpcodeLengths, ExpectedLengths);
171171
ASSERT_EQ(Prologue.IncludeDirectories.size(), 1u);
172172
ASSERT_EQ(Prologue.IncludeDirectories[0].getForm(), DW_FORM_string);
173-
EXPECT_STREQ(*Prologue.IncludeDirectories[0].getAsCString(), "a dir");
173+
EXPECT_STREQ(*toString(Prologue.IncludeDirectories[0]), "a dir");
174174
ASSERT_EQ(Prologue.FileNames.size(), 1u);
175175
ASSERT_EQ(Prologue.FileNames[0].Name.getForm(), DW_FORM_string);
176176
ASSERT_EQ(Prologue.FileNames[0].DirIdx, 0u);
177-
EXPECT_STREQ(*Prologue.FileNames[0].Name.getAsCString(), "a file");
177+
EXPECT_STREQ(*toString(Prologue.FileNames[0].Name), "a file");
178178
}
179179

180180
#ifdef _AIX

llvm/unittests/DebugInfo/DWARF/DwarfGenerator.cpp

+7-12
Original file line numberDiff line numberDiff line change
@@ -336,15 +336,13 @@ static void writeCString(StringRef Str, AsmPrinter &Asm) {
336336

337337
static void writeV2IncludeAndFileTable(const DWARFDebugLine::Prologue &Prologue,
338338
AsmPrinter &Asm) {
339-
for (auto Include : Prologue.IncludeDirectories) {
340-
assert(Include.getAsCString() && "expected a string form for include dir");
341-
writeCString(*Include.getAsCString(), Asm);
342-
}
339+
for (auto Include : Prologue.IncludeDirectories)
340+
writeCString(*toString(Include), Asm);
341+
343342
Asm.emitInt8(0);
344343

345344
for (auto File : Prologue.FileNames) {
346-
assert(File.Name.getAsCString() && "expected a string form for file name");
347-
writeCString(*File.Name.getAsCString(), Asm);
345+
writeCString(*toString(File.Name), Asm);
348346
Asm.emitULEB128(File.DirIdx);
349347
Asm.emitULEB128(File.ModTime);
350348
Asm.emitULEB128(File.Length);
@@ -360,10 +358,8 @@ static void writeV5IncludeAndFileTable(const DWARFDebugLine::Prologue &Prologue,
360358
Asm.emitULEB128(DW_LNCT_path);
361359
Asm.emitULEB128(DW_FORM_string);
362360
Asm.emitULEB128(Prologue.IncludeDirectories.size());
363-
for (auto Include : Prologue.IncludeDirectories) {
364-
assert(Include.getAsCString() && "expected a string form for include dir");
365-
writeCString(*Include.getAsCString(), Asm);
366-
}
361+
for (auto Include : Prologue.IncludeDirectories)
362+
writeCString(*toString(Include), Asm);
367363

368364
Asm.emitInt8(2); // file_name_entry_format_count.
369365
Asm.emitULEB128(DW_LNCT_path);
@@ -372,8 +368,7 @@ static void writeV5IncludeAndFileTable(const DWARFDebugLine::Prologue &Prologue,
372368
Asm.emitULEB128(DW_FORM_data1);
373369
Asm.emitULEB128(Prologue.FileNames.size());
374370
for (auto File : Prologue.FileNames) {
375-
assert(File.Name.getAsCString() && "expected a string form for file name");
376-
writeCString(*File.Name.getAsCString(), Asm);
371+
writeCString(*toString(File.Name), Asm);
377372
Asm.emitInt8(File.DirIdx);
378373
}
379374
}

0 commit comments

Comments
 (0)