clang  14.0.0git
Namespaces | Classes | Typedefs | Enumerations | Functions | Variables
clang::transformer Namespace Reference

Namespaces

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

Classes

struct  ASTEdit
 
struct  Edit
 A concrete description of a source edit, represented by a character range in the source to be replaced and a corresponding replacement string. More...
 
class  MatchComputation
 A failable computation over nodes bound by AST matchers, with (limited) reflection via the toString method. More...
 
struct  RewriteRule
 Description of a source-code transformation. More...
 

Typedefs

template<typename T >
using MatchConsumer = std::function< Expected< T >(const ast_matchers::MatchFinder::MatchResult &)>
 A failable computation over nodes bound by AST matchers. More...
 
using RangeSelector = MatchConsumer< CharSourceRange >
 
using EditGenerator = MatchConsumer< llvm::SmallVector< Edit, 1 > >
 Maps a match result to a list of concrete edits (with possible failure). More...
 
using TextGenerator = std::shared_ptr< MatchComputation< std::string > >
 
using AnyGenerator = MatchConsumer< llvm::Any >
 
using StencilInterface = MatchComputation< std::string >
 
using Stencil = std::shared_ptr< StencilInterface >
 A sequence of code fragments, references to parameters and code-generation operations that together can be evaluated to (a fragment of) source code or a diagnostic message, given a match result. More...
 

Enumerations

enum  EditKind { EditKind::Range, EditKind::AddInclude }
 
enum  IncludeFormat { IncludeFormat::Quoted, IncludeFormat::Angled }
 Format of the path in an include directive – angle brackets or quotes. More...
 

Functions

llvm::Error notBoundError (llvm::StringRef Id)
 Creates an error that signals that a MatchConsumer expected a certain node to be bound by AST matchers, but it was not actually bound. More...
 
template<typename T >
MatchConsumer< T > ifBound (std::string ID, MatchConsumer< T > TrueC, MatchConsumer< T > FalseC)
 Chooses between the two consumers, based on whether ID is bound in the match. More...
 
llvm::Expected< RangeSelectorparseRangeSelector (llvm::StringRef Input)
 Parses a string representation of a RangeSelector. More...
 
RangeSelector charRange (CharSourceRange R)
 
RangeSelector enclose (RangeSelector Begin, RangeSelector End)
 Selects from the start of Begin and to the end of End. More...
 
RangeSelector encloseNodes (std::string BeginID, std::string EndID)
 Convenience version of range where end-points are bound nodes. More...
 
RangeSelector range (RangeSelector Begin, RangeSelector End)
 DEPRECATED. Use enclose. More...
 
RangeSelector range (std::string BeginID, std::string EndID)
 DEPRECATED. Use encloseNodes. More...
 
RangeSelector before (RangeSelector Selector)
 Selects the (empty) range [B,B) when Selector selects the range [B,E). More...
 
RangeSelector after (RangeSelector Selector)
 Selects the point immediately following Selector. More...
 
