clang  15.0.0git
Functions
clang::transformer::detail Namespace Reference

The following three functions are a low-level part of the RewriteRule API. More...

Functions

EditGenerator injectEdits (ASTEdit E)
 Helper function to construct an EditGenerator. More...
 
EditGenerator injectEdits (EditGenerator G)
 
RewriteRule makeRule (ast_matchers::internal::DynTypedMatcher M, EditGenerator Edits)
 
template<typename MetadataT >
RewriteRuleWith< MetadataT > makeRule (ast_matchers::internal::DynTypedMatcher M, EditGenerator Edits, Generator< MetadataT > Metadata)
 
EditGenerator makeEditGenerator (EditGenerator Edits)
 
EditGenerator makeEditGenerator (llvm::SmallVector< ASTEdit, 1 > Edits)
 
EditGenerator makeEditGenerator (ASTEdit Edit)
 
ast_matchers::internal::DynTypedMatcher buildMatcher (const RewriteRuleBase &Rule)
 Builds a single matcher for the rule, covering all of the rule's cases. More...
 
std::vector< ast_matchers::internal::DynTypedMatcher > buildMatchers (const RewriteRuleBase &Rule)
 Builds a set of matchers that cover the rule. More...
 
SourceLocation getRuleMatchLoc (const ast_matchers::MatchFinder::MatchResult &Result)
 Gets the beginning location of the source matched by a rewrite rule. More...
 
size_t findSelectedCase (const ast_matchers::MatchFinder::MatchResult &Result, const RewriteRuleBase &Rule)
 Returns the index of the Case of Rule that was selected in the match result. More...
 
Stencil makeStencil (llvm::StringRef Text)
 Convenience function to construct a Stencil. More...
 
Stencil makeStencil (RangeSelector Selector)
 
Stencil makeStencil (Stencil S)
 

Detailed Description

The following three functions are a low-level part of the RewriteRule API.

We expose them for use in implementing the fixtures that interpret RewriteRule, like Transformer and TransfomerTidy, or for more advanced users.

Function Documentation

◆ buildMatcher()

DynTypedMatcher clang::transformer::detail::buildMatcher ( const RewriteRuleBase Rule)

Builds a single matcher for the rule, covering all of the rule's cases.

Only supports Rules whose cases' matchers share the same base "kind" (Stmt, Decl, etc.) Deprecated: use buildMatchers instead, which supports mixing matchers of different kinds.

Definition at line 426 of file RewriteRule.cpp.

References buildMatchers().

◆ buildMatchers()

std::vector< DynTypedMatcher > clang::transformer::detail::buildMatchers ( const RewriteRuleBase Rule)

Builds a set of matchers that cover the rule.

One matcher is built for each distinct node matcher base kind: Stmt, Decl, etc. Node-matchers for QualType and Type are not permitted, since such nodes carry no source location information and are therefore not relevant for rewriting. If any such matchers are included, will return an empty vector.

Definition at line 394 of file RewriteRule.cpp.

References clang::transformer::RewriteRuleBase::Cases, and hasValidKind().

Referenced by buildMatcher().

◆ findSelectedCase()

size_t clang::transformer::detail::findSelectedCase ( const ast_matchers::MatchFinder::MatchResult Result,
const RewriteRuleBase Rule 
)

Returns the index of the Case of Rule that was selected in the match result.

Assumes a matcher built with buildMatcher.

Definition at line 449 of file RewriteRule.cpp.

References clang::transformer::RewriteRuleBase::Cases, and string().

◆ getRuleMatchLoc()

SourceLocation clang::transformer::detail::getRuleMatchLoc ( const ast_matchers::MatchFinder::MatchResult Result)

Gets the beginning location of the source matched by a rewrite rule.

If the match occurs within a macro expansion, returns the beginning of the expansion point. Result must come from the matching of a rewrite rule.

Definition at line 432 of file RewriteRule.cpp.

References clang::tooling::getRangeForEdit(), clang::CharSourceRange::getTokenRange(), and clang::transformer::RootID.

◆ injectEdits() [1/2]

EditGenerator clang::transformer::detail::injectEdits ( ASTEdit  E)
inline

Helper function to construct an EditGenerator.

Overloaded for common cases so that user doesn't need to specify which factory function to use. This pattern gives benefits similar to implicit constructors, while maintaing a higher degree of explicitness.

Definition at line 163 of file RewriteRule.h.

References clang::transformer::edit().

Referenced by clang::transformer::flatten().

◆ injectEdits() [2/2]

EditGenerator clang::transformer::detail::injectEdits ( EditGenerator  G)
inline

Definition at line 164 of file RewriteRule.h.

◆ makeEditGenerator() [1/3]

EditGenerator clang::transformer::detail::makeEditGenerator ( ASTEdit  Edit)

Definition at line 174 of file RewriteRule.cpp.

References clang::transformer::edit().

◆ makeEditGenerator() [2/3]

EditGenerator clang::transformer::detail::makeEditGenerator ( EditGenerator  Edits)
inline

Definition at line 313 of file RewriteRule.h.

Referenced by clang::transformer::makeRule().

