blob: 16889bff80a9b6d48707f34e7cd0fc9ec0d344dd [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
10#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_H
11#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_H
12
Chandler Carruth85e6e872014-01-07 20:05:01 +000013#include "ClangTidyDiagnosticConsumer.h"
Daniel Jasperd07c8402013-07-29 08:19:24 +000014#include "clang/ASTMatchers/ASTMatchFinder.h"
15#include "clang/Basic/Diagnostic.h"
16#include "clang/Basic/SourceManager.h"
17#include "clang/Tooling/Refactoring.h"
18
19namespace clang {
20
21class CompilerInstance;
Daniel Jasperd07c8402013-07-29 08:19:24 +000022namespace tooling {
23class CompilationDatabase;
24}
25
26namespace tidy {
27
Daniel Jasperd07c8402013-07-29 08:19:24 +000028/// \brief Base class for all clang-tidy checks.
29///
30/// To implement a \c ClangTidyCheck, write a subclass and overwrite some of the
31/// base class's methods. E.g. to implement a check that validates namespace
32/// declarations, overwrite \c registerMatchers:
33///
34/// \code
35/// registerMatchers(ast_matchers::MatchFinder *Finder) {
36/// Finder->addMatcher(namespaceDecl().bind("namespace"), this);
37/// }
38/// \endcode
39///
40/// and then overwrite \c check(const MatchResult &Result) to do the actual
41/// check for each match.
42///
43/// A new \c ClangTidyCheck instance is created per translation unit.
44///
45/// FIXME: Figure out whether carrying information from one TU to another is
46/// useful/necessary.
47class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
48public:
49 virtual ~ClangTidyCheck() {}
50
51 /// \brief Overwrite this to register \c PPCallbacks with \c Compiler.
52 ///
53 /// This should be used for clang-tidy checks that analyze preprocessor-
54 /// dependent properties, e.g. the order of include directives.
55 virtual void registerPPCallbacks(CompilerInstance &Compiler) {}
56
57 /// \brief Overwrite this to register ASTMatchers with \p Finder.
58 ///
59 /// This should be used by clang-tidy checks that analyze code properties that
60 /// dependent on AST knowledge.
61 ///
62 /// You can register as many matchers as necessary with \p Finder. Usually,
63 /// "this" will be used as callback, but you can also specify other callback
64 /// classes. Thereby, different matchers can trigger different callbacks.
65 ///
66 /// If you need to merge information between the different matchers, you can
67 /// store these as members of the derived class. However, note that all
68 /// matches occur in the order of the AST traversal.
69 virtual void registerMatchers(ast_matchers::MatchFinder *Finder) {}
70
71 /// \brief \c ClangTidyChecks that register ASTMatchers should do the actual
72 /// work in here.
73 virtual void check(const ast_matchers::MatchFinder::MatchResult &Result) {}
74
75 /// \brief The infrastructure sets the context to \p Ctx with this function.
76 void setContext(ClangTidyContext *Ctx) { Context = Ctx; }
77
78protected:
79 ClangTidyContext *Context;
80
81private:
82 virtual void run(const ast_matchers::MatchFinder::MatchResult &Result);
83};
84
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +000085/// \brief Filters checks by name.
86class ChecksFilter {
87public:
88 ChecksFilter(StringRef EnableChecksRegex, StringRef DisableChecksRegex);
89 bool IsCheckEnabled(StringRef Name);
90
91private:
92 llvm::Regex EnableChecks;
93 llvm::Regex DisableChecks;
94};
95
Alexander Kornienko175fefb2014-01-03 09:31:57 +000096class ClangTidyCheckFactories;
97
98class ClangTidyASTConsumerFactory {
99public:
100 ClangTidyASTConsumerFactory(StringRef EnableChecksRegex,
101 StringRef DisableChecksRegex,
102 ClangTidyContext &Context);
103 ~ClangTidyASTConsumerFactory();
104
105 /// \brief Returns an ASTConsumer that runs the specified clang-tidy checks.
106 clang::ASTConsumer *CreateASTConsumer(clang::CompilerInstance &Compiler,
107 StringRef File);
108
109 /// \brief Get the list of enabled checks.
110 std::vector<std::string> getCheckNames();
111
112private:
113 typedef std::vector<std::pair<std::string, bool> > CheckersList;
114 CheckersList getCheckersControlList();
115
116 ChecksFilter Filter;
117 SmallVector<ClangTidyCheck *, 8> Checks;
118 ClangTidyContext &Context;
119 ast_matchers::MatchFinder Finder;
120 OwningPtr<ClangTidyCheckFactories> CheckFactories;
121};
122
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000123/// \brief Fills the list of check names that are enabled when the provided
124/// filters are applied.
125std::vector<std::string> getCheckNames(StringRef EnableChecksRegex,
126 StringRef DisableChecksRegex);
127
Daniel Jasperd07c8402013-07-29 08:19:24 +0000128/// \brief Run a set of clang-tidy checks on a set of files.
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000129void runClangTidy(StringRef EnableChecksRegex, StringRef DisableChecksRegex,
Daniel Jasperd07c8402013-07-29 08:19:24 +0000130 const tooling::CompilationDatabase &Compilations,
131 ArrayRef<std::string> Ranges,
132 SmallVectorImpl<ClangTidyError> *Errors);
133
134// FIXME: This interface will need to be significantly extended to be useful.
135// FIXME: Implement confidence levels for displaying/fixing errors.
136//
137/// \brief Displays the found \p Errors to the users. If \p Fix is true, \p
138/// Errors containing fixes are automatically applied.
139void handleErrors(SmallVectorImpl<ClangTidyError> &Errors, bool Fix);
140
141} // end namespace tidy
142} // end namespace clang
143
144#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_H