RangeSelector between (RangeSelector R1, RangeSelector R2)
 Selects the range between R1 and `R2. More...
 
RangeSelector node (std::string ID)
 Selects a node, including trailing semicolon, if any (for declarations and non-expression statements). More...
 
RangeSelector statement (std::string ID)
 Selects a node, including trailing semicolon (always). More...
 
RangeSelector member (std::string ID)
 Given a MemberExpr, selects the member token. More...
 
RangeSelector name (std::string ID)
 Given a node with a "name", (like NamedDecl, DeclRefExpr, CxxCtorInitializer, and TypeLoc) selects the name's token. More...
 
RangeSelector callArgs (std::string ID)
 
RangeSelector statements (std::string ID)
 
RangeSelector initListElements (std::string ID)
 
RangeSelector elseBranch (std::string ID)
 Given an \IfStmt (bound to ID), selects the range of the else branch, starting from the else keyword. More...
 
RangeSelector expansion (RangeSelector S)
 Selects the range from which S was expanded (possibly along with other source), if S is an expansion, and S itself, otherwise. More...
 
EditGenerator edit (ASTEdit E)
 Generates a single (specified) edit. More...
 
EditGenerator editList (llvm::SmallVector< ASTEdit, 1 > Edits)
 Lifts a list of ASTEdits into an EditGenerator. More...
 
EditGenerator noEdits ()
 Generates no edits. More...
 
EditGenerator noopEdit (RangeSelector Anchor)
 Generates a single, no-op edit anchored at the start location of the specified range. More...
 
EditGenerator ifBound (std::string ID, ASTEdit TrueEdit, ASTEdit FalseEdit)
 Version of ifBound specialized to ASTEdit. More...
 
EditGenerator ifBound (std::string ID, ASTEdit TrueEdit)
 Version of ifBound that has no "False" branch. More...
 
EditGenerator flattenVector (SmallVector< EditGenerator, 2 > Generators)
 Flattens a list of generators into a single generator whose elements are the concatenation of the results of the argument generators. More...
 
template<typename... Ts>
EditGenerator flatten (Ts &&...Edits)
 
ASTEdit changeTo (RangeSelector Target, TextGenerator Replacement)
 Replaces a portion of the source text with Replacement. More...
 
ASTEdit change (RangeSelector Target, TextGenerator Replacement)
 DEPRECATED: use changeTo. More...
 
ASTEdit changeTo (TextGenerator Replacement)
 Replaces the entirety of a RewriteRule's match with Replacement. More...
 
ASTEdit change (TextGenerator Replacement)
 DEPRECATED: use changeTo. More...
 
ASTEdit insertBefore (RangeSelector S, TextGenerator Replacement)
 Inserts Replacement before S, leaving the source selected by \S unchanged. More...
 
ASTEdit insertAfter (RangeSelector S, TextGenerator Replacement)
 Inserts Replacement after S, leaving the source selected by \S unchanged. More...
 
ASTEdit remove (RangeSelector S)
 Removes the source selected by S. More...
 
ASTEdit addInclude (RangeSelector Target, StringRef Header, IncludeFormat Format=IncludeFormat::Quoted)
 Adds an include directive for the given header to the file of Target. More...
 
ASTEdit addInclude (StringRef Header, IncludeFormat Format=IncludeFormat::Quoted)
 Adds an include directive for the given header to the file associated with RootID. More...
 
template<typename Callable >
ASTEdit withMetadata (ASTEdit Edit, Callable Metadata)
 
EditGenerator shrinkTo (RangeSelector outer, RangeSelector inner)
 Assuming that the inner range is enclosed by the outer range, creates precision edits to remove the parts of the outer range that are not included in the inner range. More...
 
RewriteRule makeRule (ast_matchers::internal::DynTypedMatcher M, EditGenerator Edits, TextGenerator Explanation=nullptr)
 Constructs a simple RewriteRule. More...
 
RewriteRule makeRule (ast_matchers::internal::DynTypedMatcher M, llvm::SmallVector< ASTEdit, 1 > Edits, TextGenerator Explanation=nullptr)
 Constructs a RewriteRule from multiple ASTEdits. More...
 
RewriteRule makeRule (ast_matchers::internal::DynTypedMatcher M, ASTEdit Edit, TextGenerator Explanation=nullptr)
 Overload of makeRule for common case of only one edit. More...
 
void addInclude (RewriteRule &Rule, llvm::StringRef Header, IncludeFormat Format=IncludeFormat::Quoted)
 For every case in Rule, adds an include directive for the given header. More...
 
RewriteRule applyFirst (ArrayRef< RewriteRule > Rules)
 Applies the first rule whose pattern matches; other rules are ignored. More...
 
EditGenerator rewriteDescendants (std::string NodeId, RewriteRule Rule)
 Applies Rule to all descendants of the node bound to NodeId. More...
 
Stencil catVector (std::vector< Stencil > Parts)
 Constructs the string representing the concatenation of the given Parts. More...
 
template<typename... Ts>
Stencil cat (Ts &&... Parts)
 Concatenates 0+ stencil pieces into a single stencil. More...
 
Stencil expression (llvm::StringRef Id)
 Generates the source of the expression bound to Id, wrapping it in parentheses if it may parse differently depending on context. More...
 
Stencil deref (llvm::StringRef ExprId)
 Constructs an idiomatic dereferencing of the expression bound to ExprId. More...
 
Stencil maybeDeref (llvm::StringRef ExprId)
 If ExprId is of pointer type, constructs an idiomatic dereferencing of the expression bound to ExprId, including wrapping it in parentheses, if needed. More...
 
Stencil addressOf (llvm::StringRef ExprId)
 Constructs an expression that idiomatically takes the address of the expression bound to ExprId. More...
 
Stencil maybeAddressOf (llvm::StringRef ExprId)
 If ExprId is not a pointer type, constructs an expression that idiomatically takes the address of the expression bound to ExprId, including wrapping ExprId in parentheses, if needed. More...
 
Stencil access (llvm::StringRef BaseId, Stencil Member)
 Constructs a MemberExpr that accesses the named member (Member) of the object bound to BaseId. More...
 
Stencil access (llvm::StringRef BaseId, llvm::StringRef Member)
 
Stencil ifBound (llvm::StringRef Id, Stencil TrueStencil, Stencil FalseStencil)
 Chooses between the two stencil parts, based on whether ID is bound in the match. More...
 
Stencil ifBound (llvm::StringRef Id, llvm::StringRef TrueText, llvm::StringRef FalseText)
 Chooses between the two strings, based on whether ID is bound in the match. More...
 
Stencil run (MatchConsumer< std::string > C)
 Wraps a MatchConsumer in a Stencil, so that it can be used in a Stencil. More...
 
Stencil describe (llvm::StringRef Id)
 Produces a human-readable rendering of the node bound to Id, suitable for diagnostics and debugging. More...
 
Stencil dPrint (llvm::StringRef Id)
 For debug use only; semantics are not guaranteed. More...
 

Variables

const char RootID [] = "___root___"
 

Typedef Documentation

◆ AnyGenerator

using clang::transformer::AnyGenerator = typedef MatchConsumer<llvm::Any>

Definition at line 66 of file RewriteRule.h.

◆ EditGenerator

Maps a match result to a list of concrete edits (with possible failure).

This type is a building block of rewrite rules, but users will generally work in terms of ASTEdits (below) rather than directly in terms of EditGenerator.

Definition at line 62 of file RewriteRule.h.

◆ MatchConsumer

template<typename T >
using clang::transformer::MatchConsumer = typedef std::function<Expected<T>(const ast_matchers::MatchFinder::MatchResult &)>

A failable computation over nodes bound by AST matchers.

The computation should report any errors though its return value (rather than terminating the program) to enable usage in interactive scenarios like clang-query.

This is a central abstraction of the Transformer framework.

Definition at line 35 of file MatchConsumer.h.

◆ RangeSelector

Definition at line 27 of file RangeSelector.h.

◆ Stencil

using clang::transformer::Stencil = typedef std::shared_ptr<StencilInterface>

A sequence of code fragments, references to parameters and code-generation operations that together can be evaluated to (a fragment of) source code or a diagnostic message, given a match result.

We use a shared_ptr to allow for easy and cheap copying of stencils. Since StencilInterface is an immutable interface, the sharing doesn't impose any risks. Otherwise, we would have to add a virtual copy method to the API and implement it for all derived classes.

Definition at line 46 of file Stencil.h.

◆ StencilInterface

Definition at line 36 of file Stencil.h.

◆ TextGenerator

Definition at line 64 of file RewriteRule.h.

Enumeration Type Documentation

◆ EditKind

Enumerator
Range 
AddInclude 

Definition at line 35 of file RewriteRule.h.

◆ IncludeFormat

Format of the path in an include directive – angle brackets or quotes.

Enumerator
Quoted 
Angled 

Definition at line 53 of file RewriteRule.h.

Function Documentation

◆ access() [1/2]

Stencil clang::transformer::access ( llvm::StringRef  BaseId,
llvm::StringRef  Member 
)
inline

Definition at line 104 of file Stencil.h.

References access(), clang::transformer::detail::makeStencil(), and clang::Member.

◆ access() [2/2]

Stencil clang::transformer::access ( llvm::StringRef  BaseId,
Stencil  Member 
)

Constructs a MemberExpr that accesses the named member (Member) of the object bound to BaseId.

The access is constructed idiomatically: if BaseId is bound to e and Member identifies member m, then returns e->m, when e is a pointer, e2->m when e = *e2 and e.m otherwise. Additionally, e is wrapped in parentheses, if needed.

Referenced by access(), and clang::Sema::CheckFriendAccess().

◆ addInclude() [1/3]

ASTEdit clang::transformer::addInclude ( RangeSelector  Target,
StringRef  Header,
IncludeFormat  Format = IncludeFormat::Quoted 
)

Adds an include directive for the given header to the file of Target.

The particular location specified by Target is ignored.

Definition at line 160 of file RewriteRule.cpp.

References AddInclude, formatHeaderPath(), clang::transformer::ASTEdit::Kind, makeText(), clang::transformer::ASTEdit::Replacement, clang::Target, and clang::transformer::ASTEdit::TargetRange.

Referenced by addInclude().

◆ addInclude() [2/3]

void clang::transformer::addInclude ( RewriteRule Rule,
llvm::StringRef  Header,
IncludeFormat  Format = IncludeFormat::Quoted 
)

For every case in Rule, adds an include directive for the given header.

The common use is assumed to be a rule with only one case. For example, to replace a function call and add headers corresponding to the new code, one could write:

auto R = makeRule(callExpr(callee(functionDecl(hasName("foo")))),
changeTo(cat("bar()")));
addInclude(R, "path/to/bar_header.h");

◆ addInclude() [3/3]

ASTEdit clang::transformer::addInclude ( StringRef  Header,
IncludeFormat  Format = IncludeFormat::Quoted 
)
inline

Adds an include directive for the given header to the file associated with RootID.

If RootID matches inside a macro expansion, will add the directive to the file in which the macro was expanded (as opposed to the file in which the macro is defined).

Definition at line 219 of file RewriteRule.h.

References addInclude(), expansion(), node(), and RootID.

◆ addressOf()

Stencil clang::transformer::addressOf ( llvm::StringRef  ExprId)

Constructs an expression that idiomatically takes the address of the expression bound to ExprId.

ExprId is wrapped in parentheses, if needed.

Definition at line 440 of file Stencil.cpp.

References string().

◆ after()

RangeSelector clang::transformer::after ( RangeSelector  Selector)

Selects the point immediately following Selector.

That is, the (empty) range [E,E), when Selector selects either

  • the CharRange [B,E) or
  • the TokenRange [B,E'] where the token at E' spans the range [E',E).

Definition at line 115 of file RangeSelector.cpp.

References End, clang::CharSourceRange::getCharRange(), clang::Decl::getLangOpts(), clang::CharSourceRange::getTokenRange(), invalidArgumentError(), clang::Lexer::makeFileCharRange(), and Range.

Referenced by between(), getUnaryRangeSelectors(), clang::edit::Commit::insertWrap(), and shrinkTo().

◆ applyFirst()

RewriteRule clang::transformer::applyFirst ( ArrayRef< RewriteRule Rules)

Applies the first rule whose pattern matches; other rules are ignored.

If the matchers are independent then order doesn't matter. In that case, applyFirst is simply joining the set of rules into one.

Definition at line 369 of file RewriteRule.cpp.

References clang::transformer::RewriteRule::Cases.

◆ before()

RangeSelector clang::transformer::before ( RangeSelector  Selector)

Selects the (empty) range [B,B) when Selector selects the range [B,E).

Definition at line 106 of file RangeSelector.cpp.

References clang::CharSourceRange::getCharRange().

Referenced by between(), clang::CodeGen::CodeGenFunction::createBasicBlock(), getUnaryRangeSelectors(), clang::edit::Commit::insertWrap(), and shrinkTo().

◆ between()

RangeSelector clang::transformer::between ( RangeSelector  R1,
RangeSelector  R2 
)
inline

Selects the range between R1 and `R2.

