11 #include "llvm/ADT/STLExtras.h" 19 return llvm::is_contained(
Node.capture_inits(), E);
23 ast_matchers::internal::Matcher<DeclStmt>, InnerMatcher) {
24 const DeclStmt *
const Range =
Node.getRangeStmt();
25 return InnerMatcher.matches(*Range, Finder, Builder);
28 const ast_matchers::internal::VariadicDynCastAllOfMatcher<Stmt, CXXTypeidExpr>
31 AST_MATCHER(CXXTypeidExpr, isPotentiallyEvaluated) {
32 return Node.isPotentiallyEvaluated();
35 const ast_matchers::internal::VariadicDynCastAllOfMatcher<Stmt, CXXNoexceptExpr>
38 const ast_matchers::internal::VariadicDynCastAllOfMatcher<Stmt,
43 ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
44 return InnerMatcher.matches(*
Node.getControllingExpr(), Finder, Builder);
47 const auto nonConstReferenceType = [] {
48 return hasUnqualifiedDesugaredType(
52 const auto nonConstPointerType = [] {
53 return hasUnqualifiedDesugaredType(
57 const auto isMoveOnly = [] {
67 template <
class T>
struct NodeID;
68 template <>
struct NodeID<Expr> {
static const std::string value; };
69 template <>
struct NodeID<
Decl> {
static const std::string value; };
70 const std::string NodeID<Expr>::value =
"expr";
71 const std::string NodeID<Decl>::value =
"decl";
73 template <
class T,
class F = const Stmt *(ExprMutationAnalyzer::*)(const T *)>
74 const Stmt *tryEachMatch(ArrayRef<ast_matchers::BoundNodes> Matches,
75 ExprMutationAnalyzer *Analyzer, F Finder) {
76 const StringRef
ID = NodeID<T>::value;
77 for (
const auto &
Nodes : Matches) {
78 if (
const Stmt *S = (Analyzer->*Finder)(
Nodes.getNodeAs<T>(ID)))
87 return findMutationMemoized(Exp,
88 {&ExprMutationAnalyzer::findDirectMutation,
89 &ExprMutationAnalyzer::findMemberMutation,
90 &ExprMutationAnalyzer::findArrayElementMutation,
91 &ExprMutationAnalyzer::findCastMutation,
92 &ExprMutationAnalyzer::findRangeLoopMutation,
93 &ExprMutationAnalyzer::findReferenceMutation,
94 &ExprMutationAnalyzer::findFunctionArgMutation},
103 return findMutationMemoized(Exp, {}, PointeeResults);
110 const Stmt *ExprMutationAnalyzer::findMutationMemoized(
112 ResultMap &MemoizedResults) {
113 const auto Memoized = MemoizedResults.find(Exp);
114 if (Memoized != MemoizedResults.end())
115 return Memoized->second;
117 if (isUnevaluated(Exp))
118 return MemoizedResults[Exp] =
nullptr;
120 for (
const auto &Finder : Finders) {
121 if (
const Stmt *S = (this->*Finder)(Exp))
122 return MemoizedResults[Exp] = S;
125 return MemoizedResults[Exp] =
nullptr;
128 const Stmt *ExprMutationAnalyzer::tryEachDeclRef(
const Decl *Dec,
129 MutationFinder Finder) {
133 for (
const auto &RefNodes : Refs) {
134 const auto *E = RefNodes.getNodeAs<
Expr>(NodeID<Expr>::value);
135 if ((this->*Finder)(E))
141 bool ExprMutationAnalyzer::isUnevaluated(
const Expr *Exp) {
142 return selectFirst<Expr>(
146 expr(equalsNode(Exp),
162 unless(isPotentiallyEvaluated())),
165 genericSelectionExpr(hasControllingExpr(
167 cxxNoexceptExpr())))))
168 .bind(NodeID<Expr>::value)),
169 Stm, Context)) !=
nullptr;
182 const Stmt *ExprMutationAnalyzer::findExprPointeeMutation(
184 return tryEachMatch<Expr>(Matches,
this,
188 const Stmt *ExprMutationAnalyzer::findDeclPointeeMutation(
190 return tryEachMatch<Decl>(Matches,
this,
194 const Stmt *ExprMutationAnalyzer::findDirectMutation(
const Expr *Exp) {
196 const auto AsAssignmentLhs =
200 const auto AsIncDecOperand =
202 hasUnaryOperand(equalsNode(Exp)));
207 const auto AsNonConstThis =
210 hasArgument(0, equalsNode(Exp))),
214 hasObjectExpression(equalsNode(Exp)))))))));
220 const auto AsAmpersandOperand =
224 hasUnaryOperand(equalsNode(Exp)));
225 const auto AsPointerFromArrayDecay =
226 castExpr(hasCastKind(CK_ArrayToPointerDecay),
231 const auto AsOperatorArrowThis =
234 returns(nonConstPointerType()))),
235 argumentCountIs(1), hasArgument(0, equalsNode(Exp)));
242 const auto NonConstRefParam = forEachArgumentWithParam(
243 equalsNode(Exp),
parmVarDecl(hasType(nonConstReferenceType())));
245 const auto AsNonConstRefArg =
anyOf(
246 callExpr(NonConstRefParam, NotInstantiated),
251 hasAnyArgument(equalsNode(Exp))),
258 const auto AsLambdaRefCaptureInit =
lambdaExpr(hasCaptureInit(Exp));
265 const auto AsNonConstRefReturn =
returnStmt(hasReturnValue(equalsNode(Exp)));
269 AsAmpersandOperand, AsPointerFromArrayDecay,
270 AsOperatorArrowThis, AsNonConstRefArg,
271 AsLambdaRefCaptureInit, AsNonConstRefReturn))
274 return selectFirst<Stmt>(
"stmt", Matches);
277 const Stmt *ExprMutationAnalyzer::findMemberMutation(
const Expr *Exp) {
279 const auto MemberExprs =
282 hasObjectExpression(equalsNode(Exp)))))
283 .bind(NodeID<Expr>::value)),
285 return findExprMutation(MemberExprs);
288 const Stmt *ExprMutationAnalyzer::findArrayElementMutation(
const Expr *Exp) {
290 const auto SubscriptExprs =
match(
292 .bind(NodeID<Expr>::value)),
294 return findExprMutation(SubscriptExprs);
297 const Stmt *ExprMutationAnalyzer::findCastMutation(
const Expr *Exp) {
302 nonConstReferenceType())),
304 nonConstReferenceType()))))
305 .bind(NodeID<Expr>::value)),
307 if (
const Stmt *S = findExprMutation(Casts))
312 hasAnyName(
"::std::move",
"::std::forward"))),
313 hasArgument(0, equalsNode(Exp)))
316 return findExprMutation(Calls);
319 const Stmt *ExprMutationAnalyzer::findRangeLoopMutation(
const Expr *Exp) {
322 const auto LoopVars =
324 hasLoopVariable(
varDecl(hasType(nonConstReferenceType()))
325 .bind(NodeID<Decl>::value)),
326 hasRangeInit(equalsNode(Exp)))),
328 return findDeclMutation(LoopVars);
331 const Stmt *ExprMutationAnalyzer::findReferenceMutation(
const Expr *Exp) {
339 returns(nonConstReferenceType()))),
340 argumentCountIs(1), hasArgument(0, equalsNode(Exp)))
341 .bind(NodeID<Expr>::value)),
343 if (
const Stmt *S = findExprMutation(Ref))
347 const auto Refs =
match(
350 hasType(nonConstReferenceType()),
351 hasInitializer(
anyOf(equalsNode(Exp),
353 hasTrueExpression(equalsNode(Exp)),
354 hasFalseExpression(equalsNode(Exp)))))),
360 .bind(NodeID<Decl>::value))),
362 return findDeclMutation(Refs);
365 const Stmt *ExprMutationAnalyzer::findFunctionArgMutation(
const Expr *Exp) {
366 const auto NonConstRefParam = forEachArgumentWithParam(
368 parmVarDecl(hasType(nonConstReferenceType())).bind(
"parm"));
371 const auto Matches =
match(
374 "::std::move",
"::std::forward"))))),
377 .bind(NodeID<Expr>::value)),
379 for (
const auto &
Nodes : Matches) {
380 const auto *Exp =
Nodes.getNodeAs<
Expr>(NodeID<Expr>::value);
382 if (!Func->getBody() || !Func->getPrimaryTemplate())
387 Func->getPrimaryTemplate()->getTemplatedDecl()->parameters();
389 AllParams[std::min<size_t>(Parm->getFunctionScopeIndex(),
390 AllParams.size() - 1)]
393 ParmType = T->getPattern();
398 if (!RefType->getPointeeType().getQualifiers() &&
400 std::unique_ptr<FunctionParmMutationAnalyzer> &Analyzer =
401 FuncParmAnalyzer[Func];
404 if (Analyzer->findMutation(Parm))
417 : BodyAnalyzer(*Func.getBody(), Context) {
418 if (
const auto *Ctor = dyn_cast<CXXConstructorDecl>(&Func)) {
423 for (
const ParmVarDecl *Parm : Ctor->parameters()) {
424 if (Results.find(Parm) != Results.end())
426 if (
const Stmt *S = InitAnalyzer.findMutation(Parm))
435 const auto Memoized = Results.find(Parm);
436 if (Memoized != Results.end())
437 return Memoized->second;
440 return Results[Parm] = S;
442 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. ...
bool isAssignmentOperator(OverloadedOperatorKind OK)
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].
llvm::DOTGraphTraits< ExplodedGraph * > DefaultDOTGraphTraits const ExplodedNode const ExplodedNode *Out<< "\l\|";Out<< "StateID: ST"<< State-> NodeID
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.