blob: aac5658d565e4bef084ea53f3ce917651cdafe32 [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"
DeLesley Hutchins48a31762013-08-12 21:20:55 +000020#include "clang/AST/StmtCXX.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000021#include "clang/AST/StmtVisitor.h"
DeLesley Hutchins48a31762013-08-12 21:20:55 +000022#include "clang/AST/Type.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000023#include "clang/Analysis/Analyses/Consumed.h"
DeLesley Hutchins48a31762013-08-12 21:20:55 +000024#include "clang/Analysis/Analyses/PostOrderCFGView.h"
25#include "clang/Analysis/AnalysisContext.h"
26#include "clang/Analysis/CFG.h"
DeLesley Hutchins48a31762013-08-12 21:20:55 +000027#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"
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000033#include <memory>
DeLesley Hutchins48a31762013-08-12 21:20:55 +000034
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +000035// TODO: Adjust states of args to constructors in the same way that arguments to
36// function calls are handled.
37// TODO: Use information from tests in for- and while-loop conditional.
DeLesley Hutchinsfc368252013-09-03 20:11:38 +000038// TODO: Add notes about the actual and expected state for
DeLesley Hutchins5533ec52013-08-29 17:26:57 +000039// TODO: Correctly identify unreachable blocks when chaining boolean operators.
DeLesley Hutchins210791a2013-10-04 21:28:06 +000040// TODO: Adjust the parser and AttributesList class to support lists of
41// identifiers.
DeLesley Hutchins5533ec52013-08-29 17:26:57 +000042// TODO: Warn about unreachable code.
43// TODO: Switch to using a bitmap to track unreachable blocks.
DeLesley Hutchins5533ec52013-08-29 17:26:57 +000044// TODO: Handle variable definitions, e.g. bool valid = x.isValid();
45// if (valid) ...; (Deferred)
DeLesley Hutchins48a31762013-08-12 21:20:55 +000046// TODO: Take notes on state transitions to provide better warning messages.
47// (Deferred)
48// TODO: Test nested conditionals: A) Checking the same value multiple times,
49// and 2) Checking different values. (Deferred)
DeLesley Hutchins48a31762013-08-12 21:20:55 +000050
51using namespace clang;
52using namespace consumed;
53
54// Key method definition
55ConsumedWarningsHandlerBase::~ConsumedWarningsHandlerBase() {}
56
DeLesley Hutchins65013202013-10-17 18:19:31 +000057static SourceLocation getFirstStmtLoc(const CFGBlock *Block) {
58 // Find the source location of the first statement in the block, if the block
59 // is not empty.
Aaron Ballman35897d92014-04-28 14:56:59 +000060 for (const auto &B : *Block)
61 if (Optional<CFGStmt> CS = B.getAs<CFGStmt>())
DeLesley Hutchins65013202013-10-17 18:19:31 +000062 return CS->getStmt()->getLocStart();
DeLesley Hutchins65013202013-10-17 18:19:31 +000063
64 // Block is empty.
65 // If we have one successor, return the first statement in that block
66 if (Block->succ_size() == 1 && *Block->succ_begin())
67 return getFirstStmtLoc(*Block->succ_begin());
68
69 return SourceLocation();
70}
71
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +000072static SourceLocation getLastStmtLoc(const CFGBlock *Block) {
DeLesley Hutchins3277a612013-10-09 18:30:24 +000073 // Find the source location of the last statement in the block, if the block
74 // is not empty.
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +000075 if (const Stmt *StmtNode = Block->getTerminator()) {
DeLesley Hutchins3277a612013-10-09 18:30:24 +000076 return StmtNode->getLocStart();
77 } else {
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +000078 for (CFGBlock::const_reverse_iterator BI = Block->rbegin(),
79 BE = Block->rend(); BI != BE; ++BI) {
DeLesley Hutchins3277a612013-10-09 18:30:24 +000080 if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>())
81 return CS->getStmt()->getLocStart();
82 }
83 }
DeLesley Hutchins65013202013-10-17 18:19:31 +000084
85 // If we have one successor, return the first statement in that block
86 SourceLocation Loc;
87 if (Block->succ_size() == 1 && *Block->succ_begin())
88 Loc = getFirstStmtLoc(*Block->succ_begin());
89 if (Loc.isValid())
90 return Loc;
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +000091
DeLesley Hutchins65013202013-10-17 18:19:31 +000092 // If we have one predecessor, return the last statement in that block
93 if (Block->pred_size() == 1 && *Block->pred_begin())
94 return getLastStmtLoc(*Block->pred_begin());
95
96 return Loc;
DeLesley Hutchins3277a612013-10-09 18:30:24 +000097}
98
DeLesley Hutchins5533ec52013-08-29 17:26:57 +000099static ConsumedState invertConsumedUnconsumed(ConsumedState State) {
100 switch (State) {
101 case CS_Unconsumed:
102 return CS_Consumed;
103 case CS_Consumed:
104 return CS_Unconsumed;
105 case CS_None:
106 return CS_None;
107 case CS_Unknown:
108 return CS_Unknown;
109 }
110 llvm_unreachable("invalid enum");
111}
112
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000113static bool isCallableInState(const CallableWhenAttr *CWAttr,
114 ConsumedState State) {
115
116 CallableWhenAttr::callableState_iterator I = CWAttr->callableState_begin(),
117 E = CWAttr->callableState_end();
118
119 for (; I != E; ++I) {
120
121 ConsumedState MappedAttrState = CS_None;
122
123 switch (*I) {
124 case CallableWhenAttr::Unknown:
125 MappedAttrState = CS_Unknown;
126 break;
127
128 case CallableWhenAttr::Unconsumed:
129 MappedAttrState = CS_Unconsumed;
130 break;
131
132 case CallableWhenAttr::Consumed:
133 MappedAttrState = CS_Consumed;
134 break;
135 }
136
137 if (MappedAttrState == State)
138 return true;
139 }
140
141 return false;
142}
143
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000144
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000145static bool isConsumableType(const QualType &QT) {
Chris Wailes93edffa2013-10-31 15:38:12 +0000146 if (QT->isPointerType() || QT->isReferenceType())
147 return false;
148
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000149 if (const CXXRecordDecl *RD = QT->getAsCXXRecordDecl())
150 return RD->hasAttr<ConsumableAttr>();
Chris Wailes93edffa2013-10-31 15:38:12 +0000151
152 return false;
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000153}
154
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000155static bool isAutoCastType(const QualType &QT) {
156 if (QT->isPointerType() || QT->isReferenceType())
157 return false;
158
159 if (const CXXRecordDecl *RD = QT->getAsCXXRecordDecl())
160 return RD->hasAttr<ConsumableAutoCastAttr>();
161
162 return false;
163}
164
165static bool isSetOnReadPtrType(const QualType &QT) {
166 if (const CXXRecordDecl *RD = QT->getPointeeCXXRecordDecl())
167 return RD->hasAttr<ConsumableSetOnReadAttr>();
168 return false;
169}
170
171
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000172static bool isKnownState(ConsumedState State) {
173 switch (State) {
174 case CS_Unconsumed:
175 case CS_Consumed:
176 return true;
177 case CS_None:
178 case CS_Unknown:
179 return false;
180 }
Aaron Ballmana21f4b82013-08-29 20:36:09 +0000181 llvm_unreachable("invalid enum");
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000182}
183
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000184static bool isRValueRef(QualType ParamType) {
185 return ParamType->isRValueReferenceType();
DeLesley Hutchins0bd25892013-10-18 19:25:18 +0000186}
187
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000188static bool isTestingFunction(const FunctionDecl *FunDecl) {
Chris Wailes9385f9f2013-10-29 20:28:41 +0000189 return FunDecl->hasAttr<TestTypestateAttr>();
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000190}
191
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000192static bool isPointerOrRef(QualType ParamType) {
193 return ParamType->isPointerType() || ParamType->isReferenceType();
DeLesley Hutchins0bd25892013-10-18 19:25:18 +0000194}
195
David Blaikie16f76d22013-09-06 01:28:43 +0000196static ConsumedState mapConsumableAttrState(const QualType QT) {
197 assert(isConsumableType(QT));
198
199 const ConsumableAttr *CAttr =
200 QT->getAsCXXRecordDecl()->getAttr<ConsumableAttr>();
201
202 switch (CAttr->getDefaultState()) {
203 case ConsumableAttr::Unknown:
204 return CS_Unknown;
205 case ConsumableAttr::Unconsumed:
206 return CS_Unconsumed;
207 case ConsumableAttr::Consumed:
208 return CS_Consumed;
209 }
210 llvm_unreachable("invalid enum");
211}
212
DeLesley Hutchins69391772013-10-17 23:23:53 +0000213static ConsumedState
214mapParamTypestateAttrState(const ParamTypestateAttr *PTAttr) {
215 switch (PTAttr->getParamState()) {
216 case ParamTypestateAttr::Unknown:
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000217 return CS_Unknown;
DeLesley Hutchins69391772013-10-17 23:23:53 +0000218 case ParamTypestateAttr::Unconsumed:
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000219 return CS_Unconsumed;
DeLesley Hutchins69391772013-10-17 23:23:53 +0000220 case ParamTypestateAttr::Consumed:
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000221 return CS_Consumed;
222 }
223 llvm_unreachable("invalid_enum");
224}
225
Eric Christopherde156242013-09-03 20:43:00 +0000226static ConsumedState
227mapReturnTypestateAttrState(const ReturnTypestateAttr *RTSAttr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000228 switch (RTSAttr->getState()) {
229 case ReturnTypestateAttr::Unknown:
230 return CS_Unknown;
231 case ReturnTypestateAttr::Unconsumed:
232 return CS_Unconsumed;
233 case ReturnTypestateAttr::Consumed:
234 return CS_Consumed;
235 }
Eric Christopherde156242013-09-03 20:43:00 +0000236 llvm_unreachable("invalid enum");
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000237}
238
DeLesley Hutchins69391772013-10-17 23:23:53 +0000239static ConsumedState mapSetTypestateAttrState(const SetTypestateAttr *STAttr) {
240 switch (STAttr->getNewState()) {
241 case SetTypestateAttr::Unknown:
242 return CS_Unknown;
243 case SetTypestateAttr::Unconsumed:
244 return CS_Unconsumed;
245 case SetTypestateAttr::Consumed:
246 return CS_Consumed;
247 }
248 llvm_unreachable("invalid_enum");
249}
250
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000251static StringRef stateToString(ConsumedState State) {
252 switch (State) {
253 case consumed::CS_None:
254 return "none";
255
256 case consumed::CS_Unknown:
257 return "unknown";
258
259 case consumed::CS_Unconsumed:
260 return "unconsumed";
261
262 case consumed::CS_Consumed:
263 return "consumed";
264 }
Reid Kleckner6454d0a2013-08-13 00:11:59 +0000265 llvm_unreachable("invalid enum");
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000266}
267
DeLesley Hutchins8d41d992013-10-11 22:30:48 +0000268static ConsumedState testsFor(const FunctionDecl *FunDecl) {
269 assert(isTestingFunction(FunDecl));
Chris Wailes9385f9f2013-10-29 20:28:41 +0000270 switch (FunDecl->getAttr<TestTypestateAttr>()->getTestState()) {
271 case TestTypestateAttr::Unconsumed:
DeLesley Hutchins8d41d992013-10-11 22:30:48 +0000272 return CS_Unconsumed;
Chris Wailes9385f9f2013-10-29 20:28:41 +0000273 case TestTypestateAttr::Consumed:
DeLesley Hutchins8d41d992013-10-11 22:30:48 +0000274 return CS_Consumed;
275 }
276 llvm_unreachable("invalid enum");
277}
278
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000279namespace {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000280struct VarTestResult {
281 const VarDecl *Var;
282 ConsumedState TestsFor;
283};
284} // end anonymous::VarTestResult
285
286namespace clang {
287namespace consumed {
288
289enum EffectiveOp {
290 EO_And,
291 EO_Or
292};
293
294class PropagationInfo {
295 enum {
296 IT_None,
297 IT_State,
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000298 IT_VarTest,
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000299 IT_BinTest,
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000300 IT_Var,
301 IT_Tmp
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000302 } InfoType;
Eric Christopherf8a1baa2013-08-29 18:00:58 +0000303
304 struct BinTestTy {
305 const BinaryOperator *Source;
306 EffectiveOp EOp;
307 VarTestResult LTest;
308 VarTestResult RTest;
309 };
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000310
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000311 union {
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000312 ConsumedState State;
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000313 VarTestResult VarTest;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000314 const VarDecl *Var;
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000315 const CXXBindTemporaryExpr *Tmp;
Eric Christopherf8a1baa2013-08-29 18:00:58 +0000316 BinTestTy BinTest;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000317 };
318
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000319public:
320 PropagationInfo() : InfoType(IT_None) {}
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000321
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000322 PropagationInfo(const VarTestResult &VarTest)
323 : InfoType(IT_VarTest), VarTest(VarTest) {}
324
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000325 PropagationInfo(const VarDecl *Var, ConsumedState TestsFor)
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000326 : InfoType(IT_VarTest) {
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000327
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000328 VarTest.Var = Var;
329 VarTest.TestsFor = TestsFor;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000330 }
331
332 PropagationInfo(const BinaryOperator *Source, EffectiveOp EOp,
333 const VarTestResult &LTest, const VarTestResult &RTest)
334 : InfoType(IT_BinTest) {
335
336 BinTest.Source = Source;
337 BinTest.EOp = EOp;
338 BinTest.LTest = LTest;
339 BinTest.RTest = RTest;
340 }
341
342 PropagationInfo(const BinaryOperator *Source, EffectiveOp EOp,
343 const VarDecl *LVar, ConsumedState LTestsFor,
344 const VarDecl *RVar, ConsumedState RTestsFor)
345 : InfoType(IT_BinTest) {
346
347 BinTest.Source = Source;
348 BinTest.EOp = EOp;
349 BinTest.LTest.Var = LVar;
350 BinTest.LTest.TestsFor = LTestsFor;
351 BinTest.RTest.Var = RVar;
352 BinTest.RTest.TestsFor = RTestsFor;
353 }
354
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000355 PropagationInfo(ConsumedState State)
356 : InfoType(IT_State), State(State) {}
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000357
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000358 PropagationInfo(const VarDecl *Var) : InfoType(IT_Var), Var(Var) {}
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000359 PropagationInfo(const CXXBindTemporaryExpr *Tmp)
360 : InfoType(IT_Tmp), Tmp(Tmp) {}
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000361
362 const ConsumedState & getState() const {
363 assert(InfoType == IT_State);
364 return State;
365 }
366
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000367 const VarTestResult & getVarTest() const {
368 assert(InfoType == IT_VarTest);
369 return VarTest;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000370 }
371
372 const VarTestResult & getLTest() const {
373 assert(InfoType == IT_BinTest);
374 return BinTest.LTest;
375 }
376
377 const VarTestResult & getRTest() const {
378 assert(InfoType == IT_BinTest);
379 return BinTest.RTest;
380 }
381
382 const VarDecl * getVar() const {
383 assert(InfoType == IT_Var);
384 return Var;
385 }
386
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000387 const CXXBindTemporaryExpr * getTmp() const {
388 assert(InfoType == IT_Tmp);
389 return Tmp;
390 }
391
392 ConsumedState getAsState(const ConsumedStateMap *StateMap) const {
393 assert(isVar() || isTmp() || isState());
394
395 if (isVar())
396 return StateMap->getState(Var);
397 else if (isTmp())
398 return StateMap->getState(Tmp);
399 else if (isState())
400 return State;
401 else
402 return CS_None;
403 }
404
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000405 EffectiveOp testEffectiveOp() const {
406 assert(InfoType == IT_BinTest);
407 return BinTest.EOp;
408 }
409
410 const BinaryOperator * testSourceNode() const {
411 assert(InfoType == IT_BinTest);
412 return BinTest.Source;
413 }
414
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000415 inline bool isValid() const { return InfoType != IT_None; }
416 inline bool isState() const { return InfoType == IT_State; }
417 inline bool isVarTest() const { return InfoType == IT_VarTest; }
418 inline bool isBinTest() const { return InfoType == IT_BinTest; }
419 inline bool isVar() const { return InfoType == IT_Var; }
420 inline bool isTmp() const { return InfoType == IT_Tmp; }
421
422 bool isTest() const {
423 return InfoType == IT_VarTest || InfoType == IT_BinTest;
424 }
425
426 bool isPointerToValue() const {
427 return InfoType == IT_Var || InfoType == IT_Tmp;
428 }
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000429
430 PropagationInfo invertTest() const {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000431 assert(InfoType == IT_VarTest || InfoType == IT_BinTest);
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000432
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000433 if (InfoType == IT_VarTest) {
434 return PropagationInfo(VarTest.Var,
435 invertConsumedUnconsumed(VarTest.TestsFor));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000436
437 } else if (InfoType == IT_BinTest) {
438 return PropagationInfo(BinTest.Source,
439 BinTest.EOp == EO_And ? EO_Or : EO_And,
440 BinTest.LTest.Var, invertConsumedUnconsumed(BinTest.LTest.TestsFor),
441 BinTest.RTest.Var, invertConsumedUnconsumed(BinTest.RTest.TestsFor));
442 } else {
443 return PropagationInfo();
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000444 }
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000445 }
446};
447
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000448static inline void
449setStateForVarOrTmp(ConsumedStateMap *StateMap, const PropagationInfo &PInfo,
450 ConsumedState State) {
451
452 assert(PInfo.isVar() || PInfo.isTmp());
453
454 if (PInfo.isVar())
455 StateMap->setState(PInfo.getVar(), State);
456 else
457 StateMap->setState(PInfo.getTmp(), State);
458}
459
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000460class ConsumedStmtVisitor : public ConstStmtVisitor<ConsumedStmtVisitor> {
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000461
462 typedef llvm::DenseMap<const Stmt *, PropagationInfo> MapType;
463 typedef std::pair<const Stmt *, PropagationInfo> PairType;
464 typedef MapType::iterator InfoEntry;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000465 typedef MapType::const_iterator ConstInfoEntry;
466
Reid Klecknere846dea2013-08-12 23:49:39 +0000467 AnalysisDeclContext &AC;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000468 ConsumedAnalyzer &Analyzer;
469 ConsumedStateMap *StateMap;
470 MapType PropagationMap;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000471
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000472 InfoEntry findInfo(const Expr *E) {
473 return PropagationMap.find(E->IgnoreParens());
474 }
475 ConstInfoEntry findInfo(const Expr *E) const {
476 return PropagationMap.find(E->IgnoreParens());
477 }
478 void insertInfo(const Expr *E, const PropagationInfo &PI) {
479 PropagationMap.insert(PairType(E->IgnoreParens(), PI));
480 }
481
482 void forwardInfo(const Expr *From, const Expr *To);
483 void copyInfo(const Expr *From, const Expr *To, ConsumedState CS);
484 ConsumedState getInfo(const Expr *From);
485 void setInfo(const Expr *To, ConsumedState NS);
486 void propagateReturnType(const Expr *Call, const FunctionDecl *Fun);
DeLesley Hutchins81218662013-10-18 23:11:49 +0000487
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000488public:
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000489 void checkCallability(const PropagationInfo &PInfo,
490 const FunctionDecl *FunDecl,
491 SourceLocation BlameLoc);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000492 bool handleCall(const CallExpr *Call, const Expr *ObjArg,
493 const FunctionDecl *FunD);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000494
495 void VisitBinaryOperator(const BinaryOperator *BinOp);
496 void VisitCallExpr(const CallExpr *Call);
497 void VisitCastExpr(const CastExpr *Cast);
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000498 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Temp);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000499 void VisitCXXConstructExpr(const CXXConstructExpr *Call);
500 void VisitCXXMemberCallExpr(const CXXMemberCallExpr *Call);
501 void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *Call);
502 void VisitDeclRefExpr(const DeclRefExpr *DeclRef);
503 void VisitDeclStmt(const DeclStmt *DelcS);
504 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Temp);
505 void VisitMemberExpr(const MemberExpr *MExpr);
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000506 void VisitParmVarDecl(const ParmVarDecl *Param);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000507 void VisitReturnStmt(const ReturnStmt *Ret);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000508 void VisitUnaryOperator(const UnaryOperator *UOp);
509 void VisitVarDecl(const VarDecl *Var);
Reid Klecknere846dea2013-08-12 23:49:39 +0000510
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000511 ConsumedStmtVisitor(AnalysisDeclContext &AC, ConsumedAnalyzer &Analyzer,
512 ConsumedStateMap *StateMap)
513 : AC(AC), Analyzer(Analyzer), StateMap(StateMap) {}
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000514
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000515 PropagationInfo getInfo(const Expr *StmtNode) const {
516 ConstInfoEntry Entry = findInfo(StmtNode);
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000517
518 if (Entry != PropagationMap.end())
519 return Entry->second;
520 else
521 return PropagationInfo();
522 }
523
524 void reset(ConsumedStateMap *NewStateMap) {
525 StateMap = NewStateMap;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000526 }
527};
528
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000529
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000530void ConsumedStmtVisitor::forwardInfo(const Expr *From, const Expr *To) {
531 InfoEntry Entry = findInfo(From);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000532 if (Entry != PropagationMap.end())
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000533 insertInfo(To, Entry->second);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000534}
535
536
537// Create a new state for To, which is initialized to the state of From.
538// If NS is not CS_None, sets the state of From to NS.
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000539void ConsumedStmtVisitor::copyInfo(const Expr *From, const Expr *To,
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000540 ConsumedState NS) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000541 InfoEntry Entry = findInfo(From);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000542 if (Entry != PropagationMap.end()) {
543 PropagationInfo& PInfo = Entry->second;
544 ConsumedState CS = PInfo.getAsState(StateMap);
545 if (CS != CS_None)
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000546 insertInfo(To, PropagationInfo(CS));
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000547 if (NS != CS_None && PInfo.isPointerToValue())
548 setStateForVarOrTmp(StateMap, PInfo, NS);
549 }
550}
551
552
553// Get the ConsumedState for From
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000554ConsumedState ConsumedStmtVisitor::getInfo(const Expr *From) {
555 InfoEntry Entry = findInfo(From);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000556 if (Entry != PropagationMap.end()) {
557 PropagationInfo& PInfo = Entry->second;
558 return PInfo.getAsState(StateMap);
559 }
560 return CS_None;
561}
562
563
564// If we already have info for To then update it, otherwise create a new entry.
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000565void ConsumedStmtVisitor::setInfo(const Expr *To, ConsumedState NS) {
566 InfoEntry Entry = findInfo(To);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000567 if (Entry != PropagationMap.end()) {
568 PropagationInfo& PInfo = Entry->second;
569 if (PInfo.isPointerToValue())
570 setStateForVarOrTmp(StateMap, PInfo, NS);
571 } else if (NS != CS_None) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000572 insertInfo(To, PropagationInfo(NS));
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000573 }
574}
575
576
577
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000578void ConsumedStmtVisitor::checkCallability(const PropagationInfo &PInfo,
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000579 const FunctionDecl *FunDecl,
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000580 SourceLocation BlameLoc) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000581 assert(!PInfo.isTest());
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000582
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000583 const CallableWhenAttr *CWAttr = FunDecl->getAttr<CallableWhenAttr>();
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000584 if (!CWAttr)
585 return;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000586
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000587 if (PInfo.isVar()) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000588 ConsumedState VarState = StateMap->getState(PInfo.getVar());
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000589
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000590 if (VarState == CS_None || isCallableInState(CWAttr, VarState))
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000591 return;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000592
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000593 Analyzer.WarningsHandler.warnUseInInvalidState(
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000594 FunDecl->getNameAsString(), PInfo.getVar()->getNameAsString(),
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000595 stateToString(VarState), BlameLoc);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000596
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000597 } else {
598 ConsumedState TmpState = PInfo.getAsState(StateMap);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000599
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000600 if (TmpState == CS_None || isCallableInState(CWAttr, TmpState))
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000601 return;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000602
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000603 Analyzer.WarningsHandler.warnUseOfTempInInvalidState(
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000604 FunDecl->getNameAsString(), stateToString(TmpState), BlameLoc);
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000605 }
606}
607
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000608
609// Factors out common behavior for function, method, and operator calls.
610// Check parameters and set parameter state if necessary.
611// Returns true if the state of ObjArg is set, or false otherwise.
612bool ConsumedStmtVisitor::handleCall(const CallExpr *Call, const Expr *ObjArg,
613 const FunctionDecl *FunD) {
614 unsigned Offset = 0;
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000615 if (isa<CXXOperatorCallExpr>(Call) && isa<CXXMethodDecl>(FunD))
616 Offset = 1; // first argument is 'this'
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000617
618 // check explicit parameters
619 for (unsigned Index = Offset; Index < Call->getNumArgs(); ++Index) {
620 // Skip variable argument lists.
621 if (Index - Offset >= FunD->getNumParams())
622 break;
623
624 const ParmVarDecl *Param = FunD->getParamDecl(Index - Offset);
625 QualType ParamType = Param->getType();
626
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000627 InfoEntry Entry = findInfo(Call->getArg(Index));
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000628
629 if (Entry == PropagationMap.end() || Entry->second.isTest())
630 continue;
631 PropagationInfo PInfo = Entry->second;
632
633 // Check that the parameter is in the correct state.
634 if (ParamTypestateAttr *PTA = Param->getAttr<ParamTypestateAttr>()) {
635 ConsumedState ParamState = PInfo.getAsState(StateMap);
636 ConsumedState ExpectedState = mapParamTypestateAttrState(PTA);
637
638 if (ParamState != ExpectedState)
639 Analyzer.WarningsHandler.warnParamTypestateMismatch(
640 Call->getArg(Index)->getExprLoc(),
641 stateToString(ExpectedState), stateToString(ParamState));
642 }
643
644 if (!(Entry->second.isVar() || Entry->second.isTmp()))
645 continue;
646
647 // Adjust state on the caller side.
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000648 if (isRValueRef(ParamType))
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000649 setStateForVarOrTmp(StateMap, PInfo, consumed::CS_Consumed);
650 else if (ReturnTypestateAttr *RT = Param->getAttr<ReturnTypestateAttr>())
651 setStateForVarOrTmp(StateMap, PInfo, mapReturnTypestateAttrState(RT));
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000652 else if (isPointerOrRef(ParamType) &&
653 (!ParamType->getPointeeType().isConstQualified() ||
654 isSetOnReadPtrType(ParamType)))
655 setStateForVarOrTmp(StateMap, PInfo, consumed::CS_Unknown);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000656 }
657
658 if (!ObjArg)
659 return false;
660
661 // check implicit 'self' parameter, if present
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000662 InfoEntry Entry = findInfo(ObjArg);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000663 if (Entry != PropagationMap.end()) {
664 PropagationInfo PInfo = Entry->second;
665 checkCallability(PInfo, FunD, Call->getExprLoc());
666
667 if (SetTypestateAttr *STA = FunD->getAttr<SetTypestateAttr>()) {
668 if (PInfo.isVar()) {
669 StateMap->setState(PInfo.getVar(), mapSetTypestateAttrState(STA));
670 return true;
671 }
672 else if (PInfo.isTmp()) {
673 StateMap->setState(PInfo.getTmp(), mapSetTypestateAttrState(STA));
674 return true;
675 }
676 }
677 else if (isTestingFunction(FunD) && PInfo.isVar()) {
678 PropagationMap.insert(PairType(Call,
679 PropagationInfo(PInfo.getVar(), testsFor(FunD))));
680 }
681 }
682 return false;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000683}
684
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000685
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000686void ConsumedStmtVisitor::propagateReturnType(const Expr *Call,
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000687 const FunctionDecl *Fun) {
688 QualType RetType = Fun->getCallResultType();
689 if (RetType->isReferenceType())
690 RetType = RetType->getPointeeType();
691
692 if (isConsumableType(RetType)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000693 ConsumedState ReturnState;
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000694 if (ReturnTypestateAttr *RTA = Fun->getAttr<ReturnTypestateAttr>())
695 ReturnState = mapReturnTypestateAttrState(RTA);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000696 else
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000697 ReturnState = mapConsumableAttrState(RetType);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000698
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000699 PropagationMap.insert(PairType(Call, PropagationInfo(ReturnState)));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000700 }
701}
702
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000703
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000704void ConsumedStmtVisitor::VisitBinaryOperator(const BinaryOperator *BinOp) {
705 switch (BinOp->getOpcode()) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000706 case BO_LAnd:
707 case BO_LOr : {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000708 InfoEntry LEntry = findInfo(BinOp->getLHS()),
709 REntry = findInfo(BinOp->getRHS());
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000710
711 VarTestResult LTest, RTest;
712
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000713 if (LEntry != PropagationMap.end() && LEntry->second.isVarTest()) {
714 LTest = LEntry->second.getVarTest();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000715
716 } else {
717 LTest.Var = NULL;
718 LTest.TestsFor = CS_None;
719 }
720
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000721 if (REntry != PropagationMap.end() && REntry->second.isVarTest()) {
722 RTest = REntry->second.getVarTest();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000723
724 } else {
725 RTest.Var = NULL;
726 RTest.TestsFor = CS_None;
727 }
728
729 if (!(LTest.Var == NULL && RTest.Var == NULL))
730 PropagationMap.insert(PairType(BinOp, PropagationInfo(BinOp,
731 static_cast<EffectiveOp>(BinOp->getOpcode() == BO_LOr), LTest, RTest)));
732
733 break;
734 }
735
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000736 case BO_PtrMemD:
737 case BO_PtrMemI:
738 forwardInfo(BinOp->getLHS(), BinOp);
739 break;
740
741 default:
742 break;
743 }
744}
745
Richard Trieuc6896912013-12-17 00:40:40 +0000746static bool isStdNamespace(const DeclContext *DC) {
747 if (!DC->isNamespace()) return false;
748 while (DC->getParent()->isNamespace())
749 DC = DC->getParent();
750 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
751
752 return ND && ND->getName() == "std" &&
753 ND->getDeclContext()->isTranslationUnit();
754}
755
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000756void ConsumedStmtVisitor::VisitCallExpr(const CallExpr *Call) {
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000757 const FunctionDecl *FunDecl = Call->getDirectCallee();
758 if (!FunDecl)
759 return;
760
761 // Special case for the std::move function.
762 // TODO: Make this more specific. (Deferred)
763 if (Call->getNumArgs() == 1 &&
764 FunDecl->getNameAsString() == "move" &&
765 isStdNamespace(FunDecl->getDeclContext())) {
766 copyInfo(Call->getArg(0), Call, CS_Consumed);
767 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000768 }
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000769
770 handleCall(Call, 0, FunDecl);
771 propagateReturnType(Call, FunDecl);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000772}
773
774void ConsumedStmtVisitor::VisitCastExpr(const CastExpr *Cast) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000775 forwardInfo(Cast->getSubExpr(), Cast);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000776}
777
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000778void ConsumedStmtVisitor::VisitCXXBindTemporaryExpr(
779 const CXXBindTemporaryExpr *Temp) {
780
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000781 InfoEntry Entry = findInfo(Temp->getSubExpr());
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000782
783 if (Entry != PropagationMap.end() && !Entry->second.isTest()) {
784 StateMap->setState(Temp, Entry->second.getAsState(StateMap));
785 PropagationMap.insert(PairType(Temp, PropagationInfo(Temp)));
786 }
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000787}
788
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000789void ConsumedStmtVisitor::VisitCXXConstructExpr(const CXXConstructExpr *Call) {
790 CXXConstructorDecl *Constructor = Call->getConstructor();
Reid Klecknere846dea2013-08-12 23:49:39 +0000791
792 ASTContext &CurrContext = AC.getASTContext();
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000793 QualType ThisType = Constructor->getThisType(CurrContext)->getPointeeType();
794
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000795 if (!isConsumableType(ThisType))
796 return;
797
798 // FIXME: What should happen if someone annotates the move constructor?
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000799 if (ReturnTypestateAttr *RTA = Constructor->getAttr<ReturnTypestateAttr>()) {
800 // TODO: Adjust state of args appropriately.
801 ConsumedState RetState = mapReturnTypestateAttrState(RTA);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000802 PropagationMap.insert(PairType(Call, PropagationInfo(RetState)));
803 } else if (Constructor->isDefaultConstructor()) {
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000804 PropagationMap.insert(PairType(Call,
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000805 PropagationInfo(consumed::CS_Consumed)));
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000806 } else if (Constructor->isMoveConstructor()) {
807 copyInfo(Call->getArg(0), Call, CS_Consumed);
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000808 } else if (Constructor->isCopyConstructor()) {
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000809 // Copy state from arg. If setStateOnRead then set arg to CS_Unknown.
810 ConsumedState NS =
811 isSetOnReadPtrType(Constructor->getThisType(CurrContext)) ?
812 CS_Unknown : CS_None;
813 copyInfo(Call->getArg(0), Call, NS);
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000814 } else {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000815 // TODO: Adjust state of args appropriately.
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000816 ConsumedState RetState = mapConsumableAttrState(ThisType);
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000817 PropagationMap.insert(PairType(Call, PropagationInfo(RetState)));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000818 }
819}
820
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000821
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000822void ConsumedStmtVisitor::VisitCXXMemberCallExpr(
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000823 const CXXMemberCallExpr *Call) {
824 CXXMethodDecl* MD = Call->getMethodDecl();
825 if (!MD)
826 return;
827
828 handleCall(Call, Call->getImplicitObjectArgument(), MD);
829 propagateReturnType(Call, MD);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000830}
831
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000832
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000833void ConsumedStmtVisitor::VisitCXXOperatorCallExpr(
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000834 const CXXOperatorCallExpr *Call) {
835
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000836 const FunctionDecl *FunDecl =
837 dyn_cast_or_null<FunctionDecl>(Call->getDirectCallee());
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000838 if (!FunDecl) return;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000839
840 if (Call->getOperator() == OO_Equal) {
841 ConsumedState CS = getInfo(Call->getArg(1));
842 if (!handleCall(Call, Call->getArg(0), FunDecl))
843 setInfo(Call->getArg(0), CS);
844 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000845 }
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000846
847 if (const CXXMemberCallExpr *MCall = dyn_cast<CXXMemberCallExpr>(Call))
848 handleCall(MCall, MCall->getImplicitObjectArgument(), FunDecl);
849 else
850 handleCall(Call, Call->getArg(0), FunDecl);
851
852 propagateReturnType(Call, FunDecl);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000853}
854
855void ConsumedStmtVisitor::VisitDeclRefExpr(const DeclRefExpr *DeclRef) {
856 if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(DeclRef->getDecl()))
857 if (StateMap->getState(Var) != consumed::CS_None)
858 PropagationMap.insert(PairType(DeclRef, PropagationInfo(Var)));
859}
860
861void ConsumedStmtVisitor::VisitDeclStmt(const DeclStmt *DeclS) {
Aaron Ballman535bbcc2014-03-14 17:01:24 +0000862 for (const auto *DI : DeclS->decls())
863 if (isa<VarDecl>(DI))
864 VisitVarDecl(cast<VarDecl>(DI));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000865
866 if (DeclS->isSingleDecl())
867 if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(DeclS->getSingleDecl()))
868 PropagationMap.insert(PairType(DeclS, PropagationInfo(Var)));
869}
870
871void ConsumedStmtVisitor::VisitMaterializeTemporaryExpr(
872 const MaterializeTemporaryExpr *Temp) {
873
Chris Wailes44930882013-10-24 14:28:17 +0000874 forwardInfo(Temp->GetTemporaryExpr(), Temp);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000875}
876
877void ConsumedStmtVisitor::VisitMemberExpr(const MemberExpr *MExpr) {
878 forwardInfo(MExpr->getBase(), MExpr);
879}
880
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000881
882void ConsumedStmtVisitor::VisitParmVarDecl(const ParmVarDecl *Param) {
David Blaikie16f76d22013-09-06 01:28:43 +0000883 QualType ParamType = Param->getType();
884 ConsumedState ParamState = consumed::CS_None;
DeLesley Hutchins69391772013-10-17 23:23:53 +0000885
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000886 if (const ParamTypestateAttr *PTA = Param->getAttr<ParamTypestateAttr>())
887 ParamState = mapParamTypestateAttrState(PTA);
888 else if (isConsumableType(ParamType))
889 ParamState = mapConsumableAttrState(ParamType);
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000890 else if (isRValueRef(ParamType) &&
891 isConsumableType(ParamType->getPointeeType()))
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000892 ParamState = mapConsumableAttrState(ParamType->getPointeeType());
893 else if (ParamType->isReferenceType() &&
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000894 isConsumableType(ParamType->getPointeeType()))
David Blaikie16f76d22013-09-06 01:28:43 +0000895 ParamState = consumed::CS_Unknown;
DeLesley Hutchins69391772013-10-17 23:23:53 +0000896
897 if (ParamState != CS_None)
David Blaikie16f76d22013-09-06 01:28:43 +0000898 StateMap->setState(Param, ParamState);
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000899}
900
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000901void ConsumedStmtVisitor::VisitReturnStmt(const ReturnStmt *Ret) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000902 ConsumedState ExpectedState = Analyzer.getExpectedReturnState();
903
904 if (ExpectedState != CS_None) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000905 InfoEntry Entry = findInfo(Ret->getRetValue());
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000906
907 if (Entry != PropagationMap.end()) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000908 ConsumedState RetState = Entry->second.getAsState(StateMap);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000909
910 if (RetState != ExpectedState)
911 Analyzer.WarningsHandler.warnReturnTypestateMismatch(
912 Ret->getReturnLoc(), stateToString(ExpectedState),
913 stateToString(RetState));
914 }
915 }
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000916
917 StateMap->checkParamsForReturnTypestate(Ret->getLocStart(),
918 Analyzer.WarningsHandler);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000919}
920
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000921void ConsumedStmtVisitor::VisitUnaryOperator(const UnaryOperator *UOp) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000922 InfoEntry Entry = findInfo(UOp->getSubExpr());
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000923 if (Entry == PropagationMap.end()) return;
924
925 switch (UOp->getOpcode()) {
926 case UO_AddrOf:
927 PropagationMap.insert(PairType(UOp, Entry->second));
928 break;
929
930 case UO_LNot:
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000931 if (Entry->second.isTest())
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000932 PropagationMap.insert(PairType(UOp, Entry->second.invertTest()));
933 break;
934
935 default:
936 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000937 }
938}
939
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000940// TODO: See if I need to check for reference types here.
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000941void ConsumedStmtVisitor::VisitVarDecl(const VarDecl *Var) {
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000942 if (isConsumableType(Var->getType())) {
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000943 if (Var->hasInit()) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000944 MapType::iterator VIT = findInfo(Var->getInit()->IgnoreImplicit());
DeLesley Hutchins81218662013-10-18 23:11:49 +0000945 if (VIT != PropagationMap.end()) {
946 PropagationInfo PInfo = VIT->second;
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000947 ConsumedState St = PInfo.getAsState(StateMap);
948
DeLesley Hutchins81218662013-10-18 23:11:49 +0000949 if (St != consumed::CS_None) {
950 StateMap->setState(Var, St);
951 return;
952 }
953 }
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000954 }
DeLesley Hutchins81218662013-10-18 23:11:49 +0000955 // Otherwise
956 StateMap->setState(Var, consumed::CS_Unknown);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000957 }
958}
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000959}} // end clang::consumed::ConsumedStmtVisitor
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000960
961namespace clang {
962namespace consumed {
963
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000964void splitVarStateForIf(const IfStmt * IfNode, const VarTestResult &Test,
965 ConsumedStateMap *ThenStates,
966 ConsumedStateMap *ElseStates) {
967
968 ConsumedState VarState = ThenStates->getState(Test.Var);
969
970 if (VarState == CS_Unknown) {
971 ThenStates->setState(Test.Var, Test.TestsFor);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000972 ElseStates->setState(Test.Var, invertConsumedUnconsumed(Test.TestsFor));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000973
974 } else if (VarState == invertConsumedUnconsumed(Test.TestsFor)) {
975 ThenStates->markUnreachable();
976
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000977 } else if (VarState == Test.TestsFor) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000978 ElseStates->markUnreachable();
979 }
980}
981
982void splitVarStateForIfBinOp(const PropagationInfo &PInfo,
983 ConsumedStateMap *ThenStates, ConsumedStateMap *ElseStates) {
984
985 const VarTestResult &LTest = PInfo.getLTest(),
986 &RTest = PInfo.getRTest();
987
988 ConsumedState LState = LTest.Var ? ThenStates->getState(LTest.Var) : CS_None,
989 RState = RTest.Var ? ThenStates->getState(RTest.Var) : CS_None;
990
991 if (LTest.Var) {
992 if (PInfo.testEffectiveOp() == EO_And) {
993 if (LState == CS_Unknown) {
994 ThenStates->setState(LTest.Var, LTest.TestsFor);
995
996 } else if (LState == invertConsumedUnconsumed(LTest.TestsFor)) {
997 ThenStates->markUnreachable();
998
999 } else if (LState == LTest.TestsFor && isKnownState(RState)) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001000 if (RState == RTest.TestsFor)
1001 ElseStates->markUnreachable();
1002 else
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001003 ThenStates->markUnreachable();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001004 }
1005
1006 } else {
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001007 if (LState == CS_Unknown) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001008 ElseStates->setState(LTest.Var,
1009 invertConsumedUnconsumed(LTest.TestsFor));
1010
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001011 } else if (LState == LTest.TestsFor) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001012 ElseStates->markUnreachable();
1013
1014 } else if (LState == invertConsumedUnconsumed(LTest.TestsFor) &&
1015 isKnownState(RState)) {
1016
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001017 if (RState == RTest.TestsFor)
1018 ElseStates->markUnreachable();
1019 else
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001020 ThenStates->markUnreachable();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001021 }
1022 }
1023 }
1024
1025 if (RTest.Var) {
1026 if (PInfo.testEffectiveOp() == EO_And) {
1027 if (RState == CS_Unknown)
1028 ThenStates->setState(RTest.Var, RTest.TestsFor);
1029 else if (RState == invertConsumedUnconsumed(RTest.TestsFor))
1030 ThenStates->markUnreachable();
1031
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001032 } else {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001033 if (RState == CS_Unknown)
1034 ElseStates->setState(RTest.Var,
1035 invertConsumedUnconsumed(RTest.TestsFor));
1036 else if (RState == RTest.TestsFor)
1037 ElseStates->markUnreachable();
1038 }
1039 }
1040}
1041
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001042bool ConsumedBlockInfo::allBackEdgesVisited(const CFGBlock *CurrBlock,
1043 const CFGBlock *TargetBlock) {
1044
1045 assert(CurrBlock && "Block pointer must not be NULL");
1046 assert(TargetBlock && "TargetBlock pointer must not be NULL");
1047
1048 unsigned int CurrBlockOrder = VisitOrder[CurrBlock->getBlockID()];
1049 for (CFGBlock::const_pred_iterator PI = TargetBlock->pred_begin(),
1050 PE = TargetBlock->pred_end(); PI != PE; ++PI) {
1051 if (*PI && CurrBlockOrder < VisitOrder[(*PI)->getBlockID()] )
1052 return false;
1053 }
1054 return true;
1055}
1056
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001057void ConsumedBlockInfo::addInfo(const CFGBlock *Block,
1058 ConsumedStateMap *StateMap,
1059 bool &AlreadyOwned) {
1060
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001061 assert(Block && "Block pointer must not be NULL");
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001062
1063 ConsumedStateMap *Entry = StateMapsArray[Block->getBlockID()];
1064
1065 if (Entry) {
1066 Entry->intersect(StateMap);
1067
1068 } else if (AlreadyOwned) {
1069 StateMapsArray[Block->getBlockID()] = new ConsumedStateMap(*StateMap);
1070
1071 } else {
1072 StateMapsArray[Block->getBlockID()] = StateMap;
1073 AlreadyOwned = true;
1074 }
1075}
1076
1077void ConsumedBlockInfo::addInfo(const CFGBlock *Block,
1078 ConsumedStateMap *StateMap) {
1079
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001080 assert(Block != NULL && "Block pointer must not be NULL");
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001081
1082 ConsumedStateMap *Entry = StateMapsArray[Block->getBlockID()];
1083
1084 if (Entry) {
1085 Entry->intersect(StateMap);
1086 delete StateMap;
1087
1088 } else {
1089 StateMapsArray[Block->getBlockID()] = StateMap;
1090 }
1091}
1092
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001093ConsumedStateMap* ConsumedBlockInfo::borrowInfo(const CFGBlock *Block) {
1094 assert(Block && "Block pointer must not be NULL");
1095 assert(StateMapsArray[Block->getBlockID()] && "Block has no block info");
1096
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001097 return StateMapsArray[Block->getBlockID()];
1098}
1099
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001100void ConsumedBlockInfo::discardInfo(const CFGBlock *Block) {
1101 unsigned int BlockID = Block->getBlockID();
1102 delete StateMapsArray[BlockID];
1103 StateMapsArray[BlockID] = NULL;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001104}
1105
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001106ConsumedStateMap* ConsumedBlockInfo::getInfo(const CFGBlock *Block) {
1107 assert(Block && "Block pointer must not be NULL");
1108
1109 ConsumedStateMap *StateMap = StateMapsArray[Block->getBlockID()];
1110 if (isBackEdgeTarget(Block)) {
1111 return new ConsumedStateMap(*StateMap);
1112 } else {
1113 StateMapsArray[Block->getBlockID()] = NULL;
1114 return StateMap;
1115 }
1116}
1117
1118bool ConsumedBlockInfo::isBackEdge(const CFGBlock *From, const CFGBlock *To) {
1119 assert(From && "From block must not be NULL");
1120 assert(To && "From block must not be NULL");
1121
1122 return VisitOrder[From->getBlockID()] > VisitOrder[To->getBlockID()];
1123}
1124
1125bool ConsumedBlockInfo::isBackEdgeTarget(const CFGBlock *Block) {
1126 assert(Block != NULL && "Block pointer must not be NULL");
1127
1128 // Anything with less than two predecessors can't be the target of a back
1129 // edge.
1130 if (Block->pred_size() < 2)
1131 return false;
1132
1133 unsigned int BlockVisitOrder = VisitOrder[Block->getBlockID()];
1134 for (CFGBlock::const_pred_iterator PI = Block->pred_begin(),
1135 PE = Block->pred_end(); PI != PE; ++PI) {
1136 if (*PI && BlockVisitOrder < VisitOrder[(*PI)->getBlockID()])
1137 return true;
1138 }
1139 return false;
1140}
1141
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001142void ConsumedStateMap::checkParamsForReturnTypestate(SourceLocation BlameLoc,
1143 ConsumedWarningsHandlerBase &WarningsHandler) const {
1144
Aaron Ballman35897d92014-04-28 14:56:59 +00001145 for (const auto &DM : VarMap) {
1146 if (isa<ParmVarDecl>(DM.first)) {
1147 const ParmVarDecl *Param = cast<ParmVarDecl>(DM.first);
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +00001148 const ReturnTypestateAttr *RTA = Param->getAttr<ReturnTypestateAttr>();
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001149
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +00001150 if (!RTA)
1151 continue;
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001152
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +00001153 ConsumedState ExpectedState = mapReturnTypestateAttrState(RTA);
Aaron Ballman35897d92014-04-28 14:56:59 +00001154 if (DM.second != ExpectedState)
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001155 WarningsHandler.warnParamReturnTypestateMismatch(BlameLoc,
1156 Param->getNameAsString(), stateToString(ExpectedState),
Aaron Ballman35897d92014-04-28 14:56:59 +00001157 stateToString(DM.second));
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001158 }
1159 }
1160}
1161
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001162void ConsumedStateMap::clearTemporaries() {
1163 TmpMap.clear();
1164}
1165
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001166ConsumedState ConsumedStateMap::getState(const VarDecl *Var) const {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001167 VarMapType::const_iterator Entry = VarMap.find(Var);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001168
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001169 if (Entry != VarMap.end())
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001170 return Entry->second;
1171
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001172 return CS_None;
1173}
1174
1175ConsumedState
1176ConsumedStateMap::getState(const CXXBindTemporaryExpr *Tmp) const {
1177 TmpMapType::const_iterator Entry = TmpMap.find(Tmp);
1178
1179 if (Entry != TmpMap.end())
1180 return Entry->second;
1181
1182 return CS_None;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001183}
1184
1185void ConsumedStateMap::intersect(const ConsumedStateMap *Other) {
1186 ConsumedState LocalState;
1187
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001188 if (this->From && this->From == Other->From && !Other->Reachable) {
1189 this->markUnreachable();
1190 return;
1191 }
1192
Aaron Ballman35897d92014-04-28 14:56:59 +00001193 for (const auto &DM : Other->VarMap) {
1194 LocalState = this->getState(DM.first);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001195
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001196 if (LocalState == CS_None)
1197 continue;
1198
Aaron Ballman35897d92014-04-28 14:56:59 +00001199 if (LocalState != DM.second)
1200 VarMap[DM.first] = CS_Unknown;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001201 }
1202}
1203
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001204void ConsumedStateMap::intersectAtLoopHead(const CFGBlock *LoopHead,
1205 const CFGBlock *LoopBack, const ConsumedStateMap *LoopBackStates,
1206 ConsumedWarningsHandlerBase &WarningsHandler) {
1207
1208 ConsumedState LocalState;
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001209 SourceLocation BlameLoc = getLastStmtLoc(LoopBack);
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001210
Aaron Ballman35897d92014-04-28 14:56:59 +00001211 for (const auto &DM : LoopBackStates->VarMap) {
1212 LocalState = this->getState(DM.first);
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001213
1214 if (LocalState == CS_None)
1215 continue;
1216
Aaron Ballman35897d92014-04-28 14:56:59 +00001217 if (LocalState != DM.second) {
1218 VarMap[DM.first] = CS_Unknown;
Aaron Ballmanfe46e622014-04-28 13:01:32 +00001219 WarningsHandler.warnLoopStateMismatch(BlameLoc,
Aaron Ballman35897d92014-04-28 14:56:59 +00001220 DM.first->getNameAsString());
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001221 }
1222 }
1223}
1224
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001225void ConsumedStateMap::markUnreachable() {
1226 this->Reachable = false;
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001227 VarMap.clear();
1228 TmpMap.clear();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001229}
1230
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001231void ConsumedStateMap::setState(const VarDecl *Var, ConsumedState State) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001232 VarMap[Var] = State;
1233}
1234
1235void ConsumedStateMap::setState(const CXXBindTemporaryExpr *Tmp,
1236 ConsumedState State) {
1237 TmpMap[Tmp] = State;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001238}
1239
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001240void ConsumedStateMap::remove(const VarDecl *Var) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001241 VarMap.erase(Var);
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001242}
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001243
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001244bool ConsumedStateMap::operator!=(const ConsumedStateMap *Other) const {
Aaron Ballman35897d92014-04-28 14:56:59 +00001245 for (const auto &DM : Other->VarMap)
1246 if (this->getState(DM.first) != DM.second)
Aaron Ballmanfe46e622014-04-28 13:01:32 +00001247 return true;
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001248 return false;
1249}
1250
David Blaikie16f76d22013-09-06 01:28:43 +00001251void ConsumedAnalyzer::determineExpectedReturnState(AnalysisDeclContext &AC,
1252 const FunctionDecl *D) {
1253 QualType ReturnType;
1254 if (const CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1255 ASTContext &CurrContext = AC.getASTContext();
1256 ReturnType = Constructor->getThisType(CurrContext)->getPointeeType();
1257 } else
1258 ReturnType = D->getCallResultType();
1259
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +00001260 if (const ReturnTypestateAttr *RTSAttr = D->getAttr<ReturnTypestateAttr>()) {
David Blaikie16f76d22013-09-06 01:28:43 +00001261 const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1262 if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1263 // FIXME: This should be removed when template instantiation propagates
1264 // attributes at template specialization definition, not
1265 // declaration. When it is removed the test needs to be enabled
1266 // in SemaDeclAttr.cpp.
1267 WarningsHandler.warnReturnTypestateForUnconsumableType(
1268 RTSAttr->getLocation(), ReturnType.getAsString());
1269 ExpectedReturnState = CS_None;
1270 } else
1271 ExpectedReturnState = mapReturnTypestateAttrState(RTSAttr);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00001272 } else if (isConsumableType(ReturnType)) {
1273 if (isAutoCastType(ReturnType)) // We can auto-cast the state to the
1274 ExpectedReturnState = CS_None; // expected state.
1275 else
1276 ExpectedReturnState = mapConsumableAttrState(ReturnType);
1277 }
David Blaikie16f76d22013-09-06 01:28:43 +00001278 else
1279 ExpectedReturnState = CS_None;
1280}
1281
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001282bool ConsumedAnalyzer::splitState(const CFGBlock *CurrBlock,
1283 const ConsumedStmtVisitor &Visitor) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001284
1285 std::unique_ptr<ConsumedStateMap> FalseStates(
1286 new ConsumedStateMap(*CurrStates));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001287 PropagationInfo PInfo;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001288
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001289 if (const IfStmt *IfNode =
1290 dyn_cast_or_null<IfStmt>(CurrBlock->getTerminator().getStmt())) {
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001291
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +00001292 const Expr *Cond = IfNode->getCond();
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001293
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001294 PInfo = Visitor.getInfo(Cond);
1295 if (!PInfo.isValid() && isa<BinaryOperator>(Cond))
1296 PInfo = Visitor.getInfo(cast<BinaryOperator>(Cond)->getRHS());
1297
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001298 if (PInfo.isVarTest()) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001299 CurrStates->setSource(Cond);
1300 FalseStates->setSource(Cond);
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001301 splitVarStateForIf(IfNode, PInfo.getVarTest(), CurrStates,
Chris Wailes2dc8c422013-10-25 15:33:28 +00001302 FalseStates.get());
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001303
1304 } else if (PInfo.isBinTest()) {
1305 CurrStates->setSource(PInfo.testSourceNode());
1306 FalseStates->setSource(PInfo.testSourceNode());
Chris Wailes2dc8c422013-10-25 15:33:28 +00001307 splitVarStateForIfBinOp(PInfo, CurrStates, FalseStates.get());
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001308
1309 } else {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001310 return false;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001311 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001312
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001313 } else if (const BinaryOperator *BinOp =
1314 dyn_cast_or_null<BinaryOperator>(CurrBlock->getTerminator().getStmt())) {
1315
1316 PInfo = Visitor.getInfo(BinOp->getLHS());
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001317 if (!PInfo.isVarTest()) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001318 if ((BinOp = dyn_cast_or_null<BinaryOperator>(BinOp->getLHS()))) {
1319 PInfo = Visitor.getInfo(BinOp->getRHS());
1320
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001321 if (!PInfo.isVarTest())
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001322 return false;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001323
1324 } else {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001325 return false;
1326 }
1327 }
1328
1329 CurrStates->setSource(BinOp);
1330 FalseStates->setSource(BinOp);
1331
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001332 const VarTestResult &Test = PInfo.getVarTest();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001333 ConsumedState VarState = CurrStates->getState(Test.Var);
1334
1335 if (BinOp->getOpcode() == BO_LAnd) {
1336 if (VarState == CS_Unknown)
1337 CurrStates->setState(Test.Var, Test.TestsFor);
1338 else if (VarState == invertConsumedUnconsumed(Test.TestsFor))
1339 CurrStates->markUnreachable();
1340
1341 } else if (BinOp->getOpcode() == BO_LOr) {
1342 if (VarState == CS_Unknown)
1343 FalseStates->setState(Test.Var,
1344 invertConsumedUnconsumed(Test.TestsFor));
1345 else if (VarState == Test.TestsFor)
1346 FalseStates->markUnreachable();
1347 }
1348
1349 } else {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001350 return false;
1351 }
1352
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001353 CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin();
1354
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001355 if (*SI)
1356 BlockInfo.addInfo(*SI, CurrStates);
1357 else
1358 delete CurrStates;
1359
1360 if (*++SI)
Ahmed Charles9a16beb2014-03-07 19:33:25 +00001361 BlockInfo.addInfo(*SI, FalseStates.release());
1362
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001363 CurrStates = NULL;
1364 return true;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001365}
1366
1367void ConsumedAnalyzer::run(AnalysisDeclContext &AC) {
1368 const FunctionDecl *D = dyn_cast_or_null<FunctionDecl>(AC.getDecl());
DeLesley Hutchins85c07d92013-09-10 23:10:10 +00001369 if (!D)
1370 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001371
DeLesley Hutchins85c07d92013-09-10 23:10:10 +00001372 CFG *CFGraph = AC.getCFG();
1373 if (!CFGraph)
1374 return;
DeLesley Hutchins65013202013-10-17 18:19:31 +00001375
David Blaikie16f76d22013-09-06 01:28:43 +00001376 determineExpectedReturnState(AC, D);
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001377
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001378 PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>();
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001379 // AC.getCFG()->viewCFG(LangOptions());
1380
1381 BlockInfo = ConsumedBlockInfo(CFGraph->getNumBlockIDs(), SortedGraph);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001382
1383 CurrStates = new ConsumedStateMap();
DeLesley Hutchinsb570c132013-08-29 22:36:05 +00001384 ConsumedStmtVisitor Visitor(AC, *this, CurrStates);
1385
1386 // Add all trackable parameters to the state map.
Aaron Ballmanfe46e622014-04-28 13:01:32 +00001387 for (const auto *PI : D->params())
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00001388 Visitor.VisitParmVarDecl(PI);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001389
1390 // Visit all of the function's basic blocks.
Aaron Ballmanfe46e622014-04-28 13:01:32 +00001391 for (const auto *CurrBlock : *SortedGraph) {
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001392 if (CurrStates == NULL)
1393 CurrStates = BlockInfo.getInfo(CurrBlock);
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001394
1395 if (!CurrStates) {
1396 continue;
1397
1398 } else if (!CurrStates->isReachable()) {
1399 delete CurrStates;
1400 CurrStates = NULL;
1401 continue;
1402 }
1403
1404 Visitor.reset(CurrStates);
1405
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001406 // Visit all of the basic block's statements.
Aaron Ballman35897d92014-04-28 14:56:59 +00001407 for (const auto &B : *CurrBlock) {
1408 switch (B.getKind()) {
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001409 case CFGElement::Statement:
Aaron Ballman35897d92014-04-28 14:56:59 +00001410 Visitor.Visit(B.castAs<CFGStmt>().getStmt());
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001411 break;
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001412
1413 case CFGElement::TemporaryDtor: {
Aaron Ballman35897d92014-04-28 14:56:59 +00001414 const CFGTemporaryDtor &DTor = B.castAs<CFGTemporaryDtor>();
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001415 const CXXBindTemporaryExpr *BTE = DTor.getBindTemporaryExpr();
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001416
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001417 Visitor.checkCallability(PropagationInfo(BTE),
1418 DTor.getDestructorDecl(AC.getASTContext()),
1419 BTE->getExprLoc());
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001420 break;
1421 }
1422
1423 case CFGElement::AutomaticObjectDtor: {
Aaron Ballman35897d92014-04-28 14:56:59 +00001424 const CFGAutomaticObjDtor &DTor = B.castAs<CFGAutomaticObjDtor>();
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001425 SourceLocation Loc = DTor.getTriggerStmt()->getLocEnd();
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001426 const VarDecl *Var = DTor.getVarDecl();
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001427
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001428 Visitor.checkCallability(PropagationInfo(Var),
1429 DTor.getDestructorDecl(AC.getASTContext()),
1430 Loc);
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001431 break;
1432 }
1433
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001434 default:
1435 break;
1436 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001437 }
1438
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001439 CurrStates->clearTemporaries();
1440
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001441 // TODO: Handle other forms of branching with precision, including while-
1442 // and for-loops. (Deferred)
1443 if (!splitState(CurrBlock, Visitor)) {
1444 CurrStates->setSource(NULL);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001445
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001446 if (CurrBlock->succ_size() > 1 ||
1447 (CurrBlock->succ_size() == 1 &&
1448 (*CurrBlock->succ_begin())->pred_size() > 1)) {
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001449
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001450 bool OwnershipTaken = false;
1451
1452 for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
1453 SE = CurrBlock->succ_end(); SI != SE; ++SI) {
1454
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001455 if (*SI == NULL) continue;
1456
1457 if (BlockInfo.isBackEdge(CurrBlock, *SI)) {
1458 BlockInfo.borrowInfo(*SI)->intersectAtLoopHead(*SI, CurrBlock,
1459 CurrStates,
1460 WarningsHandler);
1461
1462 if (BlockInfo.allBackEdgesVisited(*SI, CurrBlock))
1463 BlockInfo.discardInfo(*SI);
1464 } else {
1465 BlockInfo.addInfo(*SI, CurrStates, OwnershipTaken);
1466 }
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001467 }
1468
1469 if (!OwnershipTaken)
1470 delete CurrStates;
1471
1472 CurrStates = NULL;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001473 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001474 }
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001475
1476 if (CurrBlock == &AC.getCFG()->getExit() &&
1477 D->getCallResultType()->isVoidType())
1478 CurrStates->checkParamsForReturnTypestate(D->getLocation(),
1479 WarningsHandler);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001480 } // End of block iterator.
1481
1482 // Delete the last existing state map.
1483 delete CurrStates;
1484
1485 WarningsHandler.emitDiagnostics();
1486}
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001487}} // end namespace clang::consumed