Definition at line 60 of file RangeSelector.h.

References after(), before(), and enclose().

Referenced by getBinaryRangeSelectors().

◆ callArgs()

RangeSelector clang::transformer::callArgs ( std::string  ID)

◆ cat()

template<typename... Ts>
Stencil clang::transformer::cat ( Ts &&...  Parts)

Concatenates 0+ stencil pieces into a single stencil.

Arguments can be raw text, ranges in the matched code (RangeSelector) or other Stencils.

Definition at line 64 of file Stencil.h.

References catVector(), and clang::transformer::detail::makeStencil().

Referenced by clang::ASTContext::getVariableArrayDecayedType(), and clang::DiagnosticIDs::isARCDiagnostic().

◆ catVector()

Stencil clang::transformer::catVector ( std::vector< Stencil Parts)

Constructs the string representing the concatenation of the given Parts.

If only one element is passed in Parts, returns that element.

Definition at line 469 of file Stencil.cpp.

Referenced by cat().

◆ change() [1/2]

ASTEdit clang::transformer::change ( RangeSelector  Target,
TextGenerator  Replacement 
)
inline

DEPRECATED: use changeTo.

Definition at line 177 of file RewriteRule.h.

References changeTo(), and clang::Target.

◆ change() [2/2]

ASTEdit clang::transformer::change ( TextGenerator  Replacement)
inline

