[docs] Fix build-docs.sh
[llvm-project.git] / llvm / unittests / DebugInfo / DWARF / DWARFDieManualExtractTest.cpp
blobea54f321e44121f8a9066d2dc01f8f251b583da8
1 //===-llvm/unittest/DebugInfo/DWARFDieManualExtractTest.cpp---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "DwarfGenerator.h"
10 #include "DwarfUtils.h"
11 #include "llvm/BinaryFormat/Dwarf.h"
12 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
13 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
14 #include "llvm/ObjectYAML/DWARFEmitter.h"
15 #include "llvm/Testing/Support/Error.h"
16 #include "gtest/gtest.h"
18 using namespace llvm;
19 using namespace llvm::dwarf;
20 using namespace utils;
22 namespace {
24 TEST(DWARFDie, manualExtractDump) {
25 typedef uint32_t AddrType;
26 uint16_t Version = 4;
27 Triple Triple = getDefaultTargetTripleForAddrSize(sizeof(AddrType));
28 if (!isConfigurationSupported(Triple))
29 GTEST_SKIP();
31 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
32 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
33 dwarfgen::Generator *DG = ExpectedDG.get().get();
34 dwarfgen::CompileUnit &DGCU = DG->addCompileUnit();
35 dwarfgen::DIE CUDie = DGCU.getUnitDIE();
37 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
38 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
40 dwarfgen::DIE SubprogramDie = CUDie.addChild(DW_TAG_subprogram);
41 SubprogramDie.addAttribute(DW_AT_name, DW_FORM_strp, "main");
42 SubprogramDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, 0x1000U);
43 SubprogramDie.addAttribute(DW_AT_high_pc, DW_FORM_addr, 0x2000U);
45 StringRef FileBytes = DG->generate();
46 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
47 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
48 EXPECT_TRUE((bool)Obj);
49 std::unique_ptr<DWARFContext> Ctx = DWARFContext::create(**Obj);
51 DWARFCompileUnit *CU = Ctx->getCompileUnitForOffset(0);
52 ASSERT_NE(nullptr, CU);
53 // Manually extracting DWARF DIE.
54 uint64_t DIEOffset = CU->getOffset() + CU->getHeaderSize();
55 uint64_t NextCUOffset = CU->getNextUnitOffset();
56 DWARFDebugInfoEntry DieInfo;
57 DWARFDataExtractor DebugInfoData = CU->getDebugInfoExtractor();
58 ASSERT_TRUE(DieInfo.extractFast(*CU, &DIEOffset, DebugInfoData, NextCUOffset,
59 UINT32_MAX));
60 DWARFDie Die(CU, &DieInfo);
61 ASSERT_TRUE(Die.isValid());
62 ASSERT_TRUE(Die.hasChildren());
63 // Since we have extracted manually DieArray is empty.
64 // Dump function should respect the default flags and print just current DIE,
65 // and not explore children.
66 SmallString<512> Output;
67 raw_svector_ostream OS(Output);
68 Die.dump(OS);
69 constexpr size_t NumOfLines = 3;
70 SmallVector<StringRef, NumOfLines> Strings;
71 SmallVector<StringRef, NumOfLines> ValidStrings = {
72 "0x0000000b: DW_TAG_compile_unit",
73 " DW_AT_name (\"/tmp/main.c\")",
74 " DW_AT_language (DW_LANG_C)"};
75 Output.str().split(Strings, '\n', -1, false);
76 ASSERT_EQ(Strings.size(), NumOfLines);
77 for (size_t I = 0; I < NumOfLines; ++I)
78 EXPECT_EQ(ValidStrings[I], Strings[I]);
81 } // end anonymous namespace