clang-tools 10.0.0

TransformerClangTidyCheck.h
Go to the documentation of this file.
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"
13 #include "../utils/IncludeInserter.h"
14 #include "clang/ASTMatchers/ASTMatchFinder.h"
15 #include "clang/Frontend/CompilerInstance.h"
16 #include "clang/Tooling/Transformer/Transformer.h"
17 #include <deque>
18 #include <vector>
19
20 namespace clang {
21 namespace tidy {
22 namespace 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 // };
34 class TransformerClangTidyCheck : public ClangTidyCheck {
35 public:
36 // \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<transformer::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.
54 TransformerClangTidyCheck(transformer::RewriteRule R, StringRef Name,
55 ClangTidyContext *Context);
56
57 void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
58 Preprocessor *ModuleExpanderPP) override;
59 void registerMatchers(ast_matchers::MatchFinder *Finder) final;
60 void check(const ast_matchers::MatchFinder::MatchResult &Result) final;
61
62 private:
63 Optional<transformer::RewriteRule> Rule;
64 std::unique_ptr<clang::tidy::utils::IncludeInserter> Inserter;
65 };
66
67 } // namespace utils
68 } // namespace tidy
69 } // namespace clang
70
71 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_TRANSFORMER_CLANG_TIDY_CHECK_H
clang::tidy::ClangTidyCheck::OptionsView
Provides access to the ClangTidyCheck options via check-local names.
Definition: ClangTidyCheck.h:108
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::utils::TransformerClangTidyCheck::registerPPCallbacks
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
Definition: TransformerClangTidyCheck.cpp:49
clang::tidy::utils::TransformerClangTidyCheck::TransformerClangTidyCheck
TransformerClangTidyCheck(std::function< Optional< transformer::RewriteRule >(const LangOptions &, const OptionsView &)> MakeRule, StringRef Name, ClangTidyContext *Context)
clang::tidy::utils::TransformerClangTidyCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) final
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: TransformerClangTidyCheck.cpp:69
clang::tidy::utils::TransformerClangTidyCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) final
Override this to register AST matchers with Finder.
Definition: TransformerClangTidyCheck.cpp:62
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::utils::TransformerClangTidyCheck
Definition: TransformerClangTidyCheck.h:34
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:28
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77