◆ makeEditGenerator() [3/3]

EditGenerator clang::transformer::detail::makeEditGenerator ( llvm::SmallVector< ASTEdit, 1 >  Edits)

Definition at line 170 of file RewriteRule.cpp.

References clang::transformer::editList().

◆ makeRule() [1/2]

RewriteRule clang::transformer::detail::makeRule ( ast_matchers::internal::DynTypedMatcher  M,
EditGenerator  Edits 
)

◆ makeRule() [2/2]

template<typename MetadataT >
RewriteRuleWith<MetadataT> clang::transformer::detail::makeRule ( ast_matchers::internal::DynTypedMatcher  M,
EditGenerator  Edits,
Generator< MetadataT >  Metadata 
)

◆ makeStencil() [1/3]

Stencil clang::transformer::detail::makeStencil ( llvm::StringRef  Text)

Convenience function to construct a Stencil.

Overloaded for common cases so that user doesn't need to specify which factory function to use. This pattern gives benefits similar to implicit constructors, while maintaing a higher degree of explicitness.

Referenced by clang::transformer::access(), clang::transformer::cat(), and clang::transformer::ifBound().

◆ makeStencil() [2/3]

Stencil clang::transformer::detail::makeStencil ( RangeSelector  Selector)

Definition at line 429 of file Stencil.cpp.

◆ makeStencil() [3/3]

Stencil clang::transformer::detail::makeStencil ( Stencil  S)
inline

Definition at line 55 of file Stencil.h.

◆ rewriteDescendants() [1/4]

llvm::Expected< SmallVector< clang::transformer::Edit, 1 > > clang::transformer::detail::rewriteDescendants ( const Decl Node,
RewriteRule  Rule,
const ast_matchers::MatchFinder::MatchResult Result 
)

The following overload set is a version of rewriteDescendants that operates directly on the AST, rather than generating a Transformer combinator.

It applies Rule to all descendants of Node, although not Node itself. Rule can refer to nodes bound in Result.

For example, assuming that "body" is bound to a function body in MatchResult Results, this will produce edits to change all appearances of x in that body to 3.

auto InlineX =
const auto *Node = Results.Nodes.getNodeAs<Stmt>("body");
auto Edits = rewriteDescendants(*Node, InlineX, Results);

Definition at line 294 of file RewriteRule.cpp.

References Node, and rewriteDescendantsImpl().

Referenced by clang::transformer::rewriteDescendants().

◆ rewriteDescendants() [2/4]

llvm::Expected< SmallVector< clang::transformer::Edit, 1 > > clang::transformer::detail::rewriteDescendants ( const DynTypedNode Node,
RewriteRule  Rule,
const ast_matchers::MatchFinder::MatchResult Result 
)

◆ rewriteDescendants() [3/4]

llvm::Expected< SmallVector< clang::transformer::Edit, 1 > > clang::transformer::detail::rewriteDescendants ( const Stmt Node,
RewriteRule  Rule,
const ast_matchers::MatchFinder::MatchResult Result 
)

Definition at line 300 of file RewriteRule.cpp.

References Node, and rewriteDescendantsImpl().

◆ rewriteDescendants() [4/4]

llvm::Expected< SmallVector< clang::transformer::Edit, 1 > > clang::transformer::detail::rewriteDescendants ( const TypeLoc Node,
RewriteRule  Rule,
const ast_matchers::MatchFinder::MatchResult Result 
)

Definition at line 306 of file RewriteRule.cpp.

References Node, and rewriteDescendantsImpl().

clang::transformer::cat
Stencil cat(Ts &&... Parts)
Concatenates 0+ stencil pieces into a single stencil.
Definition: Stencil.h:64
Node
DynTypedNode Node
Definition: ASTMatchFinder.cpp:68
clang::transformer::changeTo
ASTEdit changeTo(RangeSelector Target, TextGenerator Replacement)
Replaces a portion of the source text with Replacement.
Definition: RewriteRule.cpp:117
clang::ast_matchers::declRefExpr
const internal::VariadicDynCastAllOfMatcher< Stmt, DeclRefExpr > declRefExpr
Matches expressions that refer to declarations.
Definition: ASTMatchersInternal.cpp:891
clang::transformer::detail::rewriteDescendants
llvm::Expected< SmallVector< Edit, 1 > > rewriteDescendants(const Decl &Node, RewriteRule Rule, const ast_matchers::MatchFinder::MatchResult &Result)
The following overload set is a version of rewriteDescendants that operates directly on the AST,...
Definition: RewriteRule.cpp:294
clang::ast_matchers::hasName
internal::Matcher< NamedDecl > hasName(StringRef Name)
Matches NamedDecl nodes that have the specified name.
Definition: ASTMatchers.h:3000
clang::transformer::detail::makeRule
RewriteRule makeRule(ast_matchers::internal::DynTypedMatcher M, EditGenerator Edits)
clang::ast_matchers::varDecl
const internal::VariadicDynCastAllOfMatcher< Decl, VarDecl > varDecl
Matches variable declarations.
Definition: ASTMatchersInternal.cpp:802