LLVM  17.0.0git
DIPrinter.h
Go to the documentation of this file.
1 //===- llvm/DebugInfo/Symbolize/DIPrinter.h ---------------------*- C++ -*-===//
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 // This file declares the DIPrinter class, which is responsible for printing
10 // structures defined in DebugInfo/DIContext.h
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_DEBUGINFO_SYMBOLIZE_DIPRINTER_H
15 #define LLVM_DEBUGINFO_SYMBOLIZE_DIPRINTER_H
16 
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/JSON.h"
19 #include <memory>
20 #include <vector>
21 
22 namespace llvm {
23 struct DILineInfo;
24 class DIInliningInfo;
25 struct DIGlobal;
26 struct DILocal;
27 class ErrorInfoBase;
28 class raw_ostream;
29 
30 namespace symbolize {
31 
32 class SourceCode;
33 
34 struct Request {
36  std::optional<uint64_t> Address;
37 };
38 
39 class DIPrinter {
40 public:
41  DIPrinter() = default;
42  virtual ~DIPrinter() = default;
43 
44  virtual void print(const Request &Request, const DILineInfo &Info) = 0;
45  virtual void print(const Request &Request, const DIInliningInfo &Info) = 0;
46  virtual void print(const Request &Request, const DIGlobal &Global) = 0;
47  virtual void print(const Request &Request,
48  const std::vector<DILocal> &Locals) = 0;
49 
50  virtual void printInvalidCommand(const Request &Request,
51  StringRef Command) = 0;
52 
53  virtual bool printError(const Request &Request,
54  const ErrorInfoBase &ErrorInfo,
55  StringRef ErrorBanner) = 0;
56 
57  virtual void listBegin() = 0;
58  virtual void listEnd() = 0;
59 };
60 
61 struct PrinterConfig {
64  bool Pretty;
65  bool Verbose;
67 };
68 
69 class PlainPrinterBase : public DIPrinter {
70 protected:
74 
75  void print(const DILineInfo &Info, bool Inlined);
76  void printFunctionName(StringRef FunctionName, bool Inlined);
77  virtual void printSimpleLocation(StringRef Filename,
78  const DILineInfo &Info) = 0;
80  void printVerbose(StringRef Filename, const DILineInfo &Info);
81  virtual void printStartAddress(const DILineInfo &Info) {}
82  virtual void printFooter() {}
83 
84 private:
85  void printHeader(uint64_t Address);
86 
87 public:
89  : OS(OS), ES(ES), Config(Config) {}
90 
91  void print(const Request &Request, const DILineInfo &Info) override;
92  void print(const Request &Request, const DIInliningInfo &Info) override;
93  void print(const Request &Request, const DIGlobal &Global) override;
94  void print(const Request &Request,
95  const std::vector<DILocal> &Locals) override;
96 
97  void printInvalidCommand(const Request &Request, StringRef Command) override;
98 
99  bool printError(const Request &Request, const ErrorInfoBase &ErrorInfo,
100  StringRef ErrorBanner) override;
101 
102  void listBegin() override {}
103  void listEnd() override {}
104 };
105 
107 private:
108  void printSimpleLocation(StringRef Filename, const DILineInfo &Info) override;
109  void printStartAddress(const DILineInfo &Info) override;
110  void printFooter() override;
111 
112 public:
114  : PlainPrinterBase(OS, ES, Config) {}
115 };
116 
117 class GNUPrinter : public PlainPrinterBase {
118 private:
119  void printSimpleLocation(StringRef Filename, const DILineInfo &Info) override;
120 
121 public:
123  : PlainPrinterBase(OS, ES, Config) {}
124 };
125 
126 class JSONPrinter : public DIPrinter {
127 private:
128  raw_ostream &OS;
129  PrinterConfig Config;
130  std::unique_ptr<json::Array> ObjectList;
131 
132  void printJSON(const json::Value &V) {
133  json::OStream JOS(OS, Config.Pretty ? 2 : 0);
134  JOS.value(V);
135  OS << '\n';
136  }
137 
138 public:
140  : OS(OS), Config(Config) {}
141 
142  void print(const Request &Request, const DILineInfo &Info) override;
143  void print(const Request &Request, const DIInliningInfo &Info) override;
144  void print(const Request &Request, const DIGlobal &Global) override;
145  void print(const Request &Request,
146  const std::vector<DILocal> &Locals) override;
147 
148  void printInvalidCommand(const Request &Request, StringRef Command) override;
149 
150  bool printError(const Request &Request, const ErrorInfoBase &ErrorInfo,
151  StringRef ErrorBanner) override;
152 
153  void listBegin() override;
154  void listEnd() override;
155 };
156 } // namespace symbolize
157 } // namespace llvm
158 
159 #endif
llvm::symbolize::DIPrinter::printError
virtual bool printError(const Request &Request, const ErrorInfoBase &ErrorInfo, StringRef ErrorBanner)=0
llvm::json::Value
A Value is an JSON value of unknown type.
Definition: JSON.h:282
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::symbolize::PlainPrinterBase::printInvalidCommand
void printInvalidCommand(const Request &Request, StringRef Command) override
Definition: DIPrinter.cpp:263
llvm::symbolize::Request::Address
std::optional< uint64_t > Address
Definition: DIPrinter.h:36
llvm::symbolize::PlainPrinterBase::printContext
void printContext(SourceCode SourceCode)
Definition: DIPrinter.cpp:118
llvm::DIInliningInfo
A format-neutral container for inlined code description.
Definition: DIContext.h:88
llvm::symbolize::PlainPrinterBase
Definition: DIPrinter.h:69
StringRef.h
llvm::symbolize::DIPrinter::DIPrinter
DIPrinter()=default
llvm::symbolize::JSONPrinter::listBegin
void listBegin() override
Definition: DIPrinter.cpp:388
llvm::symbolize::PlainPrinterBase::Config
PrinterConfig Config
Definition: DIPrinter.h:73
llvm::symbolize::DIPrinter
Definition: DIPrinter.h:39
llvm::symbolize::PlainPrinterBase::listEnd
void listEnd() override
Definition: DIPrinter.h:103
llvm::symbolize::PlainPrinterBase::OS
raw_ostream & OS
Definition: DIPrinter.h:71
llvm::symbolize::PlainPrinterBase::print
void print(const DILineInfo &Info, bool Inlined)
Definition: DIPrinter.cpp:173
llvm::symbolize::PlainPrinterBase::printFooter
virtual void printFooter()
Definition: DIPrinter.h:82
llvm::symbolize::JSONPrinter::print
void print(const Request &Request, const DILineInfo &Info) override
Definition: DIPrinter.cpp:291
llvm::symbolize::DIPrinter::listBegin
virtual void listBegin()=0
llvm::symbolize::PrinterConfig::PrintAddress
bool PrintAddress
Definition: DIPrinter.h:62
llvm::symbolize::PlainPrinterBase::printStartAddress
virtual void printStartAddress(const DILineInfo &Info)
Definition: DIPrinter.h:81
llvm::symbolize::JSONPrinter::JSONPrinter
JSONPrinter(raw_ostream &OS, PrinterConfig &Config)
Definition: DIPrinter.h:139
llvm::symbolize::DIPrinter::listEnd
virtual void listEnd()=0
llvm::symbolize::PrinterConfig
Definition: DIPrinter.h:61
llvm::symbolize::JSONPrinter::printInvalidCommand
void printInvalidCommand(const Request &Request, StringRef Command) override
Definition: DIPrinter.cpp:369
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
JSON.h
llvm::symbolize::PrinterConfig::PrintFunctions
bool PrintFunctions
Definition: DIPrinter.h:63
llvm::symbolize::Request::ModuleName
StringRef ModuleName
Definition: DIPrinter.h:35
llvm::ErrorInfoBase
Base class for error info classes.
Definition: Error.h:47
llvm::symbolize::PrinterConfig::SourceContextLines
int SourceContextLines
Definition: DIPrinter.h:66
llvm::symbolize::PlainPrinterBase::listBegin
void listBegin() override
Definition: DIPrinter.h:102
llvm::json::OStream
json::OStream allows writing well-formed JSON without materializing all structures as json::Value ahe...
Definition: JSON.h:956
uint64_t
llvm::symbolize::PrinterConfig::Verbose
bool Verbose
Definition: DIPrinter.h:65
llvm::DILineInfo
A format-neutral container for source line information.
Definition: DIContext.h:32
llvm::symbolize::DIPrinter::print
virtual void print(const Request &Request, const DILineInfo &Info)=0
llvm::symbolize::GNUPrinter::GNUPrinter
GNUPrinter(raw_ostream &OS, raw_ostream &ES, PrinterConfig &Config)
Definition: DIPrinter.h:122
llvm::symbolize::PlainPrinterBase::PlainPrinterBase
PlainPrinterBase(raw_ostream &OS, raw_ostream &ES, PrinterConfig &Config)
Definition: DIPrinter.h:88
llvm::symbolize::JSONPrinter
Definition: DIPrinter.h:126
llvm::ErrorInfo
Base class for user error types.
Definition: Error.h:348
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::symbolize::PrinterConfig::Pretty
bool Pretty
Definition: DIPrinter.h:64
llvm::symbolize::LLVMPrinter::LLVMPrinter
LLVMPrinter(raw_ostream &OS, raw_ostream &ES, PrinterConfig &Config)
Definition: DIPrinter.h:113
llvm::DIGlobal
Container for description of a global variable.
Definition: DIContext.h:114
llvm::symbolize::PlainPrinterBase::printVerbose
void printVerbose(StringRef Filename, const DILineInfo &Info)
Definition: DIPrinter.cpp:149
llvm::symbolize::Request
Definition: DIPrinter.h:34
llvm::symbolize::GNUPrinter
Definition: DIPrinter.h:117
llvm::json::OStream::value
void value(const Value &V)
Emit a self-contained value (number, string, vector<string> etc).
Definition: JSON.cpp:756
llvm::symbolize::PlainPrinterBase::printSimpleLocation
virtual void printSimpleLocation(StringRef Filename, const DILineInfo &Info)=0
llvm::symbolize::JSONPrinter::listEnd
void listEnd() override
Definition: DIPrinter.cpp:393
llvm::symbolize::SourceCode
Definition: DIPrinter.cpp:31
llvm::symbolize::PlainPrinterBase::ES
raw_ostream & ES
Definition: DIPrinter.h:72
llvm::symbolize::LLVMPrinter
Definition: DIPrinter.h:106
llvm::symbolize::PlainPrinterBase::printError
bool printError(const Request &Request, const ErrorInfoBase &ErrorInfo, StringRef ErrorBanner) override
Definition: DIPrinter.cpp:268
llvm::symbolize::DIPrinter::~DIPrinter
virtual ~DIPrinter()=default
llvm::symbolize::DIPrinter::printInvalidCommand
virtual void printInvalidCommand(const Request &Request, StringRef Command)=0
llvm::symbolize::JSONPrinter::printError
bool printError(const Request &Request, const ErrorInfoBase &ErrorInfo, StringRef ErrorBanner) override
Definition: DIPrinter.cpp:377
llvm::symbolize::PlainPrinterBase::printFunctionName
void printFunctionName(StringRef FunctionName, bool Inlined)
Definition: DIPrinter.cpp:122