1f4a2713aSLionel Sambuc //===- Consumed.cpp --------------------------------------------*- C++ --*-===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // A intra-procedural analysis for checking consumed properties. This is based,
11f4a2713aSLionel Sambuc // in part, on research on linear types.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc
15f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
16f4a2713aSLionel Sambuc #include "clang/AST/Attr.h"
17f4a2713aSLionel Sambuc #include "clang/AST/DeclCXX.h"
18f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
19f4a2713aSLionel Sambuc #include "clang/AST/RecursiveASTVisitor.h"
20f4a2713aSLionel Sambuc #include "clang/AST/StmtCXX.h"
21*0a6a1f1dSLionel Sambuc #include "clang/AST/StmtVisitor.h"
22f4a2713aSLionel Sambuc #include "clang/AST/Type.h"
23*0a6a1f1dSLionel Sambuc #include "clang/Analysis/Analyses/Consumed.h"
24f4a2713aSLionel Sambuc #include "clang/Analysis/Analyses/PostOrderCFGView.h"
25f4a2713aSLionel Sambuc #include "clang/Analysis/AnalysisContext.h"
26f4a2713aSLionel Sambuc #include "clang/Analysis/CFG.h"
27f4a2713aSLionel Sambuc #include "clang/Basic/OperatorKinds.h"
28f4a2713aSLionel Sambuc #include "clang/Basic/SourceLocation.h"
29f4a2713aSLionel Sambuc #include "llvm/ADT/DenseMap.h"
30f4a2713aSLionel Sambuc #include "llvm/ADT/SmallVector.h"
31f4a2713aSLionel Sambuc #include "llvm/Support/Compiler.h"
32f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
33*0a6a1f1dSLionel Sambuc #include <memory>
34f4a2713aSLionel Sambuc
35f4a2713aSLionel Sambuc // TODO: Adjust states of args to constructors in the same way that arguments to
36f4a2713aSLionel Sambuc // function calls are handled.
37f4a2713aSLionel Sambuc // TODO: Use information from tests in for- and while-loop conditional.
38f4a2713aSLionel Sambuc // TODO: Add notes about the actual and expected state for
39f4a2713aSLionel Sambuc // TODO: Correctly identify unreachable blocks when chaining boolean operators.
40f4a2713aSLionel Sambuc // TODO: Adjust the parser and AttributesList class to support lists of
41f4a2713aSLionel Sambuc // identifiers.
42f4a2713aSLionel Sambuc // TODO: Warn about unreachable code.
43f4a2713aSLionel Sambuc // TODO: Switch to using a bitmap to track unreachable blocks.
44f4a2713aSLionel Sambuc // TODO: Handle variable definitions, e.g. bool valid = x.isValid();
45f4a2713aSLionel Sambuc // if (valid) ...; (Deferred)
46f4a2713aSLionel Sambuc // TODO: Take notes on state transitions to provide better warning messages.
47f4a2713aSLionel Sambuc // (Deferred)
48f4a2713aSLionel Sambuc // TODO: Test nested conditionals: A) Checking the same value multiple times,
49f4a2713aSLionel Sambuc // and 2) Checking different values. (Deferred)
50f4a2713aSLionel Sambuc
51f4a2713aSLionel Sambuc using namespace clang;
52f4a2713aSLionel Sambuc using namespace consumed;
53f4a2713aSLionel Sambuc
54f4a2713aSLionel Sambuc // Key method definition
~ConsumedWarningsHandlerBase()55f4a2713aSLionel Sambuc ConsumedWarningsHandlerBase::~ConsumedWarningsHandlerBase() {}
56f4a2713aSLionel Sambuc
getFirstStmtLoc(const CFGBlock * Block)57f4a2713aSLionel Sambuc static SourceLocation getFirstStmtLoc(const CFGBlock *Block) {
58f4a2713aSLionel Sambuc // Find the source location of the first statement in the block, if the block
59f4a2713aSLionel Sambuc // is not empty.
60*0a6a1f1dSLionel Sambuc for (const auto &B : *Block)
61*0a6a1f1dSLionel Sambuc if (Optional<CFGStmt> CS = B.getAs<CFGStmt>())
62f4a2713aSLionel Sambuc return CS->getStmt()->getLocStart();
63f4a2713aSLionel Sambuc
64f4a2713aSLionel Sambuc // Block is empty.
65f4a2713aSLionel Sambuc // If we have one successor, return the first statement in that block
66f4a2713aSLionel Sambuc if (Block->succ_size() == 1 && *Block->succ_begin())
67f4a2713aSLionel Sambuc return getFirstStmtLoc(*Block->succ_begin());
68f4a2713aSLionel Sambuc
69f4a2713aSLionel Sambuc return SourceLocation();
70f4a2713aSLionel Sambuc }
71f4a2713aSLionel Sambuc
getLastStmtLoc(const CFGBlock * Block)72f4a2713aSLionel Sambuc static SourceLocation getLastStmtLoc(const CFGBlock *Block) {
73f4a2713aSLionel Sambuc // Find the source location of the last statement in the block, if the block
74f4a2713aSLionel Sambuc // is not empty.
75f4a2713aSLionel Sambuc if (const Stmt *StmtNode = Block->getTerminator()) {
76f4a2713aSLionel Sambuc return StmtNode->getLocStart();
77f4a2713aSLionel Sambuc } else {
78f4a2713aSLionel Sambuc for (CFGBlock::const_reverse_iterator BI = Block->rbegin(),
79f4a2713aSLionel Sambuc BE = Block->rend(); BI != BE; ++BI) {
80f4a2713aSLionel Sambuc if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>())
81f4a2713aSLionel Sambuc return CS->getStmt()->getLocStart();
82f4a2713aSLionel Sambuc }
83f4a2713aSLionel Sambuc }
84f4a2713aSLionel Sambuc
85f4a2713aSLionel Sambuc // If we have one successor, return the first statement in that block
86f4a2713aSLionel Sambuc SourceLocation Loc;
87f4a2713aSLionel Sambuc if (Block->succ_size() == 1 && *Block->succ_begin())
88f4a2713aSLionel Sambuc Loc = getFirstStmtLoc(*Block->succ_begin());
89f4a2713aSLionel Sambuc if (Loc.isValid())
90f4a2713aSLionel Sambuc return Loc;
91f4a2713aSLionel Sambuc
92f4a2713aSLionel Sambuc // If we have one predecessor, return the last statement in that block
93f4a2713aSLionel Sambuc if (Block->pred_size() == 1 && *Block->pred_begin())
94f4a2713aSLionel Sambuc return getLastStmtLoc(*Block->pred_begin());
95f4a2713aSLionel Sambuc
96f4a2713aSLionel Sambuc return Loc;
97f4a2713aSLionel Sambuc }
98f4a2713aSLionel Sambuc
invertConsumedUnconsumed(ConsumedState State)99f4a2713aSLionel Sambuc static ConsumedState invertConsumedUnconsumed(ConsumedState State) {
100f4a2713aSLionel Sambuc switch (State) {
101f4a2713aSLionel Sambuc case CS_Unconsumed:
102f4a2713aSLionel Sambuc return CS_Consumed;
103f4a2713aSLionel Sambuc case CS_Consumed:
104f4a2713aSLionel Sambuc return CS_Unconsumed;
105f4a2713aSLionel Sambuc case CS_None:
106f4a2713aSLionel Sambuc return CS_None;
107f4a2713aSLionel Sambuc case CS_Unknown:
108f4a2713aSLionel Sambuc return CS_Unknown;
109f4a2713aSLionel Sambuc }
110f4a2713aSLionel Sambuc llvm_unreachable("invalid enum");
111f4a2713aSLionel Sambuc }
112f4a2713aSLionel Sambuc
isCallableInState(const CallableWhenAttr * CWAttr,ConsumedState State)113f4a2713aSLionel Sambuc static bool isCallableInState(const CallableWhenAttr *CWAttr,
114f4a2713aSLionel Sambuc ConsumedState State) {
115f4a2713aSLionel Sambuc
116*0a6a1f1dSLionel Sambuc for (const auto &S : CWAttr->callableStates()) {
117f4a2713aSLionel Sambuc ConsumedState MappedAttrState = CS_None;
118f4a2713aSLionel Sambuc
119*0a6a1f1dSLionel Sambuc switch (S) {
120f4a2713aSLionel Sambuc case CallableWhenAttr::Unknown:
121f4a2713aSLionel Sambuc MappedAttrState = CS_Unknown;
122f4a2713aSLionel Sambuc break;
123f4a2713aSLionel Sambuc
124f4a2713aSLionel Sambuc case CallableWhenAttr::Unconsumed:
125f4a2713aSLionel Sambuc MappedAttrState = CS_Unconsumed;
126f4a2713aSLionel Sambuc break;
127f4a2713aSLionel Sambuc
128f4a2713aSLionel Sambuc case CallableWhenAttr::Consumed:
129f4a2713aSLionel Sambuc MappedAttrState = CS_Consumed;
130f4a2713aSLionel Sambuc break;
131f4a2713aSLionel Sambuc }
132f4a2713aSLionel Sambuc
133f4a2713aSLionel Sambuc if (MappedAttrState == State)
134f4a2713aSLionel Sambuc return true;
135f4a2713aSLionel Sambuc }
136f4a2713aSLionel Sambuc
137f4a2713aSLionel Sambuc return false;
138f4a2713aSLionel Sambuc }
139f4a2713aSLionel Sambuc
140*0a6a1f1dSLionel Sambuc
isConsumableType(const QualType & QT)141f4a2713aSLionel Sambuc static bool isConsumableType(const QualType &QT) {
142f4a2713aSLionel Sambuc if (QT->isPointerType() || QT->isReferenceType())
143f4a2713aSLionel Sambuc return false;
144f4a2713aSLionel Sambuc
145f4a2713aSLionel Sambuc if (const CXXRecordDecl *RD = QT->getAsCXXRecordDecl())
146f4a2713aSLionel Sambuc return RD->hasAttr<ConsumableAttr>();
147f4a2713aSLionel Sambuc
148f4a2713aSLionel Sambuc return false;
149f4a2713aSLionel Sambuc }
150f4a2713aSLionel Sambuc
isAutoCastType(const QualType & QT)151*0a6a1f1dSLionel Sambuc static bool isAutoCastType(const QualType &QT) {
152*0a6a1f1dSLionel Sambuc if (QT->isPointerType() || QT->isReferenceType())
153*0a6a1f1dSLionel Sambuc return false;
154*0a6a1f1dSLionel Sambuc
155*0a6a1f1dSLionel Sambuc if (const CXXRecordDecl *RD = QT->getAsCXXRecordDecl())
156*0a6a1f1dSLionel Sambuc return RD->hasAttr<ConsumableAutoCastAttr>();
157*0a6a1f1dSLionel Sambuc
158*0a6a1f1dSLionel Sambuc return false;
159*0a6a1f1dSLionel Sambuc }
160*0a6a1f1dSLionel Sambuc
isSetOnReadPtrType(const QualType & QT)161*0a6a1f1dSLionel Sambuc static bool isSetOnReadPtrType(const QualType &QT) {
162*0a6a1f1dSLionel Sambuc if (const CXXRecordDecl *RD = QT->getPointeeCXXRecordDecl())
163*0a6a1f1dSLionel Sambuc return RD->hasAttr<ConsumableSetOnReadAttr>();
164*0a6a1f1dSLionel Sambuc return false;
165*0a6a1f1dSLionel Sambuc }
166*0a6a1f1dSLionel Sambuc
167*0a6a1f1dSLionel Sambuc
isKnownState(ConsumedState State)168f4a2713aSLionel Sambuc static bool isKnownState(ConsumedState State) {
169f4a2713aSLionel Sambuc switch (State) {
170f4a2713aSLionel Sambuc case CS_Unconsumed:
171f4a2713aSLionel Sambuc case CS_Consumed:
172f4a2713aSLionel Sambuc return true;
173f4a2713aSLionel Sambuc case CS_None:
174f4a2713aSLionel Sambuc case CS_Unknown:
175f4a2713aSLionel Sambuc return false;
176f4a2713aSLionel Sambuc }
177f4a2713aSLionel Sambuc llvm_unreachable("invalid enum");
178f4a2713aSLionel Sambuc }
179f4a2713aSLionel Sambuc
isRValueRef(QualType ParamType)180*0a6a1f1dSLionel Sambuc static bool isRValueRef(QualType ParamType) {
181*0a6a1f1dSLionel Sambuc return ParamType->isRValueReferenceType();
182f4a2713aSLionel Sambuc }
183f4a2713aSLionel Sambuc
isTestingFunction(const FunctionDecl * FunDecl)184f4a2713aSLionel Sambuc static bool isTestingFunction(const FunctionDecl *FunDecl) {
185f4a2713aSLionel Sambuc return FunDecl->hasAttr<TestTypestateAttr>();
186f4a2713aSLionel Sambuc }
187f4a2713aSLionel Sambuc
isPointerOrRef(QualType ParamType)188*0a6a1f1dSLionel Sambuc static bool isPointerOrRef(QualType ParamType) {
189*0a6a1f1dSLionel Sambuc return ParamType->isPointerType() || ParamType->isReferenceType();
190f4a2713aSLionel Sambuc }
191f4a2713aSLionel Sambuc
mapConsumableAttrState(const QualType QT)192f4a2713aSLionel Sambuc static ConsumedState mapConsumableAttrState(const QualType QT) {
193f4a2713aSLionel Sambuc assert(isConsumableType(QT));
194f4a2713aSLionel Sambuc
195f4a2713aSLionel Sambuc const ConsumableAttr *CAttr =
196f4a2713aSLionel Sambuc QT->getAsCXXRecordDecl()->getAttr<ConsumableAttr>();
197f4a2713aSLionel Sambuc
198f4a2713aSLionel Sambuc switch (CAttr->getDefaultState()) {
199f4a2713aSLionel Sambuc case ConsumableAttr::Unknown:
200f4a2713aSLionel Sambuc return CS_Unknown;
201f4a2713aSLionel Sambuc case ConsumableAttr::Unconsumed:
202f4a2713aSLionel Sambuc return CS_Unconsumed;
203f4a2713aSLionel Sambuc case ConsumableAttr::Consumed:
204f4a2713aSLionel Sambuc return CS_Consumed;
205f4a2713aSLionel Sambuc }
206f4a2713aSLionel Sambuc llvm_unreachable("invalid enum");
207f4a2713aSLionel Sambuc }
208f4a2713aSLionel Sambuc
209f4a2713aSLionel Sambuc static ConsumedState
mapParamTypestateAttrState(const ParamTypestateAttr * PTAttr)210f4a2713aSLionel Sambuc mapParamTypestateAttrState(const ParamTypestateAttr *PTAttr) {
211f4a2713aSLionel Sambuc switch (PTAttr->getParamState()) {
212f4a2713aSLionel Sambuc case ParamTypestateAttr::Unknown:
213f4a2713aSLionel Sambuc return CS_Unknown;
214f4a2713aSLionel Sambuc case ParamTypestateAttr::Unconsumed:
215f4a2713aSLionel Sambuc return CS_Unconsumed;
216f4a2713aSLionel Sambuc case ParamTypestateAttr::Consumed:
217f4a2713aSLionel Sambuc return CS_Consumed;
218f4a2713aSLionel Sambuc }
219f4a2713aSLionel Sambuc llvm_unreachable("invalid_enum");
220f4a2713aSLionel Sambuc }
221f4a2713aSLionel Sambuc
222f4a2713aSLionel Sambuc static ConsumedState
mapReturnTypestateAttrState(const ReturnTypestateAttr * RTSAttr)223f4a2713aSLionel Sambuc mapReturnTypestateAttrState(const ReturnTypestateAttr *RTSAttr) {
224f4a2713aSLionel Sambuc switch (RTSAttr->getState()) {
225f4a2713aSLionel Sambuc case ReturnTypestateAttr::Unknown:
226f4a2713aSLionel Sambuc return CS_Unknown;
227f4a2713aSLionel Sambuc case ReturnTypestateAttr::Unconsumed:
228f4a2713aSLionel Sambuc return CS_Unconsumed;
229f4a2713aSLionel Sambuc case ReturnTypestateAttr::Consumed:
230f4a2713aSLionel Sambuc return CS_Consumed;
231f4a2713aSLionel Sambuc }
232f4a2713aSLionel Sambuc llvm_unreachable("invalid enum");
233f4a2713aSLionel Sambuc }
234f4a2713aSLionel Sambuc
mapSetTypestateAttrState(const SetTypestateAttr * STAttr)235f4a2713aSLionel Sambuc static ConsumedState mapSetTypestateAttrState(const SetTypestateAttr *STAttr) {
236f4a2713aSLionel Sambuc switch (STAttr->getNewState()) {
237f4a2713aSLionel Sambuc case SetTypestateAttr::Unknown:
238f4a2713aSLionel Sambuc return CS_Unknown;
239f4a2713aSLionel Sambuc case SetTypestateAttr::Unconsumed:
240f4a2713aSLionel Sambuc return CS_Unconsumed;
241f4a2713aSLionel Sambuc case SetTypestateAttr::Consumed:
242f4a2713aSLionel Sambuc return CS_Consumed;
243f4a2713aSLionel Sambuc }
244f4a2713aSLionel Sambuc llvm_unreachable("invalid_enum");
245f4a2713aSLionel Sambuc }
246f4a2713aSLionel Sambuc
stateToString(ConsumedState State)247f4a2713aSLionel Sambuc static StringRef stateToString(ConsumedState State) {
248f4a2713aSLionel Sambuc switch (State) {
249f4a2713aSLionel Sambuc case consumed::CS_None:
250f4a2713aSLionel Sambuc return "none";
251f4a2713aSLionel Sambuc
252f4a2713aSLionel Sambuc case consumed::CS_Unknown:
253f4a2713aSLionel Sambuc return "unknown";
254f4a2713aSLionel Sambuc
255f4a2713aSLionel Sambuc case consumed::CS_Unconsumed:
256f4a2713aSLionel Sambuc return "unconsumed";
257f4a2713aSLionel Sambuc
258f4a2713aSLionel Sambuc case consumed::CS_Consumed:
259f4a2713aSLionel Sambuc return "consumed";
260f4a2713aSLionel Sambuc }
261f4a2713aSLionel Sambuc llvm_unreachable("invalid enum");
262f4a2713aSLionel Sambuc }
263f4a2713aSLionel Sambuc
testsFor(const FunctionDecl * FunDecl)264f4a2713aSLionel Sambuc static ConsumedState testsFor(const FunctionDecl *FunDecl) {
265f4a2713aSLionel Sambuc assert(isTestingFunction(FunDecl));
266f4a2713aSLionel Sambuc switch (FunDecl->getAttr<TestTypestateAttr>()->getTestState()) {
267f4a2713aSLionel Sambuc case TestTypestateAttr::Unconsumed:
268f4a2713aSLionel Sambuc return CS_Unconsumed;
269f4a2713aSLionel Sambuc case TestTypestateAttr::Consumed:
270f4a2713aSLionel Sambuc return CS_Consumed;
271f4a2713aSLionel Sambuc }
272f4a2713aSLionel Sambuc llvm_unreachable("invalid enum");
273f4a2713aSLionel Sambuc }
274f4a2713aSLionel Sambuc
275f4a2713aSLionel Sambuc namespace {
276f4a2713aSLionel Sambuc struct VarTestResult {
277f4a2713aSLionel Sambuc const VarDecl *Var;
278f4a2713aSLionel Sambuc ConsumedState TestsFor;
279f4a2713aSLionel Sambuc };
280f4a2713aSLionel Sambuc } // end anonymous::VarTestResult
281f4a2713aSLionel Sambuc
282f4a2713aSLionel Sambuc namespace clang {
283f4a2713aSLionel Sambuc namespace consumed {
284f4a2713aSLionel Sambuc
285f4a2713aSLionel Sambuc enum EffectiveOp {
286f4a2713aSLionel Sambuc EO_And,
287f4a2713aSLionel Sambuc EO_Or
288f4a2713aSLionel Sambuc };
289f4a2713aSLionel Sambuc
290f4a2713aSLionel Sambuc class PropagationInfo {
291f4a2713aSLionel Sambuc enum {
292f4a2713aSLionel Sambuc IT_None,
293f4a2713aSLionel Sambuc IT_State,
294f4a2713aSLionel Sambuc IT_VarTest,
295f4a2713aSLionel Sambuc IT_BinTest,
296f4a2713aSLionel Sambuc IT_Var,
297f4a2713aSLionel Sambuc IT_Tmp
298f4a2713aSLionel Sambuc } InfoType;
299f4a2713aSLionel Sambuc
300f4a2713aSLionel Sambuc struct BinTestTy {
301f4a2713aSLionel Sambuc const BinaryOperator *Source;
302f4a2713aSLionel Sambuc EffectiveOp EOp;
303f4a2713aSLionel Sambuc VarTestResult LTest;
304f4a2713aSLionel Sambuc VarTestResult RTest;
305f4a2713aSLionel Sambuc };
306f4a2713aSLionel Sambuc
307f4a2713aSLionel Sambuc union {
308f4a2713aSLionel Sambuc ConsumedState State;
309f4a2713aSLionel Sambuc VarTestResult VarTest;
310f4a2713aSLionel Sambuc const VarDecl *Var;
311f4a2713aSLionel Sambuc const CXXBindTemporaryExpr *Tmp;
312f4a2713aSLionel Sambuc BinTestTy BinTest;
313f4a2713aSLionel Sambuc };
314f4a2713aSLionel Sambuc
315f4a2713aSLionel Sambuc public:
PropagationInfo()316f4a2713aSLionel Sambuc PropagationInfo() : InfoType(IT_None) {}
317f4a2713aSLionel Sambuc
PropagationInfo(const VarTestResult & VarTest)318f4a2713aSLionel Sambuc PropagationInfo(const VarTestResult &VarTest)
319f4a2713aSLionel Sambuc : InfoType(IT_VarTest), VarTest(VarTest) {}
320f4a2713aSLionel Sambuc
PropagationInfo(const VarDecl * Var,ConsumedState TestsFor)321f4a2713aSLionel Sambuc PropagationInfo(const VarDecl *Var, ConsumedState TestsFor)
322f4a2713aSLionel Sambuc : InfoType(IT_VarTest) {
323f4a2713aSLionel Sambuc
324f4a2713aSLionel Sambuc VarTest.Var = Var;
325f4a2713aSLionel Sambuc VarTest.TestsFor = TestsFor;
326f4a2713aSLionel Sambuc }
327f4a2713aSLionel Sambuc
PropagationInfo(const BinaryOperator * Source,EffectiveOp EOp,const VarTestResult & LTest,const VarTestResult & RTest)328f4a2713aSLionel Sambuc PropagationInfo(const BinaryOperator *Source, EffectiveOp EOp,
329f4a2713aSLionel Sambuc const VarTestResult <est, const VarTestResult &RTest)
330f4a2713aSLionel Sambuc : InfoType(IT_BinTest) {
331f4a2713aSLionel Sambuc
332f4a2713aSLionel Sambuc BinTest.Source = Source;
333f4a2713aSLionel Sambuc BinTest.EOp = EOp;
334f4a2713aSLionel Sambuc BinTest.LTest = LTest;
335f4a2713aSLionel Sambuc BinTest.RTest = RTest;
336f4a2713aSLionel Sambuc }
337f4a2713aSLionel Sambuc
PropagationInfo(const BinaryOperator * Source,EffectiveOp EOp,const VarDecl * LVar,ConsumedState LTestsFor,const VarDecl * RVar,ConsumedState RTestsFor)338f4a2713aSLionel Sambuc PropagationInfo(const BinaryOperator *Source, EffectiveOp EOp,
339f4a2713aSLionel Sambuc const VarDecl *LVar, ConsumedState LTestsFor,
340f4a2713aSLionel Sambuc const VarDecl *RVar, ConsumedState RTestsFor)
341f4a2713aSLionel Sambuc : InfoType(IT_BinTest) {
342f4a2713aSLionel Sambuc
343f4a2713aSLionel Sambuc BinTest.Source = Source;
344f4a2713aSLionel Sambuc BinTest.EOp = EOp;
345f4a2713aSLionel Sambuc BinTest.LTest.Var = LVar;
346f4a2713aSLionel Sambuc BinTest.LTest.TestsFor = LTestsFor;
347f4a2713aSLionel Sambuc BinTest.RTest.Var = RVar;
348f4a2713aSLionel Sambuc BinTest.RTest.TestsFor = RTestsFor;
349f4a2713aSLionel Sambuc }
350f4a2713aSLionel Sambuc
PropagationInfo(ConsumedState State)351f4a2713aSLionel Sambuc PropagationInfo(ConsumedState State)
352f4a2713aSLionel Sambuc : InfoType(IT_State), State(State) {}
353f4a2713aSLionel Sambuc
PropagationInfo(const VarDecl * Var)354f4a2713aSLionel Sambuc PropagationInfo(const VarDecl *Var) : InfoType(IT_Var), Var(Var) {}
PropagationInfo(const CXXBindTemporaryExpr * Tmp)355f4a2713aSLionel Sambuc PropagationInfo(const CXXBindTemporaryExpr *Tmp)
356f4a2713aSLionel Sambuc : InfoType(IT_Tmp), Tmp(Tmp) {}
357f4a2713aSLionel Sambuc
getState() const358f4a2713aSLionel Sambuc const ConsumedState & getState() const {
359f4a2713aSLionel Sambuc assert(InfoType == IT_State);
360f4a2713aSLionel Sambuc return State;
361f4a2713aSLionel Sambuc }
362f4a2713aSLionel Sambuc
getVarTest() const363f4a2713aSLionel Sambuc const VarTestResult & getVarTest() const {
364f4a2713aSLionel Sambuc assert(InfoType == IT_VarTest);
365f4a2713aSLionel Sambuc return VarTest;
366f4a2713aSLionel Sambuc }
367f4a2713aSLionel Sambuc
getLTest() const368f4a2713aSLionel Sambuc const VarTestResult & getLTest() const {
369f4a2713aSLionel Sambuc assert(InfoType == IT_BinTest);
370f4a2713aSLionel Sambuc return BinTest.LTest;
371f4a2713aSLionel Sambuc }
372f4a2713aSLionel Sambuc
getRTest() const373f4a2713aSLionel Sambuc const VarTestResult & getRTest() const {
374f4a2713aSLionel Sambuc assert(InfoType == IT_BinTest);
375f4a2713aSLionel Sambuc return BinTest.RTest;
376f4a2713aSLionel Sambuc }
377f4a2713aSLionel Sambuc
getVar() const378f4a2713aSLionel Sambuc const VarDecl * getVar() const {
379f4a2713aSLionel Sambuc assert(InfoType == IT_Var);
380f4a2713aSLionel Sambuc return Var;
381f4a2713aSLionel Sambuc }
382f4a2713aSLionel Sambuc
getTmp() const383f4a2713aSLionel Sambuc const CXXBindTemporaryExpr * getTmp() const {
384f4a2713aSLionel Sambuc assert(InfoType == IT_Tmp);
385f4a2713aSLionel Sambuc return Tmp;
386f4a2713aSLionel Sambuc }
387f4a2713aSLionel Sambuc
getAsState(const ConsumedStateMap * StateMap) const388f4a2713aSLionel Sambuc ConsumedState getAsState(const ConsumedStateMap *StateMap) const {
389f4a2713aSLionel Sambuc assert(isVar() || isTmp() || isState());
390f4a2713aSLionel Sambuc
391f4a2713aSLionel Sambuc if (isVar())
392f4a2713aSLionel Sambuc return StateMap->getState(Var);
393f4a2713aSLionel Sambuc else if (isTmp())
394f4a2713aSLionel Sambuc return StateMap->getState(Tmp);
395f4a2713aSLionel Sambuc else if (isState())
396f4a2713aSLionel Sambuc return State;
397f4a2713aSLionel Sambuc else
398f4a2713aSLionel Sambuc return CS_None;
399f4a2713aSLionel Sambuc }
400f4a2713aSLionel Sambuc
testEffectiveOp() const401f4a2713aSLionel Sambuc EffectiveOp testEffectiveOp() const {
402f4a2713aSLionel Sambuc assert(InfoType == IT_BinTest);
403f4a2713aSLionel Sambuc return BinTest.EOp;
404f4a2713aSLionel Sambuc }
405f4a2713aSLionel Sambuc
testSourceNode() const406f4a2713aSLionel Sambuc const BinaryOperator * testSourceNode() const {
407f4a2713aSLionel Sambuc assert(InfoType == IT_BinTest);
408f4a2713aSLionel Sambuc return BinTest.Source;
409f4a2713aSLionel Sambuc }
410f4a2713aSLionel Sambuc
isValid() const411f4a2713aSLionel Sambuc inline bool isValid() const { return InfoType != IT_None; }
isState() const412f4a2713aSLionel Sambuc inline bool isState() const { return InfoType == IT_State; }
isVarTest() const413f4a2713aSLionel Sambuc inline bool isVarTest() const { return InfoType == IT_VarTest; }
isBinTest() const414f4a2713aSLionel Sambuc inline bool isBinTest() const { return InfoType == IT_BinTest; }
isVar() const415f4a2713aSLionel Sambuc inline bool isVar() const { return InfoType == IT_Var; }
isTmp() const416f4a2713aSLionel Sambuc inline bool isTmp() const { return InfoType == IT_Tmp; }
417f4a2713aSLionel Sambuc
isTest() const418f4a2713aSLionel Sambuc bool isTest() const {
419f4a2713aSLionel Sambuc return InfoType == IT_VarTest || InfoType == IT_BinTest;
420f4a2713aSLionel Sambuc }
421f4a2713aSLionel Sambuc
isPointerToValue() const422f4a2713aSLionel Sambuc bool isPointerToValue() const {
423f4a2713aSLionel Sambuc return InfoType == IT_Var || InfoType == IT_Tmp;
424f4a2713aSLionel Sambuc }
425f4a2713aSLionel Sambuc
invertTest() const426f4a2713aSLionel Sambuc PropagationInfo invertTest() const {
427f4a2713aSLionel Sambuc assert(InfoType == IT_VarTest || InfoType == IT_BinTest);
428f4a2713aSLionel Sambuc
429f4a2713aSLionel Sambuc if (InfoType == IT_VarTest) {
430f4a2713aSLionel Sambuc return PropagationInfo(VarTest.Var,
431f4a2713aSLionel Sambuc invertConsumedUnconsumed(VarTest.TestsFor));
432f4a2713aSLionel Sambuc
433f4a2713aSLionel Sambuc } else if (InfoType == IT_BinTest) {
434f4a2713aSLionel Sambuc return PropagationInfo(BinTest.Source,
435f4a2713aSLionel Sambuc BinTest.EOp == EO_And ? EO_Or : EO_And,
436f4a2713aSLionel Sambuc BinTest.LTest.Var, invertConsumedUnconsumed(BinTest.LTest.TestsFor),
437f4a2713aSLionel Sambuc BinTest.RTest.Var, invertConsumedUnconsumed(BinTest.RTest.TestsFor));
438f4a2713aSLionel Sambuc } else {
439f4a2713aSLionel Sambuc return PropagationInfo();
440f4a2713aSLionel Sambuc }
441f4a2713aSLionel Sambuc }
442f4a2713aSLionel Sambuc };
443f4a2713aSLionel Sambuc
444f4a2713aSLionel Sambuc static inline void
setStateForVarOrTmp(ConsumedStateMap * StateMap,const PropagationInfo & PInfo,ConsumedState State)445f4a2713aSLionel Sambuc setStateForVarOrTmp(ConsumedStateMap *StateMap, const PropagationInfo &PInfo,
446f4a2713aSLionel Sambuc ConsumedState State) {
447f4a2713aSLionel Sambuc
448f4a2713aSLionel Sambuc assert(PInfo.isVar() || PInfo.isTmp());
449f4a2713aSLionel Sambuc
450f4a2713aSLionel Sambuc if (PInfo.isVar())
451f4a2713aSLionel Sambuc StateMap->setState(PInfo.getVar(), State);
452f4a2713aSLionel Sambuc else
453f4a2713aSLionel Sambuc StateMap->setState(PInfo.getTmp(), State);
454f4a2713aSLionel Sambuc }
455f4a2713aSLionel Sambuc
456f4a2713aSLionel Sambuc class ConsumedStmtVisitor : public ConstStmtVisitor<ConsumedStmtVisitor> {
457f4a2713aSLionel Sambuc
458f4a2713aSLionel Sambuc typedef llvm::DenseMap<const Stmt *, PropagationInfo> MapType;
459f4a2713aSLionel Sambuc typedef std::pair<const Stmt *, PropagationInfo> PairType;
460f4a2713aSLionel Sambuc typedef MapType::iterator InfoEntry;
461f4a2713aSLionel Sambuc typedef MapType::const_iterator ConstInfoEntry;
462f4a2713aSLionel Sambuc
463f4a2713aSLionel Sambuc AnalysisDeclContext &AC;
464f4a2713aSLionel Sambuc ConsumedAnalyzer &Analyzer;
465f4a2713aSLionel Sambuc ConsumedStateMap *StateMap;
466f4a2713aSLionel Sambuc MapType PropagationMap;
467*0a6a1f1dSLionel Sambuc
findInfo(const Expr * E)468*0a6a1f1dSLionel Sambuc InfoEntry findInfo(const Expr *E) {
469*0a6a1f1dSLionel Sambuc return PropagationMap.find(E->IgnoreParens());
470*0a6a1f1dSLionel Sambuc }
findInfo(const Expr * E) const471*0a6a1f1dSLionel Sambuc ConstInfoEntry findInfo(const Expr *E) const {
472*0a6a1f1dSLionel Sambuc return PropagationMap.find(E->IgnoreParens());
473*0a6a1f1dSLionel Sambuc }
insertInfo(const Expr * E,const PropagationInfo & PI)474*0a6a1f1dSLionel Sambuc void insertInfo(const Expr *E, const PropagationInfo &PI) {
475*0a6a1f1dSLionel Sambuc PropagationMap.insert(PairType(E->IgnoreParens(), PI));
476*0a6a1f1dSLionel Sambuc }
477*0a6a1f1dSLionel Sambuc
478*0a6a1f1dSLionel Sambuc void forwardInfo(const Expr *From, const Expr *To);
479*0a6a1f1dSLionel Sambuc void copyInfo(const Expr *From, const Expr *To, ConsumedState CS);
480*0a6a1f1dSLionel Sambuc ConsumedState getInfo(const Expr *From);
481*0a6a1f1dSLionel Sambuc void setInfo(const Expr *To, ConsumedState NS);
482*0a6a1f1dSLionel Sambuc void propagateReturnType(const Expr *Call, const FunctionDecl *Fun);
483f4a2713aSLionel Sambuc
484f4a2713aSLionel Sambuc public:
485f4a2713aSLionel Sambuc void checkCallability(const PropagationInfo &PInfo,
486f4a2713aSLionel Sambuc const FunctionDecl *FunDecl,
487f4a2713aSLionel Sambuc SourceLocation BlameLoc);
488*0a6a1f1dSLionel Sambuc bool handleCall(const CallExpr *Call, const Expr *ObjArg,
489*0a6a1f1dSLionel Sambuc const FunctionDecl *FunD);
490f4a2713aSLionel Sambuc
491f4a2713aSLionel Sambuc void VisitBinaryOperator(const BinaryOperator *BinOp);
492f4a2713aSLionel Sambuc void VisitCallExpr(const CallExpr *Call);
493f4a2713aSLionel Sambuc void VisitCastExpr(const CastExpr *Cast);
494f4a2713aSLionel Sambuc void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Temp);
495f4a2713aSLionel Sambuc void VisitCXXConstructExpr(const CXXConstructExpr *Call);
496f4a2713aSLionel Sambuc void VisitCXXMemberCallExpr(const CXXMemberCallExpr *Call);
497f4a2713aSLionel Sambuc void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *Call);
498f4a2713aSLionel Sambuc void VisitDeclRefExpr(const DeclRefExpr *DeclRef);
499f4a2713aSLionel Sambuc void VisitDeclStmt(const DeclStmt *DelcS);
500f4a2713aSLionel Sambuc void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Temp);
501f4a2713aSLionel Sambuc void VisitMemberExpr(const MemberExpr *MExpr);
502f4a2713aSLionel Sambuc void VisitParmVarDecl(const ParmVarDecl *Param);
503f4a2713aSLionel Sambuc void VisitReturnStmt(const ReturnStmt *Ret);
504f4a2713aSLionel Sambuc void VisitUnaryOperator(const UnaryOperator *UOp);
505f4a2713aSLionel Sambuc void VisitVarDecl(const VarDecl *Var);
506f4a2713aSLionel Sambuc
ConsumedStmtVisitor(AnalysisDeclContext & AC,ConsumedAnalyzer & Analyzer,ConsumedStateMap * StateMap)507f4a2713aSLionel Sambuc ConsumedStmtVisitor(AnalysisDeclContext &AC, ConsumedAnalyzer &Analyzer,
508f4a2713aSLionel Sambuc ConsumedStateMap *StateMap)
509f4a2713aSLionel Sambuc : AC(AC), Analyzer(Analyzer), StateMap(StateMap) {}
510f4a2713aSLionel Sambuc
getInfo(const Expr * StmtNode) const511*0a6a1f1dSLionel Sambuc PropagationInfo getInfo(const Expr *StmtNode) const {
512*0a6a1f1dSLionel Sambuc ConstInfoEntry Entry = findInfo(StmtNode);
513f4a2713aSLionel Sambuc
514f4a2713aSLionel Sambuc if (Entry != PropagationMap.end())
515f4a2713aSLionel Sambuc return Entry->second;
516f4a2713aSLionel Sambuc else
517f4a2713aSLionel Sambuc return PropagationInfo();
518f4a2713aSLionel Sambuc }
519f4a2713aSLionel Sambuc
reset(ConsumedStateMap * NewStateMap)520f4a2713aSLionel Sambuc void reset(ConsumedStateMap *NewStateMap) {
521f4a2713aSLionel Sambuc StateMap = NewStateMap;
522f4a2713aSLionel Sambuc }
523f4a2713aSLionel Sambuc };
524f4a2713aSLionel Sambuc
525*0a6a1f1dSLionel Sambuc
forwardInfo(const Expr * From,const Expr * To)526*0a6a1f1dSLionel Sambuc void ConsumedStmtVisitor::forwardInfo(const Expr *From, const Expr *To) {
527*0a6a1f1dSLionel Sambuc InfoEntry Entry = findInfo(From);
528*0a6a1f1dSLionel Sambuc if (Entry != PropagationMap.end())
529*0a6a1f1dSLionel Sambuc insertInfo(To, Entry->second);
530*0a6a1f1dSLionel Sambuc }
531*0a6a1f1dSLionel Sambuc
532*0a6a1f1dSLionel Sambuc
533*0a6a1f1dSLionel Sambuc // Create a new state for To, which is initialized to the state of From.
534*0a6a1f1dSLionel Sambuc // If NS is not CS_None, sets the state of From to NS.
copyInfo(const Expr * From,const Expr * To,ConsumedState NS)535*0a6a1f1dSLionel Sambuc void ConsumedStmtVisitor::copyInfo(const Expr *From, const Expr *To,
536*0a6a1f1dSLionel Sambuc ConsumedState NS) {
537*0a6a1f1dSLionel Sambuc InfoEntry Entry = findInfo(From);
538*0a6a1f1dSLionel Sambuc if (Entry != PropagationMap.end()) {
539*0a6a1f1dSLionel Sambuc PropagationInfo& PInfo = Entry->second;
540*0a6a1f1dSLionel Sambuc ConsumedState CS = PInfo.getAsState(StateMap);
541*0a6a1f1dSLionel Sambuc if (CS != CS_None)
542*0a6a1f1dSLionel Sambuc insertInfo(To, PropagationInfo(CS));
543*0a6a1f1dSLionel Sambuc if (NS != CS_None && PInfo.isPointerToValue())
544*0a6a1f1dSLionel Sambuc setStateForVarOrTmp(StateMap, PInfo, NS);
545*0a6a1f1dSLionel Sambuc }
546*0a6a1f1dSLionel Sambuc }
547*0a6a1f1dSLionel Sambuc
548*0a6a1f1dSLionel Sambuc
549*0a6a1f1dSLionel Sambuc // Get the ConsumedState for From
getInfo(const Expr * From)550*0a6a1f1dSLionel Sambuc ConsumedState ConsumedStmtVisitor::getInfo(const Expr *From) {
551*0a6a1f1dSLionel Sambuc InfoEntry Entry = findInfo(From);
552*0a6a1f1dSLionel Sambuc if (Entry != PropagationMap.end()) {
553*0a6a1f1dSLionel Sambuc PropagationInfo& PInfo = Entry->second;
554*0a6a1f1dSLionel Sambuc return PInfo.getAsState(StateMap);
555*0a6a1f1dSLionel Sambuc }
556*0a6a1f1dSLionel Sambuc return CS_None;
557*0a6a1f1dSLionel Sambuc }
558*0a6a1f1dSLionel Sambuc
559*0a6a1f1dSLionel Sambuc
560*0a6a1f1dSLionel Sambuc // If we already have info for To then update it, otherwise create a new entry.
setInfo(const Expr * To,ConsumedState NS)561*0a6a1f1dSLionel Sambuc void ConsumedStmtVisitor::setInfo(const Expr *To, ConsumedState NS) {
562*0a6a1f1dSLionel Sambuc InfoEntry Entry = findInfo(To);
563*0a6a1f1dSLionel Sambuc if (Entry != PropagationMap.end()) {
564*0a6a1f1dSLionel Sambuc PropagationInfo& PInfo = Entry->second;
565*0a6a1f1dSLionel Sambuc if (PInfo.isPointerToValue())
566*0a6a1f1dSLionel Sambuc setStateForVarOrTmp(StateMap, PInfo, NS);
567*0a6a1f1dSLionel Sambuc } else if (NS != CS_None) {
568*0a6a1f1dSLionel Sambuc insertInfo(To, PropagationInfo(NS));
569*0a6a1f1dSLionel Sambuc }
570*0a6a1f1dSLionel Sambuc }
571*0a6a1f1dSLionel Sambuc
572*0a6a1f1dSLionel Sambuc
573*0a6a1f1dSLionel Sambuc
checkCallability(const PropagationInfo & PInfo,const FunctionDecl * FunDecl,SourceLocation BlameLoc)574f4a2713aSLionel Sambuc void ConsumedStmtVisitor::checkCallability(const PropagationInfo &PInfo,
575f4a2713aSLionel Sambuc const FunctionDecl *FunDecl,
576f4a2713aSLionel Sambuc SourceLocation BlameLoc) {
577f4a2713aSLionel Sambuc assert(!PInfo.isTest());
578f4a2713aSLionel Sambuc
579f4a2713aSLionel Sambuc const CallableWhenAttr *CWAttr = FunDecl->getAttr<CallableWhenAttr>();
580*0a6a1f1dSLionel Sambuc if (!CWAttr)
581*0a6a1f1dSLionel Sambuc return;
582f4a2713aSLionel Sambuc
583f4a2713aSLionel Sambuc if (PInfo.isVar()) {
584f4a2713aSLionel Sambuc ConsumedState VarState = StateMap->getState(PInfo.getVar());
585f4a2713aSLionel Sambuc
586f4a2713aSLionel Sambuc if (VarState == CS_None || isCallableInState(CWAttr, VarState))
587f4a2713aSLionel Sambuc return;
588f4a2713aSLionel Sambuc
589f4a2713aSLionel Sambuc Analyzer.WarningsHandler.warnUseInInvalidState(
590f4a2713aSLionel Sambuc FunDecl->getNameAsString(), PInfo.getVar()->getNameAsString(),
591f4a2713aSLionel Sambuc stateToString(VarState), BlameLoc);
592f4a2713aSLionel Sambuc
593f4a2713aSLionel Sambuc } else {
594f4a2713aSLionel Sambuc ConsumedState TmpState = PInfo.getAsState(StateMap);
595f4a2713aSLionel Sambuc
596f4a2713aSLionel Sambuc if (TmpState == CS_None || isCallableInState(CWAttr, TmpState))
597f4a2713aSLionel Sambuc return;
598f4a2713aSLionel Sambuc
599f4a2713aSLionel Sambuc Analyzer.WarningsHandler.warnUseOfTempInInvalidState(
600f4a2713aSLionel Sambuc FunDecl->getNameAsString(), stateToString(TmpState), BlameLoc);
601f4a2713aSLionel Sambuc }
602f4a2713aSLionel Sambuc }
603f4a2713aSLionel Sambuc
604f4a2713aSLionel Sambuc
605*0a6a1f1dSLionel Sambuc // Factors out common behavior for function, method, and operator calls.
606*0a6a1f1dSLionel Sambuc // Check parameters and set parameter state if necessary.
607*0a6a1f1dSLionel Sambuc // Returns true if the state of ObjArg is set, or false otherwise.
handleCall(const CallExpr * Call,const Expr * ObjArg,const FunctionDecl * FunD)608*0a6a1f1dSLionel Sambuc bool ConsumedStmtVisitor::handleCall(const CallExpr *Call, const Expr *ObjArg,
609*0a6a1f1dSLionel Sambuc const FunctionDecl *FunD) {
610*0a6a1f1dSLionel Sambuc unsigned Offset = 0;
611*0a6a1f1dSLionel Sambuc if (isa<CXXOperatorCallExpr>(Call) && isa<CXXMethodDecl>(FunD))
612*0a6a1f1dSLionel Sambuc Offset = 1; // first argument is 'this'
613*0a6a1f1dSLionel Sambuc
614*0a6a1f1dSLionel Sambuc // check explicit parameters
615*0a6a1f1dSLionel Sambuc for (unsigned Index = Offset; Index < Call->getNumArgs(); ++Index) {
616*0a6a1f1dSLionel Sambuc // Skip variable argument lists.
617*0a6a1f1dSLionel Sambuc if (Index - Offset >= FunD->getNumParams())
618*0a6a1f1dSLionel Sambuc break;
619*0a6a1f1dSLionel Sambuc
620*0a6a1f1dSLionel Sambuc const ParmVarDecl *Param = FunD->getParamDecl(Index - Offset);
621*0a6a1f1dSLionel Sambuc QualType ParamType = Param->getType();
622*0a6a1f1dSLionel Sambuc
623*0a6a1f1dSLionel Sambuc InfoEntry Entry = findInfo(Call->getArg(Index));
624*0a6a1f1dSLionel Sambuc
625*0a6a1f1dSLionel Sambuc if (Entry == PropagationMap.end() || Entry->second.isTest())
626*0a6a1f1dSLionel Sambuc continue;
627*0a6a1f1dSLionel Sambuc PropagationInfo PInfo = Entry->second;
628*0a6a1f1dSLionel Sambuc
629*0a6a1f1dSLionel Sambuc // Check that the parameter is in the correct state.
630*0a6a1f1dSLionel Sambuc if (ParamTypestateAttr *PTA = Param->getAttr<ParamTypestateAttr>()) {
631*0a6a1f1dSLionel Sambuc ConsumedState ParamState = PInfo.getAsState(StateMap);
632*0a6a1f1dSLionel Sambuc ConsumedState ExpectedState = mapParamTypestateAttrState(PTA);
633*0a6a1f1dSLionel Sambuc
634*0a6a1f1dSLionel Sambuc if (ParamState != ExpectedState)
635*0a6a1f1dSLionel Sambuc Analyzer.WarningsHandler.warnParamTypestateMismatch(
636*0a6a1f1dSLionel Sambuc Call->getArg(Index)->getExprLoc(),
637*0a6a1f1dSLionel Sambuc stateToString(ExpectedState), stateToString(ParamState));
638f4a2713aSLionel Sambuc }
639f4a2713aSLionel Sambuc
640*0a6a1f1dSLionel Sambuc if (!(Entry->second.isVar() || Entry->second.isTmp()))
641*0a6a1f1dSLionel Sambuc continue;
642f4a2713aSLionel Sambuc
643*0a6a1f1dSLionel Sambuc // Adjust state on the caller side.
644*0a6a1f1dSLionel Sambuc if (isRValueRef(ParamType))
645*0a6a1f1dSLionel Sambuc setStateForVarOrTmp(StateMap, PInfo, consumed::CS_Consumed);
646*0a6a1f1dSLionel Sambuc else if (ReturnTypestateAttr *RT = Param->getAttr<ReturnTypestateAttr>())
647*0a6a1f1dSLionel Sambuc setStateForVarOrTmp(StateMap, PInfo, mapReturnTypestateAttrState(RT));
648*0a6a1f1dSLionel Sambuc else if (isPointerOrRef(ParamType) &&
649*0a6a1f1dSLionel Sambuc (!ParamType->getPointeeType().isConstQualified() ||
650*0a6a1f1dSLionel Sambuc isSetOnReadPtrType(ParamType)))
651*0a6a1f1dSLionel Sambuc setStateForVarOrTmp(StateMap, PInfo, consumed::CS_Unknown);
652f4a2713aSLionel Sambuc }
653f4a2713aSLionel Sambuc
654*0a6a1f1dSLionel Sambuc if (!ObjArg)
655*0a6a1f1dSLionel Sambuc return false;
656f4a2713aSLionel Sambuc
657*0a6a1f1dSLionel Sambuc // check implicit 'self' parameter, if present
658*0a6a1f1dSLionel Sambuc InfoEntry Entry = findInfo(ObjArg);
659*0a6a1f1dSLionel Sambuc if (Entry != PropagationMap.end()) {
660*0a6a1f1dSLionel Sambuc PropagationInfo PInfo = Entry->second;
661*0a6a1f1dSLionel Sambuc checkCallability(PInfo, FunD, Call->getExprLoc());
662*0a6a1f1dSLionel Sambuc
663*0a6a1f1dSLionel Sambuc if (SetTypestateAttr *STA = FunD->getAttr<SetTypestateAttr>()) {
664*0a6a1f1dSLionel Sambuc if (PInfo.isVar()) {
665*0a6a1f1dSLionel Sambuc StateMap->setState(PInfo.getVar(), mapSetTypestateAttrState(STA));
666*0a6a1f1dSLionel Sambuc return true;
667*0a6a1f1dSLionel Sambuc }
668*0a6a1f1dSLionel Sambuc else if (PInfo.isTmp()) {
669*0a6a1f1dSLionel Sambuc StateMap->setState(PInfo.getTmp(), mapSetTypestateAttrState(STA));
670*0a6a1f1dSLionel Sambuc return true;
671*0a6a1f1dSLionel Sambuc }
672*0a6a1f1dSLionel Sambuc }
673*0a6a1f1dSLionel Sambuc else if (isTestingFunction(FunD) && PInfo.isVar()) {
674*0a6a1f1dSLionel Sambuc PropagationMap.insert(PairType(Call,
675*0a6a1f1dSLionel Sambuc PropagationInfo(PInfo.getVar(), testsFor(FunD))));
676*0a6a1f1dSLionel Sambuc }
677*0a6a1f1dSLionel Sambuc }
678*0a6a1f1dSLionel Sambuc return false;
679*0a6a1f1dSLionel Sambuc }
680*0a6a1f1dSLionel Sambuc
681*0a6a1f1dSLionel Sambuc
propagateReturnType(const Expr * Call,const FunctionDecl * Fun)682*0a6a1f1dSLionel Sambuc void ConsumedStmtVisitor::propagateReturnType(const Expr *Call,
683*0a6a1f1dSLionel Sambuc const FunctionDecl *Fun) {
684*0a6a1f1dSLionel Sambuc QualType RetType = Fun->getCallResultType();
685*0a6a1f1dSLionel Sambuc if (RetType->isReferenceType())
686*0a6a1f1dSLionel Sambuc RetType = RetType->getPointeeType();
687*0a6a1f1dSLionel Sambuc
688*0a6a1f1dSLionel Sambuc if (isConsumableType(RetType)) {
689f4a2713aSLionel Sambuc ConsumedState ReturnState;
690*0a6a1f1dSLionel Sambuc if (ReturnTypestateAttr *RTA = Fun->getAttr<ReturnTypestateAttr>())
691*0a6a1f1dSLionel Sambuc ReturnState = mapReturnTypestateAttrState(RTA);
692f4a2713aSLionel Sambuc else
693*0a6a1f1dSLionel Sambuc ReturnState = mapConsumableAttrState(RetType);
694f4a2713aSLionel Sambuc
695f4a2713aSLionel Sambuc PropagationMap.insert(PairType(Call, PropagationInfo(ReturnState)));
696f4a2713aSLionel Sambuc }
697f4a2713aSLionel Sambuc }
698f4a2713aSLionel Sambuc
699*0a6a1f1dSLionel Sambuc
VisitBinaryOperator(const BinaryOperator * BinOp)700f4a2713aSLionel Sambuc void ConsumedStmtVisitor::VisitBinaryOperator(const BinaryOperator *BinOp) {
701f4a2713aSLionel Sambuc switch (BinOp->getOpcode()) {
702f4a2713aSLionel Sambuc case BO_LAnd:
703f4a2713aSLionel Sambuc case BO_LOr : {
704*0a6a1f1dSLionel Sambuc InfoEntry LEntry = findInfo(BinOp->getLHS()),
705*0a6a1f1dSLionel Sambuc REntry = findInfo(BinOp->getRHS());
706f4a2713aSLionel Sambuc
707f4a2713aSLionel Sambuc VarTestResult LTest, RTest;
708f4a2713aSLionel Sambuc
709f4a2713aSLionel Sambuc if (LEntry != PropagationMap.end() && LEntry->second.isVarTest()) {
710f4a2713aSLionel Sambuc LTest = LEntry->second.getVarTest();
711f4a2713aSLionel Sambuc
712f4a2713aSLionel Sambuc } else {
713*0a6a1f1dSLionel Sambuc LTest.Var = nullptr;
714f4a2713aSLionel Sambuc LTest.TestsFor = CS_None;
715f4a2713aSLionel Sambuc }
716f4a2713aSLionel Sambuc
717f4a2713aSLionel Sambuc if (REntry != PropagationMap.end() && REntry->second.isVarTest()) {
718f4a2713aSLionel Sambuc RTest = REntry->second.getVarTest();
719f4a2713aSLionel Sambuc
720f4a2713aSLionel Sambuc } else {
721*0a6a1f1dSLionel Sambuc RTest.Var = nullptr;
722f4a2713aSLionel Sambuc RTest.TestsFor = CS_None;
723f4a2713aSLionel Sambuc }
724f4a2713aSLionel Sambuc
725*0a6a1f1dSLionel Sambuc if (!(LTest.Var == nullptr && RTest.Var == nullptr))
726f4a2713aSLionel Sambuc PropagationMap.insert(PairType(BinOp, PropagationInfo(BinOp,
727f4a2713aSLionel Sambuc static_cast<EffectiveOp>(BinOp->getOpcode() == BO_LOr), LTest, RTest)));
728f4a2713aSLionel Sambuc
729f4a2713aSLionel Sambuc break;
730f4a2713aSLionel Sambuc }
731f4a2713aSLionel Sambuc
732f4a2713aSLionel Sambuc case BO_PtrMemD:
733f4a2713aSLionel Sambuc case BO_PtrMemI:
734f4a2713aSLionel Sambuc forwardInfo(BinOp->getLHS(), BinOp);
735f4a2713aSLionel Sambuc break;
736f4a2713aSLionel Sambuc
737f4a2713aSLionel Sambuc default:
738f4a2713aSLionel Sambuc break;
739f4a2713aSLionel Sambuc }
740f4a2713aSLionel Sambuc }
741f4a2713aSLionel Sambuc
VisitCallExpr(const CallExpr * Call)742f4a2713aSLionel Sambuc void ConsumedStmtVisitor::VisitCallExpr(const CallExpr *Call) {
743*0a6a1f1dSLionel Sambuc const FunctionDecl *FunDecl = Call->getDirectCallee();
744*0a6a1f1dSLionel Sambuc if (!FunDecl)
745*0a6a1f1dSLionel Sambuc return;
746f4a2713aSLionel Sambuc
747f4a2713aSLionel Sambuc // Special case for the std::move function.
748f4a2713aSLionel Sambuc // TODO: Make this more specific. (Deferred)
749*0a6a1f1dSLionel Sambuc if (Call->getNumArgs() == 1 && FunDecl->getNameAsString() == "move" &&
750*0a6a1f1dSLionel Sambuc FunDecl->isInStdNamespace()) {
751*0a6a1f1dSLionel Sambuc copyInfo(Call->getArg(0), Call, CS_Consumed);
752f4a2713aSLionel Sambuc return;
753f4a2713aSLionel Sambuc }
754f4a2713aSLionel Sambuc
755*0a6a1f1dSLionel Sambuc handleCall(Call, nullptr, FunDecl);
756*0a6a1f1dSLionel Sambuc propagateReturnType(Call, FunDecl);
757f4a2713aSLionel Sambuc }
758f4a2713aSLionel Sambuc
VisitCastExpr(const CastExpr * Cast)759f4a2713aSLionel Sambuc void ConsumedStmtVisitor::VisitCastExpr(const CastExpr *Cast) {
760f4a2713aSLionel Sambuc forwardInfo(Cast->getSubExpr(), Cast);
761f4a2713aSLionel Sambuc }
762f4a2713aSLionel Sambuc
VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr * Temp)763f4a2713aSLionel Sambuc void ConsumedStmtVisitor::VisitCXXBindTemporaryExpr(
764f4a2713aSLionel Sambuc const CXXBindTemporaryExpr *Temp) {
765f4a2713aSLionel Sambuc
766*0a6a1f1dSLionel Sambuc InfoEntry Entry = findInfo(Temp->getSubExpr());
767f4a2713aSLionel Sambuc
768f4a2713aSLionel Sambuc if (Entry != PropagationMap.end() && !Entry->second.isTest()) {
769f4a2713aSLionel Sambuc StateMap->setState(Temp, Entry->second.getAsState(StateMap));
770f4a2713aSLionel Sambuc PropagationMap.insert(PairType(Temp, PropagationInfo(Temp)));
771f4a2713aSLionel Sambuc }
772f4a2713aSLionel Sambuc }
773f4a2713aSLionel Sambuc
VisitCXXConstructExpr(const CXXConstructExpr * Call)774f4a2713aSLionel Sambuc void ConsumedStmtVisitor::VisitCXXConstructExpr(const CXXConstructExpr *Call) {
775f4a2713aSLionel Sambuc CXXConstructorDecl *Constructor = Call->getConstructor();
776f4a2713aSLionel Sambuc
777f4a2713aSLionel Sambuc ASTContext &CurrContext = AC.getASTContext();
778f4a2713aSLionel Sambuc QualType ThisType = Constructor->getThisType(CurrContext)->getPointeeType();
779f4a2713aSLionel Sambuc
780f4a2713aSLionel Sambuc if (!isConsumableType(ThisType))
781f4a2713aSLionel Sambuc return;
782f4a2713aSLionel Sambuc
783f4a2713aSLionel Sambuc // FIXME: What should happen if someone annotates the move constructor?
784*0a6a1f1dSLionel Sambuc if (ReturnTypestateAttr *RTA = Constructor->getAttr<ReturnTypestateAttr>()) {
785f4a2713aSLionel Sambuc // TODO: Adjust state of args appropriately.
786*0a6a1f1dSLionel Sambuc ConsumedState RetState = mapReturnTypestateAttrState(RTA);
787f4a2713aSLionel Sambuc PropagationMap.insert(PairType(Call, PropagationInfo(RetState)));
788f4a2713aSLionel Sambuc } else if (Constructor->isDefaultConstructor()) {
789f4a2713aSLionel Sambuc PropagationMap.insert(PairType(Call,
790f4a2713aSLionel Sambuc PropagationInfo(consumed::CS_Consumed)));
791f4a2713aSLionel Sambuc } else if (Constructor->isMoveConstructor()) {
792*0a6a1f1dSLionel Sambuc copyInfo(Call->getArg(0), Call, CS_Consumed);
793f4a2713aSLionel Sambuc } else if (Constructor->isCopyConstructor()) {
794*0a6a1f1dSLionel Sambuc // Copy state from arg. If setStateOnRead then set arg to CS_Unknown.
795*0a6a1f1dSLionel Sambuc ConsumedState NS =
796*0a6a1f1dSLionel Sambuc isSetOnReadPtrType(Constructor->getThisType(CurrContext)) ?
797*0a6a1f1dSLionel Sambuc CS_Unknown : CS_None;
798*0a6a1f1dSLionel Sambuc copyInfo(Call->getArg(0), Call, NS);
799f4a2713aSLionel Sambuc } else {
800f4a2713aSLionel Sambuc // TODO: Adjust state of args appropriately.
801f4a2713aSLionel Sambuc ConsumedState RetState = mapConsumableAttrState(ThisType);
802f4a2713aSLionel Sambuc PropagationMap.insert(PairType(Call, PropagationInfo(RetState)));
803f4a2713aSLionel Sambuc }
804f4a2713aSLionel Sambuc }
805f4a2713aSLionel Sambuc
806*0a6a1f1dSLionel Sambuc
VisitCXXMemberCallExpr(const CXXMemberCallExpr * Call)807f4a2713aSLionel Sambuc void ConsumedStmtVisitor::VisitCXXMemberCallExpr(
808f4a2713aSLionel Sambuc const CXXMemberCallExpr *Call) {
809*0a6a1f1dSLionel Sambuc CXXMethodDecl* MD = Call->getMethodDecl();
810*0a6a1f1dSLionel Sambuc if (!MD)
811*0a6a1f1dSLionel Sambuc return;
812f4a2713aSLionel Sambuc
813*0a6a1f1dSLionel Sambuc handleCall(Call, Call->getImplicitObjectArgument(), MD);
814*0a6a1f1dSLionel Sambuc propagateReturnType(Call, MD);
815f4a2713aSLionel Sambuc }
816*0a6a1f1dSLionel Sambuc
817f4a2713aSLionel Sambuc
VisitCXXOperatorCallExpr(const CXXOperatorCallExpr * Call)818f4a2713aSLionel Sambuc void ConsumedStmtVisitor::VisitCXXOperatorCallExpr(
819f4a2713aSLionel Sambuc const CXXOperatorCallExpr *Call) {
820f4a2713aSLionel Sambuc
821f4a2713aSLionel Sambuc const FunctionDecl *FunDecl =
822f4a2713aSLionel Sambuc dyn_cast_or_null<FunctionDecl>(Call->getDirectCallee());
823f4a2713aSLionel Sambuc if (!FunDecl) return;
824f4a2713aSLionel Sambuc
825*0a6a1f1dSLionel Sambuc if (Call->getOperator() == OO_Equal) {
826*0a6a1f1dSLionel Sambuc ConsumedState CS = getInfo(Call->getArg(1));
827*0a6a1f1dSLionel Sambuc if (!handleCall(Call, Call->getArg(0), FunDecl))
828*0a6a1f1dSLionel Sambuc setInfo(Call->getArg(0), CS);
829*0a6a1f1dSLionel Sambuc return;
830f4a2713aSLionel Sambuc }
831f4a2713aSLionel Sambuc
832*0a6a1f1dSLionel Sambuc if (const CXXMemberCallExpr *MCall = dyn_cast<CXXMemberCallExpr>(Call))
833*0a6a1f1dSLionel Sambuc handleCall(MCall, MCall->getImplicitObjectArgument(), FunDecl);
834*0a6a1f1dSLionel Sambuc else
835*0a6a1f1dSLionel Sambuc handleCall(Call, Call->getArg(0), FunDecl);
836f4a2713aSLionel Sambuc
837*0a6a1f1dSLionel Sambuc propagateReturnType(Call, FunDecl);
838f4a2713aSLionel Sambuc }
839f4a2713aSLionel Sambuc
VisitDeclRefExpr(const DeclRefExpr * DeclRef)840f4a2713aSLionel Sambuc void ConsumedStmtVisitor::VisitDeclRefExpr(const DeclRefExpr *DeclRef) {
841f4a2713aSLionel Sambuc if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(DeclRef->getDecl()))
842f4a2713aSLionel Sambuc if (StateMap->getState(Var) != consumed::CS_None)
843f4a2713aSLionel Sambuc PropagationMap.insert(PairType(DeclRef, PropagationInfo(Var)));
844f4a2713aSLionel Sambuc }
845f4a2713aSLionel Sambuc
VisitDeclStmt(const DeclStmt * DeclS)846f4a2713aSLionel Sambuc void ConsumedStmtVisitor::VisitDeclStmt(const DeclStmt *DeclS) {
847*0a6a1f1dSLionel Sambuc for (const auto *DI : DeclS->decls())
848*0a6a1f1dSLionel Sambuc if (isa<VarDecl>(DI))
849*0a6a1f1dSLionel Sambuc VisitVarDecl(cast<VarDecl>(DI));
850f4a2713aSLionel Sambuc
851f4a2713aSLionel Sambuc if (DeclS->isSingleDecl())
852f4a2713aSLionel Sambuc if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(DeclS->getSingleDecl()))
853f4a2713aSLionel Sambuc PropagationMap.insert(PairType(DeclS, PropagationInfo(Var)));
854f4a2713aSLionel Sambuc }
855f4a2713aSLionel Sambuc
VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr * Temp)856f4a2713aSLionel Sambuc void ConsumedStmtVisitor::VisitMaterializeTemporaryExpr(
857f4a2713aSLionel Sambuc const MaterializeTemporaryExpr *Temp) {
858f4a2713aSLionel Sambuc
859f4a2713aSLionel Sambuc forwardInfo(Temp->GetTemporaryExpr(), Temp);
860f4a2713aSLionel Sambuc }
861f4a2713aSLionel Sambuc
VisitMemberExpr(const MemberExpr * MExpr)862f4a2713aSLionel Sambuc void ConsumedStmtVisitor::VisitMemberExpr(const MemberExpr *MExpr) {
863f4a2713aSLionel Sambuc forwardInfo(MExpr->getBase(), MExpr);
864f4a2713aSLionel Sambuc }
865f4a2713aSLionel Sambuc
866f4a2713aSLionel Sambuc
VisitParmVarDecl(const ParmVarDecl * Param)867f4a2713aSLionel Sambuc void ConsumedStmtVisitor::VisitParmVarDecl(const ParmVarDecl *Param) {
868f4a2713aSLionel Sambuc QualType ParamType = Param->getType();
869f4a2713aSLionel Sambuc ConsumedState ParamState = consumed::CS_None;
870f4a2713aSLionel Sambuc
871*0a6a1f1dSLionel Sambuc if (const ParamTypestateAttr *PTA = Param->getAttr<ParamTypestateAttr>())
872*0a6a1f1dSLionel Sambuc ParamState = mapParamTypestateAttrState(PTA);
873*0a6a1f1dSLionel Sambuc else if (isConsumableType(ParamType))
874f4a2713aSLionel Sambuc ParamState = mapConsumableAttrState(ParamType);
875*0a6a1f1dSLionel Sambuc else if (isRValueRef(ParamType) &&
876*0a6a1f1dSLionel Sambuc isConsumableType(ParamType->getPointeeType()))
877f4a2713aSLionel Sambuc ParamState = mapConsumableAttrState(ParamType->getPointeeType());
878*0a6a1f1dSLionel Sambuc else if (ParamType->isReferenceType() &&
879*0a6a1f1dSLionel Sambuc isConsumableType(ParamType->getPointeeType()))
880f4a2713aSLionel Sambuc ParamState = consumed::CS_Unknown;
881f4a2713aSLionel Sambuc
882f4a2713aSLionel Sambuc if (ParamState != CS_None)
883f4a2713aSLionel Sambuc StateMap->setState(Param, ParamState);
884f4a2713aSLionel Sambuc }
885f4a2713aSLionel Sambuc
VisitReturnStmt(const ReturnStmt * Ret)886f4a2713aSLionel Sambuc void ConsumedStmtVisitor::VisitReturnStmt(const ReturnStmt *Ret) {
887f4a2713aSLionel Sambuc ConsumedState ExpectedState = Analyzer.getExpectedReturnState();
888f4a2713aSLionel Sambuc
889f4a2713aSLionel Sambuc if (ExpectedState != CS_None) {
890*0a6a1f1dSLionel Sambuc InfoEntry Entry = findInfo(Ret->getRetValue());
891f4a2713aSLionel Sambuc
892f4a2713aSLionel Sambuc if (Entry != PropagationMap.end()) {
893f4a2713aSLionel Sambuc ConsumedState RetState = Entry->second.getAsState(StateMap);
894f4a2713aSLionel Sambuc
895f4a2713aSLionel Sambuc if (RetState != ExpectedState)
896f4a2713aSLionel Sambuc Analyzer.WarningsHandler.warnReturnTypestateMismatch(
897f4a2713aSLionel Sambuc Ret->getReturnLoc(), stateToString(ExpectedState),
898f4a2713aSLionel Sambuc stateToString(RetState));
899f4a2713aSLionel Sambuc }
900f4a2713aSLionel Sambuc }
901f4a2713aSLionel Sambuc
902f4a2713aSLionel Sambuc StateMap->checkParamsForReturnTypestate(Ret->getLocStart(),
903f4a2713aSLionel Sambuc Analyzer.WarningsHandler);
904f4a2713aSLionel Sambuc }
905f4a2713aSLionel Sambuc
VisitUnaryOperator(const UnaryOperator * UOp)906f4a2713aSLionel Sambuc void ConsumedStmtVisitor::VisitUnaryOperator(const UnaryOperator *UOp) {
907*0a6a1f1dSLionel Sambuc InfoEntry Entry = findInfo(UOp->getSubExpr());
908f4a2713aSLionel Sambuc if (Entry == PropagationMap.end()) return;
909f4a2713aSLionel Sambuc
910f4a2713aSLionel Sambuc switch (UOp->getOpcode()) {
911f4a2713aSLionel Sambuc case UO_AddrOf:
912f4a2713aSLionel Sambuc PropagationMap.insert(PairType(UOp, Entry->second));
913f4a2713aSLionel Sambuc break;
914f4a2713aSLionel Sambuc
915f4a2713aSLionel Sambuc case UO_LNot:
916f4a2713aSLionel Sambuc if (Entry->second.isTest())
917f4a2713aSLionel Sambuc PropagationMap.insert(PairType(UOp, Entry->second.invertTest()));
918f4a2713aSLionel Sambuc break;
919f4a2713aSLionel Sambuc
920f4a2713aSLionel Sambuc default:
921f4a2713aSLionel Sambuc break;
922f4a2713aSLionel Sambuc }
923f4a2713aSLionel Sambuc }
924f4a2713aSLionel Sambuc
925f4a2713aSLionel Sambuc // TODO: See if I need to check for reference types here.
VisitVarDecl(const VarDecl * Var)926f4a2713aSLionel Sambuc void ConsumedStmtVisitor::VisitVarDecl(const VarDecl *Var) {
927f4a2713aSLionel Sambuc if (isConsumableType(Var->getType())) {
928f4a2713aSLionel Sambuc if (Var->hasInit()) {
929*0a6a1f1dSLionel Sambuc MapType::iterator VIT = findInfo(Var->getInit()->IgnoreImplicit());
930f4a2713aSLionel Sambuc if (VIT != PropagationMap.end()) {
931f4a2713aSLionel Sambuc PropagationInfo PInfo = VIT->second;
932f4a2713aSLionel Sambuc ConsumedState St = PInfo.getAsState(StateMap);
933f4a2713aSLionel Sambuc
934f4a2713aSLionel Sambuc if (St != consumed::CS_None) {
935f4a2713aSLionel Sambuc StateMap->setState(Var, St);
936f4a2713aSLionel Sambuc return;
937f4a2713aSLionel Sambuc }
938f4a2713aSLionel Sambuc }
939f4a2713aSLionel Sambuc }
940f4a2713aSLionel Sambuc // Otherwise
941f4a2713aSLionel Sambuc StateMap->setState(Var, consumed::CS_Unknown);
942f4a2713aSLionel Sambuc }
943f4a2713aSLionel Sambuc }
944f4a2713aSLionel Sambuc }} // end clang::consumed::ConsumedStmtVisitor
945f4a2713aSLionel Sambuc
946f4a2713aSLionel Sambuc namespace clang {
947f4a2713aSLionel Sambuc namespace consumed {
948f4a2713aSLionel Sambuc
splitVarStateForIf(const IfStmt * IfNode,const VarTestResult & Test,ConsumedStateMap * ThenStates,ConsumedStateMap * ElseStates)949f4a2713aSLionel Sambuc void splitVarStateForIf(const IfStmt * IfNode, const VarTestResult &Test,
950f4a2713aSLionel Sambuc ConsumedStateMap *ThenStates,
951f4a2713aSLionel Sambuc ConsumedStateMap *ElseStates) {
952f4a2713aSLionel Sambuc
953f4a2713aSLionel Sambuc ConsumedState VarState = ThenStates->getState(Test.Var);
954f4a2713aSLionel Sambuc
955f4a2713aSLionel Sambuc if (VarState == CS_Unknown) {
956f4a2713aSLionel Sambuc ThenStates->setState(Test.Var, Test.TestsFor);
957f4a2713aSLionel Sambuc ElseStates->setState(Test.Var, invertConsumedUnconsumed(Test.TestsFor));
958f4a2713aSLionel Sambuc
959f4a2713aSLionel Sambuc } else if (VarState == invertConsumedUnconsumed(Test.TestsFor)) {
960f4a2713aSLionel Sambuc ThenStates->markUnreachable();
961f4a2713aSLionel Sambuc
962f4a2713aSLionel Sambuc } else if (VarState == Test.TestsFor) {
963f4a2713aSLionel Sambuc ElseStates->markUnreachable();
964f4a2713aSLionel Sambuc }
965f4a2713aSLionel Sambuc }
966f4a2713aSLionel Sambuc
splitVarStateForIfBinOp(const PropagationInfo & PInfo,ConsumedStateMap * ThenStates,ConsumedStateMap * ElseStates)967f4a2713aSLionel Sambuc void splitVarStateForIfBinOp(const PropagationInfo &PInfo,
968f4a2713aSLionel Sambuc ConsumedStateMap *ThenStates, ConsumedStateMap *ElseStates) {
969f4a2713aSLionel Sambuc
970f4a2713aSLionel Sambuc const VarTestResult <est = PInfo.getLTest(),
971f4a2713aSLionel Sambuc &RTest = PInfo.getRTest();
972f4a2713aSLionel Sambuc
973f4a2713aSLionel Sambuc ConsumedState LState = LTest.Var ? ThenStates->getState(LTest.Var) : CS_None,
974f4a2713aSLionel Sambuc RState = RTest.Var ? ThenStates->getState(RTest.Var) : CS_None;
975f4a2713aSLionel Sambuc
976f4a2713aSLionel Sambuc if (LTest.Var) {
977f4a2713aSLionel Sambuc if (PInfo.testEffectiveOp() == EO_And) {
978f4a2713aSLionel Sambuc if (LState == CS_Unknown) {
979f4a2713aSLionel Sambuc ThenStates->setState(LTest.Var, LTest.TestsFor);
980f4a2713aSLionel Sambuc
981f4a2713aSLionel Sambuc } else if (LState == invertConsumedUnconsumed(LTest.TestsFor)) {
982f4a2713aSLionel Sambuc ThenStates->markUnreachable();
983f4a2713aSLionel Sambuc
984f4a2713aSLionel Sambuc } else if (LState == LTest.TestsFor && isKnownState(RState)) {
985f4a2713aSLionel Sambuc if (RState == RTest.TestsFor)
986f4a2713aSLionel Sambuc ElseStates->markUnreachable();
987f4a2713aSLionel Sambuc else
988f4a2713aSLionel Sambuc ThenStates->markUnreachable();
989f4a2713aSLionel Sambuc }
990f4a2713aSLionel Sambuc
991f4a2713aSLionel Sambuc } else {
992f4a2713aSLionel Sambuc if (LState == CS_Unknown) {
993f4a2713aSLionel Sambuc ElseStates->setState(LTest.Var,
994f4a2713aSLionel Sambuc invertConsumedUnconsumed(LTest.TestsFor));
995f4a2713aSLionel Sambuc
996f4a2713aSLionel Sambuc } else if (LState == LTest.TestsFor) {
997f4a2713aSLionel Sambuc ElseStates->markUnreachable();
998f4a2713aSLionel Sambuc
999f4a2713aSLionel Sambuc } else if (LState == invertConsumedUnconsumed(LTest.TestsFor) &&
1000f4a2713aSLionel Sambuc isKnownState(RState)) {
1001f4a2713aSLionel Sambuc
1002f4a2713aSLionel Sambuc if (RState == RTest.TestsFor)
1003f4a2713aSLionel Sambuc ElseStates->markUnreachable();
1004f4a2713aSLionel Sambuc else
1005f4a2713aSLionel Sambuc ThenStates->markUnreachable();
1006f4a2713aSLionel Sambuc }
1007f4a2713aSLionel Sambuc }
1008f4a2713aSLionel Sambuc }
1009f4a2713aSLionel Sambuc
1010f4a2713aSLionel Sambuc if (RTest.Var) {
1011f4a2713aSLionel Sambuc if (PInfo.testEffectiveOp() == EO_And) {
1012f4a2713aSLionel Sambuc if (RState == CS_Unknown)
1013f4a2713aSLionel Sambuc ThenStates->setState(RTest.Var, RTest.TestsFor);
1014f4a2713aSLionel Sambuc else if (RState == invertConsumedUnconsumed(RTest.TestsFor))
1015f4a2713aSLionel Sambuc ThenStates->markUnreachable();
1016f4a2713aSLionel Sambuc
1017f4a2713aSLionel Sambuc } else {
1018f4a2713aSLionel Sambuc if (RState == CS_Unknown)
1019f4a2713aSLionel Sambuc ElseStates->setState(RTest.Var,
1020f4a2713aSLionel Sambuc invertConsumedUnconsumed(RTest.TestsFor));
1021f4a2713aSLionel Sambuc else if (RState == RTest.TestsFor)
1022f4a2713aSLionel Sambuc ElseStates->markUnreachable();
1023f4a2713aSLionel Sambuc }
1024f4a2713aSLionel Sambuc }
1025f4a2713aSLionel Sambuc }
1026f4a2713aSLionel Sambuc
allBackEdgesVisited(const CFGBlock * CurrBlock,const CFGBlock * TargetBlock)1027f4a2713aSLionel Sambuc bool ConsumedBlockInfo::allBackEdgesVisited(const CFGBlock *CurrBlock,
1028f4a2713aSLionel Sambuc const CFGBlock *TargetBlock) {
1029f4a2713aSLionel Sambuc
1030f4a2713aSLionel Sambuc assert(CurrBlock && "Block pointer must not be NULL");
1031f4a2713aSLionel Sambuc assert(TargetBlock && "TargetBlock pointer must not be NULL");
1032f4a2713aSLionel Sambuc
1033f4a2713aSLionel Sambuc unsigned int CurrBlockOrder = VisitOrder[CurrBlock->getBlockID()];
1034f4a2713aSLionel Sambuc for (CFGBlock::const_pred_iterator PI = TargetBlock->pred_begin(),
1035f4a2713aSLionel Sambuc PE = TargetBlock->pred_end(); PI != PE; ++PI) {
1036f4a2713aSLionel Sambuc if (*PI && CurrBlockOrder < VisitOrder[(*PI)->getBlockID()] )
1037f4a2713aSLionel Sambuc return false;
1038f4a2713aSLionel Sambuc }
1039f4a2713aSLionel Sambuc return true;
1040f4a2713aSLionel Sambuc }
1041f4a2713aSLionel Sambuc
addInfo(const CFGBlock * Block,ConsumedStateMap * StateMap,bool & AlreadyOwned)1042f4a2713aSLionel Sambuc void ConsumedBlockInfo::addInfo(const CFGBlock *Block,
1043f4a2713aSLionel Sambuc ConsumedStateMap *StateMap,
1044f4a2713aSLionel Sambuc bool &AlreadyOwned) {
1045f4a2713aSLionel Sambuc
1046f4a2713aSLionel Sambuc assert(Block && "Block pointer must not be NULL");
1047f4a2713aSLionel Sambuc
1048f4a2713aSLionel Sambuc ConsumedStateMap *Entry = StateMapsArray[Block->getBlockID()];
1049f4a2713aSLionel Sambuc
1050f4a2713aSLionel Sambuc if (Entry) {
1051f4a2713aSLionel Sambuc Entry->intersect(StateMap);
1052f4a2713aSLionel Sambuc
1053f4a2713aSLionel Sambuc } else if (AlreadyOwned) {
1054f4a2713aSLionel Sambuc StateMapsArray[Block->getBlockID()] = new ConsumedStateMap(*StateMap);
1055f4a2713aSLionel Sambuc
1056f4a2713aSLionel Sambuc } else {
1057f4a2713aSLionel Sambuc StateMapsArray[Block->getBlockID()] = StateMap;
1058f4a2713aSLionel Sambuc AlreadyOwned = true;
1059f4a2713aSLionel Sambuc }
1060f4a2713aSLionel Sambuc }
1061f4a2713aSLionel Sambuc
addInfo(const CFGBlock * Block,ConsumedStateMap * StateMap)1062f4a2713aSLionel Sambuc void ConsumedBlockInfo::addInfo(const CFGBlock *Block,
1063f4a2713aSLionel Sambuc ConsumedStateMap *StateMap) {
1064f4a2713aSLionel Sambuc
1065*0a6a1f1dSLionel Sambuc assert(Block && "Block pointer must not be NULL");
1066f4a2713aSLionel Sambuc
1067f4a2713aSLionel Sambuc ConsumedStateMap *Entry = StateMapsArray[Block->getBlockID()];
1068f4a2713aSLionel Sambuc
1069f4a2713aSLionel Sambuc if (Entry) {
1070f4a2713aSLionel Sambuc Entry->intersect(StateMap);
1071f4a2713aSLionel Sambuc delete StateMap;
1072f4a2713aSLionel Sambuc
1073f4a2713aSLionel Sambuc } else {
1074f4a2713aSLionel Sambuc StateMapsArray[Block->getBlockID()] = StateMap;
1075f4a2713aSLionel Sambuc }
1076f4a2713aSLionel Sambuc }
1077f4a2713aSLionel Sambuc
borrowInfo(const CFGBlock * Block)1078f4a2713aSLionel Sambuc ConsumedStateMap* ConsumedBlockInfo::borrowInfo(const CFGBlock *Block) {
1079f4a2713aSLionel Sambuc assert(Block && "Block pointer must not be NULL");
1080f4a2713aSLionel Sambuc assert(StateMapsArray[Block->getBlockID()] && "Block has no block info");
1081f4a2713aSLionel Sambuc
1082f4a2713aSLionel Sambuc return StateMapsArray[Block->getBlockID()];
1083f4a2713aSLionel Sambuc }
1084f4a2713aSLionel Sambuc
discardInfo(const CFGBlock * Block)1085f4a2713aSLionel Sambuc void ConsumedBlockInfo::discardInfo(const CFGBlock *Block) {
1086f4a2713aSLionel Sambuc unsigned int BlockID = Block->getBlockID();
1087f4a2713aSLionel Sambuc delete StateMapsArray[BlockID];
1088*0a6a1f1dSLionel Sambuc StateMapsArray[BlockID] = nullptr;
1089f4a2713aSLionel Sambuc }
1090f4a2713aSLionel Sambuc
getInfo(const CFGBlock * Block)1091f4a2713aSLionel Sambuc ConsumedStateMap* ConsumedBlockInfo::getInfo(const CFGBlock *Block) {
1092f4a2713aSLionel Sambuc assert(Block && "Block pointer must not be NULL");
1093f4a2713aSLionel Sambuc
1094f4a2713aSLionel Sambuc ConsumedStateMap *StateMap = StateMapsArray[Block->getBlockID()];
1095f4a2713aSLionel Sambuc if (isBackEdgeTarget(Block)) {
1096f4a2713aSLionel Sambuc return new ConsumedStateMap(*StateMap);
1097f4a2713aSLionel Sambuc } else {
1098*0a6a1f1dSLionel Sambuc StateMapsArray[Block->getBlockID()] = nullptr;
1099f4a2713aSLionel Sambuc return StateMap;
1100f4a2713aSLionel Sambuc }
1101f4a2713aSLionel Sambuc }
1102f4a2713aSLionel Sambuc
isBackEdge(const CFGBlock * From,const CFGBlock * To)1103f4a2713aSLionel Sambuc bool ConsumedBlockInfo::isBackEdge(const CFGBlock *From, const CFGBlock *To) {
1104f4a2713aSLionel Sambuc assert(From && "From block must not be NULL");
1105f4a2713aSLionel Sambuc assert(To && "From block must not be NULL");
1106f4a2713aSLionel Sambuc
1107f4a2713aSLionel Sambuc return VisitOrder[From->getBlockID()] > VisitOrder[To->getBlockID()];
1108f4a2713aSLionel Sambuc }
1109f4a2713aSLionel Sambuc
isBackEdgeTarget(const CFGBlock * Block)1110f4a2713aSLionel Sambuc bool ConsumedBlockInfo::isBackEdgeTarget(const CFGBlock *Block) {
1111*0a6a1f1dSLionel Sambuc assert(Block && "Block pointer must not be NULL");
1112f4a2713aSLionel Sambuc
1113f4a2713aSLionel Sambuc // Anything with less than two predecessors can't be the target of a back
1114f4a2713aSLionel Sambuc // edge.
1115f4a2713aSLionel Sambuc if (Block->pred_size() < 2)
1116f4a2713aSLionel Sambuc return false;
1117f4a2713aSLionel Sambuc
1118f4a2713aSLionel Sambuc unsigned int BlockVisitOrder = VisitOrder[Block->getBlockID()];
1119f4a2713aSLionel Sambuc for (CFGBlock::const_pred_iterator PI = Block->pred_begin(),
1120f4a2713aSLionel Sambuc PE = Block->pred_end(); PI != PE; ++PI) {
1121f4a2713aSLionel Sambuc if (*PI && BlockVisitOrder < VisitOrder[(*PI)->getBlockID()])
1122f4a2713aSLionel Sambuc return true;
1123f4a2713aSLionel Sambuc }
1124f4a2713aSLionel Sambuc return false;
1125f4a2713aSLionel Sambuc }
1126f4a2713aSLionel Sambuc
checkParamsForReturnTypestate(SourceLocation BlameLoc,ConsumedWarningsHandlerBase & WarningsHandler) const1127f4a2713aSLionel Sambuc void ConsumedStateMap::checkParamsForReturnTypestate(SourceLocation BlameLoc,
1128f4a2713aSLionel Sambuc ConsumedWarningsHandlerBase &WarningsHandler) const {
1129f4a2713aSLionel Sambuc
1130*0a6a1f1dSLionel Sambuc for (const auto &DM : VarMap) {
1131*0a6a1f1dSLionel Sambuc if (isa<ParmVarDecl>(DM.first)) {
1132*0a6a1f1dSLionel Sambuc const ParmVarDecl *Param = cast<ParmVarDecl>(DM.first);
1133*0a6a1f1dSLionel Sambuc const ReturnTypestateAttr *RTA = Param->getAttr<ReturnTypestateAttr>();
1134f4a2713aSLionel Sambuc
1135*0a6a1f1dSLionel Sambuc if (!RTA)
1136*0a6a1f1dSLionel Sambuc continue;
1137f4a2713aSLionel Sambuc
1138*0a6a1f1dSLionel Sambuc ConsumedState ExpectedState = mapReturnTypestateAttrState(RTA);
1139*0a6a1f1dSLionel Sambuc if (DM.second != ExpectedState)
1140f4a2713aSLionel Sambuc WarningsHandler.warnParamReturnTypestateMismatch(BlameLoc,
1141f4a2713aSLionel Sambuc Param->getNameAsString(), stateToString(ExpectedState),
1142*0a6a1f1dSLionel Sambuc stateToString(DM.second));
1143f4a2713aSLionel Sambuc }
1144f4a2713aSLionel Sambuc }
1145f4a2713aSLionel Sambuc }
1146f4a2713aSLionel Sambuc
clearTemporaries()1147f4a2713aSLionel Sambuc void ConsumedStateMap::clearTemporaries() {
1148f4a2713aSLionel Sambuc TmpMap.clear();
1149f4a2713aSLionel Sambuc }
1150f4a2713aSLionel Sambuc
getState(const VarDecl * Var) const1151f4a2713aSLionel Sambuc ConsumedState ConsumedStateMap::getState(const VarDecl *Var) const {
1152f4a2713aSLionel Sambuc VarMapType::const_iterator Entry = VarMap.find(Var);
1153f4a2713aSLionel Sambuc
1154f4a2713aSLionel Sambuc if (Entry != VarMap.end())
1155f4a2713aSLionel Sambuc return Entry->second;
1156f4a2713aSLionel Sambuc
1157f4a2713aSLionel Sambuc return CS_None;
1158f4a2713aSLionel Sambuc }
1159f4a2713aSLionel Sambuc
1160f4a2713aSLionel Sambuc ConsumedState
getState(const CXXBindTemporaryExpr * Tmp) const1161f4a2713aSLionel Sambuc ConsumedStateMap::getState(const CXXBindTemporaryExpr *Tmp) const {
1162f4a2713aSLionel Sambuc TmpMapType::const_iterator Entry = TmpMap.find(Tmp);
1163f4a2713aSLionel Sambuc
1164f4a2713aSLionel Sambuc if (Entry != TmpMap.end())
1165f4a2713aSLionel Sambuc return Entry->second;
1166f4a2713aSLionel Sambuc
1167f4a2713aSLionel Sambuc return CS_None;
1168f4a2713aSLionel Sambuc }
1169f4a2713aSLionel Sambuc
intersect(const ConsumedStateMap * Other)1170f4a2713aSLionel Sambuc void ConsumedStateMap::intersect(const ConsumedStateMap *Other) {
1171f4a2713aSLionel Sambuc ConsumedState LocalState;
1172f4a2713aSLionel Sambuc
1173f4a2713aSLionel Sambuc if (this->From && this->From == Other->From && !Other->Reachable) {
1174f4a2713aSLionel Sambuc this->markUnreachable();
1175f4a2713aSLionel Sambuc return;
1176f4a2713aSLionel Sambuc }
1177f4a2713aSLionel Sambuc
1178*0a6a1f1dSLionel Sambuc for (const auto &DM : Other->VarMap) {
1179*0a6a1f1dSLionel Sambuc LocalState = this->getState(DM.first);
1180f4a2713aSLionel Sambuc
1181f4a2713aSLionel Sambuc if (LocalState == CS_None)
1182f4a2713aSLionel Sambuc continue;
1183f4a2713aSLionel Sambuc
1184*0a6a1f1dSLionel Sambuc if (LocalState != DM.second)
1185*0a6a1f1dSLionel Sambuc VarMap[DM.first] = CS_Unknown;
1186f4a2713aSLionel Sambuc }
1187f4a2713aSLionel Sambuc }
1188f4a2713aSLionel Sambuc
intersectAtLoopHead(const CFGBlock * LoopHead,const CFGBlock * LoopBack,const ConsumedStateMap * LoopBackStates,ConsumedWarningsHandlerBase & WarningsHandler)1189f4a2713aSLionel Sambuc void ConsumedStateMap::intersectAtLoopHead(const CFGBlock *LoopHead,
1190f4a2713aSLionel Sambuc const CFGBlock *LoopBack, const ConsumedStateMap *LoopBackStates,
1191f4a2713aSLionel Sambuc ConsumedWarningsHandlerBase &WarningsHandler) {
1192f4a2713aSLionel Sambuc
1193f4a2713aSLionel Sambuc ConsumedState LocalState;
1194f4a2713aSLionel Sambuc SourceLocation BlameLoc = getLastStmtLoc(LoopBack);
1195f4a2713aSLionel Sambuc
1196*0a6a1f1dSLionel Sambuc for (const auto &DM : LoopBackStates->VarMap) {
1197*0a6a1f1dSLionel Sambuc LocalState = this->getState(DM.first);
1198f4a2713aSLionel Sambuc
1199f4a2713aSLionel Sambuc if (LocalState == CS_None)
1200f4a2713aSLionel Sambuc continue;
1201f4a2713aSLionel Sambuc
1202*0a6a1f1dSLionel Sambuc if (LocalState != DM.second) {
1203*0a6a1f1dSLionel Sambuc VarMap[DM.first] = CS_Unknown;
1204*0a6a1f1dSLionel Sambuc WarningsHandler.warnLoopStateMismatch(BlameLoc,
1205*0a6a1f1dSLionel Sambuc DM.first->getNameAsString());
1206f4a2713aSLionel Sambuc }
1207f4a2713aSLionel Sambuc }
1208f4a2713aSLionel Sambuc }
1209f4a2713aSLionel Sambuc
markUnreachable()1210f4a2713aSLionel Sambuc void ConsumedStateMap::markUnreachable() {
1211f4a2713aSLionel Sambuc this->Reachable = false;
1212f4a2713aSLionel Sambuc VarMap.clear();
1213f4a2713aSLionel Sambuc TmpMap.clear();
1214f4a2713aSLionel Sambuc }
1215f4a2713aSLionel Sambuc
setState(const VarDecl * Var,ConsumedState State)1216f4a2713aSLionel Sambuc void ConsumedStateMap::setState(const VarDecl *Var, ConsumedState State) {
1217f4a2713aSLionel Sambuc VarMap[Var] = State;
1218f4a2713aSLionel Sambuc }
1219f4a2713aSLionel Sambuc
setState(const CXXBindTemporaryExpr * Tmp,ConsumedState State)1220f4a2713aSLionel Sambuc void ConsumedStateMap::setState(const CXXBindTemporaryExpr *Tmp,
1221f4a2713aSLionel Sambuc ConsumedState State) {
1222f4a2713aSLionel Sambuc TmpMap[Tmp] = State;
1223f4a2713aSLionel Sambuc }
1224f4a2713aSLionel Sambuc
remove(const CXXBindTemporaryExpr * Tmp)1225*0a6a1f1dSLionel Sambuc void ConsumedStateMap::remove(const CXXBindTemporaryExpr *Tmp) {
1226*0a6a1f1dSLionel Sambuc TmpMap.erase(Tmp);
1227f4a2713aSLionel Sambuc }
1228f4a2713aSLionel Sambuc
operator !=(const ConsumedStateMap * Other) const1229f4a2713aSLionel Sambuc bool ConsumedStateMap::operator!=(const ConsumedStateMap *Other) const {
1230*0a6a1f1dSLionel Sambuc for (const auto &DM : Other->VarMap)
1231*0a6a1f1dSLionel Sambuc if (this->getState(DM.first) != DM.second)
1232f4a2713aSLionel Sambuc return true;
1233f4a2713aSLionel Sambuc return false;
1234f4a2713aSLionel Sambuc }
1235f4a2713aSLionel Sambuc
determineExpectedReturnState(AnalysisDeclContext & AC,const FunctionDecl * D)1236f4a2713aSLionel Sambuc void ConsumedAnalyzer::determineExpectedReturnState(AnalysisDeclContext &AC,
1237f4a2713aSLionel Sambuc const FunctionDecl *D) {
1238f4a2713aSLionel Sambuc QualType ReturnType;
1239f4a2713aSLionel Sambuc if (const CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1240f4a2713aSLionel Sambuc ASTContext &CurrContext = AC.getASTContext();
1241f4a2713aSLionel Sambuc ReturnType = Constructor->getThisType(CurrContext)->getPointeeType();
1242f4a2713aSLionel Sambuc } else
1243f4a2713aSLionel Sambuc ReturnType = D->getCallResultType();
1244f4a2713aSLionel Sambuc
1245*0a6a1f1dSLionel Sambuc if (const ReturnTypestateAttr *RTSAttr = D->getAttr<ReturnTypestateAttr>()) {
1246f4a2713aSLionel Sambuc const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1247f4a2713aSLionel Sambuc if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1248f4a2713aSLionel Sambuc // FIXME: This should be removed when template instantiation propagates
1249f4a2713aSLionel Sambuc // attributes at template specialization definition, not
1250f4a2713aSLionel Sambuc // declaration. When it is removed the test needs to be enabled
1251f4a2713aSLionel Sambuc // in SemaDeclAttr.cpp.
1252f4a2713aSLionel Sambuc WarningsHandler.warnReturnTypestateForUnconsumableType(
1253f4a2713aSLionel Sambuc RTSAttr->getLocation(), ReturnType.getAsString());
1254f4a2713aSLionel Sambuc ExpectedReturnState = CS_None;
1255f4a2713aSLionel Sambuc } else
1256f4a2713aSLionel Sambuc ExpectedReturnState = mapReturnTypestateAttrState(RTSAttr);
1257*0a6a1f1dSLionel Sambuc } else if (isConsumableType(ReturnType)) {
1258*0a6a1f1dSLionel Sambuc if (isAutoCastType(ReturnType)) // We can auto-cast the state to the
1259*0a6a1f1dSLionel Sambuc ExpectedReturnState = CS_None; // expected state.
1260*0a6a1f1dSLionel Sambuc else
1261f4a2713aSLionel Sambuc ExpectedReturnState = mapConsumableAttrState(ReturnType);
1262*0a6a1f1dSLionel Sambuc }
1263f4a2713aSLionel Sambuc else
1264f4a2713aSLionel Sambuc ExpectedReturnState = CS_None;
1265f4a2713aSLionel Sambuc }
1266f4a2713aSLionel Sambuc
splitState(const CFGBlock * CurrBlock,const ConsumedStmtVisitor & Visitor)1267f4a2713aSLionel Sambuc bool ConsumedAnalyzer::splitState(const CFGBlock *CurrBlock,
1268f4a2713aSLionel Sambuc const ConsumedStmtVisitor &Visitor) {
1269f4a2713aSLionel Sambuc
1270*0a6a1f1dSLionel Sambuc std::unique_ptr<ConsumedStateMap> FalseStates(
1271*0a6a1f1dSLionel Sambuc new ConsumedStateMap(*CurrStates));
1272f4a2713aSLionel Sambuc PropagationInfo PInfo;
1273f4a2713aSLionel Sambuc
1274f4a2713aSLionel Sambuc if (const IfStmt *IfNode =
1275f4a2713aSLionel Sambuc dyn_cast_or_null<IfStmt>(CurrBlock->getTerminator().getStmt())) {
1276f4a2713aSLionel Sambuc
1277*0a6a1f1dSLionel Sambuc const Expr *Cond = IfNode->getCond();
1278f4a2713aSLionel Sambuc
1279f4a2713aSLionel Sambuc PInfo = Visitor.getInfo(Cond);
1280f4a2713aSLionel Sambuc if (!PInfo.isValid() && isa<BinaryOperator>(Cond))
1281f4a2713aSLionel Sambuc PInfo = Visitor.getInfo(cast<BinaryOperator>(Cond)->getRHS());
1282f4a2713aSLionel Sambuc
1283f4a2713aSLionel Sambuc if (PInfo.isVarTest()) {
1284f4a2713aSLionel Sambuc CurrStates->setSource(Cond);
1285f4a2713aSLionel Sambuc FalseStates->setSource(Cond);
1286f4a2713aSLionel Sambuc splitVarStateForIf(IfNode, PInfo.getVarTest(), CurrStates,
1287f4a2713aSLionel Sambuc FalseStates.get());
1288f4a2713aSLionel Sambuc
1289f4a2713aSLionel Sambuc } else if (PInfo.isBinTest()) {
1290f4a2713aSLionel Sambuc CurrStates->setSource(PInfo.testSourceNode());
1291f4a2713aSLionel Sambuc FalseStates->setSource(PInfo.testSourceNode());
1292f4a2713aSLionel Sambuc splitVarStateForIfBinOp(PInfo, CurrStates, FalseStates.get());
1293f4a2713aSLionel Sambuc
1294f4a2713aSLionel Sambuc } else {
1295f4a2713aSLionel Sambuc return false;
1296f4a2713aSLionel Sambuc }
1297f4a2713aSLionel Sambuc
1298f4a2713aSLionel Sambuc } else if (const BinaryOperator *BinOp =
1299f4a2713aSLionel Sambuc dyn_cast_or_null<BinaryOperator>(CurrBlock->getTerminator().getStmt())) {
1300f4a2713aSLionel Sambuc
1301f4a2713aSLionel Sambuc PInfo = Visitor.getInfo(BinOp->getLHS());
1302f4a2713aSLionel Sambuc if (!PInfo.isVarTest()) {
1303f4a2713aSLionel Sambuc if ((BinOp = dyn_cast_or_null<BinaryOperator>(BinOp->getLHS()))) {
1304f4a2713aSLionel Sambuc PInfo = Visitor.getInfo(BinOp->getRHS());
1305f4a2713aSLionel Sambuc
1306f4a2713aSLionel Sambuc if (!PInfo.isVarTest())
1307f4a2713aSLionel Sambuc return false;
1308f4a2713aSLionel Sambuc
1309f4a2713aSLionel Sambuc } else {
1310f4a2713aSLionel Sambuc return false;
1311f4a2713aSLionel Sambuc }
1312f4a2713aSLionel Sambuc }
1313f4a2713aSLionel Sambuc
1314f4a2713aSLionel Sambuc CurrStates->setSource(BinOp);
1315f4a2713aSLionel Sambuc FalseStates->setSource(BinOp);
1316f4a2713aSLionel Sambuc
1317f4a2713aSLionel Sambuc const VarTestResult &Test = PInfo.getVarTest();
1318f4a2713aSLionel Sambuc ConsumedState VarState = CurrStates->getState(Test.Var);
1319f4a2713aSLionel Sambuc
1320f4a2713aSLionel Sambuc if (BinOp->getOpcode() == BO_LAnd) {
1321f4a2713aSLionel Sambuc if (VarState == CS_Unknown)
1322f4a2713aSLionel Sambuc CurrStates->setState(Test.Var, Test.TestsFor);
1323f4a2713aSLionel Sambuc else if (VarState == invertConsumedUnconsumed(Test.TestsFor))
1324f4a2713aSLionel Sambuc CurrStates->markUnreachable();
1325f4a2713aSLionel Sambuc
1326f4a2713aSLionel Sambuc } else if (BinOp->getOpcode() == BO_LOr) {
1327f4a2713aSLionel Sambuc if (VarState == CS_Unknown)
1328f4a2713aSLionel Sambuc FalseStates->setState(Test.Var,
1329f4a2713aSLionel Sambuc invertConsumedUnconsumed(Test.TestsFor));
1330f4a2713aSLionel Sambuc else if (VarState == Test.TestsFor)
1331f4a2713aSLionel Sambuc FalseStates->markUnreachable();
1332f4a2713aSLionel Sambuc }
1333f4a2713aSLionel Sambuc
1334f4a2713aSLionel Sambuc } else {
1335f4a2713aSLionel Sambuc return false;
1336f4a2713aSLionel Sambuc }
1337f4a2713aSLionel Sambuc
1338f4a2713aSLionel Sambuc CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin();
1339f4a2713aSLionel Sambuc
1340f4a2713aSLionel Sambuc if (*SI)
1341f4a2713aSLionel Sambuc BlockInfo.addInfo(*SI, CurrStates);
1342f4a2713aSLionel Sambuc else
1343f4a2713aSLionel Sambuc delete CurrStates;
1344f4a2713aSLionel Sambuc
1345f4a2713aSLionel Sambuc if (*++SI)
1346*0a6a1f1dSLionel Sambuc BlockInfo.addInfo(*SI, FalseStates.release());
1347f4a2713aSLionel Sambuc
1348*0a6a1f1dSLionel Sambuc CurrStates = nullptr;
1349f4a2713aSLionel Sambuc return true;
1350f4a2713aSLionel Sambuc }
1351f4a2713aSLionel Sambuc
run(AnalysisDeclContext & AC)1352f4a2713aSLionel Sambuc void ConsumedAnalyzer::run(AnalysisDeclContext &AC) {
1353f4a2713aSLionel Sambuc const FunctionDecl *D = dyn_cast_or_null<FunctionDecl>(AC.getDecl());
1354f4a2713aSLionel Sambuc if (!D)
1355f4a2713aSLionel Sambuc return;
1356f4a2713aSLionel Sambuc
1357f4a2713aSLionel Sambuc CFG *CFGraph = AC.getCFG();
1358f4a2713aSLionel Sambuc if (!CFGraph)
1359f4a2713aSLionel Sambuc return;
1360f4a2713aSLionel Sambuc
1361f4a2713aSLionel Sambuc determineExpectedReturnState(AC, D);
1362f4a2713aSLionel Sambuc
1363f4a2713aSLionel Sambuc PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>();
1364f4a2713aSLionel Sambuc // AC.getCFG()->viewCFG(LangOptions());
1365f4a2713aSLionel Sambuc
1366f4a2713aSLionel Sambuc BlockInfo = ConsumedBlockInfo(CFGraph->getNumBlockIDs(), SortedGraph);
1367f4a2713aSLionel Sambuc
1368f4a2713aSLionel Sambuc CurrStates = new ConsumedStateMap();
1369f4a2713aSLionel Sambuc ConsumedStmtVisitor Visitor(AC, *this, CurrStates);
1370f4a2713aSLionel Sambuc
1371f4a2713aSLionel Sambuc // Add all trackable parameters to the state map.
1372*0a6a1f1dSLionel Sambuc for (const auto *PI : D->params())
1373*0a6a1f1dSLionel Sambuc Visitor.VisitParmVarDecl(PI);
1374f4a2713aSLionel Sambuc
1375f4a2713aSLionel Sambuc // Visit all of the function's basic blocks.
1376*0a6a1f1dSLionel Sambuc for (const auto *CurrBlock : *SortedGraph) {
1377*0a6a1f1dSLionel Sambuc if (!CurrStates)
1378f4a2713aSLionel Sambuc CurrStates = BlockInfo.getInfo(CurrBlock);
1379f4a2713aSLionel Sambuc
1380f4a2713aSLionel Sambuc if (!CurrStates) {
1381f4a2713aSLionel Sambuc continue;
1382f4a2713aSLionel Sambuc
1383f4a2713aSLionel Sambuc } else if (!CurrStates->isReachable()) {
1384f4a2713aSLionel Sambuc delete CurrStates;
1385*0a6a1f1dSLionel Sambuc CurrStates = nullptr;
1386f4a2713aSLionel Sambuc continue;
1387f4a2713aSLionel Sambuc }
1388f4a2713aSLionel Sambuc
1389f4a2713aSLionel Sambuc Visitor.reset(CurrStates);
1390f4a2713aSLionel Sambuc
1391f4a2713aSLionel Sambuc // Visit all of the basic block's statements.
1392*0a6a1f1dSLionel Sambuc for (const auto &B : *CurrBlock) {
1393*0a6a1f1dSLionel Sambuc switch (B.getKind()) {
1394f4a2713aSLionel Sambuc case CFGElement::Statement:
1395*0a6a1f1dSLionel Sambuc Visitor.Visit(B.castAs<CFGStmt>().getStmt());
1396f4a2713aSLionel Sambuc break;
1397f4a2713aSLionel Sambuc
1398f4a2713aSLionel Sambuc case CFGElement::TemporaryDtor: {
1399*0a6a1f1dSLionel Sambuc const CFGTemporaryDtor &DTor = B.castAs<CFGTemporaryDtor>();
1400f4a2713aSLionel Sambuc const CXXBindTemporaryExpr *BTE = DTor.getBindTemporaryExpr();
1401f4a2713aSLionel Sambuc
1402f4a2713aSLionel Sambuc Visitor.checkCallability(PropagationInfo(BTE),
1403f4a2713aSLionel Sambuc DTor.getDestructorDecl(AC.getASTContext()),
1404f4a2713aSLionel Sambuc BTE->getExprLoc());
1405*0a6a1f1dSLionel Sambuc CurrStates->remove(BTE);
1406f4a2713aSLionel Sambuc break;
1407f4a2713aSLionel Sambuc }
1408f4a2713aSLionel Sambuc
1409f4a2713aSLionel Sambuc case CFGElement::AutomaticObjectDtor: {
1410*0a6a1f1dSLionel Sambuc const CFGAutomaticObjDtor &DTor = B.castAs<CFGAutomaticObjDtor>();
1411f4a2713aSLionel Sambuc SourceLocation Loc = DTor.getTriggerStmt()->getLocEnd();
1412f4a2713aSLionel Sambuc const VarDecl *Var = DTor.getVarDecl();
1413f4a2713aSLionel Sambuc
1414f4a2713aSLionel Sambuc Visitor.checkCallability(PropagationInfo(Var),
1415f4a2713aSLionel Sambuc DTor.getDestructorDecl(AC.getASTContext()),
1416f4a2713aSLionel Sambuc Loc);
1417f4a2713aSLionel Sambuc break;
1418f4a2713aSLionel Sambuc }
1419f4a2713aSLionel Sambuc
1420f4a2713aSLionel Sambuc default:
1421f4a2713aSLionel Sambuc break;
1422f4a2713aSLionel Sambuc }
1423f4a2713aSLionel Sambuc }
1424f4a2713aSLionel Sambuc
1425f4a2713aSLionel Sambuc // TODO: Handle other forms of branching with precision, including while-
1426f4a2713aSLionel Sambuc // and for-loops. (Deferred)
1427f4a2713aSLionel Sambuc if (!splitState(CurrBlock, Visitor)) {
1428*0a6a1f1dSLionel Sambuc CurrStates->setSource(nullptr);
1429f4a2713aSLionel Sambuc
1430f4a2713aSLionel Sambuc if (CurrBlock->succ_size() > 1 ||
1431f4a2713aSLionel Sambuc (CurrBlock->succ_size() == 1 &&
1432f4a2713aSLionel Sambuc (*CurrBlock->succ_begin())->pred_size() > 1)) {
1433f4a2713aSLionel Sambuc
1434f4a2713aSLionel Sambuc bool OwnershipTaken = false;
1435f4a2713aSLionel Sambuc
1436f4a2713aSLionel Sambuc for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
1437f4a2713aSLionel Sambuc SE = CurrBlock->succ_end(); SI != SE; ++SI) {
1438f4a2713aSLionel Sambuc
1439*0a6a1f1dSLionel Sambuc if (*SI == nullptr) continue;
1440f4a2713aSLionel Sambuc
1441f4a2713aSLionel Sambuc if (BlockInfo.isBackEdge(CurrBlock, *SI)) {
1442f4a2713aSLionel Sambuc BlockInfo.borrowInfo(*SI)->intersectAtLoopHead(*SI, CurrBlock,
1443f4a2713aSLionel Sambuc CurrStates,
1444f4a2713aSLionel Sambuc WarningsHandler);
1445f4a2713aSLionel Sambuc
1446f4a2713aSLionel Sambuc if (BlockInfo.allBackEdgesVisited(*SI, CurrBlock))
1447f4a2713aSLionel Sambuc BlockInfo.discardInfo(*SI);
1448f4a2713aSLionel Sambuc } else {
1449f4a2713aSLionel Sambuc BlockInfo.addInfo(*SI, CurrStates, OwnershipTaken);
1450f4a2713aSLionel Sambuc }
1451f4a2713aSLionel Sambuc }
1452f4a2713aSLionel Sambuc
1453f4a2713aSLionel Sambuc if (!OwnershipTaken)
1454f4a2713aSLionel Sambuc delete CurrStates;
1455f4a2713aSLionel Sambuc
1456*0a6a1f1dSLionel Sambuc CurrStates = nullptr;
1457f4a2713aSLionel Sambuc }
1458f4a2713aSLionel Sambuc }
1459f4a2713aSLionel Sambuc
1460f4a2713aSLionel Sambuc if (CurrBlock == &AC.getCFG()->getExit() &&
1461f4a2713aSLionel Sambuc D->getCallResultType()->isVoidType())
1462f4a2713aSLionel Sambuc CurrStates->checkParamsForReturnTypestate(D->getLocation(),
1463f4a2713aSLionel Sambuc WarningsHandler);
1464f4a2713aSLionel Sambuc } // End of block iterator.
1465f4a2713aSLionel Sambuc
1466f4a2713aSLionel Sambuc // Delete the last existing state map.
1467f4a2713aSLionel Sambuc delete CurrStates;
1468f4a2713aSLionel Sambuc
1469f4a2713aSLionel Sambuc WarningsHandler.emitDiagnostics();
1470f4a2713aSLionel Sambuc }
1471f4a2713aSLionel Sambuc }} // end namespace clang::consumed
1472