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