clang 23.0.0git
ExprMutationAnalyzer.cpp
Go to the documentation of this file.
1//===---------- ExprMutationAnalyzer.cpp ----------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
9#include "clang/AST/Expr.h"
11#include "clang/AST/Stmt.h"
15#include "llvm/ADT/STLExtras.h"
16
17namespace clang {
18using namespace ast_matchers;
19
20// Check if result of Source expression could be a Target expression.
21// Checks:
22// - Implicit Casts
23// - Binary Operators
24// - ConditionalOperator
25// - BinaryConditionalOperator
26static bool canExprResolveTo(const Expr *Source, const Expr *Target) {
27 const auto IgnoreDerivedToBase = [](const Expr *E, auto Matcher) {
28 if (Matcher(E))
29 return true;
30 if (const auto *Cast = dyn_cast<ImplicitCastExpr>(E)) {
31 if ((Cast->getCastKind() == CK_DerivedToBase ||
32 Cast->getCastKind() == CK_UncheckedDerivedToBase) &&
33 Matcher(Cast->getSubExpr()))
34 return true;
35 }
36 return false;
37 };
38
39 const auto EvalCommaExpr = [](const Expr *E, auto Matcher) {
40 const Expr *Result = E;
41 while (const auto *BOComma =
42 dyn_cast_or_null<BinaryOperator>(Result->IgnoreParens())) {
43 if (!BOComma->isCommaOp())
44 break;
45 Result = BOComma->getRHS();
46 }
47
48 return Result != E && Matcher(Result);
49 };
50
51 // The 'ConditionalOperatorM' matches on `<anything> ? <expr> : <expr>`.
52 // This matching must be recursive because `<expr>` can be anything resolving
53 // to the `InnerMatcher`, for example another conditional operator.
54 // The edge-case `BaseClass &b = <cond> ? DerivedVar1 : DerivedVar2;`
55 // is handled, too. The implicit cast happens outside of the conditional.
56 // This is matched by `IgnoreDerivedToBase(canResolveToExpr(InnerMatcher))`
57 // below.
58 const auto ConditionalOperatorM = [Target](const Expr *E) {
59 if (const auto *CO = dyn_cast<AbstractConditionalOperator>(E)) {
60 const auto *TE = CO->getTrueExpr()->IgnoreParens();
61 if (TE && canExprResolveTo(TE, Target))
62 return true;
63 const auto *FE = CO->getFalseExpr()->IgnoreParens();
64 if (FE && canExprResolveTo(FE, Target))
65 return true;
66 }
67 return false;
68 };
69
70 const Expr *SourceExprP = Source->IgnoreParens();
71 return IgnoreDerivedToBase(SourceExprP,
72 [&](const Expr *E) {
73 return E == Target || ConditionalOperatorM(E);
74 }) ||
75 EvalCommaExpr(SourceExprP, [&](const Expr *E) {
76 return IgnoreDerivedToBase(
77 E->IgnoreParens(), [&](const Expr *EE) { return EE == Target; });
78 });
79}
80
81namespace {
82
83// `ArraySubscriptExpr` can switch base and idx, e.g. `a[4]` is the same as
84// `4[a]`. When type is dependent, we conservatively assume both sides are base.
85AST_MATCHER_P(ArraySubscriptExpr, hasBaseConservative,
86 ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
87 if (Node.isTypeDependent()) {
88 return InnerMatcher.matches(*Node.getLHS(), Finder, Builder) ||
89 InnerMatcher.matches(*Node.getRHS(), Finder, Builder);
90 }
91 return InnerMatcher.matches(*Node.getBase(), Finder, Builder);
92}
93
94AST_MATCHER(Type, isDependentType) { return Node.isDependentType(); }
95
96AST_MATCHER_P(LambdaExpr, hasCaptureInit, const Expr *, E) {
97 return llvm::is_contained(Node.capture_inits(), E);
98}
99
100AST_MATCHER_P(CXXForRangeStmt, hasRangeStmt,
101 ast_matchers::internal::Matcher<DeclStmt>, InnerMatcher) {
102 const DeclStmt *const Range = Node.getRangeStmt();
103 return InnerMatcher.matches(*Range, Finder, Builder);
104}
105
106AST_MATCHER_P(Stmt, canResolveToExpr, const Stmt *, Inner) {
107 auto *Exp = dyn_cast<Expr>(&Node);
108 if (!Exp)
109 return true;
110 auto *Target = dyn_cast<Expr>(Inner);
111 if (!Target)
112 return false;
113 return canExprResolveTo(Exp, Target);
114}
115
116// use class member to store data can reduce stack usage to avoid stack overflow
117// when recursive call.
118class ExprPointeeResolve {
119 const Expr *T;
120
121 bool resolveExpr(const Expr *E) {
122 if (E == nullptr)
123 return false;
124 if (E == T)
125 return true;
126
127 if (const auto *BO = dyn_cast<BinaryOperator>(E)) {
128 if (BO->isAdditiveOp())
129 return (resolveExpr(BO->getLHS()) || resolveExpr(BO->getRHS()));
130 if (BO->isCommaOp())
131 return resolveExpr(BO->getRHS());
132 return false;
133 }
134
135 if (const auto *PE = dyn_cast<ParenExpr>(E))
136 return resolveExpr(PE->getSubExpr());
137
138 if (const auto *UO = dyn_cast<UnaryOperator>(E)) {
139 if (UO->getOpcode() == UO_AddrOf)
140 return resolveExpr(UO->getSubExpr());
141 }
142
143 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E)) {
144 // only implicit cast needs to be treated as resolvable.
145 // explicit cast will be checked in `findPointeeToNonConst`
146 const CastKind kind = ICE->getCastKind();
147 if (kind == CK_LValueToRValue || kind == CK_DerivedToBase ||
148 kind == CK_UncheckedDerivedToBase)
149 return resolveExpr(ICE->getSubExpr());
150 if (kind == CK_NoOp) {
151 // Binding `T *` to `T *const &` only adds top-level qualifiers to the
152 // pointer object, so this `CK_NoOp` still refers to the same pointer.
153 const auto GetLocallyUnqualifiedCanonicalType = [](QualType Type) {
154 return Type.getLocalUnqualifiedType().getCanonicalType();
155 };
156 const QualType CastType =
157 GetLocallyUnqualifiedCanonicalType(ICE->getType());
158 const QualType SubExprType =
159 GetLocallyUnqualifiedCanonicalType(ICE->getSubExpr()->getType());
160 if (CastType == SubExprType)
161 return resolveExpr(ICE->getSubExpr());
162 }
163 return false;
164 }
165
166 if (const auto *ACE = dyn_cast<AbstractConditionalOperator>(E))
167 return resolve(ACE->getTrueExpr()) || resolve(ACE->getFalseExpr());
168
169 return false;
170 }
171
172public:
173 ExprPointeeResolve(const Expr *T) : T(T) {}
174 bool resolve(const Expr *S) { return resolveExpr(S); }
175};
176
177AST_MATCHER_P(Stmt, canResolveToExprPointee, const Stmt *, T) {
178 auto *Exp = dyn_cast<Expr>(&Node);
179 if (!Exp)
180 return true;
181 auto *Target = dyn_cast<Expr>(T);
182 if (!Target)
183 return false;
184 return ExprPointeeResolve{Target}.resolve(Exp);
185}
186
187// Similar to 'hasAnyArgument', but does not work because 'InitListExpr' does
188// not have the 'arguments()' method.
189AST_MATCHER_P(InitListExpr, hasAnyInit, ast_matchers::internal::Matcher<Expr>,
190 InnerMatcher) {
191 for (const Expr *Arg : Node.inits()) {
192 if (Arg == nullptr)
193 continue;
194 ast_matchers::internal::BoundNodesTreeBuilder Result(*Builder);
195 if (InnerMatcher.matches(*Arg, Finder, &Result)) {
196 *Builder = std::move(Result);
197 return true;
198 }
199 }
200 return false;
201}
202
203const ast_matchers::internal::VariadicDynCastAllOfMatcher<Stmt, CXXTypeidExpr>
204 cxxTypeidExpr;
205
206AST_MATCHER(CXXTypeidExpr, isPotentiallyEvaluated) {
207 return Node.isPotentiallyEvaluated();
208}
209
210AST_MATCHER(CXXMemberCallExpr, isConstCallee) {
211 const Decl *CalleeDecl = Node.getCalleeDecl();
212 const auto *VD = dyn_cast_or_null<ValueDecl>(CalleeDecl);
213 if (!VD)
214 return false;
215 const QualType T = VD->getType().getCanonicalType();
216 const auto *MPT = dyn_cast<MemberPointerType>(T);
217 const auto *FPT = MPT ? cast<FunctionProtoType>(MPT->getPointeeType())
218 : dyn_cast<FunctionProtoType>(T);
219 if (!FPT)
220 return false;
221 return FPT->isConst();
222}
223
224AST_MATCHER_P(GenericSelectionExpr, hasControllingExpr,
225 ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
226 if (Node.isTypePredicate())
227 return false;
228 return InnerMatcher.matches(*Node.getControllingExpr(), Finder, Builder);
229}
230
231template <typename T>
232ast_matchers::internal::Matcher<T>
233findFirst(const ast_matchers::internal::Matcher<T> &Matcher) {
234 return anyOf(Matcher, hasDescendant(Matcher));
235}
236
237const auto nonConstReferenceType = [] {
238 return hasUnqualifiedDesugaredType(
239 referenceType(pointee(unless(isConstQualified()))));
240};
241
242const auto constReferenceToPointerWithNonConstPointeeType = [] {
243 return hasUnqualifiedDesugaredType(referenceType(pointee(qualType(
244 isConstQualified(), hasUnqualifiedDesugaredType(pointerType(
245 pointee(unless(isConstQualified()))))))));
246};
247
248const auto nonConstPointerType = [] {
249 return hasUnqualifiedDesugaredType(
250 pointerType(pointee(unless(isConstQualified()))));
251};
252
253const auto isMoveOnly = [] {
254 return cxxRecordDecl(
255 hasMethod(cxxConstructorDecl(isMoveConstructor(), unless(isDeleted()))),
256 hasMethod(cxxMethodDecl(isMoveAssignmentOperator(), unless(isDeleted()))),
257 unless(anyOf(hasMethod(cxxConstructorDecl(isCopyConstructor(),
258 unless(isDeleted()))),
259 hasMethod(cxxMethodDecl(isCopyAssignmentOperator(),
260 unless(isDeleted()))))));
261};
262
263template <class T> struct NodeID;
264template <> struct NodeID<Expr> {
265 static constexpr StringRef value = "expr";
266};
267template <> struct NodeID<Decl> {
268 static constexpr StringRef value = "decl";
269};
270
271template <class T,
272 class F = const Stmt *(ExprMutationAnalyzer::Analyzer::*)(const T *)>
273const Stmt *tryEachMatch(ArrayRef<ast_matchers::BoundNodes> Matches,
274 ExprMutationAnalyzer::Analyzer *Analyzer, F Finder) {
275 const StringRef ID = NodeID<T>::value;
276 for (const auto &Nodes : Matches) {
277 if (const Stmt *S = (Analyzer->*Finder)(Nodes.getNodeAs<T>(ID)))
278 return S;
279 }
280 return nullptr;
281}
282
283} // namespace
284
286 return findMutationMemoized(
287 Exp,
288 {&ExprMutationAnalyzer::Analyzer::findDirectMutation,
289 &ExprMutationAnalyzer::Analyzer::findMemberMutation,
290 &ExprMutationAnalyzer::Analyzer::findArrayElementMutation,
291 &ExprMutationAnalyzer::Analyzer::findCastMutation,
292 &ExprMutationAnalyzer::Analyzer::findRangeLoopMutation,
293 &ExprMutationAnalyzer::Analyzer::findReferenceMutation,
294 &ExprMutationAnalyzer::Analyzer::findFunctionArgMutation},
295 Memorized.Results);
296}
297
301
302const Stmt *
304 return findMutationMemoized(
305 Exp,
306 {
307 &ExprMutationAnalyzer::Analyzer::findPointeeValueMutation,
308 &ExprMutationAnalyzer::Analyzer::findPointeeMemberMutation,
309 &ExprMutationAnalyzer::Analyzer::findPointeeToNonConst,
310 },
311 Memorized.PointeeResults);
312}
313
314const Stmt *
319
320const Stmt *ExprMutationAnalyzer::Analyzer::findMutationMemoized(
321 const Expr *Exp, llvm::ArrayRef<MutationFinder> Finders,
322 Memoized::ResultMap &MemoizedResults) {
323 // Assume Exp is not mutated before analyzing Exp.
324 auto [Memoized, Inserted] = MemoizedResults.try_emplace(Exp);
325 if (!Inserted)
326 return Memoized->second;
327
328 if (ExprMutationAnalyzer::isUnevaluated(Exp, Context))
329 return nullptr;
330
331 for (const auto &Finder : Finders) {
332 if (const Stmt *S = (this->*Finder)(Exp))
333 return MemoizedResults[Exp] = S;
334 }
335
336 return nullptr;
337}
338
339const Stmt *
340ExprMutationAnalyzer::Analyzer::tryEachDeclRef(const Decl *Dec,
341 MutationFinder Finder) {
342 const auto Refs = match(
343 findAll(
344 declRefExpr(to(
345 // `Dec` or a binding if `Dec` is a decomposition.
346 anyOf(equalsNode(Dec),
347 bindingDecl(forDecomposition(equalsNode(Dec))))
348 //
349 ))
350 .bind(NodeID<Expr>::value)),
351 Stm, Context);
352 for (const auto &RefNodes : Refs) {
353 const auto *E = RefNodes.getNodeAs<Expr>(NodeID<Expr>::value);
354 if ((this->*Finder)(E))
355 return E;
356 }
357 return nullptr;
358}
359
361 return !match(stmt(anyOf(
362 // `Exp` is part of the underlying expression of
363 // decltype/typeof if it has an ancestor of
364 // typeLoc.
368 // `UnaryExprOrTypeTraitExpr` is unevaluated
369 // unless it's sizeof on VLA.
371 hasArgumentOfType(variableArrayType())))),
372 // `CXXTypeidExpr` is unevaluated unless it's
373 // applied to an expression of glvalue of
374 // polymorphic class type.
375 cxxTypeidExpr(unless(isPotentiallyEvaluated())),
376 // The controlling expression of
377 // `GenericSelectionExpr` is unevaluated.
379 hasControllingExpr(hasDescendant(equalsNode(Stm)))),
380 cxxNoexceptExpr()))))),
381 *Stm, Context)
382 .empty();
383}
384
385const Stmt *
386ExprMutationAnalyzer::Analyzer::findExprMutation(ArrayRef<BoundNodes> Matches) {
387 return tryEachMatch<Expr>(Matches, this,
389}
390
391const Stmt *
392ExprMutationAnalyzer::Analyzer::findDeclMutation(ArrayRef<BoundNodes> Matches) {
393 return tryEachMatch<Decl>(Matches, this,
395}
396
397const Stmt *ExprMutationAnalyzer::Analyzer::findExprPointeeMutation(
398 ArrayRef<ast_matchers::BoundNodes> Matches) {
399 return tryEachMatch<Expr>(
401}
402
403const Stmt *ExprMutationAnalyzer::Analyzer::findDeclPointeeMutation(
404 ArrayRef<ast_matchers::BoundNodes> Matches) {
405 return tryEachMatch<Decl>(
407}
408
409const Stmt *
410ExprMutationAnalyzer::Analyzer::findDirectMutation(const Expr *Exp) {
411 // LHS of any assignment operators.
412 const auto AsAssignmentLhs =
413 binaryOperator(isAssignmentOperator(), hasLHS(canResolveToExpr(Exp)));
414
415 // Operand of increment/decrement operators.
416 const auto AsIncDecOperand =
417 unaryOperator(anyOf(hasOperatorName("++"), hasOperatorName("--")),
418 hasUnaryOperand(canResolveToExpr(Exp)));
419
420 // Invoking non-const member function.
421 // A member function is assumed to be non-const when it is unresolved.
422 const auto NonConstMethod = cxxMethodDecl(unless(isConst()));
423
424 const auto AsNonConstThis = expr(anyOf(
425 cxxMemberCallExpr(on(canResolveToExpr(Exp)), unless(isConstCallee())),
426 cxxOperatorCallExpr(callee(NonConstMethod),
427 hasArgument(0, canResolveToExpr(Exp))),
428 // In case of a templated type, calling overloaded operators is not
429 // resolved and modelled as `binaryOperator` on a dependent type.
430 // Such instances are considered a modification, because they can modify
431 // in different instantiations of the template.
432 binaryOperator(isTypeDependent(),
433 hasEitherOperand(ignoringImpCasts(canResolveToExpr(Exp)))),
434 // A fold expression may contain `Exp` as it's initializer.
435 // We don't know if the operator modifies `Exp` because the
436 // operator is type dependent due to the parameter pack.
437 cxxFoldExpr(hasFoldInit(ignoringImpCasts(canResolveToExpr(Exp)))),
438 // Within class templates and member functions the member expression might
439 // not be resolved. In that case, the `callExpr` is considered to be a
440 // modification.
441 callExpr(callee(expr(anyOf(
442 unresolvedMemberExpr(hasObjectExpression(canResolveToExpr(Exp))),
444 hasObjectExpression(canResolveToExpr(Exp))))))),
445 // Match on a call to a known method, but the call itself is type
446 // dependent (e.g. `vector<T> v; v.push(T{});` in a templated function).
448 isTypeDependent(),
449 callee(memberExpr(hasDeclaration(NonConstMethod),
450 hasObjectExpression(canResolveToExpr(Exp))))))));
451
452 // Taking address of 'Exp'.
453 // We're assuming 'Exp' is mutated as soon as its address is taken, though in
454 // theory we can follow the pointer and see whether it escaped `Stm` or is
455 // dereferenced and then mutated. This is left for future improvements.
456 const auto AsAmpersandOperand =
457 unaryOperator(hasOperatorName("&"),
458 // A NoOp implicit cast is adding const.
459 unless(hasParent(implicitCastExpr(hasCastKind(CK_NoOp)))),
460 hasUnaryOperand(canResolveToExpr(Exp)));
461 const auto AsPointerFromArrayDecay = castExpr(
462 hasCastKind(CK_ArrayToPointerDecay),
463 unless(hasParent(arraySubscriptExpr())), has(canResolveToExpr(Exp)));
464 // Treat calling `operator->()` of move-only classes as taking address.
465 // These are typically smart pointers with unique ownership so we treat
466 // mutation of pointee as mutation of the smart pointer itself.
467 const auto AsOperatorArrowThis = cxxOperatorCallExpr(
469 callee(
470 cxxMethodDecl(ofClass(isMoveOnly()), returns(nonConstPointerType()))),
471 argumentCountIs(1), hasArgument(0, canResolveToExpr(Exp)));
472
473 // Used as non-const-ref argument when calling a function.
474 // An argument is assumed to be non-const-ref when the function is unresolved.
475 // Instantiated template functions are not handled here but in
476 // findFunctionArgMutation which has additional smarts for handling forwarding
477 // references.
478 const auto NonConstRefParam = forEachArgumentWithParamType(
479 anyOf(canResolveToExpr(Exp),
481 hasObjectExpression(ignoringImpCasts(canResolveToExpr(Exp))))),
482 nonConstReferenceType());
483 const auto NotInstantiated = unless(hasDeclaration(isInstantiated()));
484
485 const auto AsNonConstRefArg =
486 anyOf(callExpr(NonConstRefParam, NotInstantiated),
487 cxxConstructExpr(NonConstRefParam, NotInstantiated),
488 // If the call is type-dependent, we can't properly process any
489 // argument because required type conversions and implicit casts
490 // will be inserted only after specialization.
491 callExpr(isTypeDependent(), hasAnyArgument(canResolveToExpr(Exp))),
492 cxxUnresolvedConstructExpr(hasAnyArgument(canResolveToExpr(Exp))),
493 // Previous False Positive in the following Code:
494 // `template <typename T> void f() { int i = 42; new Type<T>(i); }`
495 // Where the constructor of `Type` takes its argument as reference.
496 // The AST does not resolve in a `cxxConstructExpr` because it is
497 // type-dependent.
498 parenListExpr(hasDescendant(expr(canResolveToExpr(Exp)))),
499 // If the initializer is for a reference type, there is no cast for
500 // the variable. Values are cast to RValue first.
501 initListExpr(hasAnyInit(expr(canResolveToExpr(Exp)))));
502
503 // Captured by a lambda by reference.
504 // If we're initializing a capture with 'Exp' directly then we're initializing
505 // a reference capture.
506 // For value captures there will be an ImplicitCastExpr <LValueToRValue>.
507 const auto AsLambdaRefCaptureInit = lambdaExpr(hasCaptureInit(Exp));
508
509 // Returned as non-const-ref.
510 // If we're returning 'Exp' directly then it's returned as non-const-ref.
511 // For returning by value there will be an ImplicitCastExpr <LValueToRValue>.
512 // For returning by const-ref there will be an ImplicitCastExpr <NoOp> (for
513 // adding const.)
514 const auto AsNonConstRefReturn =
515 returnStmt(hasReturnValue(canResolveToExpr(Exp)));
516
517 // It is used as a non-const-reference for initializing a range-for loop.
518 const auto AsNonConstRefRangeInit = cxxForRangeStmt(hasRangeInit(declRefExpr(
519 allOf(canResolveToExpr(Exp), hasType(nonConstReferenceType())))));
520
521 const auto Matches = match(
522 traverse(
523 TK_AsIs,
524 findFirst(stmt(anyOf(AsAssignmentLhs, AsIncDecOperand, AsNonConstThis,
525 AsAmpersandOperand, AsPointerFromArrayDecay,
526 AsOperatorArrowThis, AsNonConstRefArg,
527 AsLambdaRefCaptureInit, AsNonConstRefReturn,
528 AsNonConstRefRangeInit))
529 .bind("stmt"))),
530 Stm, Context);
531 return selectFirst<Stmt>("stmt", Matches);
532}
533
534const Stmt *
535ExprMutationAnalyzer::Analyzer::findMemberMutation(const Expr *Exp) {
536 // Check whether any member of 'Exp' is mutated.
537 const auto MemberExprs = match(
538 findAll(expr(anyOf(memberExpr(hasObjectExpression(canResolveToExpr(Exp))),
540 hasObjectExpression(canResolveToExpr(Exp))),
541 binaryOperator(hasOperatorName(".*"),
542 hasLHS(equalsNode(Exp)))))
543 .bind(NodeID<Expr>::value)),
544 Stm, Context);
545 return findExprMutation(MemberExprs);
546}
547
548const Stmt *
549ExprMutationAnalyzer::Analyzer::findArrayElementMutation(const Expr *Exp) {
550 // Check whether any element of an array is mutated.
551 const auto SubscriptExprs = match(
553 anyOf(hasBaseConservative(canResolveToExpr(Exp)),
554 hasBaseConservative(implicitCastExpr(allOf(
555 hasCastKind(CK_ArrayToPointerDecay),
556 hasSourceExpression(canResolveToExpr(Exp)))))))
557 .bind(NodeID<Expr>::value)),
558 Stm, Context);
559 return findExprMutation(SubscriptExprs);
560}
561
562const Stmt *ExprMutationAnalyzer::Analyzer::findCastMutation(const Expr *Exp) {
563 // If the 'Exp' is explicitly casted to a non-const reference type the
564 // 'Exp' is considered to be modified.
565 const auto ExplicitCast =
566 match(findFirst(stmt(castExpr(hasSourceExpression(canResolveToExpr(Exp)),
567 explicitCastExpr(hasDestinationType(
568 nonConstReferenceType()))))
569 .bind("stmt")),
570 Stm, Context);
571
572 if (const auto *CastStmt = selectFirst<Stmt>("stmt", ExplicitCast))
573 return CastStmt;
574
575 // If 'Exp' is casted to any non-const reference type, check the castExpr.
576 const auto Casts = match(
577 findAll(expr(castExpr(hasSourceExpression(canResolveToExpr(Exp)),
578 anyOf(explicitCastExpr(hasDestinationType(
579 nonConstReferenceType())),
580 implicitCastExpr(hasImplicitDestinationType(
581 nonConstReferenceType())))))
582 .bind(NodeID<Expr>::value)),
583 Stm, Context);
584
585 if (const Stmt *S = findExprMutation(Casts))
586 return S;
587 // Treat std::{move,forward} as cast.
588 const auto Calls =
590 hasAnyName("::std::move", "::std::forward"))),
591 hasArgument(0, canResolveToExpr(Exp)))
592 .bind("expr")),
593 Stm, Context);
594 return findExprMutation(Calls);
595}
596
597const Stmt *
598ExprMutationAnalyzer::Analyzer::findRangeLoopMutation(const Expr *Exp) {
599 // Keep the ordering for the specific initialization matches to happen first,
600 // because it is cheaper to match all potential modifications of the loop
601 // variable.
602
603 // The range variable is a reference to a builtin array. In that case the
604 // array is considered modified if the loop-variable is a non-const reference.
605 const auto DeclStmtToNonRefToArray = declStmt(hasSingleDecl(varDecl(hasType(
606 hasUnqualifiedDesugaredType(referenceType(pointee(arrayType())))))));
607 const auto RefToArrayRefToElements = match(
608 findFirst(stmt(cxxForRangeStmt(
609 hasLoopVariable(
610 varDecl(anyOf(hasType(nonConstReferenceType()),
611 hasType(nonConstPointerType())))
612 .bind(NodeID<Decl>::value)),
613 hasRangeStmt(DeclStmtToNonRefToArray),
614 hasRangeInit(canResolveToExpr(Exp))))
615 .bind("stmt")),
616 Stm, Context);
617
618 if (const auto *BadRangeInitFromArray =
619 selectFirst<Stmt>("stmt", RefToArrayRefToElements))
620 return BadRangeInitFromArray;
621
622 // Small helper to match special cases in range-for loops.
623 //
624 // It is possible that containers do not provide a const-overload for their
625 // iterator accessors. If this is the case, the variable is used non-const
626 // no matter what happens in the loop. This requires special detection as it
627 // is then faster to find all mutations of the loop variable.
628 // It aims at a different modification as well.
629 const auto HasAnyNonConstIterator =
630 anyOf(allOf(hasMethod(allOf(hasName("begin"), unless(isConst()))),
631 unless(hasMethod(allOf(hasName("begin"), isConst())))),
632 allOf(hasMethod(allOf(hasName("end"), unless(isConst()))),
633 unless(hasMethod(allOf(hasName("end"), isConst())))));
634
635 const auto DeclStmtToNonConstIteratorContainer = declStmt(
636 hasSingleDecl(varDecl(hasType(hasUnqualifiedDesugaredType(referenceType(
637 pointee(hasDeclaration(cxxRecordDecl(HasAnyNonConstIterator)))))))));
638
639 const auto RefToContainerBadIterators = match(
640 findFirst(stmt(cxxForRangeStmt(allOf(
641 hasRangeStmt(DeclStmtToNonConstIteratorContainer),
642 hasRangeInit(canResolveToExpr(Exp)))))
643 .bind("stmt")),
644 Stm, Context);
645
646 if (const auto *BadIteratorsContainer =
647 selectFirst<Stmt>("stmt", RefToContainerBadIterators))
648 return BadIteratorsContainer;
649
650 // If range for looping over 'Exp' with a non-const reference loop variable,
651 // check all declRefExpr of the loop variable.
652 const auto LoopVars =
654 hasLoopVariable(varDecl(hasType(nonConstReferenceType()))
655 .bind(NodeID<Decl>::value)),
656 hasRangeInit(canResolveToExpr(Exp)))),
657 Stm, Context);
658 return findDeclMutation(LoopVars);
659}
660
661const Stmt *
662ExprMutationAnalyzer::Analyzer::findReferenceMutation(const Expr *Exp) {
663 // Follow non-const reference returned by `operator*()` of move-only classes.
664 // These are typically smart pointers with unique ownership so we treat
665 // mutation of pointee as mutation of the smart pointer itself.
666 const auto Ref = match(
669 callee(cxxMethodDecl(ofClass(isMoveOnly()),
670 returns(nonConstReferenceType()))),
671 argumentCountIs(1), hasArgument(0, canResolveToExpr(Exp)))
672 .bind(NodeID<Expr>::value)),
673 Stm, Context);
674 if (const Stmt *S = findExprMutation(Ref))
675 return S;
676
677 // If 'Exp' is bound to a non-const reference, check all declRefExpr to that.
678 const auto Refs = match(
680 varDecl(hasType(nonConstReferenceType()),
681 hasInitializer(anyOf(
682 canResolveToExpr(Exp),
683 memberExpr(hasObjectExpression(canResolveToExpr(Exp))))),
684 hasParent(declStmt().bind("stmt")),
685 // Don't follow the reference in range statement, we've
686 // handled that separately.
688 hasRangeStmt(equalsBoundNode("stmt"))))))))
689 .bind(NodeID<Decl>::value))),
690 Stm, Context);
691 return findDeclMutation(Refs);
692}
693
694const Stmt *
695ExprMutationAnalyzer::Analyzer::findFunctionArgMutation(const Expr *Exp) {
696 const auto NonConstRefParam = forEachArgumentWithParam(
697 canResolveToExpr(Exp),
698 parmVarDecl(hasType(nonConstReferenceType())).bind("parm"));
699 const auto IsInstantiated = hasDeclaration(isInstantiated());
700 const auto FuncDecl = hasDeclaration(functionDecl().bind("func"));
701 const auto Matches = match(
702 traverse(
703 TK_AsIs,
704 findAll(
705 expr(anyOf(callExpr(NonConstRefParam, IsInstantiated, FuncDecl,
707 "::std::move", "::std::forward"))))),
708 cxxConstructExpr(NonConstRefParam, IsInstantiated,
709 FuncDecl)))
710 .bind(NodeID<Expr>::value))),
711 Stm, Context);
712 for (const auto &Nodes : Matches) {
713 const auto *Exp = Nodes.getNodeAs<Expr>(NodeID<Expr>::value);
714 const auto *Func = Nodes.getNodeAs<FunctionDecl>("func");
715 if (!Func->getBody() || !Func->getPrimaryTemplate())
716 return Exp;
717
718 const auto *Parm = Nodes.getNodeAs<ParmVarDecl>("parm");
719 const ArrayRef<ParmVarDecl *> AllParams =
720 Func->getPrimaryTemplate()->getTemplatedDecl()->parameters();
721 QualType ParmType =
722 AllParams[std::min<size_t>(Parm->getFunctionScopeIndex(),
723 AllParams.size() - 1)]
724 ->getType();
725 if (const auto *T = ParmType->getAs<PackExpansionType>())
726 ParmType = T->getPattern();
727
728 // If param type is forwarding reference, follow into the function
729 // definition and see whether the param is mutated inside.
730 if (const auto *RefType = ParmType->getAs<RValueReferenceType>()) {
731 if (!RefType->getPointeeType().getQualifiers() &&
733 RefType->getPointeeType().getCanonicalType())) {
736 *Func, Context, Memorized);
737 if (Analyzer->findMutation(Parm))
738 return Exp;
739 continue;
740 }
741 }
742 // Not forwarding reference.
743 return Exp;
744 }
745 return nullptr;
746}
747
748const Stmt *
749ExprMutationAnalyzer::Analyzer::findPointeeValueMutation(const Expr *Exp) {
750 const auto Matches = match(
752 expr(anyOf(
753 // deref by *
754 unaryOperator(hasOperatorName("*"),
755 hasUnaryOperand(canResolveToExprPointee(Exp))),
756 // deref by []
758 hasBaseConservative(canResolveToExprPointee(Exp)))))
759 .bind(NodeID<Expr>::value))),
760 Stm, Context);
761 return findExprMutation(Matches);
762}
763
764const Stmt *
765ExprMutationAnalyzer::Analyzer::findPointeeMemberMutation(const Expr *Exp) {
766 const Stmt *MemberCallExpr = selectFirst<Stmt>(
768 cxxMemberCallExpr(on(canResolveToExprPointee(Exp)),
769 unless(isConstCallee()))
770 .bind("stmt"))),
771 Stm, Context));
772 if (MemberCallExpr)
773 return MemberCallExpr;
774 const auto Matches = match(
777 hasObjectExpression(canResolveToExprPointee(Exp))),
778 binaryOperator(hasOperatorName("->*"),
779 hasLHS(canResolveToExprPointee(Exp)))))
780 .bind(NodeID<Expr>::value))),
781 Stm, Context);
782 return findExprMutation(Matches);
783}
784
785const Stmt *
786ExprMutationAnalyzer::Analyzer::findPointeeToNonConst(const Expr *Exp) {
787 const auto NonConstPointerOrNonConstRefOrDependentType = type(anyOf(
788 nonConstPointerType(), nonConstReferenceType(),
789 constReferenceToPointerWithNonConstPointeeType(), isDependentType()));
790
791 // assign
792 const auto InitToNonConst =
793 varDecl(hasType(NonConstPointerOrNonConstRefOrDependentType),
794 hasInitializer(expr(canResolveToExprPointee(Exp)).bind("stmt")));
795 const auto AssignToNonConst = binaryOperation(
796 hasOperatorName("="),
797 hasLHS(expr(hasType(NonConstPointerOrNonConstRefOrDependentType))),
798 hasRHS(canResolveToExprPointee(Exp)));
799 // arguments like
800 const auto ArgOfInstantiationDependent = allOf(
801 hasAnyArgument(canResolveToExprPointee(Exp)), isInstantiationDependent());
802 const auto ArgOfNonConstParameter =
803 forEachArgumentWithParamType(canResolveToExprPointee(Exp),
804 NonConstPointerOrNonConstRefOrDependentType);
805 const auto CallLikeMatcher =
806 anyOf(ArgOfNonConstParameter, ArgOfInstantiationDependent);
807 const auto PassAsNonConstArg =
808 expr(anyOf(cxxUnresolvedConstructExpr(ArgOfInstantiationDependent),
809 cxxNewExpr(hasAnyPlacementArg(
810 ignoringParenImpCasts(canResolveToExprPointee(Exp)))),
811 cxxConstructExpr(CallLikeMatcher), callExpr(CallLikeMatcher),
812 parenListExpr(has(canResolveToExprPointee(Exp))),
813 initListExpr(hasAnyInit(canResolveToExprPointee(Exp)))));
814 // cast
815 const auto CastToNonConst = explicitCastExpr(
816 hasSourceExpression(canResolveToExprPointee(Exp)),
817 hasDestinationType(NonConstPointerOrNonConstRefOrDependentType));
818
819 // capture
820 // FIXME: false positive if the pointee does not change in lambda
821 const auto CaptureNoConst = lambdaExpr(hasCaptureInit(Exp));
822
823 const auto ReturnNoConst =
824 returnStmt(hasReturnValue(canResolveToExprPointee(Exp)));
825
826 const auto Matches = match(
828 stmt(anyOf(AssignToNonConst, PassAsNonConstArg,
829 CastToNonConst, CaptureNoConst, ReturnNoConst))
830 .bind("stmt")),
831 forEachDescendant(InitToNonConst))),
832 Stm, Context);
833 return selectFirst<Stmt>("stmt", Matches);
834}
835
836FunctionParmMutationAnalyzer::FunctionParmMutationAnalyzer(
837 const FunctionDecl &Func, ASTContext &Context,
838 ExprMutationAnalyzer::Memoized &Memorized)
839 : BodyAnalyzer(*Func.getBody(), Context, Memorized) {
840 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(&Func)) {
841 // CXXCtorInitializer might also mutate Param but they're not part of
842 // function body, check them eagerly here since they're typically trivial.
843 for (const CXXCtorInitializer *Init : Ctor->inits()) {
844 ExprMutationAnalyzer::Analyzer InitAnalyzer(*Init->getInit(), Context,
845 Memorized);
846 for (const ParmVarDecl *Parm : Ctor->parameters()) {
847 if (Results.contains(Parm))
848 continue;
849 if (const Stmt *S = InitAnalyzer.findMutation(Parm))
850 Results[Parm] = S;
851 }
852 }
853 }
854}
855
856const Stmt *
858 auto [Place, Inserted] = Results.try_emplace(Parm);
859 if (!Inserted)
860 return Place->second;
861
862 // To handle call A -> call B -> call A. Assume parameters of A is not mutated
863 // before analyzing parameters of A. Then when analyzing the second "call A",
864 // FunctionParmMutationAnalyzer can use this memoized value to avoid infinite
865 // recursion.
866 return Place->second = BodyAnalyzer.findMutation(Parm);
867}
868
869} // namespace clang
#define AST_MATCHER(Type, DefineMatcher)
AST_MATCHER(Type, DefineMatcher) { ... } defines a zero parameter function named DefineMatcher() that...
#define AST_MATCHER_P(Type, DefineMatcher, ParamType, Param)
AST_MATCHER_P(Type, DefineMatcher, ParamType, Param) { ... } defines a single-parameter function name...
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
CastType
Definition SemaCast.cpp:50
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:227
CXXForRangeStmt - This represents C++0x [stmt.ranged]'s ranged for statement, represented as 'for (ra...
Definition StmtCXX.h:135
Represents a call to a member function that may be written either with member call syntax (e....
Definition ExprCXX.h:183
A C++ typeid expression (C++ [expr.typeid]), which gets the type_info that corresponds to the supplie...
Definition ExprCXX.h:852
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Definition Stmt.h:1641
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
static bool isUnevaluated(const Stmt *Stm, ASTContext &Context)
check whether stmt is unevaluated.
This represents one expression.
Definition Expr.h:112
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3093
static FunctionParmMutationAnalyzer * getFunctionParmMutationAnalyzer(const FunctionDecl &Func, ASTContext &Context, ExprMutationAnalyzer::Memoized &Memorized)
const Stmt * findMutation(const ParmVarDecl *Parm)
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5369
bool isConst() const
Definition TypeBase.h:4927
Represents a C11 generic selection.
Definition Expr.h:6182
Describes an C or C++ initializer list.
Definition Expr.h:5302
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition ExprCXX.h:1972
Represents a parameter to a function.
Definition Decl.h:1808
A (possibly-)qualified type.
Definition TypeBase.h:937
Stmt - This represents one statement.
Definition Stmt.h:86
The base class of the type hierarchy.
Definition TypeBase.h:1875
const internal::VariadicDynCastAllOfMatcher< Decl, VarDecl > varDecl
Matches variable declarations.
const internal::VariadicDynCastAllOfMatcher< Stmt, DeclRefExpr > declRefExpr
Matches expressions that refer to declarations.
const internal::VariadicOperatorMatcherFunc< 1, 1 > unless
Matches if the provided matcher does not match.
const internal::VariadicDynCastAllOfMatcher< Stmt, ImplicitCastExpr > implicitCastExpr
Matches the implicit cast nodes of Clang's AST.
const internal::ArgumentAdaptingMatcherFunc< internal::HasDescendantMatcher > hasDescendant
Matches AST nodes that have descendant AST nodes that match the provided matcher.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXDependentScopeMemberExpr > cxxDependentScopeMemberExpr
Matches member expressions where the actual member referenced could not be resolved because the base ...
const internal::VariadicDynCastAllOfMatcher< Decl, BindingDecl > bindingDecl
Matches binding declarations Example matches foo and bar (matcher = bindingDecl()
const internal::VariadicDynCastAllOfMatcher< Decl, ParmVarDecl > parmVarDecl
Matches parameter variable declarations.
const AstTypeMatcher< VariableArrayType > variableArrayType
const internal::VariadicDynCastAllOfMatcher< Stmt, GenericSelectionExpr > genericSelectionExpr
Matches C11 _Generic expression.
const internal::VariadicDynCastAllOfMatcher< Stmt, ReturnStmt > returnStmt
Matches return statements.
internal::Matcher< NamedDecl > hasName(StringRef Name)
Matches NamedDecl nodes that have the specified name.
const internal::VariadicDynCastAllOfMatcher< Stmt, CallExpr > callExpr
Matches call expressions.
const internal::VariadicDynCastAllOfMatcher< Stmt, LambdaExpr > lambdaExpr
Matches lambda expressions.
const internal::VariadicDynCastAllOfMatcher< Stmt, UnaryExprOrTypeTraitExpr > unaryExprOrTypeTraitExpr
Matches sizeof (C99), alignof (C++11) and vec_step (OpenCL)
const internal::ArgumentAdaptingMatcherFunc< internal::ForEachDescendantMatcher > forEachDescendant
Matches AST nodes that have descendant AST nodes that match the provided matcher.
SmallVector< BoundNodes, 1 > match(MatcherT Matcher, const NodeT &Node, ASTContext &Context)
Returns the results of matching Matcher on Node.
const internal::VariadicDynCastAllOfMatcher< Decl, NamedDecl > namedDecl
Matches a declaration of anything that could have a name.
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicAllOfMatcher< TypeLoc > typeLoc
Matches TypeLocs in the clang AST.
const internal::VariadicDynCastAllOfMatcher< Stmt, ParenListExpr > parenListExpr
Matches paren list expressions.
const AstTypeMatcher< ArrayType > arrayType
const internal::VariadicDynCastAllOfMatcher< Stmt, UnaryOperator > unaryOperator
Matches unary operator expressions.
const internal::VariadicFunction< internal::Matcher< NamedDecl >, StringRef, internal::hasAnyNameFunc > hasAnyName
Matches NamedDecl nodes that have any of the specified names.
const internal::MapAnyOfMatcher< BinaryOperator, CXXOperatorCallExpr, CXXRewrittenBinaryOperator > binaryOperation
Matches nodes which can be used with binary operators.
const internal::VariadicDynCastAllOfMatcher< Stmt, ArraySubscriptExpr > arraySubscriptExpr
Matches array subscript expressions.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXForRangeStmt > cxxForRangeStmt
Matches range-based for statements.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXMemberCallExpr > cxxMemberCallExpr
Matches member call expressions.
const internal::VariadicDynCastAllOfMatcher< Decl, CXXConstructorDecl > cxxConstructorDecl
Matches C++ constructor declarations.
internal::BindableMatcher< Stmt > sizeOfExpr(const internal::Matcher< UnaryExprOrTypeTraitExpr > &InnerMatcher)
Same as unaryExprOrTypeTraitExpr, but only matching sizeof.
const internal::VariadicDynCastAllOfMatcher< Stmt, InitListExpr > initListExpr
Matches init list expressions.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXNoexceptExpr > cxxNoexceptExpr
Matches noexcept expressions.
const NodeT * selectFirst(StringRef BoundTo, const SmallVectorImpl< BoundNodes > &Results)
Returns the first result of type NodeT bound to BoundTo.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXNewExpr > cxxNewExpr
Matches new expressions.
const internal::VariadicDynCastAllOfMatcher< Stmt, BinaryOperator > binaryOperator
Matches binary operator expressions.
const internal::ArgumentAdaptingMatcherFunc< internal::HasMatcher > has
Matches AST nodes that have child AST nodes that match the provided matcher.
const internal::VariadicDynCastAllOfMatcher< Stmt, ExplicitCastExpr > explicitCastExpr
Matches explicit cast expressions.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXConstructExpr > cxxConstructExpr
Matches constructor call expressions (including implicit ones).
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXOperatorCallExpr > cxxOperatorCallExpr
Matches overloaded operator calls.
const AstTypeMatcher< PointerType > pointerType
internal::PolymorphicMatcher< internal::HasOverloadedOperatorNameMatcher, AST_POLYMORPHIC_SUPPORTED_TYPES(CXXOperatorCallExpr, FunctionDecl), std::vector< std::string > > hasOverloadedOperatorName(StringRef Name)
Matches overloaded operator names.
const internal::VariadicOperatorMatcherFunc< 2, std::numeric_limits< unsigned >::max()> allOf
Matches if all given matchers match.
const internal::VariadicDynCastAllOfMatcher< Decl, FunctionDecl > functionDecl
Matches function declarations.
const internal::VariadicDynCastAllOfMatcher< Stmt, UnresolvedMemberExpr > unresolvedMemberExpr
Matches unresolved member expressions.
const internal::VariadicDynCastAllOfMatcher< Stmt, MemberExpr > memberExpr
Matches member expressions.
const internal::VariadicDynCastAllOfMatcher< Decl, CXXRecordDecl > cxxRecordDecl
Matches C++ class declarations.
internal::Matcher< T > traverse(TraversalKind TK, const internal::Matcher< T > &InnerMatcher)
Causes all nested matchers to be matched with the specified traversal kind.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXUnresolvedConstructExpr > cxxUnresolvedConstructExpr
Matches unresolved constructor call expressions.
internal::Matcher< T > findAll(const internal::Matcher< T > &Matcher)
Matches if the node or any descendant matches.
internal::PolymorphicMatcher< internal::HasDeclarationMatcher, void(internal::HasDeclarationSupportedTypes), internal::Matcher< Decl > > hasDeclaration(const internal::Matcher< Decl > &InnerMatcher)
Matches a node if the declaration associated with that node matches the given matcher.
const internal::VariadicDynCastAllOfMatcher< Stmt, DeclStmt > declStmt
Matches declaration statements.
const internal::VariadicAllOfMatcher< Stmt > stmt
Matches statements.
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
const internal::VariadicDynCastAllOfMatcher< Stmt, CXXFoldExpr > cxxFoldExpr
Matches C++17 fold expressions.
const internal::VariadicOperatorMatcherFunc< 2, std::numeric_limits< unsigned >::max()> anyOf
Matches if any of the given matchers matches.
const internal::VariadicDynCastAllOfMatcher< Decl, CXXMethodDecl > cxxMethodDecl
Matches method declarations.
const internal::VariadicDynCastAllOfMatcher< Stmt, CastExpr > castExpr
Matches any cast nodes of Clang's AST.
const internal::VariadicAllOfMatcher< QualType > qualType
Matches QualTypes in the clang AST.
const internal::ArgumentAdaptingMatcherFunc< internal::HasAncestorMatcher, internal::TypeList< Decl, NestedNameSpecifierLoc, Stmt, TypeLoc, Attr >, internal::TypeList< Decl, NestedNameSpecifierLoc, Stmt, TypeLoc, Attr > > hasAncestor
Matches AST nodes that have an ancestor that matches the provided matcher.
const internal::ArgumentAdaptingMatcherFunc< internal::HasParentMatcher, internal::TypeList< Decl, NestedNameSpecifierLoc, Stmt, TypeLoc, Attr >, internal::TypeList< Decl, NestedNameSpecifierLoc, Stmt, TypeLoc, Attr > > hasParent
Matches AST nodes that have a parent that matches the provided matcher.
const AstTypeMatcher< ReferenceType > referenceType
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ TK_AsIs
Will traverse all child nodes.
@ Result
The result type of a method or function.
Definition TypeBase.h:905
@ Type
The name was classified as a type.
Definition Sema.h:564
CastKind
CastKind - The kind of operation required for a conversion.
static bool canExprResolveTo(const Expr *Source, const Expr *Target)
U cast(CodeGen::Address addr)
Definition Address.h:327
const Stmt * findPointeeMutation(const Expr *Exp)
const Stmt * findMutation(const Expr *Exp)
llvm::DenseMap< const Expr *, const Stmt * > ResultMap