clang-tools  12.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/StringSet.h"
23 #include <cassert>
24 #include <string>
25 
26 namespace clang {
27 namespace tidy {
28 class ClangTidyContext;
29 } // namespace tidy
30 namespace clangd {
31 
33  /// If true, Clangd uses an LSP extension to embed the fixes with the
34  /// diagnostics that are sent to the client.
35  bool EmbedFixesInDiagnostics = false;
36 
37  /// If true, Clangd uses the relatedInformation field to include other
38  /// locations (in particular attached notes).
39  /// Otherwise, these are flattened into the diagnostic message.
40  bool EmitRelatedLocations = false;
41 
42  /// If true, Clangd uses an LSP extension to send the diagnostic's
43  /// category to the client. The category typically describes the compilation
44  /// stage during which the issue was produced, e.g. "Semantic Issue" or "Parse
45  /// Issue".
46  bool SendDiagnosticCategory = false;
47 
48  /// If true, Clangd will add a number of available fixes to the diagnostic's
49  /// message.
50  bool DisplayFixesCount = true;
51 };
52 
53 /// Contains basic information about a diagnostic.
54 struct DiagBase {
55  std::string Message;
56  // Intended to be used only in error messages.
57  // May be relative, absolute or even artificially constructed.
58  std::string File;
59  // Absolute path to containing file, if available.
60  llvm::Optional<std::string> AbsFile;
61 
63  DiagnosticsEngine::Level Severity = DiagnosticsEngine::Note;
64  std::string Category;
65  // Since File is only descriptive, we store a separate flag to distinguish
66  // diags from the main file.
67  bool InsideMainFile = false;
68  unsigned ID; // e.g. member of clang::diag, or clang-tidy assigned ID.
69 };
70 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const DiagBase &D);
71 
72 /// Represents a single fix-it that editor can apply to fix the error.
73 struct Fix {
74  /// Message for the fix-it.
75  std::string Message;
76  /// TextEdits from clang's fix-its. Must be non-empty.
77  llvm::SmallVector<TextEdit, 1> Edits;
78 };
79 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Fix &F);
80 
81 /// Represents a note for the diagnostic. Severity of notes can only be 'note'
82 /// or 'remark'.
83 struct Note : DiagBase {};
84 
85 /// A top-level diagnostic that may have Notes and Fixes.
86 struct Diag : DiagBase {
87  std::string Name; // if ID was recognized.
88  // The source of this diagnostic.
89  enum {
93  } Source = Unknown;
94  /// Elaborate on the problem, usually pointing to a related piece of code.
95  std::vector<Note> Notes;
96  /// *Alternative* fixes for this diagnostic, one should be chosen.
97  std::vector<Fix> Fixes;
98 };
99 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Diag &D);
100 
101 /// Conversion to LSP diagnostics. Formats the error message of each diagnostic
102 /// to include all its notes. Notes inside main file are also provided as
103 /// separate diagnostics with their corresponding fixits. Notes outside main
104 /// file do not have a corresponding LSP diagnostic, but can still be included
105 /// as part of their main diagnostic's message.
106 void toLSPDiags(
107  const Diag &D, const URIForFile &File, const ClangdDiagnosticOptions &Opts,
108  llvm::function_ref<void(clangd::Diagnostic, llvm::ArrayRef<Fix>)> OutFn);
109 
110 /// Convert from Fix to LSP CodeAction.
111 CodeAction toCodeAction(const Fix &D, const URIForFile &File);
112 
113 /// Convert from clang diagnostic level to LSP severity.
114 int getSeverity(DiagnosticsEngine::Level L);
115 
116 /// StoreDiags collects the diagnostics that can later be reported by
117 /// clangd. It groups all notes for a diagnostic into a single Diag
118 /// and filters out diagnostics that don't mention the main file (i.e. neither
119 /// the diag itself nor its notes are in the main file).
121 public:
122  // The ClangTidyContext populates Source and Name for clang-tidy diagnostics.
123  std::vector<Diag> take(const clang::tidy::ClangTidyContext *Tidy = nullptr);
124 
125  void BeginSourceFile(const LangOptions &Opts, const Preprocessor *) override;
126  void EndSourceFile() override;
127  void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
128  const clang::Diagnostic &Info) override;
129 
130  using DiagFixer = std::function<std::vector<Fix>(DiagnosticsEngine::Level,
131  const clang::Diagnostic &)>;
132  using LevelAdjuster = std::function<DiagnosticsEngine::Level(
133  DiagnosticsEngine::Level, const clang::Diagnostic &)>;
134  /// If set, possibly adds fixes for diagnostics using \p Fixer.
135  void contributeFixes(DiagFixer Fixer) { this->Fixer = Fixer; }
136  /// If set, this allows the client of this class to adjust the level of
137  /// diagnostics, such as promoting warnings to errors, or ignoring
138  /// diagnostics.
139  void setLevelAdjuster(LevelAdjuster Adjuster) { this->Adjuster = Adjuster; }
140 
141 private:
142  void flushLastDiag();
143 
144  DiagFixer Fixer = nullptr;
145  LevelAdjuster Adjuster = nullptr;
146  std::vector<Diag> Output;
147  llvm::Optional<LangOptions> LangOpts;
148  llvm::Optional<Diag> LastDiag;
149  llvm::Optional<FullSourceLoc> LastDiagLoc; // Valid only when LastDiag is set.
150  bool LastDiagOriginallyError = false; // Valid only when LastDiag is set.
151 
152  llvm::DenseSet<std::pair<unsigned, unsigned>> IncludedErrorLocations;
153  bool LastPrimaryDiagnosticWasSuppressed = false;
154 };
155 
156 } // namespace clangd
157 } // namespace clang
158 
159 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_DIAGNOSTICS_H
StoreDiags collects the diagnostics that can later be reported by clangd.
Definition: Diagnostics.h:120
Contains basic information about a diagnostic.
Definition: Diagnostics.h:54
CodeAction toCodeAction(const Fix &F, const URIForFile &File)
Convert from Fix to LSP CodeAction.
void contributeFixes(DiagFixer Fixer)
If set, possibly adds fixes for diagnostics using Fixer.
Definition: Diagnostics.h:135
A code action represents a change that can be performed in code, e.g.
Definition: Protocol.h:918
std::function< DiagnosticsEngine::Level(DiagnosticsEngine::Level, const clang::Diagnostic &)> LevelAdjuster
Definition: Diagnostics.h:133
std::string Name
Definition: Diagnostics.h:87
std::string Output
Definition: TraceTests.cpp:161
A top-level diagnostic that may have Notes and Fixes.
Definition: Diagnostics.h:86
std::string Message
Message for the fix-it.
Definition: Diagnostics.h:75
std::vector< Fix > Fixes
Alternative fixes for this diagnostic, one should be chosen.
Definition: Diagnostics.h:97
llvm::SmallVector< TextEdit, 1 > Edits
TextEdits from clang&#39;s fix-its. Must be non-empty.
Definition: Diagnostics.h:77
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.
int getSeverity(DiagnosticsEngine::Level L)
Convert from clang diagnostic level to LSP severity.
DiagnosticCallback Diagnostic
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:162
clangd::Range Range
Definition: Diagnostics.h:62
Represents a single fix-it that editor can apply to fix the error.
Definition: Diagnostics.h:73
FunctionInfo Info
std::vector< Note > Notes
Elaborate on the problem, usually pointing to a related piece of code.
Definition: Diagnostics.h:95
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Represents a note for the diagnostic.
Definition: Diagnostics.h:83
std::function< std::vector< Fix >(DiagnosticsEngine::Level, const clang::Diagnostic &)> DiagFixer
Definition: Diagnostics.h:131
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const CodeCompletion &C)
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:139
llvm::Optional< std::string > AbsFile
Definition: Diagnostics.h:60