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