blob: 9a40bb7397b48cc8b8e30f1024b3850a619bd57d [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"
Yitzhak Mandelbaume4232752019-07-02 13:25:07 +000013#include "../utils/IncludeInserter.h"
Yitzhak Mandelbaum9df7ce52019-05-22 18:56:18 +000014#include "clang/ASTMatchers/ASTMatchFinder.h"
Yitzhak Mandelbaume4232752019-07-02 13:25:07 +000015#include "clang/Frontend/CompilerInstance.h"
Yitzhak Mandelbaum9df7ce52019-05-22 18:56:18 +000016#include "clang/Tooling/Refactoring/Transformer.h"
17#include <deque>
18#include <vector>
19
20namespace clang {
21namespace tidy {
22namespace 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// };
34class TransformerClangTidyCheck : public ClangTidyCheck {
35public:
Yitzhak Mandelbaum039af0e2019-06-26 16:04:38 +000036 // \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 Mandelbaum9df7ce52019-05-22 18:56:18 +000054 TransformerClangTidyCheck(tooling::RewriteRule R, StringRef Name,
Yitzhak Mandelbaum5b335542019-05-24 16:32:03 +000055 ClangTidyContext *Context);
Yitzhak Mandelbaum039af0e2019-06-26 16:04:38 +000056
Yitzhak Mandelbaume4232752019-07-02 13:25:07 +000057 void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
58 Preprocessor *ModuleExpanderPP) override;
Yitzhak Mandelbaum9df7ce52019-05-22 18:56:18 +000059 void registerMatchers(ast_matchers::MatchFinder *Finder) final;
60 void check(const ast_matchers::MatchFinder::MatchResult &Result) final;
61
62private:
Yitzhak Mandelbaum039af0e2019-06-26 16:04:38 +000063 Optional<tooling::RewriteRule> Rule;
Yitzhak Mandelbaume4232752019-07-02 13:25:07 +000064 std::unique_ptr<clang::tidy::utils::IncludeInserter> Inserter;
Yitzhak Mandelbaum9df7ce52019-05-22 18:56:18 +000065};
66
67} // namespace utils
68} // namespace tidy
69} // namespace clang
70
71#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_TRANSFORMER_CLANG_TIDY_CHECK_H