|
| 1 | +//===-- DiagnosticsRendering.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 | +#ifndef LLDB_UTILITY_DIAGNOSTICSRENDERING_H |
| 10 | +#define LLDB_UTILITY_DIAGNOSTICSRENDERING_H |
| 11 | + |
| 12 | +#include "lldb/Utility/Status.h" |
| 13 | +#include "lldb/Utility/Stream.h" |
| 14 | +#include "llvm/Support/WithColor.h" |
| 15 | + |
| 16 | +namespace lldb_private { |
| 17 | + |
| 18 | +/// A compiler-independent representation of an \c |
| 19 | +/// lldb_private::Diagnostic. Expression evaluation failures often |
| 20 | +/// have more than one diagnostic that a UI layer might want to render |
| 21 | +/// differently, for example to colorize it. |
| 22 | +/// |
| 23 | +/// Running example: |
| 24 | +/// (lldb) expr 1 + foo |
| 25 | +/// error: <user expression 0>:1:3: use of undeclared identifier 'foo' |
| 26 | +/// 1 + foo |
| 27 | +/// ^~~ |
| 28 | +struct DiagnosticDetail { |
| 29 | + /// A source location consisting of a file name and position. |
| 30 | + struct SourceLocation { |
| 31 | + /// \c "<user expression 0>" in the example above. |
| 32 | + FileSpec file; |
| 33 | + /// \c 1 in the example above. |
| 34 | + unsigned line = 0; |
| 35 | + /// \c 5 in the example above. |
| 36 | + uint16_t column = 0; |
| 37 | + /// \c 3 in the example above. |
| 38 | + uint16_t length = 0; |
| 39 | + /// Whether this source location should be surfaced to the |
| 40 | + /// user. For example, syntax errors diagnosed in LLDB's |
| 41 | + /// expression wrapper code have this set to true. |
| 42 | + bool hidden = false; |
| 43 | + /// Whether this source location refers to something the user |
| 44 | + /// typed as part of the command, i.e., if this qualifies for |
| 45 | + /// inline display, or if the source line would need to be echoed |
| 46 | + /// again for the message to make sense. |
| 47 | + bool in_user_input = false; |
| 48 | + }; |
| 49 | + /// Contains this diagnostic's source location, if applicable. |
| 50 | + std::optional<SourceLocation> source_location; |
| 51 | + /// Contains \c eSeverityError in the example above. |
| 52 | + lldb::Severity severity = lldb::eSeverityInfo; |
| 53 | + /// Contains "use of undeclared identifier 'foo'" in the example above. |
| 54 | + std::string message; |
| 55 | + /// Contains the fully rendered error message, without "error: ", |
| 56 | + /// but including the source context. |
| 57 | + std::string rendered; |
| 58 | +}; |
| 59 | + |
| 60 | +class DiagnosticError |
| 61 | + : public llvm::ErrorInfo<DiagnosticError, CloneableECError> { |
| 62 | +public: |
| 63 | + using llvm::ErrorInfo<DiagnosticError, CloneableECError>::ErrorInfo; |
| 64 | + DiagnosticError(std::error_code ec, std::string msg = {}) : ErrorInfo(ec) {} |
| 65 | + lldb::ErrorType GetErrorType() const override; |
| 66 | + virtual llvm::ArrayRef<DiagnosticDetail> GetDetails() const; |
| 67 | + static char ID; |
| 68 | +}; |
| 69 | + |
| 70 | +const char *ExpressionResultAsCString(lldb::ExpressionResults result); |
| 71 | + |
| 72 | +llvm::raw_ostream &PrintSeverity(Stream &stream, lldb::Severity severity); |
| 73 | + |
| 74 | +void RenderDiagnosticDetails(Stream &stream, |
| 75 | + std::optional<uint16_t> offset_in_command, |
| 76 | + bool show_inline, |
| 77 | + llvm::ArrayRef<DiagnosticDetail> details); |
| 78 | +} // namespace lldb_private |
| 79 | +#endif |
0 commit comments