Yitzhak Mandelbaum | 9df7ce5 | 2019-05-22 18:56:18 +0000 | [diff] [blame] | 1 | //===---------- TransformerClangTidyCheck.h - clang-tidy ------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_TRANSFORMER_CLANG_TIDY_CHECK_H |
| 10 | #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_TRANSFORMER_CLANG_TIDY_CHECK_H |
| 11 | |
| 12 | #include "../ClangTidy.h" |
Yitzhak Mandelbaum | e423275 | 2019-07-02 13:25:07 +0000 | [diff] [blame] | 13 | #include "../utils/IncludeInserter.h" |
Yitzhak Mandelbaum | 9df7ce5 | 2019-05-22 18:56:18 +0000 | [diff] [blame] | 14 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
Yitzhak Mandelbaum | e423275 | 2019-07-02 13:25:07 +0000 | [diff] [blame] | 15 | #include "clang/Frontend/CompilerInstance.h" |
Yitzhak Mandelbaum | 9df7ce5 | 2019-05-22 18:56:18 +0000 | [diff] [blame] | 16 | #include "clang/Tooling/Refactoring/Transformer.h" |
| 17 | #include <deque> |
| 18 | #include <vector> |
| 19 | |
| 20 | namespace clang { |
| 21 | namespace tidy { |
| 22 | namespace utils { |
| 23 | |
| 24 | // A base class for defining a ClangTidy check based on a `RewriteRule`. |
| 25 | // |
| 26 | // For example, given a rule `MyCheckAsRewriteRule`, one can define a tidy check |
| 27 | // as follows: |
| 28 | // |
| 29 | // class MyCheck : public TransformerClangTidyCheck { |
| 30 | // public: |
| 31 | // MyCheck(StringRef Name, ClangTidyContext *Context) |
| 32 | // : TransformerClangTidyCheck(MyCheckAsRewriteRule, Name, Context) {} |
| 33 | // }; |
| 34 | class TransformerClangTidyCheck : public ClangTidyCheck { |
| 35 | public: |
Yitzhak Mandelbaum | 039af0e | 2019-06-26 16:04:38 +0000 | [diff] [blame] | 36 | // \p MakeRule generates the rewrite rule to be used by the check, based on |
| 37 | // the given language and clang-tidy options. It can return \c None to handle |
| 38 | // cases where the options disable the check. |
| 39 | // |
| 40 | // All cases in the rule generated by \p MakeRule must have a non-null \c |
| 41 | // Explanation, even though \c Explanation is optional for RewriteRule in |
| 42 | // general. Because the primary purpose of clang-tidy checks is to provide |
| 43 | // users with diagnostics, we assume that a missing explanation is a bug. If |
| 44 | // no explanation is desired, indicate that explicitly (for example, by |
| 45 | // passing `text("no explanation")` to `makeRule` as the `Explanation` |
| 46 | // argument). |
| 47 | TransformerClangTidyCheck(std::function<Optional<tooling::RewriteRule>( |
| 48 | const LangOptions &, const OptionsView &)> |
| 49 | MakeRule, |
| 50 | StringRef Name, ClangTidyContext *Context); |
| 51 | |
| 52 | // Convenience overload of the constructor when the rule doesn't depend on any |
| 53 | // of the language or clang-tidy options. |
Yitzhak Mandelbaum | 9df7ce5 | 2019-05-22 18:56:18 +0000 | [diff] [blame] | 54 | TransformerClangTidyCheck(tooling::RewriteRule R, StringRef Name, |
Yitzhak Mandelbaum | 5b33554 | 2019-05-24 16:32:03 +0000 | [diff] [blame] | 55 | ClangTidyContext *Context); |
Yitzhak Mandelbaum | 039af0e | 2019-06-26 16:04:38 +0000 | [diff] [blame] | 56 | |
Yitzhak Mandelbaum | e423275 | 2019-07-02 13:25:07 +0000 | [diff] [blame] | 57 | void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, |
| 58 | Preprocessor *ModuleExpanderPP) override; |
Yitzhak Mandelbaum | 9df7ce5 | 2019-05-22 18:56:18 +0000 | [diff] [blame] | 59 | void registerMatchers(ast_matchers::MatchFinder *Finder) final; |
| 60 | void check(const ast_matchers::MatchFinder::MatchResult &Result) final; |
| 61 | |
| 62 | private: |
Yitzhak Mandelbaum | 039af0e | 2019-06-26 16:04:38 +0000 | [diff] [blame] | 63 | Optional<tooling::RewriteRule> Rule; |
Yitzhak Mandelbaum | e423275 | 2019-07-02 13:25:07 +0000 | [diff] [blame] | 64 | std::unique_ptr<clang::tidy::utils::IncludeInserter> Inserter; |
Yitzhak Mandelbaum | 9df7ce5 | 2019-05-22 18:56:18 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | } // namespace utils |
| 68 | } // namespace tidy |
| 69 | } // namespace clang |
| 70 | |
| 71 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_TRANSFORMER_CLANG_TIDY_CHECK_H |