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/STLExtras.h"
16 #include "llvm/ADT/StringRef.h"
21 namespace lldb_private {
23 enum DiagnosticOrigin {
24 eDiagnosticOriginUnknown = 0,
25 eDiagnosticOriginLLDB,
26 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 eDiagnosticOriginLLVM:
52 case eDiagnosticOriginClang:
53 case eDiagnosticOriginSwift:
58 Diagnostic(llvm::StringRef message, DiagnosticSeverity severity,
59 DiagnosticOrigin origin, uint32_t compiler_id)
60 : m_message(message), m_severity(severity), m_origin(origin),
61 m_compiler_id(compiler_id) {}
63 Diagnostic(const Diagnostic &rhs)
64 : m_message(rhs.m_message), m_severity(rhs.m_severity),
65 m_origin(rhs.m_origin), m_compiler_id(rhs.m_compiler_id) {}
67 virtual ~Diagnostic() = default;
69 virtual bool HasFixIts() const { return false; }
71 DiagnosticSeverity GetSeverity() const { return m_severity; }
73 uint32_t GetCompilerID() const { return m_compiler_id; }
75 llvm::StringRef GetMessage() const { return m_message; }
77 void AppendMessage(llvm::StringRef message,
78 bool precede_with_newline = true) {
79 if (precede_with_newline)
80 m_message.push_back('\n');
81 m_message.append(message);
85 std::string m_message;
86 DiagnosticSeverity m_severity;
87 DiagnosticOrigin m_origin;
88 uint32_t m_compiler_id; // Compiler-specific diagnostic ID
91 typedef std::vector<std::unique_ptr<Diagnostic>> DiagnosticList;
93 class DiagnosticManager {
96 m_diagnostics.clear();
97 m_fixed_expression.clear();
100 const DiagnosticList &Diagnostics() { return m_diagnostics; }
102 bool HasFixIts() const {
103 return llvm::any_of(m_diagnostics,
104 [](const std::unique_ptr<Diagnostic> &diag) {
105 return diag->HasFixIts();
109 void AddDiagnostic(llvm::StringRef message, DiagnosticSeverity severity,
110 DiagnosticOrigin origin,
111 uint32_t compiler_id = LLDB_INVALID_COMPILER_ID) {
112 m_diagnostics.emplace_back(
113 std::make_unique<Diagnostic>(message, severity, origin, compiler_id));
116 void AddDiagnostic(std::unique_ptr<Diagnostic> diagnostic) {
117 m_diagnostics.push_back(std::move(diagnostic));
120 size_t Printf(DiagnosticSeverity severity, const char *format, ...)
121 __attribute__((format(printf, 3, 4)));
122 void PutString(DiagnosticSeverity severity, llvm::StringRef str);
124 void AppendMessageToDiagnostic(llvm::StringRef str) {
125 if (!m_diagnostics.empty())
126 m_diagnostics.back()->AppendMessage(str);
129 // Returns a string containing errors in this format:
131 // "error: error text\n
132 // warning: warning text\n
134 std::string GetString(char separator = '\n');
138 const std::string &GetFixedExpression() { return m_fixed_expression; }
140 // Moves fixed_expression to the internal storage.
141 void SetFixedExpression(std::string fixed_expression) {
142 m_fixed_expression = std::move(fixed_expression);
146 DiagnosticList m_diagnostics;
147 std::string m_fixed_expression;
151 #endif /* lldb_DiagnosticManager_h */