clang-tools
17.0.0git
llvm-project
clang-tools-extra
clang-tidy
utils
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
#include <optional>
18
19
namespace
clang::tidy::utils
{
20
21
/// A base class for defining a ClangTidy check based on a `RewriteRule`.
22
//
23
// For example, given a rule `MyCheckAsRewriteRule`, one can define a tidy check
24
// as follows:
25
//
26
// class MyCheck : public TransformerClangTidyCheck {
27
// public:
28
// MyCheck(StringRef Name, ClangTidyContext *Context)
29
// : TransformerClangTidyCheck(MyCheckAsRewriteRule, Name, Context) {}
30
// };
31
//
32
// `TransformerClangTidyCheck` recognizes this clang-tidy option:
33
//
34
// * IncludeStyle. A string specifying which file naming convention is used by
35
// the source code, 'llvm' or 'google'. Default is 'llvm'. The naming
36
// convention influences how canonical headers are distinguished from other
37
// includes.
38
class
TransformerClangTidyCheck
:
public
ClangTidyCheck
{
39
public
:
40
TransformerClangTidyCheck
(StringRef
Name
,
ClangTidyContext
*Context);
41
42
/// DEPRECATED: prefer the two argument constructor in conjunction with
43
/// \c setRule.
44
///
45
/// \p MakeRule generates the rewrite rule to be used by the check, based on
46
/// the given language and clang-tidy options. It can return \c std::nullopt
47
/// to handle cases where the options disable the check.
48
///
49
/// See \c setRule for constraints on the rule.
50
TransformerClangTidyCheck
(
51
std::function<std::optional<transformer::RewriteRuleWith<std::string>>(
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
/// dependencies.
58
TransformerClangTidyCheck
(transformer::RewriteRuleWith<std::string> R,
59
StringRef
Name
,
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::RewriteRuleWith<std::string> R);
78
79
private
:
80
transformer::RewriteRuleWith<std::string> Rule;
81
IncludeInserter
Inserter;
82
};
83
84
}
// namespace clang::tidy::utils
85
86
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_TRANSFORMER_CLANG_TIDY_CHECK_H
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition:
ClangTidyOptions.h:114
clang::tidy::utils::IncludeInserter
Produces fixes to insert specified includes to source files, if not yet present.
Definition:
IncludeInserter.h:55
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:103
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition:
ClangTidyCheck.h:53
clang::tidy::utils::TransformerClangTidyCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) final
Override this to register AST matchers with Finder.
Definition:
TransformerClangTidyCheck.cpp:96
clang::tidy::utils::TransformerClangTidyCheck::TransformerClangTidyCheck
TransformerClangTidyCheck(StringRef Name, ClangTidyContext *Context)
Definition:
TransformerClangTidyCheck.cpp:57
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition:
ClangTidyDiagnosticConsumer.h:68
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:91
Name
Token Name
Definition:
MacroToEnumCheck.cpp:87
clang::tidy::utils
Definition:
Aliasing.cpp:14
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition:
BadSignalToKillThreadCheck.cpp:28
clang::tidy::utils::TransformerClangTidyCheck
A base class for defining a ClangTidy check based on a RewriteRule.
Definition:
TransformerClangTidyCheck.h:38
IncludeSorter.h
clang::tidy::utils::TransformerClangTidyCheck::setRule
void setRule(transformer::RewriteRuleWith< std::string > R)
Set the rule that this check implements.
Definition:
TransformerClangTidyCheck.cpp:85
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:153
IncludeInserter.h
clang::tidy::ClangTidyCheck::OptionsView
Provides access to the ClangTidyCheck options via check-local names.
Definition:
ClangTidyCheck.h:139
Generated on Sat Jan 28 2023 08:31:31 for clang-tools by
1.8.17