summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp')
-rw-r--r--llvm/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/llvm/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp b/llvm/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp
index 0d299c9d840..1be0363adb0 100644
--- a/llvm/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp
+++ b/llvm/unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp
@@ -1007,6 +1007,99 @@ TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Addresses) {
TestAddresses<4, AddrType>();
}
+TEST(DWARFDebugInfo, TestStringOffsets) {
+ Triple Triple = getHostTripleForAddrSize(sizeof(void *));
+ if (!isConfigurationSupported(Triple))
+ return;
+
+ const char *String1 = "Hello";
+ const char *String2 = "World";
+
+ auto ExpectedDG = dwarfgen::Generator::create(Triple, 5);
+ ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
+ dwarfgen::Generator *DG = ExpectedDG.get().get();
+ dwarfgen::CompileUnit &CU = DG->addCompileUnit();
+ dwarfgen::DIE CUDie = CU.getUnitDIE();
+
+ CUDie.addStrOffsetsBaseAttribute();
+
+ uint16_t Attr = DW_AT_lo_user;
+
+ // Create our strings. First we create a non-indexed reference to String1,
+ // followed by an indexed String2. Finally, we add an indexed reference to
+ // String1.
+ const auto Attr1 = static_cast<dwarf::Attribute>(Attr++);
+ CUDie.addAttribute(Attr1, DW_FORM_strp, String1);
+
+ const auto Attr2 = static_cast<dwarf::Attribute>(Attr++);
+ CUDie.addAttribute(Attr2, DW_FORM_strx, String2);
+
+ const auto Attr3 = static_cast<dwarf::Attribute>(Attr++);
+ CUDie.addAttribute(Attr3, DW_FORM_strx, String1);
+
+ // Generate the DWARF
+ StringRef FileBytes = DG->generate();
+ MemoryBufferRef FileBuffer(FileBytes, "dwarf");
+ auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
+ ASSERT_TRUE((bool)Obj);
+ std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
+ uint32_t NumCUs = DwarfContext->getNumCompileUnits();
+ ASSERT_EQ(NumCUs, 1u);
+ DWARFUnit *U = DwarfContext->getUnitAtIndex(0);
+ auto DieDG = U->getUnitDIE(false);
+ ASSERT_TRUE(DieDG.isValid());
+
+ // Now make sure the string offsets came out properly. Attr2 should have index
+ // 0 (because it was the first indexed string) even though the string itself
+ // was added eariler.
+ auto Extracted1 = toString(DieDG.find(Attr1));
+ ASSERT_TRUE((bool)Extracted1);
+ EXPECT_STREQ(String1, *Extracted1);
+
+ Optional<DWARFFormValue> Form2 = DieDG.find(Attr2);
+ ASSERT_TRUE((bool)Form2);
+ EXPECT_EQ(0u, Form2->getRawUValue());
+ auto Extracted2 = toString(Form2);
+ ASSERT_TRUE((bool)Extracted2);
+ EXPECT_STREQ(String2, *Extracted2);
+
+ Optional<DWARFFormValue> Form3 = DieDG.find(Attr3);
+ ASSERT_TRUE((bool)Form3);
+ EXPECT_EQ(1u, Form3->getRawUValue());
+ auto Extracted3 = toString(Form3);
+ ASSERT_TRUE((bool)Extracted3);
+ EXPECT_STREQ(String1, *Extracted3);
+}
+
+TEST(DWARFDebugInfo, TestEmptyStringOffsets) {
+ Triple Triple = getHostTripleForAddrSize(sizeof(void *));
+ if (!isConfigurationSupported(Triple))
+ return;
+
+ const char *String1 = "Hello";
+
+ auto ExpectedDG = dwarfgen::Generator::create(Triple, 5);
+ ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
+ dwarfgen::Generator *DG = ExpectedDG.get().get();
+ dwarfgen::CompileUnit &CU = DG->addCompileUnit();
+ dwarfgen::DIE CUDie = CU.getUnitDIE();
+
+ uint16_t Attr = DW_AT_lo_user;
+
+ // We shall insert only one string. It will be referenced directly.
+ const auto Attr1 = static_cast<dwarf::Attribute>(Attr++);
+ CUDie.addAttribute(Attr1, DW_FORM_strp, String1);
+
+ // Generate the DWARF
+ StringRef FileBytes = DG->generate();
+ MemoryBufferRef FileBuffer(FileBytes, "dwarf");
+ auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
+ ASSERT_TRUE((bool)Obj);
+ std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
+ EXPECT_TRUE(
+ DwarfContext->getDWARFObj().getStringOffsetSection().Data.empty());
+}
+
TEST(DWARFDebugInfo, TestRelations) {
Triple Triple = getHostTripleForAddrSize(sizeof(void *));
if (!isConfigurationSupported(Triple))
OpenPOWER on IntegriCloud