10 #include "llvm/ADT/STLExtras.h" 18 return llvm::is_contained(
Node.capture_inits(), E);
22 ast_matchers::internal::Matcher<DeclStmt>, InnerMatcher) {
23 const DeclStmt *
const Range =
Node.getRangeStmt();
24 return InnerMatcher.matches(*Range, Finder, Builder);
28 ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
29 const Expr* Result = &
Node;
30 while (
const auto *BOComma =
31 dyn_cast_or_null<BinaryOperator>(Result->IgnoreParens())) {
32 if (!BOComma->isCommaOp())
34 Result = BOComma->getRHS();
36 return InnerMatcher.matches(*Result, Finder, Builder);
39 const ast_matchers::internal::VariadicDynCastAllOfMatcher<Stmt, CXXTypeidExpr>
42 AST_MATCHER(CXXTypeidExpr, isPotentiallyEvaluated) {
43 return Node.isPotentiallyEvaluated();
46 const ast_matchers::internal::VariadicDynCastAllOfMatcher<Stmt, CXXNoexceptExpr>
49 const ast_matchers::internal::VariadicDynCastAllOfMatcher<Stmt,
54 ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
55 return InnerMatcher.matches(*
Node.getControllingExpr(), Finder, Builder);
58 const auto nonConstReferenceType = [] {
59 return hasUnqualifiedDesugaredType(
63 const auto nonConstPointerType = [] {
64 return hasUnqualifiedDesugaredType(
68 const auto isMoveOnly = [] {
78 template <
class T>
struct NodeID;
79 template <>
struct NodeID<Expr> {
static const std::string value; };
80 template <>
struct NodeID<
Decl> {
static const std::string value; };
81 const std::string NodeID<Expr>::value =
"expr";
82 const std::string NodeID<Decl>::value =
"decl";
84 template <
class T,
class F = const Stmt *(ExprMutationAnalyzer::*)(const T *)>
85 const Stmt *tryEachMatch(ArrayRef<ast_matchers::BoundNodes> Matches,
86 ExprMutationAnalyzer *Analyzer, F Finder) {
87 const StringRef
ID = NodeID<T>::value;
88 for (
const auto &
Nodes : Matches) {
89 if (
const Stmt *S = (Analyzer->*Finder)(
Nodes.getNodeAs<T>(ID)))
98 return findMutationMemoized(Exp,
99 {&ExprMutationAnalyzer::findDirectMutation,
100 &ExprMutationAnalyzer::findMemberMutation,
101 &ExprMutationAnalyzer::findArrayElementMutation,
102 &ExprMutationAnalyzer::findCastMutation,
103 &ExprMutationAnalyzer::findRangeLoopMutation,
104 &ExprMutationAnalyzer::findReferenceMutation,
105 &ExprMutationAnalyzer::findFunctionArgMutation},
114 return findMutationMemoized(Exp, {}, PointeeResults);
121 const Stmt *ExprMutationAnalyzer::findMutationMemoized(
123 ResultMap &MemoizedResults) {
124 const auto Memoized = MemoizedResults.find(Exp);
125 if (Memoized != MemoizedResults.end())
126 return Memoized->second;
128 if (isUnevaluated(Exp))
129 return MemoizedResults[Exp] =
nullptr;
131 for (
const auto &Finder : Finders) {
132 if (
const Stmt *S = (this->*Finder)(Exp))
133 return MemoizedResults[Exp] = S;
136 return MemoizedResults[Exp] =
nullptr;
139 const Stmt *ExprMutationAnalyzer::tryEachDeclRef(
const Decl *Dec,
140 MutationFinder Finder) {
144 for (
const auto &RefNodes : Refs) {
145 const auto *E = RefNodes.getNodeAs<
Expr>(NodeID<Expr>::value);
146 if ((this->*Finder)(E))
152 bool ExprMutationAnalyzer::isUnevaluated(
const Expr *Exp) {
153 return selectFirst<Expr>(
157 expr(equalsNode(Exp),
173 unless(isPotentiallyEvaluated())),
176 genericSelectionExpr(hasControllingExpr(
178 cxxNoexceptExpr())))))
179 .bind(NodeID<Expr>::value)),
180 Stm, Context)) !=
nullptr;
193 const Stmt *ExprMutationAnalyzer::findExprPointeeMutation(
195 return tryEachMatch<Expr>(Matches,
this,
199 const Stmt *ExprMutationAnalyzer::findDeclPointeeMutation(
201 return tryEachMatch<Decl>(Matches,
this,
205 const Stmt *ExprMutationAnalyzer::findDirectMutation(
const Expr *Exp) {
207 const auto AsAssignmentLhs =
209 hasLHS(maybeEvalCommaExpr(equalsNode(Exp))));
212 const auto AsIncDecOperand =
214 hasUnaryOperand(maybeEvalCommaExpr(equalsNode(Exp))));
219 const auto AsNonConstThis =
221 on(maybeEvalCommaExpr(equalsNode(Exp)))),
224 maybeEvalCommaExpr(equalsNode(Exp)))),
227 hasObjectExpression(maybeEvalCommaExpr(equalsNode(Exp)))),
229 hasObjectExpression(maybeEvalCommaExpr(equalsNode(Exp))))))))));
235 const auto AsAmpersandOperand =
239 hasUnaryOperand(maybeEvalCommaExpr(equalsNode(Exp))));
240 const auto AsPointerFromArrayDecay =
241 castExpr(hasCastKind(CK_ArrayToPointerDecay),
243 has(maybeEvalCommaExpr(equalsNode(Exp))));
247 const auto AsOperatorArrowThis =
250 returns(nonConstPointerType()))),
252 hasArgument(0, maybeEvalCommaExpr(equalsNode(Exp))));
259 const auto NonConstRefParam = forEachArgumentWithParam(
260 maybeEvalCommaExpr(equalsNode(Exp)),
263 const auto AsNonConstRefArg =
anyOf(
264 callExpr(NonConstRefParam, NotInstantiated),
269 hasAnyArgument(maybeEvalCommaExpr(equalsNode(Exp)))),
276 const auto AsLambdaRefCaptureInit =
lambdaExpr(hasCaptureInit(Exp));
283 const auto AsNonConstRefReturn =
returnStmt(hasReturnValue(
284 maybeEvalCommaExpr(equalsNode(Exp))));
288 AsAmpersandOperand, AsPointerFromArrayDecay,
289 AsOperatorArrowThis, AsNonConstRefArg,
290 AsLambdaRefCaptureInit, AsNonConstRefReturn))
293 return selectFirst<Stmt>(
"stmt", Matches);
296 const Stmt *ExprMutationAnalyzer::findMemberMutation(
const Expr *Exp) {
298 const auto MemberExprs =
301 hasObjectExpression(equalsNode(Exp)))))
302 .bind(NodeID<Expr>::value)),
304 return findExprMutation(MemberExprs);
307 const Stmt *ExprMutationAnalyzer::findArrayElementMutation(
const Expr *Exp) {
309 const auto SubscriptExprs =
match(
311 .bind(NodeID<Expr>::value)),
313 return findExprMutation(SubscriptExprs);
316 const Stmt *ExprMutationAnalyzer::findCastMutation(
const Expr *Exp) {
321 nonConstReferenceType())),
323 nonConstReferenceType()))))
324 .bind(NodeID<Expr>::value)),
326 if (
const Stmt *S = findExprMutation(Casts))
331 hasAnyName(
"::std::move",
"::std::forward"))),
332 hasArgument(0, equalsNode(Exp)))
335 return findExprMutation(Calls);
338 const Stmt *ExprMutationAnalyzer::findRangeLoopMutation(
const Expr *Exp) {
341 const auto LoopVars =
343 hasLoopVariable(
varDecl(hasType(nonConstReferenceType()))
344 .bind(NodeID<Decl>::value)),
345 hasRangeInit(equalsNode(Exp)))),
347 return findDeclMutation(LoopVars);
350 const Stmt *ExprMutationAnalyzer::findReferenceMutation(
const Expr *Exp) {
358 returns(nonConstReferenceType()))),
359 argumentCountIs(1), hasArgument(0, equalsNode(Exp)))
360 .bind(NodeID<Expr>::value)),
362 if (
const Stmt *S = findExprMutation(Ref))
366 const auto Refs =
match(
369 hasType(nonConstReferenceType()),
370 hasInitializer(
anyOf(equalsNode(Exp),
372 hasTrueExpression(equalsNode(Exp)),
373 hasFalseExpression(equalsNode(Exp)))))),
379 .bind(NodeID<Decl>::value))),
381 return findDeclMutation(Refs);
384 const Stmt *ExprMutationAnalyzer::findFunctionArgMutation(
const Expr *Exp) {
385 const auto NonConstRefParam = forEachArgumentWithParam(
387 parmVarDecl(hasType(nonConstReferenceType())).bind(
"parm"));
390 const auto Matches =
match(
393 "::std::move",
"::std::forward"))))),
396 .bind(NodeID<Expr>::value)),
398 for (
const auto &
Nodes : Matches) {
399 const auto *Exp =
Nodes.getNodeAs<
Expr>(NodeID<Expr>::value);
401 if (!Func->getBody() || !Func->getPrimaryTemplate())
406 Func->getPrimaryTemplate()->getTemplatedDecl()->parameters();
408 AllParams[std::min<size_t>(Parm->getFunctionScopeIndex(),
409 AllParams.size() - 1)]
412 ParmType = T->getPattern();
417 if (!RefType->getPointeeType().getQualifiers() &&
419 std::unique_ptr<FunctionParmMutationAnalyzer> &Analyzer =
420 FuncParmAnalyzer[Func];
423 if (Analyzer->findMutation(Parm))
436 : BodyAnalyzer(*Func.getBody(), Context) {
437 if (
const auto *Ctor = dyn_cast<CXXConstructorDecl>(&Func)) {
442 for (
const ParmVarDecl *Parm : Ctor->parameters()) {
443 if (Results.find(Parm) != Results.end())
445 if (
const Stmt *S = InitAnalyzer.findMutation(Parm))
454 const auto Memoized = Results.find(Parm);
455 if (Memoized != Results.end())
456 return Memoized->second;
459 return Results[Parm] = S;
461 return Results[Parm] =
nullptr;
const internal::VariadicDynCastAllOfMatcher< Stmt, CallExpr > callExpr
Matches call expressions.
Represents a function declaration or definition.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
A (possibly-)qualified type.
AST_MATCHER_P(FieldDecl, hasBitWidth, unsigned, Width)
Matches non-static data members that are bit-fields of the specified bit width.
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXUnresolvedConstructExpr > cxxUnresolvedConstructExpr
Matches unresolved constructor call expressions.
const internal::ArgumentAdaptingMatcherFunc< internal::HasMatcher > has
Matches AST nodes that have child AST nodes that match the provided matcher.
internal::PolymorphicMatcherWithParam1< internal::HasDeclarationMatcher, internal::Matcher< Decl >, void(internal::HasDeclarationSupportedTypes)> hasDeclaration(const internal::Matcher< Decl > &InnerMatcher)
Matches a node if the declaration associated with that node matches the given matcher.
const internal::VariadicOperatorMatcherFunc< 2, std::numeric_limits< unsigned >::max()> anyOf
Matches if any of the given matchers matches.
Decl - This represents one declaration (or definition), e.g.
const internal::ArgumentAdaptingMatcherFunc< internal::HasDescendantMatcher > hasDescendant
Matches AST nodes that have descendant AST nodes that match the provided matcher. ...
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
const Stmt * findPointeeMutation(const Expr *Exp)
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
const AstTypeMatcher< PointerType > pointerType
Matches pointer types, but does not match Objective-C object pointer types.
const T * getAs() const
Member-template getAs<specific type>'.
Analyzes whether any mutative operations are applied to an expression within a given statement...
const internal::VariadicDynCastAllOfMatcher< Stmt, BinaryOperator > binaryOperator
Matches binary operator expressions.
AST_MATCHER(Decl, isPublic)
Matches public C++ declarations.
const internal::VariadicDynCastAllOfMatcher< Decl, VarDecl > varDecl
Matches variable declarations.
const internal::VariadicDynCastAllOfMatcher< Decl, FunctionDecl > functionDecl
Matches function declarations.
const internal::VariadicDynCastAllOfMatcher< Decl, NamedDecl > namedDecl
Matches a declaration of anything that could have a name.
Represents a parameter to a function.
BoundNodesTreeBuilder Nodes
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXOperatorCallExpr > cxxOperatorCallExpr
Matches overloaded operator calls.
const internal::VariadicDynCastAllOfMatcher< Stmt, DeclStmt > declStmt
Matches declaration statements.
An rvalue reference type, per C++11 [dcl.ref].
const internal::VariadicDynCastAllOfMatcher< Stmt, DeclRefExpr > declRefExpr
Matches expressions that refer to declarations.
const internal::VariadicFunction< internal::Matcher< NamedDecl >, StringRef, internal::hasAnyNameFunc > hasAnyName
Matches NamedDecl nodes that have any of the specified names.
internal::Matcher< Stmt > sizeOfExpr(const internal::Matcher< UnaryExprOrTypeTraitExpr > &InnerMatcher)
Same as unaryExprOrTypeTraitExpr, but only matching sizeof.
const AstTypeMatcher< VariableArrayType > variableArrayType
Matches C arrays with a specified size that is not an integer-constant-expression.
const internal::VariadicDynCastAllOfMatcher< Stmt, CastExpr > castExpr
Matches any cast nodes of Clang's AST.
internal::PolymorphicMatcherWithParam1< internal::HasOverloadedOperatorNameMatcher, StringRef, AST_POLYMORPHIC_SUPPORTED_TYPES(CXXOperatorCallExpr, FunctionDecl)> hasOverloadedOperatorName(StringRef Name)
Matches overloaded operator names.
SmallVector< BoundNodes, 1 > match(MatcherT Matcher, const NodeT &Node, ASTContext &Context)
Returns the results of matching Matcher on Node.
internal::Matcher< T > findAll(const internal::Matcher< T > &Matcher)
Matches if the node or any descendant matches.
const internal::ArgumentAdaptingMatcherFunc< internal::ForEachDescendantMatcher > forEachDescendant
Matches AST nodes that have descendant AST nodes that match the provided matcher. ...
const internal::VariadicDynCastAllOfMatcher< Stmt, MemberExpr > memberExpr
Matches member expressions.
const internal::VariadicDynCastAllOfMatcher< Stmt, ExplicitCastExpr > explicitCastExpr
Matches explicit cast expressions.
This represents one expression.
const internal::VariadicDynCastAllOfMatcher< Stmt, UnresolvedLookupExpr > unresolvedLookupExpr
Matches reference to a name that can be looked up during parsing but could not be resolved to a speci...
const internal::VariadicDynCastAllOfMatcher< Stmt, UnresolvedMemberExpr > unresolvedMemberExpr
Matches unresolved member expressions.
const internal::VariadicDynCastAllOfMatcher< Stmt, ConditionalOperator > conditionalOperator
Matches conditional operator expressions.
const internal::VariadicDynCastAllOfMatcher< Stmt, ReturnStmt > returnStmt
Matches return statements.
const internal::ArgumentAdaptingMatcherFunc< internal::HasAncestorMatcher, internal::TypeList< Decl, NestedNameSpecifierLoc, Stmt, TypeLoc >, internal::TypeList< Decl, NestedNameSpecifierLoc, Stmt, TypeLoc > > hasAncestor
Matches AST nodes that have an ancestor that matches the provided matcher.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXMemberCallExpr > cxxMemberCallExpr
Matches member call expressions.
const internal::VariadicDynCastAllOfMatcher< Decl, ParmVarDecl > parmVarDecl
Matches parameter variable declarations.
const internal::VariadicDynCastAllOfMatcher< Decl, CXXMethodDecl > cxxMethodDecl
Matches method declarations.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXForRangeStmt > cxxForRangeStmt
Matches range-based for statements.
const internal::VariadicAllOfMatcher< TypeLoc > typeLoc
Matches TypeLocs in the clang AST.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXConstructExpr > cxxConstructExpr
Matches constructor call expressions (including implicit ones).
const internal::VariadicDynCastAllOfMatcher< Stmt, ArraySubscriptExpr > arraySubscriptExpr
Matches array subscript expressions.
const internal::VariadicDynCastAllOfMatcher< Stmt, UnaryExprOrTypeTraitExpr > unaryExprOrTypeTraitExpr
Matches sizeof (C99), alignof (C++11) and vec_step (OpenCL)
const internal::ArgumentAdaptingMatcherFunc< internal::HasParentMatcher, internal::TypeList< Decl, NestedNameSpecifierLoc, Stmt, TypeLoc >, internal::TypeList< Decl, NestedNameSpecifierLoc, Stmt, TypeLoc > > hasParent
Matches AST nodes that have a parent that matches the provided matcher.
const internal::VariadicOperatorMatcherFunc< 1, 1 > unless
Matches if the provided matcher does not match.
Represents a pack expansion of types.
ast_type_traits::DynTypedNode Node
Dataflow Directional Tag Classes.
const internal::VariadicDynCastAllOfMatcher< Decl, CXXRecordDecl > cxxRecordDecl
Matches C++ class declarations.
Represents a C++ base or member initializer.
const internal::VariadicDynCastAllOfMatcher< Stmt, LambdaExpr > lambdaExpr
Matches lambda expressions.
const internal::VariadicDynCastAllOfMatcher< Stmt, UnaryOperator > unaryOperator
Matches unary operator expressions.
const internal::VariadicDynCastAllOfMatcher< Decl, CXXConstructorDecl > cxxConstructorDecl
Matches C++ constructor declarations.
const Stmt * findMutation(const ParmVarDecl *Parm)
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXDependentScopeMemberExpr > cxxDependentScopeMemberExpr
Matches member expressions where the actual member referenced could not be resolved because the base ...
const Stmt * findMutation(const Expr *Exp)
const internal::VariadicDynCastAllOfMatcher< Stmt, ImplicitCastExpr > implicitCastExpr
Matches the implicit cast nodes of Clang's AST.
FunctionParmMutationAnalyzer(const FunctionDecl &Func, ASTContext &Context)
const AstTypeMatcher< TemplateTypeParmType > templateTypeParmType
Matches template type parameter types.
const AstTypeMatcher< ReferenceType > referenceType
Matches both lvalue and rvalue reference types.