DIPrinter.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/DebugInfo/Symbolize/DIPrinter.h ---------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares the DIPrinter class, which is responsible for printing
  15. // structures defined in DebugInfo/DIContext.h
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_DEBUGINFO_SYMBOLIZE_DIPRINTER_H
  19. #define LLVM_DEBUGINFO_SYMBOLIZE_DIPRINTER_H
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/Support/JSON.h"
  22. #include <memory>
  23. #include <vector>
  24. namespace llvm {
  25. struct DILineInfo;
  26. class DIInliningInfo;
  27. struct DIGlobal;
  28. struct DILocal;
  29. class ErrorInfoBase;
  30. class raw_ostream;
  31. namespace symbolize {
  32. class SourceCode;
  33. struct Request {
  34. StringRef ModuleName;
  35. std::optional<uint64_t> Address;
  36. };
  37. class DIPrinter {
  38. public:
  39. DIPrinter() = default;
  40. virtual ~DIPrinter() = default;
  41. virtual void print(const Request &Request, const DILineInfo &Info) = 0;
  42. virtual void print(const Request &Request, const DIInliningInfo &Info) = 0;
  43. virtual void print(const Request &Request, const DIGlobal &Global) = 0;
  44. virtual void print(const Request &Request,
  45. const std::vector<DILocal> &Locals) = 0;
  46. virtual void printInvalidCommand(const Request &Request,
  47. StringRef Command) = 0;
  48. virtual bool printError(const Request &Request,
  49. const ErrorInfoBase &ErrorInfo,
  50. StringRef ErrorBanner) = 0;
  51. virtual void listBegin() = 0;
  52. virtual void listEnd() = 0;
  53. };
  54. struct PrinterConfig {
  55. bool PrintAddress;
  56. bool PrintFunctions;
  57. bool Pretty;
  58. bool Verbose;
  59. int SourceContextLines;
  60. };
  61. class PlainPrinterBase : public DIPrinter {
  62. protected:
  63. raw_ostream &OS;
  64. raw_ostream &ES;
  65. PrinterConfig Config;
  66. void print(const DILineInfo &Info, bool Inlined);
  67. void printFunctionName(StringRef FunctionName, bool Inlined);
  68. virtual void printSimpleLocation(StringRef Filename,
  69. const DILineInfo &Info) = 0;
  70. void printContext(SourceCode SourceCode);
  71. void printVerbose(StringRef Filename, const DILineInfo &Info);
  72. virtual void printStartAddress(const DILineInfo &Info) {}
  73. virtual void printFooter() {}
  74. private:
  75. void printHeader(uint64_t Address);
  76. public:
  77. PlainPrinterBase(raw_ostream &OS, raw_ostream &ES, PrinterConfig &Config)
  78. : OS(OS), ES(ES), Config(Config) {}
  79. void print(const Request &Request, const DILineInfo &Info) override;
  80. void print(const Request &Request, const DIInliningInfo &Info) override;
  81. void print(const Request &Request, const DIGlobal &Global) override;
  82. void print(const Request &Request,
  83. const std::vector<DILocal> &Locals) override;
  84. void printInvalidCommand(const Request &Request, StringRef Command) override;
  85. bool printError(const Request &Request, const ErrorInfoBase &ErrorInfo,
  86. StringRef ErrorBanner) override;
  87. void listBegin() override {}
  88. void listEnd() override {}
  89. };
  90. class LLVMPrinter : public PlainPrinterBase {
  91. private:
  92. void printSimpleLocation(StringRef Filename, const DILineInfo &Info) override;
  93. void printStartAddress(const DILineInfo &Info) override;
  94. void printFooter() override;
  95. public:
  96. LLVMPrinter(raw_ostream &OS, raw_ostream &ES, PrinterConfig &Config)
  97. : PlainPrinterBase(OS, ES, Config) {}
  98. };
  99. class GNUPrinter : public PlainPrinterBase {
  100. private:
  101. void printSimpleLocation(StringRef Filename, const DILineInfo &Info) override;
  102. public:
  103. GNUPrinter(raw_ostream &OS, raw_ostream &ES, PrinterConfig &Config)
  104. : PlainPrinterBase(OS, ES, Config) {}
  105. };
  106. class JSONPrinter : public DIPrinter {
  107. private:
  108. raw_ostream &OS;
  109. PrinterConfig Config;
  110. std::unique_ptr<json::Array> ObjectList;
  111. void printJSON(const json::Value &V) {
  112. json::OStream JOS(OS, Config.Pretty ? 2 : 0);
  113. JOS.value(V);
  114. OS << '\n';
  115. }
  116. public:
  117. JSONPrinter(raw_ostream &OS, PrinterConfig &Config)
  118. : OS(OS), Config(Config) {}
  119. void print(const Request &Request, const DILineInfo &Info) override;
  120. void print(const Request &Request, const DIInliningInfo &Info) override;
  121. void print(const Request &Request, const DIGlobal &Global) override;
  122. void print(const Request &Request,
  123. const std::vector<DILocal> &Locals) override;
  124. void printInvalidCommand(const Request &Request, StringRef Command) override;
  125. bool printError(const Request &Request, const ErrorInfoBase &ErrorInfo,
  126. StringRef ErrorBanner) override;
  127. void listBegin() override;
  128. void listEnd() override;
  129. };
  130. } // namespace symbolize
  131. } // namespace llvm
  132. #endif
  133. #ifdef __GNUC__
  134. #pragma GCC diagnostic pop
  135. #endif