blob: 25bc6ed664be9c44708a387d3fb2c05500638ad6 [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 Kornienkob3d331d2014-08-06 11:49:10 +000068/// \brief Read-only set of strings represented as a list of positive and
69/// negative globs. Positive globs add all matched strings to the set, negative
70/// globs remove them in the order of appearance in the list.
71class GlobList {
Alexander Kornienko09952d22014-03-20 09:38:22 +000072public:
Alexander Kornienkoa4695222014-06-05 13:31:45 +000073 /// \brief \p GlobList is a comma-separated list of globs (only '*'
74 /// metacharacter is supported) with optional '-' prefix to denote exclusion.
Alexander Kornienkob3d331d2014-08-06 11:49:10 +000075 GlobList(StringRef Globs);
Alexander Kornienkoa4695222014-06-05 13:31:45 +000076
Alexander Kornienkob3d331d2014-08-06 11:49:10 +000077 /// \brief Returns \c true if the pattern matches \p S. The result is the last
78 /// matching glob's Positive flag.
79 bool contains(StringRef S) { return contains(S, false); }
Alexander Kornienko09952d22014-03-20 09:38:22 +000080
81private:
Alexander Kornienkob3d331d2014-08-06 11:49:10 +000082 bool contains(StringRef S, bool Contains);
Alexander Kornienko23fe9592014-05-15 14:27:36 +000083
84 bool Positive;
85 llvm::Regex Regex;
Alexander Kornienkob3d331d2014-08-06 11:49:10 +000086 std::unique_ptr<GlobList> NextGlob;
Alexander Kornienko09952d22014-03-20 09:38:22 +000087};
88
Alexander Kornienkoa4695222014-06-05 13:31:45 +000089/// \brief Contains displayed and ignored diagnostic counters for a ClangTidy
90/// run.
Alexander Kornienko5d174542014-05-07 09:06:53 +000091struct ClangTidyStats {
92 ClangTidyStats()
93 : ErrorsDisplayed(0), ErrorsIgnoredCheckFilter(0), ErrorsIgnoredNOLINT(0),
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000094 ErrorsIgnoredNonUserCode(0), ErrorsIgnoredLineFilter(0) {}
Alexander Kornienko5d174542014-05-07 09:06:53 +000095
96 unsigned ErrorsDisplayed;
97 unsigned ErrorsIgnoredCheckFilter;
98 unsigned ErrorsIgnoredNOLINT;
99 unsigned ErrorsIgnoredNonUserCode;
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000100 unsigned ErrorsIgnoredLineFilter;
101
102 unsigned errorsIgnored() const {
103 return ErrorsIgnoredNOLINT + ErrorsIgnoredCheckFilter +
104 ErrorsIgnoredNonUserCode + ErrorsIgnoredLineFilter;
105 }
Alexander Kornienko5d174542014-05-07 09:06:53 +0000106};
107
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000108/// \brief Every \c ClangTidyCheck reports errors through a \c DiagnosticEngine
109/// provided by this context.
110///
111/// A \c ClangTidyCheck always has access to the active context to report
112/// warnings like:
113/// \code
114/// Context->Diag(Loc, "Single-argument constructors must be explicit")
115/// << FixItHint::CreateInsertion(Loc, "explicit ");
116/// \endcode
117class ClangTidyContext {
118public:
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000119 /// \brief Initializes \c ClangTidyContext instance.
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000120 ClangTidyContext(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider);
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000121
122 /// \brief Report any errors detected using this method.
123 ///
124 /// This is still under heavy development and will likely change towards using
125 /// tablegen'd diagnostic IDs.
126 /// FIXME: Figure out a way to manage ID spaces.
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +0000127 DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc,
Alexander Kornienko54461eb2014-02-06 14:50:10 +0000128 StringRef Message,
129 DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000130
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000131 /// \brief Sets the \c SourceManager of the used \c DiagnosticsEngine.
132 ///
133 /// This is called from the \c ClangTidyCheck base class.
134 void setSourceManager(SourceManager *SourceMgr);
135
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000136 /// \brief Should be called when starting to process new translation unit.
137 void setCurrentFile(StringRef File);
138
Alexander Kornienkoad216882014-07-14 14:10:03 +0000139 /// \brief Sets ASTContext for the current translation unit.
140 void setASTContext(ASTContext *Context);
141
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +0000142 /// \brief Returns the name of the clang-tidy check which produced this
143 /// diagnostic ID.
144 StringRef getCheckName(unsigned DiagnosticID) const;
145
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000146 /// \brief Returns check filter for the \c CurrentFile.
Alexander Kornienkob3d331d2014-08-06 11:49:10 +0000147 GlobList &getChecksFilter();
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000148
149 /// \brief Returns global options.
150 const ClangTidyGlobalOptions &getGlobalOptions() const;
151
152 /// \brief Returns options for \c CurrentFile.
153 const ClangTidyOptions &getOptions() const;
154
155 /// \brief Returns \c ClangTidyStats containing issued and ignored diagnostic
156 /// counters.
Alexander Kornienko5d174542014-05-07 09:06:53 +0000157 const ClangTidyStats &getStats() const { return Stats; }
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000158
159 /// \brief Returns all collected errors.
Alexander Kornienko826b5ad2014-05-09 12:24:09 +0000160 const std::vector<ClangTidyError> &getErrors() const { return Errors; }
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000161
162 /// \brief Clears collected errors.
Alexander Kornienkoc0093602014-05-09 15:50:15 +0000163 void clearErrors() { Errors.clear(); }
Alexander Kornienko09952d22014-03-20 09:38:22 +0000164
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000165private:
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000166 // Calls setDiagnosticsEngine() and storeError().
167 friend class ClangTidyDiagnosticConsumer;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000168
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000169 /// \brief Sets the \c DiagnosticsEngine so that Diagnostics can be generated
170 /// correctly.
171 void setDiagnosticsEngine(DiagnosticsEngine *Engine);
172
173 /// \brief Store an \p Error.
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000174 void storeError(const ClangTidyError &Error);
175
Alexander Kornienko826b5ad2014-05-09 12:24:09 +0000176 std::vector<ClangTidyError> Errors;
NAKAMURA Takumi338eee12014-03-20 10:53:03 +0000177 DiagnosticsEngine *DiagEngine;
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000178 std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider;
179
180 std::string CurrentFile;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000181 ClangTidyOptions CurrentOptions;
Alexander Kornienkob3d331d2014-08-06 11:49:10 +0000182 std::unique_ptr<GlobList> CheckFilter;
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000183
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