| 1 |
//===-- TestLineEntry.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 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "gtest/gtest.h" |
| 10 |
#include <iostream> |
| 11 |
#include <optional> |
| 12 |
|
| 13 |
#include "Plugins/ObjectFile/Mach-O/ObjectFileMachO.h" |
| 14 |
#include "Plugins/SymbolFile/DWARF/DWARFASTParserClang.h" |
| 15 |
#include "Plugins/SymbolFile/DWARF/SymbolFileDWARF.h" |
| 16 |
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h" |
| 17 |
#include "TestingSupport/SubsystemRAII.h" |
| 18 |
#include "TestingSupport/TestUtilities.h" |
| 19 |
|
| 20 |
#include "lldb/Core/Module.h" |
| 21 |
#include "lldb/Host/FileSystem.h" |
| 22 |
#include "lldb/Host/HostInfo.h" |
| 23 |
#include "lldb/Symbol/CompileUnit.h" |
| 24 |
#include "lldb/Symbol/SymbolContext.h" |
| 25 |
|
| 26 |
#include "llvm/Support/FileUtilities.h" |
| 27 |
#include "llvm/Support/Program.h" |
| 28 |
#include "llvm/Testing/Support/Error.h" |
| 29 |
|
| 30 |
using namespace lldb_private; |
| 31 |
using namespace lldb; |
| 32 |
|
| 33 |
class LineEntryTest : public testing::Test { |
| 34 |
SubsystemRAII<FileSystem, HostInfo, ObjectFileMachO, SymbolFileDWARF, |
| 35 |
TypeSystemClang> |
| 36 |
subsystem; |
| 37 |
|
| 38 |
public: |
| 39 |
void SetUp() override; |
| 40 |
|
| 41 |
protected: |
| 42 |
llvm::Expected<SymbolContextList> |
| 43 |
GetLineEntriesForLine(uint32_t line, std::optional<uint16_t> column); |
| 44 |
std::optional<TestFile> m_file; |
| 45 |
ModuleSP m_module_sp; |
| 46 |
}; |
| 47 |
|
| 48 |
void LineEntryTest::SetUp() { |
| 49 |
auto ExpectedFile = TestFile::fromYamlFile("inlined-functions.yaml"); |
| 50 |
ASSERT_THAT_EXPECTED(ExpectedFile, llvm::Succeeded()); |
| 51 |
m_file.emplace(std::move(*ExpectedFile)); |
| 52 |
m_module_sp = std::make_shared<Module>(m_file->moduleSpec()); |
| 53 |
} |
| 54 |
|
| 55 |
// TODO: Handle SourceLocationSpec column information |
| 56 |
llvm::Expected<SymbolContextList> LineEntryTest::GetLineEntriesForLine( |
| 57 |
uint32_t line, std::optional<uint16_t> column = std::nullopt) { |
| 58 |
SymbolContextList sc_comp_units; |
| 59 |
SymbolContextList sc_line_entries; |
| 60 |
FileSpec file_spec("inlined-functions.cpp"); |
| 61 |
m_module_sp->ResolveSymbolContextsForFileSpec( |
| 62 |
file_spec, line, /*check_inlines=*/true, lldb::eSymbolContextCompUnit, |
| 63 |
sc_comp_units); |
| 64 |
if (sc_comp_units.GetSize() == 0) |
| 65 |
return llvm::createStringError(llvm::inconvertibleErrorCode(), |
| 66 |
"No comp unit found on the test object."); |
| 67 |
|
| 68 |
SourceLocationSpec location_spec(file_spec, line, column, |
| 69 |
/*check_inlines=*/true, |
| 70 |
/*exact_match=*/true); |
| 71 |
|
| 72 |
sc_comp_units[0].comp_unit->ResolveSymbolContext( |
| 73 |
location_spec, eSymbolContextLineEntry, sc_line_entries); |
| 74 |
if (sc_line_entries.GetSize() == 0) |
| 75 |
return llvm::createStringError(llvm::inconvertibleErrorCode(), |
| 76 |
"No line entry found on the test object."); |
| 77 |
return sc_line_entries; |
| 78 |
} |
| 79 |
|
| 80 |
// This tests if we can get all line entries that match the passed line, if |
| 81 |
// no column is specified. |
| 82 |
TEST_F(LineEntryTest, GetAllExactLineMatchesWithoutColumn) { |
| 83 |
auto sc_line_entries = GetLineEntriesForLine(12); |
| 84 |
ASSERT_THAT_EXPECTED(sc_line_entries, llvm::Succeeded()); |
| 85 |
ASSERT_EQ(sc_line_entries->NumLineEntriesWithLine(12), 6u); |
| 86 |
} |
| 87 |
|
| 88 |
// This tests if we can get exact line and column matches. |
| 89 |
TEST_F(LineEntryTest, GetAllExactLineColumnMatches) { |
| 90 |
auto sc_line_entries = GetLineEntriesForLine(12, 39); |
| 91 |
ASSERT_THAT_EXPECTED(sc_line_entries, llvm::Succeeded()); |
| 92 |
ASSERT_EQ(sc_line_entries->NumLineEntriesWithLine(12), 1u); |
| 93 |
auto line_entry = sc_line_entries.get()[0].line_entry; |
| 94 |
ASSERT_EQ(line_entry.column, 39); |
| 95 |
} |
| 96 |
|
| 97 |
TEST_F(LineEntryTest, GetSameLineContiguousAddressRangeNoInlines) { |
| 98 |
auto sc_line_entries = GetLineEntriesForLine(18); |
| 99 |
ASSERT_THAT_EXPECTED(sc_line_entries, llvm::Succeeded()); |
| 100 |
auto line_entry = sc_line_entries.get()[0].line_entry; |
| 101 |
bool include_inlined_functions = false; |
| 102 |
auto range = |
| 103 |
line_entry.GetSameLineContiguousAddressRange(include_inlined_functions); |
| 104 |
ASSERT_EQ(range.GetByteSize(), (uint64_t)0x24); |
| 105 |
} |
| 106 |
|
| 107 |
TEST_F(LineEntryTest, GetSameLineContiguousAddressRangeOneInline) { |
| 108 |
auto sc_line_entries = GetLineEntriesForLine(18); |
| 109 |
ASSERT_THAT_EXPECTED(sc_line_entries, llvm::Succeeded()); |
| 110 |
auto line_entry = sc_line_entries.get()[0].line_entry; |
| 111 |
bool include_inlined_functions = true; |
| 112 |
auto range = |
| 113 |
line_entry.GetSameLineContiguousAddressRange(include_inlined_functions); |
| 114 |
ASSERT_EQ(range.GetByteSize(), (uint64_t)0x49); |
| 115 |
} |
| 116 |
|
| 117 |
TEST_F(LineEntryTest, GetSameLineContiguousAddressRangeNestedInline) { |
| 118 |
auto sc_line_entries = GetLineEntriesForLine(12); |
| 119 |
ASSERT_THAT_EXPECTED(sc_line_entries, llvm::Succeeded()); |
| 120 |
auto line_entry = sc_line_entries.get()[0].line_entry; |
| 121 |
bool include_inlined_functions = true; |
| 122 |
auto range = |
| 123 |
line_entry.GetSameLineContiguousAddressRange(include_inlined_functions); |
| 124 |
ASSERT_EQ(range.GetByteSize(), (uint64_t)0x33); |
| 125 |
} |
| 126 |
|
| 127 |
/* |
| 128 |
# inlined-functions.cpp |
| 129 |
inline __attribute__((always_inline)) int sum2(int a, int b) { |
| 130 |
int result = a + b; |
| 131 |
return result; |
| 132 |
} |
| 133 |
|
| 134 |
int sum3(int a, int b, int c) { |
| 135 |
int result = a + b + c; |
| 136 |
return result; |
| 137 |
} |
| 138 |
|
| 139 |
inline __attribute__((always_inline)) int sum4(int a, int b, int c, int d) { |
| 140 |
int result = sum2(a, b) + sum2(c, d); |
| 141 |
result += 0; |
| 142 |
return result; |
| 143 |
} |
| 144 |
|
| 145 |
int main(int argc, char** argv) { |
| 146 |
sum3(3, 4, 5) + sum2(1, 2); |
| 147 |
int sum = sum4(1, 2, 3, 4); |
| 148 |
sum2(5, 6); |
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
// g++ -c inlined-functions.cpp -o inlined-functions.o -g -Wno-unused-value |
| 153 |
// obj2yaml inlined-functions.o > inlined-functions.yaml |
| 154 |
|
| 155 |
# Dump of source line per address: |
| 156 |
# inlined-functions.cpp is src.cpp for space considerations. |
| 157 |
0x20: src.cpp:17 |
| 158 |
0x21: src.cpp:17 |
| 159 |
0x26: src.cpp:17 |
| 160 |
0x27: src.cpp:17 |
| 161 |
0x29: src.cpp:17 |
| 162 |
0x2e: src.cpp:17 |
| 163 |
0x2f: src.cpp:17 |
| 164 |
0x31: src.cpp:17 |
| 165 |
0x36: src.cpp:18 |
| 166 |
0x37: src.cpp:18 |
| 167 |
0x39: src.cpp:18 |
| 168 |
0x3e: src.cpp:18 |
| 169 |
0x3f: src.cpp:18 |
| 170 |
0x41: src.cpp:18 |
| 171 |
0x46: src.cpp:18 |
| 172 |
0x47: src.cpp:18 |
| 173 |
0x49: src.cpp:18 |
| 174 |
0x4e: src.cpp:18 |
| 175 |
0x4f: src.cpp:18 |
| 176 |
0x51: src.cpp:18 |
| 177 |
0x56: src.cpp:18 |
| 178 |
0x57: src.cpp:18 |
| 179 |
0x59: src.cpp:18 |
| 180 |
0x5e: src.cpp:18 -> [email protected]:2 |
| 181 |
0x5f: src.cpp:18 -> [email protected]:2 |
| 182 |
0x61: src.cpp:18 -> [email protected]:2 |
| 183 |
0x66: src.cpp:18 -> [email protected]:2 |
| 184 |
0x67: src.cpp:18 -> [email protected]:2 |
| 185 |
0x69: src.cpp:18 -> [email protected]:2 |
| 186 |
0x6e: src.cpp:18 -> [email protected]:2 |
| 187 |
0x6f: src.cpp:18 -> [email protected]:2 |
| 188 |
0x71: src.cpp:18 -> [email protected]:2 |
| 189 |
0x76: src.cpp:18 -> [email protected]:2 |
| 190 |
0x77: src.cpp:18 -> [email protected]:2 |
| 191 |
0x79: src.cpp:18 -> [email protected]:2 |
| 192 |
0x7e: src.cpp:18 -> [email protected]:2 |
| 193 |
0x7f: src.cpp:19 -> [email protected]:12 |
| 194 |
0x81: src.cpp:19 -> [email protected]:12 |
| 195 |
0x86: src.cpp:19 -> [email protected]:12 |
| 196 |
0x87: src.cpp:19 -> [email protected]:12 |
| 197 |
0x89: src.cpp:19 -> [email protected]:12 |
| 198 |
0x8e: src.cpp:19 -> [email protected]:12 -> [email protected]:2 |
| 199 |
0x8f: src.cpp:19 -> [email protected]:12 -> [email protected]:2 |
| 200 |
0x91: src.cpp:19 -> [email protected]:12 -> [email protected]:2 |
| 201 |
0x96: src.cpp:19 -> [email protected]:12 -> [email protected]:3 |
| 202 |
0x97: src.cpp:19 -> [email protected]:12 |
| 203 |
0x99: src.cpp:19 -> [email protected]:12 |
| 204 |
0x9e: src.cpp:19 -> [email protected]:12 |
| 205 |
0x9f: src.cpp:19 -> [email protected]:12 |
| 206 |
0xa1: src.cpp:19 -> [email protected]:12 |
| 207 |
0xa6: src.cpp:19 -> [email protected]:12 -> [email protected]:2 |
| 208 |
0xa7: src.cpp:19 -> [email protected]:12 -> [email protected]:2 |
| 209 |
0xa9: src.cpp:19 -> [email protected]:12 -> [email protected]:2 |
| 210 |
0xae: src.cpp:19 -> [email protected]:12 |
| 211 |
0xaf: src.cpp:19 -> [email protected]:12 |
| 212 |
0xb1: src.cpp:19 -> [email protected]:12 |
| 213 |
0xb6: src.cpp:19 -> [email protected]:13 |
| 214 |
0xb7: src.cpp:19 -> [email protected]:13 |
| 215 |
0xb9: src.cpp:19 -> [email protected]:14 |
| 216 |
0xbe: src.cpp:19 |
| 217 |
0xbf: src.cpp:19 |
| 218 |
0xc1: src.cpp:19 |
| 219 |
0xc6: src.cpp:19 |
| 220 |
0xc7: src.cpp:19 |
| 221 |
0xc9: src.cpp:19 |
| 222 |
0xce: src.cpp:20 -> [email protected]:2 |
| 223 |
0xcf: src.cpp:20 -> [email protected]:2 |
| 224 |
0xd1: src.cpp:20 -> [email protected]:2 |
| 225 |
0xd6: src.cpp:21 |
| 226 |
0xd7: src.cpp:21 |
| 227 |
0xd9: src.cpp:21 |
| 228 |
0xde: src.cpp:21 |
| 229 |
*/ |