clang-tools  14.0.0git
Diagnostics.h
Go to the documentation of this file.
1 //===--- Diagnostics.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 LLVM_CLANG_TOOLS_EXTRA_CLANGD_DIAGNOSTICS_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_DIAGNOSTICS_H
11 
12 #include "Protocol.h"
13 #include "support/Path.h"
14 #include "clang/Basic/Diagnostic.h"
15 #include "clang/Basic/LangOptions.h"
16 #include "clang/Basic/SourceLocation.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/None.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringSet.h"
24 #include "llvm/Support/JSON.h"
25 #include "llvm/Support/SourceMgr.h"
26 #include <cassert>
27 #include <functional>
28 #include <memory>
29 #include <string>
30 #include <utility>
31 #include <vector>
32 
33 namespace clang {
34 namespace tidy {
35 class ClangTidyContext;
36 } // namespace tidy
37 namespace clangd {
38 
40  /// If true, Clangd uses an LSP extension to embed the fixes with the
41  /// diagnostics that are sent to the client.
43 
44  /// If true, Clangd uses the relatedInformation field to include other
45  /// locations (in particular attached notes).
46  /// Otherwise, these are flattened into the diagnostic message.
47  bool EmitRelatedLocations = false;
48 
49  /// If true, Clangd uses an LSP extension to send the diagnostic's
50  /// category to the client. The category typically describes the compilation
51  /// stage during which the issue was produced, e.g. "Semantic Issue" or "Parse
52  /// Issue".
53  bool SendDiagnosticCategory = false;
54 
55  /// If true, Clangd will add a number of available fixes to the diagnostic's
56  /// message.
57  bool DisplayFixesCount = true;
58 };
59 
60 /// Contains basic information about a diagnostic.
61 struct DiagBase {
62  std::string Message;
63  // Intended to be used only in error messages.
64  // May be relative, absolute or even artificially constructed.
65  std::string File;
66  // Absolute path to containing file, if available.
67  llvm::Optional<std::string> AbsFile;
68 
70  DiagnosticsEngine::Level Severity = DiagnosticsEngine::Note;
71  std::string Category;
72  // Since File is only descriptive, we store a separate flag to distinguish
73  // diags from the main file.
74  bool InsideMainFile = false;
75  unsigned ID; // e.g. member of clang::diag, or clang-tidy assigned ID.
76  // Feature modules can make use of this field to propagate data from a
77  // diagnostic to a CodeAction request. Each module should only append to the
78  // list.
79  llvm::json::Object OpaqueData;
80 };
81 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const DiagBase &D);
82 
83 /// Represents a single fix-it that editor can apply to fix the error.
84 struct Fix {
85  /// Message for the fix-it.
86  std::string Message;
87  /// TextEdits from clang's fix-its. Must be non-empty.
88  llvm::SmallVector<TextEdit, 1> Edits;
89 };
90 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Fix &F);
91 
92 /// Represents a note for the diagnostic. Severity of notes can only be 'note'
93 /// or 'remark'.
94 struct Note : DiagBase {};
95 
96 /// A top-level diagnostic that may have Notes and Fixes.
97 struct Diag : DiagBase {
98  std::string Name; // if ID was recognized.
99  // The source of this diagnostic.
100  enum DiagSource {
105  } Source = Unknown;
106  /// Elaborate on the problem, usually pointing to a related piece of code.
107  std::vector<Note> Notes;
108  /// *Alternative* fixes for this diagnostic, one should be chosen.
109  std::vector<Fix> Fixes;
110  llvm::SmallVector<DiagnosticTag, 1> Tags;
111 };
112 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diag &D);
113 
114 Diag toDiag(const llvm::SMDiagnostic &, Diag::DiagSource Source);
115 
116 /// Conversion to LSP diagnostics. Formats the error message of each diagnostic
117 /// to include all its notes. Notes inside main file are also provided as
118 /// separate diagnostics with their corresponding fixits. Notes outside main
119 /// file do not have a corresponding LSP diagnostic, but can still be included
120 /// as part of their main diagnostic's message.
121 void toLSPDiags(
122  const Diag &D, const URIForFile &File, const ClangdDiagnosticOptions &Opts,
123  llvm::function_ref<void(clangd::Diagnostic, llvm::ArrayRef<Fix>)> OutFn);
124 
125 /// Convert from Fix to LSP CodeAction.
126 CodeAction toCodeAction(const Fix &D, const URIForFile &File);
127 
128 /// Convert from clang diagnostic level to LSP severity.
129 int getSeverity(DiagnosticsEngine::Level L);
130 
131 /// StoreDiags collects the diagnostics that can later be reported by
132 /// clangd. It groups all notes for a diagnostic into a single Diag
133 /// and filters out diagnostics that don't mention the main file (i.e. neither
134 /// the diag itself nor its notes are in the main file).
136 public:
137  // The ClangTidyContext populates Source and Name for clang-tidy diagnostics.
138  std::vector<Diag> take(const clang::tidy::ClangTidyContext *Tidy = nullptr);
139 
140  void BeginSourceFile(const LangOptions &Opts,
141  const Preprocessor *PP) override;
142  void EndSourceFile() override;
143  void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
144  const clang::Diagnostic &Info) override;
145 
146  using DiagFixer = std::function<std::vector<Fix>(DiagnosticsEngine::Level,
147  const clang::Diagnostic &)>;
148  using LevelAdjuster = std::function<DiagnosticsEngine::Level(
149  DiagnosticsEngine::Level, const clang::Diagnostic &)>;
150  using DiagCallback =
151  std::function<void(const clang::Diagnostic &, clangd::Diag &)>;
152  /// If set, possibly adds fixes for diagnostics using \p Fixer.
153  void contributeFixes(DiagFixer Fixer) { this->Fixer = Fixer; }
154  /// If set, this allows the client of this class to adjust the level of
155  /// diagnostics, such as promoting warnings to errors, or ignoring
156  /// diagnostics.
157  void setLevelAdjuster(LevelAdjuster Adjuster) { this->Adjuster = Adjuster; }
158  /// Invokes a callback every time a diagnostics is completely formed. Handler
159  /// of the callback can also mutate the diagnostic.
160  void setDiagCallback(DiagCallback CB) { DiagCB = std::move(CB); }
161 
162 private:
163  void flushLastDiag();
164 
165  DiagFixer Fixer = nullptr;
166  LevelAdjuster Adjuster = nullptr;
167  DiagCallback DiagCB = nullptr;
168  std::vector<Diag> Output;
169  llvm::Optional<LangOptions> LangOpts;
170  llvm::Optional<Diag> LastDiag;
171  llvm::Optional<FullSourceLoc> LastDiagLoc; // Valid only when LastDiag is set.
172  bool LastDiagOriginallyError = false; // Valid only when LastDiag is set.
173  SourceManager *OrigSrcMgr = nullptr;
174 
175  llvm::DenseSet<std::pair<unsigned, unsigned>> IncludedErrorLocations;
176 };
177 
178 /// Determine whether a (non-clang-tidy) diagnostic is suppressed by config.
179 bool isBuiltinDiagnosticSuppressed(unsigned ID,
180  const llvm::StringSet<> &Suppressed);
181 /// Take a user-specified diagnostic code, and convert it to a normalized form
182 /// stored in the config and consumed by isBuiltinDiagnosticsSuppressed.
183 ///
184 /// (This strips err_ and -W prefix so we can match with or without them.)
185 llvm::StringRef normalizeSuppressedCode(llvm::StringRef);
186 
187 } // namespace clangd
188 } // namespace clang
189 
190 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_DIAGNOSTICS_H
clang::clangd::DiagBase::Severity
DiagnosticsEngine::Level Severity
Definition: Diagnostics.h:70
clang::clangd::Diag::Tags
llvm::SmallVector< DiagnosticTag, 1 > Tags
Definition: Diagnostics.h:110
clang::clangd::Diag::ClangTidy
@ ClangTidy
Definition: Diagnostics.h:103
clang::clangd::Fix
Represents a single fix-it that editor can apply to fix the error.
Definition: Diagnostics.h:84
clang::clangd::CodeAction
A code action represents a change that can be performed in code, e.g.
Definition: Protocol.h:972
DiagnosticConsumer
Path.h
clang::clangd::Diag::Name
std::string Name
Definition: Diagnostics.h:98
clang::clangd::DiagBase::InsideMainFile
bool InsideMainFile
Definition: Diagnostics.h:74
clang::clangd::ClangdDiagnosticOptions
Definition: Diagnostics.h:39
clang::clangd::getSeverity
int getSeverity(DiagnosticsEngine::Level L)
Convert from clang diagnostic level to LSP severity.
Definition: Diagnostics.cpp:541
clang::clangd::StoreDiags::DiagFixer
std::function< std::vector< Fix >(DiagnosticsEngine::Level, const clang::Diagnostic &)> DiagFixer
Definition: Diagnostics.h:147
clang::clangd::Diag::Source
enum clang::clangd::Diag::DiagSource Source
clang::clangd::StoreDiags::HandleDiagnostic
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info) override
Definition: Diagnostics.cpp:657
clang::clangd::StoreDiags::DiagCallback
std::function< void(const clang::Diagnostic &, clangd::Diag &)> DiagCallback
Definition: Diagnostics.h:151
clang::clangd::StoreDiags::LevelAdjuster
std::function< DiagnosticsEngine::Level(DiagnosticsEngine::Level, const clang::Diagnostic &)> LevelAdjuster
Definition: Diagnostics.h:149
Protocol.h
clang::clangd::ClangdDiagnosticOptions::DisplayFixesCount
bool DisplayFixesCount
If true, Clangd will add a number of available fixes to the diagnostic's message.
Definition: Diagnostics.h:57
clang::clangd::Fix::Edits
llvm::SmallVector< TextEdit, 1 > Edits
TextEdits from clang's fix-its. Must be non-empty.
Definition: Diagnostics.h:88
clang::clangd::Diagnostic
Definition: Protocol.h:829
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
Diagnostic
DiagnosticCallback Diagnostic
Definition: ConfigCompile.cpp:101
clang::clangd::DiagBase::Message
std::string Message
Definition: Diagnostics.h:62
clang::clangd::Fix::Message
std::string Message
Message for the fix-it.
Definition: Diagnostics.h:86
clang::clangd::StoreDiags::EndSourceFile
void EndSourceFile() override
Definition: Diagnostics.cpp:620
clang::clangd::Diag
A top-level diagnostic that may have Notes and Fixes.
Definition: Diagnostics.h:97
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:76
clang::clangd::Diag::Notes
std::vector< Note > Notes
Elaborate on the problem, usually pointing to a related piece of code.
Definition: Diagnostics.h:107
clang::clangd::toDiag
Diag toDiag(const llvm::SMDiagnostic &D, Diag::DiagSource Source)
Definition: Diagnostics.cpp:435
clang::clangd::Diag::Unknown
@ Unknown
Definition: Diagnostics.h:101
clang::clangd::Diag::Fixes
std::vector< Fix > Fixes
Alternative fixes for this diagnostic, one should be chosen.
Definition: Diagnostics.h:109
clang::clangd::URIForFile
Definition: Protocol.h:76
clang::clangd::Diag::Clang
@ Clang
Definition: Diagnostics.h:102
clang::clangd::operator<<
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
Definition: CodeComplete.cpp:2013
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:29
clang::clangd::DiagBase::AbsFile
llvm::Optional< std::string > AbsFile
Definition: Diagnostics.h:67
clang::clangd::toCodeAction
CodeAction toCodeAction(const Fix &F, const URIForFile &File)
Convert from Fix to LSP CodeAction.
Definition: Diagnostics.cpp:426
clang::clangd::StoreDiags::contributeFixes
void contributeFixes(DiagFixer Fixer)
If set, possibly adds fixes for diagnostics using Fixer.
Definition: Diagnostics.h:153
clang::clangd::ClangdDiagnosticOptions::EmitRelatedLocations
bool EmitRelatedLocations
If true, Clangd uses the relatedInformation field to include other locations (in particular attached ...
Definition: Diagnostics.h:47
clang::clangd::ClangdDiagnosticOptions::SendDiagnosticCategory
bool SendDiagnosticCategory
If true, Clangd uses an LSP extension to send the diagnostic's category to the client.
Definition: Diagnostics.h:53
Info
FunctionInfo Info
Definition: FunctionSizeCheck.cpp:120
clang::clangd::DiagBase::OpaqueData
llvm::json::Object OpaqueData
Definition: Diagnostics.h:79
ID
static char ID
Definition: Logger.cpp:74
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:42
clang::clangd::Range
Definition: Protocol.h:177
clang::clangd::DiagBase::Category
std::string Category
Definition: Diagnostics.h:71
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:461
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:163
clang::clangd::StoreDiags::setDiagCallback
void setDiagCallback(DiagCallback CB)
Invokes a callback every time a diagnostics is completely formed.
Definition: Diagnostics.h:160
clang::clangd::StoreDiags
StoreDiags collects the diagnostics that can later be reported by clangd.
Definition: Diagnostics.h:135
clang::clangd::Diag::ClangdConfig
@ ClangdConfig
Definition: Diagnostics.h:104
clang::clangd::DiagBase::ID
unsigned ID
Definition: Diagnostics.h:75
clang::clangd::normalizeSuppressedCode
llvm::StringRef normalizeSuppressedCode(llvm::StringRef Code)
Take a user-specified diagnostic code, and convert it to a normalized form stored in the config and c...
Definition: Diagnostics.cpp:877
clang::clangd::Note
Represents a note for the diagnostic.
Definition: Diagnostics.h:94
clang::clangd::Diag::DiagSource
DiagSource
Definition: Diagnostics.h:100
clang::clangd::StoreDiags::setLevelAdjuster
void setLevelAdjuster(LevelAdjuster Adjuster)
If set, this allows the client of this class to adjust the level of diagnostics, such as promoting wa...
Definition: Diagnostics.h:157
clang::clangd::DiagBase
Contains basic information about a diagnostic.
Definition: Diagnostics.h:61
clang::clangd::StoreDiags::BeginSourceFile
void BeginSourceFile(const LangOptions &Opts, const Preprocessor *PP) override
Definition: Diagnostics.cpp:612
clang::clangd::StoreDiags::take
std::vector< Diag > take(const clang::tidy::ClangTidyContext *Tidy=nullptr)
Definition: Diagnostics.cpp:558
clang::clangd::DiagBase::File
std::string File
Definition: Diagnostics.h:65
clang::clangd::DiagBase::Range
clangd::Range Range
Definition: Diagnostics.h:69
clang::clangd::isBuiltinDiagnosticSuppressed
bool isBuiltinDiagnosticSuppressed(unsigned ID, const llvm::StringSet<> &Suppress)
Determine whether a (non-clang-tidy) diagnostic is suppressed by config.
Definition: Diagnostics.cpp:865