1 //===-- DiagnosticManager.h -------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef lldb_DiagnosticManager_h
10 #define lldb_DiagnosticManager_h
12 #include "lldb/lldb-defines.h"
13 #include "lldb/lldb-types.h"
15 #include "llvm/ADT/StringRef.h"
20 namespace lldb_private {
22 enum DiagnosticOrigin {
23 eDiagnosticOriginUnknown = 0,
24 eDiagnosticOriginLLDB,
25 eDiagnosticOriginClang,
27 eDiagnosticOriginSwift,
31 enum DiagnosticSeverity {
32 eDiagnosticSeverityError,
33 eDiagnosticSeverityWarning,
34 eDiagnosticSeverityRemark
37 const uint32_t LLDB_INVALID_COMPILER_ID = UINT32_MAX;
40 friend class DiagnosticManager;
43 DiagnosticOrigin getKind() const { return m_origin; }
45 static bool classof(const Diagnostic *diag) {
46 DiagnosticOrigin kind = diag->getKind();
48 case eDiagnosticOriginUnknown:
49 case eDiagnosticOriginLLDB:
50 case eDiagnosticOriginGo:
51 case eDiagnosticOriginLLVM:
53 case eDiagnosticOriginClang:
54 case eDiagnosticOriginSwift:
59 Diagnostic(llvm::StringRef message, DiagnosticSeverity severity,
60 DiagnosticOrigin origin, uint32_t compiler_id)
61 : m_message(message), m_severity(severity), m_origin(origin),
62 m_compiler_id(compiler_id) {}
64 Diagnostic(const Diagnostic &rhs)
65 : m_message(rhs.m_message), m_severity(rhs.m_severity),
66 m_origin(rhs.m_origin), m_compiler_id(rhs.m_compiler_id) {}
68 virtual ~Diagnostic() = default;
70 virtual bool HasFixIts() const { return false; }
72 DiagnosticSeverity GetSeverity() const { return m_severity; }
74 uint32_t GetCompilerID() const { return m_compiler_id; }
76 llvm::StringRef GetMessage() const { return m_message; }
78 void AppendMessage(llvm::StringRef message,
79 bool precede_with_newline = true) {
80 if (precede_with_newline)
81 m_message.push_back('\n');
82 m_message.append(message);
86 std::string m_message;
87 DiagnosticSeverity m_severity;
88 DiagnosticOrigin m_origin;
89 uint32_t m_compiler_id; // Compiler-specific diagnostic ID
92 typedef std::vector<Diagnostic *> DiagnosticList;
94 class DiagnosticManager {
97 m_diagnostics.clear();
98 m_fixed_expression.clear();
101 // The diagnostic manager holds a list of diagnostics, which are owned by the
103 const DiagnosticList &Diagnostics() { return m_diagnostics; }
105 ~DiagnosticManager() {
106 for (Diagnostic *diag : m_diagnostics) {
112 for (Diagnostic *diag : m_diagnostics) {
113 if (diag->HasFixIts())
119 void AddDiagnostic(llvm::StringRef message, DiagnosticSeverity severity,
120 DiagnosticOrigin origin,
121 uint32_t compiler_id = LLDB_INVALID_COMPILER_ID) {
122 m_diagnostics.push_back(
123 new Diagnostic(message, severity, origin, compiler_id));
126 void AddDiagnostic(Diagnostic *diagnostic) {
127 m_diagnostics.push_back(diagnostic);
130 void CopyDiagnostics(DiagnosticManager &otherDiagnostics);
132 size_t Printf(DiagnosticSeverity severity, const char *format, ...)
133 __attribute__((format(printf, 3, 4)));
134 size_t PutString(DiagnosticSeverity severity, llvm::StringRef str);
136 void AppendMessageToDiagnostic(llvm::StringRef str) {
137 if (!m_diagnostics.empty()) {
138 m_diagnostics.back()->AppendMessage(str);
142 // Returns a string containing errors in this format:
144 // "error: error text\n
145 // warning: warning text\n
147 std::string GetString(char separator = '\n');
151 const std::string &GetFixedExpression() { return m_fixed_expression; }
153 // Moves fixed_expression to the internal storage.
154 void SetFixedExpression(std::string fixed_expression) {
155 m_fixed_expression = std::move(fixed_expression);
156 fixed_expression.clear();
160 DiagnosticList m_diagnostics;
161 std::string m_fixed_expression;
165 #endif /* lldb_DiagnosticManager_h */