clang-tools 8.0.0

Diagnostics.h
Go to the documentation of this file.
1 //===--- Diagnostics.h -------------------------------------------*- C++-*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_DIAGNOSTICS_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_DIAGNOSTICS_H
12
13 #include "Path.h"
14 #include "Protocol.h"
15 #include "clang/Basic/Diagnostic.h"
16 #include "clang/Basic/LangOptions.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/StringSet.h"
20 #include <cassert>
21 #include <string>
22
23 namespace clang {
24 namespace clangd {
25
26 struct ClangdDiagnosticOptions {
27 /// If true, Clangd uses an LSP extension to embed the fixes with the
28 /// diagnostics that are sent to the client.
29 bool EmbedFixesInDiagnostics = false;
30
31 /// If true, Clangd uses an LSP extension to send the diagnostic's
32 /// category to the client. The category typically describes the compilation
33 /// stage during which the issue was produced, e.g. "Semantic Issue" or "Parse
34 /// Issue".
35 bool SendDiagnosticCategory = false;
36 };
37
38 /// Contains basic information about a diagnostic.
39 struct DiagBase {
40 std::string Message;
41 // Intended to be used only in error messages.
42 // May be relative, absolute or even artifically constructed.
43 std::string File;
44 clangd::Range Range;
45 DiagnosticsEngine::Level Severity = DiagnosticsEngine::Note;
46 std::string Category;
47 // Since File is only descriptive, we store a separate flag to distinguish
48 // diags from the main file.
49 bool InsideMainFile = false;
50 };
51 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const DiagBase &D);
52
53 /// Represents a single fix-it that editor can apply to fix the error.
54 struct Fix {
55 /// Message for the fix-it.
56 std::string Message;
57 /// TextEdits from clang's fix-its. Must be non-empty.
58 llvm::SmallVector<TextEdit, 1> Edits;
59 };
60 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Fix &F);
61
62 /// Represents a note for the diagnostic. Severity of notes can only be 'note'
63 /// or 'remark'.
64 struct Note : DiagBase {};
65
66 /// A top-level diagnostic that may have Notes and Fixes.
67 struct Diag : DiagBase {
68 /// Elaborate on the problem, usually pointing to a related piece of code.
69 std::vector<Note> Notes;
70 /// *Alternative* fixes for this diagnostic, one should be chosen.
71 std::vector<Fix> Fixes;
72 };
73 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diag &D);
74
75 /// Conversion to LSP diagnostics. Formats the error message of each diagnostic
76 /// to include all its notes. Notes inside main file are also provided as
77 /// separate diagnostics with their corresponding fixits. Notes outside main
78 /// file do not have a corresponding LSP diagnostic, but can still be included
79 /// as part of their main diagnostic's message.
80 void toLSPDiags(
81 const Diag &D, const URIForFile &File, const ClangdDiagnosticOptions &Opts,
82 llvm::function_ref<void(clangd::Diagnostic, llvm::ArrayRef<Fix>)> OutFn);
83
84 /// Convert from Fix to LSP CodeAction.
85 CodeAction toCodeAction(const Fix &D, const URIForFile &File);
86
87 /// Convert from clang diagnostic level to LSP severity.
88 int getSeverity(DiagnosticsEngine::Level L);
89
90 /// StoreDiags collects the diagnostics that can later be reported by
91 /// clangd. It groups all notes for a diagnostic into a single Diag
92 /// and filters out diagnostics that don't mention the main file (i.e. neither
93 /// the diag itself nor its notes are in the main file).
94 class StoreDiags : public DiagnosticConsumer {
95 public:
96 std::vector<Diag> take();
97
98 void BeginSourceFile(const LangOptions &Opts, const Preprocessor *) override;
99 void EndSourceFile() override;
100 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
101 const clang::Diagnostic &Info) override;
102
103 private:
104 void flushLastDiag();
105
106 std::vector<Diag> Output;
107 llvm::Optional<LangOptions> LangOpts;
108 llvm::Optional<Diag> LastDiag;
109 };
110
111 } // namespace clangd
112 } // namespace clang
113
114 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_DIAGNOSTICS_H
DiagnosticConsumer
clang::clangd::StoreDiags
StoreDiags collects the diagnostics that can later be reported by clangd.
Definition: Diagnostics.h:94
clang::clangd::ClangdDiagnosticOptions::EmbedFixesInDiagnostics
bool EmbedFixesInDiagnostics
If true, Clangd uses an LSP extension to embed the fixes with the diagnostics that are sent to the cl...
Definition: Diagnostics.h:29
clang::clangd::DiagBase
Contains basic information about a diagnostic.
Definition: Diagnostics.h:39
clang::clangd::toCodeAction
CodeAction toCodeAction(const Fix &F, const URIForFile &File)
Convert from Fix to LSP CodeAction.
Definition: Diagnostics.cpp:232
Protocol.h
clang::clangd::CodeAction
A code action represents a change that can be performed in code, e.g.
Definition: Protocol.h:665
clang::clangd::Diag
A top-level diagnostic that may have Notes and Fixes.
Definition: Diagnostics.h:67
clang::clangd::Fix::Message
std::string Message
Message for the fix-it.
Definition: Diagnostics.h:56
clang::clangd::Diag::Fixes
std::vector< Fix > Fixes
Alternative fixes for this diagnostic, one should be chosen.
Definition: Diagnostics.h:71
clang::clangd::DiagBase::Category
std::string Category
Definition: Diagnostics.h:46
clang::clangd::Fix::Edits
llvm::SmallVector< TextEdit, 1 > Edits
TextEdits from clang&#39;s fix-its. Must be non-empty.
Definition: Diagnostics.h:58
clang::clangd::DiagBase::File
std::string File
Definition: Diagnostics.h:43
clang::clangd::toLSPDiags
void toLSPDiags(const Diag &D, const URIForFile &File, const ClangdDiagnosticOptions &Opts, llvm::function_ref< void(clangd::Diagnostic, llvm::ArrayRef< Fix >)> OutFn)
Conversion to LSP diagnostics.
Definition: Diagnostics.cpp:242
clang::clangd::Range
Definition: Protocol.h:154
clang::clangd::getSeverity
int getSeverity(DiagnosticsEngine::Level L)
Convert from clang diagnostic level to LSP severity.
Definition: Diagnostics.cpp:275
clang::clangd::DiagBase::Range
clangd::Range Range
Definition: Diagnostics.h:44
clang::clangd::ClangdDiagnosticOptions
Definition: Diagnostics.h:26
D
const Decl * D
Definition: XRefs.cpp:79
clang::clangd::Fix
Represents a single fix-it that editor can apply to fix the error.
Definition: Diagnostics.h:54
clang::clangd::URIForFile
Definition: Protocol.h:71
Info
FunctionInfo Info
Definition: FunctionSizeCheck.cpp:121
clang::clangd::Diag::Notes
std::vector< Note > Notes
Elaborate on the problem, usually pointing to a related piece of code.
Definition: Diagnostics.h:69
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
Path.h
clang::clangd::DiagBase::Message
std::string Message
Definition: Diagnostics.h:40
clang::clangd::ClangdDiagnosticOptions::SendDiagnosticCategory
bool SendDiagnosticCategory
If true, Clangd uses an LSP extension to send the diagnostic&#39;s category to the client.
Definition: Diagnostics.h:35
clang::clangd::Diagnostic
Definition: Protocol.h:561
clang::clangd::Note
Represents a note for the diagnostic.
Definition: Diagnostics.h:64
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:1719
clang::clangd::CompletionItemKind::File