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" |
| 13 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 14 | #include "clang/Tooling/Refactoring/Transformer.h" |
| 15 | #include <deque> |
| 16 | #include <vector> |
| 17 | |
| 18 | namespace clang { |
| 19 | namespace tidy { |
| 20 | namespace utils { |
| 21 | |
| 22 | // A base class for defining a ClangTidy check based on a `RewriteRule`. |
| 23 | // |
| 24 | // For example, given a rule `MyCheckAsRewriteRule`, one can define a tidy check |
| 25 | // as follows: |
| 26 | // |
| 27 | // class MyCheck : public TransformerClangTidyCheck { |
| 28 | // public: |
| 29 | // MyCheck(StringRef Name, ClangTidyContext *Context) |
| 30 | // : TransformerClangTidyCheck(MyCheckAsRewriteRule, Name, Context) {} |
| 31 | // }; |
| 32 | class TransformerClangTidyCheck : public ClangTidyCheck { |
| 33 | public: |
Yitzhak Mandelbaum | 039af0e | 2019-06-26 16:04:38 +0000 | [diff] [blame^] | 34 | // \p MakeRule generates the rewrite rule to be used by the check, based on |
| 35 | // the given language and clang-tidy options. It can return \c None to handle |
| 36 | // cases where the options disable the check. |
| 37 | // |
| 38 | // All cases in the rule generated by \p MakeRule must have a non-null \c |
| 39 | // Explanation, even though \c Explanation is optional for RewriteRule in |
| 40 | // general. Because the primary purpose of clang-tidy checks is to provide |
| 41 | // users with diagnostics, we assume that a missing explanation is a bug. If |
| 42 | // no explanation is desired, indicate that explicitly (for example, by |
| 43 | // passing `text("no explanation")` to `makeRule` as the `Explanation` |
| 44 | // argument). |
| 45 | TransformerClangTidyCheck(std::function<Optional<tooling::RewriteRule>( |
| 46 | const LangOptions &, const OptionsView &)> |
| 47 | MakeRule, |
| 48 | StringRef Name, ClangTidyContext *Context); |
| 49 | |
| 50 | // Convenience overload of the constructor when the rule doesn't depend on any |
| 51 | // of the language or clang-tidy options. |
Yitzhak Mandelbaum | 9df7ce5 | 2019-05-22 18:56:18 +0000 | [diff] [blame] | 52 | TransformerClangTidyCheck(tooling::RewriteRule R, StringRef Name, |
Yitzhak Mandelbaum | 5b33554 | 2019-05-24 16:32:03 +0000 | [diff] [blame] | 53 | ClangTidyContext *Context); |
Yitzhak Mandelbaum | 039af0e | 2019-06-26 16:04:38 +0000 | [diff] [blame^] | 54 | |
Yitzhak Mandelbaum | 9df7ce5 | 2019-05-22 18:56:18 +0000 | [diff] [blame] | 55 | void registerMatchers(ast_matchers::MatchFinder *Finder) final; |
| 56 | void check(const ast_matchers::MatchFinder::MatchResult &Result) final; |
| 57 | |
| 58 | private: |
Yitzhak Mandelbaum | 039af0e | 2019-06-26 16:04:38 +0000 | [diff] [blame^] | 59 | Optional<tooling::RewriteRule> Rule; |
Yitzhak Mandelbaum | 9df7ce5 | 2019-05-22 18:56:18 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | } // namespace utils |
| 63 | } // namespace tidy |
| 64 | } // namespace clang |
| 65 | |
| 66 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_TRANSFORMER_CLANG_TIDY_CHECK_H |