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