blob: cde753e8ec5767459e9c16f01c1d831c9d05a04f [file] [log] [blame]
Eugene Zelenkobc324332018-03-13 21:32:01 +00001//===- Consumed.cpp -------------------------------------------------------===//
DeLesley Hutchins48a31762013-08-12 21:20:55 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
DeLesley Hutchins48a31762013-08-12 21:20:55 +00006//
7//===----------------------------------------------------------------------===//
8//
9// A intra-procedural analysis for checking consumed properties. This is based,
10// in part, on research on linear types.
11//
12//===----------------------------------------------------------------------===//
13
Mehdi Amini9670f842016-07-18 19:02:11 +000014#include "clang/Analysis/Analyses/Consumed.h"
DeLesley Hutchins48a31762013-08-12 21:20:55 +000015#include "clang/AST/Attr.h"
Eugene Zelenkobc324332018-03-13 21:32:01 +000016#include "clang/AST/Decl.h"
DeLesley Hutchins48a31762013-08-12 21:20:55 +000017#include "clang/AST/DeclCXX.h"
Eugene Zelenkobc324332018-03-13 21:32:01 +000018#include "clang/AST/Expr.h"
DeLesley Hutchins48a31762013-08-12 21:20:55 +000019#include "clang/AST/ExprCXX.h"
Eugene Zelenkobc324332018-03-13 21:32:01 +000020#include "clang/AST/Stmt.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"
23#include "clang/Analysis/Analyses/PostOrderCFGView.h"
George Karpenkov50657f62017-09-06 21:45:03 +000024#include "clang/Analysis/AnalysisDeclContext.h"
DeLesley Hutchins48a31762013-08-12 21:20:55 +000025#include "clang/Analysis/CFG.h"
Eugene Zelenkobc324332018-03-13 21:32:01 +000026#include "clang/Basic/LLVM.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"
Eugene Zelenkobc324332018-03-13 21:32:01 +000030#include "llvm/ADT/Optional.h"
31#include "llvm/ADT/STLExtras.h"
32#include "llvm/ADT/StringRef.h"
33#include "llvm/Support/Casting.h"
34#include "llvm/Support/ErrorHandling.h"
35#include <cassert>
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000036#include <memory>
Eugene Zelenkobc324332018-03-13 21:32:01 +000037#include <utility>
DeLesley Hutchins48a31762013-08-12 21:20:55 +000038
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +000039// TODO: Adjust states of args to constructors in the same way that arguments to
40// function calls are handled.
41// TODO: Use information from tests in for- and while-loop conditional.
Fangrui Song6907ce22018-07-30 19:24:48 +000042// TODO: Add notes about the actual and expected state for
DeLesley Hutchins5533ec52013-08-29 17:26:57 +000043// TODO: Correctly identify unreachable blocks when chaining boolean operators.
DeLesley Hutchins210791a2013-10-04 21:28:06 +000044// TODO: Adjust the parser and AttributesList class to support lists of
45// identifiers.
DeLesley Hutchins5533ec52013-08-29 17:26:57 +000046// TODO: Warn about unreachable code.
47// TODO: Switch to using a bitmap to track unreachable blocks.
DeLesley Hutchins5533ec52013-08-29 17:26:57 +000048// TODO: Handle variable definitions, e.g. bool valid = x.isValid();
49// if (valid) ...; (Deferred)
DeLesley Hutchins48a31762013-08-12 21:20:55 +000050// TODO: Take notes on state transitions to provide better warning messages.
51// (Deferred)
52// TODO: Test nested conditionals: A) Checking the same value multiple times,
53// and 2) Checking different values. (Deferred)
DeLesley Hutchins48a31762013-08-12 21:20:55 +000054
55using namespace clang;
56using namespace consumed;
57
58// Key method definition
Eugene Zelenkobc324332018-03-13 21:32:01 +000059ConsumedWarningsHandlerBase::~ConsumedWarningsHandlerBase() = default;
DeLesley Hutchins48a31762013-08-12 21:20:55 +000060
DeLesley Hutchins65013202013-10-17 18:19:31 +000061static SourceLocation getFirstStmtLoc(const CFGBlock *Block) {
62 // Find the source location of the first statement in the block, if the block
63 // is not empty.
Aaron Ballman35897d92014-04-28 14:56:59 +000064 for (const auto &B : *Block)
65 if (Optional<CFGStmt> CS = B.getAs<CFGStmt>())
Stephen Kellyf2ceec42018-08-09 21:08:08 +000066 return CS->getStmt()->getBeginLoc();
DeLesley Hutchins65013202013-10-17 18:19:31 +000067
68 // Block is empty.
69 // If we have one successor, return the first statement in that block
70 if (Block->succ_size() == 1 && *Block->succ_begin())
71 return getFirstStmtLoc(*Block->succ_begin());
72
Eugene Zelenkobc324332018-03-13 21:32:01 +000073 return {};
DeLesley Hutchins65013202013-10-17 18:19:31 +000074}
75
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +000076static SourceLocation getLastStmtLoc(const CFGBlock *Block) {
DeLesley Hutchins3277a612013-10-09 18:30:24 +000077 // Find the source location of the last statement in the block, if the block
78 // is not empty.
Artem Dergachev4e530322019-05-24 01:34:22 +000079 if (const Stmt *StmtNode = Block->getTerminatorStmt()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000080 return StmtNode->getBeginLoc();
DeLesley Hutchins3277a612013-10-09 18:30:24 +000081 } else {
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +000082 for (CFGBlock::const_reverse_iterator BI = Block->rbegin(),
83 BE = Block->rend(); BI != BE; ++BI) {
DeLesley Hutchins3277a612013-10-09 18:30:24 +000084 if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>())
Stephen Kellyf2ceec42018-08-09 21:08:08 +000085 return CS->getStmt()->getBeginLoc();
DeLesley Hutchins3277a612013-10-09 18:30:24 +000086 }
87 }
DeLesley Hutchins65013202013-10-17 18:19:31 +000088
89 // If we have one successor, return the first statement in that block
90 SourceLocation Loc;
91 if (Block->succ_size() == 1 && *Block->succ_begin())
92 Loc = getFirstStmtLoc(*Block->succ_begin());
93 if (Loc.isValid())
94 return Loc;
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +000095
DeLesley Hutchins65013202013-10-17 18:19:31 +000096 // If we have one predecessor, return the last statement in that block
97 if (Block->pred_size() == 1 && *Block->pred_begin())
98 return getLastStmtLoc(*Block->pred_begin());
99
100 return Loc;
DeLesley Hutchins3277a612013-10-09 18:30:24 +0000101}
102
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000103static ConsumedState invertConsumedUnconsumed(ConsumedState State) {
104 switch (State) {
105 case CS_Unconsumed:
106 return CS_Consumed;
107 case CS_Consumed:
108 return CS_Unconsumed;
109 case CS_None:
110 return CS_None;
111 case CS_Unknown:
112 return CS_Unknown;
113 }
114 llvm_unreachable("invalid enum");
115}
116
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000117static bool isCallableInState(const CallableWhenAttr *CWAttr,
118 ConsumedState State) {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000119 for (const auto &S : CWAttr->callableStates()) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000120 ConsumedState MappedAttrState = CS_None;
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000121
122 switch (S) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000123 case CallableWhenAttr::Unknown:
124 MappedAttrState = CS_Unknown;
125 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000126
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000127 case CallableWhenAttr::Unconsumed:
128 MappedAttrState = CS_Unconsumed;
129 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000130
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000131 case CallableWhenAttr::Consumed:
132 MappedAttrState = CS_Consumed;
133 break;
134 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000135
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000136 if (MappedAttrState == State)
137 return true;
138 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000139
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000140 return false;
141}
142
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000143static bool isConsumableType(const QualType &QT) {
Chris Wailes93edffa2013-10-31 15:38:12 +0000144 if (QT->isPointerType() || QT->isReferenceType())
145 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000146
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000147 if (const CXXRecordDecl *RD = QT->getAsCXXRecordDecl())
148 return RD->hasAttr<ConsumableAttr>();
Fangrui Song6907ce22018-07-30 19:24:48 +0000149
Chris Wailes93edffa2013-10-31 15:38:12 +0000150 return false;
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000151}
152
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000153static bool isAutoCastType(const QualType &QT) {
154 if (QT->isPointerType() || QT->isReferenceType())
155 return false;
156
157 if (const CXXRecordDecl *RD = QT->getAsCXXRecordDecl())
158 return RD->hasAttr<ConsumableAutoCastAttr>();
159
160 return false;
161}
162
163static bool isSetOnReadPtrType(const QualType &QT) {
164 if (const CXXRecordDecl *RD = QT->getPointeeCXXRecordDecl())
165 return RD->hasAttr<ConsumableSetOnReadAttr>();
166 return false;
167}
168
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000169static bool isKnownState(ConsumedState State) {
170 switch (State) {
171 case CS_Unconsumed:
172 case CS_Consumed:
173 return true;
174 case CS_None:
175 case CS_Unknown:
176 return false;
177 }
Aaron Ballmana21f4b82013-08-29 20:36:09 +0000178 llvm_unreachable("invalid enum");
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000179}
180
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000181static bool isRValueRef(QualType ParamType) {
182 return ParamType->isRValueReferenceType();
DeLesley Hutchins0bd25892013-10-18 19:25:18 +0000183}
184
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000185static bool isTestingFunction(const FunctionDecl *FunDecl) {
Chris Wailes9385f9f2013-10-29 20:28:41 +0000186 return FunDecl->hasAttr<TestTypestateAttr>();
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000187}
188
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000189static bool isPointerOrRef(QualType ParamType) {
190 return ParamType->isPointerType() || ParamType->isReferenceType();
DeLesley Hutchins0bd25892013-10-18 19:25:18 +0000191}
192
David Blaikie16f76d22013-09-06 01:28:43 +0000193static ConsumedState mapConsumableAttrState(const QualType QT) {
194 assert(isConsumableType(QT));
195
196 const ConsumableAttr *CAttr =
197 QT->getAsCXXRecordDecl()->getAttr<ConsumableAttr>();
198
199 switch (CAttr->getDefaultState()) {
200 case ConsumableAttr::Unknown:
201 return CS_Unknown;
202 case ConsumableAttr::Unconsumed:
203 return CS_Unconsumed;
204 case ConsumableAttr::Consumed:
205 return CS_Consumed;
206 }
207 llvm_unreachable("invalid enum");
208}
209
DeLesley Hutchins69391772013-10-17 23:23:53 +0000210static ConsumedState
211mapParamTypestateAttrState(const ParamTypestateAttr *PTAttr) {
212 switch (PTAttr->getParamState()) {
213 case ParamTypestateAttr::Unknown:
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000214 return CS_Unknown;
DeLesley Hutchins69391772013-10-17 23:23:53 +0000215 case ParamTypestateAttr::Unconsumed:
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000216 return CS_Unconsumed;
DeLesley Hutchins69391772013-10-17 23:23:53 +0000217 case ParamTypestateAttr::Consumed:
DeLesley Hutchins33a29342013-10-11 23:03:26 +0000218 return CS_Consumed;
219 }
220 llvm_unreachable("invalid_enum");
221}
222
Eric Christopherde156242013-09-03 20:43:00 +0000223static ConsumedState
224mapReturnTypestateAttrState(const ReturnTypestateAttr *RTSAttr) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000225 switch (RTSAttr->getState()) {
226 case ReturnTypestateAttr::Unknown:
227 return CS_Unknown;
228 case ReturnTypestateAttr::Unconsumed:
229 return CS_Unconsumed;
230 case ReturnTypestateAttr::Consumed:
231 return CS_Consumed;
232 }
Eric Christopherde156242013-09-03 20:43:00 +0000233 llvm_unreachable("invalid enum");
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000234}
235
DeLesley Hutchins69391772013-10-17 23:23:53 +0000236static ConsumedState mapSetTypestateAttrState(const SetTypestateAttr *STAttr) {
237 switch (STAttr->getNewState()) {
238 case SetTypestateAttr::Unknown:
239 return CS_Unknown;
240 case SetTypestateAttr::Unconsumed:
241 return CS_Unconsumed;
242 case SetTypestateAttr::Consumed:
243 return CS_Consumed;
244 }
245 llvm_unreachable("invalid_enum");
246}
247
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000248static StringRef stateToString(ConsumedState State) {
249 switch (State) {
250 case consumed::CS_None:
251 return "none";
Fangrui Song6907ce22018-07-30 19:24:48 +0000252
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000253 case consumed::CS_Unknown:
254 return "unknown";
Fangrui Song6907ce22018-07-30 19:24:48 +0000255
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000256 case consumed::CS_Unconsumed:
257 return "unconsumed";
Fangrui Song6907ce22018-07-30 19:24:48 +0000258
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000259 case consumed::CS_Consumed:
260 return "consumed";
261 }
Reid Kleckner6454d0a2013-08-13 00:11:59 +0000262 llvm_unreachable("invalid enum");
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000263}
264
DeLesley Hutchins8d41d992013-10-11 22:30:48 +0000265static ConsumedState testsFor(const FunctionDecl *FunDecl) {
266 assert(isTestingFunction(FunDecl));
Chris Wailes9385f9f2013-10-29 20:28:41 +0000267 switch (FunDecl->getAttr<TestTypestateAttr>()->getTestState()) {
268 case TestTypestateAttr::Unconsumed:
DeLesley Hutchins8d41d992013-10-11 22:30:48 +0000269 return CS_Unconsumed;
Chris Wailes9385f9f2013-10-29 20:28:41 +0000270 case TestTypestateAttr::Consumed:
DeLesley Hutchins8d41d992013-10-11 22:30:48 +0000271 return CS_Consumed;
272 }
273 llvm_unreachable("invalid enum");
274}
275
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000276namespace {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000277
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000278struct VarTestResult {
279 const VarDecl *Var;
280 ConsumedState TestsFor;
281};
Eugene Zelenkobc324332018-03-13 21:32:01 +0000282
283} // namespace
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000284
285namespace clang {
286namespace consumed {
287
288enum EffectiveOp {
289 EO_And,
290 EO_Or
291};
292
293class PropagationInfo {
294 enum {
295 IT_None,
296 IT_State,
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000297 IT_VarTest,
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000298 IT_BinTest,
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000299 IT_Var,
300 IT_Tmp
Eugene Zelenkobc324332018-03-13 21:32:01 +0000301 } InfoType = IT_None;
Eric Christopherf8a1baa2013-08-29 18:00:58 +0000302
303 struct BinTestTy {
304 const BinaryOperator *Source;
305 EffectiveOp EOp;
306 VarTestResult LTest;
307 VarTestResult RTest;
308 };
Fangrui Song6907ce22018-07-30 19:24:48 +0000309
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000310 union {
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000311 ConsumedState State;
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000312 VarTestResult VarTest;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000313 const VarDecl *Var;
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000314 const CXXBindTemporaryExpr *Tmp;
Eric Christopherf8a1baa2013-08-29 18:00:58 +0000315 BinTestTy BinTest;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000316 };
Fangrui Song6907ce22018-07-30 19:24:48 +0000317
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000318public:
Eugene Zelenkobc324332018-03-13 21:32:01 +0000319 PropagationInfo() = default;
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000320 PropagationInfo(const VarTestResult &VarTest)
Eugene Zelenkobc324332018-03-13 21:32:01 +0000321 : InfoType(IT_VarTest), VarTest(VarTest) {}
322
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000323 PropagationInfo(const VarDecl *Var, ConsumedState TestsFor)
Eugene Zelenkobc324332018-03-13 21:32:01 +0000324 : InfoType(IT_VarTest) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000325 VarTest.Var = Var;
326 VarTest.TestsFor = TestsFor;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000327 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000328
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000329 PropagationInfo(const BinaryOperator *Source, EffectiveOp EOp,
330 const VarTestResult &LTest, const VarTestResult &RTest)
Eugene Zelenkobc324332018-03-13 21:32:01 +0000331 : InfoType(IT_BinTest) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000332 BinTest.Source = Source;
333 BinTest.EOp = EOp;
334 BinTest.LTest = LTest;
335 BinTest.RTest = RTest;
336 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000337
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000338 PropagationInfo(const BinaryOperator *Source, EffectiveOp EOp,
339 const VarDecl *LVar, ConsumedState LTestsFor,
340 const VarDecl *RVar, ConsumedState RTestsFor)
Eugene Zelenkobc324332018-03-13 21:32:01 +0000341 : InfoType(IT_BinTest) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000342 BinTest.Source = Source;
343 BinTest.EOp = EOp;
344 BinTest.LTest.Var = LVar;
345 BinTest.LTest.TestsFor = LTestsFor;
346 BinTest.RTest.Var = RVar;
347 BinTest.RTest.TestsFor = RTestsFor;
348 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000349
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000350 PropagationInfo(ConsumedState State)
Eugene Zelenkobc324332018-03-13 21:32:01 +0000351 : InfoType(IT_State), State(State) {}
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000352 PropagationInfo(const VarDecl *Var) : InfoType(IT_Var), Var(Var) {}
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000353 PropagationInfo(const CXXBindTemporaryExpr *Tmp)
Eugene Zelenkobc324332018-03-13 21:32:01 +0000354 : InfoType(IT_Tmp), Tmp(Tmp) {}
Fangrui Song6907ce22018-07-30 19:24:48 +0000355
Eugene Zelenkobc324332018-03-13 21:32:01 +0000356 const ConsumedState &getState() const {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000357 assert(InfoType == IT_State);
358 return State;
359 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000360
Eugene Zelenkobc324332018-03-13 21:32:01 +0000361 const VarTestResult &getVarTest() const {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000362 assert(InfoType == IT_VarTest);
363 return VarTest;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000364 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000365
Eugene Zelenkobc324332018-03-13 21:32:01 +0000366 const VarTestResult &getLTest() const {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000367 assert(InfoType == IT_BinTest);
368 return BinTest.LTest;
369 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000370
Eugene Zelenkobc324332018-03-13 21:32:01 +0000371 const VarTestResult &getRTest() const {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000372 assert(InfoType == IT_BinTest);
373 return BinTest.RTest;
374 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000375
Eugene Zelenkobc324332018-03-13 21:32:01 +0000376 const VarDecl *getVar() const {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000377 assert(InfoType == IT_Var);
378 return Var;
379 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000380
Eugene Zelenkobc324332018-03-13 21:32:01 +0000381 const CXXBindTemporaryExpr *getTmp() const {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000382 assert(InfoType == IT_Tmp);
383 return Tmp;
384 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000385
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000386 ConsumedState getAsState(const ConsumedStateMap *StateMap) const {
387 assert(isVar() || isTmp() || isState());
Fangrui Song6907ce22018-07-30 19:24:48 +0000388
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000389 if (isVar())
390 return StateMap->getState(Var);
391 else if (isTmp())
392 return StateMap->getState(Tmp);
393 else if (isState())
394 return State;
395 else
396 return CS_None;
397 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000398
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000399 EffectiveOp testEffectiveOp() const {
400 assert(InfoType == IT_BinTest);
401 return BinTest.EOp;
402 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000403
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000404 const BinaryOperator * testSourceNode() const {
405 assert(InfoType == IT_BinTest);
406 return BinTest.Source;
407 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000408
Eugene Zelenkobc324332018-03-13 21:32:01 +0000409 bool isValid() const { return InfoType != IT_None; }
410 bool isState() const { return InfoType == IT_State; }
411 bool isVarTest() const { return InfoType == IT_VarTest; }
412 bool isBinTest() const { return InfoType == IT_BinTest; }
413 bool isVar() const { return InfoType == IT_Var; }
414 bool isTmp() const { return InfoType == IT_Tmp; }
Fangrui Song6907ce22018-07-30 19:24:48 +0000415
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000416 bool isTest() const {
417 return InfoType == IT_VarTest || InfoType == IT_BinTest;
418 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000419
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000420 bool isPointerToValue() const {
421 return InfoType == IT_Var || InfoType == IT_Tmp;
422 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000423
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000424 PropagationInfo invertTest() const {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000425 assert(InfoType == IT_VarTest || InfoType == IT_BinTest);
Fangrui Song6907ce22018-07-30 19:24:48 +0000426
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000427 if (InfoType == IT_VarTest) {
428 return PropagationInfo(VarTest.Var,
429 invertConsumedUnconsumed(VarTest.TestsFor));
Fangrui Song6907ce22018-07-30 19:24:48 +0000430
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000431 } else if (InfoType == IT_BinTest) {
432 return PropagationInfo(BinTest.Source,
433 BinTest.EOp == EO_And ? EO_Or : EO_And,
434 BinTest.LTest.Var, invertConsumedUnconsumed(BinTest.LTest.TestsFor),
435 BinTest.RTest.Var, invertConsumedUnconsumed(BinTest.RTest.TestsFor));
436 } else {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000437 return {};
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000438 }
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000439 }
440};
441
Eugene Zelenkobc324332018-03-13 21:32:01 +0000442} // namespace consumed
443} // namespace clang
444
445static void
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000446setStateForVarOrTmp(ConsumedStateMap *StateMap, const PropagationInfo &PInfo,
447 ConsumedState State) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000448 assert(PInfo.isVar() || PInfo.isTmp());
Fangrui Song6907ce22018-07-30 19:24:48 +0000449
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000450 if (PInfo.isVar())
451 StateMap->setState(PInfo.getVar(), State);
452 else
453 StateMap->setState(PInfo.getTmp(), State);
454}
455
Eugene Zelenkobc324332018-03-13 21:32:01 +0000456namespace clang {
457namespace consumed {
458
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000459class ConsumedStmtVisitor : public ConstStmtVisitor<ConsumedStmtVisitor> {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000460 using MapType = llvm::DenseMap<const Stmt *, PropagationInfo>;
461 using PairType= std::pair<const Stmt *, PropagationInfo>;
462 using InfoEntry = MapType::iterator;
463 using ConstInfoEntry = MapType::const_iterator;
Fangrui Song6907ce22018-07-30 19:24:48 +0000464
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000465 ConsumedAnalyzer &Analyzer;
466 ConsumedStateMap *StateMap;
467 MapType PropagationMap;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000468
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000469 InfoEntry findInfo(const Expr *E) {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000470 if (const auto Cleanups = dyn_cast<ExprWithCleanups>(E))
Tim Shen4a05bb82016-06-21 20:29:17 +0000471 if (!Cleanups->cleanupsHaveSideEffects())
472 E = Cleanups->getSubExpr();
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000473 return PropagationMap.find(E->IgnoreParens());
474 }
Eugene Zelenkobc324332018-03-13 21:32:01 +0000475
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000476 ConstInfoEntry findInfo(const Expr *E) const {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000477 if (const auto Cleanups = dyn_cast<ExprWithCleanups>(E))
Tim Shen4a05bb82016-06-21 20:29:17 +0000478 if (!Cleanups->cleanupsHaveSideEffects())
479 E = Cleanups->getSubExpr();
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000480 return PropagationMap.find(E->IgnoreParens());
481 }
Eugene Zelenkobc324332018-03-13 21:32:01 +0000482
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000483 void insertInfo(const Expr *E, const PropagationInfo &PI) {
484 PropagationMap.insert(PairType(E->IgnoreParens(), PI));
485 }
486
487 void forwardInfo(const Expr *From, const Expr *To);
488 void copyInfo(const Expr *From, const Expr *To, ConsumedState CS);
489 ConsumedState getInfo(const Expr *From);
490 void setInfo(const Expr *To, ConsumedState NS);
491 void propagateReturnType(const Expr *Call, const FunctionDecl *Fun);
DeLesley Hutchins81218662013-10-18 23:11:49 +0000492
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000493public:
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000494 void checkCallability(const PropagationInfo &PInfo,
495 const FunctionDecl *FunDecl,
496 SourceLocation BlameLoc);
Nicholas Allegra695a8bd2019-09-27 01:58:31 +0000497 bool handleCall(const CallExpr *Call, const Expr *ObjArg,
498 const FunctionDecl *FunD);
Fangrui Song6907ce22018-07-30 19:24:48 +0000499
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000500 void VisitBinaryOperator(const BinaryOperator *BinOp);
501 void VisitCallExpr(const CallExpr *Call);
502 void VisitCastExpr(const CastExpr *Cast);
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000503 void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Temp);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000504 void VisitCXXConstructExpr(const CXXConstructExpr *Call);
505 void VisitCXXMemberCallExpr(const CXXMemberCallExpr *Call);
506 void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *Call);
507 void VisitDeclRefExpr(const DeclRefExpr *DeclRef);
508 void VisitDeclStmt(const DeclStmt *DelcS);
509 void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Temp);
510 void VisitMemberExpr(const MemberExpr *MExpr);
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000511 void VisitParmVarDecl(const ParmVarDecl *Param);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000512 void VisitReturnStmt(const ReturnStmt *Ret);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000513 void VisitUnaryOperator(const UnaryOperator *UOp);
514 void VisitVarDecl(const VarDecl *Var);
Reid Klecknere846dea2013-08-12 23:49:39 +0000515
Brian Gesiak5488ab42019-01-11 01:54:53 +0000516 ConsumedStmtVisitor(ConsumedAnalyzer &Analyzer, ConsumedStateMap *StateMap)
517 : Analyzer(Analyzer), StateMap(StateMap) {}
Fangrui Song6907ce22018-07-30 19:24:48 +0000518
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000519 PropagationInfo getInfo(const Expr *StmtNode) const {
520 ConstInfoEntry Entry = findInfo(StmtNode);
Fangrui Song6907ce22018-07-30 19:24:48 +0000521
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000522 if (Entry != PropagationMap.end())
523 return Entry->second;
524 else
Eugene Zelenkobc324332018-03-13 21:32:01 +0000525 return {};
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000526 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000527
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000528 void reset(ConsumedStateMap *NewStateMap) {
529 StateMap = NewStateMap;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000530 }
531};
532
Eugene Zelenkobc324332018-03-13 21:32:01 +0000533} // namespace consumed
534} // namespace clang
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000535
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000536void ConsumedStmtVisitor::forwardInfo(const Expr *From, const Expr *To) {
537 InfoEntry Entry = findInfo(From);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000538 if (Entry != PropagationMap.end())
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000539 insertInfo(To, Entry->second);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000540}
541
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000542// Create a new state for To, which is initialized to the state of From.
543// If NS is not CS_None, sets the state of From to NS.
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000544void ConsumedStmtVisitor::copyInfo(const Expr *From, const Expr *To,
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000545 ConsumedState NS) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000546 InfoEntry Entry = findInfo(From);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000547 if (Entry != PropagationMap.end()) {
548 PropagationInfo& PInfo = Entry->second;
549 ConsumedState CS = PInfo.getAsState(StateMap);
550 if (CS != CS_None)
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000551 insertInfo(To, PropagationInfo(CS));
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000552 if (NS != CS_None && PInfo.isPointerToValue())
553 setStateForVarOrTmp(StateMap, PInfo, NS);
554 }
555}
556
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000557// Get the ConsumedState for From
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000558ConsumedState ConsumedStmtVisitor::getInfo(const Expr *From) {
559 InfoEntry Entry = findInfo(From);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000560 if (Entry != PropagationMap.end()) {
561 PropagationInfo& PInfo = Entry->second;
562 return PInfo.getAsState(StateMap);
563 }
564 return CS_None;
565}
566
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000567// If we already have info for To then update it, otherwise create a new entry.
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000568void ConsumedStmtVisitor::setInfo(const Expr *To, ConsumedState NS) {
569 InfoEntry Entry = findInfo(To);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000570 if (Entry != PropagationMap.end()) {
571 PropagationInfo& PInfo = Entry->second;
572 if (PInfo.isPointerToValue())
573 setStateForVarOrTmp(StateMap, PInfo, NS);
574 } else if (NS != CS_None) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000575 insertInfo(To, PropagationInfo(NS));
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000576 }
577}
578
DeLesley Hutchins2445b122013-08-26 20:34:59 +0000579void ConsumedStmtVisitor::checkCallability(const PropagationInfo &PInfo,
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000580 const FunctionDecl *FunDecl,
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000581 SourceLocation BlameLoc) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000582 assert(!PInfo.isTest());
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000583
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000584 const CallableWhenAttr *CWAttr = FunDecl->getAttr<CallableWhenAttr>();
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000585 if (!CWAttr)
586 return;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000587
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000588 if (PInfo.isVar()) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000589 ConsumedState VarState = StateMap->getState(PInfo.getVar());
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000590
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000591 if (VarState == CS_None || isCallableInState(CWAttr, VarState))
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000592 return;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000593
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000594 Analyzer.WarningsHandler.warnUseInInvalidState(
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000595 FunDecl->getNameAsString(), PInfo.getVar()->getNameAsString(),
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000596 stateToString(VarState), BlameLoc);
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000597 } else {
598 ConsumedState TmpState = PInfo.getAsState(StateMap);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000599
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000600 if (TmpState == CS_None || isCallableInState(CWAttr, TmpState))
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000601 return;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000602
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000603 Analyzer.WarningsHandler.warnUseOfTempInInvalidState(
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000604 FunDecl->getNameAsString(), stateToString(TmpState), BlameLoc);
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +0000605 }
606}
607
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000608// Factors out common behavior for function, method, and operator calls.
609// Check parameters and set parameter state if necessary.
610// Returns true if the state of ObjArg is set, or false otherwise.
Nicholas Allegra695a8bd2019-09-27 01:58:31 +0000611bool ConsumedStmtVisitor::handleCall(const CallExpr *Call, const Expr *ObjArg,
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000612 const FunctionDecl *FunD) {
Nicholas Allegra695a8bd2019-09-27 01:58:31 +0000613 unsigned Offset = 0;
614 if (isa<CXXOperatorCallExpr>(Call) && isa<CXXMethodDecl>(FunD))
615 Offset = 1; // first argument is 'this'
616
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000617 // check explicit parameters
Nicholas Allegra695a8bd2019-09-27 01:58:31 +0000618 for (unsigned Index = Offset; Index < Call->getNumArgs(); ++Index) {
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000619 // Skip variable argument lists.
Nicholas Allegra695a8bd2019-09-27 01:58:31 +0000620 if (Index - Offset >= FunD->getNumParams())
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000621 break;
622
Nicholas Allegra695a8bd2019-09-27 01:58:31 +0000623 const ParmVarDecl *Param = FunD->getParamDecl(Index - Offset);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000624 QualType ParamType = Param->getType();
625
Nicholas Allegra695a8bd2019-09-27 01:58:31 +0000626 InfoEntry Entry = findInfo(Call->getArg(Index));
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000627
628 if (Entry == PropagationMap.end() || Entry->second.isTest())
629 continue;
630 PropagationInfo PInfo = Entry->second;
631
632 // Check that the parameter is in the correct state.
633 if (ParamTypestateAttr *PTA = Param->getAttr<ParamTypestateAttr>()) {
634 ConsumedState ParamState = PInfo.getAsState(StateMap);
635 ConsumedState ExpectedState = mapParamTypestateAttrState(PTA);
636
637 if (ParamState != ExpectedState)
638 Analyzer.WarningsHandler.warnParamTypestateMismatch(
Nicholas Allegra695a8bd2019-09-27 01:58:31 +0000639 Call->getArg(Index)->getExprLoc(),
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000640 stateToString(ExpectedState), stateToString(ParamState));
641 }
642
643 if (!(Entry->second.isVar() || Entry->second.isTmp()))
644 continue;
645
646 // Adjust state on the caller side.
Nicholas Allegra9dd57df2019-09-19 23:00:31 +0000647 if (ReturnTypestateAttr *RT = Param->getAttr<ReturnTypestateAttr>())
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000648 setStateForVarOrTmp(StateMap, PInfo, mapReturnTypestateAttrState(RT));
Nicholas Allegra9dd57df2019-09-19 23:00:31 +0000649 else if (isRValueRef(ParamType) || isConsumableType(ParamType))
650 setStateForVarOrTmp(StateMap, PInfo, consumed::CS_Consumed);
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000651 else if (isPointerOrRef(ParamType) &&
652 (!ParamType->getPointeeType().isConstQualified() ||
653 isSetOnReadPtrType(ParamType)))
654 setStateForVarOrTmp(StateMap, PInfo, consumed::CS_Unknown);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000655 }
656
657 if (!ObjArg)
658 return false;
659
660 // check implicit 'self' parameter, if present
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000661 InfoEntry Entry = findInfo(ObjArg);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000662 if (Entry != PropagationMap.end()) {
663 PropagationInfo PInfo = Entry->second;
664 checkCallability(PInfo, FunD, Call->getExprLoc());
665
666 if (SetTypestateAttr *STA = FunD->getAttr<SetTypestateAttr>()) {
667 if (PInfo.isVar()) {
668 StateMap->setState(PInfo.getVar(), mapSetTypestateAttrState(STA));
669 return true;
670 }
671 else if (PInfo.isTmp()) {
672 StateMap->setState(PInfo.getTmp(), mapSetTypestateAttrState(STA));
673 return true;
674 }
675 }
676 else if (isTestingFunction(FunD) && PInfo.isVar()) {
677 PropagationMap.insert(PairType(Call,
678 PropagationInfo(PInfo.getVar(), testsFor(FunD))));
679 }
680 }
681 return false;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000682}
683
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000684void ConsumedStmtVisitor::propagateReturnType(const Expr *Call,
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000685 const FunctionDecl *Fun) {
686 QualType RetType = Fun->getCallResultType();
687 if (RetType->isReferenceType())
688 RetType = RetType->getPointeeType();
689
690 if (isConsumableType(RetType)) {
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000691 ConsumedState ReturnState;
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000692 if (ReturnTypestateAttr *RTA = Fun->getAttr<ReturnTypestateAttr>())
693 ReturnState = mapReturnTypestateAttrState(RTA);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000694 else
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000695 ReturnState = mapConsumableAttrState(RetType);
Fangrui Song6907ce22018-07-30 19:24:48 +0000696
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000697 PropagationMap.insert(PairType(Call, PropagationInfo(ReturnState)));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000698 }
699}
700
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000701void ConsumedStmtVisitor::VisitBinaryOperator(const BinaryOperator *BinOp) {
702 switch (BinOp->getOpcode()) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000703 case BO_LAnd:
704 case BO_LOr : {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000705 InfoEntry LEntry = findInfo(BinOp->getLHS()),
706 REntry = findInfo(BinOp->getRHS());
Fangrui Song6907ce22018-07-30 19:24:48 +0000707
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000708 VarTestResult LTest, RTest;
Fangrui Song6907ce22018-07-30 19:24:48 +0000709
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000710 if (LEntry != PropagationMap.end() && LEntry->second.isVarTest()) {
711 LTest = LEntry->second.getVarTest();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000712 } 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 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000716
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 } else {
Craig Topper25542942014-05-20 04:30:07 +0000720 RTest.Var = nullptr;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000721 RTest.TestsFor = CS_None;
722 }
Craig Topper25542942014-05-20 04:30:07 +0000723
724 if (!(LTest.Var == nullptr && RTest.Var == nullptr))
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000725 PropagationMap.insert(PairType(BinOp, PropagationInfo(BinOp,
726 static_cast<EffectiveOp>(BinOp->getOpcode() == BO_LOr), LTest, RTest)));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000727 break;
728 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000729
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000730 case BO_PtrMemD:
731 case BO_PtrMemI:
732 forwardInfo(BinOp->getLHS(), BinOp);
733 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000734
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000735 default:
736 break;
737 }
738}
739
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000740void ConsumedStmtVisitor::VisitCallExpr(const CallExpr *Call) {
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000741 const FunctionDecl *FunDecl = Call->getDirectCallee();
742 if (!FunDecl)
743 return;
744
745 // Special case for the std::move function.
746 // TODO: Make this more specific. (Deferred)
Nico Weberb688d132017-09-28 16:16:39 +0000747 if (Call->isCallToStdMove()) {
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000748 copyInfo(Call->getArg(0), Call, CS_Consumed);
749 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000750 }
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000751
Nicholas Allegra695a8bd2019-09-27 01:58:31 +0000752 handleCall(Call, nullptr, FunDecl);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000753 propagateReturnType(Call, FunDecl);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000754}
755
756void ConsumedStmtVisitor::VisitCastExpr(const CastExpr *Cast) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000757 forwardInfo(Cast->getSubExpr(), Cast);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000758}
759
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000760void ConsumedStmtVisitor::VisitCXXBindTemporaryExpr(
761 const CXXBindTemporaryExpr *Temp) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000762
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000763 InfoEntry Entry = findInfo(Temp->getSubExpr());
Fangrui Song6907ce22018-07-30 19:24:48 +0000764
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000765 if (Entry != PropagationMap.end() && !Entry->second.isTest()) {
766 StateMap->setState(Temp, Entry->second.getAsState(StateMap));
767 PropagationMap.insert(PairType(Temp, PropagationInfo(Temp)));
768 }
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +0000769}
770
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000771void ConsumedStmtVisitor::VisitCXXConstructExpr(const CXXConstructExpr *Call) {
772 CXXConstructorDecl *Constructor = Call->getConstructor();
Reid Klecknere846dea2013-08-12 23:49:39 +0000773
Brian Gesiak5488ab42019-01-11 01:54:53 +0000774 QualType ThisType = Constructor->getThisType()->getPointeeType();
Fangrui Song6907ce22018-07-30 19:24:48 +0000775
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000776 if (!isConsumableType(ThisType))
777 return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000778
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000779 // FIXME: What should happen if someone annotates the move constructor?
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000780 if (ReturnTypestateAttr *RTA = Constructor->getAttr<ReturnTypestateAttr>()) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000781 // TODO: Adjust state of args appropriately.
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000782 ConsumedState RetState = mapReturnTypestateAttrState(RTA);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000783 PropagationMap.insert(PairType(Call, PropagationInfo(RetState)));
784 } else if (Constructor->isDefaultConstructor()) {
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000785 PropagationMap.insert(PairType(Call,
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000786 PropagationInfo(consumed::CS_Consumed)));
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000787 } else if (Constructor->isMoveConstructor()) {
788 copyInfo(Call->getArg(0), Call, CS_Consumed);
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000789 } else if (Constructor->isCopyConstructor()) {
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000790 // Copy state from arg. If setStateOnRead then set arg to CS_Unknown.
791 ConsumedState NS =
Brian Gesiak5488ab42019-01-11 01:54:53 +0000792 isSetOnReadPtrType(Constructor->getThisType()) ?
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000793 CS_Unknown : CS_None;
794 copyInfo(Call->getArg(0), Call, NS);
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000795 } else {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000796 // TODO: Adjust state of args appropriately.
DeLesley Hutchins11a66c12013-10-18 18:36:21 +0000797 ConsumedState RetState = mapConsumableAttrState(ThisType);
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000798 PropagationMap.insert(PairType(Call, PropagationInfo(RetState)));
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000799 }
800}
801
802void ConsumedStmtVisitor::VisitCXXMemberCallExpr(
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000803 const CXXMemberCallExpr *Call) {
804 CXXMethodDecl* MD = Call->getMethodDecl();
805 if (!MD)
806 return;
807
Nicholas Allegra695a8bd2019-09-27 01:58:31 +0000808 handleCall(Call, Call->getImplicitObjectArgument(), MD);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000809 propagateReturnType(Call, MD);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000810}
811
812void ConsumedStmtVisitor::VisitCXXOperatorCallExpr(
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000813 const CXXOperatorCallExpr *Call) {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000814 const auto *FunDecl = dyn_cast_or_null<FunctionDecl>(Call->getDirectCallee());
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000815 if (!FunDecl) return;
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000816
817 if (Call->getOperator() == OO_Equal) {
Nicholas Allegra695a8bd2019-09-27 01:58:31 +0000818 ConsumedState CS = getInfo(Call->getArg(1));
819 if (!handleCall(Call, Call->getArg(0), FunDecl))
820 setInfo(Call->getArg(0), CS);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000821 return;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000822 }
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000823
Nicholas Allegra695a8bd2019-09-27 01:58:31 +0000824 if (const auto *MCall = dyn_cast<CXXMemberCallExpr>(Call))
825 handleCall(MCall, MCall->getImplicitObjectArgument(), FunDecl);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000826 else
Nicholas Allegra695a8bd2019-09-27 01:58:31 +0000827 handleCall(Call, Call->getArg(0), FunDecl);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +0000828
829 propagateReturnType(Call, FunDecl);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000830}
831
832void ConsumedStmtVisitor::VisitDeclRefExpr(const DeclRefExpr *DeclRef) {
Eugene Zelenkobc324332018-03-13 21:32:01 +0000833 if (const auto *Var = dyn_cast_or_null<VarDecl>(DeclRef->getDecl()))
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000834 if (StateMap->getState(Var) != consumed::CS_None)
835 PropagationMap.insert(PairType(DeclRef, PropagationInfo(Var)));
836}
837
838void ConsumedStmtVisitor::VisitDeclStmt(const DeclStmt *DeclS) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000839 for (const auto *DI : DeclS->decls())
Aaron Ballman535bbcc2014-03-14 17:01:24 +0000840 if (isa<VarDecl>(DI))
841 VisitVarDecl(cast<VarDecl>(DI));
Fangrui Song6907ce22018-07-30 19:24:48 +0000842
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000843 if (DeclS->isSingleDecl())
Eugene Zelenkobc324332018-03-13 21:32:01 +0000844 if (const auto *Var = dyn_cast_or_null<VarDecl>(DeclS->getSingleDecl()))
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000845 PropagationMap.insert(PairType(DeclS, PropagationInfo(Var)));
846}
847
848void ConsumedStmtVisitor::VisitMaterializeTemporaryExpr(
849 const MaterializeTemporaryExpr *Temp) {
Nico Weberc9276fb2019-11-17 02:09:25 -0500850 forwardInfo(Temp->GetTemporaryExpr(), Temp);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000851}
852
853void ConsumedStmtVisitor::VisitMemberExpr(const MemberExpr *MExpr) {
854 forwardInfo(MExpr->getBase(), MExpr);
855}
856
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000857void ConsumedStmtVisitor::VisitParmVarDecl(const ParmVarDecl *Param) {
David Blaikie16f76d22013-09-06 01:28:43 +0000858 QualType ParamType = Param->getType();
859 ConsumedState ParamState = consumed::CS_None;
Fangrui Song6907ce22018-07-30 19:24:48 +0000860
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000861 if (const ParamTypestateAttr *PTA = Param->getAttr<ParamTypestateAttr>())
Fangrui Song6907ce22018-07-30 19:24:48 +0000862 ParamState = mapParamTypestateAttrState(PTA);
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000863 else if (isConsumableType(ParamType))
Fangrui Song6907ce22018-07-30 19:24:48 +0000864 ParamState = mapConsumableAttrState(ParamType);
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000865 else if (isRValueRef(ParamType) &&
866 isConsumableType(ParamType->getPointeeType()))
Fangrui Song6907ce22018-07-30 19:24:48 +0000867 ParamState = mapConsumableAttrState(ParamType->getPointeeType());
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +0000868 else if (ParamType->isReferenceType() &&
DeLesley Hutchins80a38422014-01-16 23:07:16 +0000869 isConsumableType(ParamType->getPointeeType()))
David Blaikie16f76d22013-09-06 01:28:43 +0000870 ParamState = consumed::CS_Unknown;
Fangrui Song6907ce22018-07-30 19:24:48 +0000871
DeLesley Hutchins69391772013-10-17 23:23:53 +0000872 if (ParamState != CS_None)
David Blaikie16f76d22013-09-06 01:28:43 +0000873 StateMap->setState(Param, ParamState);
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000874}
875
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000876void ConsumedStmtVisitor::VisitReturnStmt(const ReturnStmt *Ret) {
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000877 ConsumedState ExpectedState = Analyzer.getExpectedReturnState();
Fangrui Song6907ce22018-07-30 19:24:48 +0000878
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000879 if (ExpectedState != CS_None) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000880 InfoEntry Entry = findInfo(Ret->getRetValue());
Fangrui Song6907ce22018-07-30 19:24:48 +0000881
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000882 if (Entry != PropagationMap.end()) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000883 ConsumedState RetState = Entry->second.getAsState(StateMap);
Fangrui Song6907ce22018-07-30 19:24:48 +0000884
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000885 if (RetState != ExpectedState)
886 Analyzer.WarningsHandler.warnReturnTypestateMismatch(
887 Ret->getReturnLoc(), stateToString(ExpectedState),
888 stateToString(RetState));
889 }
890 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000891
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000892 StateMap->checkParamsForReturnTypestate(Ret->getBeginLoc(),
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +0000893 Analyzer.WarningsHandler);
DeLesley Hutchinsfc368252013-09-03 20:11:38 +0000894}
895
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000896void ConsumedStmtVisitor::VisitUnaryOperator(const UnaryOperator *UOp) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000897 InfoEntry Entry = findInfo(UOp->getSubExpr());
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000898 if (Entry == PropagationMap.end()) return;
Fangrui Song6907ce22018-07-30 19:24:48 +0000899
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000900 switch (UOp->getOpcode()) {
901 case UO_AddrOf:
902 PropagationMap.insert(PairType(UOp, Entry->second));
903 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000904
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000905 case UO_LNot:
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000906 if (Entry->second.isTest())
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000907 PropagationMap.insert(PairType(UOp, Entry->second.invertTest()));
908 break;
Fangrui Song6907ce22018-07-30 19:24:48 +0000909
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000910 default:
911 break;
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000912 }
913}
914
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000915// TODO: See if I need to check for reference types here.
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000916void ConsumedStmtVisitor::VisitVarDecl(const VarDecl *Var) {
DeLesley Hutchins5a715c42013-08-30 22:56:34 +0000917 if (isConsumableType(Var->getType())) {
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000918 if (Var->hasInit()) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +0000919 MapType::iterator VIT = findInfo(Var->getInit()->IgnoreImplicit());
DeLesley Hutchins81218662013-10-18 23:11:49 +0000920 if (VIT != PropagationMap.end()) {
921 PropagationInfo PInfo = VIT->second;
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +0000922 ConsumedState St = PInfo.getAsState(StateMap);
Fangrui Song6907ce22018-07-30 19:24:48 +0000923
DeLesley Hutchins81218662013-10-18 23:11:49 +0000924 if (St != consumed::CS_None) {
925 StateMap->setState(Var, St);
926 return;
927 }
928 }
DeLesley Hutchinsb570c132013-08-29 22:36:05 +0000929 }
DeLesley Hutchins81218662013-10-18 23:11:49 +0000930 // Otherwise
931 StateMap->setState(Var, consumed::CS_Unknown);
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000932 }
933}
DeLesley Hutchins48a31762013-08-12 21:20:55 +0000934
Benjamin Kramer3a743452015-03-09 15:03:32 +0000935static void splitVarStateForIf(const IfStmt *IfNode, const VarTestResult &Test,
936 ConsumedStateMap *ThenStates,
937 ConsumedStateMap *ElseStates) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000938 ConsumedState VarState = ThenStates->getState(Test.Var);
Fangrui Song6907ce22018-07-30 19:24:48 +0000939
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000940 if (VarState == CS_Unknown) {
941 ThenStates->setState(Test.Var, Test.TestsFor);
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000942 ElseStates->setState(Test.Var, invertConsumedUnconsumed(Test.TestsFor));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000943 } else if (VarState == invertConsumedUnconsumed(Test.TestsFor)) {
944 ThenStates->markUnreachable();
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000945 } else if (VarState == Test.TestsFor) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000946 ElseStates->markUnreachable();
947 }
948}
949
Benjamin Kramer3a743452015-03-09 15:03:32 +0000950static void splitVarStateForIfBinOp(const PropagationInfo &PInfo,
951 ConsumedStateMap *ThenStates,
952 ConsumedStateMap *ElseStates) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000953 const VarTestResult &LTest = PInfo.getLTest(),
954 &RTest = PInfo.getRTest();
Fangrui Song6907ce22018-07-30 19:24:48 +0000955
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000956 ConsumedState LState = LTest.Var ? ThenStates->getState(LTest.Var) : CS_None,
957 RState = RTest.Var ? ThenStates->getState(RTest.Var) : CS_None;
Fangrui Song6907ce22018-07-30 19:24:48 +0000958
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000959 if (LTest.Var) {
960 if (PInfo.testEffectiveOp() == EO_And) {
961 if (LState == CS_Unknown) {
962 ThenStates->setState(LTest.Var, LTest.TestsFor);
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000963 } else if (LState == invertConsumedUnconsumed(LTest.TestsFor)) {
964 ThenStates->markUnreachable();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000965 } else if (LState == LTest.TestsFor && isKnownState(RState)) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000966 if (RState == RTest.TestsFor)
967 ElseStates->markUnreachable();
968 else
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000969 ThenStates->markUnreachable();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000970 }
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000971 } else {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000972 if (LState == CS_Unknown) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000973 ElseStates->setState(LTest.Var,
974 invertConsumedUnconsumed(LTest.TestsFor));
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000975 } else if (LState == LTest.TestsFor) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000976 ElseStates->markUnreachable();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000977 } else if (LState == invertConsumedUnconsumed(LTest.TestsFor) &&
978 isKnownState(RState)) {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000979 if (RState == RTest.TestsFor)
980 ElseStates->markUnreachable();
981 else
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000982 ThenStates->markUnreachable();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000983 }
984 }
985 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000986
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000987 if (RTest.Var) {
988 if (PInfo.testEffectiveOp() == EO_And) {
989 if (RState == CS_Unknown)
990 ThenStates->setState(RTest.Var, RTest.TestsFor);
991 else if (RState == invertConsumedUnconsumed(RTest.TestsFor))
992 ThenStates->markUnreachable();
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000993 } else {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +0000994 if (RState == CS_Unknown)
995 ElseStates->setState(RTest.Var,
996 invertConsumedUnconsumed(RTest.TestsFor));
997 else if (RState == RTest.TestsFor)
998 ElseStates->markUnreachable();
999 }
1000 }
1001}
1002
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001003bool ConsumedBlockInfo::allBackEdgesVisited(const CFGBlock *CurrBlock,
1004 const CFGBlock *TargetBlock) {
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001005 assert(CurrBlock && "Block pointer must not be NULL");
1006 assert(TargetBlock && "TargetBlock pointer must not be NULL");
Fangrui Song6907ce22018-07-30 19:24:48 +00001007
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001008 unsigned int CurrBlockOrder = VisitOrder[CurrBlock->getBlockID()];
1009 for (CFGBlock::const_pred_iterator PI = TargetBlock->pred_begin(),
1010 PE = TargetBlock->pred_end(); PI != PE; ++PI) {
1011 if (*PI && CurrBlockOrder < VisitOrder[(*PI)->getBlockID()] )
1012 return false;
1013 }
1014 return true;
1015}
1016
David Blaikie86d8cf32015-08-14 01:26:19 +00001017void ConsumedBlockInfo::addInfo(
1018 const CFGBlock *Block, ConsumedStateMap *StateMap,
1019 std::unique_ptr<ConsumedStateMap> &OwnedStateMap) {
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001020 assert(Block && "Block pointer must not be NULL");
David Blaikie86d8cf32015-08-14 01:26:19 +00001021
1022 auto &Entry = StateMapsArray[Block->getBlockID()];
1023
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001024 if (Entry) {
David Blaikie86d8cf32015-08-14 01:26:19 +00001025 Entry->intersect(*StateMap);
1026 } else if (OwnedStateMap)
1027 Entry = std::move(OwnedStateMap);
1028 else
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001029 Entry = std::make_unique<ConsumedStateMap>(*StateMap);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001030}
1031
1032void ConsumedBlockInfo::addInfo(const CFGBlock *Block,
David Blaikie86d8cf32015-08-14 01:26:19 +00001033 std::unique_ptr<ConsumedStateMap> StateMap) {
Craig Topper25542942014-05-20 04:30:07 +00001034 assert(Block && "Block pointer must not be NULL");
1035
David Blaikie86d8cf32015-08-14 01:26:19 +00001036 auto &Entry = StateMapsArray[Block->getBlockID()];
1037
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001038 if (Entry) {
David Blaikie86d8cf32015-08-14 01:26:19 +00001039 Entry->intersect(*StateMap);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001040 } else {
David Blaikie86d8cf32015-08-14 01:26:19 +00001041 Entry = std::move(StateMap);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001042 }
1043}
1044
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001045ConsumedStateMap* ConsumedBlockInfo::borrowInfo(const CFGBlock *Block) {
1046 assert(Block && "Block pointer must not be NULL");
1047 assert(StateMapsArray[Block->getBlockID()] && "Block has no block info");
David Blaikie86d8cf32015-08-14 01:26:19 +00001048
1049 return StateMapsArray[Block->getBlockID()].get();
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001050}
1051
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001052void ConsumedBlockInfo::discardInfo(const CFGBlock *Block) {
David Blaikie86d8cf32015-08-14 01:26:19 +00001053 StateMapsArray[Block->getBlockID()] = nullptr;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001054}
1055
David Blaikie86d8cf32015-08-14 01:26:19 +00001056std::unique_ptr<ConsumedStateMap>
1057ConsumedBlockInfo::getInfo(const CFGBlock *Block) {
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001058 assert(Block && "Block pointer must not be NULL");
David Blaikie86d8cf32015-08-14 01:26:19 +00001059
1060 auto &Entry = StateMapsArray[Block->getBlockID()];
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001061 return isBackEdgeTarget(Block) ? std::make_unique<ConsumedStateMap>(*Entry)
David Blaikie86d8cf32015-08-14 01:26:19 +00001062 : std::move(Entry);
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001063}
1064
1065bool ConsumedBlockInfo::isBackEdge(const CFGBlock *From, const CFGBlock *To) {
1066 assert(From && "From block must not be NULL");
1067 assert(To && "From block must not be NULL");
Fangrui Song6907ce22018-07-30 19:24:48 +00001068
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001069 return VisitOrder[From->getBlockID()] > VisitOrder[To->getBlockID()];
1070}
1071
1072bool ConsumedBlockInfo::isBackEdgeTarget(const CFGBlock *Block) {
Craig Topper25542942014-05-20 04:30:07 +00001073 assert(Block && "Block pointer must not be NULL");
1074
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001075 // Anything with less than two predecessors can't be the target of a back
1076 // edge.
1077 if (Block->pred_size() < 2)
1078 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +00001079
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001080 unsigned int BlockVisitOrder = VisitOrder[Block->getBlockID()];
1081 for (CFGBlock::const_pred_iterator PI = Block->pred_begin(),
1082 PE = Block->pred_end(); PI != PE; ++PI) {
1083 if (*PI && BlockVisitOrder < VisitOrder[(*PI)->getBlockID()])
1084 return true;
1085 }
1086 return false;
1087}
1088
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001089void ConsumedStateMap::checkParamsForReturnTypestate(SourceLocation BlameLoc,
1090 ConsumedWarningsHandlerBase &WarningsHandler) const {
Fangrui Song6907ce22018-07-30 19:24:48 +00001091
Aaron Ballman35897d92014-04-28 14:56:59 +00001092 for (const auto &DM : VarMap) {
1093 if (isa<ParmVarDecl>(DM.first)) {
Eugene Zelenkobc324332018-03-13 21:32:01 +00001094 const auto *Param = cast<ParmVarDecl>(DM.first);
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +00001095 const ReturnTypestateAttr *RTA = Param->getAttr<ReturnTypestateAttr>();
Fangrui Song6907ce22018-07-30 19:24:48 +00001096
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +00001097 if (!RTA)
1098 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00001099
1100 ConsumedState ExpectedState = mapReturnTypestateAttrState(RTA);
Aaron Ballman35897d92014-04-28 14:56:59 +00001101 if (DM.second != ExpectedState)
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001102 WarningsHandler.warnParamReturnTypestateMismatch(BlameLoc,
1103 Param->getNameAsString(), stateToString(ExpectedState),
Aaron Ballman35897d92014-04-28 14:56:59 +00001104 stateToString(DM.second));
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001105 }
1106 }
1107}
1108
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001109void ConsumedStateMap::clearTemporaries() {
1110 TmpMap.clear();
1111}
1112
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001113ConsumedState ConsumedStateMap::getState(const VarDecl *Var) const {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001114 VarMapType::const_iterator Entry = VarMap.find(Var);
Fangrui Song6907ce22018-07-30 19:24:48 +00001115
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001116 if (Entry != VarMap.end())
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001117 return Entry->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00001118
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001119 return CS_None;
1120}
1121
1122ConsumedState
1123ConsumedStateMap::getState(const CXXBindTemporaryExpr *Tmp) const {
1124 TmpMapType::const_iterator Entry = TmpMap.find(Tmp);
Fangrui Song6907ce22018-07-30 19:24:48 +00001125
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001126 if (Entry != TmpMap.end())
1127 return Entry->second;
Fangrui Song6907ce22018-07-30 19:24:48 +00001128
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001129 return CS_None;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001130}
1131
David Blaikie86d8cf32015-08-14 01:26:19 +00001132void ConsumedStateMap::intersect(const ConsumedStateMap &Other) {
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001133 ConsumedState LocalState;
David Blaikie86d8cf32015-08-14 01:26:19 +00001134
1135 if (this->From && this->From == Other.From && !Other.Reachable) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001136 this->markUnreachable();
1137 return;
1138 }
David Blaikie86d8cf32015-08-14 01:26:19 +00001139
1140 for (const auto &DM : Other.VarMap) {
Aaron Ballman35897d92014-04-28 14:56:59 +00001141 LocalState = this->getState(DM.first);
Fangrui Song6907ce22018-07-30 19:24:48 +00001142
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001143 if (LocalState == CS_None)
1144 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00001145
Aaron Ballman35897d92014-04-28 14:56:59 +00001146 if (LocalState != DM.second)
1147 VarMap[DM.first] = CS_Unknown;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001148 }
1149}
1150
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001151void ConsumedStateMap::intersectAtLoopHead(const CFGBlock *LoopHead,
1152 const CFGBlock *LoopBack, const ConsumedStateMap *LoopBackStates,
1153 ConsumedWarningsHandlerBase &WarningsHandler) {
Fangrui Song6907ce22018-07-30 19:24:48 +00001154
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001155 ConsumedState LocalState;
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001156 SourceLocation BlameLoc = getLastStmtLoc(LoopBack);
Fangrui Song6907ce22018-07-30 19:24:48 +00001157
1158 for (const auto &DM : LoopBackStates->VarMap) {
Aaron Ballman35897d92014-04-28 14:56:59 +00001159 LocalState = this->getState(DM.first);
Fangrui Song6907ce22018-07-30 19:24:48 +00001160
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001161 if (LocalState == CS_None)
1162 continue;
Fangrui Song6907ce22018-07-30 19:24:48 +00001163
Aaron Ballman35897d92014-04-28 14:56:59 +00001164 if (LocalState != DM.second) {
1165 VarMap[DM.first] = CS_Unknown;
Aaron Ballmanfe46e622014-04-28 13:01:32 +00001166 WarningsHandler.warnLoopStateMismatch(BlameLoc,
Aaron Ballman35897d92014-04-28 14:56:59 +00001167 DM.first->getNameAsString());
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001168 }
1169 }
1170}
1171
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001172void ConsumedStateMap::markUnreachable() {
1173 this->Reachable = false;
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001174 VarMap.clear();
1175 TmpMap.clear();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001176}
1177
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001178void ConsumedStateMap::setState(const VarDecl *Var, ConsumedState State) {
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001179 VarMap[Var] = State;
1180}
1181
1182void ConsumedStateMap::setState(const CXXBindTemporaryExpr *Tmp,
1183 ConsumedState State) {
1184 TmpMap[Tmp] = State;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001185}
1186
Manuel Klimekb33bded2014-05-08 11:50:00 +00001187void ConsumedStateMap::remove(const CXXBindTemporaryExpr *Tmp) {
1188 TmpMap.erase(Tmp);
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001189}
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001190
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001191bool ConsumedStateMap::operator!=(const ConsumedStateMap *Other) const {
Aaron Ballman35897d92014-04-28 14:56:59 +00001192 for (const auto &DM : Other->VarMap)
1193 if (this->getState(DM.first) != DM.second)
Fangrui Song6907ce22018-07-30 19:24:48 +00001194 return true;
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001195 return false;
1196}
1197
David Blaikie16f76d22013-09-06 01:28:43 +00001198void ConsumedAnalyzer::determineExpectedReturnState(AnalysisDeclContext &AC,
1199 const FunctionDecl *D) {
1200 QualType ReturnType;
Eugene Zelenkobc324332018-03-13 21:32:01 +00001201 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(D)) {
Brian Gesiak5488ab42019-01-11 01:54:53 +00001202 ReturnType = Constructor->getThisType()->getPointeeType();
David Blaikie16f76d22013-09-06 01:28:43 +00001203 } else
1204 ReturnType = D->getCallResultType();
1205
Aaron Ballmanb06f2ef2013-12-19 02:58:51 +00001206 if (const ReturnTypestateAttr *RTSAttr = D->getAttr<ReturnTypestateAttr>()) {
David Blaikie16f76d22013-09-06 01:28:43 +00001207 const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl();
1208 if (!RD || !RD->hasAttr<ConsumableAttr>()) {
1209 // FIXME: This should be removed when template instantiation propagates
1210 // attributes at template specialization definition, not
1211 // declaration. When it is removed the test needs to be enabled
1212 // in SemaDeclAttr.cpp.
1213 WarningsHandler.warnReturnTypestateForUnconsumableType(
1214 RTSAttr->getLocation(), ReturnType.getAsString());
1215 ExpectedReturnState = CS_None;
1216 } else
1217 ExpectedReturnState = mapReturnTypestateAttrState(RTSAttr);
DeLesley Hutchinsf28bbec2014-01-14 00:36:53 +00001218 } else if (isConsumableType(ReturnType)) {
1219 if (isAutoCastType(ReturnType)) // We can auto-cast the state to the
1220 ExpectedReturnState = CS_None; // expected state.
1221 else
1222 ExpectedReturnState = mapConsumableAttrState(ReturnType);
1223 }
David Blaikie16f76d22013-09-06 01:28:43 +00001224 else
1225 ExpectedReturnState = CS_None;
1226}
1227
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001228bool ConsumedAnalyzer::splitState(const CFGBlock *CurrBlock,
1229 const ConsumedStmtVisitor &Visitor) {
Ahmed Charlesb8984322014-03-07 20:03:18 +00001230 std::unique_ptr<ConsumedStateMap> FalseStates(
1231 new ConsumedStateMap(*CurrStates));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001232 PropagationInfo PInfo;
Fangrui Song6907ce22018-07-30 19:24:48 +00001233
Eugene Zelenkobc324332018-03-13 21:32:01 +00001234 if (const auto *IfNode =
1235 dyn_cast_or_null<IfStmt>(CurrBlock->getTerminator().getStmt())) {
DeLesley Hutchinsd7fa5bd2014-03-20 20:39:20 +00001236 const Expr *Cond = IfNode->getCond();
Fangrui Song6907ce22018-07-30 19:24:48 +00001237
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001238 PInfo = Visitor.getInfo(Cond);
1239 if (!PInfo.isValid() && isa<BinaryOperator>(Cond))
1240 PInfo = Visitor.getInfo(cast<BinaryOperator>(Cond)->getRHS());
Fangrui Song6907ce22018-07-30 19:24:48 +00001241
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001242 if (PInfo.isVarTest()) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001243 CurrStates->setSource(Cond);
1244 FalseStates->setSource(Cond);
David Blaikie86d8cf32015-08-14 01:26:19 +00001245 splitVarStateForIf(IfNode, PInfo.getVarTest(), CurrStates.get(),
Chris Wailes2dc8c422013-10-25 15:33:28 +00001246 FalseStates.get());
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001247 } else if (PInfo.isBinTest()) {
1248 CurrStates->setSource(PInfo.testSourceNode());
1249 FalseStates->setSource(PInfo.testSourceNode());
David Blaikie86d8cf32015-08-14 01:26:19 +00001250 splitVarStateForIfBinOp(PInfo, CurrStates.get(), FalseStates.get());
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001251 } else {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001252 return false;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001253 }
Eugene Zelenkobc324332018-03-13 21:32:01 +00001254 } else if (const auto *BinOp =
1255 dyn_cast_or_null<BinaryOperator>(CurrBlock->getTerminator().getStmt())) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001256 PInfo = Visitor.getInfo(BinOp->getLHS());
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001257 if (!PInfo.isVarTest()) {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001258 if ((BinOp = dyn_cast_or_null<BinaryOperator>(BinOp->getLHS()))) {
1259 PInfo = Visitor.getInfo(BinOp->getRHS());
Fangrui Song6907ce22018-07-30 19:24:48 +00001260
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001261 if (!PInfo.isVarTest())
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001262 return false;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001263 } else {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001264 return false;
1265 }
1266 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001267
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001268 CurrStates->setSource(BinOp);
1269 FalseStates->setSource(BinOp);
Fangrui Song6907ce22018-07-30 19:24:48 +00001270
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001271 const VarTestResult &Test = PInfo.getVarTest();
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001272 ConsumedState VarState = CurrStates->getState(Test.Var);
Fangrui Song6907ce22018-07-30 19:24:48 +00001273
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001274 if (BinOp->getOpcode() == BO_LAnd) {
1275 if (VarState == CS_Unknown)
1276 CurrStates->setState(Test.Var, Test.TestsFor);
1277 else if (VarState == invertConsumedUnconsumed(Test.TestsFor))
1278 CurrStates->markUnreachable();
Fangrui Song6907ce22018-07-30 19:24:48 +00001279
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001280 } else if (BinOp->getOpcode() == BO_LOr) {
1281 if (VarState == CS_Unknown)
1282 FalseStates->setState(Test.Var,
1283 invertConsumedUnconsumed(Test.TestsFor));
1284 else if (VarState == Test.TestsFor)
1285 FalseStates->markUnreachable();
1286 }
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001287 } else {
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001288 return false;
1289 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001290
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001291 CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin();
Fangrui Song6907ce22018-07-30 19:24:48 +00001292
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001293 if (*SI)
David Blaikie86d8cf32015-08-14 01:26:19 +00001294 BlockInfo.addInfo(*SI, std::move(CurrStates));
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001295 else
David Blaikie86d8cf32015-08-14 01:26:19 +00001296 CurrStates = nullptr;
Ahmed Charles9a16beb2014-03-07 19:33:25 +00001297
David Blaikie86d8cf32015-08-14 01:26:19 +00001298 if (*++SI)
1299 BlockInfo.addInfo(*SI, std::move(FalseStates));
1300
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001301 return true;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001302}
1303
1304void ConsumedAnalyzer::run(AnalysisDeclContext &AC) {
Eugene Zelenkobc324332018-03-13 21:32:01 +00001305 const auto *D = dyn_cast_or_null<FunctionDecl>(AC.getDecl());
DeLesley Hutchins85c07d92013-09-10 23:10:10 +00001306 if (!D)
1307 return;
Fangrui Song6907ce22018-07-30 19:24:48 +00001308
DeLesley Hutchins85c07d92013-09-10 23:10:10 +00001309 CFG *CFGraph = AC.getCFG();
1310 if (!CFGraph)
1311 return;
DeLesley Hutchins65013202013-10-17 18:19:31 +00001312
David Blaikie16f76d22013-09-06 01:28:43 +00001313 determineExpectedReturnState(AC, D);
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001314
Artyom Skrobov27720762014-09-23 08:34:41 +00001315 PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>();
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001316 // AC.getCFG()->viewCFG(LangOptions());
Fangrui Song6907ce22018-07-30 19:24:48 +00001317
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001318 BlockInfo = ConsumedBlockInfo(CFGraph->getNumBlockIDs(), SortedGraph);
David Blaikie86d8cf32015-08-14 01:26:19 +00001319
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001320 CurrStates = std::make_unique<ConsumedStateMap>();
Brian Gesiak5488ab42019-01-11 01:54:53 +00001321 ConsumedStmtVisitor Visitor(*this, CurrStates.get());
David Blaikie86d8cf32015-08-14 01:26:19 +00001322
DeLesley Hutchinsb570c132013-08-29 22:36:05 +00001323 // Add all trackable parameters to the state map.
David Majnemer59f77922016-06-24 04:05:48 +00001324 for (const auto *PI : D->parameters())
Aaron Ballmanf6bf62e2014-03-07 15:12:56 +00001325 Visitor.VisitParmVarDecl(PI);
Fangrui Song6907ce22018-07-30 19:24:48 +00001326
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001327 // Visit all of the function's basic blocks.
Aaron Ballmanfe46e622014-04-28 13:01:32 +00001328 for (const auto *CurrBlock : *SortedGraph) {
Craig Topper25542942014-05-20 04:30:07 +00001329 if (!CurrStates)
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001330 CurrStates = BlockInfo.getInfo(CurrBlock);
Fangrui Song6907ce22018-07-30 19:24:48 +00001331
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001332 if (!CurrStates) {
1333 continue;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001334 } else if (!CurrStates->isReachable()) {
Craig Topper25542942014-05-20 04:30:07 +00001335 CurrStates = nullptr;
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001336 continue;
1337 }
David Blaikie86d8cf32015-08-14 01:26:19 +00001338
1339 Visitor.reset(CurrStates.get());
1340
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001341 // Visit all of the basic block's statements.
Aaron Ballman35897d92014-04-28 14:56:59 +00001342 for (const auto &B : *CurrBlock) {
1343 switch (B.getKind()) {
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001344 case CFGElement::Statement:
Aaron Ballman35897d92014-04-28 14:56:59 +00001345 Visitor.Visit(B.castAs<CFGStmt>().getStmt());
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001346 break;
Fangrui Song6907ce22018-07-30 19:24:48 +00001347
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001348 case CFGElement::TemporaryDtor: {
Aaron Ballman35897d92014-04-28 14:56:59 +00001349 const CFGTemporaryDtor &DTor = B.castAs<CFGTemporaryDtor>();
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001350 const CXXBindTemporaryExpr *BTE = DTor.getBindTemporaryExpr();
Fangrui Song6907ce22018-07-30 19:24:48 +00001351
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001352 Visitor.checkCallability(PropagationInfo(BTE),
1353 DTor.getDestructorDecl(AC.getASTContext()),
1354 BTE->getExprLoc());
Manuel Klimekb33bded2014-05-08 11:50:00 +00001355 CurrStates->remove(BTE);
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001356 break;
1357 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001358
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001359 case CFGElement::AutomaticObjectDtor: {
Aaron Ballman35897d92014-04-28 14:56:59 +00001360 const CFGAutomaticObjDtor &DTor = B.castAs<CFGAutomaticObjDtor>();
Stephen Kelly1c301dc2018-08-09 21:09:38 +00001361 SourceLocation Loc = DTor.getTriggerStmt()->getEndLoc();
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001362 const VarDecl *Var = DTor.getVarDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +00001363
DeLesley Hutchins68cc3f12013-11-16 00:22:43 +00001364 Visitor.checkCallability(PropagationInfo(Var),
1365 DTor.getDestructorDecl(AC.getASTContext()),
1366 Loc);
DeLesley Hutchinsfbdee4e2013-10-11 21:55:33 +00001367 break;
1368 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001369
DeLesley Hutchinsc2ecf0d2013-08-22 20:44:47 +00001370 default:
1371 break;
1372 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001373 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001374
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001375 // TODO: Handle other forms of branching with precision, including while-
1376 // and for-loops. (Deferred)
1377 if (!splitState(CurrBlock, Visitor)) {
Craig Topper25542942014-05-20 04:30:07 +00001378 CurrStates->setSource(nullptr);
1379
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001380 if (CurrBlock->succ_size() > 1 ||
1381 (CurrBlock->succ_size() == 1 &&
1382 (*CurrBlock->succ_begin())->pred_size() > 1)) {
David Blaikie86d8cf32015-08-14 01:26:19 +00001383
1384 auto *RawState = CurrStates.get();
1385
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001386 for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(),
1387 SE = CurrBlock->succ_end(); SI != SE; ++SI) {
Craig Topper25542942014-05-20 04:30:07 +00001388 if (*SI == nullptr) continue;
1389
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001390 if (BlockInfo.isBackEdge(CurrBlock, *SI)) {
David Blaikie86d8cf32015-08-14 01:26:19 +00001391 BlockInfo.borrowInfo(*SI)->intersectAtLoopHead(
1392 *SI, CurrBlock, RawState, WarningsHandler);
1393
DeLesley Hutchins773ba372015-04-15 22:32:44 +00001394 if (BlockInfo.allBackEdgesVisited(CurrBlock, *SI))
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001395 BlockInfo.discardInfo(*SI);
1396 } else {
David Blaikie86d8cf32015-08-14 01:26:19 +00001397 BlockInfo.addInfo(*SI, RawState, CurrStates);
DeLesley Hutchins3277a612013-10-09 18:30:24 +00001398 }
DeLesley Hutchins5533ec52013-08-29 17:26:57 +00001399 }
Craig Topper25542942014-05-20 04:30:07 +00001400
1401 CurrStates = nullptr;
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001402 }
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001403 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001404
DeLesley Hutchins36ea1dd2013-10-17 22:53:04 +00001405 if (CurrBlock == &AC.getCFG()->getExit() &&
1406 D->getCallResultType()->isVoidType())
1407 CurrStates->checkParamsForReturnTypestate(D->getLocation(),
1408 WarningsHandler);
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001409 } // End of block iterator.
Fangrui Song6907ce22018-07-30 19:24:48 +00001410
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001411 // Delete the last existing state map.
David Blaikie86d8cf32015-08-14 01:26:19 +00001412 CurrStates = nullptr;
1413
DeLesley Hutchins48a31762013-08-12 21:20:55 +00001414 WarningsHandler.emitDiagnostics();
1415}