clang-tools  11.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 "../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.
40 public:
41  // \p MakeRule generates the rewrite rule to be used by the check, based on
42  // the given language and clang-tidy options. It can return \c None to handle
43  // cases where the options disable the check.
44  //
45  // All cases in the rule generated by \p MakeRule must have a non-null \c
46  // Explanation, even though \c Explanation is optional for RewriteRule in
47  // general. Because the primary purpose of clang-tidy checks is to provide
48  // users with diagnostics, we assume that a missing explanation is a bug. If
49  // no explanation is desired, indicate that explicitly (for example, by
50  // passing `text("no explanation")` to `makeRule` as the `Explanation`
51  // argument).
52  TransformerClangTidyCheck(std::function<Optional<transformer::RewriteRule>(
53  const LangOptions &, const OptionsView &)>
54  MakeRule,
55  StringRef Name, ClangTidyContext *Context);
56 
57  // Convenience overload of the constructor when the rule doesn't depend on any
58  // of the language or clang-tidy options.
59  TransformerClangTidyCheck(transformer::RewriteRule R, StringRef Name,
60  ClangTidyContext *Context);
61 
62  void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
63  Preprocessor *ModuleExpanderPP) override;
64  void registerMatchers(ast_matchers::MatchFinder *Finder) final;
65  void check(const ast_matchers::MatchFinder::MatchResult &Result) final;
66 
67  /// Derived classes that override this function should call this method from
68  /// the overridden method.
69  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
70 
71 private:
72  Optional<transformer::RewriteRule> Rule;
73  const IncludeSorter::IncludeStyle IncludeStyle;
74  std::unique_ptr<IncludeInserter> Inserter;
75 };
76 
77 } // namespace utils
78 } // namespace tidy
79 } // namespace clang
80 
81 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_TRANSFORMER_CLANG_TIDY_CHECK_H
clang::tidy::abseil::MakeRule
static llvm::Optional< transformer::RewriteRule > MakeRule(const LangOptions &LangOpts, const ClangTidyCheck::OptionsView &Options)
Definition: StringFindStrContainsCheck.cpp:39
clang::tidy::utils::IncludeSorter::IncludeStyle
IncludeStyle
Supported include styles.
Definition: IncludeSorter.h:26
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:74
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:114
clang::tidy::utils::TransformerClangTidyCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) final
Override this to register AST matchers with Finder.
Definition: TransformerClangTidyCheck.cpp:67
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:76
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
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:54
clang::tidy::utils::TransformerClangTidyCheck::TransformerClangTidyCheck
TransformerClangTidyCheck(std::function< Optional< transformer::RewriteRule >(const LangOptions &, const OptionsView &)> MakeRule, StringRef Name, ClangTidyContext *Context)
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:29
clang::tidy::utils::TransformerClangTidyCheck
Definition: TransformerClangTidyCheck.h:39
IncludeSorter.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::utils::TransformerClangTidyCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Derived classes that override this function should call this method from the overridden method.
Definition: TransformerClangTidyCheck.cpp:111
IncludeInserter.h
clang::tidy::ClangTidyCheck::OptionsView
Provides access to the ClangTidyCheck options via check-local names.
Definition: ClangTidyCheck.h:191
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:111