DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1 | //===- Consumed.cpp --------------------------------------------*- C++ --*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // A intra-procedural analysis for checking consumed properties. This is based, |
| 11 | // in part, on research on linear types. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/Attr.h" |
| 17 | #include "clang/AST/DeclCXX.h" |
| 18 | #include "clang/AST/ExprCXX.h" |
| 19 | #include "clang/AST/RecursiveASTVisitor.h" |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtCXX.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtVisitor.h" |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 22 | #include "clang/AST/Type.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 23 | #include "clang/Analysis/Analyses/Consumed.h" |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 24 | #include "clang/Analysis/Analyses/PostOrderCFGView.h" |
| 25 | #include "clang/Analysis/AnalysisContext.h" |
| 26 | #include "clang/Analysis/CFG.h" |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 27 | #include "clang/Basic/OperatorKinds.h" |
| 28 | #include "clang/Basic/SourceLocation.h" |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseMap.h" |
| 30 | #include "llvm/ADT/SmallVector.h" |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Compiler.h" |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Ahmed Charles | dfca6f9 | 2014-03-09 11:36:40 +0000 | [diff] [blame] | 33 | #include <memory> |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 34 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 35 | // TODO: Adjust states of args to constructors in the same way that arguments to |
| 36 | // function calls are handled. |
| 37 | // TODO: Use information from tests in for- and while-loop conditional. |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 38 | // TODO: Add notes about the actual and expected state for |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 39 | // TODO: Correctly identify unreachable blocks when chaining boolean operators. |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 40 | // TODO: Adjust the parser and AttributesList class to support lists of |
| 41 | // identifiers. |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 42 | // TODO: Warn about unreachable code. |
| 43 | // TODO: Switch to using a bitmap to track unreachable blocks. |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 44 | // TODO: Handle variable definitions, e.g. bool valid = x.isValid(); |
| 45 | // if (valid) ...; (Deferred) |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 46 | // TODO: Take notes on state transitions to provide better warning messages. |
| 47 | // (Deferred) |
| 48 | // TODO: Test nested conditionals: A) Checking the same value multiple times, |
| 49 | // and 2) Checking different values. (Deferred) |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 50 | |
| 51 | using namespace clang; |
| 52 | using namespace consumed; |
| 53 | |
| 54 | // Key method definition |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 55 | ConsumedWarningsHandlerBase::~ConsumedWarningsHandlerBase() {} |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 56 | |
DeLesley Hutchins | 6501320 | 2013-10-17 18:19:31 +0000 | [diff] [blame] | 57 | static SourceLocation getFirstStmtLoc(const CFGBlock *Block) { |
| 58 | // Find the source location of the first statement in the block, if the block |
| 59 | // is not empty. |
Aaron Ballman | 35897d9 | 2014-04-28 14:56:59 +0000 | [diff] [blame] | 60 | for (const auto &B : *Block) |
| 61 | if (Optional<CFGStmt> CS = B.getAs<CFGStmt>()) |
DeLesley Hutchins | 6501320 | 2013-10-17 18:19:31 +0000 | [diff] [blame] | 62 | return CS->getStmt()->getLocStart(); |
DeLesley Hutchins | 6501320 | 2013-10-17 18:19:31 +0000 | [diff] [blame] | 63 | |
| 64 | // Block is empty. |
| 65 | // If we have one successor, return the first statement in that block |
| 66 | if (Block->succ_size() == 1 && *Block->succ_begin()) |
| 67 | return getFirstStmtLoc(*Block->succ_begin()); |
| 68 | |
| 69 | return SourceLocation(); |
| 70 | } |
| 71 | |
DeLesley Hutchins | fbdee4e | 2013-10-11 21:55:33 +0000 | [diff] [blame] | 72 | static SourceLocation getLastStmtLoc(const CFGBlock *Block) { |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 73 | // Find the source location of the last statement in the block, if the block |
| 74 | // is not empty. |
DeLesley Hutchins | fbdee4e | 2013-10-11 21:55:33 +0000 | [diff] [blame] | 75 | if (const Stmt *StmtNode = Block->getTerminator()) { |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 76 | return StmtNode->getLocStart(); |
| 77 | } else { |
DeLesley Hutchins | fbdee4e | 2013-10-11 21:55:33 +0000 | [diff] [blame] | 78 | for (CFGBlock::const_reverse_iterator BI = Block->rbegin(), |
| 79 | BE = Block->rend(); BI != BE; ++BI) { |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 80 | if (Optional<CFGStmt> CS = BI->getAs<CFGStmt>()) |
| 81 | return CS->getStmt()->getLocStart(); |
| 82 | } |
| 83 | } |
DeLesley Hutchins | 6501320 | 2013-10-17 18:19:31 +0000 | [diff] [blame] | 84 | |
| 85 | // If we have one successor, return the first statement in that block |
| 86 | SourceLocation Loc; |
| 87 | if (Block->succ_size() == 1 && *Block->succ_begin()) |
| 88 | Loc = getFirstStmtLoc(*Block->succ_begin()); |
| 89 | if (Loc.isValid()) |
| 90 | return Loc; |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 91 | |
DeLesley Hutchins | 6501320 | 2013-10-17 18:19:31 +0000 | [diff] [blame] | 92 | // If we have one predecessor, return the last statement in that block |
| 93 | if (Block->pred_size() == 1 && *Block->pred_begin()) |
| 94 | return getLastStmtLoc(*Block->pred_begin()); |
| 95 | |
| 96 | return Loc; |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 97 | } |
| 98 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 99 | static ConsumedState invertConsumedUnconsumed(ConsumedState State) { |
| 100 | switch (State) { |
| 101 | case CS_Unconsumed: |
| 102 | return CS_Consumed; |
| 103 | case CS_Consumed: |
| 104 | return CS_Unconsumed; |
| 105 | case CS_None: |
| 106 | return CS_None; |
| 107 | case CS_Unknown: |
| 108 | return CS_Unknown; |
| 109 | } |
| 110 | llvm_unreachable("invalid enum"); |
| 111 | } |
| 112 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 113 | static bool isCallableInState(const CallableWhenAttr *CWAttr, |
| 114 | ConsumedState State) { |
| 115 | |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 116 | for (const auto &S : CWAttr->callableStates()) { |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 117 | ConsumedState MappedAttrState = CS_None; |
Aaron Ballman | a82eaa7 | 2014-05-02 13:35:42 +0000 | [diff] [blame] | 118 | |
| 119 | switch (S) { |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 120 | case CallableWhenAttr::Unknown: |
| 121 | MappedAttrState = CS_Unknown; |
| 122 | break; |
| 123 | |
| 124 | case CallableWhenAttr::Unconsumed: |
| 125 | MappedAttrState = CS_Unconsumed; |
| 126 | break; |
| 127 | |
| 128 | case CallableWhenAttr::Consumed: |
| 129 | MappedAttrState = CS_Consumed; |
| 130 | break; |
| 131 | } |
| 132 | |
| 133 | if (MappedAttrState == State) |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | return false; |
| 138 | } |
| 139 | |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 140 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 141 | static bool isConsumableType(const QualType &QT) { |
Chris Wailes | 93edffa | 2013-10-31 15:38:12 +0000 | [diff] [blame] | 142 | if (QT->isPointerType() || QT->isReferenceType()) |
| 143 | return false; |
| 144 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 145 | if (const CXXRecordDecl *RD = QT->getAsCXXRecordDecl()) |
| 146 | return RD->hasAttr<ConsumableAttr>(); |
Chris Wailes | 93edffa | 2013-10-31 15:38:12 +0000 | [diff] [blame] | 147 | |
| 148 | return false; |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 149 | } |
| 150 | |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 151 | static bool isAutoCastType(const QualType &QT) { |
| 152 | if (QT->isPointerType() || QT->isReferenceType()) |
| 153 | return false; |
| 154 | |
| 155 | if (const CXXRecordDecl *RD = QT->getAsCXXRecordDecl()) |
| 156 | return RD->hasAttr<ConsumableAutoCastAttr>(); |
| 157 | |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | static bool isSetOnReadPtrType(const QualType &QT) { |
| 162 | if (const CXXRecordDecl *RD = QT->getPointeeCXXRecordDecl()) |
| 163 | return RD->hasAttr<ConsumableSetOnReadAttr>(); |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 168 | static bool isKnownState(ConsumedState State) { |
| 169 | switch (State) { |
| 170 | case CS_Unconsumed: |
| 171 | case CS_Consumed: |
| 172 | return true; |
| 173 | case CS_None: |
| 174 | case CS_Unknown: |
| 175 | return false; |
| 176 | } |
Aaron Ballman | a21f4b8 | 2013-08-29 20:36:09 +0000 | [diff] [blame] | 177 | llvm_unreachable("invalid enum"); |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 178 | } |
| 179 | |
DeLesley Hutchins | 80a3842 | 2014-01-16 23:07:16 +0000 | [diff] [blame] | 180 | static bool isRValueRef(QualType ParamType) { |
| 181 | return ParamType->isRValueReferenceType(); |
DeLesley Hutchins | 0bd2589 | 2013-10-18 19:25:18 +0000 | [diff] [blame] | 182 | } |
| 183 | |
DeLesley Hutchins | c2ecf0d | 2013-08-22 20:44:47 +0000 | [diff] [blame] | 184 | static bool isTestingFunction(const FunctionDecl *FunDecl) { |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 185 | return FunDecl->hasAttr<TestTypestateAttr>(); |
DeLesley Hutchins | c2ecf0d | 2013-08-22 20:44:47 +0000 | [diff] [blame] | 186 | } |
| 187 | |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 188 | static bool isPointerOrRef(QualType ParamType) { |
| 189 | return ParamType->isPointerType() || ParamType->isReferenceType(); |
DeLesley Hutchins | 0bd2589 | 2013-10-18 19:25:18 +0000 | [diff] [blame] | 190 | } |
| 191 | |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 192 | static ConsumedState mapConsumableAttrState(const QualType QT) { |
| 193 | assert(isConsumableType(QT)); |
| 194 | |
| 195 | const ConsumableAttr *CAttr = |
| 196 | QT->getAsCXXRecordDecl()->getAttr<ConsumableAttr>(); |
| 197 | |
| 198 | switch (CAttr->getDefaultState()) { |
| 199 | case ConsumableAttr::Unknown: |
| 200 | return CS_Unknown; |
| 201 | case ConsumableAttr::Unconsumed: |
| 202 | return CS_Unconsumed; |
| 203 | case ConsumableAttr::Consumed: |
| 204 | return CS_Consumed; |
| 205 | } |
| 206 | llvm_unreachable("invalid enum"); |
| 207 | } |
| 208 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 209 | static ConsumedState |
| 210 | mapParamTypestateAttrState(const ParamTypestateAttr *PTAttr) { |
| 211 | switch (PTAttr->getParamState()) { |
| 212 | case ParamTypestateAttr::Unknown: |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 213 | return CS_Unknown; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 214 | case ParamTypestateAttr::Unconsumed: |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 215 | return CS_Unconsumed; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 216 | case ParamTypestateAttr::Consumed: |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 217 | return CS_Consumed; |
| 218 | } |
| 219 | llvm_unreachable("invalid_enum"); |
| 220 | } |
| 221 | |
Eric Christopher | de15624 | 2013-09-03 20:43:00 +0000 | [diff] [blame] | 222 | static ConsumedState |
| 223 | mapReturnTypestateAttrState(const ReturnTypestateAttr *RTSAttr) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 224 | switch (RTSAttr->getState()) { |
| 225 | case ReturnTypestateAttr::Unknown: |
| 226 | return CS_Unknown; |
| 227 | case ReturnTypestateAttr::Unconsumed: |
| 228 | return CS_Unconsumed; |
| 229 | case ReturnTypestateAttr::Consumed: |
| 230 | return CS_Consumed; |
| 231 | } |
Eric Christopher | de15624 | 2013-09-03 20:43:00 +0000 | [diff] [blame] | 232 | llvm_unreachable("invalid enum"); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 233 | } |
| 234 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 235 | static ConsumedState mapSetTypestateAttrState(const SetTypestateAttr *STAttr) { |
| 236 | switch (STAttr->getNewState()) { |
| 237 | case SetTypestateAttr::Unknown: |
| 238 | return CS_Unknown; |
| 239 | case SetTypestateAttr::Unconsumed: |
| 240 | return CS_Unconsumed; |
| 241 | case SetTypestateAttr::Consumed: |
| 242 | return CS_Consumed; |
| 243 | } |
| 244 | llvm_unreachable("invalid_enum"); |
| 245 | } |
| 246 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 247 | static StringRef stateToString(ConsumedState State) { |
| 248 | switch (State) { |
| 249 | case consumed::CS_None: |
| 250 | return "none"; |
| 251 | |
| 252 | case consumed::CS_Unknown: |
| 253 | return "unknown"; |
| 254 | |
| 255 | case consumed::CS_Unconsumed: |
| 256 | return "unconsumed"; |
| 257 | |
| 258 | case consumed::CS_Consumed: |
| 259 | return "consumed"; |
| 260 | } |
Reid Kleckner | 6454d0a | 2013-08-13 00:11:59 +0000 | [diff] [blame] | 261 | llvm_unreachable("invalid enum"); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 262 | } |
| 263 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 264 | static ConsumedState testsFor(const FunctionDecl *FunDecl) { |
| 265 | assert(isTestingFunction(FunDecl)); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 266 | switch (FunDecl->getAttr<TestTypestateAttr>()->getTestState()) { |
| 267 | case TestTypestateAttr::Unconsumed: |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 268 | return CS_Unconsumed; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 269 | case TestTypestateAttr::Consumed: |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 270 | return CS_Consumed; |
| 271 | } |
| 272 | llvm_unreachable("invalid enum"); |
| 273 | } |
| 274 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 275 | namespace { |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 276 | struct VarTestResult { |
| 277 | const VarDecl *Var; |
| 278 | ConsumedState TestsFor; |
| 279 | }; |
| 280 | } // end anonymous::VarTestResult |
| 281 | |
| 282 | namespace clang { |
| 283 | namespace consumed { |
| 284 | |
| 285 | enum EffectiveOp { |
| 286 | EO_And, |
| 287 | EO_Or |
| 288 | }; |
| 289 | |
| 290 | class PropagationInfo { |
| 291 | enum { |
| 292 | IT_None, |
| 293 | IT_State, |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 294 | IT_VarTest, |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 295 | IT_BinTest, |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 296 | IT_Var, |
| 297 | IT_Tmp |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 298 | } InfoType; |
Eric Christopher | f8a1baa | 2013-08-29 18:00:58 +0000 | [diff] [blame] | 299 | |
| 300 | struct BinTestTy { |
| 301 | const BinaryOperator *Source; |
| 302 | EffectiveOp EOp; |
| 303 | VarTestResult LTest; |
| 304 | VarTestResult RTest; |
| 305 | }; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 306 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 307 | union { |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 308 | ConsumedState State; |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 309 | VarTestResult VarTest; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 310 | const VarDecl *Var; |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 311 | const CXXBindTemporaryExpr *Tmp; |
Eric Christopher | f8a1baa | 2013-08-29 18:00:58 +0000 | [diff] [blame] | 312 | BinTestTy BinTest; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 313 | }; |
| 314 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 315 | public: |
| 316 | PropagationInfo() : InfoType(IT_None) {} |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 317 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 318 | PropagationInfo(const VarTestResult &VarTest) |
| 319 | : InfoType(IT_VarTest), VarTest(VarTest) {} |
| 320 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 321 | PropagationInfo(const VarDecl *Var, ConsumedState TestsFor) |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 322 | : InfoType(IT_VarTest) { |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 323 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 324 | VarTest.Var = Var; |
| 325 | VarTest.TestsFor = TestsFor; |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | PropagationInfo(const BinaryOperator *Source, EffectiveOp EOp, |
| 329 | const VarTestResult <est, const VarTestResult &RTest) |
| 330 | : InfoType(IT_BinTest) { |
| 331 | |
| 332 | BinTest.Source = Source; |
| 333 | BinTest.EOp = EOp; |
| 334 | BinTest.LTest = LTest; |
| 335 | BinTest.RTest = RTest; |
| 336 | } |
| 337 | |
| 338 | PropagationInfo(const BinaryOperator *Source, EffectiveOp EOp, |
| 339 | const VarDecl *LVar, ConsumedState LTestsFor, |
| 340 | const VarDecl *RVar, ConsumedState RTestsFor) |
| 341 | : InfoType(IT_BinTest) { |
| 342 | |
| 343 | BinTest.Source = Source; |
| 344 | BinTest.EOp = EOp; |
| 345 | BinTest.LTest.Var = LVar; |
| 346 | BinTest.LTest.TestsFor = LTestsFor; |
| 347 | BinTest.RTest.Var = RVar; |
| 348 | BinTest.RTest.TestsFor = RTestsFor; |
| 349 | } |
| 350 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 351 | PropagationInfo(ConsumedState State) |
| 352 | : InfoType(IT_State), State(State) {} |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 353 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 354 | PropagationInfo(const VarDecl *Var) : InfoType(IT_Var), Var(Var) {} |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 355 | PropagationInfo(const CXXBindTemporaryExpr *Tmp) |
| 356 | : InfoType(IT_Tmp), Tmp(Tmp) {} |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 357 | |
| 358 | const ConsumedState & getState() const { |
| 359 | assert(InfoType == IT_State); |
| 360 | return State; |
| 361 | } |
| 362 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 363 | const VarTestResult & getVarTest() const { |
| 364 | assert(InfoType == IT_VarTest); |
| 365 | return VarTest; |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | const VarTestResult & getLTest() const { |
| 369 | assert(InfoType == IT_BinTest); |
| 370 | return BinTest.LTest; |
| 371 | } |
| 372 | |
| 373 | const VarTestResult & getRTest() const { |
| 374 | assert(InfoType == IT_BinTest); |
| 375 | return BinTest.RTest; |
| 376 | } |
| 377 | |
| 378 | const VarDecl * getVar() const { |
| 379 | assert(InfoType == IT_Var); |
| 380 | return Var; |
| 381 | } |
| 382 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 383 | const CXXBindTemporaryExpr * getTmp() const { |
| 384 | assert(InfoType == IT_Tmp); |
| 385 | return Tmp; |
| 386 | } |
| 387 | |
| 388 | ConsumedState getAsState(const ConsumedStateMap *StateMap) const { |
| 389 | assert(isVar() || isTmp() || isState()); |
| 390 | |
| 391 | if (isVar()) |
| 392 | return StateMap->getState(Var); |
| 393 | else if (isTmp()) |
| 394 | return StateMap->getState(Tmp); |
| 395 | else if (isState()) |
| 396 | return State; |
| 397 | else |
| 398 | return CS_None; |
| 399 | } |
| 400 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 401 | EffectiveOp testEffectiveOp() const { |
| 402 | assert(InfoType == IT_BinTest); |
| 403 | return BinTest.EOp; |
| 404 | } |
| 405 | |
| 406 | const BinaryOperator * testSourceNode() const { |
| 407 | assert(InfoType == IT_BinTest); |
| 408 | return BinTest.Source; |
| 409 | } |
| 410 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 411 | inline bool isValid() const { return InfoType != IT_None; } |
| 412 | inline bool isState() const { return InfoType == IT_State; } |
| 413 | inline bool isVarTest() const { return InfoType == IT_VarTest; } |
| 414 | inline bool isBinTest() const { return InfoType == IT_BinTest; } |
| 415 | inline bool isVar() const { return InfoType == IT_Var; } |
| 416 | inline bool isTmp() const { return InfoType == IT_Tmp; } |
| 417 | |
| 418 | bool isTest() const { |
| 419 | return InfoType == IT_VarTest || InfoType == IT_BinTest; |
| 420 | } |
| 421 | |
| 422 | bool isPointerToValue() const { |
| 423 | return InfoType == IT_Var || InfoType == IT_Tmp; |
| 424 | } |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 425 | |
| 426 | PropagationInfo invertTest() const { |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 427 | assert(InfoType == IT_VarTest || InfoType == IT_BinTest); |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 428 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 429 | if (InfoType == IT_VarTest) { |
| 430 | return PropagationInfo(VarTest.Var, |
| 431 | invertConsumedUnconsumed(VarTest.TestsFor)); |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 432 | |
| 433 | } else if (InfoType == IT_BinTest) { |
| 434 | return PropagationInfo(BinTest.Source, |
| 435 | BinTest.EOp == EO_And ? EO_Or : EO_And, |
| 436 | BinTest.LTest.Var, invertConsumedUnconsumed(BinTest.LTest.TestsFor), |
| 437 | BinTest.RTest.Var, invertConsumedUnconsumed(BinTest.RTest.TestsFor)); |
| 438 | } else { |
| 439 | return PropagationInfo(); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 440 | } |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 441 | } |
| 442 | }; |
| 443 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 444 | static inline void |
| 445 | setStateForVarOrTmp(ConsumedStateMap *StateMap, const PropagationInfo &PInfo, |
| 446 | ConsumedState State) { |
| 447 | |
| 448 | assert(PInfo.isVar() || PInfo.isTmp()); |
| 449 | |
| 450 | if (PInfo.isVar()) |
| 451 | StateMap->setState(PInfo.getVar(), State); |
| 452 | else |
| 453 | StateMap->setState(PInfo.getTmp(), State); |
| 454 | } |
| 455 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 456 | class ConsumedStmtVisitor : public ConstStmtVisitor<ConsumedStmtVisitor> { |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 457 | |
| 458 | typedef llvm::DenseMap<const Stmt *, PropagationInfo> MapType; |
| 459 | typedef std::pair<const Stmt *, PropagationInfo> PairType; |
| 460 | typedef MapType::iterator InfoEntry; |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 461 | typedef MapType::const_iterator ConstInfoEntry; |
| 462 | |
Reid Kleckner | e846dea | 2013-08-12 23:49:39 +0000 | [diff] [blame] | 463 | AnalysisDeclContext &AC; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 464 | ConsumedAnalyzer &Analyzer; |
| 465 | ConsumedStateMap *StateMap; |
| 466 | MapType PropagationMap; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 467 | |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 468 | InfoEntry findInfo(const Expr *E) { |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 469 | if (auto Cleanups = dyn_cast<ExprWithCleanups>(E)) |
| 470 | if (!Cleanups->cleanupsHaveSideEffects()) |
| 471 | E = Cleanups->getSubExpr(); |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 472 | return PropagationMap.find(E->IgnoreParens()); |
| 473 | } |
| 474 | ConstInfoEntry findInfo(const Expr *E) const { |
Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 475 | if (auto Cleanups = dyn_cast<ExprWithCleanups>(E)) |
| 476 | if (!Cleanups->cleanupsHaveSideEffects()) |
| 477 | E = Cleanups->getSubExpr(); |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 478 | return PropagationMap.find(E->IgnoreParens()); |
| 479 | } |
| 480 | void insertInfo(const Expr *E, const PropagationInfo &PI) { |
| 481 | PropagationMap.insert(PairType(E->IgnoreParens(), PI)); |
| 482 | } |
| 483 | |
| 484 | void forwardInfo(const Expr *From, const Expr *To); |
| 485 | void copyInfo(const Expr *From, const Expr *To, ConsumedState CS); |
| 486 | ConsumedState getInfo(const Expr *From); |
| 487 | void setInfo(const Expr *To, ConsumedState NS); |
| 488 | void propagateReturnType(const Expr *Call, const FunctionDecl *Fun); |
DeLesley Hutchins | 8121866 | 2013-10-18 23:11:49 +0000 | [diff] [blame] | 489 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 490 | public: |
DeLesley Hutchins | fbdee4e | 2013-10-11 21:55:33 +0000 | [diff] [blame] | 491 | void checkCallability(const PropagationInfo &PInfo, |
| 492 | const FunctionDecl *FunDecl, |
| 493 | SourceLocation BlameLoc); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 494 | bool handleCall(const CallExpr *Call, const Expr *ObjArg, |
| 495 | const FunctionDecl *FunD); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 496 | |
| 497 | void VisitBinaryOperator(const BinaryOperator *BinOp); |
| 498 | void VisitCallExpr(const CallExpr *Call); |
| 499 | void VisitCastExpr(const CastExpr *Cast); |
DeLesley Hutchins | fbdee4e | 2013-10-11 21:55:33 +0000 | [diff] [blame] | 500 | void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *Temp); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 501 | void VisitCXXConstructExpr(const CXXConstructExpr *Call); |
| 502 | void VisitCXXMemberCallExpr(const CXXMemberCallExpr *Call); |
| 503 | void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *Call); |
| 504 | void VisitDeclRefExpr(const DeclRefExpr *DeclRef); |
| 505 | void VisitDeclStmt(const DeclStmt *DelcS); |
| 506 | void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *Temp); |
| 507 | void VisitMemberExpr(const MemberExpr *MExpr); |
DeLesley Hutchins | b570c13 | 2013-08-29 22:36:05 +0000 | [diff] [blame] | 508 | void VisitParmVarDecl(const ParmVarDecl *Param); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 509 | void VisitReturnStmt(const ReturnStmt *Ret); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 510 | void VisitUnaryOperator(const UnaryOperator *UOp); |
| 511 | void VisitVarDecl(const VarDecl *Var); |
Reid Kleckner | e846dea | 2013-08-12 23:49:39 +0000 | [diff] [blame] | 512 | |
DeLesley Hutchins | b570c13 | 2013-08-29 22:36:05 +0000 | [diff] [blame] | 513 | ConsumedStmtVisitor(AnalysisDeclContext &AC, ConsumedAnalyzer &Analyzer, |
| 514 | ConsumedStateMap *StateMap) |
| 515 | : AC(AC), Analyzer(Analyzer), StateMap(StateMap) {} |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 516 | |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 517 | PropagationInfo getInfo(const Expr *StmtNode) const { |
| 518 | ConstInfoEntry Entry = findInfo(StmtNode); |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 519 | |
| 520 | if (Entry != PropagationMap.end()) |
| 521 | return Entry->second; |
| 522 | else |
| 523 | return PropagationInfo(); |
| 524 | } |
| 525 | |
| 526 | void reset(ConsumedStateMap *NewStateMap) { |
| 527 | StateMap = NewStateMap; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 528 | } |
| 529 | }; |
| 530 | |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 531 | |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 532 | void ConsumedStmtVisitor::forwardInfo(const Expr *From, const Expr *To) { |
| 533 | InfoEntry Entry = findInfo(From); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 534 | if (Entry != PropagationMap.end()) |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 535 | insertInfo(To, Entry->second); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | |
| 539 | // Create a new state for To, which is initialized to the state of From. |
| 540 | // If NS is not CS_None, sets the state of From to NS. |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 541 | void ConsumedStmtVisitor::copyInfo(const Expr *From, const Expr *To, |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 542 | ConsumedState NS) { |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 543 | InfoEntry Entry = findInfo(From); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 544 | if (Entry != PropagationMap.end()) { |
| 545 | PropagationInfo& PInfo = Entry->second; |
| 546 | ConsumedState CS = PInfo.getAsState(StateMap); |
| 547 | if (CS != CS_None) |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 548 | insertInfo(To, PropagationInfo(CS)); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 549 | if (NS != CS_None && PInfo.isPointerToValue()) |
| 550 | setStateForVarOrTmp(StateMap, PInfo, NS); |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | |
| 555 | // Get the ConsumedState for From |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 556 | ConsumedState ConsumedStmtVisitor::getInfo(const Expr *From) { |
| 557 | InfoEntry Entry = findInfo(From); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 558 | if (Entry != PropagationMap.end()) { |
| 559 | PropagationInfo& PInfo = Entry->second; |
| 560 | return PInfo.getAsState(StateMap); |
| 561 | } |
| 562 | return CS_None; |
| 563 | } |
| 564 | |
| 565 | |
| 566 | // If we already have info for To then update it, otherwise create a new entry. |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 567 | void ConsumedStmtVisitor::setInfo(const Expr *To, ConsumedState NS) { |
| 568 | InfoEntry Entry = findInfo(To); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 569 | if (Entry != PropagationMap.end()) { |
| 570 | PropagationInfo& PInfo = Entry->second; |
| 571 | if (PInfo.isPointerToValue()) |
| 572 | setStateForVarOrTmp(StateMap, PInfo, NS); |
| 573 | } else if (NS != CS_None) { |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 574 | insertInfo(To, PropagationInfo(NS)); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | |
| 578 | |
| 579 | |
DeLesley Hutchins | 2445b12 | 2013-08-26 20:34:59 +0000 | [diff] [blame] | 580 | void ConsumedStmtVisitor::checkCallability(const PropagationInfo &PInfo, |
DeLesley Hutchins | c2ecf0d | 2013-08-22 20:44:47 +0000 | [diff] [blame] | 581 | const FunctionDecl *FunDecl, |
DeLesley Hutchins | fbdee4e | 2013-10-11 21:55:33 +0000 | [diff] [blame] | 582 | SourceLocation BlameLoc) { |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 583 | assert(!PInfo.isTest()); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 584 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 585 | const CallableWhenAttr *CWAttr = FunDecl->getAttr<CallableWhenAttr>(); |
Aaron Ballman | b06f2ef | 2013-12-19 02:58:51 +0000 | [diff] [blame] | 586 | if (!CWAttr) |
| 587 | return; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 588 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 589 | if (PInfo.isVar()) { |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 590 | ConsumedState VarState = StateMap->getState(PInfo.getVar()); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 591 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 592 | if (VarState == CS_None || isCallableInState(CWAttr, VarState)) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 593 | return; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 594 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 595 | Analyzer.WarningsHandler.warnUseInInvalidState( |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 596 | FunDecl->getNameAsString(), PInfo.getVar()->getNameAsString(), |
DeLesley Hutchins | fbdee4e | 2013-10-11 21:55:33 +0000 | [diff] [blame] | 597 | stateToString(VarState), BlameLoc); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 598 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 599 | } else { |
| 600 | ConsumedState TmpState = PInfo.getAsState(StateMap); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 601 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 602 | if (TmpState == CS_None || isCallableInState(CWAttr, TmpState)) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 603 | return; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 604 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 605 | Analyzer.WarningsHandler.warnUseOfTempInInvalidState( |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 606 | FunDecl->getNameAsString(), stateToString(TmpState), BlameLoc); |
DeLesley Hutchins | c2ecf0d | 2013-08-22 20:44:47 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 610 | |
| 611 | // 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. |
| 614 | bool ConsumedStmtVisitor::handleCall(const CallExpr *Call, const Expr *ObjArg, |
| 615 | const FunctionDecl *FunD) { |
| 616 | unsigned Offset = 0; |
DeLesley Hutchins | 80a3842 | 2014-01-16 23:07:16 +0000 | [diff] [blame] | 617 | if (isa<CXXOperatorCallExpr>(Call) && isa<CXXMethodDecl>(FunD)) |
| 618 | Offset = 1; // first argument is 'this' |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 619 | |
| 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 Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 629 | InfoEntry Entry = findInfo(Call->getArg(Index)); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 630 | |
| 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 Hutchins | 80a3842 | 2014-01-16 23:07:16 +0000 | [diff] [blame] | 650 | if (isRValueRef(ParamType)) |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 651 | setStateForVarOrTmp(StateMap, PInfo, consumed::CS_Consumed); |
| 652 | else if (ReturnTypestateAttr *RT = Param->getAttr<ReturnTypestateAttr>()) |
| 653 | setStateForVarOrTmp(StateMap, PInfo, mapReturnTypestateAttrState(RT)); |
DeLesley Hutchins | 80a3842 | 2014-01-16 23:07:16 +0000 | [diff] [blame] | 654 | else if (isPointerOrRef(ParamType) && |
| 655 | (!ParamType->getPointeeType().isConstQualified() || |
| 656 | isSetOnReadPtrType(ParamType))) |
| 657 | setStateForVarOrTmp(StateMap, PInfo, consumed::CS_Unknown); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | if (!ObjArg) |
| 661 | return false; |
| 662 | |
| 663 | // check implicit 'self' parameter, if present |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 664 | InfoEntry Entry = findInfo(ObjArg); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 665 | 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 Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 685 | } |
| 686 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 687 | |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 688 | void ConsumedStmtVisitor::propagateReturnType(const Expr *Call, |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 689 | const FunctionDecl *Fun) { |
| 690 | QualType RetType = Fun->getCallResultType(); |
| 691 | if (RetType->isReferenceType()) |
| 692 | RetType = RetType->getPointeeType(); |
| 693 | |
| 694 | if (isConsumableType(RetType)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 695 | ConsumedState ReturnState; |
Aaron Ballman | b06f2ef | 2013-12-19 02:58:51 +0000 | [diff] [blame] | 696 | if (ReturnTypestateAttr *RTA = Fun->getAttr<ReturnTypestateAttr>()) |
| 697 | ReturnState = mapReturnTypestateAttrState(RTA); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 698 | else |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 699 | ReturnState = mapConsumableAttrState(RetType); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 700 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 701 | PropagationMap.insert(PairType(Call, PropagationInfo(ReturnState))); |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 702 | } |
| 703 | } |
| 704 | |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 705 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 706 | void ConsumedStmtVisitor::VisitBinaryOperator(const BinaryOperator *BinOp) { |
| 707 | switch (BinOp->getOpcode()) { |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 708 | case BO_LAnd: |
| 709 | case BO_LOr : { |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 710 | InfoEntry LEntry = findInfo(BinOp->getLHS()), |
| 711 | REntry = findInfo(BinOp->getRHS()); |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 712 | |
| 713 | VarTestResult LTest, RTest; |
| 714 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 715 | if (LEntry != PropagationMap.end() && LEntry->second.isVarTest()) { |
| 716 | LTest = LEntry->second.getVarTest(); |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 717 | |
| 718 | } else { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 719 | LTest.Var = nullptr; |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 720 | LTest.TestsFor = CS_None; |
| 721 | } |
| 722 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 723 | if (REntry != PropagationMap.end() && REntry->second.isVarTest()) { |
| 724 | RTest = REntry->second.getVarTest(); |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 725 | |
| 726 | } else { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 727 | RTest.Var = nullptr; |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 728 | RTest.TestsFor = CS_None; |
| 729 | } |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 730 | |
| 731 | if (!(LTest.Var == nullptr && RTest.Var == nullptr)) |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 732 | PropagationMap.insert(PairType(BinOp, PropagationInfo(BinOp, |
| 733 | static_cast<EffectiveOp>(BinOp->getOpcode() == BO_LOr), LTest, RTest))); |
| 734 | |
| 735 | break; |
| 736 | } |
| 737 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 738 | case BO_PtrMemD: |
| 739 | case BO_PtrMemI: |
| 740 | forwardInfo(BinOp->getLHS(), BinOp); |
| 741 | break; |
| 742 | |
| 743 | default: |
| 744 | break; |
| 745 | } |
| 746 | } |
| 747 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 748 | void ConsumedStmtVisitor::VisitCallExpr(const CallExpr *Call) { |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 749 | const FunctionDecl *FunDecl = Call->getDirectCallee(); |
| 750 | if (!FunDecl) |
| 751 | return; |
| 752 | |
| 753 | // Special case for the std::move function. |
| 754 | // TODO: Make this more specific. (Deferred) |
Richard Trieu | c771d5d | 2014-05-28 02:16:01 +0000 | [diff] [blame] | 755 | if (Call->getNumArgs() == 1 && FunDecl->getNameAsString() == "move" && |
| 756 | FunDecl->isInStdNamespace()) { |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 757 | copyInfo(Call->getArg(0), Call, CS_Consumed); |
| 758 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 759 | } |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 760 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 761 | handleCall(Call, nullptr, FunDecl); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 762 | propagateReturnType(Call, FunDecl); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | void ConsumedStmtVisitor::VisitCastExpr(const CastExpr *Cast) { |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 766 | forwardInfo(Cast->getSubExpr(), Cast); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 767 | } |
| 768 | |
DeLesley Hutchins | fbdee4e | 2013-10-11 21:55:33 +0000 | [diff] [blame] | 769 | void ConsumedStmtVisitor::VisitCXXBindTemporaryExpr( |
| 770 | const CXXBindTemporaryExpr *Temp) { |
| 771 | |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 772 | InfoEntry Entry = findInfo(Temp->getSubExpr()); |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 773 | |
| 774 | if (Entry != PropagationMap.end() && !Entry->second.isTest()) { |
| 775 | StateMap->setState(Temp, Entry->second.getAsState(StateMap)); |
| 776 | PropagationMap.insert(PairType(Temp, PropagationInfo(Temp))); |
| 777 | } |
DeLesley Hutchins | fbdee4e | 2013-10-11 21:55:33 +0000 | [diff] [blame] | 778 | } |
| 779 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 780 | void ConsumedStmtVisitor::VisitCXXConstructExpr(const CXXConstructExpr *Call) { |
| 781 | CXXConstructorDecl *Constructor = Call->getConstructor(); |
Reid Kleckner | e846dea | 2013-08-12 23:49:39 +0000 | [diff] [blame] | 782 | |
| 783 | ASTContext &CurrContext = AC.getASTContext(); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 784 | QualType ThisType = Constructor->getThisType(CurrContext)->getPointeeType(); |
| 785 | |
DeLesley Hutchins | 11a66c1 | 2013-10-18 18:36:21 +0000 | [diff] [blame] | 786 | if (!isConsumableType(ThisType)) |
| 787 | return; |
| 788 | |
| 789 | // FIXME: What should happen if someone annotates the move constructor? |
Aaron Ballman | b06f2ef | 2013-12-19 02:58:51 +0000 | [diff] [blame] | 790 | if (ReturnTypestateAttr *RTA = Constructor->getAttr<ReturnTypestateAttr>()) { |
| 791 | // TODO: Adjust state of args appropriately. |
| 792 | ConsumedState RetState = mapReturnTypestateAttrState(RTA); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 793 | PropagationMap.insert(PairType(Call, PropagationInfo(RetState))); |
| 794 | } else if (Constructor->isDefaultConstructor()) { |
DeLesley Hutchins | 11a66c1 | 2013-10-18 18:36:21 +0000 | [diff] [blame] | 795 | PropagationMap.insert(PairType(Call, |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 796 | PropagationInfo(consumed::CS_Consumed))); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 797 | } else if (Constructor->isMoveConstructor()) { |
| 798 | copyInfo(Call->getArg(0), Call, CS_Consumed); |
DeLesley Hutchins | 11a66c1 | 2013-10-18 18:36:21 +0000 | [diff] [blame] | 799 | } else if (Constructor->isCopyConstructor()) { |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 800 | // Copy state from arg. If setStateOnRead then set arg to CS_Unknown. |
| 801 | ConsumedState NS = |
| 802 | isSetOnReadPtrType(Constructor->getThisType(CurrContext)) ? |
| 803 | CS_Unknown : CS_None; |
| 804 | copyInfo(Call->getArg(0), Call, NS); |
DeLesley Hutchins | 11a66c1 | 2013-10-18 18:36:21 +0000 | [diff] [blame] | 805 | } else { |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 806 | // TODO: Adjust state of args appropriately. |
DeLesley Hutchins | 11a66c1 | 2013-10-18 18:36:21 +0000 | [diff] [blame] | 807 | ConsumedState RetState = mapConsumableAttrState(ThisType); |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 808 | PropagationMap.insert(PairType(Call, PropagationInfo(RetState))); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 812 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 813 | void ConsumedStmtVisitor::VisitCXXMemberCallExpr( |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 814 | const CXXMemberCallExpr *Call) { |
| 815 | CXXMethodDecl* MD = Call->getMethodDecl(); |
| 816 | if (!MD) |
| 817 | return; |
| 818 | |
| 819 | handleCall(Call, Call->getImplicitObjectArgument(), MD); |
| 820 | propagateReturnType(Call, MD); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 821 | } |
| 822 | |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 823 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 824 | void ConsumedStmtVisitor::VisitCXXOperatorCallExpr( |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 825 | const CXXOperatorCallExpr *Call) { |
| 826 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 827 | const FunctionDecl *FunDecl = |
| 828 | dyn_cast_or_null<FunctionDecl>(Call->getDirectCallee()); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 829 | if (!FunDecl) return; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 830 | |
| 831 | if (Call->getOperator() == OO_Equal) { |
| 832 | ConsumedState CS = getInfo(Call->getArg(1)); |
| 833 | if (!handleCall(Call, Call->getArg(0), FunDecl)) |
| 834 | setInfo(Call->getArg(0), CS); |
| 835 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 836 | } |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 837 | |
| 838 | if (const CXXMemberCallExpr *MCall = dyn_cast<CXXMemberCallExpr>(Call)) |
| 839 | handleCall(MCall, MCall->getImplicitObjectArgument(), FunDecl); |
| 840 | else |
| 841 | handleCall(Call, Call->getArg(0), FunDecl); |
| 842 | |
| 843 | propagateReturnType(Call, FunDecl); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | void ConsumedStmtVisitor::VisitDeclRefExpr(const DeclRefExpr *DeclRef) { |
| 847 | if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(DeclRef->getDecl())) |
| 848 | if (StateMap->getState(Var) != consumed::CS_None) |
| 849 | PropagationMap.insert(PairType(DeclRef, PropagationInfo(Var))); |
| 850 | } |
| 851 | |
| 852 | void ConsumedStmtVisitor::VisitDeclStmt(const DeclStmt *DeclS) { |
Aaron Ballman | 535bbcc | 2014-03-14 17:01:24 +0000 | [diff] [blame] | 853 | for (const auto *DI : DeclS->decls()) |
| 854 | if (isa<VarDecl>(DI)) |
| 855 | VisitVarDecl(cast<VarDecl>(DI)); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 856 | |
| 857 | if (DeclS->isSingleDecl()) |
| 858 | if (const VarDecl *Var = dyn_cast_or_null<VarDecl>(DeclS->getSingleDecl())) |
| 859 | PropagationMap.insert(PairType(DeclS, PropagationInfo(Var))); |
| 860 | } |
| 861 | |
| 862 | void ConsumedStmtVisitor::VisitMaterializeTemporaryExpr( |
| 863 | const MaterializeTemporaryExpr *Temp) { |
| 864 | |
Chris Wailes | 4493088 | 2013-10-24 14:28:17 +0000 | [diff] [blame] | 865 | forwardInfo(Temp->GetTemporaryExpr(), Temp); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | void ConsumedStmtVisitor::VisitMemberExpr(const MemberExpr *MExpr) { |
| 869 | forwardInfo(MExpr->getBase(), MExpr); |
| 870 | } |
| 871 | |
DeLesley Hutchins | b570c13 | 2013-08-29 22:36:05 +0000 | [diff] [blame] | 872 | |
| 873 | void ConsumedStmtVisitor::VisitParmVarDecl(const ParmVarDecl *Param) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 874 | QualType ParamType = Param->getType(); |
| 875 | ConsumedState ParamState = consumed::CS_None; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 876 | |
Aaron Ballman | b06f2ef | 2013-12-19 02:58:51 +0000 | [diff] [blame] | 877 | if (const ParamTypestateAttr *PTA = Param->getAttr<ParamTypestateAttr>()) |
| 878 | ParamState = mapParamTypestateAttrState(PTA); |
| 879 | else if (isConsumableType(ParamType)) |
| 880 | ParamState = mapConsumableAttrState(ParamType); |
DeLesley Hutchins | 80a3842 | 2014-01-16 23:07:16 +0000 | [diff] [blame] | 881 | else if (isRValueRef(ParamType) && |
| 882 | isConsumableType(ParamType->getPointeeType())) |
Aaron Ballman | b06f2ef | 2013-12-19 02:58:51 +0000 | [diff] [blame] | 883 | ParamState = mapConsumableAttrState(ParamType->getPointeeType()); |
| 884 | else if (ParamType->isReferenceType() && |
DeLesley Hutchins | 80a3842 | 2014-01-16 23:07:16 +0000 | [diff] [blame] | 885 | isConsumableType(ParamType->getPointeeType())) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 886 | ParamState = consumed::CS_Unknown; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 887 | |
| 888 | if (ParamState != CS_None) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 889 | StateMap->setState(Param, ParamState); |
DeLesley Hutchins | b570c13 | 2013-08-29 22:36:05 +0000 | [diff] [blame] | 890 | } |
| 891 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 892 | void ConsumedStmtVisitor::VisitReturnStmt(const ReturnStmt *Ret) { |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 893 | ConsumedState ExpectedState = Analyzer.getExpectedReturnState(); |
| 894 | |
| 895 | if (ExpectedState != CS_None) { |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 896 | InfoEntry Entry = findInfo(Ret->getRetValue()); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 897 | |
| 898 | if (Entry != PropagationMap.end()) { |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 899 | ConsumedState RetState = Entry->second.getAsState(StateMap); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 900 | |
| 901 | if (RetState != ExpectedState) |
| 902 | Analyzer.WarningsHandler.warnReturnTypestateMismatch( |
| 903 | Ret->getReturnLoc(), stateToString(ExpectedState), |
| 904 | stateToString(RetState)); |
| 905 | } |
| 906 | } |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 907 | |
| 908 | StateMap->checkParamsForReturnTypestate(Ret->getLocStart(), |
| 909 | Analyzer.WarningsHandler); |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 910 | } |
| 911 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 912 | void ConsumedStmtVisitor::VisitUnaryOperator(const UnaryOperator *UOp) { |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 913 | InfoEntry Entry = findInfo(UOp->getSubExpr()); |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 914 | if (Entry == PropagationMap.end()) return; |
| 915 | |
| 916 | switch (UOp->getOpcode()) { |
| 917 | case UO_AddrOf: |
| 918 | PropagationMap.insert(PairType(UOp, Entry->second)); |
| 919 | break; |
| 920 | |
| 921 | case UO_LNot: |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 922 | if (Entry->second.isTest()) |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 923 | PropagationMap.insert(PairType(UOp, Entry->second.invertTest())); |
| 924 | break; |
| 925 | |
| 926 | default: |
| 927 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 928 | } |
| 929 | } |
| 930 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 931 | // TODO: See if I need to check for reference types here. |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 932 | void ConsumedStmtVisitor::VisitVarDecl(const VarDecl *Var) { |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 933 | if (isConsumableType(Var->getType())) { |
DeLesley Hutchins | b570c13 | 2013-08-29 22:36:05 +0000 | [diff] [blame] | 934 | if (Var->hasInit()) { |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 935 | MapType::iterator VIT = findInfo(Var->getInit()->IgnoreImplicit()); |
DeLesley Hutchins | 8121866 | 2013-10-18 23:11:49 +0000 | [diff] [blame] | 936 | if (VIT != PropagationMap.end()) { |
| 937 | PropagationInfo PInfo = VIT->second; |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 938 | ConsumedState St = PInfo.getAsState(StateMap); |
| 939 | |
DeLesley Hutchins | 8121866 | 2013-10-18 23:11:49 +0000 | [diff] [blame] | 940 | if (St != consumed::CS_None) { |
| 941 | StateMap->setState(Var, St); |
| 942 | return; |
| 943 | } |
| 944 | } |
DeLesley Hutchins | b570c13 | 2013-08-29 22:36:05 +0000 | [diff] [blame] | 945 | } |
DeLesley Hutchins | 8121866 | 2013-10-18 23:11:49 +0000 | [diff] [blame] | 946 | // Otherwise |
| 947 | StateMap->setState(Var, consumed::CS_Unknown); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 948 | } |
| 949 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 950 | }} // end clang::consumed::ConsumedStmtVisitor |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 951 | |
| 952 | namespace clang { |
| 953 | namespace consumed { |
| 954 | |
Benjamin Kramer | 3a74345 | 2015-03-09 15:03:32 +0000 | [diff] [blame] | 955 | static void splitVarStateForIf(const IfStmt *IfNode, const VarTestResult &Test, |
| 956 | ConsumedStateMap *ThenStates, |
| 957 | ConsumedStateMap *ElseStates) { |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 958 | ConsumedState VarState = ThenStates->getState(Test.Var); |
| 959 | |
| 960 | if (VarState == CS_Unknown) { |
| 961 | ThenStates->setState(Test.Var, Test.TestsFor); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 962 | ElseStates->setState(Test.Var, invertConsumedUnconsumed(Test.TestsFor)); |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 963 | |
| 964 | } else if (VarState == invertConsumedUnconsumed(Test.TestsFor)) { |
| 965 | ThenStates->markUnreachable(); |
| 966 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 967 | } else if (VarState == Test.TestsFor) { |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 968 | ElseStates->markUnreachable(); |
| 969 | } |
| 970 | } |
| 971 | |
Benjamin Kramer | 3a74345 | 2015-03-09 15:03:32 +0000 | [diff] [blame] | 972 | static void splitVarStateForIfBinOp(const PropagationInfo &PInfo, |
| 973 | ConsumedStateMap *ThenStates, |
| 974 | ConsumedStateMap *ElseStates) { |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 975 | const VarTestResult <est = PInfo.getLTest(), |
| 976 | &RTest = PInfo.getRTest(); |
| 977 | |
| 978 | ConsumedState LState = LTest.Var ? ThenStates->getState(LTest.Var) : CS_None, |
| 979 | RState = RTest.Var ? ThenStates->getState(RTest.Var) : CS_None; |
| 980 | |
| 981 | if (LTest.Var) { |
| 982 | if (PInfo.testEffectiveOp() == EO_And) { |
| 983 | if (LState == CS_Unknown) { |
| 984 | ThenStates->setState(LTest.Var, LTest.TestsFor); |
| 985 | |
| 986 | } else if (LState == invertConsumedUnconsumed(LTest.TestsFor)) { |
| 987 | ThenStates->markUnreachable(); |
| 988 | |
| 989 | } else if (LState == LTest.TestsFor && isKnownState(RState)) { |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 990 | if (RState == RTest.TestsFor) |
| 991 | ElseStates->markUnreachable(); |
| 992 | else |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 993 | ThenStates->markUnreachable(); |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | } else { |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 997 | if (LState == CS_Unknown) { |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 998 | ElseStates->setState(LTest.Var, |
| 999 | invertConsumedUnconsumed(LTest.TestsFor)); |
| 1000 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1001 | } else if (LState == LTest.TestsFor) { |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1002 | ElseStates->markUnreachable(); |
| 1003 | |
| 1004 | } else if (LState == invertConsumedUnconsumed(LTest.TestsFor) && |
| 1005 | isKnownState(RState)) { |
| 1006 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1007 | if (RState == RTest.TestsFor) |
| 1008 | ElseStates->markUnreachable(); |
| 1009 | else |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1010 | ThenStates->markUnreachable(); |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | if (RTest.Var) { |
| 1016 | if (PInfo.testEffectiveOp() == EO_And) { |
| 1017 | if (RState == CS_Unknown) |
| 1018 | ThenStates->setState(RTest.Var, RTest.TestsFor); |
| 1019 | else if (RState == invertConsumedUnconsumed(RTest.TestsFor)) |
| 1020 | ThenStates->markUnreachable(); |
| 1021 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1022 | } else { |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1023 | if (RState == CS_Unknown) |
| 1024 | ElseStates->setState(RTest.Var, |
| 1025 | invertConsumedUnconsumed(RTest.TestsFor)); |
| 1026 | else if (RState == RTest.TestsFor) |
| 1027 | ElseStates->markUnreachable(); |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1032 | bool ConsumedBlockInfo::allBackEdgesVisited(const CFGBlock *CurrBlock, |
| 1033 | const CFGBlock *TargetBlock) { |
| 1034 | |
| 1035 | assert(CurrBlock && "Block pointer must not be NULL"); |
| 1036 | assert(TargetBlock && "TargetBlock pointer must not be NULL"); |
| 1037 | |
| 1038 | unsigned int CurrBlockOrder = VisitOrder[CurrBlock->getBlockID()]; |
| 1039 | for (CFGBlock::const_pred_iterator PI = TargetBlock->pred_begin(), |
| 1040 | PE = TargetBlock->pred_end(); PI != PE; ++PI) { |
| 1041 | if (*PI && CurrBlockOrder < VisitOrder[(*PI)->getBlockID()] ) |
| 1042 | return false; |
| 1043 | } |
| 1044 | return true; |
| 1045 | } |
| 1046 | |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1047 | void ConsumedBlockInfo::addInfo( |
| 1048 | const CFGBlock *Block, ConsumedStateMap *StateMap, |
| 1049 | std::unique_ptr<ConsumedStateMap> &OwnedStateMap) { |
| 1050 | |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1051 | assert(Block && "Block pointer must not be NULL"); |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1052 | |
| 1053 | auto &Entry = StateMapsArray[Block->getBlockID()]; |
| 1054 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1055 | if (Entry) { |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1056 | Entry->intersect(*StateMap); |
| 1057 | } else if (OwnedStateMap) |
| 1058 | Entry = std::move(OwnedStateMap); |
| 1059 | else |
| 1060 | Entry = llvm::make_unique<ConsumedStateMap>(*StateMap); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | void ConsumedBlockInfo::addInfo(const CFGBlock *Block, |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1064 | std::unique_ptr<ConsumedStateMap> StateMap) { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1065 | |
| 1066 | assert(Block && "Block pointer must not be NULL"); |
| 1067 | |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1068 | auto &Entry = StateMapsArray[Block->getBlockID()]; |
| 1069 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1070 | if (Entry) { |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1071 | Entry->intersect(*StateMap); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1072 | } else { |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1073 | Entry = std::move(StateMap); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1077 | ConsumedStateMap* ConsumedBlockInfo::borrowInfo(const CFGBlock *Block) { |
| 1078 | assert(Block && "Block pointer must not be NULL"); |
| 1079 | assert(StateMapsArray[Block->getBlockID()] && "Block has no block info"); |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1080 | |
| 1081 | return StateMapsArray[Block->getBlockID()].get(); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1084 | void ConsumedBlockInfo::discardInfo(const CFGBlock *Block) { |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1085 | StateMapsArray[Block->getBlockID()] = nullptr; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1088 | std::unique_ptr<ConsumedStateMap> |
| 1089 | ConsumedBlockInfo::getInfo(const CFGBlock *Block) { |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1090 | assert(Block && "Block pointer must not be NULL"); |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1091 | |
| 1092 | auto &Entry = StateMapsArray[Block->getBlockID()]; |
| 1093 | return isBackEdgeTarget(Block) ? llvm::make_unique<ConsumedStateMap>(*Entry) |
| 1094 | : std::move(Entry); |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | bool ConsumedBlockInfo::isBackEdge(const CFGBlock *From, const CFGBlock *To) { |
| 1098 | assert(From && "From block must not be NULL"); |
| 1099 | assert(To && "From block must not be NULL"); |
| 1100 | |
| 1101 | return VisitOrder[From->getBlockID()] > VisitOrder[To->getBlockID()]; |
| 1102 | } |
| 1103 | |
| 1104 | bool ConsumedBlockInfo::isBackEdgeTarget(const CFGBlock *Block) { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1105 | assert(Block && "Block pointer must not be NULL"); |
| 1106 | |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1107 | // Anything with less than two predecessors can't be the target of a back |
| 1108 | // edge. |
| 1109 | if (Block->pred_size() < 2) |
| 1110 | return false; |
| 1111 | |
| 1112 | unsigned int BlockVisitOrder = VisitOrder[Block->getBlockID()]; |
| 1113 | for (CFGBlock::const_pred_iterator PI = Block->pred_begin(), |
| 1114 | PE = Block->pred_end(); PI != PE; ++PI) { |
| 1115 | if (*PI && BlockVisitOrder < VisitOrder[(*PI)->getBlockID()]) |
| 1116 | return true; |
| 1117 | } |
| 1118 | return false; |
| 1119 | } |
| 1120 | |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1121 | void ConsumedStateMap::checkParamsForReturnTypestate(SourceLocation BlameLoc, |
| 1122 | ConsumedWarningsHandlerBase &WarningsHandler) const { |
| 1123 | |
Aaron Ballman | 35897d9 | 2014-04-28 14:56:59 +0000 | [diff] [blame] | 1124 | for (const auto &DM : VarMap) { |
| 1125 | if (isa<ParmVarDecl>(DM.first)) { |
| 1126 | const ParmVarDecl *Param = cast<ParmVarDecl>(DM.first); |
Aaron Ballman | b06f2ef | 2013-12-19 02:58:51 +0000 | [diff] [blame] | 1127 | const ReturnTypestateAttr *RTA = Param->getAttr<ReturnTypestateAttr>(); |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1128 | |
Aaron Ballman | b06f2ef | 2013-12-19 02:58:51 +0000 | [diff] [blame] | 1129 | if (!RTA) |
| 1130 | continue; |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1131 | |
Aaron Ballman | b06f2ef | 2013-12-19 02:58:51 +0000 | [diff] [blame] | 1132 | ConsumedState ExpectedState = mapReturnTypestateAttrState(RTA); |
Aaron Ballman | 35897d9 | 2014-04-28 14:56:59 +0000 | [diff] [blame] | 1133 | if (DM.second != ExpectedState) |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1134 | WarningsHandler.warnParamReturnTypestateMismatch(BlameLoc, |
| 1135 | Param->getNameAsString(), stateToString(ExpectedState), |
Aaron Ballman | 35897d9 | 2014-04-28 14:56:59 +0000 | [diff] [blame] | 1136 | stateToString(DM.second)); |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 1141 | void ConsumedStateMap::clearTemporaries() { |
| 1142 | TmpMap.clear(); |
| 1143 | } |
| 1144 | |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1145 | ConsumedState ConsumedStateMap::getState(const VarDecl *Var) const { |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 1146 | VarMapType::const_iterator Entry = VarMap.find(Var); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1147 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 1148 | if (Entry != VarMap.end()) |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1149 | return Entry->second; |
| 1150 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 1151 | return CS_None; |
| 1152 | } |
| 1153 | |
| 1154 | ConsumedState |
| 1155 | ConsumedStateMap::getState(const CXXBindTemporaryExpr *Tmp) const { |
| 1156 | TmpMapType::const_iterator Entry = TmpMap.find(Tmp); |
| 1157 | |
| 1158 | if (Entry != TmpMap.end()) |
| 1159 | return Entry->second; |
| 1160 | |
| 1161 | return CS_None; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1164 | void ConsumedStateMap::intersect(const ConsumedStateMap &Other) { |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1165 | ConsumedState LocalState; |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1166 | |
| 1167 | if (this->From && this->From == Other.From && !Other.Reachable) { |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1168 | this->markUnreachable(); |
| 1169 | return; |
| 1170 | } |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1171 | |
| 1172 | for (const auto &DM : Other.VarMap) { |
Aaron Ballman | 35897d9 | 2014-04-28 14:56:59 +0000 | [diff] [blame] | 1173 | LocalState = this->getState(DM.first); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1174 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1175 | if (LocalState == CS_None) |
| 1176 | continue; |
| 1177 | |
Aaron Ballman | 35897d9 | 2014-04-28 14:56:59 +0000 | [diff] [blame] | 1178 | if (LocalState != DM.second) |
| 1179 | VarMap[DM.first] = CS_Unknown; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1180 | } |
| 1181 | } |
| 1182 | |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1183 | void ConsumedStateMap::intersectAtLoopHead(const CFGBlock *LoopHead, |
| 1184 | const CFGBlock *LoopBack, const ConsumedStateMap *LoopBackStates, |
| 1185 | ConsumedWarningsHandlerBase &WarningsHandler) { |
| 1186 | |
| 1187 | ConsumedState LocalState; |
DeLesley Hutchins | fbdee4e | 2013-10-11 21:55:33 +0000 | [diff] [blame] | 1188 | SourceLocation BlameLoc = getLastStmtLoc(LoopBack); |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1189 | |
Aaron Ballman | 35897d9 | 2014-04-28 14:56:59 +0000 | [diff] [blame] | 1190 | for (const auto &DM : LoopBackStates->VarMap) { |
| 1191 | LocalState = this->getState(DM.first); |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1192 | |
| 1193 | if (LocalState == CS_None) |
| 1194 | continue; |
| 1195 | |
Aaron Ballman | 35897d9 | 2014-04-28 14:56:59 +0000 | [diff] [blame] | 1196 | if (LocalState != DM.second) { |
| 1197 | VarMap[DM.first] = CS_Unknown; |
Aaron Ballman | fe46e62 | 2014-04-28 13:01:32 +0000 | [diff] [blame] | 1198 | WarningsHandler.warnLoopStateMismatch(BlameLoc, |
Aaron Ballman | 35897d9 | 2014-04-28 14:56:59 +0000 | [diff] [blame] | 1199 | DM.first->getNameAsString()); |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | } |
| 1203 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1204 | void ConsumedStateMap::markUnreachable() { |
| 1205 | this->Reachable = false; |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 1206 | VarMap.clear(); |
| 1207 | TmpMap.clear(); |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1210 | void ConsumedStateMap::setState(const VarDecl *Var, ConsumedState State) { |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 1211 | VarMap[Var] = State; |
| 1212 | } |
| 1213 | |
| 1214 | void ConsumedStateMap::setState(const CXXBindTemporaryExpr *Tmp, |
| 1215 | ConsumedState State) { |
| 1216 | TmpMap[Tmp] = State; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Manuel Klimek | b33bded | 2014-05-08 11:50:00 +0000 | [diff] [blame] | 1219 | void ConsumedStateMap::remove(const CXXBindTemporaryExpr *Tmp) { |
| 1220 | TmpMap.erase(Tmp); |
DeLesley Hutchins | c2ecf0d | 2013-08-22 20:44:47 +0000 | [diff] [blame] | 1221 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1222 | |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1223 | bool ConsumedStateMap::operator!=(const ConsumedStateMap *Other) const { |
Aaron Ballman | 35897d9 | 2014-04-28 14:56:59 +0000 | [diff] [blame] | 1224 | for (const auto &DM : Other->VarMap) |
| 1225 | if (this->getState(DM.first) != DM.second) |
Aaron Ballman | fe46e62 | 2014-04-28 13:01:32 +0000 | [diff] [blame] | 1226 | return true; |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1227 | return false; |
| 1228 | } |
| 1229 | |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1230 | void ConsumedAnalyzer::determineExpectedReturnState(AnalysisDeclContext &AC, |
| 1231 | const FunctionDecl *D) { |
| 1232 | QualType ReturnType; |
| 1233 | if (const CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
| 1234 | ASTContext &CurrContext = AC.getASTContext(); |
| 1235 | ReturnType = Constructor->getThisType(CurrContext)->getPointeeType(); |
| 1236 | } else |
| 1237 | ReturnType = D->getCallResultType(); |
| 1238 | |
Aaron Ballman | b06f2ef | 2013-12-19 02:58:51 +0000 | [diff] [blame] | 1239 | if (const ReturnTypestateAttr *RTSAttr = D->getAttr<ReturnTypestateAttr>()) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1240 | const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1241 | if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1242 | // FIXME: This should be removed when template instantiation propagates |
| 1243 | // attributes at template specialization definition, not |
| 1244 | // declaration. When it is removed the test needs to be enabled |
| 1245 | // in SemaDeclAttr.cpp. |
| 1246 | WarningsHandler.warnReturnTypestateForUnconsumableType( |
| 1247 | RTSAttr->getLocation(), ReturnType.getAsString()); |
| 1248 | ExpectedReturnState = CS_None; |
| 1249 | } else |
| 1250 | ExpectedReturnState = mapReturnTypestateAttrState(RTSAttr); |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 1251 | } else if (isConsumableType(ReturnType)) { |
| 1252 | if (isAutoCastType(ReturnType)) // We can auto-cast the state to the |
| 1253 | ExpectedReturnState = CS_None; // expected state. |
| 1254 | else |
| 1255 | ExpectedReturnState = mapConsumableAttrState(ReturnType); |
| 1256 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1257 | else |
| 1258 | ExpectedReturnState = CS_None; |
| 1259 | } |
| 1260 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1261 | bool ConsumedAnalyzer::splitState(const CFGBlock *CurrBlock, |
| 1262 | const ConsumedStmtVisitor &Visitor) { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 1263 | |
| 1264 | std::unique_ptr<ConsumedStateMap> FalseStates( |
| 1265 | new ConsumedStateMap(*CurrStates)); |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1266 | PropagationInfo PInfo; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1267 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1268 | if (const IfStmt *IfNode = |
| 1269 | dyn_cast_or_null<IfStmt>(CurrBlock->getTerminator().getStmt())) { |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1270 | |
DeLesley Hutchins | d7fa5bd | 2014-03-20 20:39:20 +0000 | [diff] [blame] | 1271 | const Expr *Cond = IfNode->getCond(); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1272 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1273 | PInfo = Visitor.getInfo(Cond); |
| 1274 | if (!PInfo.isValid() && isa<BinaryOperator>(Cond)) |
| 1275 | PInfo = Visitor.getInfo(cast<BinaryOperator>(Cond)->getRHS()); |
| 1276 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 1277 | if (PInfo.isVarTest()) { |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1278 | CurrStates->setSource(Cond); |
| 1279 | FalseStates->setSource(Cond); |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1280 | splitVarStateForIf(IfNode, PInfo.getVarTest(), CurrStates.get(), |
Chris Wailes | 2dc8c42 | 2013-10-25 15:33:28 +0000 | [diff] [blame] | 1281 | FalseStates.get()); |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1282 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1283 | } else if (PInfo.isBinTest()) { |
| 1284 | CurrStates->setSource(PInfo.testSourceNode()); |
| 1285 | FalseStates->setSource(PInfo.testSourceNode()); |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1286 | splitVarStateForIfBinOp(PInfo, CurrStates.get(), FalseStates.get()); |
| 1287 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1288 | } else { |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1289 | return false; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1290 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1291 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1292 | } else if (const BinaryOperator *BinOp = |
| 1293 | dyn_cast_or_null<BinaryOperator>(CurrBlock->getTerminator().getStmt())) { |
| 1294 | |
| 1295 | PInfo = Visitor.getInfo(BinOp->getLHS()); |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 1296 | if (!PInfo.isVarTest()) { |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1297 | if ((BinOp = dyn_cast_or_null<BinaryOperator>(BinOp->getLHS()))) { |
| 1298 | PInfo = Visitor.getInfo(BinOp->getRHS()); |
| 1299 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 1300 | if (!PInfo.isVarTest()) |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1301 | return false; |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1302 | |
| 1303 | } else { |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1304 | return false; |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | CurrStates->setSource(BinOp); |
| 1309 | FalseStates->setSource(BinOp); |
| 1310 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 1311 | const VarTestResult &Test = PInfo.getVarTest(); |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1312 | ConsumedState VarState = CurrStates->getState(Test.Var); |
| 1313 | |
| 1314 | if (BinOp->getOpcode() == BO_LAnd) { |
| 1315 | if (VarState == CS_Unknown) |
| 1316 | CurrStates->setState(Test.Var, Test.TestsFor); |
| 1317 | else if (VarState == invertConsumedUnconsumed(Test.TestsFor)) |
| 1318 | CurrStates->markUnreachable(); |
| 1319 | |
| 1320 | } else if (BinOp->getOpcode() == BO_LOr) { |
| 1321 | if (VarState == CS_Unknown) |
| 1322 | FalseStates->setState(Test.Var, |
| 1323 | invertConsumedUnconsumed(Test.TestsFor)); |
| 1324 | else if (VarState == Test.TestsFor) |
| 1325 | FalseStates->markUnreachable(); |
| 1326 | } |
| 1327 | |
| 1328 | } else { |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1329 | return false; |
| 1330 | } |
| 1331 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1332 | CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(); |
| 1333 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1334 | if (*SI) |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1335 | BlockInfo.addInfo(*SI, std::move(CurrStates)); |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1336 | else |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1337 | CurrStates = nullptr; |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 1338 | |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1339 | if (*++SI) |
| 1340 | BlockInfo.addInfo(*SI, std::move(FalseStates)); |
| 1341 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1342 | return true; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | void ConsumedAnalyzer::run(AnalysisDeclContext &AC) { |
| 1346 | const FunctionDecl *D = dyn_cast_or_null<FunctionDecl>(AC.getDecl()); |
DeLesley Hutchins | 85c07d9 | 2013-09-10 23:10:10 +0000 | [diff] [blame] | 1347 | if (!D) |
| 1348 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1349 | |
DeLesley Hutchins | 85c07d9 | 2013-09-10 23:10:10 +0000 | [diff] [blame] | 1350 | CFG *CFGraph = AC.getCFG(); |
| 1351 | if (!CFGraph) |
| 1352 | return; |
DeLesley Hutchins | 6501320 | 2013-10-17 18:19:31 +0000 | [diff] [blame] | 1353 | |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1354 | determineExpectedReturnState(AC, D); |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1355 | |
Artyom Skrobov | 2772076 | 2014-09-23 08:34:41 +0000 | [diff] [blame] | 1356 | PostOrderCFGView *SortedGraph = AC.getAnalysis<PostOrderCFGView>(); |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1357 | // AC.getCFG()->viewCFG(LangOptions()); |
| 1358 | |
| 1359 | BlockInfo = ConsumedBlockInfo(CFGraph->getNumBlockIDs(), SortedGraph); |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1360 | |
| 1361 | CurrStates = llvm::make_unique<ConsumedStateMap>(); |
| 1362 | ConsumedStmtVisitor Visitor(AC, *this, CurrStates.get()); |
| 1363 | |
DeLesley Hutchins | b570c13 | 2013-08-29 22:36:05 +0000 | [diff] [blame] | 1364 | // Add all trackable parameters to the state map. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame^] | 1365 | for (const auto *PI : D->parameters()) |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 1366 | Visitor.VisitParmVarDecl(PI); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1367 | |
| 1368 | // Visit all of the function's basic blocks. |
Aaron Ballman | fe46e62 | 2014-04-28 13:01:32 +0000 | [diff] [blame] | 1369 | for (const auto *CurrBlock : *SortedGraph) { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1370 | if (!CurrStates) |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1371 | CurrStates = BlockInfo.getInfo(CurrBlock); |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1372 | |
| 1373 | if (!CurrStates) { |
| 1374 | continue; |
| 1375 | |
| 1376 | } else if (!CurrStates->isReachable()) { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1377 | CurrStates = nullptr; |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1378 | continue; |
| 1379 | } |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1380 | |
| 1381 | Visitor.reset(CurrStates.get()); |
| 1382 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1383 | // Visit all of the basic block's statements. |
Aaron Ballman | 35897d9 | 2014-04-28 14:56:59 +0000 | [diff] [blame] | 1384 | for (const auto &B : *CurrBlock) { |
| 1385 | switch (B.getKind()) { |
DeLesley Hutchins | c2ecf0d | 2013-08-22 20:44:47 +0000 | [diff] [blame] | 1386 | case CFGElement::Statement: |
Aaron Ballman | 35897d9 | 2014-04-28 14:56:59 +0000 | [diff] [blame] | 1387 | Visitor.Visit(B.castAs<CFGStmt>().getStmt()); |
DeLesley Hutchins | c2ecf0d | 2013-08-22 20:44:47 +0000 | [diff] [blame] | 1388 | break; |
DeLesley Hutchins | fbdee4e | 2013-10-11 21:55:33 +0000 | [diff] [blame] | 1389 | |
| 1390 | case CFGElement::TemporaryDtor: { |
Aaron Ballman | 35897d9 | 2014-04-28 14:56:59 +0000 | [diff] [blame] | 1391 | const CFGTemporaryDtor &DTor = B.castAs<CFGTemporaryDtor>(); |
DeLesley Hutchins | fbdee4e | 2013-10-11 21:55:33 +0000 | [diff] [blame] | 1392 | const CXXBindTemporaryExpr *BTE = DTor.getBindTemporaryExpr(); |
DeLesley Hutchins | fbdee4e | 2013-10-11 21:55:33 +0000 | [diff] [blame] | 1393 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 1394 | Visitor.checkCallability(PropagationInfo(BTE), |
| 1395 | DTor.getDestructorDecl(AC.getASTContext()), |
| 1396 | BTE->getExprLoc()); |
Manuel Klimek | b33bded | 2014-05-08 11:50:00 +0000 | [diff] [blame] | 1397 | CurrStates->remove(BTE); |
DeLesley Hutchins | fbdee4e | 2013-10-11 21:55:33 +0000 | [diff] [blame] | 1398 | break; |
| 1399 | } |
| 1400 | |
| 1401 | case CFGElement::AutomaticObjectDtor: { |
Aaron Ballman | 35897d9 | 2014-04-28 14:56:59 +0000 | [diff] [blame] | 1402 | const CFGAutomaticObjDtor &DTor = B.castAs<CFGAutomaticObjDtor>(); |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 1403 | SourceLocation Loc = DTor.getTriggerStmt()->getLocEnd(); |
DeLesley Hutchins | fbdee4e | 2013-10-11 21:55:33 +0000 | [diff] [blame] | 1404 | const VarDecl *Var = DTor.getVarDecl(); |
DeLesley Hutchins | fbdee4e | 2013-10-11 21:55:33 +0000 | [diff] [blame] | 1405 | |
DeLesley Hutchins | 68cc3f1 | 2013-11-16 00:22:43 +0000 | [diff] [blame] | 1406 | Visitor.checkCallability(PropagationInfo(Var), |
| 1407 | DTor.getDestructorDecl(AC.getASTContext()), |
| 1408 | Loc); |
DeLesley Hutchins | fbdee4e | 2013-10-11 21:55:33 +0000 | [diff] [blame] | 1409 | break; |
| 1410 | } |
| 1411 | |
DeLesley Hutchins | c2ecf0d | 2013-08-22 20:44:47 +0000 | [diff] [blame] | 1412 | default: |
| 1413 | break; |
| 1414 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1417 | // TODO: Handle other forms of branching with precision, including while- |
| 1418 | // and for-loops. (Deferred) |
| 1419 | if (!splitState(CurrBlock, Visitor)) { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1420 | CurrStates->setSource(nullptr); |
| 1421 | |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1422 | if (CurrBlock->succ_size() > 1 || |
| 1423 | (CurrBlock->succ_size() == 1 && |
| 1424 | (*CurrBlock->succ_begin())->pred_size() > 1)) { |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1425 | |
| 1426 | auto *RawState = CurrStates.get(); |
| 1427 | |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1428 | for (CFGBlock::const_succ_iterator SI = CurrBlock->succ_begin(), |
| 1429 | SE = CurrBlock->succ_end(); SI != SE; ++SI) { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1430 | |
| 1431 | if (*SI == nullptr) continue; |
| 1432 | |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1433 | if (BlockInfo.isBackEdge(CurrBlock, *SI)) { |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1434 | BlockInfo.borrowInfo(*SI)->intersectAtLoopHead( |
| 1435 | *SI, CurrBlock, RawState, WarningsHandler); |
| 1436 | |
DeLesley Hutchins | 773ba37 | 2015-04-15 22:32:44 +0000 | [diff] [blame] | 1437 | if (BlockInfo.allBackEdgesVisited(CurrBlock, *SI)) |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1438 | BlockInfo.discardInfo(*SI); |
| 1439 | } else { |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1440 | BlockInfo.addInfo(*SI, RawState, CurrStates); |
DeLesley Hutchins | 3277a61 | 2013-10-09 18:30:24 +0000 | [diff] [blame] | 1441 | } |
DeLesley Hutchins | 5533ec5 | 2013-08-29 17:26:57 +0000 | [diff] [blame] | 1442 | } |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 1443 | |
| 1444 | CurrStates = nullptr; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1445 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1446 | } |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1447 | |
| 1448 | if (CurrBlock == &AC.getCFG()->getExit() && |
| 1449 | D->getCallResultType()->isVoidType()) |
| 1450 | CurrStates->checkParamsForReturnTypestate(D->getLocation(), |
| 1451 | WarningsHandler); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1452 | } // End of block iterator. |
| 1453 | |
| 1454 | // Delete the last existing state map. |
David Blaikie | 86d8cf3 | 2015-08-14 01:26:19 +0000 | [diff] [blame] | 1455 | CurrStates = nullptr; |
| 1456 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1457 | WarningsHandler.emitDiagnostics(); |
| 1458 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1459 | }} // end namespace clang::consumed |