blob: 2b2da2c69a417eda3f83fb1cd9a79faa2965d5fc [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
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000116 for (const auto &S : CWAttr->callableStates()) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000117 ConsumedState MappedAttrState = CS_None;
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000118
119 switch (S) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000120 case CallableWhenAttr::Unknown:
121 MappedAttrState = CS_Unknown;
122 break;
123
124 case CallableWhenAttr::Unconsumed:
125 MappedAttrState = CS_Unconsumed;
126 break;
127
128 case CallableWhenAttr::Consumed:
129 MappedAttrState = CS_Consumed;
130 break;
131 }
132
133 if (MappedAttrState == State)
134 return true;
135 }
136
137 return false;
138}
139
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000140
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000141static bool isConsumableType(const QualType &QT) {
Chris Wailes93edffa2013-10-31 15:38:12 +0000142 if (QT->isPointerType() || QT->isReferenceType())
143 return false;
144
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000145 if (const CXXRecordDecl *RD = QT->getAsCXXRecordDecl())
146 return RD->hasAttr<ConsumableAttr>();
Chris Wailes93edffa2013-10-31 15:38:12 +0000147
148 return false;
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000149}
150
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000151static bool isAutoCastType(const QualType &QT) {
152 if (QT->isPointerType() || QT->isReferenceType())
153 return false;
154
155 if (const CXXRecordDecl *RD = QT->getAsCXXRecordDecl())
156 return RD->hasAttr<ConsumableAutoCastAttr>();
157
158 return false;
159}
160
161static bool isSetOnReadPtrType(const QualType &QT) {
162 if (const CXXRecordDecl *RD = QT->getPointeeCXXRecordDecl())
163 return RD->hasAttr<ConsumableSetOnReadAttr>();
164 return false;
165}
166
167
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000168static bool isKnownState(ConsumedState State) {
169 switch (State) {
170 case CS_Unconsumed:
171 case CS_Consumed:
172 return true;
173 case CS_None:
174 case CS_Unknown:
175 return false;
176 }
Aaron Ballmana21f4b82013-08-29 20:36:09 +0000177 llvm_unreachable("invalid enum");
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000178}
179
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000180static bool isRValueRef(QualType ParamType) {
181 return ParamType->isRValueReferenceType();
DeLesley Hutchins0bd25892013-10-18 19:25:18 +0000182}
183
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000184static bool isTestingFunction(const FunctionDecl *FunDecl) {
Chris Wailes9385f9f2013-10-29 20:28:41 +0000185 return FunDecl->hasAttr<TestTypestateAttr>();
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000186}
187
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000188static bool isPointerOrRef(QualType ParamType) {
189 return ParamType->isPointerType() || ParamType->isReferenceType();
DeLesley Hutchins0bd25892013-10-18 19:25:18 +0000190}
191
David Blaikie16f76d22013-09-06 01:28:43 +0000192static ConsumedState mapConsumableAttrState(const QualType QT) {
193 assert(isConsumableType(QT));
194
195 const ConsumableAttr *CAttr =
196 QT->getAsCXXRecordDecl()->getAttr<ConsumableAttr>();
197
198 switch (CAttr->getDefaultState()) {
199 case ConsumableAttr::Unknown:
200 return CS_Unknown;
201 case ConsumableAttr::Unconsumed:
202 return CS_Unconsumed;
203 case ConsumableAttr::Consumed:
204 return CS_Consumed;
205 }
206 llvm_unreachable("invalid enum");
207}
208
DeLesley Hutchins69391772013-10-17 23:23:53 +0000209static ConsumedState
210mapParamTypestateAttrState(const ParamTypestateAttr *PTAttr) {
211 switch (PTAttr->getParamState()) {
212 case ParamTypestateAttr::Unknown:
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000213 return CS_Unknown;
DeLesley Hutchins69391772013-10-17 23:23:53 +0000214 case ParamTypestateAttr::Unconsumed:
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000215 return CS_Unconsumed;
DeLesley Hutchins69391772013-10-17 23:23:53 +0000216 case ParamTypestateAttr::Consumed:
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000217 return CS_Consumed;
218 }
219 llvm_unreachable("invalid_enum");
220}
221
Eric Christopherde156242013-09-03 20:43:00 +0000222static ConsumedState
223mapReturnTypestateAttrState(const ReturnTypestateAttr *RTSAttr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000224 switch (RTSAttr->getState()) {
225 case ReturnTypestateAttr::Unknown:
226 return CS_Unknown;
227 case ReturnTypestateAttr::Unconsumed:
228 return CS_Unconsumed;
229 case ReturnTypestateAttr::Consumed:
230 return CS_Consumed;
231 }
Eric Christopherde156242013-09-03 20:43:00 +0000232 llvm_unreachable("invalid enum");
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000233}
234
DeLesley Hutchins69391772013-10-17 23:23:53 +0000235static ConsumedState mapSetTypestateAttrState(const SetTypestateAttr *STAttr) {
236 switch (STAttr->getNewState()) {
237 case SetTypestateAttr::Unknown:
238 return CS_Unknown;
239 case SetTypestateAttr::Unconsumed:
240 return CS_Unconsumed;
241 case SetTypestateAttr::Consumed:
242 return CS_Consumed;
243 }
244 llvm_unreachable("invalid_enum");
245}
246
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000247static StringRef stateToString(ConsumedState State) {
248 switch (State) {
249 case consumed::CS_None:
250 return "none";
251
252 case consumed::CS_Unknown:
253 return "unknown";
254
255 case consumed::CS_Unconsumed:
256 return "unconsumed";
257
258 case consumed::CS_Consumed:
259 return "consumed";
260 }
Reid Kleckner6454d0a2013-08-13 00:11:59 +0000261 llvm_unreachable("invalid enum");
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000262}
263
DeLesley Hutchins8d41d992013-10-11 22:30:48 +0000264static ConsumedState testsFor(const FunctionDecl *FunDecl) {
265 assert(isTestingFunction(FunDecl));
Chris Wailes9385f9f2013-10-29 20:28:41 +0000266 switch (FunDecl->getAttr<TestTypestateAttr>()->getTestState()) {
267 case TestTypestateAttr::Unconsumed:
DeLesley Hutchins8d41d992013-10-11 22:30:48 +0000268 return CS_Unconsumed;
Chris Wailes9385f9f2013-10-29 20:28:41 +0000269 case TestTypestateAttr::Consumed:
DeLesley Hutchins8d41d992013-10-11 22:30:48 +0000270 return CS_Consumed;
271 }
272 llvm_unreachable("invalid enum");
273}
274
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000275namespace {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000276struct VarTestResult {
277 const VarDecl *Var;
278 ConsumedState TestsFor;
279};
280} // end anonymous::VarTestResult
281
282namespace clang {
283namespace consumed {
284
285enum EffectiveOp {
286 EO_And,
287 EO_Or
288};
289
290class PropagationInfo {
291 enum {
292 IT_None,
293 IT_State,
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000294 IT_VarTest,
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000295 IT_BinTest,
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000296 IT_Var,
297 IT_Tmp
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000298 } InfoType;
Eric Christopherf8a1baa2013-08-29 18:00:58 +0000299
300 struct BinTestTy {
301 const BinaryOperator *Source;
302 EffectiveOp EOp;
303 VarTestResult LTest;
304 VarTestResult RTest;
305 };
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000306
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000307 union {
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000308 ConsumedState State;
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000309 VarTestResult VarTest;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000310 const VarDecl *Var;
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000311 const CXXBindTemporaryExpr *Tmp;
Eric Christopherf8a1baa2013-08-29 18:00:58 +0000312 BinTestTy BinTest;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000313 };
314
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000315public:
316 PropagationInfo() : InfoType(IT_None) {}
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000317
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000318 PropagationInfo(const VarTestResult &VarTest)
319 : InfoType(IT_VarTest), VarTest(VarTest) {}
320
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000321 PropagationInfo(const VarDecl *Var, ConsumedState TestsFor)
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000322 : InfoType(IT_VarTest) {
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000323
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000324 VarTest.Var = Var;
325 VarTest.TestsFor = TestsFor;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000326 }
327
328 PropagationInfo(const BinaryOperator *Source, EffectiveOp EOp,
329 const VarTestResult &LTest, const VarTestResult &RTest)
330 : InfoType(IT_BinTest) {
331
332 BinTest.Source = Source;
333 BinTest.EOp = EOp;
334 BinTest.LTest = LTest;
335 BinTest.RTest = RTest;
336 }
337
338 PropagationInfo(const BinaryOperator *Source, EffectiveOp EOp,
339 const VarDecl *LVar, ConsumedState LTestsFor,
340 const VarDecl *RVar, ConsumedState RTestsFor)
341 : InfoType(IT_BinTest) {
342
343 BinTest.Source = Source;
344 BinTest.EOp = EOp;
345 BinTest.LTest.Var = LVar;
346 BinTest.LTest.TestsFor = LTestsFor;
347 BinTest.RTest.Var = RVar;
348 BinTest.RTest.TestsFor = RTestsFor;
349 }
350
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000351 PropagationInfo(ConsumedState State)
352 : InfoType(IT_State), State(State) {}
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000353
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000354 PropagationInfo(const VarDecl *Var) : InfoType(IT_Var), Var(Var) {}
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000355 PropagationInfo(const CXXBindTemporaryExpr *Tmp)
356 : InfoType(IT_Tmp), Tmp(Tmp) {}
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000357
358 const ConsumedState & getState() const {
359 assert(InfoType == IT_State);
360 return State;
361 }
362
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000363 const VarTestResult & getVarTest() const {
364 assert(InfoType == IT_VarTest);
365 return VarTest;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000366 }
367
368 const VarTestResult & getLTest() const {
369 assert(InfoType == IT_BinTest);
370 return BinTest.LTest;
371 }
372
373 const VarTestResult & getRTest() const {
374 assert(InfoType == IT_BinTest);
375 return BinTest.RTest;
376 }
377
378 const VarDecl * getVar() const {
379 assert(InfoType == IT_Var);
380 return Var;
381 }
382
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000383 const CXXBindTemporaryExpr * getTmp() const {
384 assert(InfoType == IT_Tmp);
385 return Tmp;
386 }
387
388 ConsumedState getAsState(const ConsumedStateMap *StateMap) const {
389 assert(isVar() || isTmp() || isState());
390
391 if (isVar())
392 return StateMap->getState(Var);
393 else if (isTmp())
394 return StateMap->getState(Tmp);
395 else if (isState())
396 return State;
397 else
398 return CS_None;
399 }
400
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000401 EffectiveOp testEffectiveOp() const {
402 assert(InfoType == IT_BinTest);
403 return BinTest.EOp;
404 }
405
406 const BinaryOperator * testSourceNode() const {
407 assert(InfoType == IT_BinTest);
408 return BinTest.Source;
409 }
410
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000411 inline bool isValid() const { return InfoType != IT_None; }
412 inline bool isState() const { return InfoType == IT_State; }
413 inline bool isVarTest() const { return InfoType == IT_VarTest; }
414 inline bool isBinTest() const { return InfoType == IT_BinTest; }
415 inline bool isVar() const { return InfoType == IT_Var; }
416 inline bool isTmp() const { return InfoType == IT_Tmp; }
417
418 bool isTest() const {
419 return InfoType == IT_VarTest || InfoType == IT_BinTest;
420 }
421
422 bool isPointerToValue() const {
423 return InfoType == IT_Var || InfoType == IT_Tmp;
424 }
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000425
426 PropagationInfo invertTest() const {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000427 assert(InfoType == IT_VarTest || InfoType == IT_BinTest);
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000428
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000429 if (InfoType == IT_VarTest) {
430 return PropagationInfo(VarTest.Var,
431 invertConsumedUnconsumed(VarTest.TestsFor));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000432
433 } else if (InfoType == IT_BinTest) {
434 return PropagationInfo(BinTest.Source,
435 BinTest.EOp == EO_And ? EO_Or : EO_And,
436 BinTest.LTest.Var, invertConsumedUnconsumed(BinTest.LTest.TestsFor),
437 BinTest.RTest.Var, invertConsumedUnconsumed(BinTest.RTest.TestsFor));
438 } else {
439 return PropagationInfo();
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000440 }
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000441 }
442};
443
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000444static inline void
445setStateForVarOrTmp(ConsumedStateMap *StateMap, const PropagationInfo &PInfo,
446 ConsumedState State) {
447
448 assert(PInfo.isVar() || PInfo.isTmp());
449
450 if (PInfo.isVar())
451 StateMap->setState(PInfo.getVar(), State);
452 else
453 StateMap->setState(PInfo.getTmp(), State);
454}
455
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000456class ConsumedStmtVisitor : public ConstStmtVisitor<ConsumedStmtVisitor> {
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000457
458 typedef llvm::DenseMap<const Stmt *, PropagationInfo> MapType;
459 typedef std::pair<const Stmt *, PropagationInfo> PairType;
460 typedef MapType::iterator InfoEntry;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000461 typedef MapType::const_iterator ConstInfoEntry;
462
Reid Klecknere846dea2013-08-12 23:49:39 +0000463 AnalysisDeclContext &AC;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000464 ConsumedAnalyzer &Analyzer;
465 ConsumedStateMap *StateMap;
466 MapType PropagationMap;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000467
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000468 InfoEntry findInfo(const Expr *E) {
469 return PropagationMap.find(E->IgnoreParens());
470 }
471 ConstInfoEntry findInfo(const Expr *E) const {
472 return PropagationMap.find(E->IgnoreParens());
473 }
474 void insertInfo(const Expr *E, const PropagationInfo &PI) {
475 PropagationMap.insert(PairType(E->IgnoreParens(), PI));
476 }
477
478 void forwardInfo(const Expr *From, const Expr *To);
479 void copyInfo(const Expr *From, const Expr *To, ConsumedState CS);
480 ConsumedState getInfo(const Expr *From);
481 void setInfo(const Expr *To, ConsumedState NS);
482 void propagateReturnType(const Expr *Call, const FunctionDecl *Fun);
DeLesley Hutchins81218662013-10-18 23:11:49 +0000483
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000484public:
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000485 void checkCallability(const PropagationInfo &PInfo,
486 const FunctionDecl *FunDecl,
487 SourceLocation BlameLoc);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000488 bool handleCall(const CallExpr *Call, const Expr *ObjArg,
489 const FunctionDecl *FunD);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000490
491 void VisitBinaryOperator(const BinaryOperator *BinOp);
492 void VisitCallExpr(const CallExpr *Call);
493 void VisitCastExpr(const CastExpr *Cast);
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000494 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Temp);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000495 void VisitCXXConstructExpr(const CXXConstructExpr *Call);
496 void VisitCXXMemberCallExpr(const CXXMemberCallExpr *Call);
497 void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *Call);
498 void VisitDeclRefExpr(const DeclRefExpr *DeclRef);
499 void VisitDeclStmt(const DeclStmt *DelcS);
500 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Temp);
501 void VisitMemberExpr(const MemberExpr *MExpr);
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000502 void VisitParmVarDecl(const ParmVarDecl *Param);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000503 void VisitReturnStmt(const ReturnStmt *Ret);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000504 void VisitUnaryOperator(const UnaryOperator *UOp);
505 void VisitVarDecl(const VarDecl *Var);
Reid Klecknere846dea2013-08-12 23:49:39 +0000506
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000507 ConsumedStmtVisitor(AnalysisDeclContext &AC, ConsumedAnalyzer &Analyzer,
508 ConsumedStateMap *StateMap)
509 : AC(AC), Analyzer(Analyzer), StateMap(StateMap) {}
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000510
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000511 PropagationInfo getInfo(const Expr *StmtNode) const {
512 ConstInfoEntry Entry = findInfo(StmtNode);
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000513
514 if (Entry != PropagationMap.end())
515 return Entry->second;
516 else
517 return PropagationInfo();
518 }
519
520 void reset(ConsumedStateMap *NewStateMap) {
521 StateMap = NewStateMap;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000522 }
523};
524
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000525
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000526void ConsumedStmtVisitor::forwardInfo(const Expr *From, const Expr *To) {
527 InfoEntry Entry = findInfo(From);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000528 if (Entry != PropagationMap.end())
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000529 insertInfo(To, Entry->second);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000530}
531
532
533// Create a new state for To, which is initialized to the state of From.
534// If NS is not CS_None, sets the state of From to NS.
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000535void ConsumedStmtVisitor::copyInfo(const Expr *From, const Expr *To,
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000536 ConsumedState NS) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000537 InfoEntry Entry = findInfo(From);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000538 if (Entry != PropagationMap.end()) {
539 PropagationInfo& PInfo = Entry->second;
540 ConsumedState CS = PInfo.getAsState(StateMap);
541 if (CS != CS_None)
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000542 insertInfo(To, PropagationInfo(CS));
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000543 if (NS != CS_None && PInfo.isPointerToValue())
544 setStateForVarOrTmp(StateMap, PInfo, NS);
545 }
546}
547
548
549// Get the ConsumedState for From
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000550ConsumedState ConsumedStmtVisitor::getInfo(const Expr *From) {
551 InfoEntry Entry = findInfo(From);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000552 if (Entry != PropagationMap.end()) {
553 PropagationInfo& PInfo = Entry->second;
554 return PInfo.getAsState(StateMap);
555 }
556 return CS_None;
557}
558
559
560// If we already have info for To then update it, otherwise create a new entry.
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000561void ConsumedStmtVisitor::setInfo(const Expr *To, ConsumedState NS) {
562 InfoEntry Entry = findInfo(To);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000563 if (Entry != PropagationMap.end()) {
564 PropagationInfo& PInfo = Entry->second;
565 if (PInfo.isPointerToValue())
566 setStateForVarOrTmp(StateMap, PInfo, NS);
567 } else if (NS != CS_None) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000568 insertInfo(To, PropagationInfo(NS));
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000569 }
570}
571
572
573
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000574void ConsumedStmtVisitor::checkCallability(const PropagationInfo &PInfo,
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000575 const FunctionDecl *FunDecl,
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000576 SourceLocation BlameLoc) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000577 assert(!PInfo.isTest());
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000578
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000579 const CallableWhenAttr *CWAttr = FunDecl->getAttr<CallableWhenAttr>();
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000580 if (!CWAttr)
581 return;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000582
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000583 if (PInfo.isVar()) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000584 ConsumedState VarState = StateMap->getState(PInfo.getVar());
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000585
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000586 if (VarState == CS_None || isCallableInState(CWAttr, VarState))
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000587 return;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000588
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000589 Analyzer.WarningsHandler.warnUseInInvalidState(
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000590 FunDecl->getNameAsString(), PInfo.getVar()->getNameAsString(),
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000591 stateToString(VarState), BlameLoc);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000592
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000593 } else {
594 ConsumedState TmpState = PInfo.getAsState(StateMap);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000595
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000596 if (TmpState == CS_None || isCallableInState(CWAttr, TmpState))
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000597 return;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000598
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000599 Analyzer.WarningsHandler.warnUseOfTempInInvalidState(
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000600 FunDecl->getNameAsString(), stateToString(TmpState), BlameLoc);
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000601 }
602}
603
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000604
605// Factors out common behavior for function, method, and operator calls.
606// Check parameters and set parameter state if necessary.
607// Returns true if the state of ObjArg is set, or false otherwise.
608bool ConsumedStmtVisitor::handleCall(const CallExpr *Call, const Expr *ObjArg,
609 const FunctionDecl *FunD) {
610 unsigned Offset = 0;
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000611 if (isa<CXXOperatorCallExpr>(Call) && isa<CXXMethodDecl>(FunD))
612 Offset = 1; // first argument is 'this'
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000613
614 // check explicit parameters
615 for (unsigned Index = Offset; Index < Call->getNumArgs(); ++Index) {
616 // Skip variable argument lists.
617 if (Index - Offset >= FunD->getNumParams())
618 break;
619
620 const ParmVarDecl *Param = FunD->getParamDecl(Index - Offset);
621 QualType ParamType = Param->getType();
622
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000623 InfoEntry Entry = findInfo(Call->getArg(Index));
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000624
625 if (Entry == PropagationMap.end() || Entry->second.isTest())
626 continue;
627 PropagationInfo PInfo = Entry->second;
628
629 // Check that the parameter is in the correct state.
630 if (ParamTypestateAttr *PTA = Param->getAttr<ParamTypestateAttr>()) {
631 ConsumedState ParamState = PInfo.getAsState(StateMap);
632 ConsumedState ExpectedState = mapParamTypestateAttrState(PTA);
633
634 if (ParamState != ExpectedState)
635 Analyzer.WarningsHandler.warnParamTypestateMismatch(
636 Call->getArg(Index)->getExprLoc(),
637 stateToString(ExpectedState), stateToString(ParamState));
638 }
639
640 if (!(Entry->second.isVar() || Entry->second.isTmp()))
641 continue;
642
643 // Adjust state on the caller side.
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000644 if (isRValueRef(ParamType))
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000645 setStateForVarOrTmp(StateMap, PInfo, consumed::CS_Consumed);
646 else if (ReturnTypestateAttr *RT = Param->getAttr<ReturnTypestateAttr>())
647 setStateForVarOrTmp(StateMap, PInfo, mapReturnTypestateAttrState(RT));
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000648 else if (isPointerOrRef(ParamType) &&
649 (!ParamType->getPointeeType().isConstQualified() ||
650 isSetOnReadPtrType(ParamType)))
651 setStateForVarOrTmp(StateMap, PInfo, consumed::CS_Unknown);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000652 }
653
654 if (!ObjArg)
655 return false;
656
657 // check implicit 'self' parameter, if present
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000658 InfoEntry Entry = findInfo(ObjArg);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000659 if (Entry != PropagationMap.end()) {
660 PropagationInfo PInfo = Entry->second;
661 checkCallability(PInfo, FunD, Call->getExprLoc());
662
663 if (SetTypestateAttr *STA = FunD->getAttr<SetTypestateAttr>()) {
664 if (PInfo.isVar()) {
665 StateMap->setState(PInfo.getVar(), mapSetTypestateAttrState(STA));
666 return true;
667 }
668 else if (PInfo.isTmp()) {
669 StateMap->setState(PInfo.getTmp(), mapSetTypestateAttrState(STA));
670 return true;
671 }
672 }
673 else if (isTestingFunction(FunD) && PInfo.isVar()) {
674 PropagationMap.insert(PairType(Call,
675 PropagationInfo(PInfo.getVar(), testsFor(FunD))));
676 }
677 }
678 return false;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000679}
680
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000681
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000682void ConsumedStmtVisitor::propagateReturnType(const Expr *Call,
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000683 const FunctionDecl *Fun) {
684 QualType RetType = Fun->getCallResultType();
685 if (RetType->isReferenceType())
686 RetType = RetType->getPointeeType();
687
688 if (isConsumableType(RetType)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000689 ConsumedState ReturnState;
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000690 if (ReturnTypestateAttr *RTA = Fun->getAttr<ReturnTypestateAttr>())
691 ReturnState = mapReturnTypestateAttrState(RTA);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000692 else
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000693 ReturnState = mapConsumableAttrState(RetType);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000694
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000695 PropagationMap.insert(PairType(Call, PropagationInfo(ReturnState)));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000696 }
697}
698
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000699
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000700void ConsumedStmtVisitor::VisitBinaryOperator(const BinaryOperator *BinOp) {
701 switch (BinOp->getOpcode()) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000702 case BO_LAnd:
703 case BO_LOr : {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000704 InfoEntry LEntry = findInfo(BinOp->getLHS()),
705 REntry = findInfo(BinOp->getRHS());
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000706
707 VarTestResult LTest, RTest;
708
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000709 if (LEntry != PropagationMap.end() && LEntry->second.isVarTest()) {
710 LTest = LEntry->second.getVarTest();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000711
712 } else {
Craig Topper25542942014-05-20 04:30:07 +0000713 LTest.Var = nullptr;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000714 LTest.TestsFor = CS_None;
715 }
716
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000717 if (REntry != PropagationMap.end() && REntry->second.isVarTest()) {
718 RTest = REntry->second.getVarTest();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000719
720 } else {
Craig Topper25542942014-05-20 04:30:07 +0000721 RTest.Var = nullptr;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000722 RTest.TestsFor = CS_None;
723 }
Craig Topper25542942014-05-20 04:30:07 +0000724
725 if (!(LTest.Var == nullptr && RTest.Var == nullptr))
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000726 PropagationMap.insert(PairType(BinOp, PropagationInfo(BinOp,
727 static_cast<EffectiveOp>(BinOp->getOpcode() == BO_LOr), LTest, RTest)));
728
729 break;
730 }
731
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000732 case BO_PtrMemD:
733 case BO_PtrMemI:
734 forwardInfo(BinOp->getLHS(), BinOp);
735 break;
736
737 default:
738 break;
739 }
740}
741
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000742void ConsumedStmtVisitor::VisitCallExpr(const CallExpr *Call) {
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000743 const FunctionDecl *FunDecl = Call->getDirectCallee();
744 if (!FunDecl)
745 return;
746
747 // Special case for the std::move function.
748 // TODO: Make this more specific. (Deferred)
Richard Trieuc771d5d2014-05-28 02:16:01 +0000749 if (Call->getNumArgs() == 1 && FunDecl->getNameAsString() == "move" &&
750 FunDecl->isInStdNamespace()) {
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000751 copyInfo(Call->getArg(0), Call, CS_Consumed);
752 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000753 }
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000754
Craig Topper25542942014-05-20 04:30:07 +0000755 handleCall(Call, nullptr, FunDecl);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000756 propagateReturnType(Call, FunDecl);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000757}
758
759void ConsumedStmtVisitor::VisitCastExpr(const CastExpr *Cast) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000760 forwardInfo(Cast->getSubExpr(), Cast);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000761}
762
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000763void ConsumedStmtVisitor::VisitCXXBindTemporaryExpr(
764 const CXXBindTemporaryExpr *Temp) {
765
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000766 InfoEntry Entry = findInfo(Temp->getSubExpr());
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000767
768 if (Entry != PropagationMap.end() && !Entry->second.isTest()) {
769 StateMap->setState(Temp, Entry->second.getAsState(StateMap));
770 PropagationMap.insert(PairType(Temp, PropagationInfo(Temp)));
771 }
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000772}
773
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000774void ConsumedStmtVisitor::VisitCXXConstructExpr(const CXXConstructExpr *Call) {
775 CXXConstructorDecl *Constructor = Call->getConstructor();
Reid Klecknere846dea2013-08-12 23:49:39 +0000776
777 ASTContext &CurrContext = AC.getASTContext();
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000778 QualType ThisType = Constructor->getThisType(CurrContext)->getPointeeType();
779
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000780 if (!isConsumableType(ThisType))
781 return;
782
783 // FIXME: What should happen if someone annotates the move constructor?
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000784 if (ReturnTypestateAttr *RTA = Constructor->getAttr<ReturnTypestateAttr>()) {
785 // TODO: Adjust state of args appropriately.
786 ConsumedState RetState = mapReturnTypestateAttrState(RTA);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000787 PropagationMap.insert(PairType(Call, PropagationInfo(RetState)));
788 } else if (Constructor->isDefaultConstructor()) {
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000789 PropagationMap.insert(PairType(Call,
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000790 PropagationInfo(consumed::CS_Consumed)));
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000791 } else if (Constructor->isMoveConstructor()) {
792 copyInfo(Call->getArg(0), Call, CS_Consumed);
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000793 } else if (Constructor->isCopyConstructor()) {
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000794 // Copy state from arg. If setStateOnRead then set arg to CS_Unknown.
795 ConsumedState NS =
796 isSetOnReadPtrType(Constructor->getThisType(CurrContext)) ?
797 CS_Unknown : CS_None;
798 copyInfo(Call->getArg(0), Call, NS);
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000799 } else {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000800 // TODO: Adjust state of args appropriately.
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000801 ConsumedState RetState = mapConsumableAttrState(ThisType);
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000802 PropagationMap.insert(PairType(Call, PropagationInfo(RetState)));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000803 }
804}
805
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000806
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000807void ConsumedStmtVisitor::VisitCXXMemberCallExpr(
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000808 const CXXMemberCallExpr *Call) {
809 CXXMethodDecl* MD = Call->getMethodDecl();
810 if (!MD)
811 return;
812
813 handleCall(Call, Call->getImplicitObjectArgument(), MD);
814 propagateReturnType(Call, MD);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000815}
816
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000817
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000818void ConsumedStmtVisitor::VisitCXXOperatorCallExpr(
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000819 const CXXOperatorCallExpr *Call) {
820
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000821 const FunctionDecl *FunDecl =
822 dyn_cast_or_null<FunctionDecl>(Call->getDirectCallee());
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000823 if (!FunDecl) return;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000824
825 if (Call->getOperator() == OO_Equal) {
826 ConsumedState CS = getInfo(Call->getArg(1));
827 if (!handleCall(Call, Call->getArg(0), FunDecl))
828 setInfo(Call->getArg(0), CS);
829 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000830 }
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000831
832 if (const CXXMemberCallExpr *MCall = dyn_cast<CXXMemberCallExpr>(Call))
833 handleCall(MCall, MCall->getImplicitObjectArgument(), FunDecl);
834 else
835 handleCall(Call, Call->getArg(0), FunDecl);
836
837 propagateReturnType(Call, FunDecl);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000838}
839
840void ConsumedStmtVisitor::VisitDeclRefExpr(const DeclRefExpr *DeclRef) {
841 if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(DeclRef->getDecl()))
842 if (StateMap->getState(Var) != consumed::CS_None)
843 PropagationMap.insert(PairType(DeclRef, PropagationInfo(Var)));
844}
845
846void ConsumedStmtVisitor::VisitDeclStmt(const DeclStmt *DeclS) {
Aaron Ballman535bbcc2014-03-14 17:01:24 +0000847 for (const auto *DI : DeclS->decls())
848 if (isa<VarDecl>(DI))
849 VisitVarDecl(cast<VarDecl>(DI));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000850
851 if (DeclS->isSingleDecl())
852 if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(DeclS->getSingleDecl()))
853 PropagationMap.insert(PairType(DeclS, PropagationInfo(Var)));
854}
855
856void ConsumedStmtVisitor::VisitMaterializeTemporaryExpr(
857 const MaterializeTemporaryExpr *Temp) {
858
Chris Wailes44930882013-10-24 14:28:17 +0000859 forwardInfo(Temp->GetTemporaryExpr(), Temp);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000860}
861
862void ConsumedStmtVisitor::VisitMemberExpr(const MemberExpr *MExpr) {
863 forwardInfo(MExpr->getBase(), MExpr);
864}
865
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000866
867void ConsumedStmtVisitor::VisitParmVarDecl(const ParmVarDecl *Param) {
David Blaikie16f76d22013-09-06 01:28:43 +0000868 QualType ParamType = Param->getType();
869 ConsumedState ParamState = consumed::CS_None;
DeLesley Hutchins69391772013-10-17 23:23:53 +0000870
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000871 if (const ParamTypestateAttr *PTA = Param->getAttr<ParamTypestateAttr>())
872 ParamState = mapParamTypestateAttrState(PTA);
873 else if (isConsumableType(ParamType))
874 ParamState = mapConsumableAttrState(ParamType);
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000875 else if (isRValueRef(ParamType) &&
876 isConsumableType(ParamType->getPointeeType()))
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000877 ParamState = mapConsumableAttrState(ParamType->getPointeeType());
878 else if (ParamType->isReferenceType() &&
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000879 isConsumableType(ParamType->getPointeeType()))
David Blaikie16f76d22013-09-06 01:28:43 +0000880 ParamState = consumed::CS_Unknown;
DeLesley Hutchins69391772013-10-17 23:23:53 +0000881
882 if (ParamState != CS_None)
David Blaikie16f76d22013-09-06 01:28:43 +0000883 StateMap->setState(Param, ParamState);
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000884}
885
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000886void ConsumedStmtVisitor::VisitReturnStmt(const ReturnStmt *Ret) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000887 ConsumedState ExpectedState = Analyzer.getExpectedReturnState();
888
889 if (ExpectedState != CS_None) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000890 InfoEntry Entry = findInfo(Ret->getRetValue());
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000891
892 if (Entry != PropagationMap.end()) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000893 ConsumedState RetState = Entry->second.getAsState(StateMap);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000894
895 if (RetState != ExpectedState)
896 Analyzer.WarningsHandler.warnReturnTypestateMismatch(
897 Ret->getReturnLoc(), stateToString(ExpectedState),
898 stateToString(RetState));
899 }
900 }
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000901
902 StateMap->checkParamsForReturnTypestate(Ret->getLocStart(),
903 Analyzer.WarningsHandler);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000904}
905
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000906void ConsumedStmtVisitor::VisitUnaryOperator(const UnaryOperator *UOp) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000907 InfoEntry Entry = findInfo(UOp->getSubExpr());
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000908 if (Entry == PropagationMap.end()) return;
909
910 switch (UOp->getOpcode()) {
911 case UO_AddrOf:
912 PropagationMap.insert(PairType(UOp, Entry->second));
913 break;
914
915 case UO_LNot:
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000916 if (Entry->second.isTest())
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000917 PropagationMap.insert(PairType(UOp, Entry->second.invertTest()));
918 break;
919
920 default:
921 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000922 }
923}
924
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000925// TODO: See if I need to check for reference types here.
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000926void ConsumedStmtVisitor::VisitVarDecl(const VarDecl *Var) {
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000927 if (isConsumableType(Var->getType())) {
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000928 if (Var->hasInit()) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000929 MapType::iterator VIT = findInfo(Var->getInit()->IgnoreImplicit());
DeLesley Hutchins81218662013-10-18 23:11:49 +0000930 if (VIT != PropagationMap.end()) {
931 PropagationInfo PInfo = VIT->second;
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000932 ConsumedState St = PInfo.getAsState(StateMap);
933
DeLesley Hutchins81218662013-10-18 23:11:49 +0000934 if (St != consumed::CS_None) {
935 StateMap->setState(Var, St);
936 return;
937 }
938 }
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000939 }
DeLesley Hutchins81218662013-10-18 23:11:49 +0000940 // Otherwise
941 StateMap->setState(Var, consumed::CS_Unknown);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000942 }
943}
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000944}} // end clang::consumed::ConsumedStmtVisitor
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000945
946namespace clang {
947namespace consumed {
948
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000949void splitVarStateForIf(const IfStmt * IfNode, const VarTestResult &Test,
950 ConsumedStateMap *ThenStates,
951 ConsumedStateMap *ElseStates) {
952
953 ConsumedState VarState = ThenStates->getState(Test.Var);
954
955 if (VarState == CS_Unknown) {
956 ThenStates->setState(Test.Var, Test.TestsFor);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000957 ElseStates->setState(Test.Var, invertConsumedUnconsumed(Test.TestsFor));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000958
959 } else if (VarState == invertConsumedUnconsumed(Test.TestsFor)) {
960 ThenStates->markUnreachable();
961
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000962 } else if (VarState == Test.TestsFor) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000963 ElseStates->markUnreachable();
964 }
965}
966
967void splitVarStateForIfBinOp(const PropagationInfo &PInfo,
968 ConsumedStateMap *ThenStates, ConsumedStateMap *ElseStates) {
969
970 const VarTestResult &LTest = PInfo.getLTest(),
971 &RTest = PInfo.getRTest();
972
973 ConsumedState LState = LTest.Var ? ThenStates->getState(LTest.Var) : CS_None,
974 RState = RTest.Var ? ThenStates->getState(RTest.Var) : CS_None;
975
976 if (LTest.Var) {
977 if (PInfo.testEffectiveOp() == EO_And) {
978 if (LState == CS_Unknown) {
979 ThenStates->setState(LTest.Var, LTest.TestsFor);
980
981 } else if (LState == invertConsumedUnconsumed(LTest.TestsFor)) {
982 ThenStates->markUnreachable();
983
984 } else if (LState == LTest.TestsFor && isKnownState(RState)) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000985 if (RState == RTest.TestsFor)
986 ElseStates->markUnreachable();
987 else
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000988 ThenStates->markUnreachable();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000989 }
990
991 } else {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000992 if (LState == CS_Unknown) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000993 ElseStates->setState(LTest.Var,
994 invertConsumedUnconsumed(LTest.TestsFor));
995
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000996 } else if (LState == LTest.TestsFor) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000997 ElseStates->markUnreachable();
998
999 } else if (LState == invertConsumedUnconsumed(LTest.TestsFor) &&
1000 isKnownState(RState)) {
1001
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001002 if (RState == RTest.TestsFor)
1003 ElseStates->markUnreachable();
1004 else
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001005 ThenStates->markUnreachable();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001006 }
1007 }
1008 }
1009
1010 if (RTest.Var) {
1011 if (PInfo.testEffectiveOp() == EO_And) {
1012 if (RState == CS_Unknown)
1013 ThenStates->setState(RTest.Var, RTest.TestsFor);
1014 else if (RState == invertConsumedUnconsumed(RTest.TestsFor))
1015 ThenStates->markUnreachable();
1016
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001017 } else {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001018 if (RState == CS_Unknown)
1019 ElseStates->setState(RTest.Var,
1020 invertConsumedUnconsumed(RTest.TestsFor));
1021 else if (RState == RTest.TestsFor)
1022 ElseStates->markUnreachable();
1023 }
1024 }
1025}
1026
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001027bool ConsumedBlockInfo::allBackEdgesVisited(const CFGBlock *CurrBlock,
1028 const CFGBlock *TargetBlock) {
1029
1030 assert(CurrBlock && "Block pointer must not be NULL");
1031 assert(TargetBlock && "TargetBlock pointer must not be NULL");
1032
1033 unsigned int CurrBlockOrder = VisitOrder[CurrBlock->getBlockID()];
1034 for (CFGBlock::const_pred_iterator PI = TargetBlock->pred_begin(),
1035 PE = TargetBlock->pred_end(); PI != PE; ++PI) {
1036 if (*PI && CurrBlockOrder < VisitOrder[(*PI)->getBlockID()] )
1037 return false;
1038 }
1039 return true;
1040}
1041
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001042void ConsumedBlockInfo::addInfo(const CFGBlock *Block,
1043 ConsumedStateMap *StateMap,
1044 bool &AlreadyOwned) {
1045
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001046 assert(Block && "Block pointer must not be NULL");
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001047
1048 ConsumedStateMap *Entry = StateMapsArray[Block->getBlockID()];
1049
1050 if (Entry) {
1051 Entry->intersect(StateMap);
1052
1053 } else if (AlreadyOwned) {
1054 StateMapsArray[Block->getBlockID()] = new ConsumedStateMap(*StateMap);
1055
1056 } else {
1057 StateMapsArray[Block->getBlockID()] = StateMap;
1058 AlreadyOwned = true;
1059 }
1060}
1061
1062void ConsumedBlockInfo::addInfo(const CFGBlock *Block,
1063 ConsumedStateMap *StateMap) {
Craig Topper25542942014-05-20 04:30:07 +00001064
1065 assert(Block && "Block pointer must not be NULL");
1066
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001067 ConsumedStateMap *Entry = StateMapsArray[Block->getBlockID()];
1068
1069 if (Entry) {
1070 Entry->intersect(StateMap);
1071 delete StateMap;
1072
1073 } else {
1074 StateMapsArray[Block->getBlockID()] = StateMap;
1075 }
1076}
1077
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001078ConsumedStateMap* ConsumedBlockInfo::borrowInfo(const CFGBlock *Block) {
1079 assert(Block && "Block pointer must not be NULL");
1080 assert(StateMapsArray[Block->getBlockID()] && "Block has no block info");
1081
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001082 return StateMapsArray[Block->getBlockID()];
1083}
1084
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001085void ConsumedBlockInfo::discardInfo(const CFGBlock *Block) {
1086 unsigned int BlockID = Block->getBlockID();
1087 delete StateMapsArray[BlockID];
Craig Topper25542942014-05-20 04:30:07 +00001088 StateMapsArray[BlockID] = nullptr;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001089}
1090
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001091ConsumedStateMap* ConsumedBlockInfo::getInfo(const CFGBlock *Block) {
1092 assert(Block && "Block pointer must not be NULL");
1093
1094 ConsumedStateMap *StateMap = StateMapsArray[Block->getBlockID()];
1095 if (isBackEdgeTarget(Block)) {
1096 return new ConsumedStateMap(*StateMap);
1097 } else {
Craig Topper25542942014-05-20 04:30:07 +00001098 StateMapsArray[Block->getBlockID()] = nullptr;
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001099 return StateMap;
1100 }
1101}
1102
1103bool ConsumedBlockInfo::isBackEdge(const CFGBlock *From, const CFGBlock *To) {
1104 assert(From && "From block must not be NULL");
1105 assert(To && "From block must not be NULL");
1106
1107 return VisitOrder[From->getBlockID()] > VisitOrder[To->getBlockID()];
1108}
1109
1110bool ConsumedBlockInfo::isBackEdgeTarget(const CFGBlock *Block) {
Craig Topper25542942014-05-20 04:30:07 +00001111 assert(Block && "Block pointer must not be NULL");
1112
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001113 // Anything with less than two predecessors can't be the target of a back
1114 // edge.
1115 if (Block->pred_size() < 2)
1116 return false;
1117
1118 unsigned int BlockVisitOrder = VisitOrder[Block->getBlockID()];
1119 for (CFGBlock::const_pred_iterator PI = Block->pred_begin(),
1120 PE = Block->pred_end(); PI != PE; ++PI) {
1121 if (*PI && BlockVisitOrder < VisitOrder[(*PI)->getBlockID()])
1122 return true;
1123 }
1124 return false;
1125}
1126
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001127void ConsumedStateMap::checkParamsForReturnTypestate(SourceLocation BlameLoc,
1128 ConsumedWarningsHandlerBase &WarningsHandler) const {
1129
Aaron Ballman35897d92014-04-28 14:56:59 +00001130 for (const auto &DM : VarMap) {
1131 if (isa<ParmVarDecl>(DM.first)) {
1132 const ParmVarDecl *Param = cast<ParmVarDecl>(DM.first);
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +00001133 const ReturnTypestateAttr *RTA = Param->getAttr<ReturnTypestateAttr>();
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001134
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +00001135 if (!RTA)
1136 continue;
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001137
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +00001138 ConsumedState ExpectedState = mapReturnTypestateAttrState(RTA);
Aaron Ballman35897d92014-04-28 14:56:59 +00001139 if (DM.second != ExpectedState)
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001140 WarningsHandler.warnParamReturnTypestateMismatch(BlameLoc,
1141 Param->getNameAsString(), stateToString(ExpectedState),
Aaron Ballman35897d92014-04-28 14:56:59 +00001142 stateToString(DM.second));
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001143 }
1144 }
1145}
1146
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001147void ConsumedStateMap::clearTemporaries() {
1148 TmpMap.clear();
1149}
1150
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001151ConsumedState ConsumedStateMap::getState(const VarDecl *Var) const {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001152 VarMapType::const_iterator Entry = VarMap.find(Var);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001153
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001154 if (Entry != VarMap.end())
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001155 return Entry->second;
1156
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001157 return CS_None;
1158}
1159
1160ConsumedState
1161ConsumedStateMap::getState(const CXXBindTemporaryExpr *Tmp) const {
1162 TmpMapType::const_iterator Entry = TmpMap.find(Tmp);
1163
1164 if (Entry != TmpMap.end())
1165 return Entry->second;
1166
1167 return CS_None;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001168}
1169
1170void ConsumedStateMap::intersect(const ConsumedStateMap *Other) {
1171 ConsumedState LocalState;
1172
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001173 if (this->From && this->From == Other->From && !Other->Reachable) {
1174 this->markUnreachable();
1175 return;
1176 }
1177
Aaron Ballman35897d92014-04-28 14:56:59 +00001178 for (const auto &DM : Other->VarMap) {
1179 LocalState = this->getState(DM.first);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001180
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001181 if (LocalState == CS_None)
1182 continue;
1183
Aaron Ballman35897d92014-04-28 14:56:59 +00001184 if (LocalState != DM.second)
1185 VarMap[DM.first] = CS_Unknown;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001186 }
1187}
1188
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001189void ConsumedStateMap::intersectAtLoopHead(const CFGBlock *LoopHead,
1190 const CFGBlock *LoopBack, const ConsumedStateMap *LoopBackStates,
1191 ConsumedWarningsHandlerBase &WarningsHandler) {
1192
1193 ConsumedState LocalState;
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001194 SourceLocation BlameLoc = getLastStmtLoc(LoopBack);
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001195
Aaron Ballman35897d92014-04-28 14:56:59 +00001196 for (const auto &DM : LoopBackStates->VarMap) {
1197 LocalState = this->getState(DM.first);
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001198
1199 if (LocalState == CS_None)
1200 continue;
1201
Aaron Ballman35897d92014-04-28 14:56:59 +00001202 if (LocalState != DM.second) {
1203 VarMap[DM.first] = CS_Unknown;
Aaron Ballmanfe46e622014-04-28 13:01:32 +00001204 WarningsHandler.warnLoopStateMismatch(BlameLoc,
Aaron Ballman35897d92014-04-28 14:56:59 +00001205 DM.first->getNameAsString());
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001206 }
1207 }
1208}
1209
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001210void ConsumedStateMap::markUnreachable() {
1211 this->Reachable = false;
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001212 VarMap.clear();
1213 TmpMap.clear();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001214}
1215
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001216void ConsumedStateMap::setState(const VarDecl *Var, ConsumedState State) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001217 VarMap[Var] = State;
1218}
1219
1220void ConsumedStateMap::setState(const CXXBindTemporaryExpr *Tmp,
1221 ConsumedState State) {
1222 TmpMap[Tmp] = State;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001223}
1224
Manuel Klimekb33bded2014-05-08 11:50:00 +00001225void ConsumedStateMap::remove(const CXXBindTemporaryExpr *Tmp) {
1226 TmpMap.erase(Tmp);
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001227}
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001228
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001229bool ConsumedStateMap::operator!=(const ConsumedStateMap *Other) const {
Aaron Ballman35897d92014-04-28 14:56:59 +00001230 for (const auto &DM : Other->VarMap)
1231 if (this->getState(DM.first) != DM.second)
Aaron Ballmanfe46e622014-04-28 13:01:32 +00001232 return true;
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001233 return false;
1234}
1235
David Blaikie16f76d22013-09-06 01:28:43 +00001236void ConsumedAnalyzer::determineExpectedReturnState(AnalysisDeclContext &AC,
1237 const FunctionDecl *D) {
1238 QualType ReturnType;
1239 if (const CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
1240 ASTContext &CurrContext = AC.getASTContext();
1241 ReturnType = Constructor->getThisType(CurrContext)->getPointeeType();
1242 } else
1243 ReturnType = D->getCallResultType();
1244
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +00001245 if (const ReturnTypestateAttr *RTSAttr = D->getAttr<ReturnTypestateAttr>()) {
David Blaikie16f76d22013-09-06 01:28:43 +00001246 const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1247 if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1248 // FIXME: This should be removed when template instantiation propagates
1249 // attributes at template specialization definition, not
1250 // declaration. When it is removed the test needs to be enabled
1251 // in SemaDeclAttr.cpp.
1252 WarningsHandler.warnReturnTypestateForUnconsumableType(
1253 RTSAttr->getLocation(), ReturnType.getAsString());
1254 ExpectedReturnState = CS_None;
1255 } else
1256 ExpectedReturnState = mapReturnTypestateAttrState(RTSAttr);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00001257 } else if (isConsumableType(ReturnType)) {
1258 if (isAutoCastType(ReturnType)) // We can auto-cast the state to the
1259 ExpectedReturnState = CS_None; // expected state.
1260 else
1261 ExpectedReturnState = mapConsumableAttrState(ReturnType);
1262 }
David Blaikie16f76d22013-09-06 01:28:43 +00001263 else
1264 ExpectedReturnState = CS_None;
1265}
1266
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001267bool ConsumedAnalyzer::splitState(const CFGBlock *CurrBlock,
1268 const ConsumedStmtVisitor &Visitor) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001269
1270 std::unique_ptr<ConsumedStateMap> FalseStates(
1271 new ConsumedStateMap(*CurrStates));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001272 PropagationInfo PInfo;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001273
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001274 if (const IfStmt *IfNode =
1275 dyn_cast_or_null<IfStmt>(CurrBlock->getTerminator().getStmt())) {
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001276
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +00001277 const Expr *Cond = IfNode->getCond();
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001278
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001279 PInfo = Visitor.getInfo(Cond);
1280 if (!PInfo.isValid() && isa<BinaryOperator>(Cond))
1281 PInfo = Visitor.getInfo(cast<BinaryOperator>(Cond)->getRHS());
1282
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001283 if (PInfo.isVarTest()) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001284 CurrStates->setSource(Cond);
1285 FalseStates->setSource(Cond);
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001286 splitVarStateForIf(IfNode, PInfo.getVarTest(), CurrStates,
Chris Wailes2dc8c422013-10-25 15:33:28 +00001287 FalseStates.get());
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001288
1289 } else if (PInfo.isBinTest()) {
1290 CurrStates->setSource(PInfo.testSourceNode());
1291 FalseStates->setSource(PInfo.testSourceNode());
Chris Wailes2dc8c422013-10-25 15:33:28 +00001292 splitVarStateForIfBinOp(PInfo, CurrStates, FalseStates.get());
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001293
1294 } else {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001295 return false;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001296 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001297
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001298 } else if (const BinaryOperator *BinOp =
1299 dyn_cast_or_null<BinaryOperator>(CurrBlock->getTerminator().getStmt())) {
1300
1301 PInfo = Visitor.getInfo(BinOp->getLHS());
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001302 if (!PInfo.isVarTest()) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001303 if ((BinOp = dyn_cast_or_null<BinaryOperator>(BinOp->getLHS()))) {
1304 PInfo = Visitor.getInfo(BinOp->getRHS());
1305
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001306 if (!PInfo.isVarTest())
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001307 return false;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001308
1309 } else {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001310 return false;
1311 }
1312 }
1313
1314 CurrStates->setSource(BinOp);
1315 FalseStates->setSource(BinOp);
1316
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001317 const VarTestResult &Test = PInfo.getVarTest();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001318 ConsumedState VarState = CurrStates->getState(Test.Var);
1319
1320 if (BinOp->getOpcode() == BO_LAnd) {
1321 if (VarState == CS_Unknown)
1322 CurrStates->setState(Test.Var, Test.TestsFor);
1323 else if (VarState == invertConsumedUnconsumed(Test.TestsFor))
1324 CurrStates->markUnreachable();
1325
1326 } else if (BinOp->getOpcode() == BO_LOr) {
1327 if (VarState == CS_Unknown)
1328 FalseStates->setState(Test.Var,
1329 invertConsumedUnconsumed(Test.TestsFor));
1330 else if (VarState == Test.TestsFor)
1331 FalseStates->markUnreachable();
1332 }
1333
1334 } else {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001335 return false;
1336 }
1337
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001338 CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin();
1339
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001340 if (*SI)
1341 BlockInfo.addInfo(*SI, CurrStates);
1342 else
1343 delete CurrStates;
1344
1345 if (*++SI)
Ahmed Charles9a16beb2014-03-07 19:33:25 +00001346 BlockInfo.addInfo(*SI, FalseStates.release());
1347
Craig Topper25542942014-05-20 04:30:07 +00001348 CurrStates = nullptr;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001349 return true;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001350}
1351
1352void ConsumedAnalyzer::run(AnalysisDeclContext &AC) {
1353 const FunctionDecl *D = dyn_cast_or_null<FunctionDecl>(AC.getDecl());
DeLesley Hutchins85c07d92013-09-10 23:10:10 +00001354 if (!D)
1355 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001356
DeLesley Hutchins85c07d92013-09-10 23:10:10 +00001357 CFG *CFGraph = AC.getCFG();
1358 if (!CFGraph)
1359 return;
DeLesley Hutchins65013202013-10-17 18:19:31 +00001360
David Blaikie16f76d22013-09-06 01:28:43 +00001361 determineExpectedReturnState(AC, D);
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001362
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001363 PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>();
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001364 // AC.getCFG()->viewCFG(LangOptions());
1365
1366 BlockInfo = ConsumedBlockInfo(CFGraph->getNumBlockIDs(), SortedGraph);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001367
1368 CurrStates = new ConsumedStateMap();
DeLesley Hutchinsb570c132013-08-29 22:36:05 +00001369 ConsumedStmtVisitor Visitor(AC, *this, CurrStates);
1370
1371 // Add all trackable parameters to the state map.
Aaron Ballmanfe46e622014-04-28 13:01:32 +00001372 for (const auto *PI : D->params())
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00001373 Visitor.VisitParmVarDecl(PI);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001374
1375 // Visit all of the function's basic blocks.
Aaron Ballmanfe46e622014-04-28 13:01:32 +00001376 for (const auto *CurrBlock : *SortedGraph) {
Craig Topper25542942014-05-20 04:30:07 +00001377 if (!CurrStates)
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001378 CurrStates = BlockInfo.getInfo(CurrBlock);
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001379
1380 if (!CurrStates) {
1381 continue;
1382
1383 } else if (!CurrStates->isReachable()) {
1384 delete CurrStates;
Craig Topper25542942014-05-20 04:30:07 +00001385 CurrStates = nullptr;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001386 continue;
1387 }
1388
1389 Visitor.reset(CurrStates);
1390
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001391 // Visit all of the basic block's statements.
Aaron Ballman35897d92014-04-28 14:56:59 +00001392 for (const auto &B : *CurrBlock) {
1393 switch (B.getKind()) {
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001394 case CFGElement::Statement:
Aaron Ballman35897d92014-04-28 14:56:59 +00001395 Visitor.Visit(B.castAs<CFGStmt>().getStmt());
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001396 break;
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001397
1398 case CFGElement::TemporaryDtor: {
Aaron Ballman35897d92014-04-28 14:56:59 +00001399 const CFGTemporaryDtor &DTor = B.castAs<CFGTemporaryDtor>();
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001400 const CXXBindTemporaryExpr *BTE = DTor.getBindTemporaryExpr();
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001401
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001402 Visitor.checkCallability(PropagationInfo(BTE),
1403 DTor.getDestructorDecl(AC.getASTContext()),
1404 BTE->getExprLoc());
Manuel Klimekb33bded2014-05-08 11:50:00 +00001405 CurrStates->remove(BTE);
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001406 break;
1407 }
1408
1409 case CFGElement::AutomaticObjectDtor: {
Aaron Ballman35897d92014-04-28 14:56:59 +00001410 const CFGAutomaticObjDtor &DTor = B.castAs<CFGAutomaticObjDtor>();
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001411 SourceLocation Loc = DTor.getTriggerStmt()->getLocEnd();
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001412 const VarDecl *Var = DTor.getVarDecl();
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001413
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001414 Visitor.checkCallability(PropagationInfo(Var),
1415 DTor.getDestructorDecl(AC.getASTContext()),
1416 Loc);
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001417 break;
1418 }
1419
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001420 default:
1421 break;
1422 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001423 }
1424
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001425 // TODO: Handle other forms of branching with precision, including while-
1426 // and for-loops. (Deferred)
1427 if (!splitState(CurrBlock, Visitor)) {
Craig Topper25542942014-05-20 04:30:07 +00001428 CurrStates->setSource(nullptr);
1429
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001430 if (CurrBlock->succ_size() > 1 ||
1431 (CurrBlock->succ_size() == 1 &&
1432 (*CurrBlock->succ_begin())->pred_size() > 1)) {
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001433
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001434 bool OwnershipTaken = false;
1435
1436 for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
1437 SE = CurrBlock->succ_end(); SI != SE; ++SI) {
Craig Topper25542942014-05-20 04:30:07 +00001438
1439 if (*SI == nullptr) continue;
1440
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001441 if (BlockInfo.isBackEdge(CurrBlock, *SI)) {
1442 BlockInfo.borrowInfo(*SI)->intersectAtLoopHead(*SI, CurrBlock,
1443 CurrStates,
1444 WarningsHandler);
1445
1446 if (BlockInfo.allBackEdgesVisited(*SI, CurrBlock))
1447 BlockInfo.discardInfo(*SI);
1448 } else {
1449 BlockInfo.addInfo(*SI, CurrStates, OwnershipTaken);
1450 }
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001451 }
1452
1453 if (!OwnershipTaken)
1454 delete CurrStates;
Craig Topper25542942014-05-20 04:30:07 +00001455
1456 CurrStates = nullptr;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001457 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001458 }
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001459
1460 if (CurrBlock == &AC.getCFG()->getExit() &&
1461 D->getCallResultType()->isVoidType())
1462 CurrStates->checkParamsForReturnTypestate(D->getLocation(),
1463 WarningsHandler);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001464 } // End of block iterator.
1465
1466 // Delete the last existing state map.
1467 delete CurrStates;
1468
1469 WarningsHandler.emitDiagnostics();
1470}
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001471}} // end namespace clang::consumed