blob: 268a2d2047129780130d9547511731c7d7a68913 [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"
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 Kornienkoa4695222014-06-05 13:31:45 +000019#include <memory>
Alexander Kornienko826b5ad2014-05-09 12:24:09 +000020#include <vector>
Daniel Jasperd07c8402013-07-29 08:19:24 +000021
22namespace clang {
23
24class CompilerInstance;
Daniel Jasperd07c8402013-07-29 08:19:24 +000025namespace tooling {
26class CompilationDatabase;
27}
28
29namespace tidy {
30
Daniel Jasperd07c8402013-07-29 08:19:24 +000031/// \brief Base class for all clang-tidy checks.
32///
33/// To implement a \c ClangTidyCheck, write a subclass and overwrite some of the
34/// base class's methods. E.g. to implement a check that validates namespace
35/// declarations, overwrite \c registerMatchers:
36///
37/// \code
38/// registerMatchers(ast_matchers::MatchFinder *Finder) {
39/// Finder->addMatcher(namespaceDecl().bind("namespace"), this);
40/// }
41/// \endcode
42///
43/// and then overwrite \c check(const MatchResult &Result) to do the actual
44/// check for each match.
45///
46/// A new \c ClangTidyCheck instance is created per translation unit.
47///
48/// FIXME: Figure out whether carrying information from one TU to another is
49/// useful/necessary.
50class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback {
51public:
52 virtual ~ClangTidyCheck() {}
53
54 /// \brief Overwrite this to register \c PPCallbacks with \c Compiler.
55 ///
56 /// This should be used for clang-tidy checks that analyze preprocessor-
57 /// dependent properties, e.g. the order of include directives.
58 virtual void registerPPCallbacks(CompilerInstance &Compiler) {}
59
60 /// \brief Overwrite this to register ASTMatchers with \p Finder.
61 ///
62 /// This should be used by clang-tidy checks that analyze code properties that
63 /// dependent on AST knowledge.
64 ///
65 /// You can register as many matchers as necessary with \p Finder. Usually,
66 /// "this" will be used as callback, but you can also specify other callback
67 /// classes. Thereby, different matchers can trigger different callbacks.
68 ///
69 /// If you need to merge information between the different matchers, you can
70 /// store these as members of the derived class. However, note that all
71 /// matches occur in the order of the AST traversal.
72 virtual void registerMatchers(ast_matchers::MatchFinder *Finder) {}
73
74 /// \brief \c ClangTidyChecks that register ASTMatchers should do the actual
75 /// work in here.
76 virtual void check(const ast_matchers::MatchFinder::MatchResult &Result) {}
77
78 /// \brief The infrastructure sets the context to \p Ctx with this function.
79 void setContext(ClangTidyContext *Ctx) { Context = Ctx; }
80
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +000081 /// \brief Add a diagnostic with the check's name.
Peter Collingbourneb17a3b32014-03-02 23:34:48 +000082 DiagnosticBuilder diag(SourceLocation Loc, StringRef Description,
83 DiagnosticIDs::Level Level = DiagnosticIDs::Warning);
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +000084
85 /// \brief Sets the check name. Intended to be used by the clang-tidy
86 /// framework. Can be called only once.
87 void setName(StringRef Name);
Daniel Jasperd07c8402013-07-29 08:19:24 +000088
89private:
Craig Toppera3dbe842014-03-02 10:20:11 +000090 void run(const ast_matchers::MatchFinder::MatchResult &Result) override;
Alexander Kornienko41bfe8d2014-01-13 10:50:51 +000091 ClangTidyContext *Context;
92 std::string CheckName;
Daniel Jasperd07c8402013-07-29 08:19:24 +000093};
94
Alexander Kornienko175fefb2014-01-03 09:31:57 +000095class ClangTidyCheckFactories;
96
97class ClangTidyASTConsumerFactory {
98public:
Alexander Kornienkoa4695222014-06-05 13:31:45 +000099 ClangTidyASTConsumerFactory(ClangTidyContext &Context);
Alexander Kornienko175fefb2014-01-03 09:31:57 +0000100
101 /// \brief Returns an ASTConsumer that runs the specified clang-tidy checks.
David Blaikie680c4c82014-08-10 19:56:59 +0000102 std::unique_ptr<clang::ASTConsumer>
103 CreateASTConsumer(clang::CompilerInstance &Compiler, StringRef File);
Alexander Kornienko175fefb2014-01-03 09:31:57 +0000104
105 /// \brief Get the list of enabled checks.
Alexander Kornienkob3d331d2014-08-06 11:49:10 +0000106 std::vector<std::string> getCheckNames(GlobList &Filter);
Alexander Kornienko175fefb2014-01-03 09:31:57 +0000107
108private:
109 typedef std::vector<std::pair<std::string, bool> > CheckersList;
Alexander Kornienkob3d331d2014-08-06 11:49:10 +0000110 CheckersList getCheckersControlList(GlobList &Filter);
Alexander Kornienko175fefb2014-01-03 09:31:57 +0000111
Alexander Kornienko175fefb2014-01-03 09:31:57 +0000112 ClangTidyContext &Context;
Ahmed Charles6a2dc5c2014-03-09 09:24:40 +0000113 std::unique_ptr<ClangTidyCheckFactories> CheckFactories;
Alexander Kornienko175fefb2014-01-03 09:31:57 +0000114};
115
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000116/// \brief Fills the list of check names that are enabled when the provided
117/// filters are applied.
Alexander Kornienko33a9bcc2014-04-29 15:20:10 +0000118std::vector<std::string> getCheckNames(const ClangTidyOptions &Options);
Alexander Kornienkofb9e92b2013-12-19 19:57:05 +0000119
Daniel Jasperd07c8402013-07-29 08:19:24 +0000120/// \brief Run a set of clang-tidy checks on a set of files.
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000121ClangTidyStats
Alexander Kornienkod53d2682014-09-04 14:23:36 +0000122runClangTidy(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
Alexander Kornienkoa4695222014-06-05 13:31:45 +0000123 const tooling::CompilationDatabase &Compilations,
124 ArrayRef<std::string> InputFiles,
125 std::vector<ClangTidyError> *Errors);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000126
127// FIXME: This interface will need to be significantly extended to be useful.
128// FIXME: Implement confidence levels for displaying/fixing errors.
129//
130/// \brief Displays the found \p Errors to the users. If \p Fix is true, \p
131/// Errors containing fixes are automatically applied.
Alexander Kornienko826b5ad2014-05-09 12:24:09 +0000132void handleErrors(const std::vector<ClangTidyError> &Errors, bool Fix);
Daniel Jasperd07c8402013-07-29 08:19:24 +0000133
Benjamin Kramerfb98b742014-09-04 10:31:23 +0000134/// \brief Serializes replacements into YAML and writes them to the specified
135/// output stream.
136void exportReplacements(const std::vector<ClangTidyError> &Errors,
137 raw_ostream &OS);
138
Daniel Jasperd07c8402013-07-29 08:19:24 +0000139} // end namespace tidy
140} // end namespace clang
141
142#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_H