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