blob: d1529a93d69f554f575c1463d4a482cfeaf49d71 [file] [log] [blame]
Daniel Jasperd07c8402013-07-29 08:19:24 +00001//===--- 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_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H
11#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H
12
Alexander Kornienko9ff5b6f2014-05-05 14:54:47 +000013#include "ClangTidyOptions.h"
Daniel Jasperd07c8402013-07-29 08:19:24 +000014#include "clang/Basic/Diagnostic.h"
15#include "clang/Basic/SourceManager.h"
16#include "clang/Tooling/Refactoring.h"
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +000017#include "llvm/ADT/DenseMap.h"
Alexander Kornienko09952d22014-03-20 09:38:22 +000018#include "llvm/Support/Regex.h"
Daniel Jasperd07c8402013-07-29 08:19:24 +000019
20namespace clang {
21
Alexander Kornienkoad216882014-07-14 14:10:03 +000022class ASTContext;
Daniel Jasperd07c8402013-07-29 08:19:24 +000023class CompilerInstance;
24namespace ast_matchers {
25class MatchFinder;
26}
27namespace tooling {
28class CompilationDatabase;
29}
30
31namespace tidy {
32
Manuel Klimek814f9bd2013-11-14 15:49:44 +000033/// \brief A message from a clang-tidy check.
34///
35/// Note that this is independent of a \c SourceManager.
36struct ClangTidyMessage {
37 ClangTidyMessage(StringRef Message = "");
38 ClangTidyMessage(StringRef Message, const SourceManager &Sources,
39 SourceLocation Loc);
40 std::string Message;
41 std::string FilePath;
42 unsigned FileOffset;
43};
44
45/// \brief A detected error complete with information to display diagnostic and
46/// automatic fix.
47///
48/// This is used as an intermediate format to transport Diagnostics without a
49/// dependency on a SourceManager.
50///
51/// FIXME: Make Diagnostics flexible enough to support this directly.
52struct ClangTidyError {
Alexander Kornienko348cae82014-06-02 20:44:32 +000053 enum Level {
54 Warning = DiagnosticsEngine::Warning,
55 Error = DiagnosticsEngine::Error
56 };
57
58 ClangTidyError(StringRef CheckName, Level DiagLevel);
Manuel Klimek814f9bd2013-11-14 15:49:44 +000059
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +000060 std::string CheckName;
Manuel Klimek814f9bd2013-11-14 15:49:44 +000061 ClangTidyMessage Message;
62 tooling::Replacements Fix;
63 SmallVector<ClangTidyMessage, 1> Notes;
Alexander Kornienko348cae82014-06-02 20:44:32 +000064
65 Level DiagLevel;
Manuel Klimek814f9bd2013-11-14 15:49:44 +000066};
67
Alexander Kornienko09952d22014-03-20 09:38:22 +000068/// \brief Filters checks by name.
69class ChecksFilter {
70public:
Alexander Kornienkoa4695222014-06-05 13:31:45 +000071 /// \brief \p GlobList is a comma-separated list of globs (only '*'
72 /// metacharacter is supported) with optional '-' prefix to denote exclusion.
Alexander Kornienko23fe9592014-05-15 14:27:36 +000073 ChecksFilter(StringRef GlobList);
Alexander Kornienkoa4695222014-06-05 13:31:45 +000074
75 /// \brief Returns \c true if the check with the specified \p Name should be
76 /// enabled. The result is the last matching glob's Positive flag. If \p Name
77 /// is not matched by any globs, the check is not enabled.
Alexander Kornienko23fe9592014-05-15 14:27:36 +000078 bool isCheckEnabled(StringRef Name) { return isCheckEnabled(Name, false); }
Alexander Kornienko09952d22014-03-20 09:38:22 +000079
80private:
Alexander Kornienko23fe9592014-05-15 14:27:36 +000081 bool isCheckEnabled(StringRef Name, bool Enabled);
82
83 bool Positive;
84 llvm::Regex Regex;
85 std::unique_ptr<ChecksFilter> NextFilter;
Alexander Kornienko09952d22014-03-20 09:38:22 +000086};
87
Alexander Kornienkoa4695222014-06-05 13:31:45 +000088/// \brief Contains displayed and ignored diagnostic counters for a ClangTidy
89/// run.
Alexander Kornienko5d174542014-05-07 09:06:53 +000090struct ClangTidyStats {
91 ClangTidyStats()
92 : ErrorsDisplayed(0), ErrorsIgnoredCheckFilter(0), ErrorsIgnoredNOLINT(0),
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000093 ErrorsIgnoredNonUserCode(0), ErrorsIgnoredLineFilter(0) {}
Alexander Kornienko5d174542014-05-07 09:06:53 +000094
95 unsigned ErrorsDisplayed;
96 unsigned ErrorsIgnoredCheckFilter;
97 unsigned ErrorsIgnoredNOLINT;
98 unsigned ErrorsIgnoredNonUserCode;
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000099 unsigned ErrorsIgnoredLineFilter;
100
101 unsigned errorsIgnored() const {
102 return ErrorsIgnoredNOLINT + ErrorsIgnoredCheckFilter +
103 ErrorsIgnoredNonUserCode + ErrorsIgnoredLineFilter;
104 }
Alexander Kornienko5d174542014-05-07 09:06:53 +0000105};
106
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000107/// \brief Every \c ClangTidyCheck reports errors through a \c DiagnosticEngine
108/// provided by this context.
109///
110/// A \c ClangTidyCheck always has access to the active context to report
111/// warnings like:
112/// \code
113/// Context->Diag(Loc, "Single-argument constructors must be explicit")
114/// << FixItHint::CreateInsertion(Loc, "explicit ");
115/// \endcode
116class ClangTidyContext {
117public:
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000118 /// \brief Initializes \c ClangTidyContext instance.
119 ///
120 /// Takes ownership of the \c OptionsProvider.
121 ClangTidyContext(ClangTidyOptionsProvider *OptionsProvider);
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000122
123 /// \brief Report any errors detected using this method.
124 ///
125 /// This is still under heavy development and will likely change towards using
126 /// tablegen'd diagnostic IDs.
127 /// FIXME: Figure out a way to manage ID spaces.
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +0000128 DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc,
Alexander Kornienko54461eb2014-02-06 14:50:10 +0000129 StringRef Message,
130 DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000131
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000132 /// \brief Sets the \c SourceManager of the used \c DiagnosticsEngine.
133 ///
134 /// This is called from the \c ClangTidyCheck base class.
135 void setSourceManager(SourceManager *SourceMgr);
136
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000137 /// \brief Should be called when starting to process new translation unit.
138 void setCurrentFile(StringRef File);
139
Alexander Kornienkoad216882014-07-14 14:10:03 +0000140 /// \brief Sets ASTContext for the current translation unit.
141 void setASTContext(ASTContext *Context);
142
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +0000143 /// \brief Returns the name of the clang-tidy check which produced this
144 /// diagnostic ID.
145 StringRef getCheckName(unsigned DiagnosticID) const;
146
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000147 /// \brief Returns check filter for the \c CurrentFile.
148 ChecksFilter &getChecksFilter();
149
150 /// \brief Returns global options.
151 const ClangTidyGlobalOptions &getGlobalOptions() const;
152
153 /// \brief Returns options for \c CurrentFile.
154 const ClangTidyOptions &getOptions() const;
155
156 /// \brief Returns \c ClangTidyStats containing issued and ignored diagnostic
157 /// counters.
Alexander Kornienko5d174542014-05-07 09:06:53 +0000158 const ClangTidyStats &getStats() const { return Stats; }
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000159
160 /// \brief Returns all collected errors.
Alexander Kornienko826b5ad2014-05-09 12:24:09 +0000161 const std::vector<ClangTidyError> &getErrors() const { return Errors; }
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000162
163 /// \brief Clears collected errors.
Alexander Kornienkoc0093602014-05-09 15:50:15 +0000164 void clearErrors() { Errors.clear(); }
Alexander Kornienko09952d22014-03-20 09:38:22 +0000165
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000166private:
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000167 // Calls setDiagnosticsEngine() and storeError().
168 friend class ClangTidyDiagnosticConsumer;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000169
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000170 /// \brief Sets the \c DiagnosticsEngine so that Diagnostics can be generated
171 /// correctly.
172 void setDiagnosticsEngine(DiagnosticsEngine *Engine);
173
174 /// \brief Store an \p Error.
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000175 void storeError(const ClangTidyError &Error);
176
Alexander Kornienko826b5ad2014-05-09 12:24:09 +0000177 std::vector<ClangTidyError> Errors;
NAKAMURA Takumi338eee12014-03-20 10:53:03 +0000178 DiagnosticsEngine *DiagEngine;
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000179 std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider;
180
181 std::string CurrentFile;
182 std::unique_ptr<ChecksFilter> CheckFilter;
183
Alexander Kornienko5d174542014-05-07 09:06:53 +0000184 ClangTidyStats Stats;
Alexander Kornienko09952d22014-03-20 09:38:22 +0000185
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +0000186 llvm::DenseMap<unsigned, std::string> CheckNamesByDiagnosticID;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000187};
188
Daniel Jasperd07c8402013-07-29 08:19:24 +0000189/// \brief A diagnostic consumer that turns each \c Diagnostic into a
190/// \c SourceManager-independent \c ClangTidyError.
191//
192// FIXME: If we move away from unit-tests, this can be moved to a private
193// implementation file.
194class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
195public:
Alexander Kornienko0ba86b72014-01-09 16:31:25 +0000196 ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000197
198 // FIXME: The concept of converting between FixItHints and Replacements is
199 // more generic and should be pulled out into a more useful Diagnostics
200 // library.
Alexander Kornienkocb9272f2014-02-27 13:14:51 +0000201 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
Craig Toppera3dbe842014-03-02 10:20:11 +0000202 const Diagnostic &Info) override;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000203
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000204 /// \brief Sets \c HeaderFilter to the value configured for this file.
205 void BeginSourceFile(const LangOptions &LangOpts,
206 const Preprocessor *PP) override;
207
208 /// \brief Flushes the internal diagnostics buffer to the ClangTidyContext.
Craig Toppera3dbe842014-03-02 10:20:11 +0000209 void finish() override;
Daniel Jasperd07c8402013-07-29 08:19:24 +0000210
211private:
Alexander Kornienko54461eb2014-02-06 14:50:10 +0000212 void finalizeLastError();
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000213
214 /// \brief Updates \c LastErrorRelatesToUserCode and LastErrorPassesLineFilter
215 /// according to the diagnostic \p Location.
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000216 void checkFilters(SourceLocation Location);
217 bool passesLineFilter(StringRef FileName, unsigned LineNumber) const;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000218
Daniel Jasperd07c8402013-07-29 08:19:24 +0000219 ClangTidyContext &Context;
Ahmed Charles6a2dc5c2014-03-09 09:24:40 +0000220 std::unique_ptr<DiagnosticsEngine> Diags;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000221 SmallVector<ClangTidyError, 8> Errors;
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000222 std::unique_ptr<llvm::Regex> HeaderFilter;
Alexander Kornienko54461eb2014-02-06 14:50:10 +0000223 bool LastErrorRelatesToUserCode;
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000224 bool LastErrorPassesLineFilter;
Daniel Jasperd07c8402013-07-29 08:19:24 +0000225};
226
227} // end namespace tidy
228} // end namespace clang
229
230#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H