| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_DIAGNOSTICS_H |
| 10 |
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_DIAGNOSTICS_H |
| 11 |
|
| 12 |
#include "Protocol.h" |
| 13 |
#include "clang/Basic/Diagnostic.h" |
| 14 |
#include "clang/Basic/LangOptions.h" |
| 15 |
#include "clang/Basic/SourceLocation.h" |
| 16 |
#include "llvm/ADT/ArrayRef.h" |
| 17 |
#include "llvm/ADT/DenseSet.h" |
| 18 |
#include "llvm/ADT/SmallVector.h" |
| 19 |
#include "llvm/ADT/StringSet.h" |
| 20 |
#include "llvm/Support/JSON.h" |
| 21 |
#include "llvm/Support/SourceMgr.h" |
| 22 |
#include <cassert> |
| 23 |
#include <functional> |
| 24 |
#include <memory> |
| 25 |
#include <optional> |
| 26 |
#include <string> |
| 27 |
#include <utility> |
| 28 |
#include <vector> |
| 29 |
|
| 30 |
namespace clang { |
| 31 |
namespace tidy { |
| 32 |
class ClangTidyContext; |
| 33 |
} |
| 34 |
namespace clangd { |
| 35 |
|
| 36 |
struct ClangdDiagnosticOptions { |
| 37 |
|
| 38 |
|
| 39 |
bool EmbedFixesInDiagnostics = false; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
bool EmitRelatedLocations = false; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
bool SendDiagnosticCategory = false; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
bool DisplayFixesCount = true; |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
struct DiagBase { |
| 59 |
std::string Message; |
| 60 |
|
| 61 |
|
| 62 |
std::string File; |
| 63 |
|
| 64 |
std::optional<std::string> AbsFile; |
| 65 |
|
| 66 |
clangd::Range Range; |
| 67 |
DiagnosticsEngine::Level Severity = DiagnosticsEngine::Note; |
| 68 |
std::string Category; |
| 69 |
|
| 70 |
|
| 71 |
bool InsideMainFile = false; |
| 72 |
unsigned ID = 0; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
llvm::json::Object OpaqueData; |
| 77 |
}; |
| 78 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const DiagBase &D); |
| 79 |
|
| 80 |
|
| 81 |
struct Fix { |
| 82 |
|
| 83 |
std::string Message; |
| 84 |
|
| 85 |
llvm::SmallVector<TextEdit, 1> Edits; |
| 86 |
}; |
| 87 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Fix &F); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
struct Note : DiagBase {}; |
| 92 |
|
| 93 |
|
| 94 |
struct Diag : DiagBase { |
| 95 |
std::string Name; |
| 96 |
|
| 97 |
enum DiagSource { |
| 98 |
Unknown, |
| 99 |
Clang, |
| 100 |
ClangTidy, |
| 101 |
Clangd, |
| 102 |
ClangdConfig, |
| 103 |
} Source = Unknown; |
| 104 |
|
| 105 |
std::vector<Note> Notes; |
| 106 |
|
| 107 |
std::vector<Fix> Fixes; |
| 108 |
llvm::SmallVector<DiagnosticTag, 1> Tags; |
| 109 |
}; |
| 110 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diag &D); |
| 111 |
|
| 112 |
Diag toDiag(const llvm::SMDiagnostic &, Diag::DiagSource Source); |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
void toLSPDiags( |
| 120 |
const Diag &D, const URIForFile &File, const ClangdDiagnosticOptions &Opts, |
| 121 |
llvm::function_ref<void(clangd::Diagnostic, llvm::ArrayRef<Fix>)> OutFn); |
| 122 |
|
| 123 |
|
| 124 |
CodeAction toCodeAction(const Fix &D, const URIForFile &File); |
| 125 |
|
| 126 |
|
| 127 |
int getSeverity(DiagnosticsEngine::Level L); |
| 128 |
|
| 129 |
|
| 130 |
std::optional<std::string> getDiagnosticDocURI(Diag::DiagSource, unsigned ID, |
| 131 |
llvm::StringRef Name); |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
class StoreDiags : public DiagnosticConsumer { |
| 138 |
public: |
| 139 |
|
| 140 |
std::vector<Diag> take(const clang::tidy::ClangTidyContext *Tidy = nullptr); |
| 141 |
|
| 142 |
void BeginSourceFile(const LangOptions &Opts, |
| 143 |
const Preprocessor *PP) override; |
| 144 |
void EndSourceFile() override; |
| 145 |
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 146 |
const clang::Diagnostic &Info) override; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
using DiagFixer = std::function<std::vector<Fix>(DiagnosticsEngine::Level, |
| 151 |
const clang::Diagnostic &)>; |
| 152 |
using LevelAdjuster = std::function<DiagnosticsEngine::Level( |
| 153 |
DiagnosticsEngine::Level, const clang::Diagnostic &)>; |
| 154 |
using DiagCallback = |
| 155 |
std::function<void(const clang::Diagnostic &, clangd::Diag &)>; |
| 156 |
|
| 157 |
void contributeFixes(DiagFixer Fixer) { this->Fixer = Fixer; } |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
void setLevelAdjuster(LevelAdjuster Adjuster) { this->Adjuster = Adjuster; } |
| 162 |
|
| 163 |
|
| 164 |
void setDiagCallback(DiagCallback CB) { DiagCB = std::move(CB); } |
| 165 |
|
| 166 |
private: |
| 167 |
void flushLastDiag(); |
| 168 |
|
| 169 |
DiagFixer Fixer = nullptr; |
| 170 |
LevelAdjuster Adjuster = nullptr; |
| 171 |
DiagCallback DiagCB = nullptr; |
| 172 |
std::vector<Diag> Output; |
| 173 |
std::optional<LangOptions> LangOpts; |
| 174 |
std::optional<Diag> LastDiag; |
| 175 |
std::optional<FullSourceLoc> LastDiagLoc; |
| 176 |
bool LastDiagOriginallyError = false; |
| 177 |
SourceManager *OrigSrcMgr = nullptr; |
| 178 |
|
| 179 |
llvm::DenseSet<std::pair<unsigned, unsigned>> IncludedErrorLocations; |
| 180 |
}; |
| 181 |
|
| 182 |
|
| 183 |
bool isBuiltinDiagnosticSuppressed(unsigned ID, |
| 184 |
const llvm::StringSet<> &Suppressed, |
| 185 |
const LangOptions &); |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
llvm::StringRef normalizeSuppressedCode(llvm::StringRef); |
| 191 |
|
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
#endif |