Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:50

0001 //===-- CommandReturnObject.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 #ifndef LLDB_INTERPRETER_COMMANDRETURNOBJECT_H
0010 #define LLDB_INTERPRETER_COMMANDRETURNOBJECT_H
0011 
0012 #include "lldb/Host/StreamFile.h"
0013 #include "lldb/Utility/DiagnosticsRendering.h"
0014 #include "lldb/Utility/StreamString.h"
0015 #include "lldb/Utility/StreamTee.h"
0016 #include "lldb/Utility/StructuredData.h"
0017 #include "lldb/lldb-private.h"
0018 
0019 #include "llvm/ADT/StringRef.h"
0020 #include "llvm/Support/Error.h"
0021 #include "llvm/Support/FormatVariadic.h"
0022 #include "llvm/Support/WithColor.h"
0023 
0024 #include <memory>
0025 
0026 namespace lldb_private {
0027 
0028 class CommandReturnObject {
0029 public:
0030   CommandReturnObject(bool colors);
0031 
0032   ~CommandReturnObject() = default;
0033 
0034   /// Format any inline diagnostics with an indentation of \c indent.
0035   std::string GetInlineDiagnosticString(unsigned indent);
0036 
0037   llvm::StringRef GetOutputString() {
0038     lldb::StreamSP stream_sp(m_out_stream.GetStreamAtIndex(eStreamStringIndex));
0039     if (stream_sp)
0040       return std::static_pointer_cast<StreamString>(stream_sp)->GetString();
0041     return llvm::StringRef();
0042   }
0043 
0044   /// Return the errors as a string.
0045   ///
0046   /// If \c with_diagnostics is true, all diagnostics are also
0047   /// rendered into the string. Otherwise the expectation is that they
0048   /// are fetched with \ref GetInlineDiagnosticString().
0049   std::string GetErrorString(bool with_diagnostics = true);
0050   StructuredData::ObjectSP GetErrorData();
0051 
0052   Stream &GetOutputStream() {
0053     // Make sure we at least have our normal string stream output stream
0054     lldb::StreamSP stream_sp(m_out_stream.GetStreamAtIndex(eStreamStringIndex));
0055     if (!stream_sp) {
0056       stream_sp = std::make_shared<StreamString>();
0057       m_out_stream.SetStreamAtIndex(eStreamStringIndex, stream_sp);
0058     }
0059     return m_out_stream;
0060   }
0061 
0062   Stream &GetErrorStream() {
0063     // Make sure we at least have our normal string stream output stream
0064     lldb::StreamSP stream_sp(m_err_stream.GetStreamAtIndex(eStreamStringIndex));
0065     if (!stream_sp) {
0066       stream_sp = std::make_shared<StreamString>();
0067       m_err_stream.SetStreamAtIndex(eStreamStringIndex, stream_sp);
0068     }
0069     return m_err_stream;
0070   }
0071 
0072   void SetImmediateOutputFile(lldb::FileSP file_sp) {
0073     if (m_suppress_immediate_output)
0074       return;
0075     lldb::StreamSP stream_sp(new StreamFile(file_sp));
0076     m_out_stream.SetStreamAtIndex(eImmediateStreamIndex, stream_sp);
0077   }
0078 
0079   void SetImmediateErrorFile(lldb::FileSP file_sp) {
0080     if (m_suppress_immediate_output)
0081       return;
0082     lldb::StreamSP stream_sp(new StreamFile(file_sp));
0083     m_err_stream.SetStreamAtIndex(eImmediateStreamIndex, stream_sp);
0084   }
0085 
0086   void SetImmediateOutputStream(const lldb::StreamSP &stream_sp) {
0087     if (m_suppress_immediate_output)
0088       return;
0089     m_out_stream.SetStreamAtIndex(eImmediateStreamIndex, stream_sp);
0090   }
0091 
0092   void SetImmediateErrorStream(const lldb::StreamSP &stream_sp) {
0093     if (m_suppress_immediate_output)
0094       return;
0095     m_err_stream.SetStreamAtIndex(eImmediateStreamIndex, stream_sp);
0096   }
0097 
0098   lldb::StreamSP GetImmediateOutputStream() {
0099     return m_out_stream.GetStreamAtIndex(eImmediateStreamIndex);
0100   }
0101 
0102   lldb::StreamSP GetImmediateErrorStream() {
0103     return m_err_stream.GetStreamAtIndex(eImmediateStreamIndex);
0104   }
0105 
0106   void Clear();
0107 
0108   void AppendMessage(llvm::StringRef in_string);
0109 
0110   void AppendMessageWithFormat(const char *format, ...)
0111       __attribute__((format(printf, 2, 3)));
0112 
0113   void AppendNote(llvm::StringRef in_string);
0114 
0115   void AppendNoteWithFormat(const char *format, ...)
0116       __attribute__((format(printf, 2, 3)));
0117 
0118   void AppendWarning(llvm::StringRef in_string);
0119 
0120   void AppendWarningWithFormat(const char *format, ...)
0121       __attribute__((format(printf, 2, 3)));
0122 
0123   void AppendError(llvm::StringRef in_string);
0124 
0125   void AppendRawError(llvm::StringRef in_string);
0126 
0127   void AppendErrorWithFormat(const char *format, ...)
0128       __attribute__((format(printf, 2, 3)));
0129 
0130   template <typename... Args>
0131   void AppendMessageWithFormatv(const char *format, Args &&... args) {
0132     AppendMessage(llvm::formatv(format, std::forward<Args>(args)...).str());
0133   }
0134 
0135   template <typename... Args>
0136   void AppendNoteWithFormatv(const char *format, Args &&...args) {
0137     AppendNote(llvm::formatv(format, std::forward<Args>(args)...).str());
0138   }
0139 
0140   template <typename... Args>
0141   void AppendWarningWithFormatv(const char *format, Args &&... args) {
0142     AppendWarning(llvm::formatv(format, std::forward<Args>(args)...).str());
0143   }
0144 
0145   template <typename... Args>
0146   void AppendErrorWithFormatv(const char *format, Args &&... args) {
0147     AppendError(llvm::formatv(format, std::forward<Args>(args)...).str());
0148   }
0149 
0150   void SetError(Status error);
0151 
0152   void SetError(llvm::Error error);
0153 
0154   void SetDiagnosticIndent(std::optional<uint16_t> indent) {
0155     m_diagnostic_indent = indent;
0156   }
0157 
0158   std::optional<uint16_t> GetDiagnosticIndent() const {
0159     return m_diagnostic_indent;
0160   }
0161 
0162   lldb::ReturnStatus GetStatus() const;
0163 
0164   void SetStatus(lldb::ReturnStatus status);
0165 
0166   bool Succeeded() const;
0167 
0168   bool HasResult() const;
0169 
0170   bool GetDidChangeProcessState() const;
0171 
0172   void SetDidChangeProcessState(bool b);
0173 
0174   bool GetInteractive() const;
0175 
0176   void SetInteractive(bool b);
0177 
0178   bool GetSuppressImmediateOutput() const;
0179 
0180   void SetSuppressImmediateOutput(bool b);
0181 
0182 private:
0183   enum { eStreamStringIndex = 0, eImmediateStreamIndex = 1 };
0184 
0185   StreamTee m_out_stream;
0186   StreamTee m_err_stream;
0187   std::vector<DiagnosticDetail> m_diagnostics;
0188   std::optional<uint16_t> m_diagnostic_indent;
0189 
0190   lldb::ReturnStatus m_status = lldb::eReturnStatusStarted;
0191 
0192   bool m_did_change_process_state = false;
0193   bool m_suppress_immediate_output = false;
0194 
0195   /// If true, then the input handle from the debugger will be hooked up.
0196   bool m_interactive = true;
0197   bool m_colors;
0198 };
0199 
0200 } // namespace lldb_private
0201 
0202 #endif // LLDB_INTERPRETER_COMMANDRETURNOBJECT_H