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 // For member calls through a pointer, the pointer variable
426 // itself is not mutated but only the pointee is mutated.
428 on(canResolveToExpr(Exp)),
429 unless(anyOf(isConstCallee(), thisPointerType(pointerType())))),
430
431 cxxOperatorCallExpr(callee(NonConstMethod),
432 hasArgument(0, canResolveToExpr(Exp))),
433 // In case of a templated type, calling overloaded operators is not
434 // resolved and modelled as `binaryOperator` on a dependent type.
435 // Such instances are considered a modification, because they can modify
436 // in different instantiations of the template.
437 binaryOperator(isTypeDependent(),
438 hasEitherOperand(ignoringImpCasts(canResolveToExpr(Exp)))),
439 // A fold expression may contain `Exp` as it's initializer.
440 // We don't know if the operator modifies `Exp` because the
441 // operator is type dependent due to the parameter pack.
442 cxxFoldExpr(hasFoldInit(ignoringImpCasts(canResolveToExpr(Exp)))),
443 // Within class templates and member functions the member expression might
444 // not be resolved. In that case, the `callExpr` is considered to be a
445 // modification.
446 callExpr(callee(expr(anyOf(
447 unresolvedMemberExpr(hasObjectExpression(canResolveToExpr(Exp))),
449 hasObjectExpression(canResolveToExpr(Exp))))))),
450 // Match on a call to a known method, but the call itself is type
451 // dependent (e.g. `vector<T> v; v.push(T{});` in a templated function).
453 isTypeDependent(),
454 callee(memberExpr(hasDeclaration(NonConstMethod),
455 hasObjectExpression(canResolveToExpr(Exp))))))));
456
457 // Taking address of 'Exp'.
458 // We're assuming 'Exp' is mutated as soon as its address is taken, though in
459 // theory we can follow the pointer and see whether it escaped `Stm` or is
460 // dereferenced and then mutated. This is left for future improvements.
461 const auto AsAmpersandOperand =
462 unaryOperator(hasOperatorName("&"),
463 // A NoOp implicit cast is adding const.
464 unless(hasParent(implicitCastExpr(hasCastKind(CK_NoOp)))),
465 hasUnaryOperand(canResolveToExpr(Exp)));
466 const auto AsPointerFromArrayDecay = castExpr(
467 hasCastKind(CK_ArrayToPointerDecay),
468 unless(hasParent(arraySubscriptExpr())), has(canResolveToExpr(Exp)));
469 // Treat calling `operator->()` of move-only classes as taking address.
470 // These are typically smart pointers with unique ownership so we treat
471 // mutation of pointee as mutation of the smart pointer itself.
472 const auto AsOperatorArrowThis = cxxOperatorCallExpr(
474 callee(
475 cxxMethodDecl(ofClass(isMoveOnly()), returns(nonConstPointerType()))),
476 argumentCountIs(1), hasArgument(0, canResolveToExpr(Exp)));
477
478 // Used as non-const-ref argument when calling a function.
479 // An argument is assumed to be non-const-ref when the function is unresolved.
480 // Instantiated template functions are not handled here but in
481 // findFunctionArgMutation which has additional smarts for handling forwarding
482 // references.
483 const auto NonConstRefParam = forEachArgumentWithParamType(
484 anyOf(canResolveToExpr(Exp),
486 hasObjectExpression(ignoringImpCasts(canResolveToExpr(Exp))))),
487 nonConstReferenceType());
488 const auto NotInstantiated = unless(hasDeclaration(isInstantiated()));
489
490 const auto AsNonConstRefArg =
491 anyOf(callExpr(NonConstRefParam, NotInstantiated),
492 cxxConstructExpr(NonConstRefParam, NotInstantiated),
493 // If the call is type-dependent, we can't properly process any
494 // argument because required type conversions and implicit casts
495 // will be inserted only after specialization.
496 callExpr(isTypeDependent(), hasAnyArgument(canResolveToExpr(Exp))),
497 cxxUnresolvedConstructExpr(hasAnyArgument(canResolveToExpr(Exp))),
498 // Previous False Positive in the following Code:
499 // `template <typename T> void f() { int i = 42; new Type<T>(i); }`
500 // Where the constructor of `Type` takes its argument as reference.
501 // The AST does not resolve in a `cxxConstructExpr` because it is
502 // type-dependent.
503 parenListExpr(hasDescendant(expr(canResolveToExpr(Exp)))),
504 // If the initializer is for a reference type, there is no cast for
505 // the variable. Values are cast to RValue first.
506 initListExpr(hasAnyInit(expr(canResolveToExpr(Exp)))));
507
508 // Captured by a lambda by reference.
509 // If we're initializing a capture with 'Exp' directly then we're initializing
510 // a reference capture.
511 // For value captures there will be an ImplicitCastExpr <LValueToRValue>.
512 const auto AsLambdaRefCaptureInit = lambdaExpr(hasCaptureInit(Exp));
513
514 // Returned as non-const-ref.
515 // If we're returning 'Exp' directly then it's returned as non-const-ref.
516 // For returning by value there will be an ImplicitCastExpr <LValueToRValue>.
517 // For returning by const-ref there will be an ImplicitCastExpr <NoOp> (for
518 // adding const.)
519 const auto AsNonConstRefReturn =
520 returnStmt(hasReturnValue(canResolveToExpr(Exp)));
521
522 // It is used as a non-const-reference for initializing a range-for loop.
523 const auto AsNonConstRefRangeInit = cxxForRangeStmt(hasRangeInit(declRefExpr(
524 allOf(canResolveToExpr(Exp), hasType(nonConstReferenceType())))));
525
526 const auto Matches = match(
527 traverse(
528 TK_AsIs,
529 findFirst(stmt(anyOf(AsAssignmentLhs, AsIncDecOperand, AsNonConstThis,
530 AsAmpersandOperand, AsPointerFromArrayDecay,
531 AsOperatorArrowThis, AsNonConstRefArg,
532 AsLambdaRefCaptureInit, AsNonConstRefReturn,
533 AsNonConstRefRangeInit))
534 .bind("stmt"))),
535 Stm, Context);
536 return selectFirst<Stmt>("stmt", Matches);
537}
538
539const Stmt *
540ExprMutationAnalyzer::Analyzer::findMemberMutation(const Expr *Exp) {
541 // Check whether any member of 'Exp' is mutated.
542 const auto MemberExprs = match(
543 findAll(expr(anyOf(memberExpr(hasObjectExpression(canResolveToExpr(Exp))),
545 hasObjectExpression(canResolveToExpr(Exp))),
546 binaryOperator(hasOperatorName(".*"),
547 hasLHS(equalsNode(Exp)))))
548 .bind(NodeID<Expr>::value)),
549 Stm, Context);
550 return findExprMutation(MemberExprs);
551}
552
553const Stmt *
554ExprMutationAnalyzer::Analyzer::findArrayElementMutation(const Expr *Exp) {
555 // Check whether any element of an array is mutated.
556 const auto SubscriptExprs = match(
558 anyOf(hasBaseConservative(canResolveToExpr(Exp)),
559 hasBaseConservative(implicitCastExpr(allOf(
560 hasCastKind(CK_ArrayToPointerDecay),
561 hasSourceExpression(canResolveToExpr(Exp)))))))
562 .bind(NodeID<Expr>::value)),
563 Stm, Context);
564 return findExprMutation(SubscriptExprs);
565}
566
567const Stmt *ExprMutationAnalyzer::Analyzer::findCastMutation(const Expr *Exp) {
568 // If the 'Exp' is explicitly casted to a non-const reference type the
569 // 'Exp' is considered to be modified.
570 const auto ExplicitCast =
571 match(findFirst(stmt(castExpr(hasSourceExpression(canResolveToExpr(Exp)),
572 explicitCastExpr(hasDestinationType(
573 nonConstReferenceType()))))
574 .bind("stmt")),
575 Stm, Context);
576
577 if (const auto *CastStmt = selectFirst<Stmt>("stmt", ExplicitCast))
578 return CastStmt;
579
580 // If 'Exp' is casted to any non-const reference type, check the castExpr.
581 const auto Casts = match(
582 findAll(expr(castExpr(hasSourceExpression(canResolveToExpr(Exp)),
583 anyOf(explicitCastExpr(hasDestinationType(
584 nonConstReferenceType())),
585 implicitCastExpr(hasImplicitDestinationType(
586 nonConstReferenceType())))))
587 .bind(NodeID<Expr>::value)),
588 Stm, Context);
589
590 if (const Stmt *S = findExprMutation(Casts))
591 return S;
592 // Treat std::{move,forward} as cast.
593 const auto Calls =
595 hasAnyName("::std::move", "::std::forward"))),
596 hasArgument(0, canResolveToExpr(Exp)))
597 .bind("expr")),
598 Stm, Context);
599 return findExprMutation(Calls);
600}
601
602const Stmt *
603ExprMutationAnalyzer::Analyzer::findRangeLoopMutation(const Expr *Exp) {
604 // Keep the ordering for the specific initialization matches to happen first,
605 // because it is cheaper to match all potential modifications of the loop
606 // variable.
607
608 // The range variable is a reference to a builtin array. In that case the
609 // array is considered modified if the loop-variable is a non-const reference.
610 const auto DeclStmtToNonRefToArray = declStmt(hasSingleDecl(varDecl(hasType(
611 hasUnqualifiedDesugaredType(referenceType(pointee(arrayType())))))));
612 const auto RefToArrayRefToElements = match(
613 findFirst(stmt(cxxForRangeStmt(
614 hasLoopVariable(
615 varDecl(anyOf(hasType(nonConstReferenceType()),
616 hasType(nonConstPointerType())))
617 .bind(NodeID<Decl>::value)),
618 hasRangeStmt(DeclStmtToNonRefToArray),
619 hasRangeInit(canResolveToExpr(Exp))))
620 .bind("stmt")),
621 Stm, Context);
622
623 if (const auto *BadRangeInitFromArray =
624 selectFirst<Stmt>("stmt", RefToArrayRefToElements))
625 return BadRangeInitFromArray;
626
627 // Small helper to match special cases in range-for loops.
628 //
629 // It is possible that containers do not provide a const-overload for their
630 // iterator accessors. If this is the case, the variable is used non-const
631 // no matter what happens in the loop. This requires special detection as it
632 // is then faster to find all mutations of the loop variable.
633 // It aims at a different modification as well.
634 const auto HasAnyNonConstIterator =
635 anyOf(allOf(hasMethod(allOf(hasName("begin"), unless(isConst()))),
636 unless(hasMethod(allOf(hasName("begin"), isConst())))),
637 allOf(hasMethod(allOf(hasName("end"), unless(isConst()))),
638 unless(hasMethod(allOf(hasName("end"), isConst())))));
639
640 const auto DeclStmtToNonConstIteratorContainer = declStmt(
641 hasSingleDecl(varDecl(hasType(hasUnqualifiedDesugaredType(referenceType(
642 pointee(hasDeclaration(cxxRecordDecl(HasAnyNonConstIterator)))))))));
643
644 const auto RefToContainerBadIterators = match(
645 findFirst(stmt(cxxForRangeStmt(allOf(
646 hasRangeStmt(DeclStmtToNonConstIteratorContainer),
647 hasRangeInit(canResolveToExpr(Exp)))))
648 .bind("stmt")),
649 Stm, Context);
650
651 if (const auto *BadIteratorsContainer =
652 selectFirst<Stmt>("stmt", RefToContainerBadIterators))
653 return BadIteratorsContainer;
654
655 // If range for looping over 'Exp' with a non-const reference loop variable,
656 // check all declRefExpr of the loop variable.
657 const auto LoopVars =
659 hasLoopVariable(varDecl(hasType(nonConstReferenceType()))
660 .bind(NodeID<Decl>::value)),
661 hasRangeInit(canResolveToExpr(Exp)))),
662 Stm, Context);
663 return findDeclMutation(LoopVars);
664}
665
666const Stmt *
667ExprMutationAnalyzer::Analyzer::findReferenceMutation(const Expr *Exp) {
668 // Follow non-const reference returned by `operator*()` of move-only classes.
669 // These are typically smart pointers with unique ownership so we treat
670 // mutation of pointee as mutation of the smart pointer itself.
671 const auto Ref = match(
674 callee(cxxMethodDecl(ofClass(isMoveOnly()),
675 returns(nonConstReferenceType()))),
676 argumentCountIs(1), hasArgument(0, canResolveToExpr(Exp)))
677 .bind(NodeID<Expr>::value)),
678 Stm, Context);
679 if (const Stmt *S = findExprMutation(Ref))
680 return S;
681
682 // If 'Exp' is bound to a non-const reference, check all declRefExpr to that.
683 const auto Refs = match(
685 varDecl(hasType(nonConstReferenceType()),
686 hasInitializer(anyOf(
687 canResolveToExpr(Exp),
688 memberExpr(hasObjectExpression(canResolveToExpr(Exp))))),
689 hasParent(declStmt().bind("stmt")),
690 // Don't follow the reference in range statement, we've
691 // handled that separately.
693 hasRangeStmt(equalsBoundNode("stmt"))))))))
694 .bind(NodeID<Decl>::value))),
695 Stm, Context);
696 return findDeclMutation(Refs);
697}
698
699const Stmt *
700ExprMutationAnalyzer::Analyzer::findFunctionArgMutation(const Expr *Exp) {
701 const auto NonConstRefParam = forEachArgumentWithParam(
702 canResolveToExpr(Exp),
703 parmVarDecl(hasType(nonConstReferenceType())).bind("parm"));
704 const auto IsInstantiated = hasDeclaration(isInstantiated());
705 const auto FuncDecl = hasDeclaration(functionDecl().bind("func"));
706 const auto Matches = match(
707 traverse(
708 TK_AsIs,
709 findAll(
710 expr(anyOf(callExpr(NonConstRefParam, IsInstantiated, FuncDecl,
712 "::std::move", "::std::forward"))))),
713 cxxConstructExpr(NonConstRefParam, IsInstantiated,
714 FuncDecl)))
715 .bind(NodeID<Expr>::value))),
716 Stm, Context);
717 for (const auto &Nodes : Matches) {
718 const auto *Exp = Nodes.getNodeAs<Expr>(NodeID<Expr>::value);
719 const auto *Func = Nodes.getNodeAs<FunctionDecl>("func");
720 if (!Func->getBody() || !Func->getPrimaryTemplate())
721 return Exp;
722
723 const auto *Parm = Nodes.getNodeAs<ParmVarDecl>("parm");
724 const ArrayRef<ParmVarDecl *> AllParams =
725 Func->getPrimaryTemplate()->getTemplatedDecl()->parameters();
726 QualType ParmType =
727 AllParams[std::min<size_t>(Parm->getFunctionScopeIndex(),
728 AllParams.size() - 1)]
729 ->getType();
730 if (const auto *T = ParmType->getAs<PackExpansionType>())
731 ParmType = T->getPattern();
732
733 // If param type is forwarding reference, follow into the function
734 // definition and see whether the param is mutated inside.
735 if (const auto *RefType = ParmType->getAs<RValueReferenceType>()) {
736 if (!RefType->getPointeeType().getQualifiers() &&
738 RefType->getPointeeType().getCanonicalType())) {
741 *Func, Context, Memorized);
742 if (Analyzer->findMutation(Parm))
743 return Exp;
744 continue;
745 }
746 }
747 // Not forwarding reference.
748 return Exp;
749 }
750 return nullptr;
751}
752
753const Stmt *
754ExprMutationAnalyzer::Analyzer::findPointeeValueMutation(const Expr *Exp) {
755 const auto Matches = match(
757 expr(anyOf(
758 // deref by *
759 unaryOperator(hasOperatorName("*"),
760 hasUnaryOperand(canResolveToExprPointee(Exp))),
761 // deref by []
763 hasBaseConservative(canResolveToExprPointee(Exp)))))
764 .bind(NodeID<Expr>::value))),
765 Stm, Context);
766 return findExprMutation(Matches);
767}
768
769const Stmt *
770ExprMutationAnalyzer::Analyzer::findPointeeMemberMutation(const Expr *Exp) {
771 const Stmt *MemberCallExpr = selectFirst<Stmt>(
773 cxxMemberCallExpr(on(canResolveToExprPointee(Exp)),
774 unless(isConstCallee()))
775 .bind("stmt"))),
776 Stm, Context));
777 if (MemberCallExpr)
778 return MemberCallExpr;
779 const auto Matches = match(
782 hasObjectExpression(canResolveToExprPointee(Exp))),
783 binaryOperator(hasOperatorName("->*"),
784 hasLHS(canResolveToExprPointee(Exp)))))
785 .bind(NodeID<Expr>::value))),
786 Stm, Context);
787 return findExprMutation(Matches);
788}
789
790const Stmt *
791ExprMutationAnalyzer::Analyzer::findPointeeToNonConst(const Expr *Exp) {
792 const auto NonConstPointerOrNonConstRefOrDependentType = type(anyOf(
793 nonConstPointerType(), nonConstReferenceType(),
794 constReferenceToPointerWithNonConstPointeeType(), isDependentType()));
795
796 // assign
797 const auto InitToNonConst =
798 varDecl(hasType(NonConstPointerOrNonConstRefOrDependentType),
799 hasInitializer(expr(canResolveToExprPointee(Exp)).bind("stmt")));
800 const auto AssignToNonConst = binaryOperation(
801 hasOperatorName("="),
802 hasLHS(expr(hasType(NonConstPointerOrNonConstRefOrDependentType))),
803 hasRHS(canResolveToExprPointee(Exp)));
804 // arguments like
805 const auto ArgOfInstantiationDependent = allOf(
806 hasAnyArgument(canResolveToExprPointee(Exp)), isInstantiationDependent());
807 const auto ArgOfNonConstParameter =
808 forEachArgumentWithParamType(canResolveToExprPointee(Exp),
809 NonConstPointerOrNonConstRefOrDependentType);
810 const auto CallLikeMatcher =
811 anyOf(ArgOfNonConstParameter, ArgOfInstantiationDependent);
812 const auto PassAsNonConstArg =
813 expr(anyOf(cxxUnresolvedConstructExpr(ArgOfInstantiationDependent),
814 cxxNewExpr(hasAnyPlacementArg(
815 ignoringParenImpCasts(canResolveToExprPointee(Exp)))),
816 cxxConstructExpr(CallLikeMatcher), callExpr(CallLikeMatcher),
817 parenListExpr(has(canResolveToExprPointee(Exp))),
818 initListExpr(hasAnyInit(canResolveToExprPointee(Exp)))));
819 // cast
820 const auto CastToNonConst = explicitCastExpr(
821 hasSourceExpression(canResolveToExprPointee(Exp)),
822 hasDestinationType(NonConstPointerOrNonConstRefOrDependentType));
823
824 // capture
825 // FIXME: false positive if the pointee does not change in lambda
826 const auto CaptureNoConst = lambdaExpr(hasCaptureInit(Exp));
827
828 const auto ReturnNoConst =
829 returnStmt(hasReturnValue(canResolveToExprPointee(Exp)));
830
831 const auto Matches = match(
833 stmt(anyOf(AssignToNonConst, PassAsNonConstArg,
834 CastToNonConst, CaptureNoConst, ReturnNoConst))
835 .bind("stmt")),
836 forEachDescendant(InitToNonConst))),
837 Stm, Context);
838 return selectFirst<Stmt>("stmt", Matches);
839}
840
841FunctionParmMutationAnalyzer::FunctionParmMutationAnalyzer(
842 const FunctionDecl &Func, ASTContext &Context,
843 ExprMutationAnalyzer::Memoized &Memorized)
844 : BodyAnalyzer(*Func.getBody(), Context, Memorized) {
845 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(&Func)) {
846 // CXXCtorInitializer might also mutate Param but they're not part of
847 // function body, check them eagerly here since they're typically trivial.
848 for (const CXXCtorInitializer *Init : Ctor->inits()) {
849 ExprMutationAnalyzer::Analyzer InitAnalyzer(*Init->getInit(), Context,
850 Memorized);
851 for (const ParmVarDecl *Parm : Ctor->parameters()) {
852 if (Results.contains(Parm))
853 continue;
854 if (const Stmt *S = InitAnalyzer.findMutation(Parm))
855 Results[Parm] = S;
856 }
857 }
858 }
859}
860
861const Stmt *
863 auto [Place, Inserted] = Results.try_emplace(Parm);
864 if (!Inserted)
865 return Place->second;
866
867 // To handle call A -> call B -> call A. Assume parameters of A is not mutated
868 // before analyzing parameters of A. Then when analyzing the second "call A",
869 // FunctionParmMutationAnalyzer can use this memoized value to avoid infinite
870 // recursion.
871 return Place->second = BodyAnalyzer.findMutation(Parm);
872}
873
874} // 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:226
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