xref: /llvm-project/llvm/lib/DebugInfo/GSYM/LookupResult.cpp (revision 4a1c33d34c31893aa781ac43285ae2100a540fd4)
1 //===- LookupResult.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 "llvm/DebugInfo/GSYM/LookupResult.h"
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/DebugInfo/GSYM/ExtractRanges.h"
12 #include "llvm/Support/Path.h"
13 #include "llvm/Support/raw_ostream.h"
14 
15 using namespace llvm;
16 using namespace gsym;
17 
18 std::string LookupResult::getSourceFile(uint32_t Index) const {
19   std::string Fullpath;
20   if (Index < Locations.size()) {
21     if (!Locations[Index].Dir.empty()) {
22       if (Locations[Index].Base.empty()) {
23         Fullpath = std::string(Locations[Index].Dir);
24       } else {
25         llvm::SmallString<64> Storage;
26         llvm::sys::path::append(Storage, Locations[Index].Dir,
27                                 Locations[Index].Base);
28         Fullpath.assign(Storage.begin(), Storage.end());
29       }
30     } else if (!Locations[Index].Base.empty())
31       Fullpath = std::string(Locations[Index].Base);
32   }
33   return Fullpath;
34 }
35 
36 raw_ostream &llvm::gsym::operator<<(raw_ostream &OS, const SourceLocation &SL) {
37   OS << SL.Name;
38   if (SL.Offset > 0)
39     OS << " + " << SL.Offset;
40   if (SL.Dir.size() || SL.Base.size()) {
41     OS << " @ ";
42     if (!SL.Dir.empty()) {
43       OS << SL.Dir;
44       if (SL.Dir.contains('\\') && !SL.Dir.contains('/'))
45         OS << '\\';
46       else
47         OS << '/';
48     }
49     if (SL.Base.empty())
50       OS << "<invalid-file>";
51     else
52       OS << SL.Base;
53     OS << ':' << SL.Line;
54   }
55   return OS;
56 }
57 
58 raw_ostream &llvm::gsym::operator<<(raw_ostream &OS, const LookupResult &LR) {
59   OS << HEX64(LR.LookupAddr) << ": ";
60   auto NumLocations = LR.Locations.size();
61   for (size_t I = 0; I < NumLocations; ++I) {
62     if (I > 0) {
63       OS << '\n';
64       OS.indent(20);
65     }
66     const bool IsInlined = I + 1 != NumLocations;
67     OS << LR.Locations[I];
68     if (IsInlined)
69       OS << " [inlined]";
70   }
71 
72   if (!LR.CallSiteFuncRegex.empty()) {
73     OS << "\n      CallSites: ";
74     for (size_t i = 0; i < LR.CallSiteFuncRegex.size(); ++i) {
75       if (i > 0)
76         OS << ", ";
77       OS << LR.CallSiteFuncRegex[i];
78     }
79   }
80 
81   OS << '\n';
82   return OS;
83 }
84