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   bool check_inlines = true;
57   bool exact = true;
58   SymbolContextList sc_comp_units;
59   SymbolContextList sc_line_entries;
60   FileSpec file_spec("inlined-functions.cpp");
61   m_module_sp->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
62                                                 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   sc_comp_units[0].comp_unit->ResolveSymbolContext(
68       file_spec, line, check_inlines, exact, eSymbolContextLineEntry,
69       sc_line_entries);
70   if (sc_line_entries.GetSize() == 0)
71     return llvm::createStringError(llvm::inconvertibleErrorCode(),
72                                    "No line entry found on the test object.");
73   return sc_line_entries[0].line_entry;
74 }
75 
76 TEST_F(LineEntryTest, GetSameLineContiguousAddressRangeNoInlines) {
77   auto line_entry = GetLineEntryForLine(18);
78   ASSERT_THAT_EXPECTED(line_entry, llvm::Succeeded());
79   bool include_inlined_functions = false;
80   auto range =
81       line_entry->GetSameLineContiguousAddressRange(include_inlined_functions);
82   ASSERT_EQ(range.GetByteSize(), (uint64_t)0x24);
83 }
84 
85 TEST_F(LineEntryTest, GetSameLineContiguousAddressRangeOneInline) {
86   auto line_entry = GetLineEntryForLine(18);
87   ASSERT_THAT_EXPECTED(line_entry, llvm::Succeeded());
88   bool include_inlined_functions = true;
89   auto range =
90       line_entry->GetSameLineContiguousAddressRange(include_inlined_functions);
91   ASSERT_EQ(range.GetByteSize(), (uint64_t)0x49);
92 }
93 
94 TEST_F(LineEntryTest, GetSameLineContiguousAddressRangeNestedInline) {
95   auto line_entry = GetLineEntryForLine(12);
96   ASSERT_THAT_EXPECTED(line_entry, llvm::Succeeded());
97   bool include_inlined_functions = true;
98   auto range =
99       line_entry->GetSameLineContiguousAddressRange(include_inlined_functions);
100   ASSERT_EQ(range.GetByteSize(), (uint64_t)0x33);
101 }
102 
103 /*
104 # inlined-functions.cpp
105 inline __attribute__((always_inline)) int sum2(int a, int b) {
106     int result = a + b;
107     return result;
108 }
109 
110 int sum3(int a, int b, int c) {
111     int result = a + b + c;
112     return result;
113 }
114 
115 inline __attribute__((always_inline)) int sum4(int a, int b, int c, int d) {
116     int result = sum2(a, b) + sum2(c, d);
117     result += 0;
118     return result;
119 }
120 
121 int main(int argc, char** argv) {
122     sum3(3, 4, 5) + sum2(1, 2);
123     int sum = sum4(1, 2, 3, 4);
124     sum2(5, 6);
125     return 0;
126 }
127 
128 // g++ -c inlined-functions.cpp -o inlined-functions.o -g -Wno-unused-value
129 // obj2yaml inlined-functions.o > inlined-functions.yaml
130 
131 # Dump of source line per address:
132 # inlined-functions.cpp is src.cpp for space considerations.
133 0x20: src.cpp:17
134 0x21: src.cpp:17
135 0x26: src.cpp:17
136 0x27: src.cpp:17
137 0x29: src.cpp:17
138 0x2e: src.cpp:17
139 0x2f: src.cpp:17
140 0x31: src.cpp:17
141 0x36: src.cpp:18
142 0x37: src.cpp:18
143 0x39: src.cpp:18
144 0x3e: src.cpp:18
145 0x3f: src.cpp:18
146 0x41: src.cpp:18
147 0x46: src.cpp:18
148 0x47: src.cpp:18
149 0x49: src.cpp:18
150 0x4e: src.cpp:18
151 0x4f: src.cpp:18
152 0x51: src.cpp:18
153 0x56: src.cpp:18
154 0x57: src.cpp:18
155 0x59: src.cpp:18
156 0x5e: src.cpp:18 -> [email protected]:2
157 0x5f: src.cpp:18 -> [email protected]:2
158 0x61: src.cpp:18 -> [email protected]:2
159 0x66: src.cpp:18 -> [email protected]:2
160 0x67: src.cpp:18 -> [email protected]:2
161 0x69: src.cpp:18 -> [email protected]:2
162 0x6e: src.cpp:18 -> [email protected]:2
163 0x6f: src.cpp:18 -> [email protected]:2
164 0x71: src.cpp:18 -> [email protected]:2
165 0x76: src.cpp:18 -> [email protected]:2
166 0x77: src.cpp:18 -> [email protected]:2
167 0x79: src.cpp:18 -> [email protected]:2
168 0x7e: src.cpp:18 -> [email protected]:2
169 0x7f: src.cpp:19 -> [email protected]:12
170 0x81: src.cpp:19 -> [email protected]:12
171 0x86: src.cpp:19 -> [email protected]:12
172 0x87: src.cpp:19 -> [email protected]:12
173 0x89: src.cpp:19 -> [email protected]:12
174 0x8e: src.cpp:19 -> [email protected]:12 -> [email protected]:2
175 0x8f: src.cpp:19 -> [email protected]:12 -> [email protected]:2
176 0x91: src.cpp:19 -> [email protected]:12 -> [email protected]:2
177 0x96: src.cpp:19 -> [email protected]:12 -> [email protected]:3
178 0x97: src.cpp:19 -> [email protected]:12
179 0x99: src.cpp:19 -> [email protected]:12
180 0x9e: src.cpp:19 -> [email protected]:12
181 0x9f: src.cpp:19 -> [email protected]:12
182 0xa1: src.cpp:19 -> [email protected]:12
183 0xa6: src.cpp:19 -> [email protected]:12 -> [email protected]:2
184 0xa7: src.cpp:19 -> [email protected]:12 -> [email protected]:2
185 0xa9: src.cpp:19 -> [email protected]:12 -> [email protected]:2
186 0xae: src.cpp:19 -> [email protected]:12
187 0xaf: src.cpp:19 -> [email protected]:12
188 0xb1: src.cpp:19 -> [email protected]:12
189 0xb6: src.cpp:19 -> [email protected]:13
190 0xb7: src.cpp:19 -> [email protected]:13
191 0xb9: src.cpp:19 -> [email protected]:14
192 0xbe: src.cpp:19
193 0xbf: src.cpp:19
194 0xc1: src.cpp:19
195 0xc6: src.cpp:19
196 0xc7: src.cpp:19
197 0xc9: src.cpp:19
198 0xce: src.cpp:20 -> [email protected]:2
199 0xcf: src.cpp:20 -> [email protected]:2
200 0xd1: src.cpp:20 -> [email protected]:2
201 0xd6: src.cpp:21
202 0xd7: src.cpp:21
203 0xd9: src.cpp:21
204 0xde: src.cpp:21
205 */
206