blob: 0cd386f5b07d674c4e7692ea4fc76ad244d38fa4 [file] [log] [blame]
Yitzhak Mandelbaum9df7ce52019-05-22 18:56:18 +00001//===---------- 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
18namespace clang {
19namespace tidy {
20namespace 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// };
32class TransformerClangTidyCheck : public ClangTidyCheck {
33public:
Yitzhak Mandelbaum039af0e2019-06-26 16:04:38 +000034 // \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 Mandelbaum9df7ce52019-05-22 18:56:18 +000052 TransformerClangTidyCheck(tooling::RewriteRule R, StringRef Name,
Yitzhak Mandelbaum5b335542019-05-24 16:32:03 +000053 ClangTidyContext *Context);
Yitzhak Mandelbaum039af0e2019-06-26 16:04:38 +000054
Yitzhak Mandelbaum9df7ce52019-05-22 18:56:18 +000055 void registerMatchers(ast_matchers::MatchFinder *Finder) final;
56 void check(const ast_matchers::MatchFinder::MatchResult &Result) final;
57
58private:
Yitzhak Mandelbaum039af0e2019-06-26 16:04:38 +000059 Optional<tooling::RewriteRule> Rule;
Yitzhak Mandelbaum9df7ce52019-05-22 18:56:18 +000060};
61
62} // namespace utils
63} // namespace tidy
64} // namespace clang
65
66#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_TRANSFORMER_CLANG_TIDY_CHECK_H