blob: ef9617a3ec4213a40087d38caaf26e91088aeefb [file] [log] [blame]
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001//===- Consumed.cpp --------------------------------------------*- C++ --*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// A intra-procedural analysis for checking consumed properties. This is based,
11// in part, on research on linear types.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/AST/RecursiveASTVisitor.h"
20#include "clang/AST/StmtVisitor.h"
21#include "clang/AST/StmtCXX.h"
22#include "clang/AST/Type.h"
23#include "clang/Analysis/Analyses/PostOrderCFGView.h"
24#include "clang/Analysis/AnalysisContext.h"
25#include "clang/Analysis/CFG.h"
26#include "clang/Analysis/Analyses/Consumed.h"
27#include "clang/Basic/OperatorKinds.h"
28#include "clang/Basic/SourceLocation.h"
DeLesley Hutchins48a31762013-08-12 21:20:55 +000029#include "llvm/ADT/DenseMap.h"
30#include "llvm/ADT/SmallVector.h"
DeLesley Hutchins5533ec52013-08-29 17:26:57 +000031#include "llvm/Support/Compiler.h"
DeLesley Hutchins48a31762013-08-12 21:20:55 +000032#include "llvm/Support/raw_ostream.h"
33
DeLesley Hutchins3277a612013-10-09 18:30:24 +000034// TODO: Use information from tests in while-loop conditional.
DeLesley Hutchinsfc368252013-09-03 20:11:38 +000035// TODO: Add notes about the actual and expected state for
DeLesley Hutchins5533ec52013-08-29 17:26:57 +000036// TODO: Correctly identify unreachable blocks when chaining boolean operators.
DeLesley Hutchins210791a2013-10-04 21:28:06 +000037// TODO: Adjust the parser and AttributesList class to support lists of
38// identifiers.
DeLesley Hutchins5533ec52013-08-29 17:26:57 +000039// TODO: Warn about unreachable code.
40// TODO: Switch to using a bitmap to track unreachable blocks.
DeLesley Hutchins5533ec52013-08-29 17:26:57 +000041// TODO: Handle variable definitions, e.g. bool valid = x.isValid();
42// if (valid) ...; (Deferred)
DeLesley Hutchins48a31762013-08-12 21:20:55 +000043// TODO: Take notes on state transitions to provide better warning messages.
44// (Deferred)
45// TODO: Test nested conditionals: A) Checking the same value multiple times,
46// and 2) Checking different values. (Deferred)
DeLesley Hutchins48a31762013-08-12 21:20:55 +000047
48using namespace clang;
49using namespace consumed;
50
51// Key method definition
52ConsumedWarningsHandlerBase::~ConsumedWarningsHandlerBase() {}
53
DeLesley Hutchins65013202013-10-17 18:19:31 +000054static SourceLocation getFirstStmtLoc(const CFGBlock *Block) {
55 // Find the source location of the first statement in the block, if the block
56 // is not empty.
57 for (CFGBlock::const_iterator BI = Block->begin(), BE = Block->end();
58 BI != BE; ++BI) {
59 if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>())
60 return CS->getStmt()->getLocStart();
61 }
62
63 // Block is empty.
64 // If we have one successor, return the first statement in that block
65 if (Block->succ_size() == 1 && *Block->succ_begin())
66 return getFirstStmtLoc(*Block->succ_begin());
67
68 return SourceLocation();
69}
70
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +000071static SourceLocation getLastStmtLoc(const CFGBlock *Block) {
DeLesley Hutchins3277a612013-10-09 18:30:24 +000072 // Find the source location of the last statement in the block, if the block
73 // is not empty.
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +000074 if (const Stmt *StmtNode = Block->getTerminator()) {
DeLesley Hutchins3277a612013-10-09 18:30:24 +000075 return StmtNode->getLocStart();
76 } else {
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +000077 for (CFGBlock::const_reverse_iterator BI = Block->rbegin(),
78 BE = Block->rend(); BI != BE; ++BI) {
DeLesley Hutchins3277a612013-10-09 18:30:24 +000079 if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>())
80 return CS->getStmt()->getLocStart();
81 }
82 }
DeLesley Hutchins65013202013-10-17 18:19:31 +000083
84 // If we have one successor, return the first statement in that block
85 SourceLocation Loc;
86 if (Block->succ_size() == 1 && *Block->succ_begin())
87 Loc = getFirstStmtLoc(*Block->succ_begin());
88 if (Loc.isValid())
89 return Loc;
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +000090
DeLesley Hutchins65013202013-10-17 18:19:31 +000091 // If we have one predecessor, return the last statement in that block
92 if (Block->pred_size() == 1 && *Block->pred_begin())
93 return getLastStmtLoc(*Block->pred_begin());
94
95 return Loc;
DeLesley Hutchins3277a612013-10-09 18:30:24 +000096}
97
DeLesley Hutchins5533ec52013-08-29 17:26:57 +000098static ConsumedState invertConsumedUnconsumed(ConsumedState State) {
99 switch (State) {
100 case CS_Unconsumed:
101 return CS_Consumed;
102 case CS_Consumed:
103 return CS_Unconsumed;
104 case CS_None:
105 return CS_None;
106 case CS_Unknown:
107 return CS_Unknown;
108 }
109 llvm_unreachable("invalid enum");
110}
111
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000112static bool isCallableInState(const CallableWhenAttr *CWAttr,
113 ConsumedState State) {
114
115 CallableWhenAttr::callableState_iterator I = CWAttr->callableState_begin(),
116 E = CWAttr->callableState_end();
117
118 for (; I != E; ++I) {
119
120 ConsumedState MappedAttrState = CS_None;
121
122 switch (*I) {
123 case CallableWhenAttr::Unknown:
124 MappedAttrState = CS_Unknown;
125 break;
126
127 case CallableWhenAttr::Unconsumed:
128 MappedAttrState = CS_Unconsumed;
129 break;
130
131 case CallableWhenAttr::Consumed:
132 MappedAttrState = CS_Consumed;
133 break;
134 }
135
136 if (MappedAttrState == State)
137 return true;
138 }
139
140 return false;
141}
142
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000143static bool isConsumableType(const QualType &QT) {
144 if (const CXXRecordDecl *RD = QT->getAsCXXRecordDecl())
145 return RD->hasAttr<ConsumableAttr>();
146 else
147 return false;
148}
149
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000150static bool isKnownState(ConsumedState State) {
151 switch (State) {
152 case CS_Unconsumed:
153 case CS_Consumed:
154 return true;
155 case CS_None:
156 case CS_Unknown:
157 return false;
158 }
Aaron Ballmana21f4b82013-08-29 20:36:09 +0000159 llvm_unreachable("invalid enum");
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000160}
161
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000162static bool isTestingFunction(const FunctionDecl *FunDecl) {
DeLesley Hutchins8d41d992013-10-11 22:30:48 +0000163 return FunDecl->hasAttr<TestsTypestateAttr>();
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000164}
165
David Blaikie16f76d22013-09-06 01:28:43 +0000166static ConsumedState mapConsumableAttrState(const QualType QT) {
167 assert(isConsumableType(QT));
168
169 const ConsumableAttr *CAttr =
170 QT->getAsCXXRecordDecl()->getAttr<ConsumableAttr>();
171
172 switch (CAttr->getDefaultState()) {
173 case ConsumableAttr::Unknown:
174 return CS_Unknown;
175 case ConsumableAttr::Unconsumed:
176 return CS_Unconsumed;
177 case ConsumableAttr::Consumed:
178 return CS_Consumed;
179 }
180 llvm_unreachable("invalid enum");
181}
182
DeLesley Hutchins69391772013-10-17 23:23:53 +0000183static ConsumedState
184mapParamTypestateAttrState(const ParamTypestateAttr *PTAttr) {
185 switch (PTAttr->getParamState()) {
186 case ParamTypestateAttr::Unknown:
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000187 return CS_Unknown;
DeLesley Hutchins69391772013-10-17 23:23:53 +0000188 case ParamTypestateAttr::Unconsumed:
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000189 return CS_Unconsumed;
DeLesley Hutchins69391772013-10-17 23:23:53 +0000190 case ParamTypestateAttr::Consumed:
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000191 return CS_Consumed;
192 }
193 llvm_unreachable("invalid_enum");
194}
195
Eric Christopherde156242013-09-03 20:43:00 +0000196static ConsumedState
197mapReturnTypestateAttrState(const ReturnTypestateAttr *RTSAttr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000198 switch (RTSAttr->getState()) {
199 case ReturnTypestateAttr::Unknown:
200 return CS_Unknown;
201 case ReturnTypestateAttr::Unconsumed:
202 return CS_Unconsumed;
203 case ReturnTypestateAttr::Consumed:
204 return CS_Consumed;
205 }
Eric Christopherde156242013-09-03 20:43:00 +0000206 llvm_unreachable("invalid enum");
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000207}
208
DeLesley Hutchins69391772013-10-17 23:23:53 +0000209static ConsumedState mapSetTypestateAttrState(const SetTypestateAttr *STAttr) {
210 switch (STAttr->getNewState()) {
211 case SetTypestateAttr::Unknown:
212 return CS_Unknown;
213 case SetTypestateAttr::Unconsumed:
214 return CS_Unconsumed;
215 case SetTypestateAttr::Consumed:
216 return CS_Consumed;
217 }
218 llvm_unreachable("invalid_enum");
219}
220
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000221static StringRef stateToString(ConsumedState State) {
222 switch (State) {
223 case consumed::CS_None:
224 return "none";
225
226 case consumed::CS_Unknown:
227 return "unknown";
228
229 case consumed::CS_Unconsumed:
230 return "unconsumed";
231
232 case consumed::CS_Consumed:
233 return "consumed";
234 }
Reid Kleckner6454d0a2013-08-13 00:11:59 +0000235 llvm_unreachable("invalid enum");
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000236}
237
DeLesley Hutchins8d41d992013-10-11 22:30:48 +0000238static ConsumedState testsFor(const FunctionDecl *FunDecl) {
239 assert(isTestingFunction(FunDecl));
240 switch (FunDecl->getAttr<TestsTypestateAttr>()->getTestState()) {
241 case TestsTypestateAttr::Unconsumed:
242 return CS_Unconsumed;
243 case TestsTypestateAttr::Consumed:
244 return CS_Consumed;
245 }
246 llvm_unreachable("invalid enum");
247}
248
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000249namespace {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000250struct VarTestResult {
251 const VarDecl *Var;
252 ConsumedState TestsFor;
253};
254} // end anonymous::VarTestResult
255
256namespace clang {
257namespace consumed {
258
259enum EffectiveOp {
260 EO_And,
261 EO_Or
262};
263
264class PropagationInfo {
265 enum {
266 IT_None,
267 IT_State,
268 IT_Test,
269 IT_BinTest,
270 IT_Var
271 } InfoType;
Eric Christopherf8a1baa2013-08-29 18:00:58 +0000272
273 struct BinTestTy {
274 const BinaryOperator *Source;
275 EffectiveOp EOp;
276 VarTestResult LTest;
277 VarTestResult RTest;
278 };
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000279
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000280 union {
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000281 ConsumedState State;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000282 VarTestResult Test;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000283 const VarDecl *Var;
Eric Christopherf8a1baa2013-08-29 18:00:58 +0000284 BinTestTy BinTest;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000285 };
286
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000287 QualType TempType;
288
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000289public:
290 PropagationInfo() : InfoType(IT_None) {}
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000291
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000292 PropagationInfo(const VarTestResult &Test) : InfoType(IT_Test), Test(Test) {}
293 PropagationInfo(const VarDecl *Var, ConsumedState TestsFor)
294 : InfoType(IT_Test) {
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000295
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000296 Test.Var = Var;
297 Test.TestsFor = TestsFor;
298 }
299
300 PropagationInfo(const BinaryOperator *Source, EffectiveOp EOp,
301 const VarTestResult &LTest, const VarTestResult &RTest)
302 : InfoType(IT_BinTest) {
303
304 BinTest.Source = Source;
305 BinTest.EOp = EOp;
306 BinTest.LTest = LTest;
307 BinTest.RTest = RTest;
308 }
309
310 PropagationInfo(const BinaryOperator *Source, EffectiveOp EOp,
311 const VarDecl *LVar, ConsumedState LTestsFor,
312 const VarDecl *RVar, ConsumedState RTestsFor)
313 : InfoType(IT_BinTest) {
314
315 BinTest.Source = Source;
316 BinTest.EOp = EOp;
317 BinTest.LTest.Var = LVar;
318 BinTest.LTest.TestsFor = LTestsFor;
319 BinTest.RTest.Var = RVar;
320 BinTest.RTest.TestsFor = RTestsFor;
321 }
322
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000323 PropagationInfo(ConsumedState State, QualType TempType)
324 : InfoType(IT_State), State(State), TempType(TempType) {}
325
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000326 PropagationInfo(const VarDecl *Var) : InfoType(IT_Var), Var(Var) {}
327
328 const ConsumedState & getState() const {
329 assert(InfoType == IT_State);
330 return State;
331 }
332
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000333 const QualType & getTempType() const {
334 assert(InfoType == IT_State);
335 return TempType;
336 }
337
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000338 const VarTestResult & getTest() const {
339 assert(InfoType == IT_Test);
340 return Test;
341 }
342
343 const VarTestResult & getLTest() const {
344 assert(InfoType == IT_BinTest);
345 return BinTest.LTest;
346 }
347
348 const VarTestResult & getRTest() const {
349 assert(InfoType == IT_BinTest);
350 return BinTest.RTest;
351 }
352
353 const VarDecl * getVar() const {
354 assert(InfoType == IT_Var);
355 return Var;
356 }
357
358 EffectiveOp testEffectiveOp() const {
359 assert(InfoType == IT_BinTest);
360 return BinTest.EOp;
361 }
362
363 const BinaryOperator * testSourceNode() const {
364 assert(InfoType == IT_BinTest);
365 return BinTest.Source;
366 }
367
368 bool isValid() const { return InfoType != IT_None; }
369 bool isState() const { return InfoType == IT_State; }
370 bool isTest() const { return InfoType == IT_Test; }
371 bool isBinTest() const { return InfoType == IT_BinTest; }
372 bool isVar() const { return InfoType == IT_Var; }
373
374 PropagationInfo invertTest() const {
375 assert(InfoType == IT_Test || InfoType == IT_BinTest);
376
377 if (InfoType == IT_Test) {
378 return PropagationInfo(Test.Var, invertConsumedUnconsumed(Test.TestsFor));
379
380 } else if (InfoType == IT_BinTest) {
381 return PropagationInfo(BinTest.Source,
382 BinTest.EOp == EO_And ? EO_Or : EO_And,
383 BinTest.LTest.Var, invertConsumedUnconsumed(BinTest.LTest.TestsFor),
384 BinTest.RTest.Var, invertConsumedUnconsumed(BinTest.RTest.TestsFor));
385 } else {
386 return PropagationInfo();
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000387 }
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000388 }
389};
390
391class ConsumedStmtVisitor : public ConstStmtVisitor<ConsumedStmtVisitor> {
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000392
393 typedef llvm::DenseMap<const Stmt *, PropagationInfo> MapType;
394 typedef std::pair<const Stmt *, PropagationInfo> PairType;
395 typedef MapType::iterator InfoEntry;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000396 typedef MapType::const_iterator ConstInfoEntry;
397
Reid Klecknere846dea2013-08-12 23:49:39 +0000398 AnalysisDeclContext &AC;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000399 ConsumedAnalyzer &Analyzer;
400 ConsumedStateMap *StateMap;
401 MapType PropagationMap;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000402 void forwardInfo(const Stmt *From, const Stmt *To);
403 bool isLikeMoveAssignment(const CXXMethodDecl *MethodDecl);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000404 void propagateReturnType(const Stmt *Call, const FunctionDecl *Fun,
405 QualType ReturnType);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000406
407public:
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000408 void checkCallability(const PropagationInfo &PInfo,
409 const FunctionDecl *FunDecl,
410 SourceLocation BlameLoc);
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000411
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000412 void Visit(const Stmt *StmtNode);
413
414 void VisitBinaryOperator(const BinaryOperator *BinOp);
415 void VisitCallExpr(const CallExpr *Call);
416 void VisitCastExpr(const CastExpr *Cast);
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000417 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Temp);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000418 void VisitCXXConstructExpr(const CXXConstructExpr *Call);
419 void VisitCXXMemberCallExpr(const CXXMemberCallExpr *Call);
420 void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *Call);
421 void VisitDeclRefExpr(const DeclRefExpr *DeclRef);
422 void VisitDeclStmt(const DeclStmt *DelcS);
423 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Temp);
424 void VisitMemberExpr(const MemberExpr *MExpr);
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000425 void VisitParmVarDecl(const ParmVarDecl *Param);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000426 void VisitReturnStmt(const ReturnStmt *Ret);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000427 void VisitUnaryOperator(const UnaryOperator *UOp);
428 void VisitVarDecl(const VarDecl *Var);
Reid Klecknere846dea2013-08-12 23:49:39 +0000429
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000430 ConsumedStmtVisitor(AnalysisDeclContext &AC, ConsumedAnalyzer &Analyzer,
431 ConsumedStateMap *StateMap)
432 : AC(AC), Analyzer(Analyzer), StateMap(StateMap) {}
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000433
434 PropagationInfo getInfo(const Stmt *StmtNode) const {
435 ConstInfoEntry Entry = PropagationMap.find(StmtNode);
436
437 if (Entry != PropagationMap.end())
438 return Entry->second;
439 else
440 return PropagationInfo();
441 }
442
443 void reset(ConsumedStateMap *NewStateMap) {
444 StateMap = NewStateMap;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000445 }
446};
447
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000448void ConsumedStmtVisitor::checkCallability(const PropagationInfo &PInfo,
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000449 const FunctionDecl *FunDecl,
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000450 SourceLocation BlameLoc) {
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000451
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000452 if (!FunDecl->hasAttr<CallableWhenAttr>())
453 return;
454
455 const CallableWhenAttr *CWAttr = FunDecl->getAttr<CallableWhenAttr>();
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000456
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000457 if (PInfo.isVar()) {
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000458 const VarDecl *Var = PInfo.getVar();
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000459 ConsumedState VarState = StateMap->getState(Var);
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000460
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000461 assert(VarState != CS_None && "Invalid state");
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000462
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000463 if (isCallableInState(CWAttr, VarState))
464 return;
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000465
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000466 Analyzer.WarningsHandler.warnUseInInvalidState(
467 FunDecl->getNameAsString(), Var->getNameAsString(),
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000468 stateToString(VarState), BlameLoc);
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000469
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000470 } else if (PInfo.isState()) {
471
472 assert(PInfo.getState() != CS_None && "Invalid state");
473
474 if (isCallableInState(CWAttr, PInfo.getState()))
475 return;
476
477 Analyzer.WarningsHandler.warnUseOfTempInInvalidState(
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000478 FunDecl->getNameAsString(), stateToString(PInfo.getState()), BlameLoc);
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000479 }
480}
481
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000482void ConsumedStmtVisitor::forwardInfo(const Stmt *From, const Stmt *To) {
483 InfoEntry Entry = PropagationMap.find(From);
484
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000485 if (Entry != PropagationMap.end())
486 PropagationMap.insert(PairType(To, Entry->second));
487}
488
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000489bool ConsumedStmtVisitor::isLikeMoveAssignment(
490 const CXXMethodDecl *MethodDecl) {
491
492 return MethodDecl->isMoveAssignmentOperator() ||
493 (MethodDecl->getOverloadedOperator() == OO_Equal &&
494 MethodDecl->getNumParams() == 1 &&
495 MethodDecl->getParamDecl(0)->getType()->isRValueReferenceType());
496}
497
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000498void ConsumedStmtVisitor::propagateReturnType(const Stmt *Call,
499 const FunctionDecl *Fun,
500 QualType ReturnType) {
501 if (isConsumableType(ReturnType)) {
502
503 ConsumedState ReturnState;
504
505 if (Fun->hasAttr<ReturnTypestateAttr>())
506 ReturnState = mapReturnTypestateAttrState(
507 Fun->getAttr<ReturnTypestateAttr>());
508 else
David Blaikie16f76d22013-09-06 01:28:43 +0000509 ReturnState = mapConsumableAttrState(ReturnType);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000510
511 PropagationMap.insert(PairType(Call,
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000512 PropagationInfo(ReturnState, ReturnType)));
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000513 }
514}
515
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000516void ConsumedStmtVisitor::Visit(const Stmt *StmtNode) {
517
518 ConstStmtVisitor<ConsumedStmtVisitor>::Visit(StmtNode);
519
520 for (Stmt::const_child_iterator CI = StmtNode->child_begin(),
521 CE = StmtNode->child_end(); CI != CE; ++CI) {
522
523 PropagationMap.erase(*CI);
524 }
525}
526
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000527void ConsumedStmtVisitor::VisitBinaryOperator(const BinaryOperator *BinOp) {
528 switch (BinOp->getOpcode()) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000529 case BO_LAnd:
530 case BO_LOr : {
531 InfoEntry LEntry = PropagationMap.find(BinOp->getLHS()),
532 REntry = PropagationMap.find(BinOp->getRHS());
533
534 VarTestResult LTest, RTest;
535
536 if (LEntry != PropagationMap.end() && LEntry->second.isTest()) {
537 LTest = LEntry->second.getTest();
538
539 } else {
540 LTest.Var = NULL;
541 LTest.TestsFor = CS_None;
542 }
543
544 if (REntry != PropagationMap.end() && REntry->second.isTest()) {
545 RTest = REntry->second.getTest();
546
547 } else {
548 RTest.Var = NULL;
549 RTest.TestsFor = CS_None;
550 }
551
552 if (!(LTest.Var == NULL && RTest.Var == NULL))
553 PropagationMap.insert(PairType(BinOp, PropagationInfo(BinOp,
554 static_cast<EffectiveOp>(BinOp->getOpcode() == BO_LOr), LTest, RTest)));
555
556 break;
557 }
558
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000559 case BO_PtrMemD:
560 case BO_PtrMemI:
561 forwardInfo(BinOp->getLHS(), BinOp);
562 break;
563
564 default:
565 break;
566 }
567}
568
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000569void ConsumedStmtVisitor::VisitCallExpr(const CallExpr *Call) {
570 if (const FunctionDecl *FunDecl =
571 dyn_cast_or_null<FunctionDecl>(Call->getDirectCallee())) {
572
573 // Special case for the std::move function.
574 // TODO: Make this more specific. (Deferred)
575 if (FunDecl->getNameAsString() == "move") {
576 InfoEntry Entry = PropagationMap.find(Call->getArg(0));
577
578 if (Entry != PropagationMap.end()) {
579 PropagationMap.insert(PairType(Call, Entry->second));
580 }
581
582 return;
583 }
584
585 unsigned Offset = Call->getNumArgs() - FunDecl->getNumParams();
586
587 for (unsigned Index = Offset; Index < Call->getNumArgs(); ++Index) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000588 const ParmVarDecl *Param = FunDecl->getParamDecl(Index - Offset);
589 QualType ParamType = Param->getType();
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000590
591 InfoEntry Entry = PropagationMap.find(Call->getArg(Index));
592
DeLesley Hutchins69391772013-10-17 23:23:53 +0000593 if (Entry == PropagationMap.end() ||
594 !(Entry->second.isState() || Entry->second.isVar()))
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000595 continue;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000596
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000597 PropagationInfo PInfo = Entry->second;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000598
DeLesley Hutchins69391772013-10-17 23:23:53 +0000599 // Check that the parameter is in the correct state.
600
601 if (Param->hasAttr<ParamTypestateAttr>()) {
602 ConsumedState ParamState =
603 PInfo.isState() ? PInfo.getState() :
604 StateMap->getState(PInfo.getVar());
605
606 ConsumedState ExpectedState =
607 mapParamTypestateAttrState(Param->getAttr<ParamTypestateAttr>());
608
609 if (ParamState != ExpectedState)
610 Analyzer.WarningsHandler.warnParamTypestateMismatch(
611 Call->getArg(Index - Offset)->getExprLoc(),
612 stateToString(ExpectedState), stateToString(ParamState));
613 }
614
615 if (!Entry->second.isVar())
616 continue;
617
618 // Adjust state on the caller side.
619
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000620 if (ParamType->isRValueReferenceType() ||
621 (ParamType->isLValueReferenceType() &&
622 !cast<LValueReferenceType>(*ParamType).isSpelledAsLValue())) {
623
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000624 StateMap->setState(PInfo.getVar(), consumed::CS_Consumed);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000625
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000626 } else if (Param->hasAttr<ReturnTypestateAttr>()) {
627 StateMap->setState(PInfo.getVar(),
628 mapReturnTypestateAttrState(Param->getAttr<ReturnTypestateAttr>()));
629
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000630 } else if (!(ParamType.isConstQualified() ||
631 ((ParamType->isReferenceType() ||
632 ParamType->isPointerType()) &&
633 ParamType->getPointeeType().isConstQualified()))) {
634
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000635 StateMap->setState(PInfo.getVar(), consumed::CS_Unknown);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000636 }
637 }
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000638
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000639 QualType RetType = FunDecl->getCallResultType();
640 if (RetType->isReferenceType())
641 RetType = RetType->getPointeeType();
642
643 propagateReturnType(Call, FunDecl, RetType);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000644 }
645}
646
647void ConsumedStmtVisitor::VisitCastExpr(const CastExpr *Cast) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000648 forwardInfo(Cast->getSubExpr(), Cast);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000649}
650
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000651void ConsumedStmtVisitor::VisitCXXBindTemporaryExpr(
652 const CXXBindTemporaryExpr *Temp) {
653
654 forwardInfo(Temp->getSubExpr(), Temp);
655}
656
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000657void ConsumedStmtVisitor::VisitCXXConstructExpr(const CXXConstructExpr *Call) {
658 CXXConstructorDecl *Constructor = Call->getConstructor();
Reid Klecknere846dea2013-08-12 23:49:39 +0000659
660 ASTContext &CurrContext = AC.getASTContext();
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000661 QualType ThisType = Constructor->getThisType(CurrContext)->getPointeeType();
662
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000663 if (!isConsumableType(ThisType))
664 return;
665
666 // FIXME: What should happen if someone annotates the move constructor?
667 if (Constructor->hasAttr<ReturnTypestateAttr>()) {
668 ReturnTypestateAttr *RTAttr = Constructor->getAttr<ReturnTypestateAttr>();
669 ConsumedState RetState = mapReturnTypestateAttrState(RTAttr);
670 PropagationMap.insert(PairType(Call, PropagationInfo(RetState, ThisType)));
671
672 } else if (Constructor->isDefaultConstructor()) {
673
674 PropagationMap.insert(PairType(Call,
675 PropagationInfo(consumed::CS_Consumed, ThisType)));
676
677 } else if (Constructor->isMoveConstructor()) {
678
679 PropagationInfo PInfo =
680 PropagationMap.find(Call->getArg(0))->second;
681
682 if (PInfo.isVar()) {
683 const VarDecl* Var = PInfo.getVar();
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000684
685 PropagationMap.insert(PairType(Call,
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000686 PropagationInfo(StateMap->getState(Var), ThisType)));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000687
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000688 StateMap->setState(Var, consumed::CS_Consumed);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000689
690 } else {
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000691 PropagationMap.insert(PairType(Call, PInfo));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000692 }
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000693
694 } else if (Constructor->isCopyConstructor()) {
695 MapType::iterator Entry = PropagationMap.find(Call->getArg(0));
696
697 if (Entry != PropagationMap.end())
698 PropagationMap.insert(PairType(Call, Entry->second));
699
700 } else {
701 ConsumedState RetState = mapConsumableAttrState(ThisType);
702 PropagationMap.insert(PairType(Call, PropagationInfo(RetState, ThisType)));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000703 }
704}
705
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000706
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000707void ConsumedStmtVisitor::VisitCXXMemberCallExpr(
708 const CXXMemberCallExpr *Call) {
709
710 VisitCallExpr(Call);
711
712 InfoEntry Entry = PropagationMap.find(Call->getCallee()->IgnoreParens());
713
714 if (Entry != PropagationMap.end()) {
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000715 PropagationInfo PInfo = Entry->second;
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000716 const CXXMethodDecl *MethodDecl = Call->getMethodDecl();
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000717
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000718 checkCallability(PInfo, MethodDecl, Call->getExprLoc());
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000719
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000720 if (PInfo.isVar()) {
721 if (isTestingFunction(MethodDecl))
DeLesley Hutchins8d41d992013-10-11 22:30:48 +0000722 PropagationMap.insert(PairType(Call,
723 PropagationInfo(PInfo.getVar(), testsFor(MethodDecl))));
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000724 else if (MethodDecl->hasAttr<SetTypestateAttr>())
725 StateMap->setState(PInfo.getVar(),
726 mapSetTypestateAttrState(MethodDecl->getAttr<SetTypestateAttr>()));
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000727 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000728 }
729}
730
731void ConsumedStmtVisitor::VisitCXXOperatorCallExpr(
732 const CXXOperatorCallExpr *Call) {
733
734 const FunctionDecl *FunDecl =
735 dyn_cast_or_null<FunctionDecl>(Call->getDirectCallee());
736
737 if (!FunDecl) return;
738
739 if (isa<CXXMethodDecl>(FunDecl) &&
740 isLikeMoveAssignment(cast<CXXMethodDecl>(FunDecl))) {
741
742 InfoEntry LEntry = PropagationMap.find(Call->getArg(0));
743 InfoEntry REntry = PropagationMap.find(Call->getArg(1));
744
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000745 PropagationInfo LPInfo, RPInfo;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000746
747 if (LEntry != PropagationMap.end() &&
748 REntry != PropagationMap.end()) {
749
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000750 LPInfo = LEntry->second;
751 RPInfo = REntry->second;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000752
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000753 if (LPInfo.isVar() && RPInfo.isVar()) {
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000754 StateMap->setState(LPInfo.getVar(),
755 StateMap->getState(RPInfo.getVar()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000756
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000757 StateMap->setState(RPInfo.getVar(), consumed::CS_Consumed);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000758
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000759 PropagationMap.insert(PairType(Call, LPInfo));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000760
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000761 } else if (LPInfo.isVar() && !RPInfo.isVar()) {
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000762 StateMap->setState(LPInfo.getVar(), RPInfo.getState());
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000763
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000764 PropagationMap.insert(PairType(Call, LPInfo));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000765
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000766 } else if (!LPInfo.isVar() && RPInfo.isVar()) {
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000767 PropagationMap.insert(PairType(Call,
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000768 PropagationInfo(StateMap->getState(RPInfo.getVar()),
769 LPInfo.getTempType())));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000770
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000771 StateMap->setState(RPInfo.getVar(), consumed::CS_Consumed);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000772
773 } else {
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000774 PropagationMap.insert(PairType(Call, RPInfo));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000775 }
776
777 } else if (LEntry != PropagationMap.end() &&
778 REntry == PropagationMap.end()) {
779
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000780 LPInfo = LEntry->second;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000781
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000782 if (LPInfo.isVar()) {
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000783 StateMap->setState(LPInfo.getVar(), consumed::CS_Unknown);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000784
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000785 PropagationMap.insert(PairType(Call, LPInfo));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000786
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000787 } else if (LPInfo.isState()) {
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000788 PropagationMap.insert(PairType(Call,
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000789 PropagationInfo(consumed::CS_Unknown, LPInfo.getTempType())));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000790 }
791
792 } else if (LEntry == PropagationMap.end() &&
793 REntry != PropagationMap.end()) {
794
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000795 if (REntry->second.isVar())
796 StateMap->setState(REntry->second.getVar(), consumed::CS_Consumed);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000797 }
798
799 } else {
800
801 VisitCallExpr(Call);
802
803 InfoEntry Entry = PropagationMap.find(Call->getArg(0));
804
805 if (Entry != PropagationMap.end()) {
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000806 PropagationInfo PInfo = Entry->second;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000807
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000808 checkCallability(PInfo, FunDecl, Call->getExprLoc());
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000809
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000810 if (PInfo.isVar()) {
DeLesley Hutchins7fa60ed2013-08-29 21:17:25 +0000811 if (isTestingFunction(FunDecl))
DeLesley Hutchins8d41d992013-10-11 22:30:48 +0000812 PropagationMap.insert(PairType(Call,
813 PropagationInfo(PInfo.getVar(), testsFor(FunDecl))));
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000814 else if (FunDecl->hasAttr<SetTypestateAttr>())
815 StateMap->setState(PInfo.getVar(),
816 mapSetTypestateAttrState(FunDecl->getAttr<SetTypestateAttr>()));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000817 }
818 }
819 }
820}
821
822void ConsumedStmtVisitor::VisitDeclRefExpr(const DeclRefExpr *DeclRef) {
823 if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(DeclRef->getDecl()))
824 if (StateMap->getState(Var) != consumed::CS_None)
825 PropagationMap.insert(PairType(DeclRef, PropagationInfo(Var)));
826}
827
828void ConsumedStmtVisitor::VisitDeclStmt(const DeclStmt *DeclS) {
829 for (DeclStmt::const_decl_iterator DI = DeclS->decl_begin(),
830 DE = DeclS->decl_end(); DI != DE; ++DI) {
831
832 if (isa<VarDecl>(*DI)) VisitVarDecl(cast<VarDecl>(*DI));
833 }
834
835 if (DeclS->isSingleDecl())
836 if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(DeclS->getSingleDecl()))
837 PropagationMap.insert(PairType(DeclS, PropagationInfo(Var)));
838}
839
840void ConsumedStmtVisitor::VisitMaterializeTemporaryExpr(
841 const MaterializeTemporaryExpr *Temp) {
842
843 InfoEntry Entry = PropagationMap.find(Temp->GetTemporaryExpr());
844
845 if (Entry != PropagationMap.end())
846 PropagationMap.insert(PairType(Temp, Entry->second));
847}
848
849void ConsumedStmtVisitor::VisitMemberExpr(const MemberExpr *MExpr) {
850 forwardInfo(MExpr->getBase(), MExpr);
851}
852
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000853
854void ConsumedStmtVisitor::VisitParmVarDecl(const ParmVarDecl *Param) {
David Blaikie16f76d22013-09-06 01:28:43 +0000855 QualType ParamType = Param->getType();
856 ConsumedState ParamState = consumed::CS_None;
DeLesley Hutchins69391772013-10-17 23:23:53 +0000857
858 if (Param->hasAttr<ParamTypestateAttr>()) {
859 ParamState =
860 mapParamTypestateAttrState(Param->getAttr<ParamTypestateAttr>());
861
862 } else if (!(ParamType->isPointerType() || ParamType->isReferenceType()) &&
863 isConsumableType(ParamType)) {
864
David Blaikie16f76d22013-09-06 01:28:43 +0000865 ParamState = mapConsumableAttrState(ParamType);
DeLesley Hutchins69391772013-10-17 23:23:53 +0000866
867 } else if (ParamType->isReferenceType() &&
868 isConsumableType(ParamType->getPointeeType())) {
David Blaikie16f76d22013-09-06 01:28:43 +0000869 ParamState = consumed::CS_Unknown;
DeLesley Hutchins69391772013-10-17 23:23:53 +0000870 }
871
872 if (ParamState != CS_None)
David Blaikie16f76d22013-09-06 01:28:43 +0000873 StateMap->setState(Param, ParamState);
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000874}
875
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000876void ConsumedStmtVisitor::VisitReturnStmt(const ReturnStmt *Ret) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000877 ConsumedState ExpectedState = Analyzer.getExpectedReturnState();
878
879 if (ExpectedState != CS_None) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000880 InfoEntry Entry = PropagationMap.find(Ret->getRetValue());
881
882 if (Entry != PropagationMap.end()) {
883 assert(Entry->second.isState() || Entry->second.isVar());
884
885 ConsumedState RetState = Entry->second.isState() ?
886 Entry->second.getState() : StateMap->getState(Entry->second.getVar());
887
888 if (RetState != ExpectedState)
889 Analyzer.WarningsHandler.warnReturnTypestateMismatch(
890 Ret->getReturnLoc(), stateToString(ExpectedState),
891 stateToString(RetState));
892 }
893 }
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000894
895 StateMap->checkParamsForReturnTypestate(Ret->getLocStart(),
896 Analyzer.WarningsHandler);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000897}
898
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000899void ConsumedStmtVisitor::VisitUnaryOperator(const UnaryOperator *UOp) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000900 InfoEntry Entry = PropagationMap.find(UOp->getSubExpr()->IgnoreParens());
901 if (Entry == PropagationMap.end()) return;
902
903 switch (UOp->getOpcode()) {
904 case UO_AddrOf:
905 PropagationMap.insert(PairType(UOp, Entry->second));
906 break;
907
908 case UO_LNot:
909 if (Entry->second.isTest() || Entry->second.isBinTest())
910 PropagationMap.insert(PairType(UOp, Entry->second.invertTest()));
911 break;
912
913 default:
914 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000915 }
916}
917
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000918// TODO: See if I need to check for reference types here.
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000919void ConsumedStmtVisitor::VisitVarDecl(const VarDecl *Var) {
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000920 if (isConsumableType(Var->getType())) {
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000921 if (Var->hasInit()) {
922 PropagationInfo PInfo =
923 PropagationMap.find(Var->getInit())->second;
924
925 StateMap->setState(Var, PInfo.isVar() ?
926 StateMap->getState(PInfo.getVar()) : PInfo.getState());
927
928 } else {
929 StateMap->setState(Var, consumed::CS_Unknown);
930 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000931 }
932}
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000933}} // end clang::consumed::ConsumedStmtVisitor
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000934
935namespace clang {
936namespace consumed {
937
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000938void splitVarStateForIf(const IfStmt * IfNode, const VarTestResult &Test,
939 ConsumedStateMap *ThenStates,
940 ConsumedStateMap *ElseStates) {
941
942 ConsumedState VarState = ThenStates->getState(Test.Var);
943
944 if (VarState == CS_Unknown) {
945 ThenStates->setState(Test.Var, Test.TestsFor);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000946 ElseStates->setState(Test.Var, invertConsumedUnconsumed(Test.TestsFor));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000947
948 } else if (VarState == invertConsumedUnconsumed(Test.TestsFor)) {
949 ThenStates->markUnreachable();
950
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000951 } else if (VarState == Test.TestsFor) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000952 ElseStates->markUnreachable();
953 }
954}
955
956void splitVarStateForIfBinOp(const PropagationInfo &PInfo,
957 ConsumedStateMap *ThenStates, ConsumedStateMap *ElseStates) {
958
959 const VarTestResult &LTest = PInfo.getLTest(),
960 &RTest = PInfo.getRTest();
961
962 ConsumedState LState = LTest.Var ? ThenStates->getState(LTest.Var) : CS_None,
963 RState = RTest.Var ? ThenStates->getState(RTest.Var) : CS_None;
964
965 if (LTest.Var) {
966 if (PInfo.testEffectiveOp() == EO_And) {
967 if (LState == CS_Unknown) {
968 ThenStates->setState(LTest.Var, LTest.TestsFor);
969
970 } else if (LState == invertConsumedUnconsumed(LTest.TestsFor)) {
971 ThenStates->markUnreachable();
972
973 } else if (LState == LTest.TestsFor && isKnownState(RState)) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000974 if (RState == RTest.TestsFor)
975 ElseStates->markUnreachable();
976 else
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000977 ThenStates->markUnreachable();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000978 }
979
980 } else {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000981 if (LState == CS_Unknown) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000982 ElseStates->setState(LTest.Var,
983 invertConsumedUnconsumed(LTest.TestsFor));
984
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000985 } else if (LState == LTest.TestsFor) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000986 ElseStates->markUnreachable();
987
988 } else if (LState == invertConsumedUnconsumed(LTest.TestsFor) &&
989 isKnownState(RState)) {
990
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000991 if (RState == RTest.TestsFor)
992 ElseStates->markUnreachable();
993 else
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000994 ThenStates->markUnreachable();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000995 }
996 }
997 }
998
999 if (RTest.Var) {
1000 if (PInfo.testEffectiveOp() == EO_And) {
1001 if (RState == CS_Unknown)
1002 ThenStates->setState(RTest.Var, RTest.TestsFor);
1003 else if (RState == invertConsumedUnconsumed(RTest.TestsFor))
1004 ThenStates->markUnreachable();
1005
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001006 } else {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001007 if (RState == CS_Unknown)
1008 ElseStates->setState(RTest.Var,
1009 invertConsumedUnconsumed(RTest.TestsFor));
1010 else if (RState == RTest.TestsFor)
1011 ElseStates->markUnreachable();
1012 }
1013 }
1014}
1015
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001016bool ConsumedBlockInfo::allBackEdgesVisited(const CFGBlock *CurrBlock,
1017 const CFGBlock *TargetBlock) {
1018
1019 assert(CurrBlock && "Block pointer must not be NULL");
1020 assert(TargetBlock && "TargetBlock pointer must not be NULL");
1021
1022 unsigned int CurrBlockOrder = VisitOrder[CurrBlock->getBlockID()];
1023 for (CFGBlock::const_pred_iterator PI = TargetBlock->pred_begin(),
1024 PE = TargetBlock->pred_end(); PI != PE; ++PI) {
1025 if (*PI && CurrBlockOrder < VisitOrder[(*PI)->getBlockID()] )
1026 return false;
1027 }
1028 return true;
1029}
1030
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001031void ConsumedBlockInfo::addInfo(const CFGBlock *Block,
1032 ConsumedStateMap *StateMap,
1033 bool &AlreadyOwned) {
1034
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001035 assert(Block && "Block pointer must not be NULL");
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001036
1037 ConsumedStateMap *Entry = StateMapsArray[Block->getBlockID()];
1038
1039 if (Entry) {
1040 Entry->intersect(StateMap);
1041
1042 } else if (AlreadyOwned) {
1043 StateMapsArray[Block->getBlockID()] = new ConsumedStateMap(*StateMap);
1044
1045 } else {
1046 StateMapsArray[Block->getBlockID()] = StateMap;
1047 AlreadyOwned = true;
1048 }
1049}
1050
1051void ConsumedBlockInfo::addInfo(const CFGBlock *Block,
1052 ConsumedStateMap *StateMap) {
1053
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001054 assert(Block != NULL && "Block pointer must not be NULL");
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001055
1056 ConsumedStateMap *Entry = StateMapsArray[Block->getBlockID()];
1057
1058 if (Entry) {
1059 Entry->intersect(StateMap);
1060 delete StateMap;
1061
1062 } else {
1063 StateMapsArray[Block->getBlockID()] = StateMap;
1064 }
1065}
1066
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001067ConsumedStateMap* ConsumedBlockInfo::borrowInfo(const CFGBlock *Block) {
1068 assert(Block && "Block pointer must not be NULL");
1069 assert(StateMapsArray[Block->getBlockID()] && "Block has no block info");
1070
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001071 return StateMapsArray[Block->getBlockID()];
1072}
1073
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001074void ConsumedBlockInfo::discardInfo(const CFGBlock *Block) {
1075 unsigned int BlockID = Block->getBlockID();
1076 delete StateMapsArray[BlockID];
1077 StateMapsArray[BlockID] = NULL;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001078}
1079
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001080ConsumedStateMap* ConsumedBlockInfo::getInfo(const CFGBlock *Block) {
1081 assert(Block && "Block pointer must not be NULL");
1082
1083 ConsumedStateMap *StateMap = StateMapsArray[Block->getBlockID()];
1084 if (isBackEdgeTarget(Block)) {
1085 return new ConsumedStateMap(*StateMap);
1086 } else {
1087 StateMapsArray[Block->getBlockID()] = NULL;
1088 return StateMap;
1089 }
1090}
1091
1092bool ConsumedBlockInfo::isBackEdge(const CFGBlock *From, const CFGBlock *To) {
1093 assert(From && "From block must not be NULL");
1094 assert(To && "From block must not be NULL");
1095
1096 return VisitOrder[From->getBlockID()] > VisitOrder[To->getBlockID()];
1097}
1098
1099bool ConsumedBlockInfo::isBackEdgeTarget(const CFGBlock *Block) {
1100 assert(Block != NULL && "Block pointer must not be NULL");
1101
1102 // Anything with less than two predecessors can't be the target of a back
1103 // edge.
1104 if (Block->pred_size() < 2)
1105 return false;
1106
1107 unsigned int BlockVisitOrder = VisitOrder[Block->getBlockID()];
1108 for (CFGBlock::const_pred_iterator PI = Block->pred_begin(),
1109 PE = Block->pred_end(); PI != PE; ++PI) {
1110 if (*PI && BlockVisitOrder < VisitOrder[(*PI)->getBlockID()])
1111 return true;
1112 }
1113 return false;
1114}
1115
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001116void ConsumedStateMap::checkParamsForReturnTypestate(SourceLocation BlameLoc,
1117 ConsumedWarningsHandlerBase &WarningsHandler) const {
1118
1119 ConsumedState ExpectedState;
1120
1121 for (MapType::const_iterator DMI = Map.begin(), DME = Map.end(); DMI != DME;
1122 ++DMI) {
1123
1124 if (isa<ParmVarDecl>(DMI->first)) {
1125 const ParmVarDecl *Param = cast<ParmVarDecl>(DMI->first);
1126
1127 if (!Param->hasAttr<ReturnTypestateAttr>()) continue;
1128
1129 ExpectedState =
1130 mapReturnTypestateAttrState(Param->getAttr<ReturnTypestateAttr>());
1131
1132 if (DMI->second != ExpectedState) {
1133 WarningsHandler.warnParamReturnTypestateMismatch(BlameLoc,
1134 Param->getNameAsString(), stateToString(ExpectedState),
1135 stateToString(DMI->second));
1136 }
1137 }
1138 }
1139}
1140
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001141ConsumedState ConsumedStateMap::getState(const VarDecl *Var) const {
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001142 MapType::const_iterator Entry = Map.find(Var);
1143
1144 if (Entry != Map.end()) {
1145 return Entry->second;
1146
1147 } else {
1148 return CS_None;
1149 }
1150}
1151
1152void ConsumedStateMap::intersect(const ConsumedStateMap *Other) {
1153 ConsumedState LocalState;
1154
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001155 if (this->From && this->From == Other->From && !Other->Reachable) {
1156 this->markUnreachable();
1157 return;
1158 }
1159
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001160 for (MapType::const_iterator DMI = Other->Map.begin(), DME = Other->Map.end();
1161 DMI != DME; ++DMI) {
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001162
1163 LocalState = this->getState(DMI->first);
1164
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001165 if (LocalState == CS_None)
1166 continue;
1167
1168 if (LocalState != DMI->second)
1169 Map[DMI->first] = CS_Unknown;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001170 }
1171}
1172
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001173void ConsumedStateMap::intersectAtLoopHead(const CFGBlock *LoopHead,
1174 const CFGBlock *LoopBack, const ConsumedStateMap *LoopBackStates,
1175 ConsumedWarningsHandlerBase &WarningsHandler) {
1176
1177 ConsumedState LocalState;
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001178 SourceLocation BlameLoc = getLastStmtLoc(LoopBack);
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001179
1180 for (MapType::const_iterator DMI = LoopBackStates->Map.begin(),
1181 DME = LoopBackStates->Map.end(); DMI != DME; ++DMI) {
1182
1183 LocalState = this->getState(DMI->first);
1184
1185 if (LocalState == CS_None)
1186 continue;
1187
1188 if (LocalState != DMI->second) {
1189 Map[DMI->first] = CS_Unknown;
1190 WarningsHandler.warnLoopStateMismatch(
1191 BlameLoc, DMI->first->getNameAsString());
1192 }
1193 }
1194}
1195
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001196void ConsumedStateMap::markUnreachable() {
1197 this->Reachable = false;
1198 Map.clear();
1199}
1200
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001201void ConsumedStateMap::setState(const VarDecl *Var, ConsumedState State) {
1202 Map[Var] = State;
1203}
1204
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001205void ConsumedStateMap::remove(const VarDecl *Var) {
1206 Map.erase(Var);
1207}
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001208
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001209bool ConsumedStateMap::operator!=(const ConsumedStateMap *Other) const {
1210 for (MapType::const_iterator DMI = Other->Map.begin(), DME = Other->Map.end();
1211 DMI != DME; ++DMI) {
1212
1213 if (this->getState(DMI->first) != DMI->second)
1214 return true;
1215 }
1216
1217 return false;
1218}
1219
David Blaikie16f76d22013-09-06 01:28:43 +00001220void ConsumedAnalyzer::determineExpectedReturnState(AnalysisDeclContext &AC,
1221 const FunctionDecl *D) {
1222 QualType ReturnType;
1223 if (const CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1224 ASTContext &CurrContext = AC.getASTContext();
1225 ReturnType = Constructor->getThisType(CurrContext)->getPointeeType();
1226 } else
1227 ReturnType = D->getCallResultType();
1228
1229 if (D->hasAttr<ReturnTypestateAttr>()) {
1230 const ReturnTypestateAttr *RTSAttr = D->getAttr<ReturnTypestateAttr>();
1231
1232 const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1233 if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1234 // FIXME: This should be removed when template instantiation propagates
1235 // attributes at template specialization definition, not
1236 // declaration. When it is removed the test needs to be enabled
1237 // in SemaDeclAttr.cpp.
1238 WarningsHandler.warnReturnTypestateForUnconsumableType(
1239 RTSAttr->getLocation(), ReturnType.getAsString());
1240 ExpectedReturnState = CS_None;
1241 } else
1242 ExpectedReturnState = mapReturnTypestateAttrState(RTSAttr);
1243 } else if (isConsumableType(ReturnType))
1244 ExpectedReturnState = mapConsumableAttrState(ReturnType);
1245 else
1246 ExpectedReturnState = CS_None;
1247}
1248
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001249bool ConsumedAnalyzer::splitState(const CFGBlock *CurrBlock,
1250 const ConsumedStmtVisitor &Visitor) {
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001251
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001252 ConsumedStateMap *FalseStates = new ConsumedStateMap(*CurrStates);
1253 PropagationInfo PInfo;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001254
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001255 if (const IfStmt *IfNode =
1256 dyn_cast_or_null<IfStmt>(CurrBlock->getTerminator().getStmt())) {
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001257
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001258 const Stmt *Cond = IfNode->getCond();
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001259
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001260 PInfo = Visitor.getInfo(Cond);
1261 if (!PInfo.isValid() && isa<BinaryOperator>(Cond))
1262 PInfo = Visitor.getInfo(cast<BinaryOperator>(Cond)->getRHS());
1263
1264 if (PInfo.isTest()) {
1265 CurrStates->setSource(Cond);
1266 FalseStates->setSource(Cond);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001267 splitVarStateForIf(IfNode, PInfo.getTest(), CurrStates, FalseStates);
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001268
1269 } else if (PInfo.isBinTest()) {
1270 CurrStates->setSource(PInfo.testSourceNode());
1271 FalseStates->setSource(PInfo.testSourceNode());
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001272 splitVarStateForIfBinOp(PInfo, CurrStates, FalseStates);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001273
1274 } else {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001275 delete FalseStates;
1276 return false;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001277 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001278
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001279 } else if (const BinaryOperator *BinOp =
1280 dyn_cast_or_null<BinaryOperator>(CurrBlock->getTerminator().getStmt())) {
1281
1282 PInfo = Visitor.getInfo(BinOp->getLHS());
1283 if (!PInfo.isTest()) {
1284 if ((BinOp = dyn_cast_or_null<BinaryOperator>(BinOp->getLHS()))) {
1285 PInfo = Visitor.getInfo(BinOp->getRHS());
1286
1287 if (!PInfo.isTest()) {
1288 delete FalseStates;
1289 return false;
1290 }
1291
1292 } else {
1293 delete FalseStates;
1294 return false;
1295 }
1296 }
1297
1298 CurrStates->setSource(BinOp);
1299 FalseStates->setSource(BinOp);
1300
1301 const VarTestResult &Test = PInfo.getTest();
1302 ConsumedState VarState = CurrStates->getState(Test.Var);
1303
1304 if (BinOp->getOpcode() == BO_LAnd) {
1305 if (VarState == CS_Unknown)
1306 CurrStates->setState(Test.Var, Test.TestsFor);
1307 else if (VarState == invertConsumedUnconsumed(Test.TestsFor))
1308 CurrStates->markUnreachable();
1309
1310 } else if (BinOp->getOpcode() == BO_LOr) {
1311 if (VarState == CS_Unknown)
1312 FalseStates->setState(Test.Var,
1313 invertConsumedUnconsumed(Test.TestsFor));
1314 else if (VarState == Test.TestsFor)
1315 FalseStates->markUnreachable();
1316 }
1317
1318 } else {
1319 delete FalseStates;
1320 return false;
1321 }
1322
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001323 CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin();
1324
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001325 if (*SI)
1326 BlockInfo.addInfo(*SI, CurrStates);
1327 else
1328 delete CurrStates;
1329
1330 if (*++SI)
1331 BlockInfo.addInfo(*SI, FalseStates);
1332 else
1333 delete FalseStates;
1334
1335 CurrStates = NULL;
1336 return true;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001337}
1338
1339void ConsumedAnalyzer::run(AnalysisDeclContext &AC) {
1340 const FunctionDecl *D = dyn_cast_or_null<FunctionDecl>(AC.getDecl());
DeLesley Hutchins85c07d92013-09-10 23:10:10 +00001341 if (!D)
1342 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001343
DeLesley Hutchins85c07d92013-09-10 23:10:10 +00001344 CFG *CFGraph = AC.getCFG();
1345 if (!CFGraph)
1346 return;
DeLesley Hutchins65013202013-10-17 18:19:31 +00001347
David Blaikie16f76d22013-09-06 01:28:43 +00001348 determineExpectedReturnState(AC, D);
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001349
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001350 PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>();
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001351 // AC.getCFG()->viewCFG(LangOptions());
1352
1353 BlockInfo = ConsumedBlockInfo(CFGraph->getNumBlockIDs(), SortedGraph);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001354
1355 CurrStates = new ConsumedStateMap();
DeLesley Hutchinsb570c132013-08-29 22:36:05 +00001356 ConsumedStmtVisitor Visitor(AC, *this, CurrStates);
1357
1358 // Add all trackable parameters to the state map.
1359 for (FunctionDecl::param_const_iterator PI = D->param_begin(),
1360 PE = D->param_end(); PI != PE; ++PI) {
1361 Visitor.VisitParmVarDecl(*PI);
1362 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001363
1364 // Visit all of the function's basic blocks.
1365 for (PostOrderCFGView::iterator I = SortedGraph->begin(),
1366 E = SortedGraph->end(); I != E; ++I) {
1367
1368 const CFGBlock *CurrBlock = *I;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001369
1370 if (CurrStates == NULL)
1371 CurrStates = BlockInfo.getInfo(CurrBlock);
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001372
1373 if (!CurrStates) {
1374 continue;
1375
1376 } else if (!CurrStates->isReachable()) {
1377 delete CurrStates;
1378 CurrStates = NULL;
1379 continue;
1380 }
1381
1382 Visitor.reset(CurrStates);
1383
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001384 // Visit all of the basic block's statements.
1385 for (CFGBlock::const_iterator BI = CurrBlock->begin(),
1386 BE = CurrBlock->end(); BI != BE; ++BI) {
1387
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001388 switch (BI->getKind()) {
1389 case CFGElement::Statement:
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001390 Visitor.Visit(BI->castAs<CFGStmt>().getStmt());
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001391 break;
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001392
1393 case CFGElement::TemporaryDtor: {
1394 const CFGTemporaryDtor DTor = BI->castAs<CFGTemporaryDtor>();
1395 const CXXBindTemporaryExpr *BTE = DTor.getBindTemporaryExpr();
1396 PropagationInfo PInfo = Visitor.getInfo(BTE);
1397
1398 if (PInfo.isValid())
1399 Visitor.checkCallability(PInfo,
1400 DTor.getDestructorDecl(AC.getASTContext()),
1401 BTE->getExprLoc());
1402 break;
1403 }
1404
1405 case CFGElement::AutomaticObjectDtor: {
1406 const CFGAutomaticObjDtor DTor = BI->castAs<CFGAutomaticObjDtor>();
1407
1408 const VarDecl *Var = DTor.getVarDecl();
1409 ConsumedState VarState = CurrStates->getState(Var);
1410
1411 if (VarState != CS_None) {
1412 PropagationInfo PInfo(Var);
1413
1414 Visitor.checkCallability(PInfo,
1415 DTor.getDestructorDecl(AC.getASTContext()),
1416 getLastStmtLoc(CurrBlock));
1417
1418 CurrStates->remove(Var);
1419 }
1420 break;
1421 }
1422
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001423 default:
1424 break;
1425 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001426 }
1427
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001428 // TODO: Handle other forms of branching with precision, including while-
1429 // and for-loops. (Deferred)
1430 if (!splitState(CurrBlock, Visitor)) {
1431 CurrStates->setSource(NULL);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001432
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001433 if (CurrBlock->succ_size() > 1 ||
1434 (CurrBlock->succ_size() == 1 &&
1435 (*CurrBlock->succ_begin())->pred_size() > 1)) {
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001436
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001437 bool OwnershipTaken = false;
1438
1439 for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
1440 SE = CurrBlock->succ_end(); SI != SE; ++SI) {
1441
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001442 if (*SI == NULL) continue;
1443
1444 if (BlockInfo.isBackEdge(CurrBlock, *SI)) {
1445 BlockInfo.borrowInfo(*SI)->intersectAtLoopHead(*SI, CurrBlock,
1446 CurrStates,
1447 WarningsHandler);
1448
1449 if (BlockInfo.allBackEdgesVisited(*SI, CurrBlock))
1450 BlockInfo.discardInfo(*SI);
1451 } else {
1452 BlockInfo.addInfo(*SI, CurrStates, OwnershipTaken);
1453 }
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001454 }
1455
1456 if (!OwnershipTaken)
1457 delete CurrStates;
1458
1459 CurrStates = NULL;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001460 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001461 }
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001462
1463 if (CurrBlock == &AC.getCFG()->getExit() &&
1464 D->getCallResultType()->isVoidType())
1465 CurrStates->checkParamsForReturnTypestate(D->getLocation(),
1466 WarningsHandler);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001467 } // End of block iterator.
1468
1469 // Delete the last existing state map.
1470 delete CurrStates;
1471
1472 WarningsHandler.emitDiagnostics();
1473}
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001474}} // end namespace clang::consumed