blob: faf946ceb0feb4728994b50b35180f5d3fd451ea [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 Mandelbaum5b335542019-05-24 16:32:03 +000034 // All cases in \p R must have a non-null \c Explanation, even though \c
35 // Explanation is optional for RewriteRule in general. Because the primary
36 // purpose of clang-tidy checks is to provide users with diagnostics, we
37 // assume that a missing explanation is a bug. If no explanation is desired,
38 // indicate that explicitly (for example, by passing `text("no explanation")`
39 // to `makeRule` as the `Explanation` argument).
Yitzhak Mandelbaum9df7ce52019-05-22 18:56:18 +000040 TransformerClangTidyCheck(tooling::RewriteRule R, StringRef Name,
Yitzhak Mandelbaum5b335542019-05-24 16:32:03 +000041 ClangTidyContext *Context);
Yitzhak Mandelbaum9df7ce52019-05-22 18:56:18 +000042 void registerMatchers(ast_matchers::MatchFinder *Finder) final;
43 void check(const ast_matchers::MatchFinder::MatchResult &Result) final;
44
45private:
46 tooling::RewriteRule Rule;
47};
48
49} // namespace utils
50} // namespace tidy
51} // namespace clang
52
53#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_TRANSFORMER_CLANG_TIDY_CHECK_H