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