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 "../ClangTidyCheck.h"
13 #include "IncludeInserter.h"
14 #include "IncludeSorter.h"
15 #include "clang/ASTMatchers/ASTMatchFinder.h"
16 #include "clang/Tooling/Transformer/Transformer.h"
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 //
33 // `TransformerClangTidyCheck` recognizes this clang-tidy option:
34 //
35 //  * IncludeStyle. A string specifying which file naming convention is used by
36 //      the source code, 'llvm' or 'google'.  Default is 'llvm'. The naming
37 //      convention influences how canonical headers are distinguished from other
38 //      includes.
39 class TransformerClangTidyCheck : public ClangTidyCheck {
40 public:
41   TransformerClangTidyCheck(StringRef Name, ClangTidyContext *Context);
42 
43   /// DEPRECATED: prefer the two argument constructor in conjunction with
44   /// \c setRule.
45   ///
46   /// \p MakeRule generates the rewrite rule to be used by the check, based on
47   /// the given language and clang-tidy options. It can return \c None to handle
48   /// cases where the options disable the check.
49   ///
50   /// See \c setRule for constraints on the rule.
51   TransformerClangTidyCheck(std::function<Optional<transformer::RewriteRule>(
52                                 const LangOptions &, const OptionsView &)>
53                                 MakeRule,
54                             StringRef Name, ClangTidyContext *Context);
55 
56   /// Convenience overload of the constructor when the rule doesn't have any
57   /// dependies.
58   TransformerClangTidyCheck(transformer::RewriteRule R, StringRef Name,
59                             ClangTidyContext *Context);
60 
61   void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
62                            Preprocessor *ModuleExpanderPP) override;
63   void registerMatchers(ast_matchers::MatchFinder *Finder) final;
64   void check(const ast_matchers::MatchFinder::MatchResult &Result) final;
65 
66   /// Derived classes that override this function should call this method from
67   /// the overridden method.
68   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
69 
70   /// Set the rule that this check implements.  All cases in the rule must have
71   /// a non-null \c Explanation, even though \c Explanation is optional for
72   /// RewriteRule in general. Because the primary purpose of clang-tidy checks
73   /// is to provide users with diagnostics, we assume that a missing explanation
74   /// is a bug.  If no explanation is desired, indicate that explicitly (for
75   /// example, by passing `text("no explanation")` to `makeRule` as the
76   /// `Explanation` argument).
77   void setRule(transformer::RewriteRule R);
78 
79 private:
80   transformer::RewriteRule Rule;
81   IncludeInserter Inserter;
82 };
83 
84 } // namespace utils
85 } // namespace tidy
86 } // namespace clang
87 
88 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_TRANSFORMER_CLANG_TIDY_CHECK_H
89