Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:46

0001 //===- llvm/DebugInfo/Symbolize/DIPrinter.h ---------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file declares the DIPrinter class, which is responsible for printing
0010 // structures defined in DebugInfo/DIContext.h
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_DEBUGINFO_SYMBOLIZE_DIPRINTER_H
0015 #define LLVM_DEBUGINFO_SYMBOLIZE_DIPRINTER_H
0016 
0017 #include "llvm/ADT/StringRef.h"
0018 #include "llvm/Support/JSON.h"
0019 #include <memory>
0020 #include <vector>
0021 
0022 namespace llvm {
0023 struct DILineInfo;
0024 class DIInliningInfo;
0025 struct DIGlobal;
0026 struct DILocal;
0027 class ErrorInfoBase;
0028 class raw_ostream;
0029 
0030 namespace symbolize {
0031 
0032 class SourceCode;
0033 
0034 struct Request {
0035   StringRef ModuleName;
0036   std::optional<uint64_t> Address;
0037   StringRef Symbol;
0038 };
0039 
0040 class DIPrinter {
0041 public:
0042   DIPrinter() = default;
0043   virtual ~DIPrinter() = default;
0044 
0045   virtual void print(const Request &Request, const DILineInfo &Info) = 0;
0046   virtual void print(const Request &Request, const DIInliningInfo &Info) = 0;
0047   virtual void print(const Request &Request, const DIGlobal &Global) = 0;
0048   virtual void print(const Request &Request,
0049                      const std::vector<DILocal> &Locals) = 0;
0050   virtual void print(const Request &Request,
0051                      const std::vector<DILineInfo> &Locations) = 0;
0052 
0053   virtual bool printError(const Request &Request,
0054                           const ErrorInfoBase &ErrorInfo) = 0;
0055 
0056   virtual void listBegin() = 0;
0057   virtual void listEnd() = 0;
0058 };
0059 
0060 struct PrinterConfig {
0061   bool PrintAddress;
0062   bool PrintFunctions;
0063   bool Pretty;
0064   bool Verbose;
0065   int SourceContextLines;
0066 };
0067 
0068 using ErrorHandler = std::function<void(const ErrorInfoBase &, StringRef)>;
0069 
0070 class PlainPrinterBase : public DIPrinter {
0071 protected:
0072   raw_ostream &OS;
0073   ErrorHandler ErrHandler;
0074   PrinterConfig Config;
0075 
0076   void print(const DILineInfo &Info, bool Inlined);
0077   void printFunctionName(StringRef FunctionName, bool Inlined);
0078   virtual void printSimpleLocation(StringRef Filename,
0079                                    const DILineInfo &Info) = 0;
0080   void printContext(SourceCode SourceCode);
0081   void printVerbose(StringRef Filename, const DILineInfo &Info);
0082   virtual void printStartAddress(const DILineInfo &Info) {}
0083   virtual void printFooter() {}
0084 
0085 private:
0086   void printHeader(std::optional<uint64_t> Address);
0087 
0088 public:
0089   PlainPrinterBase(raw_ostream &OS, ErrorHandler EH, PrinterConfig &Config)
0090       : OS(OS), ErrHandler(EH), Config(Config) {}
0091 
0092   void print(const Request &Request, const DILineInfo &Info) override;
0093   void print(const Request &Request, const DIInliningInfo &Info) override;
0094   void print(const Request &Request, const DIGlobal &Global) override;
0095   void print(const Request &Request,
0096              const std::vector<DILocal> &Locals) override;
0097   void print(const Request &Request,
0098              const std::vector<DILineInfo> &Locations) override;
0099 
0100   bool printError(const Request &Request,
0101                   const ErrorInfoBase &ErrorInfo) override;
0102 
0103   void listBegin() override {}
0104   void listEnd() override {}
0105 };
0106 
0107 class LLVMPrinter : public PlainPrinterBase {
0108 private:
0109   void printSimpleLocation(StringRef Filename, const DILineInfo &Info) override;
0110   void printStartAddress(const DILineInfo &Info) override;
0111   void printFooter() override;
0112 
0113 public:
0114   LLVMPrinter(raw_ostream &OS, ErrorHandler EH, PrinterConfig &Config)
0115       : PlainPrinterBase(OS, EH, Config) {}
0116 };
0117 
0118 class GNUPrinter : public PlainPrinterBase {
0119 private:
0120   void printSimpleLocation(StringRef Filename, const DILineInfo &Info) override;
0121 
0122 public:
0123   GNUPrinter(raw_ostream &OS, ErrorHandler EH, PrinterConfig &Config)
0124       : PlainPrinterBase(OS, EH, Config) {}
0125 
0126 };
0127 
0128 class JSONPrinter : public DIPrinter {
0129 private:
0130   raw_ostream &OS;
0131   PrinterConfig Config;
0132   std::unique_ptr<json::Array> ObjectList;
0133 
0134   void printJSON(const json::Value &V) {
0135     json::OStream JOS(OS, Config.Pretty ? 2 : 0);
0136     JOS.value(V);
0137     OS << '\n';
0138   }
0139 
0140 public:
0141   JSONPrinter(raw_ostream &OS, PrinterConfig &Config)
0142       : OS(OS), Config(Config) {}
0143 
0144   void print(const Request &Request, const DILineInfo &Info) override;
0145   void print(const Request &Request, const DIInliningInfo &Info) override;
0146   void print(const Request &Request, const DIGlobal &Global) override;
0147   void print(const Request &Request,
0148              const std::vector<DILocal> &Locals) override;
0149   void print(const Request &Request,
0150              const std::vector<DILineInfo> &Locations) override;
0151 
0152   bool printError(const Request &Request,
0153                   const ErrorInfoBase &ErrorInfo) override;
0154 
0155   void listBegin() override;
0156   void listEnd() override;
0157 };
0158 } // namespace symbolize
0159 } // namespace llvm
0160 
0161 #endif