blob: 58e833c0e877ba0e8ad1fd8d79dc2de26074b51f [file] [log] [blame]
Daniel Jasperd07c8402013-07-29 08:19:24 +00001//===--- ClangTidyModule.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_CLANGTIDYMODULE_H
11#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H
Daniel Jasperd07c8402013-07-29 08:19:24 +000012
13#include "ClangTidy.h"
Daniel Jasperd07c8402013-07-29 08:19:24 +000014#include "llvm/ADT/StringRef.h"
Alexander Kornienkod3fdcf82014-09-10 11:25:43 +000015#include <functional>
16#include <map>
17#include <string>
Daniel Jasperd07c8402013-07-29 08:19:24 +000018#include <utility>
19
20namespace clang {
21namespace tidy {
22
Alexander Kornienkod3fdcf82014-09-10 11:25:43 +000023/// \brief A collection of \c ClangTidyCheckFactory instances.
Daniel Jasperd07c8402013-07-29 08:19:24 +000024///
Alexander Kornienkod3fdcf82014-09-10 11:25:43 +000025/// All clang-tidy modules register their check factories with an instance of
26/// this object.
27class ClangTidyCheckFactories {
Daniel Jasperd07c8402013-07-29 08:19:24 +000028public:
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000029 typedef std::function<ClangTidyCheck *(
30 StringRef Name, ClangTidyContext *Context)> CheckFactory;
31
Alexander Kornienkod3fdcf82014-09-10 11:25:43 +000032 /// \brief Registers check \p Factory with name \p Name.
33 ///
34 /// For all checks that have default constructors, use \c registerCheck.
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000035 void registerCheckFactory(StringRef Name, CheckFactory Factory);
Daniel Jasperd07c8402013-07-29 08:19:24 +000036
Alexander Kornienkod3fdcf82014-09-10 11:25:43 +000037 /// \brief Registers the \c CheckType with the name \p Name.
38 ///
39 /// This method should be used for all \c ClangTidyChecks that don't require
40 /// constructor parameters.
41 ///
42 /// For example, if have a clang-tidy check like:
43 /// \code
44 /// class MyTidyCheck : public ClangTidyCheck {
45 /// void registerMatchers(ast_matchers::MatchFinder *Finder) override {
46 /// ..
47 /// }
48 /// };
49 /// \endcode
50 /// you can register it with:
51 /// \code
52 /// class MyModule : public ClangTidyModule {
53 /// void addCheckFactories(ClangTidyCheckFactories &Factories) override {
54 /// Factories.registerCheck<MyTidyCheck>("myproject-my-check");
55 /// }
56 /// };
57 /// \endcode
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000058 template <typename CheckType> void registerCheck(StringRef CheckName) {
59 registerCheckFactory(CheckName,
60 [](StringRef Name, ClangTidyContext *Context) {
61 return new CheckType(Name, Context);
62 });
Alexander Kornienkod3fdcf82014-09-10 11:25:43 +000063 }
Daniel Jasperd07c8402013-07-29 08:19:24 +000064
Alexander Kornienkod3fdcf82014-09-10 11:25:43 +000065 /// \brief Create instances of all checks matching \p CheckRegexString and
66 /// store them in \p Checks.
67 ///
68 /// The caller takes ownership of the return \c ClangTidyChecks.
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000069 void createChecks(ClangTidyContext *Context,
Alexander Kornienkod3fdcf82014-09-10 11:25:43 +000070 std::vector<std::unique_ptr<ClangTidyCheck>> &Checks);
71
Alexander Kornienko6e0cbc82014-09-12 08:53:36 +000072 typedef std::map<std::string, CheckFactory> FactoryMap;
Alexander Kornienkod3fdcf82014-09-10 11:25:43 +000073 FactoryMap::const_iterator begin() const { return Factories.begin(); }
74 FactoryMap::const_iterator end() const { return Factories.end(); }
75 bool empty() const { return Factories.empty(); }
76
77private:
78 FactoryMap Factories;
79};
Daniel Jasperd07c8402013-07-29 08:19:24 +000080
81/// \brief A clang-tidy module groups a number of \c ClangTidyChecks and gives
82/// them a prefixed name.
83class ClangTidyModule {
84public:
85 virtual ~ClangTidyModule() {}
86
87 /// \brief Implement this function in order to register all \c CheckFactories
88 /// belonging to this module.
89 virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0;
Alexander Kornienko1efc4252014-10-16 11:27:57 +000090
91 /// \brief Gets default options for checks defined in this module.
92 virtual ClangTidyOptions getModuleOptions();
Daniel Jasperd07c8402013-07-29 08:19:24 +000093};
94
Daniel Jasperd07c8402013-07-29 08:19:24 +000095} // end namespace tidy
96} // end namespace clang
97
Alexander Kornienko66580552015-03-09 16:52:33 +000098#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANGTIDYMODULE_H