DEPRECATED: use changeTo.

Definition at line 191 of file RewriteRule.h.

References changeTo().

◆ changeTo() [1/2]

ASTEdit clang::transformer::changeTo ( RangeSelector  Target,
TextGenerator  Replacement 
)

Replaces a portion of the source text with Replacement.

Definition at line 117 of file RewriteRule.cpp.

References clang::transformer::ASTEdit::Replacement, clang::Target, and clang::transformer::ASTEdit::TargetRange.

Referenced by change(), and changeTo().

◆ changeTo() [2/2]

ASTEdit clang::transformer::changeTo ( TextGenerator  Replacement)
inline

Replaces the entirety of a RewriteRule's match with Replacement.

For example, to replace a function call, one could write:

changeTo(cat("bar()")))

Definition at line 187 of file RewriteRule.h.

References changeTo(), node(), and RootID.

◆ charRange()

RangeSelector clang::transformer::charRange ( CharSourceRange  R)
inline

Definition at line 29 of file RangeSelector.h.

◆ deref()

Stencil clang::transformer::deref ( llvm::StringRef  ExprId)

Constructs an idiomatic dereferencing of the expression bound to ExprId.

ExprId is wrapped in parentheses, if needed.

Definition at line 430 of file Stencil.cpp.

References string().

