clang 22.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 *ICE = dyn_cast<ImplicitCastExpr>(E)) {
139 // only implicit cast needs to be treated as resolvable.
140 // explicit cast will be checked in `findPointeeToNonConst`
141 const CastKind kind = ICE->getCastKind();
142 if (kind == CK_LValueToRValue || kind == CK_DerivedToBase ||
143 kind == CK_UncheckedDerivedToBase ||
144 (kind == CK_NoOp && (ICE->getType() == ICE->getSubExpr()->getType())))
145 return resolveExpr(ICE->getSubExpr());
146 return false;
147 }
148
149 if (const auto *ACE = dyn_cast<AbstractConditionalOperator>(E))
150 return resolve(ACE->getTrueExpr()) || resolve(ACE->getFalseExpr());
151
152 return false;
153 }
154
155public:
156 ExprPointeeResolve(const Expr *T) : T(T) {}
157 bool resolve(const Expr *S) { return resolveExpr(S); }
158};
159
160AST_MATCHER_P(Stmt, canResolveToExprPointee, const Stmt *, T) {
161 auto *Exp = dyn_cast<Expr>(&Node);
162 if (!Exp)
163 return true;
164 auto *Target = dyn_cast<Expr>(T);
165 if (!Target)
166 return false;
167 return ExprPointeeResolve{Target}.resolve(Exp);
168}
169
170// Similar to 'hasAnyArgument', but does not work because 'InitListExpr' does
171// not have the 'arguments()' method.
172AST_MATCHER_P(InitListExpr, hasAnyInit, ast_matchers::internal::Matcher<Expr>,
173 InnerMatcher) {
174 for (const Expr *Arg : Node.inits()) {
175 if (Arg == nullptr)
176 continue;
177 ast_matchers::internal::BoundNodesTreeBuilder Result(*Builder);
178 if (InnerMatcher.matches(*Arg, Finder, &Result)) {
179 *Builder = std::move(Result);
180 return true;
181 }
182 }
183 return false;
184}
185
186const ast_matchers::internal::VariadicDynCastAllOfMatcher<Stmt, CXXTypeidExpr>
187 cxxTypeidExpr;
188
189AST_MATCHER(CXXTypeidExpr, isPotentiallyEvaluated) {
190 return Node.isPotentiallyEvaluated();
191}
192
193AST_MATCHER(CXXMemberCallExpr, isConstCallee) {
194 const Decl *CalleeDecl = Node.getCalleeDecl();
195 const auto *VD = dyn_cast_or_null<ValueDecl>(CalleeDecl);
196 if (!VD)
197 return false;
198 const QualType T = VD->getType().getCanonicalType();
199 const auto *MPT = dyn_cast<MemberPointerType>(T);
200 const auto *FPT = MPT ? cast<FunctionProtoType>(MPT->getPointeeType())
201 : dyn_cast<FunctionProtoType>(T);
202 if (!FPT)
203 return false;
204 return FPT->isConst();
205}
206
207AST_MATCHER_P(GenericSelectionExpr, hasControllingExpr,
208 ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
209 if (Node.isTypePredicate())
210 return false;
211 return InnerMatcher.matches(*Node.getControllingExpr(), Finder, Builder);
212}
213
214template <typename T>
215ast_matchers::internal::Matcher<T>
216findFirst(const ast_matchers::internal::Matcher<T> &Matcher) {
217 return anyOf(Matcher, hasDescendant(Matcher));
218}
219
220const auto nonConstReferenceType = [] {
221 return hasUnqualifiedDesugaredType(
222 referenceType(pointee(unless(isConstQualified()))));
223};
224
225const auto nonConstPointerType = [] {
226 return hasUnqualifiedDesugaredType(
227 pointerType(pointee(unless(isConstQualified()))));
228};
229
230const auto isMoveOnly = [] {
231 return cxxRecordDecl(
232 hasMethod(cxxConstructorDecl(isMoveConstructor(), unless(isDeleted()))),
233 hasMethod(cxxMethodDecl(isMoveAssignmentOperator(), unless(isDeleted()))),
234 unless(anyOf(hasMethod(cxxConstructorDecl(isCopyConstructor(),
235 unless(isDeleted()))),
236 hasMethod(cxxMethodDecl(isCopyAssignmentOperator(),
237 unless(isDeleted()))))));
238};
239
240template <class T> struct NodeID;
241template <> struct NodeID<Expr> {
242 static constexpr StringRef value = "expr";
243};
244template <> struct NodeID<Decl> {
245 static constexpr StringRef value = "decl";
246};
247
248template <class T,
249 class F = const Stmt *(ExprMutationAnalyzer::Analyzer::*)(const T *)>
250const Stmt *tryEachMatch(ArrayRef<ast_matchers::BoundNodes> Matches,
251 ExprMutationAnalyzer::Analyzer *Analyzer, F Finder) {
252 const StringRef ID = NodeID<T>::value;
253 for (const auto &Nodes : Matches) {
254 if (const Stmt *S = (Analyzer->*Finder)(Nodes.getNodeAs<T>(ID)))
255 return S;
256 }
257 return nullptr;
258}
259
260} // namespace
261
263 return findMutationMemoized(
264 Exp,
265 {&ExprMutationAnalyzer::Analyzer::findDirectMutation,
266 &ExprMutationAnalyzer::Analyzer::findMemberMutation,
267 &ExprMutationAnalyzer::Analyzer::findArrayElementMutation,
268 &ExprMutationAnalyzer::Analyzer::findCastMutation,
269 &ExprMutationAnalyzer::Analyzer::findRangeLoopMutation,
270 &ExprMutationAnalyzer::Analyzer::findReferenceMutation,
271 &ExprMutationAnalyzer::Analyzer::findFunctionArgMutation},
272 Memorized.Results);
273}
274
278
279const Stmt *
281 return findMutationMemoized(
282 Exp,
283 {
284 &ExprMutationAnalyzer::Analyzer::findPointeeValueMutation,
285 &ExprMutationAnalyzer::Analyzer::findPointeeMemberMutation,
286 &ExprMutationAnalyzer::Analyzer::findPointeeToNonConst,
287 },
288 Memorized.PointeeResults);
289}
290
291const Stmt *
296
297const Stmt *ExprMutationAnalyzer::Analyzer::findMutationMemoized(
298 const Expr *Exp, llvm::ArrayRef<MutationFinder> Finders,
299 Memoized::ResultMap &MemoizedResults) {
300 // Assume Exp is not mutated before analyzing Exp.
301 auto [Memoized, Inserted] = MemoizedResults.try_emplace(Exp);
302 if (!Inserted)
303 return Memoized->second;
304
305 if (ExprMutationAnalyzer::isUnevaluated(Exp, Context))
306 return nullptr;
307
308 for (const auto &Finder : Finders) {
309 if (const Stmt *S = (this->*Finder)(Exp))
310 return MemoizedResults[Exp] = S;
311 }
312
313 return nullptr;
314}
315
316const Stmt *
317ExprMutationAnalyzer::Analyzer::tryEachDeclRef(const Decl *Dec,
318 MutationFinder Finder) {
319 const auto Refs = match(
320 findAll(
321 declRefExpr(to(
322 // `Dec` or a binding if `Dec` is a decomposition.
323 anyOf(equalsNode(Dec),
324 bindingDecl(forDecomposition(equalsNode(Dec))))
325 //
326 ))
327 .bind(NodeID<Expr>::value)),
328 Stm, Context);
329 for (const auto &RefNodes : Refs) {
330 const auto *E = RefNodes.getNodeAs<Expr>(NodeID<Expr>::value);
331 if ((this->*Finder)(E))
332 return E;
333 }
334 return nullptr;
335}
336
338 return !match(stmt(anyOf(
339 // `Exp` is part of the underlying expression of
340 // decltype/typeof if it has an ancestor of
341 // typeLoc.
345 // `UnaryExprOrTypeTraitExpr` is unevaluated
346 // unless it's sizeof on VLA.
348 hasArgumentOfType(variableArrayType())))),
349 // `CXXTypeidExpr` is unevaluated unless it's
350 // applied to an expression of glvalue of
351 // polymorphic class type.
352 cxxTypeidExpr(unless(isPotentiallyEvaluated())),
353 // The controlling expression of
354 // `GenericSelectionExpr` is unevaluated.
356 hasControllingExpr(hasDescendant(equalsNode(Stm)))),
357 cxxNoexceptExpr()))))),
358 *Stm, Context)
359 .empty();
360}
361
362const Stmt *
363ExprMutationAnalyzer::Analyzer::findExprMutation(ArrayRef<BoundNodes> Matches) {
364 return tryEachMatch<Expr>(Matches, this,
366}
367
368const Stmt *
369ExprMutationAnalyzer::Analyzer::findDeclMutation(ArrayRef<BoundNodes> Matches) {
370 return tryEachMatch<Decl>(Matches, this,
372}
373
374const Stmt *ExprMutationAnalyzer::Analyzer::findExprPointeeMutation(
375 ArrayRef<ast_matchers::BoundNodes> Matches) {
376 return tryEachMatch<Expr>(
378}
379
380const Stmt *ExprMutationAnalyzer::Analyzer::findDeclPointeeMutation(
381 ArrayRef<ast_matchers::BoundNodes> Matches) {
382 return tryEachMatch<Decl>(
384}
385
386const Stmt *
387ExprMutationAnalyzer::Analyzer::findDirectMutation(const Expr *Exp) {
388 // LHS of any assignment operators.
389 const auto AsAssignmentLhs =
390 binaryOperator(isAssignmentOperator(), hasLHS(canResolveToExpr(Exp)));
391
392 // Operand of increment/decrement operators.
393 const auto AsIncDecOperand =
394 unaryOperator(anyOf(hasOperatorName("++"), hasOperatorName("--")),
395 hasUnaryOperand(canResolveToExpr(Exp)));
396
397 // Invoking non-const member function.
398 // A member function is assumed to be non-const when it is unresolved.
399 const auto NonConstMethod = cxxMethodDecl(unless(isConst()));
400
401 const auto AsNonConstThis = expr(anyOf(
402 cxxMemberCallExpr(on(canResolveToExpr(Exp)), unless(isConstCallee())),
403 cxxOperatorCallExpr(callee(NonConstMethod),
404 hasArgument(0, canResolveToExpr(Exp))),
405 // In case of a templated type, calling overloaded operators is not
406 // resolved and modelled as `binaryOperator` on a dependent type.
407 // Such instances are considered a modification, because they can modify
408 // in different instantiations of the template.
409 binaryOperator(isTypeDependent(),
410 hasEitherOperand(ignoringImpCasts(canResolveToExpr(Exp)))),
411 // A fold expression may contain `Exp` as it's initializer.
412 // We don't know if the operator modifies `Exp` because the
413 // operator is type dependent due to the parameter pack.
414 cxxFoldExpr(hasFoldInit(ignoringImpCasts(canResolveToExpr(Exp)))),
415 // Within class templates and member functions the member expression might
416 // not be resolved. In that case, the `callExpr` is considered to be a
417 // modification.
418 callExpr(callee(expr(anyOf(
419 unresolvedMemberExpr(hasObjectExpression(canResolveToExpr(Exp))),
421 hasObjectExpression(canResolveToExpr(Exp))))))),
422 // Match on a call to a known method, but the call itself is type
423 // dependent (e.g. `vector<T> v; v.push(T{});` in a templated function).
425 isTypeDependent(),
426 callee(memberExpr(hasDeclaration(NonConstMethod),
427 hasObjectExpression(canResolveToExpr(Exp))))))));
428
429 // Taking address of 'Exp'.
430 // We're assuming 'Exp' is mutated as soon as its address is taken, though in
431 // theory we can follow the pointer and see whether it escaped `Stm` or is
432 // dereferenced and then mutated. This is left for future improvements.
433 const auto AsAmpersandOperand =
434 unaryOperator(hasOperatorName("&"),
435 // A NoOp implicit cast is adding const.
436 unless(hasParent(implicitCastExpr(hasCastKind(CK_NoOp)))),
437 hasUnaryOperand(canResolveToExpr(Exp)));
438 const auto AsPointerFromArrayDecay = castExpr(
439 hasCastKind(CK_ArrayToPointerDecay),
440 unless(hasParent(arraySubscriptExpr())), has(canResolveToExpr(Exp)));
441 // Treat calling `operator->()` of move-only classes as taking address.
442 // These are typically smart pointers with unique ownership so we treat
443 // mutation of pointee as mutation of the smart pointer itself.
444 const auto AsOperatorArrowThis = cxxOperatorCallExpr(
446 callee(
447 cxxMethodDecl(ofClass(isMoveOnly()), returns(nonConstPointerType()))),
448 argumentCountIs(1), hasArgument(0, canResolveToExpr(Exp)));
449
450 // Used as non-const-ref argument when calling a function.
451 // An argument is assumed to be non-const-ref when the function is unresolved.
452 // Instantiated template functions are not handled here but in
453 // findFunctionArgMutation which has additional smarts for handling forwarding
454 // references.
455 const auto NonConstRefParam = forEachArgumentWithParamType(
456 anyOf(canResolveToExpr(Exp),
458 hasObjectExpression(ignoringImpCasts(canResolveToExpr(Exp))))),
459 nonConstReferenceType());
460 const auto NotInstantiated = unless(hasDeclaration(isInstantiated()));
461
462 const auto AsNonConstRefArg =
463 anyOf(callExpr(NonConstRefParam, NotInstantiated),
464 cxxConstructExpr(NonConstRefParam, NotInstantiated),
465 // If the call is type-dependent, we can't properly process any
466 // argument because required type conversions and implicit casts
467 // will be inserted only after specialization.
468 callExpr(isTypeDependent(), hasAnyArgument(canResolveToExpr(Exp))),
469 cxxUnresolvedConstructExpr(hasAnyArgument(canResolveToExpr(Exp))),
470 // Previous False Positive in the following Code:
471 // `template <typename T> void f() { int i = 42; new Type<T>(i); }`
472 // Where the constructor of `Type` takes its argument as reference.
473 // The AST does not resolve in a `cxxConstructExpr` because it is
474 // type-dependent.
475 parenListExpr(hasDescendant(expr(canResolveToExpr(Exp)))),
476 // If the initializer is for a reference type, there is no cast for
477 // the variable. Values are cast to RValue first.
478 initListExpr(hasAnyInit(expr(canResolveToExpr(Exp)))));
479
480 // Captured by a lambda by reference.
481 // If we're initializing a capture with 'Exp' directly then we're initializing
482 // a reference capture.
483 // For value captures there will be an ImplicitCastExpr <LValueToRValue>.
484 const auto AsLambdaRefCaptureInit = lambdaExpr(hasCaptureInit(Exp));
485
486 // Returned as non-const-ref.
487 // If we're returning 'Exp' directly then it's returned as non-const-ref.
488 // For returning by value there will be an ImplicitCastExpr <LValueToRValue>.
489 // For returning by const-ref there will be an ImplicitCastExpr <NoOp> (for
490 // adding const.)
491 const auto AsNonConstRefReturn =
492 returnStmt(hasReturnValue(canResolveToExpr(Exp)));
493
494 // It is used as a non-const-reference for initializing a range-for loop.
495 const auto AsNonConstRefRangeInit = cxxForRangeStmt(hasRangeInit(declRefExpr(
496 allOf(canResolveToExpr(Exp), hasType(nonConstReferenceType())))));
497
498 const auto Matches = match(
499 traverse(
500 TK_AsIs,
501 findFirst(stmt(anyOf(AsAssignmentLhs, AsIncDecOperand, AsNonConstThis,
502 AsAmpersandOperand, AsPointerFromArrayDecay,
503 AsOperatorArrowThis, AsNonConstRefArg,
504 AsLambdaRefCaptureInit, AsNonConstRefReturn,
505 AsNonConstRefRangeInit))
506 .bind("stmt"))),
507 Stm, Context);
508 return selectFirst<Stmt>("stmt", Matches);
509}
510
511const Stmt *
512ExprMutationAnalyzer::Analyzer::findMemberMutation(const Expr *Exp) {
513 // Check whether any member of 'Exp' is mutated.
514 const auto MemberExprs = match(
515 findAll(expr(anyOf(memberExpr(hasObjectExpression(canResolveToExpr(Exp))),
517 hasObjectExpression(canResolveToExpr(Exp))),
518 binaryOperator(hasOperatorName(".*"),
519 hasLHS(equalsNode(Exp)))))
520 .bind(NodeID<Expr>::value)),
521 Stm, Context);
522 return findExprMutation(MemberExprs);
523}
524
525const Stmt *
526ExprMutationAnalyzer::Analyzer::findArrayElementMutation(const Expr *Exp) {
527 // Check whether any element of an array is mutated.
528 const auto SubscriptExprs = match(
530 anyOf(hasBaseConservative(canResolveToExpr(Exp)),
531 hasBaseConservative(implicitCastExpr(allOf(
532 hasCastKind(CK_ArrayToPointerDecay),
533 hasSourceExpression(canResolveToExpr(Exp)))))))
534 .bind(NodeID<Expr>::value)),
535 Stm, Context);
536 return findExprMutation(SubscriptExprs);
537}
538
539const Stmt *ExprMutationAnalyzer::Analyzer::findCastMutation(const Expr *Exp) {
540 // If the 'Exp' is explicitly casted to a non-const reference type the
541 // 'Exp' is considered to be modified.
542 const auto ExplicitCast =
543 match(findFirst(stmt(castExpr(hasSourceExpression(canResolveToExpr(Exp)),
544 explicitCastExpr(hasDestinationType(
545 nonConstReferenceType()))))
546 .bind("stmt")),
547 Stm, Context);
548
549 if (const auto *CastStmt = selectFirst<Stmt>("stmt", ExplicitCast))
550 return CastStmt;
551
552 // If 'Exp' is casted to any non-const reference type, check the castExpr.
553 const auto Casts = match(
554 findAll(expr(castExpr(hasSourceExpression(canResolveToExpr(Exp)),
555 anyOf(explicitCastExpr(hasDestinationType(
556 nonConstReferenceType())),
557 implicitCastExpr(hasImplicitDestinationType(
558 nonConstReferenceType())))))
559 .bind(NodeID<Expr>::value)),
560 Stm, Context);
561
562 if (const Stmt *S = findExprMutation(Casts))
563 return S;
564 // Treat std::{move,forward} as cast.
565 const auto Calls =
567 hasAnyName("::std::move", "::std::forward"))),
568 hasArgument(0, canResolveToExpr(Exp)))
569 .bind("expr")),
570 Stm, Context);
571 return findExprMutation(Calls);
572}
573
574const Stmt *
575ExprMutationAnalyzer::Analyzer::findRangeLoopMutation(const Expr *Exp) {
576 // Keep the ordering for the specific initialization matches to happen first,
577 // because it is cheaper to match all potential modifications of the loop
578 // variable.
579
580 // The range variable is a reference to a builtin array. In that case the
581 // array is considered modified if the loop-variable is a non-const reference.
582 const auto DeclStmtToNonRefToArray = declStmt(hasSingleDecl(varDecl(hasType(
583 hasUnqualifiedDesugaredType(referenceType(pointee(arrayType())))))));
584 const auto RefToArrayRefToElements = match(
585 findFirst(stmt(cxxForRangeStmt(
586 hasLoopVariable(
587 varDecl(anyOf(hasType(nonConstReferenceType()),
588 hasType(nonConstPointerType())))
589 .bind(NodeID<Decl>::value)),
590 hasRangeStmt(DeclStmtToNonRefToArray),
591 hasRangeInit(canResolveToExpr(Exp))))
592 .bind("stmt")),
593 Stm, Context);
594
595 if (const auto *BadRangeInitFromArray =
596 selectFirst<Stmt>("stmt", RefToArrayRefToElements))
597 return BadRangeInitFromArray;
598
599 // Small helper to match special cases in range-for loops.
600 //
601 // It is possible that containers do not provide a const-overload for their
602 // iterator accessors. If this is the case, the variable is used non-const
603 // no matter what happens in the loop. This requires special detection as it
604 // is then faster to find all mutations of the loop variable.
605 // It aims at a different modification as well.
606 const auto HasAnyNonConstIterator =
607 anyOf(allOf(hasMethod(allOf(hasName("begin"), unless(isConst()))),
608 unless(hasMethod(allOf(hasName("begin"), isConst())))),
609 allOf(hasMethod(allOf(hasName("end"), unless(isConst()))),
610 unless(hasMethod(allOf(hasName("end"), isConst())))));
611
612 const auto DeclStmtToNonConstIteratorContainer = declStmt(
613 hasSingleDecl(varDecl(hasType(hasUnqualifiedDesugaredType(referenceType(
614 pointee(hasDeclaration(cxxRecordDecl(HasAnyNonConstIterator)))))))));
615
616 const auto RefToContainerBadIterators = match(
617 findFirst(stmt(cxxForRangeStmt(allOf(
618 hasRangeStmt(DeclStmtToNonConstIteratorContainer),
619 hasRangeInit(canResolveToExpr(Exp)))))
620 .bind("stmt")),
621 Stm, Context);
622
623 if (const auto *BadIteratorsContainer =
624 selectFirst<Stmt>("stmt", RefToContainerBadIterators))
625 return BadIteratorsContainer;
626
627 // If range for looping over 'Exp' with a non-const reference loop variable,
628 // check all declRefExpr of the loop variable.
629 const auto LoopVars =
631 hasLoopVariable(varDecl(hasType(nonConstReferenceType()))
632 .bind(NodeID<Decl>::value)),
633 hasRangeInit(canResolveToExpr(Exp)))),
634 Stm, Context);
635 return findDeclMutation(LoopVars);
636}
637
638const Stmt *
639ExprMutationAnalyzer::Analyzer::findReferenceMutation(const Expr *Exp) {
640 // Follow non-const reference returned by `operator*()` of move-only classes.
641 // These are typically smart pointers with unique ownership so we treat
642 // mutation of pointee as mutation of the smart pointer itself.
643 const auto Ref = match(
646 callee(cxxMethodDecl(ofClass(isMoveOnly()),
647 returns(nonConstReferenceType()))),
648 argumentCountIs(1), hasArgument(0, canResolveToExpr(Exp)))
649 .bind(NodeID<Expr>::value)),
650 Stm, Context);
651 if (const Stmt *S = findExprMutation(Ref))
652 return S;
653
654 // If 'Exp' is bound to a non-const reference, check all declRefExpr to that.
655 const auto Refs = match(
657 varDecl(hasType(nonConstReferenceType()),
658 hasInitializer(anyOf(
659 canResolveToExpr(Exp),
660 memberExpr(hasObjectExpression(canResolveToExpr(Exp))))),
661 hasParent(declStmt().bind("stmt")),
662 // Don't follow the reference in range statement, we've
663 // handled that separately.
665 hasRangeStmt(equalsBoundNode("stmt"))))))))
666 .bind(NodeID<Decl>::value))),
667 Stm, Context);
668 return findDeclMutation(Refs);
669}
670
671const Stmt *
672ExprMutationAnalyzer::Analyzer::findFunctionArgMutation(const Expr *Exp) {
673 const auto NonConstRefParam = forEachArgumentWithParam(
674 canResolveToExpr(Exp),
675 parmVarDecl(hasType(nonConstReferenceType())).bind("parm"));
676 const auto IsInstantiated = hasDeclaration(isInstantiated());
677 const auto FuncDecl = hasDeclaration(functionDecl().bind("func"));
678 const auto Matches = match(
679 traverse(
680 TK_AsIs,
681 findAll(
682 expr(anyOf(callExpr(NonConstRefParam, IsInstantiated, FuncDecl,
684 "::std::move", "::std::forward"))))),
685 cxxConstructExpr(NonConstRefParam, IsInstantiated,
686 FuncDecl)))
687 .bind(NodeID<Expr>::value))),
688 Stm, Context);
689 for (const auto &Nodes : Matches) {
690 const auto *Exp = Nodes.getNodeAs<Expr>(NodeID<Expr>::value);
691 const auto *Func = Nodes.getNodeAs<FunctionDecl>("func");
692 if (!Func->getBody() || !Func->getPrimaryTemplate())
693 return Exp;
694
695 const auto *Parm = Nodes.getNodeAs<ParmVarDecl>("parm");
696 const ArrayRef<ParmVarDecl *> AllParams =
697 Func->getPrimaryTemplate()->getTemplatedDecl()->parameters();
698 QualType ParmType =
699 AllParams[std::min<size_t>(Parm->getFunctionScopeIndex(),
700 AllParams.size() - 1)]
701 ->getType();
702 if (const auto *T = ParmType->getAs<PackExpansionType>())
703 ParmType = T->getPattern();
704
705 // If param type is forwarding reference, follow into the function
706 // definition and see whether the param is mutated inside.
707 if (const auto *RefType = ParmType->getAs<RValueReferenceType>()) {
708 if (!RefType->getPointeeType().getQualifiers() &&
710 RefType->getPointeeType().getCanonicalType())) {
713 *Func, Context, Memorized);
714 if (Analyzer->findMutation(Parm))
715 return Exp;
716 continue;
717 }
718 }
719 // Not forwarding reference.
720 return Exp;
721 }
722 return nullptr;
723}
724
725const Stmt *
726ExprMutationAnalyzer::Analyzer::findPointeeValueMutation(const Expr *Exp) {
727 const auto Matches = match(
729 expr(anyOf(
730 // deref by *
731 unaryOperator(hasOperatorName("*"),
732 hasUnaryOperand(canResolveToExprPointee(Exp))),
733 // deref by []
735 hasBaseConservative(canResolveToExprPointee(Exp)))))
736 .bind(NodeID<Expr>::value))),
737 Stm, Context);
738 return findExprMutation(Matches);
739}
740
741const Stmt *
742ExprMutationAnalyzer::Analyzer::findPointeeMemberMutation(const Expr *Exp) {
743 const Stmt *MemberCallExpr = selectFirst<Stmt>(
745 cxxMemberCallExpr(on(canResolveToExprPointee(Exp)),
746 unless(isConstCallee()))
747 .bind("stmt"))),
748 Stm, Context));
749 if (MemberCallExpr)
750 return MemberCallExpr;
751 const auto Matches = match(
754 hasObjectExpression(canResolveToExprPointee(Exp))),
755 binaryOperator(hasOperatorName("->*"),
756 hasLHS(canResolveToExprPointee(Exp)))))
757 .bind(NodeID<Expr>::value))),
758 Stm, Context);
759 return findExprMutation(Matches);
760}
761
762const Stmt *
763ExprMutationAnalyzer::Analyzer::findPointeeToNonConst(const Expr *Exp) {
764 const auto NonConstPointerOrNonConstRefOrDependentType = type(
765 anyOf(nonConstPointerType(), nonConstReferenceType(), isDependentType()));
766
767 // assign
768 const auto InitToNonConst =
769 varDecl(hasType(NonConstPointerOrNonConstRefOrDependentType),
770 hasInitializer(expr(canResolveToExprPointee(Exp)).bind("stmt")));
771 const auto AssignToNonConst = binaryOperation(
772 hasOperatorName("="),
773 hasLHS(expr(hasType(NonConstPointerOrNonConstRefOrDependentType))),
774 hasRHS(canResolveToExprPointee(Exp)));
775 // arguments like
776 const auto ArgOfInstantiationDependent = allOf(
777 hasAnyArgument(canResolveToExprPointee(Exp)), isInstantiationDependent());
778 const auto ArgOfNonConstParameter =
779 forEachArgumentWithParamType(canResolveToExprPointee(Exp),
780 NonConstPointerOrNonConstRefOrDependentType);
781 const auto CallLikeMatcher =
782 anyOf(ArgOfNonConstParameter, ArgOfInstantiationDependent);
783 const auto PassAsNonConstArg =
784 expr(anyOf(cxxUnresolvedConstructExpr(ArgOfInstantiationDependent),
785 cxxConstructExpr(CallLikeMatcher), callExpr(CallLikeMatcher),
786 parenListExpr(has(canResolveToExprPointee(Exp))),
787 initListExpr(hasAnyInit(canResolveToExprPointee(Exp)))));
788 // cast
789 const auto CastToNonConst = explicitCastExpr(
790 hasSourceExpression(canResolveToExprPointee(Exp)),
791 hasDestinationType(NonConstPointerOrNonConstRefOrDependentType));
792
793 // capture
794 // FIXME: false positive if the pointee does not change in lambda
795 const auto CaptureNoConst = lambdaExpr(hasCaptureInit(Exp));
796
797 const auto ReturnNoConst =
798 returnStmt(hasReturnValue(canResolveToExprPointee(Exp)));
799
800 const auto Matches = match(
802 stmt(anyOf(AssignToNonConst, PassAsNonConstArg,
803 CastToNonConst, CaptureNoConst, ReturnNoConst))
804 .bind("stmt")),
805 forEachDescendant(InitToNonConst))),
806 Stm, Context);
807 return selectFirst<Stmt>("stmt", Matches);
808}
809
810FunctionParmMutationAnalyzer::FunctionParmMutationAnalyzer(
811 const FunctionDecl &Func, ASTContext &Context,
812 ExprMutationAnalyzer::Memoized &Memorized)
813 : BodyAnalyzer(*Func.getBody(), Context, Memorized) {
814 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(&Func)) {
815 // CXXCtorInitializer might also mutate Param but they're not part of
816 // function body, check them eagerly here since they're typically trivial.
817 for (const CXXCtorInitializer *Init : Ctor->inits()) {
818 ExprMutationAnalyzer::Analyzer InitAnalyzer(*Init->getInit(), Context,
819 Memorized);
820 for (const ParmVarDecl *Parm : Ctor->parameters()) {
821 if (Results.contains(Parm))
822 continue;
823 if (const Stmt *S = InitAnalyzer.findMutation(Parm))
824 Results[Parm] = S;
825 }
826 }
827 }
828}
829
830const Stmt *
832 auto [Place, Inserted] = Results.try_emplace(Parm);
833 if (!Inserted)
834 return Place->second;
835
836 // To handle call A -> call B -> call A. Assume parameters of A is not mutated
837 // before analyzing parameters of A. Then when analyzing the second "call A",
838 // FunctionParmMutationAnalyzer can use this memoized value to avoid infinite
839 // recursion.
840 return Place->second = BodyAnalyzer.findMutation(Parm);
841}
842
843} // 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...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:220
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:179
A C++ typeid expression (C++ [expr.typeid]), which gets the type_info that corresponds to the supplie...
Definition ExprCXX.h:848
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Definition Stmt.h:1610
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:3081
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:5254
bool isConst() const
Definition TypeBase.h:4812
Represents a C11 generic selection.
Definition Expr.h:6112
Describes an C or C++ initializer list.
Definition Expr.h:5233
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition ExprCXX.h:1968
Represents a parameter to a function.
Definition Decl.h:1790
A (possibly-)qualified type.
Definition TypeBase.h:937
Stmt - This represents one statement.
Definition Stmt.h:85
The base class of the type hierarchy.
Definition TypeBase.h:1833
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9091
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, 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::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
unsigned kind
All of the diagnostics that can be emitted by the frontend.
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
const FunctionProtoType * T
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