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