clang 20.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.
 
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.
 
std::vector< ast_matchers::internal::DynTypedMatcher > buildMatchers (const RewriteRuleBase &Rule)
 Builds a set of matchers that cover the rule.
 
SourceLocation getRuleMatchLoc (const ast_matchers::MatchFinder::MatchResult &Result)
 Gets the beginning location of the source matched by a rewrite rule.
 
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.
 
Stencil makeStencil (llvm::StringRef Text)
 Convenience function to construct a Stencil.
 
Stencil makeStencil (RangeSelector Selector)
 
Stencil makeStencil (Stencil S)
 
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, rather than generating a Transformer combinator.
 
llvm::Expected< SmallVector< Edit, 1 > > rewriteDescendants (const Stmt &Node, RewriteRule Rule, const ast_matchers::MatchFinder::MatchResult &Result)
 
llvm::Expected< SmallVector< Edit, 1 > > rewriteDescendants (const TypeLoc &Node, RewriteRule Rule, const ast_matchers::MatchFinder::MatchResult &Result)
 
llvm::Expected< SmallVector< Edit, 1 > > rewriteDescendants (const DynTypedNode &Node, RewriteRule Rule, const ast_matchers::MatchFinder::MatchResult &Result)
 

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 442 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 410 of file RewriteRule.cpp.

References hasValidKind(), clang::transformer::RootID, taggedMatchers(), and clang::TK_AsIs.

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 465 of file RewriteRule.cpp.

◆ 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 448 of file RewriteRule.cpp.

References clang::tooling::getFileRangeForEdit(), 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 168 of file RewriteRule.h.

References E, and clang::transformer::edit().

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

◆ injectEdits() [2/2]

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

Definition at line 169 of file RewriteRule.h.

◆ makeEditGenerator() [1/3]

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

Definition at line 190 of file RewriteRule.cpp.

References clang::transformer::edit().

◆ makeEditGenerator() [2/3]

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

Definition at line 318 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 186 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 430 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);
DynTypedNode Node
Stmt - This represents one statement.
Definition: Stmt.h:84
const internal::VariadicDynCastAllOfMatcher< Decl, VarDecl > varDecl
Matches variable declarations.
const internal::VariadicDynCastAllOfMatcher< Stmt, DeclRefExpr > declRefExpr
Matches expressions that refer to declarations.
internal::Matcher< NamedDecl > hasName(StringRef Name)
Matches NamedDecl nodes that have the specified name.
Definition: ASTMatchers.h:3079
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,...
RewriteRule makeRule(ast_matchers::internal::DynTypedMatcher M, EditGenerator Edits)
ASTEdit changeTo(RangeSelector Target, TextGenerator Replacement)
Replaces a portion of the source text with Replacement.
Stencil cat(Ts &&... Parts)
Concatenates 0+ stencil pieces into a single stencil.
Definition: Stencil.h:64

Definition at line 310 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 316 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 322 of file RewriteRule.cpp.

References Node, and rewriteDescendantsImpl().