blob: d944836c1b1d95d664debbb738daa1278d233275 [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
22class CompilerInstance;
23namespace ast_matchers {
24class MatchFinder;
25}
26namespace tooling {
27class CompilationDatabase;
28}
29
30namespace tidy {
31
Manuel Klimek814f9bd2013-11-14 15:49:44 +000032/// \brief A message from a clang-tidy check.
33///
34/// Note that this is independent of a \c SourceManager.
35struct ClangTidyMessage {
36 ClangTidyMessage(StringRef Message = "");
37 ClangTidyMessage(StringRef Message, const SourceManager &Sources,
38 SourceLocation Loc);
39 std::string Message;
40 std::string FilePath;
41 unsigned FileOffset;
42};
43
44/// \brief A detected error complete with information to display diagnostic and
45/// automatic fix.
46///
47/// This is used as an intermediate format to transport Diagnostics without a
48/// dependency on a SourceManager.
49///
50/// FIXME: Make Diagnostics flexible enough to support this directly.
51struct ClangTidyError {
Alexander Kornienko6fbc6192014-03-10 13:11:17 +000052 ClangTidyError(StringRef CheckName);
Manuel Klimek814f9bd2013-11-14 15:49:44 +000053
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +000054 std::string CheckName;
Manuel Klimek814f9bd2013-11-14 15:49:44 +000055 ClangTidyMessage Message;
56 tooling::Replacements Fix;
57 SmallVector<ClangTidyMessage, 1> Notes;
58};
59
Alexander Kornienko09952d22014-03-20 09:38:22 +000060/// \brief Filters checks by name.
61class ChecksFilter {
62public:
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +000063 ChecksFilter(const ClangTidyOptions& Options);
Alexander Kornienko09952d22014-03-20 09:38:22 +000064 bool isCheckEnabled(StringRef Name);
65
66private:
67 llvm::Regex EnableChecks;
68 llvm::Regex DisableChecks;
69};
70
Manuel Klimek814f9bd2013-11-14 15:49:44 +000071/// \brief Every \c ClangTidyCheck reports errors through a \c DiagnosticEngine
72/// provided by this context.
73///
74/// A \c ClangTidyCheck always has access to the active context to report
75/// warnings like:
76/// \code
77/// Context->Diag(Loc, "Single-argument constructors must be explicit")
78/// << FixItHint::CreateInsertion(Loc, "explicit ");
79/// \endcode
80class ClangTidyContext {
81public:
Alexander Kornienko09952d22014-03-20 09:38:22 +000082 ClangTidyContext(SmallVectorImpl<ClangTidyError> *Errors,
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +000083 const ClangTidyOptions &Options);
Manuel Klimek814f9bd2013-11-14 15:49:44 +000084
85 /// \brief Report any errors detected using this method.
86 ///
87 /// This is still under heavy development and will likely change towards using
88 /// tablegen'd diagnostic IDs.
89 /// FIXME: Figure out a way to manage ID spaces.
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +000090 DiagnosticBuilder diag(StringRef CheckName, SourceLocation Loc,
Alexander Kornienko54461eb2014-02-06 14:50:10 +000091 StringRef Message,
92 DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
Manuel Klimek814f9bd2013-11-14 15:49:44 +000093
94 /// \brief Sets the \c DiagnosticsEngine so that Diagnostics can be generated
95 /// correctly.
96 ///
97 /// This is called from the \c ClangTidyCheck base class.
98 void setDiagnosticsEngine(DiagnosticsEngine *Engine);
99
100 /// \brief Sets the \c SourceManager of the used \c DiagnosticsEngine.
101 ///
102 /// This is called from the \c ClangTidyCheck base class.
103 void setSourceManager(SourceManager *SourceMgr);
104
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +0000105 /// \brief Returns the name of the clang-tidy check which produced this
106 /// diagnostic ID.
107 StringRef getCheckName(unsigned DiagnosticID) const;
108
Alexander Kornienko09952d22014-03-20 09:38:22 +0000109 ChecksFilter &getChecksFilter() { return Filter; }
Alexander Kornienko9ff5b6f2014-05-05 14:54:47 +0000110 const ClangTidyOptions &getOptions() const { return Options; }
Alexander Kornienko09952d22014-03-20 09:38:22 +0000111
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000112private:
113 friend class ClangTidyDiagnosticConsumer; // Calls storeError().
114
115 /// \brief Store a \c ClangTidyError.
116 void storeError(const ClangTidyError &Error);
117
118 SmallVectorImpl<ClangTidyError> *Errors;
NAKAMURA Takumi338eee12014-03-20 10:53:03 +0000119 DiagnosticsEngine *DiagEngine;
Alexander Kornienko9ff5b6f2014-05-05 14:54:47 +0000120 ClangTidyOptions Options;
Alexander Kornienko09952d22014-03-20 09:38:22 +0000121 ChecksFilter Filter;
122
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +0000123 llvm::DenseMap<unsigned, std::string> CheckNamesByDiagnosticID;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000124};
125
Daniel Jasperd07c8402013-07-29 08:19:24 +0000126/// \brief A diagnostic consumer that turns each \c Diagnostic into a
127/// \c SourceManager-independent \c ClangTidyError.
128//
129// FIXME: If we move away from unit-tests, this can be moved to a private
130// implementation file.
131class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
132public:
Alexander Kornienko0ba86b72014-01-09 16:31:25 +0000133 ClangTidyDiagnosticConsumer(ClangTidyContext &Ctx);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000134
135 // FIXME: The concept of converting between FixItHints and Replacements is
136 // more generic and should be pulled out into a more useful Diagnostics
137 // library.
Alexander Kornienkocb9272f2014-02-27 13:14:51 +0000138 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
Craig Toppera3dbe842014-03-02 10:20:11 +0000139 const Diagnostic &Info) override;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000140
Alexander Kornienkofc589872014-01-03 15:34:40 +0000141 // Flushes the internal diagnostics buffer to the ClangTidyContext.
Craig Toppera3dbe842014-03-02 10:20:11 +0000142 void finish() override;
Daniel Jasperd07c8402013-07-29 08:19:24 +0000143
144private:
Alexander Kornienko54461eb2014-02-06 14:50:10 +0000145 void finalizeLastError();
Alexander Kornienko9ff5b6f2014-05-05 14:54:47 +0000146 bool relatesToUserCode(SourceLocation Location);
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000147
Daniel Jasperd07c8402013-07-29 08:19:24 +0000148 ClangTidyContext &Context;
Alexander Kornienko9ff5b6f2014-05-05 14:54:47 +0000149 llvm::Regex HeaderFilter;
Ahmed Charles6a2dc5c2014-03-09 09:24:40 +0000150 std::unique_ptr<DiagnosticsEngine> Diags;
Manuel Klimek814f9bd2013-11-14 15:49:44 +0000151 SmallVector<ClangTidyError, 8> Errors;
Alexander Kornienko54461eb2014-02-06 14:50:10 +0000152 bool LastErrorRelatesToUserCode;
Daniel Jasperd07c8402013-07-29 08:19:24 +0000153};
154
155} // end namespace tidy
156} // end namespace clang
157
158#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_DIAGNOSTIC_CONSUMER_H