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