diff options
author | Daniel Jasper <djasper@google.com> | 2013-07-29 08:19:24 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2013-07-29 08:19:24 +0000 |
commit | d07c840e6a125e954bfe015062c861369aae870e (patch) | |
tree | 23f7638f25cb4755ed1bfc24aeb2c3b2cc418d67 /clang-tools-extra/clang-tidy/ClangTidyModule.h | |
parent | baca389e28985d57f59e42d745696bdf7ed56af2 (diff) | |
download | bcm5719-llvm-d07c840e6a125e954bfe015062c861369aae870e.tar.gz bcm5719-llvm-d07c840e6a125e954bfe015062c861369aae870e.zip |
Initial architecture for clang-tidy.
This is the first version of a possible clang-tidy architecture. The
purpose of clang-tidy is to detect errors in adhering to common coding
patterns, e.g. described in the LLVM Coding Standards.
This is still heavily in flux.
Review: http://llvm-reviews.chandlerc.com/D884
llvm-svn: 187345
Diffstat (limited to 'clang-tools-extra/clang-tidy/ClangTidyModule.h')
-rw-r--r-- | clang-tools-extra/clang-tidy/ClangTidyModule.h | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/ClangTidyModule.h b/clang-tools-extra/clang-tidy/ClangTidyModule.h new file mode 100644 index 00000000000..7008863b31a --- /dev/null +++ b/clang-tools-extra/clang-tidy/ClangTidyModule.h @@ -0,0 +1,98 @@ +//===--- ClangTidyModule.h - clang-tidy -------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_MODULE_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_MODULE_H + +#include "ClangTidy.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringRef.h" +#include <utility> + +namespace clang { +namespace tidy { + +/// \brief A factory, that can instantiate a specific clang-tidy check for +/// processing a translation unit. +/// +/// In order to register your check with the \c ClangTidyModule, create a +/// subclass of \c CheckFactoryBase and implement \c createCheck(). Then, use +/// this subclass in \c ClangTidyModule::addCheckFactories(). +class CheckFactoryBase { +public: + virtual ~CheckFactoryBase(); + virtual ClangTidyCheck *createCheck() = 0; +}; + +/// \brief A subclass of \c CheckFactoryBase that should be used for all +/// \c ClangTidyChecks that don't require constructor parameters. +/// +/// For example, if have a clang-tidy check like: +/// \code +/// class MyTidyCheck : public ClangTidyCheck { +/// virtual void registerMatchers(ast_matchers::MatchFinder *Finder) { .. } +/// }; +/// \endcode +/// you can register it with: +/// \code +/// class MyModule : public ClangTidyModule { +/// virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) { +/// CheckFactories.addCheckFactory( +/// "myproject-my-check", new ClangTidyCheckFactory<MyTidyCheck>()); +/// } +/// }; +/// \endcode +template <typename T> class ClangTidyCheckFactory : public CheckFactoryBase { +public: + virtual ClangTidyCheck *createCheck() { return new T; } +}; + +class ClangTidyCheckFactories; + +/// \brief A clang-tidy module groups a number of \c ClangTidyChecks and gives +/// them a prefixed name. +class ClangTidyModule { +public: + virtual ~ClangTidyModule() {} + + /// \brief Implement this function in order to register all \c CheckFactories + /// belonging to this module. + virtual void addCheckFactories(ClangTidyCheckFactories &CheckFactories) = 0; +}; + +/// \brief A collection of \c ClangTidyCheckFactory instances. +/// +/// All clang-tidy modules register their check factories with an instance of +/// this object. +class ClangTidyCheckFactories { +public: + ClangTidyCheckFactories() {} + ~ClangTidyCheckFactories(); + + /// \brief Register \p Factory with the name \p Name. + /// + /// The \c ClangTidyCheckFactories object takes ownership of the \p Factory. + void addCheckFactory(StringRef Name, CheckFactoryBase *Factory); + + /// \brief Create instances of all checks matching \p CheckRegexString and + /// store them in \p Checks. + /// + /// The caller takes ownership of the return \c ClangTidyChecks. + void createChecks(StringRef CheckRegexString, + SmallVectorImpl<ClangTidyCheck *> &Checks); + +private: + StringRef FilterRegex; + std::map<std::string, CheckFactoryBase *> Factories; +}; + +} // end namespace tidy +} // end namespace clang + +#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_MODULE_H |