[clang] Fix -Wdangling false negative regressions caused by 117315 (#118088)
[llvm-project.git] / lldb / unittests / Symbol / JSONSymbolTest.cpp
blob76c34b89f902fcc365faf632b3ad1aae090fee54
1 //===-- JSONSymbolTest.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 "lldb/Core/Section.h"
10 #include "lldb/Symbol/Symbol.h"
11 #include "llvm/Testing/Support/Error.h"
13 #include "gtest/gtest.h"
15 using namespace lldb;
16 using namespace llvm;
17 using namespace lldb_private;
19 static std::string g_error_no_section_list = "no section list provided";
20 static std::string g_error_both_value_and_address =
21 "symbol cannot contain both a value and an address";
22 static std::string g_error_neither_value_or_address =
23 "symbol must contain either a value or an address";
25 TEST(JSONSymbolTest, DeserializeCodeAddress) {
26 std::string text = R"(
28 "name": "foo",
29 "type": "code",
30 "size": 32,
31 "address": 4096
32 })";
34 Expected<json::Value> json = json::parse(text);
35 ASSERT_TRUE(static_cast<bool>(json));
37 json::Path::Root root;
38 JSONSymbol json_symbol;
39 ASSERT_TRUE(fromJSON(*json, json_symbol, root));
41 SectionSP sect_sp(new Section(
42 /*module_sp=*/ModuleSP(),
43 /*obj_file=*/nullptr,
44 /*sect_id=*/1,
45 /*name=*/ConstString(".text"),
46 /*sect_type=*/eSectionTypeCode,
47 /*file_vm_addr=*/0x1000,
48 /*vm_size=*/0x1000,
49 /*file_offset=*/0,
50 /*file_size=*/0,
51 /*log2align=*/5,
52 /*flags=*/0x10203040));
53 SectionList sect_list;
54 sect_list.AddSection(sect_sp);
56 Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, &sect_list);
57 EXPECT_THAT_EXPECTED(symbol, llvm::Succeeded());
58 EXPECT_EQ(symbol->GetName(), ConstString("foo"));
59 EXPECT_EQ(symbol->GetFileAddress(), static_cast<lldb::addr_t>(0x1000));
60 EXPECT_EQ(symbol->GetType(), eSymbolTypeCode);
63 TEST(JSONSymbolTest, DeserializeCodeValue) {
64 std::string text = R"(
66 "name": "foo",
67 "type": "code",
68 "size": 32,
69 "value": 4096
70 })";
72 Expected<json::Value> json = json::parse(text);
73 EXPECT_THAT_EXPECTED(json, llvm::Succeeded());
75 json::Path::Root root;
76 JSONSymbol json_symbol;
77 ASSERT_TRUE(fromJSON(*json, json_symbol, root));
79 SectionList sect_list;
81 Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, &sect_list);
82 EXPECT_THAT_EXPECTED(symbol, llvm::Succeeded());
83 EXPECT_EQ(symbol->GetName(), ConstString("foo"));
84 EXPECT_EQ(symbol->GetRawValue(), static_cast<lldb::addr_t>(0x1000));
85 EXPECT_EQ(symbol->GetType(), eSymbolTypeCode);
88 TEST(JSONSymbolTest, JSONInvalidValueAndAddress) {
89 std::string text = R"(
91 "name": "foo",
92 "type": "code",
93 "size": 32,
94 "value": 4096,
95 "address": 4096
96 })";
98 Expected<json::Value> json = json::parse(text);
99 EXPECT_THAT_EXPECTED(json, llvm::Succeeded());
101 json::Path::Root root;
102 JSONSymbol json_symbol;
103 ASSERT_FALSE(fromJSON(*json, json_symbol, root));
106 TEST(JSONSymbolTest, JSONInvalidNoValueOrAddress) {
107 std::string text = R"(
109 "name": "foo",
110 "type": "code",
111 "size": 32
112 })";
114 Expected<json::Value> json = json::parse(text);
115 EXPECT_THAT_EXPECTED(json, llvm::Succeeded());
117 json::Path::Root root;
118 JSONSymbol json_symbol;
119 ASSERT_FALSE(fromJSON(*json, json_symbol, root));
122 TEST(JSONSymbolTest, JSONInvalidType) {
123 std::string text = R"(
125 "name": "foo",
126 "type": "bogus",
127 "value": 4096,
128 "size": 32
129 })";
131 Expected<json::Value> json = json::parse(text);
132 EXPECT_THAT_EXPECTED(json, llvm::Succeeded());
134 json::Path::Root root;
135 JSONSymbol json_symbol;
136 ASSERT_FALSE(fromJSON(*json, json_symbol, root));
139 TEST(JSONSymbolTest, SymbolInvalidNoSectionList) {
140 JSONSymbol json_symbol;
141 json_symbol.value = 0x1;
143 Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, nullptr);
144 EXPECT_THAT_EXPECTED(symbol,
145 llvm::FailedWithMessage(g_error_no_section_list));
148 TEST(JSONSymbolTest, SymbolInvalidValueAndAddress) {
149 JSONSymbol json_symbol;
150 json_symbol.value = 0x1;
151 json_symbol.address = 0x2;
153 SectionList sect_list;
155 Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, &sect_list);
156 EXPECT_THAT_EXPECTED(symbol,
157 llvm::FailedWithMessage(g_error_both_value_and_address));
160 TEST(JSONSymbolTest, SymbolInvalidNoValueOrAddress) {
161 JSONSymbol json_symbol;
163 SectionList sect_list;
165 Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, &sect_list);
166 EXPECT_THAT_EXPECTED(
167 symbol, llvm::FailedWithMessage(g_error_neither_value_or_address));
170 TEST(JSONSymbolTest, SymbolInvalidAddressNotInSection) {
171 JSONSymbol json_symbol;
172 json_symbol.address = 0x0fff;
174 SectionSP sect_sp(new Section(
175 /*module_sp=*/ModuleSP(),
176 /*obj_file=*/nullptr,
177 /*sect_id=*/1,
178 /*name=*/ConstString(".text"),
179 /*sect_type=*/eSectionTypeCode,
180 /*file_vm_addr=*/0x1000,
181 /*vm_size=*/0x1000,
182 /*file_offset=*/0,
183 /*file_size=*/0,
184 /*log2align=*/5,
185 /*flags=*/0x10203040));
186 SectionList sect_list;
187 sect_list.AddSection(sect_sp);
189 Expected<Symbol> symbol = Symbol::FromJSON(json_symbol, &sect_list);
190 EXPECT_THAT_EXPECTED(
191 symbol, llvm::FailedWithMessage("no section found for address: 0xfff"));