| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 1 | //===--- 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 Carruth | 85e6e87 | 2014-01-07 20:05:01 +0000 | [diff] [blame^] | 13 | #include "ClangTidyDiagnosticConsumer.h" | 
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 14 | #include "clang/ASTMatchers/ASTMatchFinder.h" | 
|  | 15 | #include "clang/Basic/Diagnostic.h" | 
|  | 16 | #include "clang/Basic/SourceManager.h" | 
|  | 17 | #include "clang/Tooling/Refactoring.h" | 
|  | 18 |  | 
|  | 19 | namespace clang { | 
|  | 20 |  | 
|  | 21 | class CompilerInstance; | 
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 22 | namespace tooling { | 
|  | 23 | class CompilationDatabase; | 
|  | 24 | } | 
|  | 25 |  | 
|  | 26 | namespace tidy { | 
|  | 27 |  | 
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 28 | /// \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. | 
|  | 47 | class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback { | 
|  | 48 | public: | 
|  | 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 |  | 
|  | 78 | protected: | 
|  | 79 | ClangTidyContext *Context; | 
|  | 80 |  | 
|  | 81 | private: | 
|  | 82 | virtual void run(const ast_matchers::MatchFinder::MatchResult &Result); | 
|  | 83 | }; | 
|  | 84 |  | 
| Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 85 | /// \brief Filters checks by name. | 
|  | 86 | class ChecksFilter { | 
|  | 87 | public: | 
|  | 88 | ChecksFilter(StringRef EnableChecksRegex, StringRef DisableChecksRegex); | 
|  | 89 | bool IsCheckEnabled(StringRef Name); | 
|  | 90 |  | 
|  | 91 | private: | 
|  | 92 | llvm::Regex EnableChecks; | 
|  | 93 | llvm::Regex DisableChecks; | 
|  | 94 | }; | 
|  | 95 |  | 
| Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 96 | class ClangTidyCheckFactories; | 
|  | 97 |  | 
|  | 98 | class ClangTidyASTConsumerFactory { | 
|  | 99 | public: | 
|  | 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 |  | 
|  | 112 | private: | 
|  | 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 Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 123 | /// \brief Fills the list of check names that are enabled when the provided | 
|  | 124 | /// filters are applied. | 
|  | 125 | std::vector<std::string> getCheckNames(StringRef EnableChecksRegex, | 
|  | 126 | StringRef DisableChecksRegex); | 
|  | 127 |  | 
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 128 | /// \brief Run a set of clang-tidy checks on a set of files. | 
| Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 129 | void runClangTidy(StringRef EnableChecksRegex, StringRef DisableChecksRegex, | 
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 130 | 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. | 
|  | 139 | void 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 |