◆ describe()

Stencil clang::transformer::describe ( llvm::StringRef  Id)

Produces a human-readable rendering of the node bound to Id, suitable for diagnostics and debugging.

This operator can be applied to any node, but is targeted at those whose source cannot be printed directly, including:

  • Types. represented based on their structure. Note that namespace qualifiers are always printed, with the anonymous namespace represented explicitly. No desugaring or canonicalization is applied.

◆ dPrint()

Stencil clang::transformer::dPrint ( llvm::StringRef  Id)

For debug use only; semantics are not guaranteed.

Returns
the string resulting from calling the node's print() method.

◆ edit()

EditGenerator clang::transformer::edit ( ASTEdit  E)

Generates a single (specified) edit.

Definition at line 75 of file RewriteRule.cpp.

References translateEdits().

Referenced by clang::edit::EditedSource::commit(), ifBound(), clang::transformer::detail::injectEdits(), and makeRule().

◆ editList()

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

Lifts a list of ASTEdits into an EditGenerator.

The EditGenerator will return an empty vector if any of the edits apply to portions of the source that are ineligible for rewriting (certain interactions with macros, for example) and it will fail if any invariants are violated relating to bound nodes in the match. However, it does not fail in the case of conflicting edits – conflict handling is left to clients. We recommend use of the AtomicChange or Replacements classes for assistance in detecting such conflicts.

Definition at line 69 of file RewriteRule.cpp.

References translateEdits().

Referenced by makeRule(), noEdits(), and shrinkTo().

◆ elseBranch()

RangeSelector clang::transformer::elseBranch ( std::string  ID)

Given an \IfStmt (bound to ID), selects the range of the else branch, starting from the else keyword.

Definition at line 325 of file RangeSelector.cpp.

Referenced by getUnaryStringSelectors().

◆ enclose()

RangeSelector clang::transformer::enclose ( RangeSelector  Begin,
RangeSelector  End 
)

Selects from the start of Begin and to the end of End.

Definition at line 164 of file RangeSelector.cpp.

References Begin, End, and invalidArgumentError().

Referenced by between(), encloseNodes(), getBinaryRangeSelectors(), range(), and shrinkTo().

◆ encloseNodes()

RangeSelector clang::transformer::encloseNodes ( std::string  BeginID,
std::string  EndID 
)

Convenience version of range where end-points are bound nodes.

Definition at line 183 of file RangeSelector.cpp.

References enclose(), and node().

Referenced by getBinaryStringSelectors(), and range().

◆ expansion()

RangeSelector clang::transformer::expansion ( RangeSelector  S)

Selects the range from which S was expanded (possibly along with other source), if S is an expansion, and S itself, otherwise.

Corresponds to SourceManager::getExpansionRange.

Definition at line 329 of file RangeSelector.cpp.

Referenced by addInclude(), and getUnaryRangeSelectors().

◆ expression()

Stencil clang::transformer::expression ( llvm::StringRef  Id)

Generates the source of the expression bound to Id, wrapping it in parentheses if it may parse differently depending on context.

For example, a binary operation is always wrapped, while a variable reference is never wrapped.

Definition at line 425 of file Stencil.cpp.

References Id, and string().

◆ flatten()

template<typename... Ts>
EditGenerator clang::transformer::flatten ( Ts &&...  Edits)

Definition at line 165 of file RewriteRule.h.

References flattenVector(), and clang::transformer::detail::injectEdits().

◆ flattenVector()

EditGenerator clang::transformer::flattenVector ( SmallVector< EditGenerator, 2 >  Generators)

Flattens a list of generators into a single generator whose elements are the concatenation of the results of the argument generators.

Definition at line 100 of file RewriteRule.cpp.

Referenced by flatten().

◆ ifBound() [1/5]

Stencil clang::transformer::ifBound ( llvm::StringRef  Id,
llvm::StringRef  TrueText,
llvm::StringRef  FalseText 
)
inline

Chooses between the two strings, based on whether ID is bound in the match.

Definition at line 114 of file Stencil.h.

References Id, ifBound(), and clang::transformer::detail::makeStencil().

◆ ifBound() [2/5]

Stencil clang::transformer::ifBound ( llvm::StringRef  Id,
Stencil  TrueStencil,
Stencil  FalseStencil 
)

Chooses between the two stencil parts, based on whether ID is bound in the match.

◆ ifBound() [3/5]

EditGenerator clang::transformer::ifBound ( std::string  ID,
ASTEdit  TrueEdit 
)
inline

Version of ifBound that has no "False" branch.

If the node is not bound, then no edits are produced.

Definition at line 148 of file RewriteRule.h.

References edit(), ifBound(), and noEdits().

◆ ifBound() [4/5]

