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