1 //===- Consumed.cpp -------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // A intra-procedural analysis for checking consumed properties. This is based,
10 // in part, on research on linear types.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Analysis/Analyses/Consumed.h"
15 #include "clang/AST/Attr.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/ExprCXX.h"
20 #include "clang/AST/Stmt.h"
21 #include "clang/AST/StmtVisitor.h"
22 #include "clang/AST/Type.h"
23 #include "clang/Analysis/Analyses/PostOrderCFGView.h"
24 #include "clang/Analysis/AnalysisDeclContext.h"
25 #include "clang/Analysis/CFG.h"
26 #include "clang/Basic/LLVM.h"
27 #include "clang/Basic/OperatorKinds.h"
28 #include "clang/Basic/SourceLocation.h"
29 #include "llvm/ADT/DenseMap.h"
30 #include "llvm/ADT/STLExtras.h"
31 #include "llvm/ADT/StringRef.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/ErrorHandling.h"
39 // TODO: Adjust states of args to constructors in the same way that arguments to
40 // function calls are handled.
41 // TODO: Use information from tests in for- and while-loop conditional.
42 // TODO: Add notes about the actual and expected state for
43 // TODO: Correctly identify unreachable blocks when chaining boolean operators.
44 // TODO: Adjust the parser and AttributesList class to support lists of
46 // TODO: Warn about unreachable code.
47 // TODO: Switch to using a bitmap to track unreachable blocks.
48 // TODO: Handle variable definitions, e.g. bool valid = x.isValid();
49 // if (valid) ...; (Deferred)
50 // TODO: Take notes on state transitions to provide better warning messages.
52 // TODO: Test nested conditionals: A) Checking the same value multiple times,
53 // and 2) Checking different values. (Deferred)
55 using namespace clang
;
56 using namespace consumed
;
58 // Key method definition
59 ConsumedWarningsHandlerBase::~ConsumedWarningsHandlerBase() = default;
61 static SourceLocation
getFirstStmtLoc(const CFGBlock
*Block
) {
62 // Find the source location of the first statement in the block, if the block
64 for (const auto &B
: *Block
)
65 if (std::optional
<CFGStmt
> CS
= B
.getAs
<CFGStmt
>())
66 return CS
->getStmt()->getBeginLoc();
69 // If we have one successor, return the first statement in that block
70 if (Block
->succ_size() == 1 && *Block
->succ_begin())
71 return getFirstStmtLoc(*Block
->succ_begin());
76 static SourceLocation
getLastStmtLoc(const CFGBlock
*Block
) {
77 // Find the source location of the last statement in the block, if the block
79 if (const Stmt
*StmtNode
= Block
->getTerminatorStmt()) {
80 return StmtNode
->getBeginLoc();
82 for (CFGBlock::const_reverse_iterator BI
= Block
->rbegin(),
83 BE
= Block
->rend(); BI
!= BE
; ++BI
) {
84 if (std::optional
<CFGStmt
> CS
= BI
->getAs
<CFGStmt
>())
85 return CS
->getStmt()->getBeginLoc();
89 // If we have one successor, return the first statement in that block
91 if (Block
->succ_size() == 1 && *Block
->succ_begin())
92 Loc
= getFirstStmtLoc(*Block
->succ_begin());
96 // If we have one predecessor, return the last statement in that block
97 if (Block
->pred_size() == 1 && *Block
->pred_begin())
98 return getLastStmtLoc(*Block
->pred_begin());
103 static ConsumedState
invertConsumedUnconsumed(ConsumedState State
) {
108 return CS_Unconsumed
;
114 llvm_unreachable("invalid enum");
117 static bool isCallableInState(const CallableWhenAttr
*CWAttr
,
118 ConsumedState State
) {
119 for (const auto &S
: CWAttr
->callableStates()) {
120 ConsumedState MappedAttrState
= CS_None
;
123 case CallableWhenAttr::Unknown
:
124 MappedAttrState
= CS_Unknown
;
127 case CallableWhenAttr::Unconsumed
:
128 MappedAttrState
= CS_Unconsumed
;
131 case CallableWhenAttr::Consumed
:
132 MappedAttrState
= CS_Consumed
;
136 if (MappedAttrState
== State
)
143 static bool isConsumableType(const QualType
&QT
) {
144 if (QT
->isPointerType() || QT
->isReferenceType())
147 if (const CXXRecordDecl
*RD
= QT
->getAsCXXRecordDecl())
148 return RD
->hasAttr
<ConsumableAttr
>();
153 static bool isAutoCastType(const QualType
&QT
) {
154 if (QT
->isPointerType() || QT
->isReferenceType())
157 if (const CXXRecordDecl
*RD
= QT
->getAsCXXRecordDecl())
158 return RD
->hasAttr
<ConsumableAutoCastAttr
>();
163 static bool isSetOnReadPtrType(const QualType
&QT
) {
164 if (const CXXRecordDecl
*RD
= QT
->getPointeeCXXRecordDecl())
165 return RD
->hasAttr
<ConsumableSetOnReadAttr
>();
169 static bool isKnownState(ConsumedState State
) {
178 llvm_unreachable("invalid enum");
181 static bool isRValueRef(QualType ParamType
) {
182 return ParamType
->isRValueReferenceType();
185 static bool isTestingFunction(const FunctionDecl
*FunDecl
) {
186 return FunDecl
->hasAttr
<TestTypestateAttr
>();
189 static bool isPointerOrRef(QualType ParamType
) {
190 return ParamType
->isPointerType() || ParamType
->isReferenceType();
193 static ConsumedState
mapConsumableAttrState(const QualType QT
) {
194 assert(isConsumableType(QT
));
196 const ConsumableAttr
*CAttr
=
197 QT
->getAsCXXRecordDecl()->getAttr
<ConsumableAttr
>();
199 switch (CAttr
->getDefaultState()) {
200 case ConsumableAttr::Unknown
:
202 case ConsumableAttr::Unconsumed
:
203 return CS_Unconsumed
;
204 case ConsumableAttr::Consumed
:
207 llvm_unreachable("invalid enum");
211 mapParamTypestateAttrState(const ParamTypestateAttr
*PTAttr
) {
212 switch (PTAttr
->getParamState()) {
213 case ParamTypestateAttr::Unknown
:
215 case ParamTypestateAttr::Unconsumed
:
216 return CS_Unconsumed
;
217 case ParamTypestateAttr::Consumed
:
220 llvm_unreachable("invalid_enum");
224 mapReturnTypestateAttrState(const ReturnTypestateAttr
*RTSAttr
) {
225 switch (RTSAttr
->getState()) {
226 case ReturnTypestateAttr::Unknown
:
228 case ReturnTypestateAttr::Unconsumed
:
229 return CS_Unconsumed
;
230 case ReturnTypestateAttr::Consumed
:
233 llvm_unreachable("invalid enum");
236 static ConsumedState
mapSetTypestateAttrState(const SetTypestateAttr
*STAttr
) {
237 switch (STAttr
->getNewState()) {
238 case SetTypestateAttr::Unknown
:
240 case SetTypestateAttr::Unconsumed
:
241 return CS_Unconsumed
;
242 case SetTypestateAttr::Consumed
:
245 llvm_unreachable("invalid_enum");
248 static StringRef
stateToString(ConsumedState State
) {
250 case consumed::CS_None
:
253 case consumed::CS_Unknown
:
256 case consumed::CS_Unconsumed
:
259 case consumed::CS_Consumed
:
262 llvm_unreachable("invalid enum");
265 static ConsumedState
testsFor(const FunctionDecl
*FunDecl
) {
266 assert(isTestingFunction(FunDecl
));
267 switch (FunDecl
->getAttr
<TestTypestateAttr
>()->getTestState()) {
268 case TestTypestateAttr::Unconsumed
:
269 return CS_Unconsumed
;
270 case TestTypestateAttr::Consumed
:
273 llvm_unreachable("invalid enum");
278 struct VarTestResult
{
280 ConsumedState TestsFor
;
293 class PropagationInfo
{
301 } InfoType
= IT_None
;
304 const BinaryOperator
*Source
;
312 VarTestResult VarTest
;
314 const CXXBindTemporaryExpr
*Tmp
;
319 PropagationInfo() = default;
320 PropagationInfo(const VarTestResult
&VarTest
)
321 : InfoType(IT_VarTest
), VarTest(VarTest
) {}
323 PropagationInfo(const VarDecl
*Var
, ConsumedState TestsFor
)
324 : InfoType(IT_VarTest
) {
326 VarTest
.TestsFor
= TestsFor
;
329 PropagationInfo(const BinaryOperator
*Source
, EffectiveOp EOp
,
330 const VarTestResult
<est
, const VarTestResult
&RTest
)
331 : InfoType(IT_BinTest
) {
332 BinTest
.Source
= Source
;
334 BinTest
.LTest
= LTest
;
335 BinTest
.RTest
= RTest
;
338 PropagationInfo(const BinaryOperator
*Source
, EffectiveOp EOp
,
339 const VarDecl
*LVar
, ConsumedState LTestsFor
,
340 const VarDecl
*RVar
, ConsumedState RTestsFor
)
341 : InfoType(IT_BinTest
) {
342 BinTest
.Source
= Source
;
344 BinTest
.LTest
.Var
= LVar
;
345 BinTest
.LTest
.TestsFor
= LTestsFor
;
346 BinTest
.RTest
.Var
= RVar
;
347 BinTest
.RTest
.TestsFor
= RTestsFor
;
350 PropagationInfo(ConsumedState State
)
351 : InfoType(IT_State
), State(State
) {}
352 PropagationInfo(const VarDecl
*Var
) : InfoType(IT_Var
), Var(Var
) {}
353 PropagationInfo(const CXXBindTemporaryExpr
*Tmp
)
354 : InfoType(IT_Tmp
), Tmp(Tmp
) {}
356 const ConsumedState
&getState() const {
357 assert(InfoType
== IT_State
);
361 const VarTestResult
&getVarTest() const {
362 assert(InfoType
== IT_VarTest
);
366 const VarTestResult
&getLTest() const {
367 assert(InfoType
== IT_BinTest
);
368 return BinTest
.LTest
;
371 const VarTestResult
&getRTest() const {
372 assert(InfoType
== IT_BinTest
);
373 return BinTest
.RTest
;
376 const VarDecl
*getVar() const {
377 assert(InfoType
== IT_Var
);
381 const CXXBindTemporaryExpr
*getTmp() const {
382 assert(InfoType
== IT_Tmp
);
386 ConsumedState
getAsState(const ConsumedStateMap
*StateMap
) const {
387 assert(isVar() || isTmp() || isState());
390 return StateMap
->getState(Var
);
392 return StateMap
->getState(Tmp
);
399 EffectiveOp
testEffectiveOp() const {
400 assert(InfoType
== IT_BinTest
);
404 const BinaryOperator
* testSourceNode() const {
405 assert(InfoType
== IT_BinTest
);
406 return BinTest
.Source
;
409 bool isValid() const { return InfoType
!= IT_None
; }
410 bool isState() const { return InfoType
== IT_State
; }
411 bool isVarTest() const { return InfoType
== IT_VarTest
; }
412 bool isBinTest() const { return InfoType
== IT_BinTest
; }
413 bool isVar() const { return InfoType
== IT_Var
; }
414 bool isTmp() const { return InfoType
== IT_Tmp
; }
416 bool isTest() const {
417 return InfoType
== IT_VarTest
|| InfoType
== IT_BinTest
;
420 bool isPointerToValue() const {
421 return InfoType
== IT_Var
|| InfoType
== IT_Tmp
;
424 PropagationInfo
invertTest() const {
425 assert(InfoType
== IT_VarTest
|| InfoType
== IT_BinTest
);
427 if (InfoType
== IT_VarTest
) {
428 return PropagationInfo(VarTest
.Var
,
429 invertConsumedUnconsumed(VarTest
.TestsFor
));
431 } else if (InfoType
== IT_BinTest
) {
432 return PropagationInfo(BinTest
.Source
,
433 BinTest
.EOp
== EO_And
? EO_Or
: EO_And
,
434 BinTest
.LTest
.Var
, invertConsumedUnconsumed(BinTest
.LTest
.TestsFor
),
435 BinTest
.RTest
.Var
, invertConsumedUnconsumed(BinTest
.RTest
.TestsFor
));
442 } // namespace consumed
446 setStateForVarOrTmp(ConsumedStateMap
*StateMap
, const PropagationInfo
&PInfo
,
447 ConsumedState State
) {
448 assert(PInfo
.isVar() || PInfo
.isTmp());
451 StateMap
->setState(PInfo
.getVar(), State
);
453 StateMap
->setState(PInfo
.getTmp(), State
);
459 class ConsumedStmtVisitor
: public ConstStmtVisitor
<ConsumedStmtVisitor
> {
460 using MapType
= llvm::DenseMap
<const Stmt
*, PropagationInfo
>;
461 using PairType
= std::pair
<const Stmt
*, PropagationInfo
>;
462 using InfoEntry
= MapType::iterator
;
463 using ConstInfoEntry
= MapType::const_iterator
;
465 ConsumedAnalyzer
&Analyzer
;
466 ConsumedStateMap
*StateMap
;
467 MapType PropagationMap
;
469 InfoEntry
findInfo(const Expr
*E
) {
470 if (const auto Cleanups
= dyn_cast
<ExprWithCleanups
>(E
))
471 if (!Cleanups
->cleanupsHaveSideEffects())
472 E
= Cleanups
->getSubExpr();
473 return PropagationMap
.find(E
->IgnoreParens());
476 ConstInfoEntry
findInfo(const Expr
*E
) const {
477 if (const auto Cleanups
= dyn_cast
<ExprWithCleanups
>(E
))
478 if (!Cleanups
->cleanupsHaveSideEffects())
479 E
= Cleanups
->getSubExpr();
480 return PropagationMap
.find(E
->IgnoreParens());
483 void insertInfo(const Expr
*E
, const PropagationInfo
&PI
) {
484 PropagationMap
.insert(PairType(E
->IgnoreParens(), PI
));
487 void forwardInfo(const Expr
*From
, const Expr
*To
);
488 void copyInfo(const Expr
*From
, const Expr
*To
, ConsumedState CS
);
489 ConsumedState
getInfo(const Expr
*From
);
490 void setInfo(const Expr
*To
, ConsumedState NS
);
491 void propagateReturnType(const Expr
*Call
, const FunctionDecl
*Fun
);
494 void checkCallability(const PropagationInfo
&PInfo
,
495 const FunctionDecl
*FunDecl
,
496 SourceLocation BlameLoc
);
497 bool handleCall(const CallExpr
*Call
, const Expr
*ObjArg
,
498 const FunctionDecl
*FunD
);
500 void VisitBinaryOperator(const BinaryOperator
*BinOp
);
501 void VisitCallExpr(const CallExpr
*Call
);
502 void VisitCastExpr(const CastExpr
*Cast
);
503 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr
*Temp
);
504 void VisitCXXConstructExpr(const CXXConstructExpr
*Call
);
505 void VisitCXXMemberCallExpr(const CXXMemberCallExpr
*Call
);
506 void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr
*Call
);
507 void VisitDeclRefExpr(const DeclRefExpr
*DeclRef
);
508 void VisitDeclStmt(const DeclStmt
*DelcS
);
509 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr
*Temp
);
510 void VisitMemberExpr(const MemberExpr
*MExpr
);
511 void VisitParmVarDecl(const ParmVarDecl
*Param
);
512 void VisitReturnStmt(const ReturnStmt
*Ret
);
513 void VisitUnaryOperator(const UnaryOperator
*UOp
);
514 void VisitVarDecl(const VarDecl
*Var
);
516 ConsumedStmtVisitor(ConsumedAnalyzer
&Analyzer
, ConsumedStateMap
*StateMap
)
517 : Analyzer(Analyzer
), StateMap(StateMap
) {}
519 PropagationInfo
getInfo(const Expr
*StmtNode
) const {
520 ConstInfoEntry Entry
= findInfo(StmtNode
);
522 if (Entry
!= PropagationMap
.end())
523 return Entry
->second
;
528 void reset(ConsumedStateMap
*NewStateMap
) {
529 StateMap
= NewStateMap
;
533 } // namespace consumed
536 void ConsumedStmtVisitor::forwardInfo(const Expr
*From
, const Expr
*To
) {
537 InfoEntry Entry
= findInfo(From
);
538 if (Entry
!= PropagationMap
.end())
539 insertInfo(To
, Entry
->second
);
542 // Create a new state for To, which is initialized to the state of From.
543 // If NS is not CS_None, sets the state of From to NS.
544 void ConsumedStmtVisitor::copyInfo(const Expr
*From
, const Expr
*To
,
546 InfoEntry Entry
= findInfo(From
);
547 if (Entry
!= PropagationMap
.end()) {
548 PropagationInfo
& PInfo
= Entry
->second
;
549 ConsumedState CS
= PInfo
.getAsState(StateMap
);
551 insertInfo(To
, PropagationInfo(CS
));
552 if (NS
!= CS_None
&& PInfo
.isPointerToValue())
553 setStateForVarOrTmp(StateMap
, PInfo
, NS
);
557 // Get the ConsumedState for From
558 ConsumedState
ConsumedStmtVisitor::getInfo(const Expr
*From
) {
559 InfoEntry Entry
= findInfo(From
);
560 if (Entry
!= PropagationMap
.end()) {
561 PropagationInfo
& PInfo
= Entry
->second
;
562 return PInfo
.getAsState(StateMap
);
567 // If we already have info for To then update it, otherwise create a new entry.
568 void ConsumedStmtVisitor::setInfo(const Expr
*To
, ConsumedState NS
) {
569 InfoEntry Entry
= findInfo(To
);
570 if (Entry
!= PropagationMap
.end()) {
571 PropagationInfo
& PInfo
= Entry
->second
;
572 if (PInfo
.isPointerToValue())
573 setStateForVarOrTmp(StateMap
, PInfo
, NS
);
574 } else if (NS
!= CS_None
) {
575 insertInfo(To
, PropagationInfo(NS
));
579 void ConsumedStmtVisitor::checkCallability(const PropagationInfo
&PInfo
,
580 const FunctionDecl
*FunDecl
,
581 SourceLocation BlameLoc
) {
582 assert(!PInfo
.isTest());
584 const CallableWhenAttr
*CWAttr
= FunDecl
->getAttr
<CallableWhenAttr
>();
589 ConsumedState VarState
= StateMap
->getState(PInfo
.getVar());
591 if (VarState
== CS_None
|| isCallableInState(CWAttr
, VarState
))
594 Analyzer
.WarningsHandler
.warnUseInInvalidState(
595 FunDecl
->getNameAsString(), PInfo
.getVar()->getNameAsString(),
596 stateToString(VarState
), BlameLoc
);
598 ConsumedState TmpState
= PInfo
.getAsState(StateMap
);
600 if (TmpState
== CS_None
|| isCallableInState(CWAttr
, TmpState
))
603 Analyzer
.WarningsHandler
.warnUseOfTempInInvalidState(
604 FunDecl
->getNameAsString(), stateToString(TmpState
), BlameLoc
);
608 // Factors out common behavior for function, method, and operator calls.
609 // Check parameters and set parameter state if necessary.
610 // Returns true if the state of ObjArg is set, or false otherwise.
611 bool ConsumedStmtVisitor::handleCall(const CallExpr
*Call
, const Expr
*ObjArg
,
612 const FunctionDecl
*FunD
) {
614 if (isa
<CXXOperatorCallExpr
>(Call
) && isa
<CXXMethodDecl
>(FunD
))
615 Offset
= 1; // first argument is 'this'
617 // check explicit parameters
618 for (unsigned Index
= Offset
; Index
< Call
->getNumArgs(); ++Index
) {
619 // Skip variable argument lists.
620 if (Index
- Offset
>= FunD
->getNumParams())
623 const ParmVarDecl
*Param
= FunD
->getParamDecl(Index
- Offset
);
624 QualType ParamType
= Param
->getType();
626 InfoEntry Entry
= findInfo(Call
->getArg(Index
));
628 if (Entry
== PropagationMap
.end() || Entry
->second
.isTest())
630 PropagationInfo PInfo
= Entry
->second
;
632 // Check that the parameter is in the correct state.
633 if (ParamTypestateAttr
*PTA
= Param
->getAttr
<ParamTypestateAttr
>()) {
634 ConsumedState ParamState
= PInfo
.getAsState(StateMap
);
635 ConsumedState ExpectedState
= mapParamTypestateAttrState(PTA
);
637 if (ParamState
!= ExpectedState
)
638 Analyzer
.WarningsHandler
.warnParamTypestateMismatch(
639 Call
->getArg(Index
)->getExprLoc(),
640 stateToString(ExpectedState
), stateToString(ParamState
));
643 if (!(Entry
->second
.isVar() || Entry
->second
.isTmp()))
646 // Adjust state on the caller side.
647 if (ReturnTypestateAttr
*RT
= Param
->getAttr
<ReturnTypestateAttr
>())
648 setStateForVarOrTmp(StateMap
, PInfo
, mapReturnTypestateAttrState(RT
));
649 else if (isRValueRef(ParamType
) || isConsumableType(ParamType
))
650 setStateForVarOrTmp(StateMap
, PInfo
, consumed::CS_Consumed
);
651 else if (isPointerOrRef(ParamType
) &&
652 (!ParamType
->getPointeeType().isConstQualified() ||
653 isSetOnReadPtrType(ParamType
)))
654 setStateForVarOrTmp(StateMap
, PInfo
, consumed::CS_Unknown
);
660 // check implicit 'self' parameter, if present
661 InfoEntry Entry
= findInfo(ObjArg
);
662 if (Entry
!= PropagationMap
.end()) {
663 PropagationInfo PInfo
= Entry
->second
;
664 checkCallability(PInfo
, FunD
, Call
->getExprLoc());
666 if (SetTypestateAttr
*STA
= FunD
->getAttr
<SetTypestateAttr
>()) {
668 StateMap
->setState(PInfo
.getVar(), mapSetTypestateAttrState(STA
));
671 else if (PInfo
.isTmp()) {
672 StateMap
->setState(PInfo
.getTmp(), mapSetTypestateAttrState(STA
));
676 else if (isTestingFunction(FunD
) && PInfo
.isVar()) {
677 PropagationMap
.insert(PairType(Call
,
678 PropagationInfo(PInfo
.getVar(), testsFor(FunD
))));
684 void ConsumedStmtVisitor::propagateReturnType(const Expr
*Call
,
685 const FunctionDecl
*Fun
) {
686 QualType RetType
= Fun
->getCallResultType();
687 if (RetType
->isReferenceType())
688 RetType
= RetType
->getPointeeType();
690 if (isConsumableType(RetType
)) {
691 ConsumedState ReturnState
;
692 if (ReturnTypestateAttr
*RTA
= Fun
->getAttr
<ReturnTypestateAttr
>())
693 ReturnState
= mapReturnTypestateAttrState(RTA
);
695 ReturnState
= mapConsumableAttrState(RetType
);
697 PropagationMap
.insert(PairType(Call
, PropagationInfo(ReturnState
)));
701 void ConsumedStmtVisitor::VisitBinaryOperator(const BinaryOperator
*BinOp
) {
702 switch (BinOp
->getOpcode()) {
705 InfoEntry LEntry
= findInfo(BinOp
->getLHS()),
706 REntry
= findInfo(BinOp
->getRHS());
708 VarTestResult LTest
, RTest
;
710 if (LEntry
!= PropagationMap
.end() && LEntry
->second
.isVarTest()) {
711 LTest
= LEntry
->second
.getVarTest();
714 LTest
.TestsFor
= CS_None
;
717 if (REntry
!= PropagationMap
.end() && REntry
->second
.isVarTest()) {
718 RTest
= REntry
->second
.getVarTest();
721 RTest
.TestsFor
= CS_None
;
724 if (!(LTest
.Var
== nullptr && RTest
.Var
== nullptr))
725 PropagationMap
.insert(PairType(BinOp
, PropagationInfo(BinOp
,
726 static_cast<EffectiveOp
>(BinOp
->getOpcode() == BO_LOr
), LTest
, RTest
)));
732 forwardInfo(BinOp
->getLHS(), BinOp
);
740 void ConsumedStmtVisitor::VisitCallExpr(const CallExpr
*Call
) {
741 const FunctionDecl
*FunDecl
= Call
->getDirectCallee();
745 // Special case for the std::move function.
746 // TODO: Make this more specific. (Deferred)
747 if (Call
->isCallToStdMove()) {
748 copyInfo(Call
->getArg(0), Call
, CS_Consumed
);
752 handleCall(Call
, nullptr, FunDecl
);
753 propagateReturnType(Call
, FunDecl
);
756 void ConsumedStmtVisitor::VisitCastExpr(const CastExpr
*Cast
) {
757 forwardInfo(Cast
->getSubExpr(), Cast
);
760 void ConsumedStmtVisitor::VisitCXXBindTemporaryExpr(
761 const CXXBindTemporaryExpr
*Temp
) {
763 InfoEntry Entry
= findInfo(Temp
->getSubExpr());
765 if (Entry
!= PropagationMap
.end() && !Entry
->second
.isTest()) {
766 StateMap
->setState(Temp
, Entry
->second
.getAsState(StateMap
));
767 PropagationMap
.insert(PairType(Temp
, PropagationInfo(Temp
)));
771 void ConsumedStmtVisitor::VisitCXXConstructExpr(const CXXConstructExpr
*Call
) {
772 CXXConstructorDecl
*Constructor
= Call
->getConstructor();
774 QualType ThisType
= Constructor
->getFunctionObjectParameterType();
776 if (!isConsumableType(ThisType
))
779 // FIXME: What should happen if someone annotates the move constructor?
780 if (ReturnTypestateAttr
*RTA
= Constructor
->getAttr
<ReturnTypestateAttr
>()) {
781 // TODO: Adjust state of args appropriately.
782 ConsumedState RetState
= mapReturnTypestateAttrState(RTA
);
783 PropagationMap
.insert(PairType(Call
, PropagationInfo(RetState
)));
784 } else if (Constructor
->isDefaultConstructor()) {
785 PropagationMap
.insert(PairType(Call
,
786 PropagationInfo(consumed::CS_Consumed
)));
787 } else if (Constructor
->isMoveConstructor()) {
788 copyInfo(Call
->getArg(0), Call
, CS_Consumed
);
789 } else if (Constructor
->isCopyConstructor()) {
790 // Copy state from arg. If setStateOnRead then set arg to CS_Unknown.
792 isSetOnReadPtrType(Constructor
->getThisType()) ?
793 CS_Unknown
: CS_None
;
794 copyInfo(Call
->getArg(0), Call
, NS
);
796 // TODO: Adjust state of args appropriately.
797 ConsumedState RetState
= mapConsumableAttrState(ThisType
);
798 PropagationMap
.insert(PairType(Call
, PropagationInfo(RetState
)));
802 void ConsumedStmtVisitor::VisitCXXMemberCallExpr(
803 const CXXMemberCallExpr
*Call
) {
804 CXXMethodDecl
* MD
= Call
->getMethodDecl();
808 handleCall(Call
, Call
->getImplicitObjectArgument(), MD
);
809 propagateReturnType(Call
, MD
);
812 void ConsumedStmtVisitor::VisitCXXOperatorCallExpr(
813 const CXXOperatorCallExpr
*Call
) {
814 const auto *FunDecl
= dyn_cast_or_null
<FunctionDecl
>(Call
->getDirectCallee());
815 if (!FunDecl
) return;
817 if (Call
->getOperator() == OO_Equal
) {
818 ConsumedState CS
= getInfo(Call
->getArg(1));
819 if (!handleCall(Call
, Call
->getArg(0), FunDecl
))
820 setInfo(Call
->getArg(0), CS
);
824 if (const auto *MCall
= dyn_cast
<CXXMemberCallExpr
>(Call
))
825 handleCall(MCall
, MCall
->getImplicitObjectArgument(), FunDecl
);
827 handleCall(Call
, Call
->getArg(0), FunDecl
);
829 propagateReturnType(Call
, FunDecl
);
832 void ConsumedStmtVisitor::VisitDeclRefExpr(const DeclRefExpr
*DeclRef
) {
833 if (const auto *Var
= dyn_cast_or_null
<VarDecl
>(DeclRef
->getDecl()))
834 if (StateMap
->getState(Var
) != consumed::CS_None
)
835 PropagationMap
.insert(PairType(DeclRef
, PropagationInfo(Var
)));
838 void ConsumedStmtVisitor::VisitDeclStmt(const DeclStmt
*DeclS
) {
839 for (const auto *DI
: DeclS
->decls())
840 if (isa
<VarDecl
>(DI
))
841 VisitVarDecl(cast
<VarDecl
>(DI
));
843 if (DeclS
->isSingleDecl())
844 if (const auto *Var
= dyn_cast_or_null
<VarDecl
>(DeclS
->getSingleDecl()))
845 PropagationMap
.insert(PairType(DeclS
, PropagationInfo(Var
)));
848 void ConsumedStmtVisitor::VisitMaterializeTemporaryExpr(
849 const MaterializeTemporaryExpr
*Temp
) {
850 forwardInfo(Temp
->getSubExpr(), Temp
);
853 void ConsumedStmtVisitor::VisitMemberExpr(const MemberExpr
*MExpr
) {
854 forwardInfo(MExpr
->getBase(), MExpr
);
857 void ConsumedStmtVisitor::VisitParmVarDecl(const ParmVarDecl
*Param
) {
858 QualType ParamType
= Param
->getType();
859 ConsumedState ParamState
= consumed::CS_None
;
861 if (const ParamTypestateAttr
*PTA
= Param
->getAttr
<ParamTypestateAttr
>())
862 ParamState
= mapParamTypestateAttrState(PTA
);
863 else if (isConsumableType(ParamType
))
864 ParamState
= mapConsumableAttrState(ParamType
);
865 else if (isRValueRef(ParamType
) &&
866 isConsumableType(ParamType
->getPointeeType()))
867 ParamState
= mapConsumableAttrState(ParamType
->getPointeeType());
868 else if (ParamType
->isReferenceType() &&
869 isConsumableType(ParamType
->getPointeeType()))
870 ParamState
= consumed::CS_Unknown
;
872 if (ParamState
!= CS_None
)
873 StateMap
->setState(Param
, ParamState
);
876 void ConsumedStmtVisitor::VisitReturnStmt(const ReturnStmt
*Ret
) {
877 ConsumedState ExpectedState
= Analyzer
.getExpectedReturnState();
879 if (ExpectedState
!= CS_None
) {
880 InfoEntry Entry
= findInfo(Ret
->getRetValue());
882 if (Entry
!= PropagationMap
.end()) {
883 ConsumedState RetState
= Entry
->second
.getAsState(StateMap
);
885 if (RetState
!= ExpectedState
)
886 Analyzer
.WarningsHandler
.warnReturnTypestateMismatch(
887 Ret
->getReturnLoc(), stateToString(ExpectedState
),
888 stateToString(RetState
));
892 StateMap
->checkParamsForReturnTypestate(Ret
->getBeginLoc(),
893 Analyzer
.WarningsHandler
);
896 void ConsumedStmtVisitor::VisitUnaryOperator(const UnaryOperator
*UOp
) {
897 InfoEntry Entry
= findInfo(UOp
->getSubExpr());
898 if (Entry
== PropagationMap
.end()) return;
900 switch (UOp
->getOpcode()) {
902 PropagationMap
.insert(PairType(UOp
, Entry
->second
));
906 if (Entry
->second
.isTest())
907 PropagationMap
.insert(PairType(UOp
, Entry
->second
.invertTest()));
915 // TODO: See if I need to check for reference types here.
916 void ConsumedStmtVisitor::VisitVarDecl(const VarDecl
*Var
) {
917 if (isConsumableType(Var
->getType())) {
918 if (Var
->hasInit()) {
919 MapType::iterator VIT
= findInfo(Var
->getInit()->IgnoreImplicit());
920 if (VIT
!= PropagationMap
.end()) {
921 PropagationInfo PInfo
= VIT
->second
;
922 ConsumedState St
= PInfo
.getAsState(StateMap
);
924 if (St
!= consumed::CS_None
) {
925 StateMap
->setState(Var
, St
);
931 StateMap
->setState(Var
, consumed::CS_Unknown
);
935 static void splitVarStateForIf(const IfStmt
*IfNode
, const VarTestResult
&Test
,
936 ConsumedStateMap
*ThenStates
,
937 ConsumedStateMap
*ElseStates
) {
938 ConsumedState VarState
= ThenStates
->getState(Test
.Var
);
940 if (VarState
== CS_Unknown
) {
941 ThenStates
->setState(Test
.Var
, Test
.TestsFor
);
942 ElseStates
->setState(Test
.Var
, invertConsumedUnconsumed(Test
.TestsFor
));
943 } else if (VarState
== invertConsumedUnconsumed(Test
.TestsFor
)) {
944 ThenStates
->markUnreachable();
945 } else if (VarState
== Test
.TestsFor
) {
946 ElseStates
->markUnreachable();
950 static void splitVarStateForIfBinOp(const PropagationInfo
&PInfo
,
951 ConsumedStateMap
*ThenStates
,
952 ConsumedStateMap
*ElseStates
) {
953 const VarTestResult
<est
= PInfo
.getLTest(),
954 &RTest
= PInfo
.getRTest();
956 ConsumedState LState
= LTest
.Var
? ThenStates
->getState(LTest
.Var
) : CS_None
,
957 RState
= RTest
.Var
? ThenStates
->getState(RTest
.Var
) : CS_None
;
960 if (PInfo
.testEffectiveOp() == EO_And
) {
961 if (LState
== CS_Unknown
) {
962 ThenStates
->setState(LTest
.Var
, LTest
.TestsFor
);
963 } else if (LState
== invertConsumedUnconsumed(LTest
.TestsFor
)) {
964 ThenStates
->markUnreachable();
965 } else if (LState
== LTest
.TestsFor
&& isKnownState(RState
)) {
966 if (RState
== RTest
.TestsFor
)
967 ElseStates
->markUnreachable();
969 ThenStates
->markUnreachable();
972 if (LState
== CS_Unknown
) {
973 ElseStates
->setState(LTest
.Var
,
974 invertConsumedUnconsumed(LTest
.TestsFor
));
975 } else if (LState
== LTest
.TestsFor
) {
976 ElseStates
->markUnreachable();
977 } else if (LState
== invertConsumedUnconsumed(LTest
.TestsFor
) &&
978 isKnownState(RState
)) {
979 if (RState
== RTest
.TestsFor
)
980 ElseStates
->markUnreachable();
982 ThenStates
->markUnreachable();
988 if (PInfo
.testEffectiveOp() == EO_And
) {
989 if (RState
== CS_Unknown
)
990 ThenStates
->setState(RTest
.Var
, RTest
.TestsFor
);
991 else if (RState
== invertConsumedUnconsumed(RTest
.TestsFor
))
992 ThenStates
->markUnreachable();
994 if (RState
== CS_Unknown
)
995 ElseStates
->setState(RTest
.Var
,
996 invertConsumedUnconsumed(RTest
.TestsFor
));
997 else if (RState
== RTest
.TestsFor
)
998 ElseStates
->markUnreachable();
1003 bool ConsumedBlockInfo::allBackEdgesVisited(const CFGBlock
*CurrBlock
,
1004 const CFGBlock
*TargetBlock
) {
1005 assert(CurrBlock
&& "Block pointer must not be NULL");
1006 assert(TargetBlock
&& "TargetBlock pointer must not be NULL");
1008 unsigned int CurrBlockOrder
= VisitOrder
[CurrBlock
->getBlockID()];
1009 for (CFGBlock::const_pred_iterator PI
= TargetBlock
->pred_begin(),
1010 PE
= TargetBlock
->pred_end(); PI
!= PE
; ++PI
) {
1011 if (*PI
&& CurrBlockOrder
< VisitOrder
[(*PI
)->getBlockID()] )
1017 void ConsumedBlockInfo::addInfo(
1018 const CFGBlock
*Block
, ConsumedStateMap
*StateMap
,
1019 std::unique_ptr
<ConsumedStateMap
> &OwnedStateMap
) {
1020 assert(Block
&& "Block pointer must not be NULL");
1022 auto &Entry
= StateMapsArray
[Block
->getBlockID()];
1025 Entry
->intersect(*StateMap
);
1026 } else if (OwnedStateMap
)
1027 Entry
= std::move(OwnedStateMap
);
1029 Entry
= std::make_unique
<ConsumedStateMap
>(*StateMap
);
1032 void ConsumedBlockInfo::addInfo(const CFGBlock
*Block
,
1033 std::unique_ptr
<ConsumedStateMap
> StateMap
) {
1034 assert(Block
&& "Block pointer must not be NULL");
1036 auto &Entry
= StateMapsArray
[Block
->getBlockID()];
1039 Entry
->intersect(*StateMap
);
1041 Entry
= std::move(StateMap
);
1045 ConsumedStateMap
* ConsumedBlockInfo::borrowInfo(const CFGBlock
*Block
) {
1046 assert(Block
&& "Block pointer must not be NULL");
1047 assert(StateMapsArray
[Block
->getBlockID()] && "Block has no block info");
1049 return StateMapsArray
[Block
->getBlockID()].get();
1052 void ConsumedBlockInfo::discardInfo(const CFGBlock
*Block
) {
1053 StateMapsArray
[Block
->getBlockID()] = nullptr;
1056 std::unique_ptr
<ConsumedStateMap
>
1057 ConsumedBlockInfo::getInfo(const CFGBlock
*Block
) {
1058 assert(Block
&& "Block pointer must not be NULL");
1060 auto &Entry
= StateMapsArray
[Block
->getBlockID()];
1061 return isBackEdgeTarget(Block
) ? std::make_unique
<ConsumedStateMap
>(*Entry
)
1065 bool ConsumedBlockInfo::isBackEdge(const CFGBlock
*From
, const CFGBlock
*To
) {
1066 assert(From
&& "From block must not be NULL");
1067 assert(To
&& "From block must not be NULL");
1069 return VisitOrder
[From
->getBlockID()] > VisitOrder
[To
->getBlockID()];
1072 bool ConsumedBlockInfo::isBackEdgeTarget(const CFGBlock
*Block
) {
1073 assert(Block
&& "Block pointer must not be NULL");
1075 // Anything with less than two predecessors can't be the target of a back
1077 if (Block
->pred_size() < 2)
1080 unsigned int BlockVisitOrder
= VisitOrder
[Block
->getBlockID()];
1081 for (CFGBlock::const_pred_iterator PI
= Block
->pred_begin(),
1082 PE
= Block
->pred_end(); PI
!= PE
; ++PI
) {
1083 if (*PI
&& BlockVisitOrder
< VisitOrder
[(*PI
)->getBlockID()])
1089 void ConsumedStateMap::checkParamsForReturnTypestate(SourceLocation BlameLoc
,
1090 ConsumedWarningsHandlerBase
&WarningsHandler
) const {
1092 for (const auto &DM
: VarMap
) {
1093 if (isa
<ParmVarDecl
>(DM
.first
)) {
1094 const auto *Param
= cast
<ParmVarDecl
>(DM
.first
);
1095 const ReturnTypestateAttr
*RTA
= Param
->getAttr
<ReturnTypestateAttr
>();
1100 ConsumedState ExpectedState
= mapReturnTypestateAttrState(RTA
);
1101 if (DM
.second
!= ExpectedState
)
1102 WarningsHandler
.warnParamReturnTypestateMismatch(BlameLoc
,
1103 Param
->getNameAsString(), stateToString(ExpectedState
),
1104 stateToString(DM
.second
));
1109 void ConsumedStateMap::clearTemporaries() {
1113 ConsumedState
ConsumedStateMap::getState(const VarDecl
*Var
) const {
1114 VarMapType::const_iterator Entry
= VarMap
.find(Var
);
1116 if (Entry
!= VarMap
.end())
1117 return Entry
->second
;
1123 ConsumedStateMap::getState(const CXXBindTemporaryExpr
*Tmp
) const {
1124 TmpMapType::const_iterator Entry
= TmpMap
.find(Tmp
);
1126 if (Entry
!= TmpMap
.end())
1127 return Entry
->second
;
1132 void ConsumedStateMap::intersect(const ConsumedStateMap
&Other
) {
1133 ConsumedState LocalState
;
1135 if (this->From
&& this->From
== Other
.From
&& !Other
.Reachable
) {
1136 this->markUnreachable();
1140 for (const auto &DM
: Other
.VarMap
) {
1141 LocalState
= this->getState(DM
.first
);
1143 if (LocalState
== CS_None
)
1146 if (LocalState
!= DM
.second
)
1147 VarMap
[DM
.first
] = CS_Unknown
;
1151 void ConsumedStateMap::intersectAtLoopHead(const CFGBlock
*LoopHead
,
1152 const CFGBlock
*LoopBack
, const ConsumedStateMap
*LoopBackStates
,
1153 ConsumedWarningsHandlerBase
&WarningsHandler
) {
1155 ConsumedState LocalState
;
1156 SourceLocation BlameLoc
= getLastStmtLoc(LoopBack
);
1158 for (const auto &DM
: LoopBackStates
->VarMap
) {
1159 LocalState
= this->getState(DM
.first
);
1161 if (LocalState
== CS_None
)
1164 if (LocalState
!= DM
.second
) {
1165 VarMap
[DM
.first
] = CS_Unknown
;
1166 WarningsHandler
.warnLoopStateMismatch(BlameLoc
,
1167 DM
.first
->getNameAsString());
1172 void ConsumedStateMap::markUnreachable() {
1173 this->Reachable
= false;
1178 void ConsumedStateMap::setState(const VarDecl
*Var
, ConsumedState State
) {
1179 VarMap
[Var
] = State
;
1182 void ConsumedStateMap::setState(const CXXBindTemporaryExpr
*Tmp
,
1183 ConsumedState State
) {
1184 TmpMap
[Tmp
] = State
;
1187 void ConsumedStateMap::remove(const CXXBindTemporaryExpr
*Tmp
) {
1191 bool ConsumedStateMap::operator!=(const ConsumedStateMap
*Other
) const {
1192 for (const auto &DM
: Other
->VarMap
)
1193 if (this->getState(DM
.first
) != DM
.second
)
1198 void ConsumedAnalyzer::determineExpectedReturnState(AnalysisDeclContext
&AC
,
1199 const FunctionDecl
*D
) {
1200 QualType ReturnType
;
1201 if (const auto *Constructor
= dyn_cast
<CXXConstructorDecl
>(D
)) {
1202 ReturnType
= Constructor
->getFunctionObjectParameterType();
1204 ReturnType
= D
->getCallResultType();
1206 if (const ReturnTypestateAttr
*RTSAttr
= D
->getAttr
<ReturnTypestateAttr
>()) {
1207 const CXXRecordDecl
*RD
= ReturnType
->getAsCXXRecordDecl();
1208 if (!RD
|| !RD
->hasAttr
<ConsumableAttr
>()) {
1209 // FIXME: This should be removed when template instantiation propagates
1210 // attributes at template specialization definition, not
1211 // declaration. When it is removed the test needs to be enabled
1212 // in SemaDeclAttr.cpp.
1213 WarningsHandler
.warnReturnTypestateForUnconsumableType(
1214 RTSAttr
->getLocation(), ReturnType
.getAsString());
1215 ExpectedReturnState
= CS_None
;
1217 ExpectedReturnState
= mapReturnTypestateAttrState(RTSAttr
);
1218 } else if (isConsumableType(ReturnType
)) {
1219 if (isAutoCastType(ReturnType
)) // We can auto-cast the state to the
1220 ExpectedReturnState
= CS_None
; // expected state.
1222 ExpectedReturnState
= mapConsumableAttrState(ReturnType
);
1225 ExpectedReturnState
= CS_None
;
1228 bool ConsumedAnalyzer::splitState(const CFGBlock
*CurrBlock
,
1229 const ConsumedStmtVisitor
&Visitor
) {
1230 std::unique_ptr
<ConsumedStateMap
> FalseStates(
1231 new ConsumedStateMap(*CurrStates
));
1232 PropagationInfo PInfo
;
1234 if (const auto *IfNode
=
1235 dyn_cast_or_null
<IfStmt
>(CurrBlock
->getTerminator().getStmt())) {
1236 const Expr
*Cond
= IfNode
->getCond();
1238 PInfo
= Visitor
.getInfo(Cond
);
1239 if (!PInfo
.isValid() && isa
<BinaryOperator
>(Cond
))
1240 PInfo
= Visitor
.getInfo(cast
<BinaryOperator
>(Cond
)->getRHS());
1242 if (PInfo
.isVarTest()) {
1243 CurrStates
->setSource(Cond
);
1244 FalseStates
->setSource(Cond
);
1245 splitVarStateForIf(IfNode
, PInfo
.getVarTest(), CurrStates
.get(),
1247 } else if (PInfo
.isBinTest()) {
1248 CurrStates
->setSource(PInfo
.testSourceNode());
1249 FalseStates
->setSource(PInfo
.testSourceNode());
1250 splitVarStateForIfBinOp(PInfo
, CurrStates
.get(), FalseStates
.get());
1254 } else if (const auto *BinOp
=
1255 dyn_cast_or_null
<BinaryOperator
>(CurrBlock
->getTerminator().getStmt())) {
1256 PInfo
= Visitor
.getInfo(BinOp
->getLHS());
1257 if (!PInfo
.isVarTest()) {
1258 if ((BinOp
= dyn_cast_or_null
<BinaryOperator
>(BinOp
->getLHS()))) {
1259 PInfo
= Visitor
.getInfo(BinOp
->getRHS());
1261 if (!PInfo
.isVarTest())
1268 CurrStates
->setSource(BinOp
);
1269 FalseStates
->setSource(BinOp
);
1271 const VarTestResult
&Test
= PInfo
.getVarTest();
1272 ConsumedState VarState
= CurrStates
->getState(Test
.Var
);
1274 if (BinOp
->getOpcode() == BO_LAnd
) {
1275 if (VarState
== CS_Unknown
)
1276 CurrStates
->setState(Test
.Var
, Test
.TestsFor
);
1277 else if (VarState
== invertConsumedUnconsumed(Test
.TestsFor
))
1278 CurrStates
->markUnreachable();
1280 } else if (BinOp
->getOpcode() == BO_LOr
) {
1281 if (VarState
== CS_Unknown
)
1282 FalseStates
->setState(Test
.Var
,
1283 invertConsumedUnconsumed(Test
.TestsFor
));
1284 else if (VarState
== Test
.TestsFor
)
1285 FalseStates
->markUnreachable();
1291 CFGBlock::const_succ_iterator SI
= CurrBlock
->succ_begin();
1294 BlockInfo
.addInfo(*SI
, std::move(CurrStates
));
1296 CurrStates
= nullptr;
1299 BlockInfo
.addInfo(*SI
, std::move(FalseStates
));
1304 void ConsumedAnalyzer::run(AnalysisDeclContext
&AC
) {
1305 const auto *D
= dyn_cast_or_null
<FunctionDecl
>(AC
.getDecl());
1309 CFG
*CFGraph
= AC
.getCFG();
1313 determineExpectedReturnState(AC
, D
);
1315 PostOrderCFGView
*SortedGraph
= AC
.getAnalysis
<PostOrderCFGView
>();
1316 // AC.getCFG()->viewCFG(LangOptions());
1318 BlockInfo
= ConsumedBlockInfo(CFGraph
->getNumBlockIDs(), SortedGraph
);
1320 CurrStates
= std::make_unique
<ConsumedStateMap
>();
1321 ConsumedStmtVisitor
Visitor(*this, CurrStates
.get());
1323 // Add all trackable parameters to the state map.
1324 for (const auto *PI
: D
->parameters())
1325 Visitor
.VisitParmVarDecl(PI
);
1327 // Visit all of the function's basic blocks.
1328 for (const auto *CurrBlock
: *SortedGraph
) {
1330 CurrStates
= BlockInfo
.getInfo(CurrBlock
);
1334 } else if (!CurrStates
->isReachable()) {
1335 CurrStates
= nullptr;
1339 Visitor
.reset(CurrStates
.get());
1341 // Visit all of the basic block's statements.
1342 for (const auto &B
: *CurrBlock
) {
1343 switch (B
.getKind()) {
1344 case CFGElement::Statement
:
1345 Visitor
.Visit(B
.castAs
<CFGStmt
>().getStmt());
1348 case CFGElement::TemporaryDtor
: {
1349 const CFGTemporaryDtor
&DTor
= B
.castAs
<CFGTemporaryDtor
>();
1350 const CXXBindTemporaryExpr
*BTE
= DTor
.getBindTemporaryExpr();
1352 Visitor
.checkCallability(PropagationInfo(BTE
),
1353 DTor
.getDestructorDecl(AC
.getASTContext()),
1355 CurrStates
->remove(BTE
);
1359 case CFGElement::AutomaticObjectDtor
: {
1360 const CFGAutomaticObjDtor
&DTor
= B
.castAs
<CFGAutomaticObjDtor
>();
1361 SourceLocation Loc
= DTor
.getTriggerStmt()->getEndLoc();
1362 const VarDecl
*Var
= DTor
.getVarDecl();
1364 Visitor
.checkCallability(PropagationInfo(Var
),
1365 DTor
.getDestructorDecl(AC
.getASTContext()),
1375 // TODO: Handle other forms of branching with precision, including while-
1376 // and for-loops. (Deferred)
1377 if (!splitState(CurrBlock
, Visitor
)) {
1378 CurrStates
->setSource(nullptr);
1380 if (CurrBlock
->succ_size() > 1 ||
1381 (CurrBlock
->succ_size() == 1 &&
1382 (*CurrBlock
->succ_begin())->pred_size() > 1)) {
1384 auto *RawState
= CurrStates
.get();
1386 for (CFGBlock::const_succ_iterator SI
= CurrBlock
->succ_begin(),
1387 SE
= CurrBlock
->succ_end(); SI
!= SE
; ++SI
) {
1388 if (*SI
== nullptr) continue;
1390 if (BlockInfo
.isBackEdge(CurrBlock
, *SI
)) {
1391 BlockInfo
.borrowInfo(*SI
)->intersectAtLoopHead(
1392 *SI
, CurrBlock
, RawState
, WarningsHandler
);
1394 if (BlockInfo
.allBackEdgesVisited(CurrBlock
, *SI
))
1395 BlockInfo
.discardInfo(*SI
);
1397 BlockInfo
.addInfo(*SI
, RawState
, CurrStates
);
1401 CurrStates
= nullptr;
1405 if (CurrBlock
== &AC
.getCFG()->getExit() &&
1406 D
->getCallResultType()->isVoidType())
1407 CurrStates
->checkParamsForReturnTypestate(D
->getLocation(),
1409 } // End of block iterator.
1411 // Delete the last existing state map.
1412 CurrStates
= nullptr;
1414 WarningsHandler
.emitDiagnostics();