blob: 12c8a2c1b34135f3a17769bb914456dcdeda4b8c [file] [log] [blame]
Eugene Zelenkobc324332018-03-13 21:32:01 +00001//===- Consumed.cpp -------------------------------------------------------===//
DeLesley Hutchins48a31762013-08-12 21:20:55 +00002//
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
Mehdi Amini9670f842016-07-18 19:02:11 +000015#include "clang/Analysis/Analyses/Consumed.h"
DeLesley Hutchins48a31762013-08-12 21:20:55 +000016#include "clang/AST/Attr.h"
Eugene Zelenkobc324332018-03-13 21:32:01 +000017#include "clang/AST/Decl.h"
DeLesley Hutchins48a31762013-08-12 21:20:55 +000018#include "clang/AST/DeclCXX.h"
Eugene Zelenkobc324332018-03-13 21:32:01 +000019#include "clang/AST/Expr.h"
DeLesley Hutchins48a31762013-08-12 21:20:55 +000020#include "clang/AST/ExprCXX.h"
Eugene Zelenkobc324332018-03-13 21:32:01 +000021#include "clang/AST/Stmt.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000022#include "clang/AST/StmtVisitor.h"
DeLesley Hutchins48a31762013-08-12 21:20:55 +000023#include "clang/AST/Type.h"
24#include "clang/Analysis/Analyses/PostOrderCFGView.h"
George Karpenkov50657f62017-09-06 21:45:03 +000025#include "clang/Analysis/AnalysisDeclContext.h"
DeLesley Hutchins48a31762013-08-12 21:20:55 +000026#include "clang/Analysis/CFG.h"
Eugene Zelenkobc324332018-03-13 21:32:01 +000027#include "clang/Basic/LLVM.h"
DeLesley Hutchins48a31762013-08-12 21:20:55 +000028#include "clang/Basic/OperatorKinds.h"
29#include "clang/Basic/SourceLocation.h"
DeLesley Hutchins48a31762013-08-12 21:20:55 +000030#include "llvm/ADT/DenseMap.h"
Eugene Zelenkobc324332018-03-13 21:32:01 +000031#include "llvm/ADT/Optional.h"
32#include "llvm/ADT/STLExtras.h"
33#include "llvm/ADT/StringRef.h"
34#include "llvm/Support/Casting.h"
35#include "llvm/Support/ErrorHandling.h"
36#include <cassert>
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000037#include <memory>
Eugene Zelenkobc324332018-03-13 21:32:01 +000038#include <utility>
DeLesley Hutchins48a31762013-08-12 21:20:55 +000039
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +000040// TODO: Adjust states of args to constructors in the same way that arguments to
41// function calls are handled.
42// TODO: Use information from tests in for- and while-loop conditional.
Fangrui Song6907ce22018-07-30 19:24:48 +000043// TODO: Add notes about the actual and expected state for
DeLesley Hutchins5533ec52013-08-29 17:26:57 +000044// TODO: Correctly identify unreachable blocks when chaining boolean operators.
DeLesley Hutchins210791a2013-10-04 21:28:06 +000045// TODO: Adjust the parser and AttributesList class to support lists of
46// identifiers.
DeLesley Hutchins5533ec52013-08-29 17:26:57 +000047// TODO: Warn about unreachable code.
48// TODO: Switch to using a bitmap to track unreachable blocks.
DeLesley Hutchins5533ec52013-08-29 17:26:57 +000049// TODO: Handle variable definitions, e.g. bool valid = x.isValid();
50// if (valid) ...; (Deferred)
DeLesley Hutchins48a31762013-08-12 21:20:55 +000051// TODO: Take notes on state transitions to provide better warning messages.
52// (Deferred)
53// TODO: Test nested conditionals: A) Checking the same value multiple times,
54// and 2) Checking different values. (Deferred)
DeLesley Hutchins48a31762013-08-12 21:20:55 +000055
56using namespace clang;
57using namespace consumed;
58
59// Key method definition
Eugene Zelenkobc324332018-03-13 21:32:01 +000060ConsumedWarningsHandlerBase::~ConsumedWarningsHandlerBase() = default;
DeLesley Hutchins48a31762013-08-12 21:20:55 +000061
DeLesley Hutchins65013202013-10-17 18:19:31 +000062static SourceLocation getFirstStmtLoc(const CFGBlock *Block) {
63 // Find the source location of the first statement in the block, if the block
64 // is not empty.
Aaron Ballman35897d92014-04-28 14:56:59 +000065 for (const auto &B : *Block)
66 if (Optional<CFGStmt> CS = B.getAs<CFGStmt>())
Stephen Kellyf2ceec42018-08-09 21:08:08 +000067 return CS->getStmt()->getBeginLoc();
DeLesley Hutchins65013202013-10-17 18:19:31 +000068
69 // Block is empty.
70 // If we have one successor, return the first statement in that block
71 if (Block->succ_size() == 1 && *Block->succ_begin())
72 return getFirstStmtLoc(*Block->succ_begin());
73
Eugene Zelenkobc324332018-03-13 21:32:01 +000074 return {};
DeLesley Hutchins65013202013-10-17 18:19:31 +000075}
76
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +000077static SourceLocation getLastStmtLoc(const CFGBlock *Block) {
DeLesley Hutchins3277a612013-10-09 18:30:24 +000078 // Find the source location of the last statement in the block, if the block
79 // is not empty.
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +000080 if (const Stmt *StmtNode = Block->getTerminator()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000081 return StmtNode->getBeginLoc();
DeLesley Hutchins3277a612013-10-09 18:30:24 +000082 } else {
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +000083 for (CFGBlock::const_reverse_iterator BI = Block->rbegin(),
84 BE = Block->rend(); BI != BE; ++BI) {
DeLesley Hutchins3277a612013-10-09 18:30:24 +000085 if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>())
Stephen Kellyf2ceec42018-08-09 21:08:08 +000086 return CS->getStmt()->getBeginLoc();
DeLesley Hutchins3277a612013-10-09 18:30:24 +000087 }
88 }
DeLesley Hutchins65013202013-10-17 18:19:31 +000089
90 // If we have one successor, return the first statement in that block
91 SourceLocation Loc;
92 if (Block->succ_size() == 1 && *Block->succ_begin())
93 Loc = getFirstStmtLoc(*Block->succ_begin());
94 if (Loc.isValid())
95 return Loc;
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +000096
DeLesley Hutchins65013202013-10-17 18:19:31 +000097 // If we have one predecessor, return the last statement in that block
98 if (Block->pred_size() == 1 && *Block->pred_begin())
99 return getLastStmtLoc(*Block->pred_begin());
100
101 return Loc;
DeLesley Hutchins3277a612013-10-09 18:30:24 +0000102}
103
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000104static ConsumedState invertConsumedUnconsumed(ConsumedState State) {
105 switch (State) {
106 case CS_Unconsumed:
107 return CS_Consumed;
108 case CS_Consumed:
109 return CS_Unconsumed;
110 case CS_None:
111 return CS_None;
112 case CS_Unknown:
113 return CS_Unknown;
114 }
115 llvm_unreachable("invalid enum");
116}
117
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000118static bool isCallableInState(const CallableWhenAttr *CWAttr,
119 ConsumedState State) {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000120 for (const auto &S : CWAttr->callableStates()) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000121 ConsumedState MappedAttrState = CS_None;
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000122
123 switch (S) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000124 case CallableWhenAttr::Unknown:
125 MappedAttrState = CS_Unknown;
126 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000127
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000128 case CallableWhenAttr::Unconsumed:
129 MappedAttrState = CS_Unconsumed;
130 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000131
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000132 case CallableWhenAttr::Consumed:
133 MappedAttrState = CS_Consumed;
134 break;
135 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000136
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000137 if (MappedAttrState == State)
138 return true;
139 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000140
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000141 return false;
142}
143
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000144static bool isConsumableType(const QualType &QT) {
Chris Wailes93edffa2013-10-31 15:38:12 +0000145 if (QT->isPointerType() || QT->isReferenceType())
146 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000147
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000148 if (const CXXRecordDecl *RD = QT->getAsCXXRecordDecl())
149 return RD->hasAttr<ConsumableAttr>();
Fangrui Song6907ce22018-07-30 19:24:48 +0000150
Chris Wailes93edffa2013-10-31 15:38:12 +0000151 return false;
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000152}
153
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000154static bool isAutoCastType(const QualType &QT) {
155 if (QT->isPointerType() || QT->isReferenceType())
156 return false;
157
158 if (const CXXRecordDecl *RD = QT->getAsCXXRecordDecl())
159 return RD->hasAttr<ConsumableAutoCastAttr>();
160
161 return false;
162}
163
164static bool isSetOnReadPtrType(const QualType &QT) {
165 if (const CXXRecordDecl *RD = QT->getPointeeCXXRecordDecl())
166 return RD->hasAttr<ConsumableSetOnReadAttr>();
167 return false;
168}
169
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000170static bool isKnownState(ConsumedState State) {
171 switch (State) {
172 case CS_Unconsumed:
173 case CS_Consumed:
174 return true;
175 case CS_None:
176 case CS_Unknown:
177 return false;
178 }
Aaron Ballmana21f4b82013-08-29 20:36:09 +0000179 llvm_unreachable("invalid enum");
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000180}
181
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000182static bool isRValueRef(QualType ParamType) {
183 return ParamType->isRValueReferenceType();
DeLesley Hutchins0bd25892013-10-18 19:25:18 +0000184}
185
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000186static bool isTestingFunction(const FunctionDecl *FunDecl) {
Chris Wailes9385f9f2013-10-29 20:28:41 +0000187 return FunDecl->hasAttr<TestTypestateAttr>();
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000188}
189
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000190static bool isPointerOrRef(QualType ParamType) {
191 return ParamType->isPointerType() || ParamType->isReferenceType();
DeLesley Hutchins0bd25892013-10-18 19:25:18 +0000192}
193
David Blaikie16f76d22013-09-06 01:28:43 +0000194static ConsumedState mapConsumableAttrState(const QualType QT) {
195 assert(isConsumableType(QT));
196
197 const ConsumableAttr *CAttr =
198 QT->getAsCXXRecordDecl()->getAttr<ConsumableAttr>();
199
200 switch (CAttr->getDefaultState()) {
201 case ConsumableAttr::Unknown:
202 return CS_Unknown;
203 case ConsumableAttr::Unconsumed:
204 return CS_Unconsumed;
205 case ConsumableAttr::Consumed:
206 return CS_Consumed;
207 }
208 llvm_unreachable("invalid enum");
209}
210
DeLesley Hutchins69391772013-10-17 23:23:53 +0000211static ConsumedState
212mapParamTypestateAttrState(const ParamTypestateAttr *PTAttr) {
213 switch (PTAttr->getParamState()) {
214 case ParamTypestateAttr::Unknown:
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000215 return CS_Unknown;
DeLesley Hutchins69391772013-10-17 23:23:53 +0000216 case ParamTypestateAttr::Unconsumed:
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000217 return CS_Unconsumed;
DeLesley Hutchins69391772013-10-17 23:23:53 +0000218 case ParamTypestateAttr::Consumed:
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000219 return CS_Consumed;
220 }
221 llvm_unreachable("invalid_enum");
222}
223
Eric Christopherde156242013-09-03 20:43:00 +0000224static ConsumedState
225mapReturnTypestateAttrState(const ReturnTypestateAttr *RTSAttr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000226 switch (RTSAttr->getState()) {
227 case ReturnTypestateAttr::Unknown:
228 return CS_Unknown;
229 case ReturnTypestateAttr::Unconsumed:
230 return CS_Unconsumed;
231 case ReturnTypestateAttr::Consumed:
232 return CS_Consumed;
233 }
Eric Christopherde156242013-09-03 20:43:00 +0000234 llvm_unreachable("invalid enum");
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000235}
236
DeLesley Hutchins69391772013-10-17 23:23:53 +0000237static ConsumedState mapSetTypestateAttrState(const SetTypestateAttr *STAttr) {
238 switch (STAttr->getNewState()) {
239 case SetTypestateAttr::Unknown:
240 return CS_Unknown;
241 case SetTypestateAttr::Unconsumed:
242 return CS_Unconsumed;
243 case SetTypestateAttr::Consumed:
244 return CS_Consumed;
245 }
246 llvm_unreachable("invalid_enum");
247}
248
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000249static StringRef stateToString(ConsumedState State) {
250 switch (State) {
251 case consumed::CS_None:
252 return "none";
Fangrui Song6907ce22018-07-30 19:24:48 +0000253
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000254 case consumed::CS_Unknown:
255 return "unknown";
Fangrui Song6907ce22018-07-30 19:24:48 +0000256
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000257 case consumed::CS_Unconsumed:
258 return "unconsumed";
Fangrui Song6907ce22018-07-30 19:24:48 +0000259
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000260 case consumed::CS_Consumed:
261 return "consumed";
262 }
Reid Kleckner6454d0a2013-08-13 00:11:59 +0000263 llvm_unreachable("invalid enum");
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000264}
265
DeLesley Hutchins8d41d992013-10-11 22:30:48 +0000266static ConsumedState testsFor(const FunctionDecl *FunDecl) {
267 assert(isTestingFunction(FunDecl));
Chris Wailes9385f9f2013-10-29 20:28:41 +0000268 switch (FunDecl->getAttr<TestTypestateAttr>()->getTestState()) {
269 case TestTypestateAttr::Unconsumed:
DeLesley Hutchins8d41d992013-10-11 22:30:48 +0000270 return CS_Unconsumed;
Chris Wailes9385f9f2013-10-29 20:28:41 +0000271 case TestTypestateAttr::Consumed:
DeLesley Hutchins8d41d992013-10-11 22:30:48 +0000272 return CS_Consumed;
273 }
274 llvm_unreachable("invalid enum");
275}
276
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000277namespace {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000278
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000279struct VarTestResult {
280 const VarDecl *Var;
281 ConsumedState TestsFor;
282};
Eugene Zelenkobc324332018-03-13 21:32:01 +0000283
284} // namespace
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000285
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
Eugene Zelenkobc324332018-03-13 21:32:01 +0000302 } InfoType = IT_None;
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 };
Fangrui Song6907ce22018-07-30 19:24:48 +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 };
Fangrui Song6907ce22018-07-30 19:24:48 +0000318
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000319public:
Eugene Zelenkobc324332018-03-13 21:32:01 +0000320 PropagationInfo() = default;
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000321 PropagationInfo(const VarTestResult &VarTest)
Eugene Zelenkobc324332018-03-13 21:32:01 +0000322 : InfoType(IT_VarTest), VarTest(VarTest) {}
323
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000324 PropagationInfo(const VarDecl *Var, ConsumedState TestsFor)
Eugene Zelenkobc324332018-03-13 21:32:01 +0000325 : InfoType(IT_VarTest) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000326 VarTest.Var = Var;
327 VarTest.TestsFor = TestsFor;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000328 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000329
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000330 PropagationInfo(const BinaryOperator *Source, EffectiveOp EOp,
331 const VarTestResult &LTest, const VarTestResult &RTest)
Eugene Zelenkobc324332018-03-13 21:32:01 +0000332 : InfoType(IT_BinTest) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000333 BinTest.Source = Source;
334 BinTest.EOp = EOp;
335 BinTest.LTest = LTest;
336 BinTest.RTest = RTest;
337 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000338
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000339 PropagationInfo(const BinaryOperator *Source, EffectiveOp EOp,
340 const VarDecl *LVar, ConsumedState LTestsFor,
341 const VarDecl *RVar, ConsumedState RTestsFor)
Eugene Zelenkobc324332018-03-13 21:32:01 +0000342 : InfoType(IT_BinTest) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000343 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 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000350
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000351 PropagationInfo(ConsumedState State)
Eugene Zelenkobc324332018-03-13 21:32:01 +0000352 : InfoType(IT_State), State(State) {}
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000353 PropagationInfo(const VarDecl *Var) : InfoType(IT_Var), Var(Var) {}
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000354 PropagationInfo(const CXXBindTemporaryExpr *Tmp)
Eugene Zelenkobc324332018-03-13 21:32:01 +0000355 : InfoType(IT_Tmp), Tmp(Tmp) {}
Fangrui Song6907ce22018-07-30 19:24:48 +0000356
Eugene Zelenkobc324332018-03-13 21:32:01 +0000357 const ConsumedState &getState() const {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000358 assert(InfoType == IT_State);
359 return State;
360 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000361
Eugene Zelenkobc324332018-03-13 21:32:01 +0000362 const VarTestResult &getVarTest() const {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000363 assert(InfoType == IT_VarTest);
364 return VarTest;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000365 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000366
Eugene Zelenkobc324332018-03-13 21:32:01 +0000367 const VarTestResult &getLTest() const {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000368 assert(InfoType == IT_BinTest);
369 return BinTest.LTest;
370 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000371
Eugene Zelenkobc324332018-03-13 21:32:01 +0000372 const VarTestResult &getRTest() const {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000373 assert(InfoType == IT_BinTest);
374 return BinTest.RTest;
375 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000376
Eugene Zelenkobc324332018-03-13 21:32:01 +0000377 const VarDecl *getVar() const {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000378 assert(InfoType == IT_Var);
379 return Var;
380 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000381
Eugene Zelenkobc324332018-03-13 21:32:01 +0000382 const CXXBindTemporaryExpr *getTmp() const {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000383 assert(InfoType == IT_Tmp);
384 return Tmp;
385 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000386
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000387 ConsumedState getAsState(const ConsumedStateMap *StateMap) const {
388 assert(isVar() || isTmp() || isState());
Fangrui Song6907ce22018-07-30 19:24:48 +0000389
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000390 if (isVar())
391 return StateMap->getState(Var);
392 else if (isTmp())
393 return StateMap->getState(Tmp);
394 else if (isState())
395 return State;
396 else
397 return CS_None;
398 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000399
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000400 EffectiveOp testEffectiveOp() const {
401 assert(InfoType == IT_BinTest);
402 return BinTest.EOp;
403 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000404
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000405 const BinaryOperator * testSourceNode() const {
406 assert(InfoType == IT_BinTest);
407 return BinTest.Source;
408 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000409
Eugene Zelenkobc324332018-03-13 21:32:01 +0000410 bool isValid() const { return InfoType != IT_None; }
411 bool isState() const { return InfoType == IT_State; }
412 bool isVarTest() const { return InfoType == IT_VarTest; }
413 bool isBinTest() const { return InfoType == IT_BinTest; }
414 bool isVar() const { return InfoType == IT_Var; }
415 bool isTmp() const { return InfoType == IT_Tmp; }
Fangrui Song6907ce22018-07-30 19:24:48 +0000416
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000417 bool isTest() const {
418 return InfoType == IT_VarTest || InfoType == IT_BinTest;
419 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000420
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000421 bool isPointerToValue() const {
422 return InfoType == IT_Var || InfoType == IT_Tmp;
423 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000424
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000425 PropagationInfo invertTest() const {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000426 assert(InfoType == IT_VarTest || InfoType == IT_BinTest);
Fangrui Song6907ce22018-07-30 19:24:48 +0000427
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000428 if (InfoType == IT_VarTest) {
429 return PropagationInfo(VarTest.Var,
430 invertConsumedUnconsumed(VarTest.TestsFor));
Fangrui Song6907ce22018-07-30 19:24:48 +0000431
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000432 } else if (InfoType == IT_BinTest) {
433 return PropagationInfo(BinTest.Source,
434 BinTest.EOp == EO_And ? EO_Or : EO_And,
435 BinTest.LTest.Var, invertConsumedUnconsumed(BinTest.LTest.TestsFor),
436 BinTest.RTest.Var, invertConsumedUnconsumed(BinTest.RTest.TestsFor));
437 } else {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000438 return {};
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000439 }
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000440 }
441};
442
Eugene Zelenkobc324332018-03-13 21:32:01 +0000443} // namespace consumed
444} // namespace clang
445
446static void
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000447setStateForVarOrTmp(ConsumedStateMap *StateMap, const PropagationInfo &PInfo,
448 ConsumedState State) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000449 assert(PInfo.isVar() || PInfo.isTmp());
Fangrui Song6907ce22018-07-30 19:24:48 +0000450
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000451 if (PInfo.isVar())
452 StateMap->setState(PInfo.getVar(), State);
453 else
454 StateMap->setState(PInfo.getTmp(), State);
455}
456
Eugene Zelenkobc324332018-03-13 21:32:01 +0000457namespace clang {
458namespace consumed {
459
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000460class ConsumedStmtVisitor : public ConstStmtVisitor<ConsumedStmtVisitor> {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000461 using MapType = llvm::DenseMap<const Stmt *, PropagationInfo>;
462 using PairType= std::pair<const Stmt *, PropagationInfo>;
463 using InfoEntry = MapType::iterator;
464 using ConstInfoEntry = MapType::const_iterator;
Fangrui Song6907ce22018-07-30 19:24:48 +0000465
Reid Klecknere846dea2013-08-12 23:49:39 +0000466 AnalysisDeclContext &AC;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000467 ConsumedAnalyzer &Analyzer;
468 ConsumedStateMap *StateMap;
469 MapType PropagationMap;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000470
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000471 InfoEntry findInfo(const Expr *E) {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000472 if (const auto Cleanups = dyn_cast<ExprWithCleanups>(E))
Tim Shen4a05bb82016-06-21 20:29:17 +0000473 if (!Cleanups->cleanupsHaveSideEffects())
474 E = Cleanups->getSubExpr();
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000475 return PropagationMap.find(E->IgnoreParens());
476 }
Eugene Zelenkobc324332018-03-13 21:32:01 +0000477
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000478 ConstInfoEntry findInfo(const Expr *E) const {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000479 if (const auto Cleanups = dyn_cast<ExprWithCleanups>(E))
Tim Shen4a05bb82016-06-21 20:29:17 +0000480 if (!Cleanups->cleanupsHaveSideEffects())
481 E = Cleanups->getSubExpr();
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000482 return PropagationMap.find(E->IgnoreParens());
483 }
Eugene Zelenkobc324332018-03-13 21:32:01 +0000484
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000485 void insertInfo(const Expr *E, const PropagationInfo &PI) {
486 PropagationMap.insert(PairType(E->IgnoreParens(), PI));
487 }
488
489 void forwardInfo(const Expr *From, const Expr *To);
490 void copyInfo(const Expr *From, const Expr *To, ConsumedState CS);
491 ConsumedState getInfo(const Expr *From);
492 void setInfo(const Expr *To, ConsumedState NS);
493 void propagateReturnType(const Expr *Call, const FunctionDecl *Fun);
DeLesley Hutchins81218662013-10-18 23:11:49 +0000494
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000495public:
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000496 void checkCallability(const PropagationInfo &PInfo,
497 const FunctionDecl *FunDecl,
498 SourceLocation BlameLoc);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000499 bool handleCall(const CallExpr *Call, const Expr *ObjArg,
500 const FunctionDecl *FunD);
Fangrui Song6907ce22018-07-30 19:24:48 +0000501
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000502 void VisitBinaryOperator(const BinaryOperator *BinOp);
503 void VisitCallExpr(const CallExpr *Call);
504 void VisitCastExpr(const CastExpr *Cast);
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000505 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Temp);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000506 void VisitCXXConstructExpr(const CXXConstructExpr *Call);
507 void VisitCXXMemberCallExpr(const CXXMemberCallExpr *Call);
508 void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *Call);
509 void VisitDeclRefExpr(const DeclRefExpr *DeclRef);
510 void VisitDeclStmt(const DeclStmt *DelcS);
511 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Temp);
512 void VisitMemberExpr(const MemberExpr *MExpr);
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000513 void VisitParmVarDecl(const ParmVarDecl *Param);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000514 void VisitReturnStmt(const ReturnStmt *Ret);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000515 void VisitUnaryOperator(const UnaryOperator *UOp);
516 void VisitVarDecl(const VarDecl *Var);
Reid Klecknere846dea2013-08-12 23:49:39 +0000517
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000518 ConsumedStmtVisitor(AnalysisDeclContext &AC, ConsumedAnalyzer &Analyzer,
519 ConsumedStateMap *StateMap)
520 : AC(AC), Analyzer(Analyzer), StateMap(StateMap) {}
Fangrui Song6907ce22018-07-30 19:24:48 +0000521
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000522 PropagationInfo getInfo(const Expr *StmtNode) const {
523 ConstInfoEntry Entry = findInfo(StmtNode);
Fangrui Song6907ce22018-07-30 19:24:48 +0000524
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000525 if (Entry != PropagationMap.end())
526 return Entry->second;
527 else
Eugene Zelenkobc324332018-03-13 21:32:01 +0000528 return {};
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000529 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000530
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000531 void reset(ConsumedStateMap *NewStateMap) {
532 StateMap = NewStateMap;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000533 }
534};
535
Eugene Zelenkobc324332018-03-13 21:32:01 +0000536} // namespace consumed
537} // namespace clang
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000538
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000539void ConsumedStmtVisitor::forwardInfo(const Expr *From, const Expr *To) {
540 InfoEntry Entry = findInfo(From);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000541 if (Entry != PropagationMap.end())
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000542 insertInfo(To, Entry->second);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000543}
544
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000545// Create a new state for To, which is initialized to the state of From.
546// If NS is not CS_None, sets the state of From to NS.
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000547void ConsumedStmtVisitor::copyInfo(const Expr *From, const Expr *To,
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000548 ConsumedState NS) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000549 InfoEntry Entry = findInfo(From);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000550 if (Entry != PropagationMap.end()) {
551 PropagationInfo& PInfo = Entry->second;
552 ConsumedState CS = PInfo.getAsState(StateMap);
553 if (CS != CS_None)
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000554 insertInfo(To, PropagationInfo(CS));
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000555 if (NS != CS_None && PInfo.isPointerToValue())
556 setStateForVarOrTmp(StateMap, PInfo, NS);
557 }
558}
559
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000560// Get the ConsumedState for From
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000561ConsumedState ConsumedStmtVisitor::getInfo(const Expr *From) {
562 InfoEntry Entry = findInfo(From);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000563 if (Entry != PropagationMap.end()) {
564 PropagationInfo& PInfo = Entry->second;
565 return PInfo.getAsState(StateMap);
566 }
567 return CS_None;
568}
569
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000570// If we already have info for To then update it, otherwise create a new entry.
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000571void ConsumedStmtVisitor::setInfo(const Expr *To, ConsumedState NS) {
572 InfoEntry Entry = findInfo(To);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000573 if (Entry != PropagationMap.end()) {
574 PropagationInfo& PInfo = Entry->second;
575 if (PInfo.isPointerToValue())
576 setStateForVarOrTmp(StateMap, PInfo, NS);
577 } else if (NS != CS_None) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000578 insertInfo(To, PropagationInfo(NS));
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000579 }
580}
581
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000582void ConsumedStmtVisitor::checkCallability(const PropagationInfo &PInfo,
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000583 const FunctionDecl *FunDecl,
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000584 SourceLocation BlameLoc) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000585 assert(!PInfo.isTest());
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000586
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000587 const CallableWhenAttr *CWAttr = FunDecl->getAttr<CallableWhenAttr>();
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000588 if (!CWAttr)
589 return;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000590
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000591 if (PInfo.isVar()) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000592 ConsumedState VarState = StateMap->getState(PInfo.getVar());
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000593
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000594 if (VarState == CS_None || isCallableInState(CWAttr, VarState))
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000595 return;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000596
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000597 Analyzer.WarningsHandler.warnUseInInvalidState(
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000598 FunDecl->getNameAsString(), PInfo.getVar()->getNameAsString(),
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000599 stateToString(VarState), BlameLoc);
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000600 } else {
601 ConsumedState TmpState = PInfo.getAsState(StateMap);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000602
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000603 if (TmpState == CS_None || isCallableInState(CWAttr, TmpState))
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000604 return;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000605
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000606 Analyzer.WarningsHandler.warnUseOfTempInInvalidState(
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000607 FunDecl->getNameAsString(), stateToString(TmpState), BlameLoc);
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000608 }
609}
610
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000611// Factors out common behavior for function, method, and operator calls.
612// Check parameters and set parameter state if necessary.
613// Returns true if the state of ObjArg is set, or false otherwise.
614bool ConsumedStmtVisitor::handleCall(const CallExpr *Call, const Expr *ObjArg,
615 const FunctionDecl *FunD) {
616 unsigned Offset = 0;
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000617 if (isa<CXXOperatorCallExpr>(Call) && isa<CXXMethodDecl>(FunD))
618 Offset = 1; // first argument is 'this'
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000619
620 // check explicit parameters
621 for (unsigned Index = Offset; Index < Call->getNumArgs(); ++Index) {
622 // Skip variable argument lists.
623 if (Index - Offset >= FunD->getNumParams())
624 break;
625
626 const ParmVarDecl *Param = FunD->getParamDecl(Index - Offset);
627 QualType ParamType = Param->getType();
628
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000629 InfoEntry Entry = findInfo(Call->getArg(Index));
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000630
631 if (Entry == PropagationMap.end() || Entry->second.isTest())
632 continue;
633 PropagationInfo PInfo = Entry->second;
634
635 // Check that the parameter is in the correct state.
636 if (ParamTypestateAttr *PTA = Param->getAttr<ParamTypestateAttr>()) {
637 ConsumedState ParamState = PInfo.getAsState(StateMap);
638 ConsumedState ExpectedState = mapParamTypestateAttrState(PTA);
639
640 if (ParamState != ExpectedState)
641 Analyzer.WarningsHandler.warnParamTypestateMismatch(
642 Call->getArg(Index)->getExprLoc(),
643 stateToString(ExpectedState), stateToString(ParamState));
644 }
645
646 if (!(Entry->second.isVar() || Entry->second.isTmp()))
647 continue;
648
649 // Adjust state on the caller side.
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000650 if (isRValueRef(ParamType))
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000651 setStateForVarOrTmp(StateMap, PInfo, consumed::CS_Consumed);
652 else if (ReturnTypestateAttr *RT = Param->getAttr<ReturnTypestateAttr>())
653 setStateForVarOrTmp(StateMap, PInfo, mapReturnTypestateAttrState(RT));
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000654 else if (isPointerOrRef(ParamType) &&
655 (!ParamType->getPointeeType().isConstQualified() ||
656 isSetOnReadPtrType(ParamType)))
657 setStateForVarOrTmp(StateMap, PInfo, consumed::CS_Unknown);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000658 }
659
660 if (!ObjArg)
661 return false;
662
663 // check implicit 'self' parameter, if present
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000664 InfoEntry Entry = findInfo(ObjArg);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000665 if (Entry != PropagationMap.end()) {
666 PropagationInfo PInfo = Entry->second;
667 checkCallability(PInfo, FunD, Call->getExprLoc());
668
669 if (SetTypestateAttr *STA = FunD->getAttr<SetTypestateAttr>()) {
670 if (PInfo.isVar()) {
671 StateMap->setState(PInfo.getVar(), mapSetTypestateAttrState(STA));
672 return true;
673 }
674 else if (PInfo.isTmp()) {
675 StateMap->setState(PInfo.getTmp(), mapSetTypestateAttrState(STA));
676 return true;
677 }
678 }
679 else if (isTestingFunction(FunD) && PInfo.isVar()) {
680 PropagationMap.insert(PairType(Call,
681 PropagationInfo(PInfo.getVar(), testsFor(FunD))));
682 }
683 }
684 return false;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000685}
686
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000687void ConsumedStmtVisitor::propagateReturnType(const Expr *Call,
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000688 const FunctionDecl *Fun) {
689 QualType RetType = Fun->getCallResultType();
690 if (RetType->isReferenceType())
691 RetType = RetType->getPointeeType();
692
693 if (isConsumableType(RetType)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000694 ConsumedState ReturnState;
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000695 if (ReturnTypestateAttr *RTA = Fun->getAttr<ReturnTypestateAttr>())
696 ReturnState = mapReturnTypestateAttrState(RTA);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000697 else
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000698 ReturnState = mapConsumableAttrState(RetType);
Fangrui Song6907ce22018-07-30 19:24:48 +0000699
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000700 PropagationMap.insert(PairType(Call, PropagationInfo(ReturnState)));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000701 }
702}
703
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());
Fangrui Song6907ce22018-07-30 19:24:48 +0000710
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000711 VarTestResult LTest, RTest;
Fangrui Song6907ce22018-07-30 19:24:48 +0000712
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 } else {
Craig Topper25542942014-05-20 04:30:07 +0000716 LTest.Var = nullptr;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000717 LTest.TestsFor = CS_None;
718 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000719
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000720 if (REntry != PropagationMap.end() && REntry->second.isVarTest()) {
721 RTest = REntry->second.getVarTest();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000722 } else {
Craig Topper25542942014-05-20 04:30:07 +0000723 RTest.Var = nullptr;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000724 RTest.TestsFor = CS_None;
725 }
Craig Topper25542942014-05-20 04:30:07 +0000726
727 if (!(LTest.Var == nullptr && RTest.Var == nullptr))
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000728 PropagationMap.insert(PairType(BinOp, PropagationInfo(BinOp,
729 static_cast<EffectiveOp>(BinOp->getOpcode() == BO_LOr), LTest, RTest)));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000730 break;
731 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000732
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000733 case BO_PtrMemD:
734 case BO_PtrMemI:
735 forwardInfo(BinOp->getLHS(), BinOp);
736 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000737
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000738 default:
739 break;
740 }
741}
742
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000743void ConsumedStmtVisitor::VisitCallExpr(const CallExpr *Call) {
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000744 const FunctionDecl *FunDecl = Call->getDirectCallee();
745 if (!FunDecl)
746 return;
747
748 // Special case for the std::move function.
749 // TODO: Make this more specific. (Deferred)
Nico Weberb688d132017-09-28 16:16:39 +0000750 if (Call->isCallToStdMove()) {
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) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000765
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000766 InfoEntry Entry = findInfo(Temp->getSubExpr());
Fangrui Song6907ce22018-07-30 19:24:48 +0000767
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000768 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();
Fangrui Song6907ce22018-07-30 19:24:48 +0000779
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000780 if (!isConsumableType(ThisType))
781 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000782
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000783 // FIXME: What should happen if someone annotates the move constructor?
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000784 if (ReturnTypestateAttr *RTA = Constructor->getAttr<ReturnTypestateAttr>()) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000785 // TODO: Adjust state of args appropriately.
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000786 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
806void ConsumedStmtVisitor::VisitCXXMemberCallExpr(
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000807 const CXXMemberCallExpr *Call) {
808 CXXMethodDecl* MD = Call->getMethodDecl();
809 if (!MD)
810 return;
811
812 handleCall(Call, Call->getImplicitObjectArgument(), MD);
813 propagateReturnType(Call, MD);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000814}
815
816void ConsumedStmtVisitor::VisitCXXOperatorCallExpr(
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000817 const CXXOperatorCallExpr *Call) {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000818 const auto *FunDecl = dyn_cast_or_null<FunctionDecl>(Call->getDirectCallee());
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000819 if (!FunDecl) return;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000820
821 if (Call->getOperator() == OO_Equal) {
822 ConsumedState CS = getInfo(Call->getArg(1));
823 if (!handleCall(Call, Call->getArg(0), FunDecl))
824 setInfo(Call->getArg(0), CS);
825 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000826 }
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000827
Eugene Zelenkobc324332018-03-13 21:32:01 +0000828 if (const auto *MCall = dyn_cast<CXXMemberCallExpr>(Call))
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000829 handleCall(MCall, MCall->getImplicitObjectArgument(), FunDecl);
830 else
831 handleCall(Call, Call->getArg(0), FunDecl);
832
833 propagateReturnType(Call, FunDecl);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000834}
835
836void ConsumedStmtVisitor::VisitDeclRefExpr(const DeclRefExpr *DeclRef) {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000837 if (const auto *Var = dyn_cast_or_null<VarDecl>(DeclRef->getDecl()))
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000838 if (StateMap->getState(Var) != consumed::CS_None)
839 PropagationMap.insert(PairType(DeclRef, PropagationInfo(Var)));
840}
841
842void ConsumedStmtVisitor::VisitDeclStmt(const DeclStmt *DeclS) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000843 for (const auto *DI : DeclS->decls())
Aaron Ballman535bbcc2014-03-14 17:01:24 +0000844 if (isa<VarDecl>(DI))
845 VisitVarDecl(cast<VarDecl>(DI));
Fangrui Song6907ce22018-07-30 19:24:48 +0000846
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000847 if (DeclS->isSingleDecl())
Eugene Zelenkobc324332018-03-13 21:32:01 +0000848 if (const auto *Var = dyn_cast_or_null<VarDecl>(DeclS->getSingleDecl()))
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000849 PropagationMap.insert(PairType(DeclS, PropagationInfo(Var)));
850}
851
852void ConsumedStmtVisitor::VisitMaterializeTemporaryExpr(
853 const MaterializeTemporaryExpr *Temp) {
Chris Wailes44930882013-10-24 14:28:17 +0000854 forwardInfo(Temp->GetTemporaryExpr(), Temp);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000855}
856
857void ConsumedStmtVisitor::VisitMemberExpr(const MemberExpr *MExpr) {
858 forwardInfo(MExpr->getBase(), MExpr);
859}
860
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000861void ConsumedStmtVisitor::VisitParmVarDecl(const ParmVarDecl *Param) {
David Blaikie16f76d22013-09-06 01:28:43 +0000862 QualType ParamType = Param->getType();
863 ConsumedState ParamState = consumed::CS_None;
Fangrui Song6907ce22018-07-30 19:24:48 +0000864
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000865 if (const ParamTypestateAttr *PTA = Param->getAttr<ParamTypestateAttr>())
Fangrui Song6907ce22018-07-30 19:24:48 +0000866 ParamState = mapParamTypestateAttrState(PTA);
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000867 else if (isConsumableType(ParamType))
Fangrui Song6907ce22018-07-30 19:24:48 +0000868 ParamState = mapConsumableAttrState(ParamType);
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000869 else if (isRValueRef(ParamType) &&
870 isConsumableType(ParamType->getPointeeType()))
Fangrui Song6907ce22018-07-30 19:24:48 +0000871 ParamState = mapConsumableAttrState(ParamType->getPointeeType());
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000872 else if (ParamType->isReferenceType() &&
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000873 isConsumableType(ParamType->getPointeeType()))
David Blaikie16f76d22013-09-06 01:28:43 +0000874 ParamState = consumed::CS_Unknown;
Fangrui Song6907ce22018-07-30 19:24:48 +0000875
DeLesley Hutchins69391772013-10-17 23:23:53 +0000876 if (ParamState != CS_None)
David Blaikie16f76d22013-09-06 01:28:43 +0000877 StateMap->setState(Param, ParamState);
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000878}
879
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000880void ConsumedStmtVisitor::VisitReturnStmt(const ReturnStmt *Ret) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000881 ConsumedState ExpectedState = Analyzer.getExpectedReturnState();
Fangrui Song6907ce22018-07-30 19:24:48 +0000882
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000883 if (ExpectedState != CS_None) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000884 InfoEntry Entry = findInfo(Ret->getRetValue());
Fangrui Song6907ce22018-07-30 19:24:48 +0000885
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000886 if (Entry != PropagationMap.end()) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000887 ConsumedState RetState = Entry->second.getAsState(StateMap);
Fangrui Song6907ce22018-07-30 19:24:48 +0000888
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000889 if (RetState != ExpectedState)
890 Analyzer.WarningsHandler.warnReturnTypestateMismatch(
891 Ret->getReturnLoc(), stateToString(ExpectedState),
892 stateToString(RetState));
893 }
894 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000895
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000896 StateMap->checkParamsForReturnTypestate(Ret->getBeginLoc(),
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000897 Analyzer.WarningsHandler);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000898}
899
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000900void ConsumedStmtVisitor::VisitUnaryOperator(const UnaryOperator *UOp) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000901 InfoEntry Entry = findInfo(UOp->getSubExpr());
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000902 if (Entry == PropagationMap.end()) return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000903
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000904 switch (UOp->getOpcode()) {
905 case UO_AddrOf:
906 PropagationMap.insert(PairType(UOp, Entry->second));
907 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000908
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000909 case UO_LNot:
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000910 if (Entry->second.isTest())
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000911 PropagationMap.insert(PairType(UOp, Entry->second.invertTest()));
912 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000913
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000914 default:
915 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000916 }
917}
918
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000919// TODO: See if I need to check for reference types here.
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000920void ConsumedStmtVisitor::VisitVarDecl(const VarDecl *Var) {
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000921 if (isConsumableType(Var->getType())) {
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000922 if (Var->hasInit()) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000923 MapType::iterator VIT = findInfo(Var->getInit()->IgnoreImplicit());
DeLesley Hutchins81218662013-10-18 23:11:49 +0000924 if (VIT != PropagationMap.end()) {
925 PropagationInfo PInfo = VIT->second;
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000926 ConsumedState St = PInfo.getAsState(StateMap);
Fangrui Song6907ce22018-07-30 19:24:48 +0000927
DeLesley Hutchins81218662013-10-18 23:11:49 +0000928 if (St != consumed::CS_None) {
929 StateMap->setState(Var, St);
930 return;
931 }
932 }
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000933 }
DeLesley Hutchins81218662013-10-18 23:11:49 +0000934 // Otherwise
935 StateMap->setState(Var, consumed::CS_Unknown);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000936 }
937}
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000938
Benjamin Kramer3a743452015-03-09 15:03:32 +0000939static void splitVarStateForIf(const IfStmt *IfNode, const VarTestResult &Test,
940 ConsumedStateMap *ThenStates,
941 ConsumedStateMap *ElseStates) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000942 ConsumedState VarState = ThenStates->getState(Test.Var);
Fangrui Song6907ce22018-07-30 19:24:48 +0000943
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000944 if (VarState == CS_Unknown) {
945 ThenStates->setState(Test.Var, Test.TestsFor);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000946 ElseStates->setState(Test.Var, invertConsumedUnconsumed(Test.TestsFor));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000947 } else if (VarState == invertConsumedUnconsumed(Test.TestsFor)) {
948 ThenStates->markUnreachable();
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000949 } else if (VarState == Test.TestsFor) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000950 ElseStates->markUnreachable();
951 }
952}
953
Benjamin Kramer3a743452015-03-09 15:03:32 +0000954static void splitVarStateForIfBinOp(const PropagationInfo &PInfo,
955 ConsumedStateMap *ThenStates,
956 ConsumedStateMap *ElseStates) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000957 const VarTestResult &LTest = PInfo.getLTest(),
958 &RTest = PInfo.getRTest();
Fangrui Song6907ce22018-07-30 19:24:48 +0000959
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000960 ConsumedState LState = LTest.Var ? ThenStates->getState(LTest.Var) : CS_None,
961 RState = RTest.Var ? ThenStates->getState(RTest.Var) : CS_None;
Fangrui Song6907ce22018-07-30 19:24:48 +0000962
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000963 if (LTest.Var) {
964 if (PInfo.testEffectiveOp() == EO_And) {
965 if (LState == CS_Unknown) {
966 ThenStates->setState(LTest.Var, LTest.TestsFor);
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000967 } else if (LState == invertConsumedUnconsumed(LTest.TestsFor)) {
968 ThenStates->markUnreachable();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000969 } else if (LState == LTest.TestsFor && isKnownState(RState)) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000970 if (RState == RTest.TestsFor)
971 ElseStates->markUnreachable();
972 else
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000973 ThenStates->markUnreachable();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000974 }
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000975 } else {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000976 if (LState == CS_Unknown) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000977 ElseStates->setState(LTest.Var,
978 invertConsumedUnconsumed(LTest.TestsFor));
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000979 } else if (LState == LTest.TestsFor) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000980 ElseStates->markUnreachable();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000981 } else if (LState == invertConsumedUnconsumed(LTest.TestsFor) &&
982 isKnownState(RState)) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000983 if (RState == RTest.TestsFor)
984 ElseStates->markUnreachable();
985 else
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000986 ThenStates->markUnreachable();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000987 }
988 }
989 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000990
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000991 if (RTest.Var) {
992 if (PInfo.testEffectiveOp() == EO_And) {
993 if (RState == CS_Unknown)
994 ThenStates->setState(RTest.Var, RTest.TestsFor);
995 else if (RState == invertConsumedUnconsumed(RTest.TestsFor))
996 ThenStates->markUnreachable();
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000997 } else {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000998 if (RState == CS_Unknown)
999 ElseStates->setState(RTest.Var,
1000 invertConsumedUnconsumed(RTest.TestsFor));
1001 else if (RState == RTest.TestsFor)
1002 ElseStates->markUnreachable();
1003 }
1004 }
1005}
1006
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001007bool ConsumedBlockInfo::allBackEdgesVisited(const CFGBlock *CurrBlock,
1008 const CFGBlock *TargetBlock) {
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001009 assert(CurrBlock && "Block pointer must not be NULL");
1010 assert(TargetBlock && "TargetBlock pointer must not be NULL");
Fangrui Song6907ce22018-07-30 19:24:48 +00001011
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001012 unsigned int CurrBlockOrder = VisitOrder[CurrBlock->getBlockID()];
1013 for (CFGBlock::const_pred_iterator PI = TargetBlock->pred_begin(),
1014 PE = TargetBlock->pred_end(); PI != PE; ++PI) {
1015 if (*PI && CurrBlockOrder < VisitOrder[(*PI)->getBlockID()] )
1016 return false;
1017 }
1018 return true;
1019}
1020
David Blaikie86d8cf32015-08-14 01:26:19 +00001021void ConsumedBlockInfo::addInfo(
1022 const CFGBlock *Block, ConsumedStateMap *StateMap,
1023 std::unique_ptr<ConsumedStateMap> &OwnedStateMap) {
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001024 assert(Block && "Block pointer must not be NULL");
David Blaikie86d8cf32015-08-14 01:26:19 +00001025
1026 auto &Entry = StateMapsArray[Block->getBlockID()];
1027
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001028 if (Entry) {
David Blaikie86d8cf32015-08-14 01:26:19 +00001029 Entry->intersect(*StateMap);
1030 } else if (OwnedStateMap)
1031 Entry = std::move(OwnedStateMap);
1032 else
1033 Entry = llvm::make_unique<ConsumedStateMap>(*StateMap);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001034}
1035
1036void ConsumedBlockInfo::addInfo(const CFGBlock *Block,
David Blaikie86d8cf32015-08-14 01:26:19 +00001037 std::unique_ptr<ConsumedStateMap> StateMap) {
Craig Topper25542942014-05-20 04:30:07 +00001038 assert(Block && "Block pointer must not be NULL");
1039
David Blaikie86d8cf32015-08-14 01:26:19 +00001040 auto &Entry = StateMapsArray[Block->getBlockID()];
1041
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001042 if (Entry) {
David Blaikie86d8cf32015-08-14 01:26:19 +00001043 Entry->intersect(*StateMap);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001044 } else {
David Blaikie86d8cf32015-08-14 01:26:19 +00001045 Entry = std::move(StateMap);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001046 }
1047}
1048
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001049ConsumedStateMap* ConsumedBlockInfo::borrowInfo(const CFGBlock *Block) {
1050 assert(Block && "Block pointer must not be NULL");
1051 assert(StateMapsArray[Block->getBlockID()] && "Block has no block info");
David Blaikie86d8cf32015-08-14 01:26:19 +00001052
1053 return StateMapsArray[Block->getBlockID()].get();
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001054}
1055
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001056void ConsumedBlockInfo::discardInfo(const CFGBlock *Block) {
David Blaikie86d8cf32015-08-14 01:26:19 +00001057 StateMapsArray[Block->getBlockID()] = nullptr;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001058}
1059
David Blaikie86d8cf32015-08-14 01:26:19 +00001060std::unique_ptr<ConsumedStateMap>
1061ConsumedBlockInfo::getInfo(const CFGBlock *Block) {
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001062 assert(Block && "Block pointer must not be NULL");
David Blaikie86d8cf32015-08-14 01:26:19 +00001063
1064 auto &Entry = StateMapsArray[Block->getBlockID()];
1065 return isBackEdgeTarget(Block) ? llvm::make_unique<ConsumedStateMap>(*Entry)
1066 : std::move(Entry);
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001067}
1068
1069bool ConsumedBlockInfo::isBackEdge(const CFGBlock *From, const CFGBlock *To) {
1070 assert(From && "From block must not be NULL");
1071 assert(To && "From block must not be NULL");
Fangrui Song6907ce22018-07-30 19:24:48 +00001072
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001073 return VisitOrder[From->getBlockID()] > VisitOrder[To->getBlockID()];
1074}
1075
1076bool ConsumedBlockInfo::isBackEdgeTarget(const CFGBlock *Block) {
Craig Topper25542942014-05-20 04:30:07 +00001077 assert(Block && "Block pointer must not be NULL");
1078
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001079 // Anything with less than two predecessors can't be the target of a back
1080 // edge.
1081 if (Block->pred_size() < 2)
1082 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001083
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001084 unsigned int BlockVisitOrder = VisitOrder[Block->getBlockID()];
1085 for (CFGBlock::const_pred_iterator PI = Block->pred_begin(),
1086 PE = Block->pred_end(); PI != PE; ++PI) {
1087 if (*PI && BlockVisitOrder < VisitOrder[(*PI)->getBlockID()])
1088 return true;
1089 }
1090 return false;
1091}
1092
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001093void ConsumedStateMap::checkParamsForReturnTypestate(SourceLocation BlameLoc,
1094 ConsumedWarningsHandlerBase &WarningsHandler) const {
Fangrui Song6907ce22018-07-30 19:24:48 +00001095
Aaron Ballman35897d92014-04-28 14:56:59 +00001096 for (const auto &DM : VarMap) {
1097 if (isa<ParmVarDecl>(DM.first)) {
Eugene Zelenkobc324332018-03-13 21:32:01 +00001098 const auto *Param = cast<ParmVarDecl>(DM.first);
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +00001099 const ReturnTypestateAttr *RTA = Param->getAttr<ReturnTypestateAttr>();
Fangrui Song6907ce22018-07-30 19:24:48 +00001100
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +00001101 if (!RTA)
1102 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00001103
1104 ConsumedState ExpectedState = mapReturnTypestateAttrState(RTA);
Aaron Ballman35897d92014-04-28 14:56:59 +00001105 if (DM.second != ExpectedState)
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001106 WarningsHandler.warnParamReturnTypestateMismatch(BlameLoc,
1107 Param->getNameAsString(), stateToString(ExpectedState),
Aaron Ballman35897d92014-04-28 14:56:59 +00001108 stateToString(DM.second));
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001109 }
1110 }
1111}
1112
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001113void ConsumedStateMap::clearTemporaries() {
1114 TmpMap.clear();
1115}
1116
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001117ConsumedState ConsumedStateMap::getState(const VarDecl *Var) const {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001118 VarMapType::const_iterator Entry = VarMap.find(Var);
Fangrui Song6907ce22018-07-30 19:24:48 +00001119
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001120 if (Entry != VarMap.end())
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001121 return Entry->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00001122
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001123 return CS_None;
1124}
1125
1126ConsumedState
1127ConsumedStateMap::getState(const CXXBindTemporaryExpr *Tmp) const {
1128 TmpMapType::const_iterator Entry = TmpMap.find(Tmp);
Fangrui Song6907ce22018-07-30 19:24:48 +00001129
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001130 if (Entry != TmpMap.end())
1131 return Entry->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00001132
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001133 return CS_None;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001134}
1135
David Blaikie86d8cf32015-08-14 01:26:19 +00001136void ConsumedStateMap::intersect(const ConsumedStateMap &Other) {
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001137 ConsumedState LocalState;
David Blaikie86d8cf32015-08-14 01:26:19 +00001138
1139 if (this->From && this->From == Other.From && !Other.Reachable) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001140 this->markUnreachable();
1141 return;
1142 }
David Blaikie86d8cf32015-08-14 01:26:19 +00001143
1144 for (const auto &DM : Other.VarMap) {
Aaron Ballman35897d92014-04-28 14:56:59 +00001145 LocalState = this->getState(DM.first);
Fangrui Song6907ce22018-07-30 19:24:48 +00001146
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001147 if (LocalState == CS_None)
1148 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00001149
Aaron Ballman35897d92014-04-28 14:56:59 +00001150 if (LocalState != DM.second)
1151 VarMap[DM.first] = CS_Unknown;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001152 }
1153}
1154
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001155void ConsumedStateMap::intersectAtLoopHead(const CFGBlock *LoopHead,
1156 const CFGBlock *LoopBack, const ConsumedStateMap *LoopBackStates,
1157 ConsumedWarningsHandlerBase &WarningsHandler) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001158
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001159 ConsumedState LocalState;
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001160 SourceLocation BlameLoc = getLastStmtLoc(LoopBack);
Fangrui Song6907ce22018-07-30 19:24:48 +00001161
1162 for (const auto &DM : LoopBackStates->VarMap) {
Aaron Ballman35897d92014-04-28 14:56:59 +00001163 LocalState = this->getState(DM.first);
Fangrui Song6907ce22018-07-30 19:24:48 +00001164
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001165 if (LocalState == CS_None)
1166 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00001167
Aaron Ballman35897d92014-04-28 14:56:59 +00001168 if (LocalState != DM.second) {
1169 VarMap[DM.first] = CS_Unknown;
Aaron Ballmanfe46e622014-04-28 13:01:32 +00001170 WarningsHandler.warnLoopStateMismatch(BlameLoc,
Aaron Ballman35897d92014-04-28 14:56:59 +00001171 DM.first->getNameAsString());
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001172 }
1173 }
1174}
1175
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001176void ConsumedStateMap::markUnreachable() {
1177 this->Reachable = false;
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001178 VarMap.clear();
1179 TmpMap.clear();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001180}
1181
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001182void ConsumedStateMap::setState(const VarDecl *Var, ConsumedState State) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001183 VarMap[Var] = State;
1184}
1185
1186void ConsumedStateMap::setState(const CXXBindTemporaryExpr *Tmp,
1187 ConsumedState State) {
1188 TmpMap[Tmp] = State;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001189}
1190
Manuel Klimekb33bded2014-05-08 11:50:00 +00001191void ConsumedStateMap::remove(const CXXBindTemporaryExpr *Tmp) {
1192 TmpMap.erase(Tmp);
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001193}
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001194
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001195bool ConsumedStateMap::operator!=(const ConsumedStateMap *Other) const {
Aaron Ballman35897d92014-04-28 14:56:59 +00001196 for (const auto &DM : Other->VarMap)
1197 if (this->getState(DM.first) != DM.second)
Fangrui Song6907ce22018-07-30 19:24:48 +00001198 return true;
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001199 return false;
1200}
1201
David Blaikie16f76d22013-09-06 01:28:43 +00001202void ConsumedAnalyzer::determineExpectedReturnState(AnalysisDeclContext &AC,
1203 const FunctionDecl *D) {
1204 QualType ReturnType;
Eugene Zelenkobc324332018-03-13 21:32:01 +00001205 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
David Blaikie16f76d22013-09-06 01:28:43 +00001206 ASTContext &CurrContext = AC.getASTContext();
1207 ReturnType = Constructor->getThisType(CurrContext)->getPointeeType();
1208 } else
1209 ReturnType = D->getCallResultType();
1210
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +00001211 if (const ReturnTypestateAttr *RTSAttr = D->getAttr<ReturnTypestateAttr>()) {
David Blaikie16f76d22013-09-06 01:28:43 +00001212 const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1213 if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1214 // FIXME: This should be removed when template instantiation propagates
1215 // attributes at template specialization definition, not
1216 // declaration. When it is removed the test needs to be enabled
1217 // in SemaDeclAttr.cpp.
1218 WarningsHandler.warnReturnTypestateForUnconsumableType(
1219 RTSAttr->getLocation(), ReturnType.getAsString());
1220 ExpectedReturnState = CS_None;
1221 } else
1222 ExpectedReturnState = mapReturnTypestateAttrState(RTSAttr);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00001223 } else if (isConsumableType(ReturnType)) {
1224 if (isAutoCastType(ReturnType)) // We can auto-cast the state to the
1225 ExpectedReturnState = CS_None; // expected state.
1226 else
1227 ExpectedReturnState = mapConsumableAttrState(ReturnType);
1228 }
David Blaikie16f76d22013-09-06 01:28:43 +00001229 else
1230 ExpectedReturnState = CS_None;
1231}
1232
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001233bool ConsumedAnalyzer::splitState(const CFGBlock *CurrBlock,
1234 const ConsumedStmtVisitor &Visitor) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001235 std::unique_ptr<ConsumedStateMap> FalseStates(
1236 new ConsumedStateMap(*CurrStates));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001237 PropagationInfo PInfo;
Fangrui Song6907ce22018-07-30 19:24:48 +00001238
Eugene Zelenkobc324332018-03-13 21:32:01 +00001239 if (const auto *IfNode =
1240 dyn_cast_or_null<IfStmt>(CurrBlock->getTerminator().getStmt())) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +00001241 const Expr *Cond = IfNode->getCond();
Fangrui Song6907ce22018-07-30 19:24:48 +00001242
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001243 PInfo = Visitor.getInfo(Cond);
1244 if (!PInfo.isValid() && isa<BinaryOperator>(Cond))
1245 PInfo = Visitor.getInfo(cast<BinaryOperator>(Cond)->getRHS());
Fangrui Song6907ce22018-07-30 19:24:48 +00001246
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001247 if (PInfo.isVarTest()) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001248 CurrStates->setSource(Cond);
1249 FalseStates->setSource(Cond);
David Blaikie86d8cf32015-08-14 01:26:19 +00001250 splitVarStateForIf(IfNode, PInfo.getVarTest(), CurrStates.get(),
Chris Wailes2dc8c422013-10-25 15:33:28 +00001251 FalseStates.get());
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001252 } else if (PInfo.isBinTest()) {
1253 CurrStates->setSource(PInfo.testSourceNode());
1254 FalseStates->setSource(PInfo.testSourceNode());
David Blaikie86d8cf32015-08-14 01:26:19 +00001255 splitVarStateForIfBinOp(PInfo, CurrStates.get(), FalseStates.get());
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001256 } else {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001257 return false;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001258 }
Eugene Zelenkobc324332018-03-13 21:32:01 +00001259 } else if (const auto *BinOp =
1260 dyn_cast_or_null<BinaryOperator>(CurrBlock->getTerminator().getStmt())) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001261 PInfo = Visitor.getInfo(BinOp->getLHS());
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001262 if (!PInfo.isVarTest()) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001263 if ((BinOp = dyn_cast_or_null<BinaryOperator>(BinOp->getLHS()))) {
1264 PInfo = Visitor.getInfo(BinOp->getRHS());
Fangrui Song6907ce22018-07-30 19:24:48 +00001265
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001266 if (!PInfo.isVarTest())
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001267 return false;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001268 } else {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001269 return false;
1270 }
1271 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001272
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001273 CurrStates->setSource(BinOp);
1274 FalseStates->setSource(BinOp);
Fangrui Song6907ce22018-07-30 19:24:48 +00001275
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001276 const VarTestResult &Test = PInfo.getVarTest();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001277 ConsumedState VarState = CurrStates->getState(Test.Var);
Fangrui Song6907ce22018-07-30 19:24:48 +00001278
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001279 if (BinOp->getOpcode() == BO_LAnd) {
1280 if (VarState == CS_Unknown)
1281 CurrStates->setState(Test.Var, Test.TestsFor);
1282 else if (VarState == invertConsumedUnconsumed(Test.TestsFor))
1283 CurrStates->markUnreachable();
Fangrui Song6907ce22018-07-30 19:24:48 +00001284
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001285 } else if (BinOp->getOpcode() == BO_LOr) {
1286 if (VarState == CS_Unknown)
1287 FalseStates->setState(Test.Var,
1288 invertConsumedUnconsumed(Test.TestsFor));
1289 else if (VarState == Test.TestsFor)
1290 FalseStates->markUnreachable();
1291 }
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001292 } else {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001293 return false;
1294 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001295
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001296 CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00001297
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001298 if (*SI)
David Blaikie86d8cf32015-08-14 01:26:19 +00001299 BlockInfo.addInfo(*SI, std::move(CurrStates));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001300 else
David Blaikie86d8cf32015-08-14 01:26:19 +00001301 CurrStates = nullptr;
Ahmed Charles9a16beb2014-03-07 19:33:25 +00001302
David Blaikie86d8cf32015-08-14 01:26:19 +00001303 if (*++SI)
1304 BlockInfo.addInfo(*SI, std::move(FalseStates));
1305
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001306 return true;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001307}
1308
1309void ConsumedAnalyzer::run(AnalysisDeclContext &AC) {
Eugene Zelenkobc324332018-03-13 21:32:01 +00001310 const auto *D = dyn_cast_or_null<FunctionDecl>(AC.getDecl());
DeLesley Hutchins85c07d92013-09-10 23:10:10 +00001311 if (!D)
1312 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001313
DeLesley Hutchins85c07d92013-09-10 23:10:10 +00001314 CFG *CFGraph = AC.getCFG();
1315 if (!CFGraph)
1316 return;
DeLesley Hutchins65013202013-10-17 18:19:31 +00001317
David Blaikie16f76d22013-09-06 01:28:43 +00001318 determineExpectedReturnState(AC, D);
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001319
Artyom Skrobov27720762014-09-23 08:34:41 +00001320 PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>();
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001321 // AC.getCFG()->viewCFG(LangOptions());
Fangrui Song6907ce22018-07-30 19:24:48 +00001322
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001323 BlockInfo = ConsumedBlockInfo(CFGraph->getNumBlockIDs(), SortedGraph);
David Blaikie86d8cf32015-08-14 01:26:19 +00001324
1325 CurrStates = llvm::make_unique<ConsumedStateMap>();
1326 ConsumedStmtVisitor Visitor(AC, *this, CurrStates.get());
1327
DeLesley Hutchinsb570c132013-08-29 22:36:05 +00001328 // Add all trackable parameters to the state map.
David Majnemer59f77922016-06-24 04:05:48 +00001329 for (const auto *PI : D->parameters())
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00001330 Visitor.VisitParmVarDecl(PI);
Fangrui Song6907ce22018-07-30 19:24:48 +00001331
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001332 // Visit all of the function's basic blocks.
Aaron Ballmanfe46e622014-04-28 13:01:32 +00001333 for (const auto *CurrBlock : *SortedGraph) {
Craig Topper25542942014-05-20 04:30:07 +00001334 if (!CurrStates)
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001335 CurrStates = BlockInfo.getInfo(CurrBlock);
Fangrui Song6907ce22018-07-30 19:24:48 +00001336
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001337 if (!CurrStates) {
1338 continue;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001339 } else if (!CurrStates->isReachable()) {
Craig Topper25542942014-05-20 04:30:07 +00001340 CurrStates = nullptr;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001341 continue;
1342 }
David Blaikie86d8cf32015-08-14 01:26:19 +00001343
1344 Visitor.reset(CurrStates.get());
1345
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001346 // Visit all of the basic block's statements.
Aaron Ballman35897d92014-04-28 14:56:59 +00001347 for (const auto &B : *CurrBlock) {
1348 switch (B.getKind()) {
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001349 case CFGElement::Statement:
Aaron Ballman35897d92014-04-28 14:56:59 +00001350 Visitor.Visit(B.castAs<CFGStmt>().getStmt());
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001351 break;
Fangrui Song6907ce22018-07-30 19:24:48 +00001352
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001353 case CFGElement::TemporaryDtor: {
Aaron Ballman35897d92014-04-28 14:56:59 +00001354 const CFGTemporaryDtor &DTor = B.castAs<CFGTemporaryDtor>();
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001355 const CXXBindTemporaryExpr *BTE = DTor.getBindTemporaryExpr();
Fangrui Song6907ce22018-07-30 19:24:48 +00001356
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001357 Visitor.checkCallability(PropagationInfo(BTE),
1358 DTor.getDestructorDecl(AC.getASTContext()),
1359 BTE->getExprLoc());
Manuel Klimekb33bded2014-05-08 11:50:00 +00001360 CurrStates->remove(BTE);
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001361 break;
1362 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001363
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001364 case CFGElement::AutomaticObjectDtor: {
Aaron Ballman35897d92014-04-28 14:56:59 +00001365 const CFGAutomaticObjDtor &DTor = B.castAs<CFGAutomaticObjDtor>();
Stephen Kelly1c301dc2018-08-09 21:09:38 +00001366 SourceLocation Loc = DTor.getTriggerStmt()->getEndLoc();
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001367 const VarDecl *Var = DTor.getVarDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00001368
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001369 Visitor.checkCallability(PropagationInfo(Var),
1370 DTor.getDestructorDecl(AC.getASTContext()),
1371 Loc);
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001372 break;
1373 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001374
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001375 default:
1376 break;
1377 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001378 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001379
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001380 // TODO: Handle other forms of branching with precision, including while-
1381 // and for-loops. (Deferred)
1382 if (!splitState(CurrBlock, Visitor)) {
Craig Topper25542942014-05-20 04:30:07 +00001383 CurrStates->setSource(nullptr);
1384
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001385 if (CurrBlock->succ_size() > 1 ||
1386 (CurrBlock->succ_size() == 1 &&
1387 (*CurrBlock->succ_begin())->pred_size() > 1)) {
David Blaikie86d8cf32015-08-14 01:26:19 +00001388
1389 auto *RawState = CurrStates.get();
1390
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001391 for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
1392 SE = CurrBlock->succ_end(); SI != SE; ++SI) {
Craig Topper25542942014-05-20 04:30:07 +00001393 if (*SI == nullptr) continue;
1394
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001395 if (BlockInfo.isBackEdge(CurrBlock, *SI)) {
David Blaikie86d8cf32015-08-14 01:26:19 +00001396 BlockInfo.borrowInfo(*SI)->intersectAtLoopHead(
1397 *SI, CurrBlock, RawState, WarningsHandler);
1398
DeLesley Hutchins773ba372015-04-15 22:32:44 +00001399 if (BlockInfo.allBackEdgesVisited(CurrBlock, *SI))
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001400 BlockInfo.discardInfo(*SI);
1401 } else {
David Blaikie86d8cf32015-08-14 01:26:19 +00001402 BlockInfo.addInfo(*SI, RawState, CurrStates);
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001403 }
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001404 }
Craig Topper25542942014-05-20 04:30:07 +00001405
1406 CurrStates = nullptr;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001407 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001408 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001409
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001410 if (CurrBlock == &AC.getCFG()->getExit() &&
1411 D->getCallResultType()->isVoidType())
1412 CurrStates->checkParamsForReturnTypestate(D->getLocation(),
1413 WarningsHandler);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001414 } // End of block iterator.
Fangrui Song6907ce22018-07-30 19:24:48 +00001415
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001416 // Delete the last existing state map.
David Blaikie86d8cf32015-08-14 01:26:19 +00001417 CurrStates = nullptr;
1418
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001419 WarningsHandler.emitDiagnostics();
1420}