1 //===- DWARFYAMLTest.cpp - Tests for DWARFYAML.cpp ------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ObjectYAML/DWARFYAML.h"
10 #include "llvm/ObjectYAML/DWARFEmitter.h"
11 #include "llvm/Support/Error.h"
12 #include "llvm/Support/SourceMgr.h"
13 #include "llvm/Support/YAMLTraits.h"
14 #include "llvm/Testing/Support/Error.h"
15 #include "gtest/gtest.h"
19 template <class T
> static Error
parseDWARFYAML(StringRef Yaml
, T
&Data
) {
20 SMDiagnostic GenerateDiag
;
22 Yaml
, /*Ctxt=*/nullptr,
23 [](const SMDiagnostic
&Diag
, void *DiagContext
) {
24 *static_cast<SMDiagnostic
*>(DiagContext
) = Diag
;
30 return createStringError(YIn
.error(), GenerateDiag
.getMessage());
32 return Error::success();
35 TEST(DebugAddrSection
, TestParseDebugAddrYAML
) {
43 EXPECT_THAT_ERROR(parseDWARFYAML(Yaml
, Data
), Succeeded());
46 TEST(DebugAddrSection
, TestMissingVersion
) {
51 DWARFYAML::AddrTableEntry AddrTableEntry
;
52 EXPECT_THAT_ERROR(parseDWARFYAML(Yaml
, AddrTableEntry
),
53 FailedWithMessage("missing required key 'Version'"));
56 TEST(DebugAddrSection
, TestUnexpectedKey
) {
63 DWARFYAML::AddrTableEntry AddrTableEntry
;
64 EXPECT_THAT_ERROR(parseDWARFYAML(Yaml
, AddrTableEntry
),
65 FailedWithMessage("unknown key 'Blah'"));
68 TEST(DebugPubSection
, TestDebugPubSection
) {
92 ASSERT_THAT_ERROR(parseDWARFYAML(Yaml
, Data
), Succeeded());
94 ASSERT_TRUE(Data
.PubNames
.has_value());
95 DWARFYAML::PubSection PubNames
= *Data
.PubNames
;
97 ASSERT_EQ(PubNames
.Entries
.size(), 2u);
98 EXPECT_EQ((uint32_t)PubNames
.Entries
[0].DieOffset
, 0x1234u
);
99 EXPECT_EQ(PubNames
.Entries
[0].Name
, "abc");
100 EXPECT_EQ((uint32_t)PubNames
.Entries
[1].DieOffset
, 0x4321u
);
101 EXPECT_EQ(PubNames
.Entries
[1].Name
, "def");
103 ASSERT_TRUE(Data
.PubTypes
.has_value());
104 DWARFYAML::PubSection PubTypes
= *Data
.PubTypes
;
106 ASSERT_EQ(PubTypes
.Entries
.size(), 2u);
107 EXPECT_EQ((uint32_t)PubTypes
.Entries
[0].DieOffset
, 0x1234u
);
108 EXPECT_EQ(PubTypes
.Entries
[0].Name
, "abc");
109 EXPECT_EQ((uint32_t)PubTypes
.Entries
[1].DieOffset
, 0x4321u
);
110 EXPECT_EQ(PubTypes
.Entries
[1].Name
, "def");
113 TEST(DebugPubSection
, TestUnexpectedDescriptor
) {
125 DWARFYAML::Data Data
;
126 EXPECT_THAT_ERROR(parseDWARFYAML(Yaml
, Data
),
127 FailedWithMessage("unknown key 'Descriptor'"));
130 TEST(DebugGNUPubSection
, TestDebugGNUPubSections
) {
157 DWARFYAML::Data Data
;
158 ASSERT_THAT_ERROR(parseDWARFYAML(Yaml
, Data
), Succeeded());
160 ASSERT_TRUE(Data
.GNUPubNames
.has_value());
161 DWARFYAML::PubSection GNUPubNames
= *Data
.GNUPubNames
;
163 ASSERT_EQ(GNUPubNames
.Entries
.size(), 2u);
164 EXPECT_EQ((uint32_t)GNUPubNames
.Entries
[0].DieOffset
, 0x1234u
);
165 EXPECT_EQ((uint8_t)GNUPubNames
.Entries
[0].Descriptor
, 0x12);
166 EXPECT_EQ(GNUPubNames
.Entries
[0].Name
, "abc");
167 EXPECT_EQ((uint32_t)GNUPubNames
.Entries
[1].DieOffset
, 0x4321u
);
168 EXPECT_EQ((uint8_t)GNUPubNames
.Entries
[1].Descriptor
, 0x34);
169 EXPECT_EQ(GNUPubNames
.Entries
[1].Name
, "def");
171 ASSERT_TRUE(Data
.GNUPubTypes
.has_value());
172 DWARFYAML::PubSection GNUPubTypes
= *Data
.GNUPubTypes
;
174 ASSERT_EQ(GNUPubTypes
.Entries
.size(), 2u);
175 EXPECT_EQ((uint32_t)GNUPubTypes
.Entries
[0].DieOffset
, 0x1234u
);
176 EXPECT_EQ((uint8_t)GNUPubTypes
.Entries
[0].Descriptor
, 0x12);
177 EXPECT_EQ(GNUPubTypes
.Entries
[0].Name
, "abc");
178 EXPECT_EQ((uint32_t)GNUPubTypes
.Entries
[1].DieOffset
, 0x4321u
);
179 EXPECT_EQ((uint8_t)GNUPubTypes
.Entries
[1].Descriptor
, 0x34);
180 EXPECT_EQ(GNUPubTypes
.Entries
[1].Name
, "def");
183 TEST(DebugGNUPubSection
, TestMissingDescriptor
) {
194 DWARFYAML::Data Data
;
195 EXPECT_THAT_ERROR(parseDWARFYAML(Yaml
, Data
),
196 FailedWithMessage("missing required key 'Descriptor'"));