1aeda128aSGreg Clayton //===- LookupResult.cpp -------------------------------------------------*-===//
2aeda128aSGreg Clayton //
3aeda128aSGreg Clayton // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4aeda128aSGreg Clayton // See https://llvm.org/LICENSE.txt for license information.
5aeda128aSGreg Clayton // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6aeda128aSGreg Clayton //
7aeda128aSGreg Clayton //===----------------------------------------------------------------------===//
8aeda128aSGreg Clayton 
9aeda128aSGreg Clayton #include "llvm/DebugInfo/GSYM/LookupResult.h"
10aeda128aSGreg Clayton #include "llvm/ADT/SmallString.h"
1170306542Sserge-sans-paille #include "llvm/DebugInfo/GSYM/ExtractRanges.h"
12aeda128aSGreg Clayton #include "llvm/Support/Format.h"
13aeda128aSGreg Clayton #include "llvm/Support/Path.h"
14aeda128aSGreg Clayton #include "llvm/Support/raw_ostream.h"
15da650094SDouglas Yung #include <ciso646>
16aeda128aSGreg Clayton 
17aeda128aSGreg Clayton using namespace llvm;
18aeda128aSGreg Clayton using namespace gsym;
19aeda128aSGreg Clayton 
getSourceFile(uint32_t Index) const20aeda128aSGreg Clayton std::string LookupResult::getSourceFile(uint32_t Index) const {
21aeda128aSGreg Clayton   std::string Fullpath;
22aeda128aSGreg Clayton   if (Index < Locations.size()) {
23aeda128aSGreg Clayton     if (!Locations[Index].Dir.empty()) {
24aeda128aSGreg Clayton       if (Locations[Index].Base.empty()) {
25adcd0268SBenjamin Kramer         Fullpath = std::string(Locations[Index].Dir);
26aeda128aSGreg Clayton       } else {
27aeda128aSGreg Clayton         llvm::SmallString<64> Storage;
28aeda128aSGreg Clayton         llvm::sys::path::append(Storage, Locations[Index].Dir,
29aeda128aSGreg Clayton                                 Locations[Index].Base);
30aeda128aSGreg Clayton         Fullpath.assign(Storage.begin(), Storage.end());
31aeda128aSGreg Clayton       }
32aeda128aSGreg Clayton     } else if (!Locations[Index].Base.empty())
33adcd0268SBenjamin Kramer       Fullpath = std::string(Locations[Index].Base);
34aeda128aSGreg Clayton   }
35aeda128aSGreg Clayton   return Fullpath;
36aeda128aSGreg Clayton }
37aeda128aSGreg Clayton 
operator <<(raw_ostream & OS,const SourceLocation & SL)38aeda128aSGreg Clayton raw_ostream &llvm::gsym::operator<<(raw_ostream &OS, const SourceLocation &SL) {
392f6cc21fSGreg Clayton   OS << SL.Name;
402f6cc21fSGreg Clayton   if (SL.Offset > 0)
412f6cc21fSGreg Clayton     OS << " + " << SL.Offset;
422f6cc21fSGreg Clayton   if (SL.Dir.size() || SL.Base.size()) {
432f6cc21fSGreg Clayton     OS << " @ ";
44aeda128aSGreg Clayton     if (!SL.Dir.empty()) {
45aeda128aSGreg Clayton       OS << SL.Dir;
46*b21c0385SAlexander Pivovarov       if (SL.Dir.contains('\\') && !SL.Dir.contains('/'))
47aeda128aSGreg Clayton         OS << '\\';
48aeda128aSGreg Clayton       else
49aeda128aSGreg Clayton         OS << '/';
50aeda128aSGreg Clayton     }
51aeda128aSGreg Clayton     if (SL.Base.empty())
52aeda128aSGreg Clayton       OS << "<invalid-file>";
53aeda128aSGreg Clayton     else
54aeda128aSGreg Clayton       OS << SL.Base;
55aeda128aSGreg Clayton     OS << ':' << SL.Line;
562f6cc21fSGreg Clayton   }
57aeda128aSGreg Clayton   return OS;
58aeda128aSGreg Clayton }
59aeda128aSGreg Clayton 
operator <<(raw_ostream & OS,const LookupResult & LR)60aeda128aSGreg Clayton raw_ostream &llvm::gsym::operator<<(raw_ostream &OS, const LookupResult &LR) {
61aeda128aSGreg Clayton   OS << HEX64(LR.LookupAddr) << ": ";
62aeda128aSGreg Clayton   auto NumLocations = LR.Locations.size();
63aeda128aSGreg Clayton   for (size_t I = 0; I < NumLocations; ++I) {
64aeda128aSGreg Clayton     if (I > 0) {
65aeda128aSGreg Clayton       OS << '\n';
66aeda128aSGreg Clayton       OS.indent(20);
67aeda128aSGreg Clayton     }
68aeda128aSGreg Clayton     const bool IsInlined = I + 1 != NumLocations;
69aeda128aSGreg Clayton     OS << LR.Locations[I];
70aeda128aSGreg Clayton     if (IsInlined)
71aeda128aSGreg Clayton       OS << " [inlined]";
72aeda128aSGreg Clayton   }
73aeda128aSGreg Clayton   OS << '\n';
74aeda128aSGreg Clayton   return OS;
75aeda128aSGreg Clayton }
76