clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

ClangTidyDiagnosticConsumer.h
Go to the documentation of this file.
1 //===--- ClangTidyDiagnosticConsumer.h - clang-tidy -------------*- 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_CLANG_TIDY_CLANGTIDYDIAGNOSTICCONSUMER_H
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYDIAGNOSTICCONSUMER_H
12
13 #include "ClangTidyOptions.h"
14 #include "clang/Basic/Diagnostic.h"
15 #include "clang/Basic/SourceManager.h"
16 #include "clang/Tooling/Refactoring.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/Support/Regex.h"
20 #include "llvm/Support/Timer.h"
21
22 namespace clang {
23
24 class ASTContext;
25 class CompilerInstance;
26 namespace ast_matchers {
27 class MatchFinder;
28 }
29 namespace tooling {
30 class CompilationDatabase;
31 }
32
33 namespace tidy {
34
35 /// \brief A message from a clang-tidy check.
36 ///
37 /// Note that this is independent of a \c SourceManager.
38 struct ClangTidyMessage {
39 ClangTidyMessage(StringRef Message = "");
40 ClangTidyMessage(StringRef Message, const SourceManager &Sources,
41 SourceLocation Loc);
42 std::string Message;
43 std::string FilePath;
44 unsigned FileOffset;
45 };
46
47 /// \brief A detected error complete with information to display diagnostic and
48 /// automatic fix.
49 ///
50 /// This is used as an intermediate format to transport Diagnostics without a
51 /// dependency on a SourceManager.
52 ///
53 /// FIXME: Make Diagnostics flexible enough to support this directly.
54 struct ClangTidyError {
55 enum Level {
56 Warning = DiagnosticsEngine::Warning,
57 Error = DiagnosticsEngine::Error
58 };
59
60 ClangTidyError(StringRef CheckName, Level DiagLevel);
61
62 std::string CheckName;
63 ClangTidyMessage Message;
64 tooling::Replacements Fix;
65 SmallVector<ClangTidyMessage, 1> Notes;
66
67 Level DiagLevel;
68 };
69
70 /// \brief Read-only set of strings represented as a list of positive and
71 /// negative globs. Positive globs add all matched strings to the set, negative
72 /// globs remove them in the order of appearance in the list.
73 class GlobList {
74 public:
75 /// \brief \p GlobList is a comma-separated list of globs (only '*'
76 /// metacharacter is supported) with optional '-' prefix to denote exclusion.
77 GlobList(StringRef Globs);
78
79 /// \brief Returns \c true if the pattern matches \p S. The result is the last
80 /// matching glob's Positive flag.
81 bool contains(StringRef S) { return contains(S, false); }
82
83 private:
84 bool contains(StringRef S, bool Contains);
85
86 bool Positive;
87 llvm::Regex Regex;
88 std::unique_ptr<GlobList> NextGlob;
89 };
90
91 /// \brief Contains displayed and ignored diagnostic counters for a ClangTidy
92 /// run.
93 struct ClangTidyStats {
94 ClangTidyStats()
95 : ErrorsDisplayed(0), ErrorsIgnoredCheckFilter(0), ErrorsIgnoredNOLINT(0),
96 ErrorsIgnoredNonUserCode(0), ErrorsIgnoredLineFilter(0) {}
97
98 unsigned ErrorsDisplayed;
99 unsigned ErrorsIgnoredCheckFilter;
100 unsigned ErrorsIgnoredNOLINT;
101 unsigned ErrorsIgnoredNonUserCode;
102 unsigned ErrorsIgnoredLineFilter;
103
104 unsigned errorsIgnored() const {
105 return ErrorsIgnoredNOLINT + ErrorsIgnoredCheckFilter +
106 ErrorsIgnoredNonUserCode + ErrorsIgnoredLineFilter;
107 }
108 };
109
110 /// \brief Container for clang-tidy profiling data.
111 struct ProfileData {
112 llvm::StringMap<llvm::TimeRecord> Records;
113 };
114
115 /// \brief Every \c ClangTidyCheck reports errors through a \c DiagnosticsEngine
116 /// provided by this context.
117 ///
118 /// A \c ClangTidyCheck always has access to the active context to report
119 /// warnings like:
120 /// \code
121 /// Context->Diag(Loc, "Single-argument constructors must be explicit")
122 /// << FixItHint::CreateInsertion(Loc, "explicit ");
123 /// \endcode
124 class ClangTidyContext {
125 public:
126 /// \brief Initializes \c ClangTidyContext instance.
127 ClangTidyContext(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider);
128
129 /// \brief Report any errors detected using this method.
130 ///
131 /// This is still under heavy development and will likely change towards using
132 /// tablegen'd diagnostic IDs.
133 /// FIXME: Figure out a way to manage ID spaces.
134 DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc,
135 StringRef Message,
136 DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
137
138 /// \brief Sets the \c SourceManager of the used \c DiagnosticsEngine.
139 ///
140 /// This is called from the \c ClangTidyCheck base class.
141 void setSourceManager(SourceManager *SourceMgr);
142
143 /// \brief Should be called when starting to process new translation unit.
144 void setCurrentFile(StringRef File);
145
146 /// \brief Returns the main file name of the current translation unit.
147 StringRef getCurrentFile() const { return CurrentFile; }
148
149 /// \brief Sets ASTContext for the current translation unit.
150 void setASTContext(ASTContext *Context);
151
152 /// \brief Gets the language options from the AST context.
153 LangOptions getLangOpts() const { return LangOpts; }
154
155 /// \brief Returns the name of the clang-tidy check which produced this
156 /// diagnostic ID.
157 StringRef getCheckName(unsigned DiagnosticID) const;
158
159 /// \brief Returns check filter for the \c CurrentFile.
160 ///
161 /// The \c CurrentFile can be changed using \c setCurrentFile.
162 GlobList &getChecksFilter();
163
164 /// \brief Returns true if the check name is enabled for the \c CurrentFile.
165 bool isCheckEnabled(StringRef CheckName) const;
166
167 /// \brief Returns global options.
168 const ClangTidyGlobalOptions &getGlobalOptions() const;
169
170 /// \brief Returns options for \c CurrentFile.
171 ///
172 /// The \c CurrentFile can be changed using \c setCurrentFile.
173 const ClangTidyOptions &getOptions() const;
174
175 /// \brief Returns options for \c File. Does not change or depend on
176 /// \c CurrentFile.
177 ClangTidyOptions getOptionsForFile(StringRef File) const;
178
179 /// \brief Returns \c ClangTidyStats containing issued and ignored diagnostic
180 /// counters.
181 const ClangTidyStats &getStats() const { return Stats; }
182
183 /// \brief Returns all collected errors.
184 const std::vector<ClangTidyError> &getErrors() const { return Errors; }
185
186 /// \brief Clears collected errors.
187 void clearErrors() { Errors.clear(); }
188
189 /// \brief Set the output struct for profile data.
190 ///
191 /// Setting a non-null pointer here will enable profile collection in
192 /// clang-tidy.
193 void setCheckProfileData(ProfileData *Profile);
194 ProfileData *getCheckProfileData() const { return Profile; }
195
196 private:
197 // Calls setDiagnosticsEngine() and storeError().
198 friend class ClangTidyDiagnosticConsumer;
199
200 /// \brief Sets the \c DiagnosticsEngine so that Diagnostics can be generated
201 /// correctly.
202 void setDiagnosticsEngine(DiagnosticsEngine *Engine);
203
204 /// \brief Store an \p Error.
205 void storeError(const ClangTidyError &Error);
206
207 std::vector<ClangTidyError> Errors;
208 DiagnosticsEngine *DiagEngine;
209 std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider;
210
211 std::string CurrentFile;
212 ClangTidyOptions CurrentOptions;
213 std::unique_ptr<GlobList> CheckFilter;
214
215 LangOptions LangOpts;
216
217 ClangTidyStats Stats;
218
219 llvm::DenseMap<unsigned, std::string> CheckNamesByDiagnosticID;
220
221 ProfileData *Profile;
222 };
223
224 /// \brief A diagnostic consumer that turns each \c Diagnostic into a
225 /// \c SourceManager-independent \c ClangTidyError.
226 //
227 // FIXME: If we move away from unit-tests, this can be moved to a private
228 // implementation file.
229 class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
230 public:
231 ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx);
232
233 // FIXME: The concept of converting between FixItHints and Replacements is
234 // more generic and should be pulled out into a more useful Diagnostics
235 // library.
236 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
237 const Diagnostic &Info) override;
238
239 /// \brief Flushes the internal diagnostics buffer to the ClangTidyContext.
240 void finish() override;
241
242 private:
243 void finalizeLastError();
244
245 void removeIncompatibleErrors(SmallVectorImpl<ClangTidyError> &Errors) const;
246
247 /// \brief Returns the \c HeaderFilter constructed for the options set in the
248 /// context.
249 llvm::Regex *getHeaderFilter();
250
251 /// \brief Updates \c LastErrorRelatesToUserCode and LastErrorPassesLineFilter
252 /// according to the diagnostic \p Location.
253 void checkFilters(SourceLocation Location);
254 bool passesLineFilter(StringRef FileName, unsigned LineNumber) const;
255
256 ClangTidyContext &Context;
257 std::unique_ptr<DiagnosticsEngine> Diags;
258 SmallVector<ClangTidyError, 8> Errors;
259 std::unique_ptr<llvm::Regex> HeaderFilter;
260 bool LastErrorRelatesToUserCode;
261 bool LastErrorPassesLineFilter;
262 };
263
264 } // end namespace tidy
265 } // end namespace clang
266
267 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYDIAGNOSTICCONSUMER_H
DiagnosticConsumer
Loc
SourceLocation Loc
'#' location in the include directive
Definition: IncludeOrderCheck.cpp:34
clang::tidy::ClangTidyStats::ClangTidyStats
ClangTidyStats()
Definition: ClangTidyDiagnosticConsumer.h:94
clang::tidy::ClangTidyMessage::Message
std::string Message
Definition: ClangTidyDiagnosticConsumer.h:42
LangOpts
LangOptions LangOpts
Definition: ClangTidy.cpp:168
clang::tidy::GlobList
Read-only set of strings represented as a list of positive and negative globs.
Definition: ClangTidyDiagnosticConsumer.h:73
clang::tidy::GlobList::GlobList
GlobList(StringRef Globs)
GlobList is a comma-separated list of globs (only '*' metacharacter is supported) with optional '-' p...
Definition: ClangTidyDiagnosticConsumer.cpp:148
File
HeaderHandle File
Definition: PreprocessorTracker.cpp:526
clang::tidy::ClangTidyDiagnosticConsumer::finish
void finish() override
Flushes the internal diagnostics buffer to the ClangTidyContext.
Definition: ClangTidyDiagnosticConsumer.cpp:545
clang::tidy::GlobList::contains
bool contains(StringRef S)
Returns true if the pattern matches S.
Definition: ClangTidyDiagnosticConsumer.h:81
clang::tidy::ClangTidyMessage
A message from a clang-tidy check.
Definition: ClangTidyDiagnosticConsumer.h:38
clang::tidy::ClangTidyError::CheckName
std::string CheckName
Definition: ClangTidyDiagnosticConsumer.h:62
clang::tidy::ClangTidyMessage::ClangTidyMessage
ClangTidyMessage(StringRef Message="")
Definition: ClangTidyDiagnosticConsumer.cpp:105
clang::tidy::ClangTidyOptions
Contains options for clang-tidy.
Definition: ClangTidyOptions.h:49
clang::tidy::ClangTidyStats::ErrorsIgnoredCheckFilter
unsigned ErrorsIgnoredCheckFilter
Definition: ClangTidyDiagnosticConsumer.h:99
clang::tidy::ClangTidyDiagnosticConsumer::ClangTidyDiagnosticConsumer
ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx)
Definition: ClangTidyDiagnosticConsumer.cpp:253
clang::tidy::ClangTidyGlobalOptions
Global options.
Definition: ClangTidyOptions.h:41
clang::tidy::ClangTidyContext::getCheckProfileData
ProfileData * getCheckProfileData() const
Definition: ClangTidyDiagnosticConsumer.h:194
clang::tidy::ProfileData::Records
llvm::StringMap< llvm::TimeRecord > Records
Definition: ClangTidyDiagnosticConsumer.h:112
clang::tidy::ClangTidyStats::ErrorsIgnoredNOLINT
unsigned ErrorsIgnoredNOLINT
Definition: ClangTidyDiagnosticConsumer.h:100
clang::tidy::ClangTidyContext::getCurrentFile
StringRef getCurrentFile() const
Returns the main file name of the current translation unit.
Definition: ClangTidyDiagnosticConsumer.h:147
clang::tidy::ClangTidyContext::getErrors
const std::vector< ClangTidyError > & getErrors() const
Returns all collected errors.
Definition: ClangTidyDiagnosticConsumer.h:184
ClangTidyOptions.h
SourceMgr
SourceManager SourceMgr
Definition: ClangTidy.cpp:172
clang::tidy::ClangTidyError::Notes
SmallVector< ClangTidyMessage, 1 > Notes
Definition: ClangTidyDiagnosticConsumer.h:65
clang::tidy::ClangTidyContext::getOptionsForFile
ClangTidyOptions getOptionsForFile(StringRef File) const
Returns options for File.
Definition: ClangTidyDiagnosticConsumer.cpp:222
clang::tidy::ClangTidyContext::getLangOpts
LangOptions getLangOpts() const
Gets the language options from the AST context.
Definition: ClangTidyDiagnosticConsumer.h:153
clang::tidy::ClangTidyError::Message
ClangTidyMessage Message
Definition: ClangTidyDiagnosticConsumer.h:63
clang::tidy::ClangTidyContext::setCurrentFile
void setCurrentFile(StringRef File)
Should be called when starting to process new translation unit.
Definition: ClangTidyDiagnosticConsumer.cpp:203
clang::tidy::ClangTidyContext::getOptions
const ClangTidyOptions & getOptions() const
Returns options for CurrentFile.
Definition: ClangTidyDiagnosticConsumer.cpp:218
clang::tidy::ClangTidyError::Level
Level
Definition: ClangTidyDiagnosticConsumer.h:55
clang::tidy::ClangTidyContext::diag
DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc, StringRef Message, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Report any errors detected using this method.
Definition: ClangTidyDiagnosticConsumer.cpp:170
clang::tidy::ClangTidyStats::ErrorsIgnoredNonUserCode
unsigned ErrorsIgnoredNonUserCode
Definition: ClangTidyDiagnosticConsumer.h:101
clang::tidy::ClangTidyContext::setCheckProfileData
void setCheckProfileData(ProfileData *Profile)
Set the output struct for profile data.
Definition: ClangTidyDiagnosticConsumer.cpp:229
clang::tidy::ClangTidyContext::ClangTidyContext
ClangTidyContext(std::unique_ptr< ClangTidyOptionsProvider > OptionsProvider)
Initializes ClangTidyContext instance.
Definition: ClangTidyDiagnosticConsumer.cpp:161
clang::tidy::ClangTidyContext::setASTContext
void setASTContext(ASTContext *Context)
Sets ASTContext for the current translation unit.
Definition: ClangTidyDiagnosticConsumer.cpp:209
clang::tidy::ClangTidyDiagnosticConsumer
A diagnostic consumer that turns each Diagnostic into a SourceManager-independent ClangTidyError...
Definition: ClangTidyDiagnosticConsumer.h:229
clang::tidy::ClangTidyError::DiagLevel
Level DiagLevel
Definition: ClangTidyDiagnosticConsumer.h:67
clang::tidy::ClangTidyStats::ErrorsIgnoredLineFilter
unsigned ErrorsIgnoredLineFilter
Definition: ClangTidyDiagnosticConsumer.h:102
clang::tidy::ClangTidyError::ClangTidyError
ClangTidyError(StringRef CheckName, Level DiagLevel)
Definition: ClangTidyDiagnosticConsumer.cpp:117
clang::tidy::ClangTidyContext::getGlobalOptions
const ClangTidyGlobalOptions & getGlobalOptions() const
Returns global options.
Definition: ClangTidyDiagnosticConsumer.cpp:214
clang::tidy::ClangTidyContext::setSourceManager
void setSourceManager(SourceManager *SourceMgr)
Sets the SourceManager of the used DiagnosticsEngine.
Definition: ClangTidyDiagnosticConsumer.cpp:199
clang::tidy::ClangTidyError::Error
Definition: ClangTidyDiagnosticConsumer.h:57
clang::tidy::ClangTidyContext::clearErrors
void clearErrors()
Clears collected errors.
Definition: ClangTidyDiagnosticConsumer.h:187
clang::tidy::ClangTidyContext::getStats
const ClangTidyStats & getStats() const
Returns ClangTidyStats containing issued and ignored diagnostic counters.
Definition: ClangTidyDiagnosticConsumer.h:181
clang::tidy::ClangTidyStats::errorsIgnored
unsigned errorsIgnored() const
Definition: ClangTidyDiagnosticConsumer.h:104
clang::tidy::ClangTidyContext::getCheckName
StringRef getCheckName(unsigned DiagnosticID) const
Returns the name of the clang-tidy check which produced this diagnostic ID.
Definition: ClangTidyDiagnosticConsumer.cpp:245
clang::tidy::ClangTidyError
A detected error complete with information to display diagnostic and automatic fix.
Definition: ClangTidyDiagnosticConsumer.h:54
clang::tidy::ClangTidyStats
Contains displayed and ignored diagnostic counters for a ClangTidy run.
Definition: ClangTidyDiagnosticConsumer.h:93
Context
ClangTidyContext & Context
Definition: ClangTidy.cpp:93
clang::tidy::ClangTidyStats::ErrorsDisplayed
unsigned ErrorsDisplayed
Definition: ClangTidyDiagnosticConsumer.h:98
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:124
clang::tidy::ClangTidyError::Warning
Definition: ClangTidyDiagnosticConsumer.h:56
clang::tidy::ClangTidyError::Fix
tooling::Replacements Fix
Definition: ClangTidyDiagnosticConsumer.h:64
clang::tidy::ClangTidyDiagnosticConsumer::HandleDiagnostic
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) override
Definition: ClangTidyDiagnosticConsumer.cpp:284
clang::tidy::ClangTidyContext::isCheckEnabled
bool isCheckEnabled(StringRef CheckName) const
Returns true if the check name is enabled for the CurrentFile.
Definition: ClangTidyDiagnosticConsumer.cpp:236
clang::tidy::ClangTidyContext::getChecksFilter
GlobList & getChecksFilter()
Returns check filter for the CurrentFile.
Definition: ClangTidyDiagnosticConsumer.cpp:231
clang::tidy::ProfileData
Container for clang-tidy profiling data.
Definition: ClangTidyDiagnosticConsumer.h:111
clang::tidy::ClangTidyMessage::FileOffset
unsigned FileOffset
Definition: ClangTidyDiagnosticConsumer.h:44
clang::tidy::ClangTidyMessage::FilePath
std::string FilePath
Definition: ClangTidyDiagnosticConsumer.h:43
Location
Definition: Modularize.cpp:386