EditGenerator clang::transformer::ifBound ( std::string  ID,
ASTEdit  TrueEdit,
ASTEdit  FalseEdit 
)
inline

Version of ifBound specialized to ASTEdit.

Definition at line 140 of file RewriteRule.h.

References edit(), and ifBound().

◆ ifBound() [5/5]

template<typename T >
MatchConsumer<T> clang::transformer::ifBound ( std::string  ID,
MatchConsumer< T >  TrueC,
MatchConsumer< T >  FalseC 
)

Chooses between the two consumers, based on whether ID is bound in the match.

Definition at line 47 of file MatchConsumer.h.

Referenced by ifBound().

◆ initListElements()

RangeSelector clang::transformer::initListElements ( std::string  ID)

Definition at line 312 of file RangeSelector.cpp.

Referenced by getUnaryStringSelectors().

◆ insertAfter()

ASTEdit clang::transformer::insertAfter ( RangeSelector  S,
TextGenerator  Replacement 
)
inline

Inserts Replacement after S, leaving the source selected by \S unchanged.

Definition at line 203 of file RewriteRule.h.

◆ insertBefore()

ASTEdit clang::transformer::insertBefore ( RangeSelector  S,
TextGenerator  Replacement 
)
inline

Inserts Replacement before S, leaving the source selected by \S unchanged.

Definition at line 197 of file RewriteRule.h.

◆ makeRule() [1/3]

RewriteRule clang::transformer::makeRule ( ast_matchers::internal::DynTypedMatcher  M,
ASTEdit  Edit,
TextGenerator  Explanation = nullptr 
)
inline

Overload of makeRule for common case of only one edit.

Definition at line 304 of file RewriteRule.h.

References edit(), and makeRule().

◆ makeRule() [2/3]

RewriteRule clang::transformer::makeRule ( ast_matchers::internal::DynTypedMatcher  M,
EditGenerator  Edits,
TextGenerator  Explanation = nullptr 
)

Constructs a simple RewriteRule.

Referenced by makeRule().

◆ makeRule() [3/3]

RewriteRule clang::transformer::makeRule ( ast_matchers::internal::DynTypedMatcher  M,
llvm::SmallVector< ASTEdit, 1 >  Edits,
TextGenerator  Explanation = nullptr 
)
inline

Constructs a RewriteRule from multiple ASTEdits.

Definition at line 296 of file RewriteRule.h.

References editList(), and makeRule().

◆ maybeAddressOf()

Stencil clang::transformer::maybeAddressOf ( llvm::StringRef  ExprId)

If ExprId is not a pointer type, constructs an expression that idiomatically takes the address of the expression bound to ExprId, including wrapping ExprId in parentheses, if needed.

Otherwise, generates the original expression source.

Definition at line 445 of file Stencil.cpp.

References string().

◆ maybeDeref()

Stencil clang::transformer::maybeDeref ( llvm::StringRef  ExprId)

If ExprId is of pointer type, constructs an idiomatic dereferencing of the expression bound to ExprId, including wrapping it in parentheses, if needed.

Otherwise, generates the original expression source.

Definition at line 435 of file Stencil.cpp.

References string().

◆ member()

RangeSelector clang::transformer::member ( std::string  ID)

Given a MemberExpr, selects the member token.

ID is the node's binding in the match result.

Definition at line 188 of file RangeSelector.cpp.

References clang::DynTypedNode::get(), getNode(), clang::DynTypedNode::getNodeKind(), clang::CharSourceRange::getTokenRange(), Node, and typeError().

Referenced by clang::Sema::CheckNontrivialField(), getUnaryStringSelectors(), and isExplicitMemberSpecialization().

◆ name()

RangeSelector clang::transformer::name ( std::string  ID)

Given a node with a "name", (like NamedDecl, DeclRefExpr, CxxCtorInitializer, and TypeLoc) selects the name's token.

Only selects the final identifier of a qualified name, but not any qualifiers or template arguments. For example, for ::foo::bar::baz and ::foo::bar::baz<int>, it selects only baz.

Parameters
IDis the node's binding in the match result.

Definition at line 200 of file RangeSelector.cpp.

References clang::DynTypedNode::get(), clang::TypeLoc::getAs(), getNode(), clang::DynTypedNode::getNodeKind(), clang::TypeLoc::getSourceRange(), clang::tooling::getText(), clang::CharSourceRange::getTokenRange(), missingPropertyError(), Node, and typeError().

