/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/include/lldb/Expression/DiagnosticManager.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- DiagnosticManager.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_EXPRESSION_DIAGNOSTICMANAGER_H |
10 | | #define LLDB_EXPRESSION_DIAGNOSTICMANAGER_H |
11 | | |
12 | | #include "lldb/lldb-defines.h" |
13 | | #include "lldb/lldb-types.h" |
14 | | |
15 | | #include "llvm/ADT/STLExtras.h" |
16 | | #include "llvm/ADT/StringRef.h" |
17 | | |
18 | | #include <string> |
19 | | #include <vector> |
20 | | |
21 | | namespace lldb_private { |
22 | | |
23 | | enum DiagnosticOrigin { |
24 | | eDiagnosticOriginUnknown = 0, |
25 | | eDiagnosticOriginLLDB, |
26 | | eDiagnosticOriginClang, |
27 | | eDiagnosticOriginSwift, |
28 | | eDiagnosticOriginLLVM |
29 | | }; |
30 | | |
31 | | enum DiagnosticSeverity { |
32 | | eDiagnosticSeverityError, |
33 | | eDiagnosticSeverityWarning, |
34 | | eDiagnosticSeverityRemark |
35 | | }; |
36 | | |
37 | | const uint32_t LLDB_INVALID_COMPILER_ID = UINT32_MAX; |
38 | | |
39 | | class Diagnostic { |
40 | | friend class DiagnosticManager; |
41 | | |
42 | | public: |
43 | 110 | DiagnosticOrigin getKind() const { return m_origin; } |
44 | | |
45 | 0 | static bool classof(const Diagnostic *diag) { |
46 | 0 | DiagnosticOrigin kind = diag->getKind(); |
47 | 0 | switch (kind) { |
48 | 0 | case eDiagnosticOriginUnknown: |
49 | 0 | case eDiagnosticOriginLLDB: |
50 | 0 | case eDiagnosticOriginLLVM: |
51 | 0 | return true; |
52 | 0 | case eDiagnosticOriginClang: |
53 | 0 | case eDiagnosticOriginSwift: |
54 | 0 | return false; |
55 | 0 | } |
56 | 0 | } |
57 | | |
58 | | Diagnostic(llvm::StringRef message, DiagnosticSeverity severity, |
59 | | DiagnosticOrigin origin, uint32_t compiler_id) |
60 | 661 | : m_message(message), m_severity(severity), m_origin(origin), |
61 | 661 | m_compiler_id(compiler_id) {} |
62 | | |
63 | | Diagnostic(const Diagnostic &rhs) |
64 | | : m_message(rhs.m_message), m_severity(rhs.m_severity), |
65 | 0 | m_origin(rhs.m_origin), m_compiler_id(rhs.m_compiler_id) {} |
66 | | |
67 | 661 | virtual ~Diagnostic() = default; |
68 | | |
69 | 14 | virtual bool HasFixIts() const { return false; } |
70 | | |
71 | 552 | DiagnosticSeverity GetSeverity() const { return m_severity; } |
72 | | |
73 | | uint32_t GetCompilerID() const { return m_compiler_id; } |
74 | | |
75 | 482 | llvm::StringRef GetMessage() const { return m_message; } |
76 | | |
77 | | void AppendMessage(llvm::StringRef message, |
78 | 106 | bool precede_with_newline = true) { |
79 | 106 | if (precede_with_newline) |
80 | 106 | m_message.push_back('\n'); |
81 | 106 | m_message += message; |
82 | 106 | } |
83 | | |
84 | | protected: |
85 | | std::string m_message; |
86 | | DiagnosticSeverity m_severity; |
87 | | DiagnosticOrigin m_origin; |
88 | | uint32_t m_compiler_id; // Compiler-specific diagnostic ID |
89 | | }; |
90 | | |
91 | | typedef std::vector<std::unique_ptr<Diagnostic>> DiagnosticList; |
92 | | |
93 | | class DiagnosticManager { |
94 | | public: |
95 | 12.8k | void Clear() { |
96 | 12.8k | m_diagnostics.clear(); |
97 | 12.8k | m_fixed_expression.clear(); |
98 | 12.8k | } |
99 | | |
100 | 914 | const DiagnosticList &Diagnostics() { return m_diagnostics; } |
101 | | |
102 | 274 | bool HasFixIts() const { |
103 | 274 | return llvm::any_of(m_diagnostics, |
104 | 388 | [](const std::unique_ptr<Diagnostic> &diag) { |
105 | 388 | return diag->HasFixIts(); |
106 | 388 | }); |
107 | 274 | } |
108 | | |
109 | | void AddDiagnostic(llvm::StringRef message, DiagnosticSeverity severity, |
110 | | DiagnosticOrigin origin, |
111 | 171 | uint32_t compiler_id = LLDB_INVALID_COMPILER_ID) { |
112 | 171 | m_diagnostics.emplace_back( |
113 | 171 | std::make_unique<Diagnostic>(message, severity, origin, compiler_id)); |
114 | 171 | } |
115 | | |
116 | 491 | void AddDiagnostic(std::unique_ptr<Diagnostic> diagnostic) { |
117 | 491 | if (diagnostic) |
118 | 490 | m_diagnostics.push_back(std::move(diagnostic)); |
119 | 491 | } |
120 | | |
121 | | size_t Printf(DiagnosticSeverity severity, const char *format, ...) |
122 | | __attribute__((format(printf, 3, 4))); |
123 | | void PutString(DiagnosticSeverity severity, llvm::StringRef str); |
124 | | |
125 | 107 | void AppendMessageToDiagnostic(llvm::StringRef str) { |
126 | 107 | if (!m_diagnostics.empty()) |
127 | 106 | m_diagnostics.back()->AppendMessage(str); |
128 | 107 | } |
129 | | |
130 | | // Returns a string containing errors in this format: |
131 | | // |
132 | | // "error: error text\n |
133 | | // warning: warning text\n |
134 | | // remark text\n" |
135 | | std::string GetString(char separator = '\n'); |
136 | | |
137 | | void Dump(Log *log); |
138 | | |
139 | 27 | const std::string &GetFixedExpression() { return m_fixed_expression; } |
140 | | |
141 | | // Moves fixed_expression to the internal storage. |
142 | 26 | void SetFixedExpression(std::string fixed_expression) { |
143 | 26 | m_fixed_expression = std::move(fixed_expression); |
144 | 26 | } |
145 | | |
146 | | protected: |
147 | | DiagnosticList m_diagnostics; |
148 | | std::string m_fixed_expression; |
149 | | }; |
150 | | } |
151 | | |
152 | | #endif // LLDB_EXPRESSION_DIAGNOSTICMANAGER_H |