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