Referenced by clang::ObjCTypeParamDecl::Create(), clang::CodeGen::CodeGenFunction::createBasicBlock(), createLoadInstBefore(), emitArraySubscriptGEP(), clang::CodeGen::CodeGenFunction::emitBlockByrefAddress(), clang::CodeGen::CodeGenFunction::emitByrefStructureInit(), clang::CodeGen::CodeGenFunction::EmitRuntimeCallOrInvoke(), clang::Sema::findMacroSpelling(), clang::CodeGen::CodeGenFunction::GenerateBlockFunction(), clang::CodeGenerator::GetDeclForMangledName(), clang::Selector::getInstTypeMethodFamily(), clang::GetNullarySelector(), clang::NamedDecl::getObjCFStringFormattingFamily(), clang::CodeGen::CodeGenModule::getTerminateFn(), clang::GetUnarySelector(), getUnaryStringSelectors(), isDebuggingName(), clang::Sema::isKnownName(), LazyInitialize(), clang::MangleContext::mangleObjCMethodName(), MigrateBlockOrFunctionPointerTypeVariable(), clang::ento::CheckerManager::setCurrentCheckerName(), clang::ObjCDeclSpec::setGetterName(), clang::ObjCDeclSpec::setSetterName(), and startsWithWord().

◆ node()

RangeSelector clang::transformer::node ( std::string  ID)

◆ noEdits()

EditGenerator clang::transformer::noEdits ( )
inline

Generates no edits.

Definition at line 132 of file RewriteRule.h.

References editList().

Referenced by ifBound().

◆ noopEdit()

EditGenerator clang::transformer::noopEdit ( RangeSelector  Anchor)

Generates a single, no-op edit anchored at the start location of the specified range.

A noopEdit may be preferred over noEdits to associate a diagnostic Explanation with the rule.

Definition at line 81 of file RewriteRule.cpp.

References Begin, clang::CharSourceRange::getCharRange(), clang::transformer::Edit::Kind, Range, and clang::transformer::Edit::Range.

◆ notBoundError()

llvm::Error clang::transformer::notBoundError ( llvm::StringRef  Id)
inline

Creates an error that signals that a MatchConsumer expected a certain node to be bound by AST matchers, but it was not actually bound.

Definition at line 39 of file MatchConsumer.h.

References Id.

◆ parseRangeSelector()

Expected< RangeSelector > clang::transformer::parseRangeSelector ( llvm::StringRef  Input)

Parses a string representation of a RangeSelector.

The grammar of these strings is closely based on the (sub)grammar of RangeSelectors as they'd appear in C++ code. However, this language constrains the set of permissible strings (for node ids) – it does not support escapes in the string. Additionally, the charRange combinator is not supported, because there is no representation of values of type CharSourceRange in this (little) language.

Definition at line 268 of file Parsing.cpp.

References consumeWhitespace(), makeParseError(), parseRangeSelectorImpl(), and State.

◆ range() [1/2]

RangeSelector clang::transformer::range ( RangeSelector  Begin,
RangeSelector  End 
)
inline

DEPRECATED. Use enclose.

Definition at line 41 of file RangeSelector.h.

References Begin, enclose(), and End.

Referenced by applyRewrite(), clang::arcmt::TransformActions::clearAllDiagnostics(), clang::arcmt::CapturedDiagList::clearDiagnostic(), clang::arcmt::TransformActions::clearDiagnostic(), clang::Sema::EmitRelatedResultTypeNoteForReturn(), clang::DependentAddressSpaceTypeLoc::getLocalSourceRange(), clang::MatrixTypeLoc::getLocalSourceRange(), clang::arcmt::CapturedDiagList::hasDiagnostic(), clang::arcmt::TransformActions::hasDiagnostic(), clang::arcmt::TransformActions::increaseIndentation(), clang::Rewriter::IncreaseIndentation(), clang::edit::Commit::insertFromRange(), clang::edit::Commit::insertWrap(), clang::ento::BasicBugReport::Profile(), clang::ento::PathSensitiveBugReport::Profile(), clang::edit::EditsReceiver::remove(), clang::arcmt::TransformActions::remove(), clang::edit::Commit::remove(), clang::Rewriter::RemoveText(), clang::arcmt::TransformActions::replace(), clang::edit::Commit::replace(), clang::Rewriter::ReplaceText(), clang::edit::Commit::replaceWithInner(), clang::arcmt::TransformActions::report(), clang::arcmt::TransformActions::reportError(), clang::arcmt::TransformActions::reportNote(), clang::arcmt::TransformActions::reportWarning(), clang::DependentAddressSpaceTypeLoc::setAttrOperandParensRange(), clang::MatrixTypeLoc::setAttrOperandParensRange(), clang::TypeofLikeTypeLoc< TypeOfTypeLoc, TypeOfType, TypeOfTypeLocInfo >::setParensRange(), and clang::DeclSpec::setTypeofParensRange().

◆ range() [2/2]

RangeSelector clang::transformer::range ( std::string  BeginID,
std::string  EndID 
)
inline

