clang
10.0.0git
|
Namespaces | |
detail | |
The following three functions are a low-level part of the RewriteRule API. | |
Classes | |
struct | ASTEdit |
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 | TextGenerator = std::shared_ptr< MatchComputation< std::string > > |
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 | 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... | |
RangeSelector | charRange (CharSourceRange R) |
RangeSelector | range (RangeSelector Begin, RangeSelector End) |
Selects from the start of Begin and to the end of End . More... | |
RangeSelector | range (std::string BeginID, std::string EndID) |
Convenience version of range where end-points are bound nodes. 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 the point immediately following Selector . More... | |
RangeSelector | node (std::string ID) |
Selects a node, including trailing semicolon (for 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 or CxxCtorInitializer ) 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 (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... | |
RewriteRule | makeRule (ast_matchers::internal::DynTypedMatcher M, SmallVector< ASTEdit, 1 > Edits, TextGenerator Explanation=nullptr) |
Convenience function for constructing a simple RewriteRule . More... | |
RewriteRule | makeRule (ast_matchers::internal::DynTypedMatcher M, ASTEdit Edit, TextGenerator Explanation=nullptr) |
Convenience 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... | |
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 unchanged. More... | |
ASTEdit | insertAfter (RangeSelector S, TextGenerator Replacement) |
Inserts Replacement after S , leaving the source selected by unchanged. More... | |
ASTEdit | remove (RangeSelector S) |
Removes the source selected by S . 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 | text (llvm::StringRef Text) |
DEPRECATED: Use cat instead. More... | |
Stencil | selection (RangeSelector Selector) |
DEPRECATED: Use cat instead. 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 | dPrint (llvm::StringRef Id) |
For debug use only; semantics are not guaranteed. More... | |
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.
using clang::transformer::RangeSelector = typedef MatchConsumer<CharSourceRange> |
Definition at line 27 of file RangeSelector.h.
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.
using clang::transformer::StencilInterface = typedef MatchComputation<std::string> |
using clang::transformer::TextGenerator = typedef std::shared_ptr<MatchComputation<std::string> > |
Definition at line 33 of file RewriteRule.h.
|
strong |
Format of the path in an include directive – angle brackets or quotes.
Enumerator | |
---|---|
Quoted | |
Angled |
Definition at line 78 of file RewriteRule.h.
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(), cat(), clang::Sema::CheckFriendAccess(), and maybeAddressOf().
|
inline |
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:
Referenced by makeRule().
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 330 of file Stencil.cpp.
Referenced by cat().
RangeSelector clang::transformer::after | ( | RangeSelector | Selector | ) |
Selects the the point immediately following Selector
.
That is, the (empty) range [E,E), when Selector
selects either
Definition at line 116 of file RangeSelector.cpp.
References clang::CharSourceRange::getCharRange(), and clang::Lexer::getLocForEndOfToken().
Referenced by charRange(), clang::edit::Commit::insertBefore(), and clang::edit::Commit::insertWrap().
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 128 of file RewriteRule.cpp.
References clang::transformer::RewriteRule::Cases.
Referenced by makeRule().
RangeSelector clang::transformer::before | ( | RangeSelector | Selector | ) |
Selects the (empty) range [B,B) when Selector
selects the range [B,E).
Definition at line 107 of file RangeSelector.cpp.
References clang::CharSourceRange::getCharRange().
Referenced by charRange(), clang::CodeGen::CodeGenFunction::createBasicBlock(), clang::edit::Commit::insertBefore(), and clang::edit::Commit::insertWrap().
RangeSelector clang::transformer::callArgs | ( | std::string | ID | ) |
Definition at line 276 of file RangeSelector.cpp.
References clang::CharSourceRange::getCharRange(), clang::InitListExpr::getLBraceLoc(), clang::SourceLocation::getLocWithOffset(), and clang::InitListExpr::getRBraceLoc().
Referenced by charRange(), and clang::CodeGen::CGObjCRuntime::MessageSendInfo::MessageSendInfo().
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 Stencil
s.
Definition at line 64 of file Stencil.h.
References access(), addressOf(), catVector(), deref(), expression(), Id, clang::transformer::detail::makeStencil(), maybeAddressOf(), maybeDeref(), selection(), text(), and Text.
Referenced by clang::tooling::ArgumentsAdjustingCompilations::getAllCompileCommands(), clang::ASTContext::getVariableArrayDecayedType(), and clang::DiagnosticIDs::isARCDiagnostic().
Constructs the string representing the concatenation of the given Parts
.
If only one element is passed in Parts
, returns that element.
Definition at line 355 of file Stencil.cpp.
Referenced by cat().
|
inline |
|
inline |
ASTEdit clang::transformer::changeTo | ( | RangeSelector | Target, |
TextGenerator | Replacement | ||
) |
Replaces a portion of the source text with Replacement
.
Definition at line 57 of file RewriteRule.cpp.
Referenced by change(), changeTo(), and makeRule().
|
inline |
Replaces the entirety of a RewriteRule's match with Replacement
.
For example, to replace a function call, one could write:
Definition at line 205 of file RewriteRule.h.
References changeTo(), node(), and clang::transformer::RewriteRule::RootID.
|
inline |
Definition at line 29 of file RangeSelector.h.
References after(), before(), Begin, callArgs(), elseBranch(), End, expansion(), initListElements(), member(), name(), node(), range(), statement(), and statements().
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 320 of file Stencil.cpp.
Referenced by AddLazyVectorDecls(), cat(), getDeclForLocalLookup(), and shouldIgnoreMacro().
Stencil clang::transformer::dPrint | ( | llvm::StringRef | Id | ) |
For debug use only; semantics are not guaranteed.
Referenced by ifBound(), and selection().
RangeSelector clang::transformer::elseBranch | ( | std::string | ID | ) |
Given an (bound to ID
), selects the range of the else branch, starting from the else
keyword.
Definition at line 303 of file RangeSelector.cpp.
Referenced by charRange().
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 307 of file RangeSelector.cpp.
Referenced by charRange().
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 315 of file Stencil.cpp.
References Id.
Referenced by cat(), and clang::syntax::ExpressionStatement::classof().
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 access(), clang::transformer::MatchComputation< T >::eval(), ifBound(), and maybeAddressOf().
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.
|
inline |
RangeSelector clang::transformer::initListElements | ( | std::string | ID | ) |
Definition at line 290 of file RangeSelector.cpp.
Referenced by charRange().
|
inline |
Inserts Replacement
after S
, leaving the source selected by unchanged.
Definition at line 221 of file RewriteRule.h.
|
inline |
Inserts Replacement
before S
, leaving the source selected by unchanged.
Definition at line 215 of file RewriteRule.h.
RewriteRule clang::transformer::makeRule | ( | ast_matchers::internal::DynTypedMatcher | M, |
SmallVector< ASTEdit, 1 > | Edits, | ||
TextGenerator | Explanation = nullptr |
||
) |
Convenience function for constructing a simple RewriteRule
.
Referenced by makeRule().
|
inline |
Convenience overload of makeRule
for common case of only one edit.
Definition at line 130 of file RewriteRule.h.
References addInclude(), applyFirst(), changeTo(), makeRule(), Quoted, clang::transformer::ASTEdit::Replacement, and clang::Target.
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. FIXME: Identify smart-pointers as pointer types.
Definition at line 335 of file Stencil.cpp.
References access(), Id, and ifBound().
Referenced by cat().
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. FIXME: Identify smart-pointers as pointer types.
Definition at line 325 of file Stencil.cpp.
Referenced by cat().
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 174 of file RangeSelector.cpp.
References getNode(), clang::CharSourceRange::getTokenRange(), Node, and typeError().
Referenced by charRange(), findRetainCycleOwner(), and isExplicitMemberSpecialization().
RangeSelector clang::transformer::name | ( | std::string | ID | ) |
Given a node with a "name", (like NamedDecl
, DeclRefExpr
or CxxCtorInitializer
) 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
.
ID | is the node's binding in the match result. |
Definition at line 186 of file RangeSelector.cpp.
References clang::ast_type_traits::DynTypedNode::get(), clang::CharSourceRange::getCharRange(), clang::CompoundStmt::getLBracLoc(), clang::SourceLocation::getLocWithOffset(), getNode(), clang::ast_type_traits::DynTypedNode::getNodeKind(), clang::CompoundStmt::getRBracLoc(), clang::tooling::getText(), clang::CharSourceRange::getTokenRange(), missingPropertyError(), Node, clang::ast_matchers::MatchFinder::MatchResult::Nodes, and typeError().
Referenced by charRange(), createLoadInstBefore(), clang::Sema::Diag(), emitArraySubscriptGEP(), clang::CodeGen::CodeGenFunction::EmitBlockLiteral(), clang::CodeGen::CodeGenFunction::emitByrefStructureInit(), clang::CodeGen::CodeGenVTables::EmitThunks(), clang::CodeGen::CodeGenFunction::GenerateBlockFunction(), clang::ento::BugReporter::getAnalyzerOptions(), clang::CodeGen::CodeGenFunction::getCleanupKind(), getFunctionSourceLocation(), clang::Selector::getInstTypeMethodFamily(), clang::NamedDecl::getObjCFStringFormattingFamily(), clang::CodeGen::CodeGenModule::getTerminateFn(), handleObjCOwnershipTypeAttr(), clang::CodeGen::ConstantInitFuture::installInGlobal(), clang::NSAPI::isNSNumberLiteralSelector(), clang::NSAPI::isSubclassOfNSClass(), mangleAArch64VectorBase(), MigrateBlockOrFunctionPointerTypeVariable(), clang::ento::CheckerManager::setCurrentCheckerName(), clang::ObjCDeclSpec::setGetterName(), clang::ObjCDeclSpec::setSetterName(), startsWithWord(), and clang::CodeGen::ConstantInitBuilderBase::~ConstantInitBuilderBase().
RangeSelector clang::transformer::node | ( | std::string | ID | ) |
Selects a node, including trailing semicolon (for non-expression statements).
ID
is the node's binding in the match result.
Definition at line 129 of file RangeSelector.cpp.
References clang::tooling::getExtendedRange(), getNode(), clang::CharSourceRange::getTokenRange(), and Node.
Referenced by clang::CodeGen::CGDebugInfo::addHeapAllocSiteMetadata(), clang::ento::ExplodedNode::addPredecessor(), buildByrefHelpers(), changeTo(), charRange(), clang::ento::ExplodedGraph::enableNodeReclamation(), clang::ento::ExplodedNode::getID(), clang::ento::ExplodedGraph::isInterestingLValueExpr(), range(), and clang::ento::ExprEngine::Visit().
|
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.
RangeSelector clang::transformer::range | ( | RangeSelector | Begin, |
RangeSelector | End | ||
) |
Selects from the start of Begin
and to the end of End
.
Definition at line 151 of file RangeSelector.cpp.
References Begin, End, and invalidArgumentError().
Referenced by addFixitForObjCARCConversion(), clang::ento::RangeSet::addRange(), applyRewrite(), charRange(), clang::DiagnosticRenderer::DiagnosticRenderer(), clang::arcmt::getFileRemappings(), clang::DependentAddressSpaceTypeLoc::getLocalSourceRange(), hasSuperInitCall(), clang::edit::Commit::insertBefore(), clang::syntax::Token::length(), clang::ento::BasicBugReport::Profile(), clang::ento::PathSensitiveBugReport::Profile(), clang::arcmt::CapturedDiagList::push_back(), range(), clang::edit::Commit::replaceText(), clang::arcmt::TransformActions::report(), clang::DependentAddressSpaceTypeLoc::setAttrOperandParensRange(), clang::DeclSpec::setTypeofParensRange(), and clang::FixItRewriter::WriteFixedFile().
RangeSelector clang::transformer::range | ( | std::string | BeginID, |
std::string | EndID | ||
) |
Convenience version of range
where end-points are bound nodes.
Definition at line 170 of file RangeSelector.cpp.
ASTEdit clang::transformer::remove | ( | RangeSelector | S | ) |
Removes the source selected by S
.
Definition at line 82 of file RewriteRule.cpp.
Referenced by clang::Sema::CheckUnusedVolatileAssignment(), clang::driver::Compilation::CleanupFile(), clang::arcmt::FileRemapper::clear(), clang::CompilerInstance::clearOutputFiles(), clang::ASTContext::deduplicateMergedDefinitonsFor(), llvm::IDFCalculatorDetail::ChildrenGetterTy< clang::CFGBlock, IsPostDom >::get(), getStableLevel(), clang::Rewriter::IncreaseIndentation(), clang::JsonFormat(), PrintFilename(), pruneModuleCache(), and clang::sema::LambdaScopeInfo::removePotentialCapture().
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 350 of file Stencil.cpp.
Referenced by clang::tooling::CompilationDatabase::getAllCompileCommands(), clang::consumed::ConsumedAnalyzer::getExpectedReturnState(), ifBound(), clang::tooling::ToolInvocation::setDiagnosticConsumer(), and clang::tooling::ClangTool::setDiagnosticConsumer().
Stencil clang::transformer::selection | ( | RangeSelector | Selector | ) |
DEPRECATED: Use cat
instead.
Definition at line 307 of file Stencil.cpp.
Referenced by cat(), and clang::transformer::detail::makeStencil().
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 141 of file RangeSelector.cpp.
References clang::tooling::getExtendedRange(), getNode(), and Node.
Referenced by charRange().
RangeSelector clang::transformer::statements | ( | std::string | ID | ) |
Definition at line 261 of file RangeSelector.cpp.
References clang::ast_matchers::MatchFinder::MatchResult::Context, findOpenParen(), clang::CharSourceRange::getCharRange(), clang::ASTContext::getLangOpts(), clang::CallExpr::getRParenLoc(), and clang::ast_matchers::MatchFinder::MatchResult::SourceManager.
Referenced by charRange(), and clang::syntax::CompoundStatement::classof().
Stencil clang::transformer::text | ( | llvm::StringRef | Text | ) |
DEPRECATED: Use cat
instead.
Referenced by access(), cat(), ifBound(), and clang::transformer::detail::makeStencil().