clang  14.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)
 
ast_matchers::internal::DynTypedMatcher buildMatcher (const RewriteRule &Rule)
 Builds a single matcher for the rule, covering all of the rule's cases. More...
 
std::vector< ast_matchers::internal::DynTypedMatcher > buildMatchers (const RewriteRule &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...
 
const RewriteRule::CasefindSelectedCase (const ast_matchers::MatchFinder::MatchResult &Result, const RewriteRule &Rule)
 Returns 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 RewriteRule 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 408 of file RewriteRule.cpp.

References buildMatchers().

◆ buildMatchers()

std::vector< DynTypedMatcher > clang::transformer::detail::buildMatchers ( const RewriteRule 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 377 of file RewriteRule.cpp.

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

Referenced by buildMatcher().

◆ findSelectedCase()

const RewriteRule::Case & clang::transformer::detail::findSelectedCase ( const ast_matchers::MatchFinder::MatchResult Result,
const RewriteRule Rule 
)

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

Assumes a matcher built with buildMatcher.

Definition at line 432 of file RewriteRule.cpp.

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

Referenced by clang::tooling::Transformer::run().

◆ 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 414 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 161 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 162 of file RewriteRule.h.

◆ 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 485 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 279 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 285 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 291 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
clang::transformer::makeRule
RewriteRule makeRule(ast_matchers::internal::DynTypedMatcher M, EditGenerator Edits, TextGenerator Explanation=nullptr)
Constructs a simple RewriteRule.
Node
DynTypedNode Node
Definition: ASTMatchFinder.cpp:67
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:890
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:279
clang::ast_matchers::hasName
internal::Matcher< NamedDecl > hasName(StringRef Name)
Matches NamedDecl nodes that have the specified name.
Definition: ASTMatchers.h:2989
clang::ast_matchers::varDecl
const internal::VariadicDynCastAllOfMatcher< Decl, VarDecl > varDecl
Matches variable declarations.
Definition: ASTMatchersInternal.cpp:801