brintos

brintos / llvm-project-archived public Read only

0
0
Text · 7.6 KiB · 592504b Raw
231 lines · cpp
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-exception6//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 30using namespace lldb;31using namespace lldb_private;32using namespace lldb_private::plugin::dwarf;33 34class LineEntryTest : public testing::Test {35  SubsystemRAII<FileSystem, HostInfo, ObjectFileMachO, SymbolFileDWARF,36                TypeSystemClang>37      subsystem;38 39public:40  void SetUp() override;41 42protected:43  llvm::Expected<SymbolContextList>44  GetLineEntriesForLine(uint32_t line, std::optional<uint16_t> column);45  std::optional<TestFile> m_file;46  ModuleSP m_module_sp;47};48 49void LineEntryTest::SetUp() {50  auto ExpectedFile = TestFile::fromYamlFile("inlined-functions.yaml");51  ASSERT_THAT_EXPECTED(ExpectedFile, llvm::Succeeded());52  m_file.emplace(std::move(*ExpectedFile));53  m_module_sp = std::make_shared<Module>(m_file->moduleSpec());54}55 56  // TODO: Handle SourceLocationSpec column information57llvm::Expected<SymbolContextList> LineEntryTest::GetLineEntriesForLine(58    uint32_t line, std::optional<uint16_t> column = std::nullopt) {59  SymbolContextList sc_comp_units;60  SymbolContextList sc_line_entries;61  FileSpec file_spec("inlined-functions.cpp");62  m_module_sp->ResolveSymbolContextsForFileSpec(63      file_spec, line, /*check_inlines=*/true, lldb::eSymbolContextCompUnit,64      sc_comp_units);65  if (sc_comp_units.GetSize() == 0)66    return llvm::createStringError(llvm::inconvertibleErrorCode(),67                                   "No comp unit found on the test object.");68 69  SourceLocationSpec location_spec(file_spec, line, column,70                                   /*check_inlines=*/true,71                                   /*exact_match=*/true);72 73  sc_comp_units[0].comp_unit->ResolveSymbolContext(74      location_spec, eSymbolContextLineEntry, sc_line_entries);75  if (sc_line_entries.GetSize() == 0)76    return llvm::createStringError(llvm::inconvertibleErrorCode(),77                                   "No line entry found on the test object.");78  return sc_line_entries;79}80 81// This tests if we can get all line entries that match the passed line, if82// no column is specified.83TEST_F(LineEntryTest, GetAllExactLineMatchesWithoutColumn) {84  auto sc_line_entries = GetLineEntriesForLine(12);85  ASSERT_THAT_EXPECTED(sc_line_entries, llvm::Succeeded());86  ASSERT_EQ(sc_line_entries->NumLineEntriesWithLine(12), 6u);87}88 89// This tests if we can get exact line and column matches.90TEST_F(LineEntryTest, GetAllExactLineColumnMatches) {91  auto sc_line_entries = GetLineEntriesForLine(12, 39);92  ASSERT_THAT_EXPECTED(sc_line_entries, llvm::Succeeded());93  ASSERT_EQ(sc_line_entries->NumLineEntriesWithLine(12), 1u);94  auto line_entry = sc_line_entries.get()[0].line_entry;95  ASSERT_EQ(line_entry.column, 39);96}97 98TEST_F(LineEntryTest, GetSameLineContiguousAddressRangeNoInlines) {99  auto sc_line_entries = GetLineEntriesForLine(18);100  ASSERT_THAT_EXPECTED(sc_line_entries, llvm::Succeeded());101  auto line_entry = sc_line_entries.get()[0].line_entry;102  bool include_inlined_functions = false;103  auto range =104      line_entry.GetSameLineContiguousAddressRange(include_inlined_functions);105  ASSERT_EQ(range.GetByteSize(), (uint64_t)0x24);106}107 108TEST_F(LineEntryTest, GetSameLineContiguousAddressRangeOneInline) {109  auto sc_line_entries = GetLineEntriesForLine(18);110  ASSERT_THAT_EXPECTED(sc_line_entries, llvm::Succeeded());111  auto line_entry = sc_line_entries.get()[0].line_entry;112  bool include_inlined_functions = true;113  auto range =114      line_entry.GetSameLineContiguousAddressRange(include_inlined_functions);115  ASSERT_EQ(range.GetByteSize(), (uint64_t)0x49);116}117 118TEST_F(LineEntryTest, GetSameLineContiguousAddressRangeNestedInline) {119  auto sc_line_entries = GetLineEntriesForLine(12);120  ASSERT_THAT_EXPECTED(sc_line_entries, llvm::Succeeded());121  auto line_entry = sc_line_entries.get()[0].line_entry;122  bool include_inlined_functions = true;123  auto range =124      line_entry.GetSameLineContiguousAddressRange(include_inlined_functions);125  ASSERT_EQ(range.GetByteSize(), (uint64_t)0x33);126}127 128/*129# inlined-functions.cpp130inline __attribute__((always_inline)) int sum2(int a, int b) {131    int result = a + b;132    return result;133}134 135int sum3(int a, int b, int c) {136    int result = a + b + c;137    return result;138}139 140inline __attribute__((always_inline)) int sum4(int a, int b, int c, int d) {141    int result = sum2(a, b) + sum2(c, d);142    result += 0;143    return result;144}145 146int main(int argc, char** argv) {147    sum3(3, 4, 5) + sum2(1, 2);148    int sum = sum4(1, 2, 3, 4);149    sum2(5, 6);150    return 0;151}152 153// g++ -c inlined-functions.cpp -o inlined-functions.o -g -Wno-unused-value154// obj2yaml inlined-functions.o > inlined-functions.yaml155 156# Dump of source line per address:157# inlined-functions.cpp is src.cpp for space considerations.1580x20: src.cpp:171590x21: src.cpp:171600x26: src.cpp:171610x27: src.cpp:171620x29: src.cpp:171630x2e: src.cpp:171640x2f: src.cpp:171650x31: src.cpp:171660x36: src.cpp:181670x37: src.cpp:181680x39: src.cpp:181690x3e: src.cpp:181700x3f: src.cpp:181710x41: src.cpp:181720x46: src.cpp:181730x47: src.cpp:181740x49: src.cpp:181750x4e: src.cpp:181760x4f: src.cpp:181770x51: src.cpp:181780x56: src.cpp:181790x57: src.cpp:181800x59: src.cpp:181810x5e: src.cpp:18 -> sum2@src.cpp:21820x5f: src.cpp:18 -> sum2@src.cpp:21830x61: src.cpp:18 -> sum2@src.cpp:21840x66: src.cpp:18 -> sum2@src.cpp:21850x67: src.cpp:18 -> sum2@src.cpp:21860x69: src.cpp:18 -> sum2@src.cpp:21870x6e: src.cpp:18 -> sum2@src.cpp:21880x6f: src.cpp:18 -> sum2@src.cpp:21890x71: src.cpp:18 -> sum2@src.cpp:21900x76: src.cpp:18 -> sum2@src.cpp:21910x77: src.cpp:18 -> sum2@src.cpp:21920x79: src.cpp:18 -> sum2@src.cpp:21930x7e: src.cpp:18 -> sum2@src.cpp:21940x7f: src.cpp:19 -> sum4@src.cpp:121950x81: src.cpp:19 -> sum4@src.cpp:121960x86: src.cpp:19 -> sum4@src.cpp:121970x87: src.cpp:19 -> sum4@src.cpp:121980x89: src.cpp:19 -> sum4@src.cpp:121990x8e: src.cpp:19 -> sum4@src.cpp:12 -> sum2@src.cpp:22000x8f: src.cpp:19 -> sum4@src.cpp:12 -> sum2@src.cpp:22010x91: src.cpp:19 -> sum4@src.cpp:12 -> sum2@src.cpp:22020x96: src.cpp:19 -> sum4@src.cpp:12 -> sum2@src.cpp:32030x97: src.cpp:19 -> sum4@src.cpp:122040x99: src.cpp:19 -> sum4@src.cpp:122050x9e: src.cpp:19 -> sum4@src.cpp:122060x9f: src.cpp:19 -> sum4@src.cpp:122070xa1: src.cpp:19 -> sum4@src.cpp:122080xa6: src.cpp:19 -> sum4@src.cpp:12 -> sum2@src.cpp:22090xa7: src.cpp:19 -> sum4@src.cpp:12 -> sum2@src.cpp:22100xa9: src.cpp:19 -> sum4@src.cpp:12 -> sum2@src.cpp:22110xae: src.cpp:19 -> sum4@src.cpp:122120xaf: src.cpp:19 -> sum4@src.cpp:122130xb1: src.cpp:19 -> sum4@src.cpp:122140xb6: src.cpp:19 -> sum4@src.cpp:132150xb7: src.cpp:19 -> sum4@src.cpp:132160xb9: src.cpp:19 -> sum4@src.cpp:142170xbe: src.cpp:192180xbf: src.cpp:192190xc1: src.cpp:192200xc6: src.cpp:192210xc7: src.cpp:192220xc9: src.cpp:192230xce: src.cpp:20 -> sum2@src.cpp:22240xcf: src.cpp:20 -> sum2@src.cpp:22250xd1: src.cpp:20 -> sum2@src.cpp:22260xd6: src.cpp:212270xd7: src.cpp:212280xd9: src.cpp:212290xde: src.cpp:21230*/231