blob: a2dbcbc540ef54036f314239e5ba41f63931e049 [file] [log] [blame]
Daniel Jasperd07c8402013-07-29 08:19:24 +00001//===--- ClangTidy.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
Alexander Kornienko66580552015-03-09 16:52:33 +000010#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDY_H
11#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDY_H
Daniel Jasperd07c8402013-07-29 08:19:24 +000012
Chandler Carruth85e6e872014-01-07 20:05:01 +000013#include "ClangTidyDiagnosticConsumer.h"
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +000014#include "ClangTidyOptions.h"
Daniel Jasperd07c8402013-07-29 08:19:24 +000015#include "clang/ASTMatchers/ASTMatchFinder.h"
16#include "clang/Basic/Diagnostic.h"
17#include "clang/Basic/SourceManager.h"
18#include "clang/Tooling/Refactoring.h"
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000019#include "llvm/ADT/StringExtras.h"
20#include "llvm/Support/raw_ostream.h"
Alexander Kornienkoa4695222014-06-05 13:31:45 +000021#include <memory>
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000022#include <type_traits>
Alexander Kornienko826b5ad2014-05-09 12:24:09 +000023#include <vector>
Daniel Jasperd07c8402013-07-29 08:19:24 +000024
25namespace clang {
26
27class CompilerInstance;
Daniel Jasperd07c8402013-07-29 08:19:24 +000028namespace tooling {
29class CompilationDatabase;
30}
31
32namespace tidy {
33
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000034/// \brief Provides access to the \c ClangTidyCheck options via check-local
35/// names.
36///
37/// Methods of this class prepend <tt>CheckName + "."</tt> to translate
38/// check-local option names to global option names.
39class OptionsView {
40public:
41 /// \brief Initializes the instance using \p CheckName + "." as a prefix.
42 OptionsView(StringRef CheckName,
43 const ClangTidyOptions::OptionMap &CheckOptions);
44
45 /// \brief Read a named option from the \c Context.
46 ///
47 /// Reads the option with the check-local name \p LocalName from the
48 /// \c CheckOptions. If the corresponding key is not present, returns
49 /// \p Default.
50 std::string get(StringRef LocalName, std::string Default) const;
51
52 /// \brief Read a named option from the \c Context and parse it as an integral
53 /// type \c T.
54 ///
55 /// Reads the option with the check-local name \p LocalName from the
56 /// \c CheckOptions. If the corresponding key is not present, returns
57 /// \p Default.
58 template <typename T>
59 typename std::enable_if<std::is_integral<T>::value, T>::type
60 get(StringRef LocalName, T Default) const {
61 std::string Value = get(LocalName, "");
62 T Result = Default;
63 if (!Value.empty())
64 StringRef(Value).getAsInteger(10, Result);
65 return Result;
66 }
67
68 /// \brief Stores an option with the check-local name \p LocalName with string
69 /// value \p Value to \p Options.
70 void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
71 StringRef Value) const;
72
73 /// \brief Stores an option with the check-local name \p LocalName with
74 /// \c int64_t value \p Value to \p Options.
75 void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName,
76 int64_t Value) const;
77
78private:
79 std::string NamePrefix;
80 const ClangTidyOptions::OptionMap &CheckOptions;
81};
82
Daniel Jasperd07c8402013-07-29 08:19:24 +000083/// \brief Base class for all clang-tidy checks.
84///
Alexander Kornienko8cef0752015-02-25 13:05:33 +000085/// To implement a \c ClangTidyCheck, write a subclass and override some of the
Daniel Jasperd07c8402013-07-29 08:19:24 +000086/// base class's methods. E.g. to implement a check that validates namespace
Alexander Kornienko8cef0752015-02-25 13:05:33 +000087/// declarations, override \c registerMatchers:
Daniel Jasperd07c8402013-07-29 08:19:24 +000088///
89/// \code
90/// registerMatchers(ast_matchers::MatchFinder *Finder) {
91/// Finder->addMatcher(namespaceDecl().bind("namespace"), this);
92/// }
93/// \endcode
94///
Alexander Kornienko8cef0752015-02-25 13:05:33 +000095/// and then override \c check(const MatchResult &Result) to do the actual
Daniel Jasperd07c8402013-07-29 08:19:24 +000096/// check for each match.
97///
98/// A new \c ClangTidyCheck instance is created per translation unit.
99///
100/// FIXME: Figure out whether carrying information from one TU to another is
101/// useful/necessary.
102class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
103public:
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +0000104 /// \brief Initializes the check with \p CheckName and \p Context.
105 ///
106 /// Derived classes must implement the constructor with this signature or
107 /// delegate it. If a check needs to read options, it can do this in the
108 /// constructor using the Options.get() methods below.
109 ClangTidyCheck(StringRef CheckName, ClangTidyContext *Context)
110 : CheckName(CheckName), Context(Context),
111 Options(CheckName, Context->getOptions().CheckOptions) {
112 assert(Context != nullptr);
113 assert(!CheckName.empty());
114 }
115
Alexander Kornienko8cef0752015-02-25 13:05:33 +0000116 /// \brief Override this to register \c PPCallbacks with \c Compiler.
Daniel Jasperd07c8402013-07-29 08:19:24 +0000117 ///
118 /// This should be used for clang-tidy checks that analyze preprocessor-
119 /// dependent properties, e.g. the order of include directives.
120 virtual void registerPPCallbacks(CompilerInstance &Compiler) {}
121
Alexander Kornienko8cef0752015-02-25 13:05:33 +0000122 /// \brief Override this to register ASTMatchers with \p Finder.
Daniel Jasperd07c8402013-07-29 08:19:24 +0000123 ///
124 /// This should be used by clang-tidy checks that analyze code properties that
125 /// dependent on AST knowledge.
126 ///
127 /// You can register as many matchers as necessary with \p Finder. Usually,
128 /// "this" will be used as callback, but you can also specify other callback
129 /// classes. Thereby, different matchers can trigger different callbacks.
130 ///
131 /// If you need to merge information between the different matchers, you can
132 /// store these as members of the derived class. However, note that all
133 /// matches occur in the order of the AST traversal.
134 virtual void registerMatchers(ast_matchers::MatchFinder *Finder) {}
135
136 /// \brief \c ClangTidyChecks that register ASTMatchers should do the actual
137 /// work in here.
138 virtual void check(const ast_matchers::MatchFinder::MatchResult &Result) {}
139
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +0000140 /// \brief Add a diagnostic with the check's name.
Peter Collingbourneb17a3b32014-03-02 23:34:48 +0000141 DiagnosticBuilder diag(SourceLocation Loc, StringRef Description,
142 DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +0000143
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +0000144 /// \brief Should store all options supported by this check with their
145 /// current values or default values for options that haven't been overridden.
146 ///
147 /// The check should use \c Options.store() to store each option it supports
148 /// whether it has the default value or it has been overridden.
149 virtual void storeOptions(ClangTidyOptions::OptionMap &Options) {}
Daniel Jasperd07c8402013-07-29 08:19:24 +0000150
151private:
Craig Toppera3dbe842014-03-02 10:20:11 +0000152 void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000153 StringRef getID() const override { return CheckName; }
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +0000154 std::string CheckName;
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +0000155 ClangTidyContext *Context;
156
157protected:
158 OptionsView Options;
Alexander Kornienko19bbeaf2015-05-21 14:08:56 +0000159 /// \brief Returns the main file name of the current translation unit.
160 StringRef getCurrentMainFile() const { return Context->getCurrentFile(); }
Aaron Ballmanf36a4252015-08-28 13:20:46 +0000161 /// \brief Returns the language options from the context.
162 LangOptions getLangOpts() const { return Context->getLangOpts(); }
Daniel Jasperd07c8402013-07-29 08:19:24 +0000163};
164
Alexander Kornienko175fefb2014-01-03 09:31:57 +0000165class ClangTidyCheckFactories;
166
167class ClangTidyASTConsumerFactory {
168public:
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000169 ClangTidyASTConsumerFactory(ClangTidyContext &Context);
Alexander Kornienko175fefb2014-01-03 09:31:57 +0000170
171 /// \brief Returns an ASTConsumer that runs the specified clang-tidy checks.
David Blaikie680c4c82014-08-10 19:56:59 +0000172 std::unique_ptr<clang::ASTConsumer>
173 CreateASTConsumer(clang::CompilerInstance &Compiler, StringRef File);
Alexander Kornienko175fefb2014-01-03 09:31:57 +0000174
175 /// \brief Get the list of enabled checks.
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +0000176 std::vector<std::string> getCheckNames();
177
178 /// \brief Get the union of options from all checks.
179 ClangTidyOptions::OptionMap getCheckOptions();
Alexander Kornienko175fefb2014-01-03 09:31:57 +0000180
181private:
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +0000182 typedef std::vector<std::pair<std::string, bool>> CheckersList;
Alexander Kornienkob3d331d2014-08-06 11:49:10 +0000183 CheckersList getCheckersControlList(GlobList &Filter);
Alexander Kornienko175fefb2014-01-03 09:31:57 +0000184
Alexander Kornienko175fefb2014-01-03 09:31:57 +0000185 ClangTidyContext &Context;
Ahmed Charles6a2dc5c2014-03-09 09:24:40 +0000186 std::unique_ptr<ClangTidyCheckFactories> CheckFactories;
Alexander Kornienko175fefb2014-01-03 09:31:57 +0000187};
188
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000189/// \brief Fills the list of check names that are enabled when the provided
190/// filters are applied.
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +0000191std::vector<std::string> getCheckNames(const ClangTidyOptions &Options);
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000192
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +0000193/// \brief Returns the effective check-specific options.
194///
195/// The method configures ClangTidy with the specified \p Options and collects
196/// effective options from all created checks. The returned set of options
197/// includes default check-specific options for all keys not overridden by \p
198/// Options.
199ClangTidyOptions::OptionMap getCheckOptions(const ClangTidyOptions &Options);
200
Daniel Jasperd07c8402013-07-29 08:19:24 +0000201/// \brief Run a set of clang-tidy checks on a set of files.
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000202///
203/// \param Profile if provided, it enables check profile collection in
204/// MatchFinder, and will contain the result of the profile.
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000205ClangTidyStats
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000206runClangTidy(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000207 const tooling::CompilationDatabase &Compilations,
208 ArrayRef<std::string> InputFiles,
Samuel Benzaquenaedd9942014-10-23 17:23:20 +0000209 std::vector<ClangTidyError> *Errors,
210 ProfileData *Profile = nullptr);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000211
212// FIXME: This interface will need to be significantly extended to be useful.
213// FIXME: Implement confidence levels for displaying/fixing errors.
214//
215/// \brief Displays the found \p Errors to the users. If \p Fix is true, \p
216/// Errors containing fixes are automatically applied.
Alexander Kornienko826b5ad2014-05-09 12:24:09 +0000217void handleErrors(const std::vector<ClangTidyError> &Errors, bool Fix);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000218
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000219/// \brief Serializes replacements into YAML and writes them to the specified
220/// output stream.
221void exportReplacements(const std::vector<ClangTidyError> &Errors,
222 raw_ostream &OS);
223
Daniel Jasperd07c8402013-07-29 08:19:24 +0000224} // end namespace tidy
225} // end namespace clang
226
Alexander Kornienko66580552015-03-09 16:52:33 +0000227#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDY_H