DEPRECATED. Use encloseNodes.

Definition at line 46 of file RangeSelector.h.

References encloseNodes().

◆ remove()

ASTEdit clang::transformer::remove ( RangeSelector  S)

◆ rewriteDescendants()

EditGenerator clang::transformer::rewriteDescendants ( std::string  NodeId,
RewriteRule  Rule 
)

Applies Rule to all descendants of the node bound to NodeId.

Rule can refer to nodes bound by the calling rule. Rule is not applied to the node itself.

For example,

auto InlineX =
makeRule(functionDecl(hasName("f"), hasBody(stmt().bind("body"))).bind("f"),
changeTo(name("f"), cat("newName")),
rewriteDescendants("body", InlineX)));

Here, we find the function f, change its name to newName and change all appearances of x in its body to 3.

Definition at line 313 of file RewriteRule.cpp.

References clang::transformer::detail::rewriteDescendants().

◆ run()

Stencil clang::transformer::run ( MatchConsumer< std::string C)

Wraps a MatchConsumer in a Stencil, so that it can be used in a Stencil.

This supports user-defined extensions to the Stencil language.

Definition at line 465 of file Stencil.cpp.

◆ shrinkTo()

EditGenerator clang::transformer::shrinkTo ( RangeSelector  outer,
RangeSelector  inner 
)
inline

Assuming that the inner range is enclosed by the outer range, creates precision edits to remove the parts of the outer range that are not included in the inner range.

Definition at line 250 of file RewriteRule.h.

References after(), before(), editList(), enclose(), and remove().

◆ statement()

RangeSelector clang::transformer::statement ( std::string  ID)

Selects a node, including trailing semicolon (always).

Useful for selecting expression statements. ID is the node's binding in the match result.

Definition at line 154 of file RangeSelector.cpp.

References clang::tooling::getExtendedRange(), getNode(), and Node.

Referenced by clang::CFGBlock::appendStmt(), and getUnaryStringSelectors().

◆ statements()

RangeSelector clang::transformer::statements ( std::string  ID)

Definition at line 283 of file RangeSelector.cpp.

Referenced by getUnaryStringSelectors().

◆ withMetadata()

template<typename Callable >
ASTEdit clang::transformer::withMetadata ( ASTEdit  Edit,
Callable  Metadata 
)
inline

Definition at line 237 of file RewriteRule.h.

References clang::transformer::Edit::Metadata.

Variable Documentation

◆ RootID

const char clang::transformer::RootID = "___root___"
clang::ast_matchers::stmt
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
Definition: ASTMatchersInternal.cpp:796
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.
clang::transformer::flatten
EditGenerator flatten(Ts &&...Edits)
Definition: RewriteRule.h:165
clang::transformer::addInclude
ASTEdit addInclude(RangeSelector Target, StringRef Header, IncludeFormat Format=IncludeFormat::Quoted)
Adds an include directive for the given header to the file of Target.
Definition: RewriteRule.cpp:160
clang::transformer::changeTo
ASTEdit changeTo(RangeSelector Target, TextGenerator Replacement)
Replaces a portion of the source text with Replacement.
Definition: RewriteRule.cpp:117
clang::transformer::rewriteDescendants
EditGenerator rewriteDescendants(std::string NodeId, RewriteRule Rule)
Applies Rule to all descendants of the node bound to NodeId.
Definition: RewriteRule.cpp:313
clang::ast_matchers::callExpr
const internal::VariadicDynCastAllOfMatcher< Stmt, CallExpr > callExpr
Matches call expressions.
Definition: ASTMatchersInternal.cpp:803
clang::ast_matchers::functionDecl
const internal::VariadicDynCastAllOfMatcher< Decl, FunctionDecl > functionDecl
Matches function declarations.
Definition: ASTMatchersInternal.cpp:792
clang::ast_matchers::declRefExpr
const internal::VariadicDynCastAllOfMatcher< Stmt, DeclRefExpr > declRefExpr
Matches expressions that refer to declarations.
Definition: ASTMatchersInternal.cpp:877
clang::ast_matchers::hasName
internal::Matcher< NamedDecl > hasName(StringRef Name)
Matches NamedDecl nodes that have the specified name.
Definition: ASTMatchers.h:2989
clang::transformer::IncludeFormat::Angled
@ Angled
clang::transformer::name
RangeSelector name(std::string ID)
Given a node with a "name", (like NamedDecl, DeclRefExpr, CxxCtorInitializer, and TypeLoc) selects th...
Definition: RangeSelector.cpp:200
clang::ast_matchers::varDecl
const internal::VariadicDynCastAllOfMatcher< Decl, VarDecl > varDecl
Matches variable declarations.
Definition: ASTMatchersInternal.cpp:788