| 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" | 
| Alexander Kornienko | 33a9bcc | 2014-04-29 15:20:10 +0000 | [diff] [blame] | 14 | #include "ClangTidyOptions.h" | 
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 15 | #include "clang/ASTMatchers/ASTMatchFinder.h" | 
|  | 16 | #include "clang/Basic/Diagnostic.h" | 
|  | 17 | #include "clang/Basic/SourceManager.h" | 
|  | 18 | #include "clang/Tooling/Refactoring.h" | 
| Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 19 | #include <memory> | 
| Alexander Kornienko | 826b5ad | 2014-05-09 12:24:09 +0000 | [diff] [blame] | 20 | #include <vector> | 
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 21 |  | 
|  | 22 | namespace clang { | 
|  | 23 |  | 
|  | 24 | class CompilerInstance; | 
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 25 | namespace tooling { | 
|  | 26 | class CompilationDatabase; | 
|  | 27 | } | 
|  | 28 |  | 
|  | 29 | namespace tidy { | 
|  | 30 |  | 
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 31 | /// \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. | 
|  | 50 | class ClangTidyCheck : public ast_matchers::MatchFinder::MatchCallback { | 
|  | 51 | public: | 
|  | 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 Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame] | 81 | /// \brief Add a diagnostic with the check's name. | 
| Peter Collingbourne | b17a3b3 | 2014-03-02 23:34:48 +0000 | [diff] [blame] | 82 | DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, | 
|  | 83 | DiagnosticIDs::Level Level = DiagnosticIDs::Warning); | 
| Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame] | 84 |  | 
|  | 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 Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 88 |  | 
|  | 89 | private: | 
| Craig Topper | a3dbe84 | 2014-03-02 10:20:11 +0000 | [diff] [blame] | 90 | void run(const ast_matchers::MatchFinder::MatchResult &Result) override; | 
| Alexander Kornienko | 41bfe8d | 2014-01-13 10:50:51 +0000 | [diff] [blame] | 91 | ClangTidyContext *Context; | 
|  | 92 | std::string CheckName; | 
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 93 | }; | 
|  | 94 |  | 
| Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 95 | class ClangTidyCheckFactories; | 
|  | 96 |  | 
|  | 97 | class ClangTidyASTConsumerFactory { | 
|  | 98 | public: | 
| Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 99 | ClangTidyASTConsumerFactory(ClangTidyContext &Context); | 
| Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 100 |  | 
|  | 101 | /// \brief Returns an ASTConsumer that runs the specified clang-tidy checks. | 
| David Blaikie | 680c4c8 | 2014-08-10 19:56:59 +0000 | [diff] [blame] | 102 | std::unique_ptr<clang::ASTConsumer> | 
|  | 103 | CreateASTConsumer(clang::CompilerInstance &Compiler, StringRef File); | 
| Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 104 |  | 
|  | 105 | /// \brief Get the list of enabled checks. | 
| Alexander Kornienko | b3d331d | 2014-08-06 11:49:10 +0000 | [diff] [blame] | 106 | std::vector<std::string> getCheckNames(GlobList &Filter); | 
| Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 107 |  | 
|  | 108 | private: | 
|  | 109 | typedef std::vector<std::pair<std::string, bool> > CheckersList; | 
| Alexander Kornienko | b3d331d | 2014-08-06 11:49:10 +0000 | [diff] [blame] | 110 | CheckersList getCheckersControlList(GlobList &Filter); | 
| Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 111 |  | 
| Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 112 | ClangTidyContext &Context; | 
| Ahmed Charles | 6a2dc5c | 2014-03-09 09:24:40 +0000 | [diff] [blame] | 113 | std::unique_ptr<ClangTidyCheckFactories> CheckFactories; | 
| Alexander Kornienko | 175fefb | 2014-01-03 09:31:57 +0000 | [diff] [blame] | 114 | }; | 
|  | 115 |  | 
| Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 116 | /// \brief Fills the list of check names that are enabled when the provided | 
|  | 117 | /// filters are applied. | 
| Alexander Kornienko | 33a9bcc | 2014-04-29 15:20:10 +0000 | [diff] [blame] | 118 | std::vector<std::string> getCheckNames(const ClangTidyOptions &Options); | 
| Alexander Kornienko | fb9e92b | 2013-12-19 19:57:05 +0000 | [diff] [blame] | 119 |  | 
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 120 | /// \brief Run a set of clang-tidy checks on a set of files. | 
| Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 121 | ClangTidyStats | 
| Alexander Kornienko | d53d268 | 2014-09-04 14:23:36 +0000 | [diff] [blame^] | 122 | runClangTidy(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider, | 
| Alexander Kornienko | a469522 | 2014-06-05 13:31:45 +0000 | [diff] [blame] | 123 | const tooling::CompilationDatabase &Compilations, | 
|  | 124 | ArrayRef<std::string> InputFiles, | 
|  | 125 | std::vector<ClangTidyError> *Errors); | 
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 126 |  | 
|  | 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 Kornienko | 826b5ad | 2014-05-09 12:24:09 +0000 | [diff] [blame] | 132 | void handleErrors(const std::vector<ClangTidyError> &Errors, bool Fix); | 
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 133 |  | 
| Benjamin Kramer | fb98b74 | 2014-09-04 10:31:23 +0000 | [diff] [blame] | 134 | /// \brief Serializes replacements into YAML and writes them to the specified | 
|  | 135 | /// output stream. | 
|  | 136 | void exportReplacements(const std::vector<ClangTidyError> &Errors, | 
|  | 137 | raw_ostream &OS); | 
|  | 138 |  | 
| Daniel Jasper | d07c840 | 2013-07-29 08:19:24 +0000 | [diff] [blame] | 139 | } // end namespace tidy | 
|  | 140 | } // end namespace clang | 
|  | 141 |  | 
|  | 142 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_CLANG_TIDY_H |