blob: 35b1736b66810efd86c5fc3564ed9b023df76f0d [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"
Samuel Benzaquenaedd9942014-10-23 17:23:20 +000018#include "llvm/ADT/StringMap.h"
Alexander Kornienko09952d22014-03-20 09:38:22 +000019#include "llvm/Support/Regex.h"
Samuel Benzaquenaedd9942014-10-23 17:23:20 +000020#include "llvm/Support/Timer.h"
Daniel Jasperd07c8402013-07-29 08:19:24 +000021
22namespace clang {
23
Alexander Kornienkoad216882014-07-14 14:10:03 +000024class ASTContext;
Daniel Jasperd07c8402013-07-29 08:19:24 +000025class CompilerInstance;
26namespace ast_matchers {
27class MatchFinder;
28}
29namespace tooling {
30class CompilationDatabase;
31}
32
33namespace tidy {
34
Manuel Klimek814f9bd2013-11-14 15:49:44 +000035/// \brief A message from a clang-tidy check.
36///
37/// Note that this is independent of a \c SourceManager.
38struct 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.
54struct ClangTidyError {
Alexander Kornienko348cae82014-06-02 20:44:32 +000055 enum Level {
56 Warning = DiagnosticsEngine::Warning,
57 Error = DiagnosticsEngine::Error
58 };
59
60 ClangTidyError(StringRef CheckName, Level DiagLevel);
Manuel Klimek814f9bd2013-11-14 15:49:44 +000061
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +000062 std::string CheckName;
Manuel Klimek814f9bd2013-11-14 15:49:44 +000063 ClangTidyMessage Message;
64 tooling::Replacements Fix;
65 SmallVector<ClangTidyMessage, 1> Notes;
Alexander Kornienko348cae82014-06-02 20:44:32 +000066
67 Level DiagLevel;
Manuel Klimek814f9bd2013-11-14 15:49:44 +000068};
69
Alexander Kornienkob3d331d2014-08-06 11:49:10 +000070/// \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.
73class GlobList {
Alexander Kornienko09952d22014-03-20 09:38:22 +000074public:
Alexander Kornienkoa4695222014-06-05 13:31:45 +000075 /// \brief \p GlobList is a comma-separated list of globs (only '*'
76 /// metacharacter is supported) with optional '-' prefix to denote exclusion.
Alexander Kornienkob3d331d2014-08-06 11:49:10 +000077 GlobList(StringRef Globs);
Alexander Kornienkoa4695222014-06-05 13:31:45 +000078
Alexander Kornienkob3d331d2014-08-06 11:49:10 +000079 /// \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); }
Alexander Kornienko09952d22014-03-20 09:38:22 +000082
83private:
Alexander Kornienkob3d331d2014-08-06 11:49:10 +000084 bool contains(StringRef S, bool Contains);
Alexander Kornienko23fe9592014-05-15 14:27:36 +000085
86 bool Positive;
87 llvm::Regex Regex;
Alexander Kornienkob3d331d2014-08-06 11:49:10 +000088 std::unique_ptr<GlobList> NextGlob;
Alexander Kornienko09952d22014-03-20 09:38:22 +000089};
90
Alexander Kornienkoa4695222014-06-05 13:31:45 +000091/// \brief Contains displayed and ignored diagnostic counters for a ClangTidy
92/// run.
Alexander Kornienko5d174542014-05-07 09:06:53 +000093struct ClangTidyStats {
94 ClangTidyStats()
95 : ErrorsDisplayed(0), ErrorsIgnoredCheckFilter(0), ErrorsIgnoredNOLINT(0),
Alexander Kornienkodad4acb2014-05-22 16:07:11 +000096 ErrorsIgnoredNonUserCode(0), ErrorsIgnoredLineFilter(0) {}
Alexander Kornienko5d174542014-05-07 09:06:53 +000097
98 unsigned ErrorsDisplayed;
99 unsigned ErrorsIgnoredCheckFilter;
100 unsigned ErrorsIgnoredNOLINT;
101 unsigned ErrorsIgnoredNonUserCode;
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000102 unsigned ErrorsIgnoredLineFilter;
103
104 unsigned errorsIgnored() const {
105 return ErrorsIgnoredNOLINT + ErrorsIgnoredCheckFilter +
106 ErrorsIgnoredNonUserCode + ErrorsIgnoredLineFilter;
107 }
Alexander Kornienko5d174542014-05-07 09:06:53 +0000108};
109
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000110/// \brief Container for clang-tidy profiling data.
111struct ProfileData {
112 llvm::StringMap<llvm::TimeRecord> Records;
113};
114
Alexander Kornienko284c2492014-12-19 15:37:02 +0000115/// \brief Every \c ClangTidyCheck reports errors through a \c DiagnosticsEngine
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000116/// 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
124class ClangTidyContext {
125public:
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000126 /// \brief Initializes \c ClangTidyContext instance.
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000127 ClangTidyContext(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider);
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000128
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.
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +0000134 DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc,
Alexander Kornienko54461eb2014-02-06 14:50:10 +0000135 StringRef Message,
136 DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000137
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000138 /// \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
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000143 /// \brief Should be called when starting to process new translation unit.
144 void setCurrentFile(StringRef File);
145
Alexander Kornienkoad216882014-07-14 14:10:03 +0000146 /// \brief Sets ASTContext for the current translation unit.
147 void setASTContext(ASTContext *Context);
148
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +0000149 /// \brief Returns the name of the clang-tidy check which produced this
150 /// diagnostic ID.
151 StringRef getCheckName(unsigned DiagnosticID) const;
152
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000153 /// \brief Returns check filter for the \c CurrentFile.
Alexander Kornienkob3d331d2014-08-06 11:49:10 +0000154 GlobList &getChecksFilter();
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000155
156 /// \brief Returns global options.
157 const ClangTidyGlobalOptions &getGlobalOptions() const;
158
159 /// \brief Returns options for \c CurrentFile.
160 const ClangTidyOptions &getOptions() const;
161
162 /// \brief Returns \c ClangTidyStats containing issued and ignored diagnostic
163 /// counters.
Alexander Kornienko5d174542014-05-07 09:06:53 +0000164 const ClangTidyStats &getStats() const { return Stats; }
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000165
166 /// \brief Returns all collected errors.
Alexander Kornienko826b5ad2014-05-09 12:24:09 +0000167 const std::vector<ClangTidyError> &getErrors() const { return Errors; }
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000168
169 /// \brief Clears collected errors.
Alexander Kornienkoc0093602014-05-09 15:50:15 +0000170 void clearErrors() { Errors.clear(); }
Alexander Kornienko09952d22014-03-20 09:38:22 +0000171
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000172 /// \brief Set the output struct for profile data.
173 ///
174 /// Setting a non-null pointer here will enable profile collection in
175 /// clang-tidy.
176 void setCheckProfileData(ProfileData* Profile);
177 ProfileData* getCheckProfileData() const { return Profile; }
178
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000179private:
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000180 // Calls setDiagnosticsEngine() and storeError().
181 friend class ClangTidyDiagnosticConsumer;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000182
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000183 /// \brief Sets the \c DiagnosticsEngine so that Diagnostics can be generated
184 /// correctly.
185 void setDiagnosticsEngine(DiagnosticsEngine *Engine);
186
187 /// \brief Store an \p Error.
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000188 void storeError(const ClangTidyError &Error);
189
Alexander Kornienko826b5ad2014-05-09 12:24:09 +0000190 std::vector<ClangTidyError> Errors;
NAKAMURA Takumi338eee12014-03-20 10:53:03 +0000191 DiagnosticsEngine *DiagEngine;
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000192 std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider;
193
194 std::string CurrentFile;
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000195 ClangTidyOptions CurrentOptions;
Alexander Kornienkob3d331d2014-08-06 11:49:10 +0000196 std::unique_ptr<GlobList> CheckFilter;
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000197
Alexander Kornienko5d174542014-05-07 09:06:53 +0000198 ClangTidyStats Stats;
Alexander Kornienko09952d22014-03-20 09:38:22 +0000199
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +0000200 llvm::DenseMap<unsigned, std::string> CheckNamesByDiagnosticID;
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000201
202 ProfileData *Profile;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000203};
204
Daniel Jasperd07c8402013-07-29 08:19:24 +0000205/// \brief A diagnostic consumer that turns each \c Diagnostic into a
206/// \c SourceManager-independent \c ClangTidyError.
207//
208// FIXME: If we move away from unit-tests, this can be moved to a private
209// implementation file.
210class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
211public:
Alexander Kornienko0ba86b72014-01-09 16:31:25 +0000212 ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000213
214 // FIXME: The concept of converting between FixItHints and Replacements is
215 // more generic and should be pulled out into a more useful Diagnostics
216 // library.
Alexander Kornienkocb9272f2014-02-27 13:14:51 +0000217 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
Craig Toppera3dbe842014-03-02 10:20:11 +0000218 const Diagnostic &Info) override;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000219
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000220 /// \brief Sets \c HeaderFilter to the value configured for this file.
221 void BeginSourceFile(const LangOptions &LangOpts,
222 const Preprocessor *PP) override;
223
224 /// \brief Flushes the internal diagnostics buffer to the ClangTidyContext.
Craig Toppera3dbe842014-03-02 10:20:11 +0000225 void finish() override;
Daniel Jasperd07c8402013-07-29 08:19:24 +0000226
227private:
Alexander Kornienko54461eb2014-02-06 14:50:10 +0000228 void finalizeLastError();
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000229
230 /// \brief Updates \c LastErrorRelatesToUserCode and LastErrorPassesLineFilter
231 /// according to the diagnostic \p Location.
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000232 void checkFilters(SourceLocation Location);
233 bool passesLineFilter(StringRef FileName, unsigned LineNumber) const;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000234
Daniel Jasperd07c8402013-07-29 08:19:24 +0000235 ClangTidyContext &Context;
Ahmed Charles6a2dc5c2014-03-09 09:24:40 +0000236 std::unique_ptr<DiagnosticsEngine> Diags;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000237 SmallVector<ClangTidyError, 8> Errors;
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000238 std::unique_ptr<llvm::Regex> HeaderFilter;
Alexander Kornienko54461eb2014-02-06 14:50:10 +0000239 bool LastErrorRelatesToUserCode;
Alexander Kornienkodad4acb2014-05-22 16:07:11 +0000240 bool LastErrorPassesLineFilter;
Daniel Jasperd07c8402013-07-29 08:19:24 +0000241};
242
243} // end namespace tidy
244} // end namespace clang
245
246#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H