Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 1 | //==- IdempotentOperationChecker.cpp - Idempotent Operations ----*- C++ -*-==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a set of path-sensitive checks for idempotent and/or |
| 11 | // tautological operations. Each potential operation is checked along all paths |
| 12 | // to see if every path results in a pointless operation. |
| 13 | // +-------------------------------------------+ |
| 14 | // |Table of idempotent/tautological operations| |
| 15 | // +-------------------------------------------+ |
| 16 | //+--------------------------------------------------------------------------+ |
| 17 | //|Operator | x op x | x op 1 | 1 op x | x op 0 | 0 op x | x op ~0 | ~0 op x | |
| 18 | //+--------------------------------------------------------------------------+ |
| 19 | // +, += | | | | x | x | | |
| 20 | // -, -= | | | | x | -x | | |
| 21 | // *, *= | | x | x | 0 | 0 | | |
| 22 | // /, /= | 1 | x | | N/A | 0 | | |
| 23 | // &, &= | x | | | 0 | 0 | x | x |
| 24 | // |, |= | x | | | x | x | ~0 | ~0 |
| 25 | // ^, ^= | 0 | | | x | x | | |
| 26 | // <<, <<= | | | | x | 0 | | |
| 27 | // >>, >>= | | | | x | 0 | | |
| 28 | // || | 1 | 1 | 1 | x | x | 1 | 1 |
| 29 | // && | 1 | x | x | 0 | 0 | x | x |
| 30 | // = | x | | | | | | |
| 31 | // == | 1 | | | | | | |
| 32 | // >= | 1 | | | | | | |
| 33 | // <= | 1 | | | | | | |
| 34 | // > | 0 | | | | | | |
| 35 | // < | 0 | | | | | | |
| 36 | // != | 0 | | | | | | |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | // |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 39 | // Things TODO: |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 40 | // - Improved error messages |
| 41 | // - Handle mixed assumptions (which assumptions can belong together?) |
| 42 | // - Finer grained false positive control (levels) |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 43 | // - Handling ~0 values |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 44 | |
Argyrios Kyrtzidis | c9f2e0f | 2011-02-15 22:55:14 +0000 | [diff] [blame] | 45 | #include "ClangSACheckers.h" |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 46 | #include "clang/Analysis/CFGStmtMap.h" |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 47 | #include "clang/Analysis/Analyses/PseudoConstantAnalysis.h" |
Ted Kremenek | 42461ee | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 48 | #include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 49 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 695fb50 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 50 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Argyrios Kyrtzidis | ecc4d33 | 2011-02-23 21:04:44 +0000 | [diff] [blame] | 51 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 52 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
| 53 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
| 54 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 55 | #include "clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h" |
| 56 | #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 57 | #include "clang/AST/Stmt.h" |
| 58 | #include "llvm/ADT/DenseMap.h" |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 59 | #include "llvm/ADT/SmallSet.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 60 | #include "llvm/ADT/SmallString.h" |
Ted Kremenek | 8e37677 | 2011-02-14 17:59:20 +0000 | [diff] [blame] | 61 | #include "llvm/ADT/BitVector.h" |
Chandler Carruth | 256565b | 2010-07-07 00:07:37 +0000 | [diff] [blame] | 62 | #include "llvm/Support/ErrorHandling.h" |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 63 | |
| 64 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 65 | using namespace ento; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 66 | |
| 67 | namespace { |
| 68 | class IdempotentOperationChecker |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 69 | : public Checker<check::PreStmt<BinaryOperator>, |
Argyrios Kyrtzidis | ecc4d33 | 2011-02-23 21:04:44 +0000 | [diff] [blame] | 70 | check::PostStmt<BinaryOperator>, |
| 71 | check::EndAnalysis> { |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 72 | public: |
Argyrios Kyrtzidis | ecc4d33 | 2011-02-23 21:04:44 +0000 | [diff] [blame] | 73 | void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const; |
| 74 | void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const; |
| 75 | void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const; |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 76 | |
| 77 | private: |
| 78 | // Our assumption about a particular operation. |
| 79 | enum Assumption { Possible = 0, Impossible, Equal, LHSis1, RHSis1, LHSis0, |
| 80 | RHSis0 }; |
| 81 | |
Argyrios Kyrtzidis | ecc4d33 | 2011-02-23 21:04:44 +0000 | [diff] [blame] | 82 | static void UpdateAssumption(Assumption &A, const Assumption &New); |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 83 | |
| 84 | // False positive reduction methods |
| 85 | static bool isSelfAssign(const Expr *LHS, const Expr *RHS); |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 86 | static bool isUnused(const Expr *E, AnalysisDeclContext *AC); |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 87 | static bool isTruncationExtensionAssignment(const Expr *LHS, |
| 88 | const Expr *RHS); |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 89 | static bool pathWasCompletelyAnalyzed(AnalysisDeclContext *AC, |
Argyrios Kyrtzidis | ecc4d33 | 2011-02-23 21:04:44 +0000 | [diff] [blame] | 90 | const CFGBlock *CB, |
| 91 | const CoreEngine &CE); |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 92 | static bool CanVary(const Expr *Ex, |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 93 | AnalysisDeclContext *AC); |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 94 | static bool isConstantOrPseudoConstant(const DeclRefExpr *DR, |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 95 | AnalysisDeclContext *AC); |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 96 | static bool containsNonLocalVarDecl(const Stmt *S); |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 97 | |
| 98 | // Hash table and related data structures |
| 99 | struct BinaryOperatorData { |
Anna Zaks | 3381a73 | 2011-10-03 23:07:13 +0000 | [diff] [blame] | 100 | BinaryOperatorData() : assumption(Possible) {} |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 101 | |
| 102 | Assumption assumption; |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 103 | ExplodedNodeSet explodedNodes; // Set of ExplodedNodes that refer to a |
| 104 | // BinaryOperator |
| 105 | }; |
| 106 | typedef llvm::DenseMap<const BinaryOperator *, BinaryOperatorData> |
| 107 | AssumptionMap; |
Argyrios Kyrtzidis | ecc4d33 | 2011-02-23 21:04:44 +0000 | [diff] [blame] | 108 | mutable AssumptionMap hash; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 109 | }; |
| 110 | } |
| 111 | |
Argyrios Kyrtzidis | ecc4d33 | 2011-02-23 21:04:44 +0000 | [diff] [blame] | 112 | void IdempotentOperationChecker::checkPreStmt(const BinaryOperator *B, |
| 113 | CheckerContext &C) const { |
Ted Kremenek | fe97fa1 | 2010-08-02 20:33:02 +0000 | [diff] [blame] | 114 | // Find or create an entry in the hash for this BinaryOperator instance. |
| 115 | // If we haven't done a lookup before, it will get default initialized to |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 116 | // 'Possible'. At this stage we do not store the ExplodedNode, as it has not |
| 117 | // been created yet. |
| 118 | BinaryOperatorData &Data = hash[B]; |
| 119 | Assumption &A = Data.assumption; |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 120 | AnalysisDeclContext *AC = C.getCurrentAnalysisDeclContext(); |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 121 | |
| 122 | // If we already have visited this node on a path that does not contain an |
| 123 | // idempotent operation, return immediately. |
| 124 | if (A == Impossible) |
| 125 | return; |
| 126 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 127 | // Retrieve both sides of the operator and determine if they can vary (which |
| 128 | // may mean this is a false positive. |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 129 | const Expr *LHS = B->getLHS(); |
| 130 | const Expr *RHS = B->getRHS(); |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 131 | |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 132 | // At this stage we can calculate whether each side contains a false positive |
| 133 | // that applies to all operators. We only need to calculate this the first |
| 134 | // time. |
| 135 | bool LHSContainsFalsePositive = false, RHSContainsFalsePositive = false; |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 136 | if (A == Possible) { |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 137 | // An expression contains a false positive if it can't vary, or if it |
| 138 | // contains a known false positive VarDecl. |
| 139 | LHSContainsFalsePositive = !CanVary(LHS, AC) |
| 140 | || containsNonLocalVarDecl(LHS); |
| 141 | RHSContainsFalsePositive = !CanVary(RHS, AC) |
| 142 | || containsNonLocalVarDecl(RHS); |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 143 | } |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 145 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 146 | const LocationContext *LCtx = C.getLocationContext(); |
| 147 | SVal LHSVal = state->getSVal(LHS, LCtx); |
| 148 | SVal RHSVal = state->getSVal(RHS, LCtx); |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 149 | |
| 150 | // If either value is unknown, we can't be 100% sure of all paths. |
| 151 | if (LHSVal.isUnknownOrUndef() || RHSVal.isUnknownOrUndef()) { |
| 152 | A = Impossible; |
| 153 | return; |
| 154 | } |
| 155 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 156 | |
| 157 | // Dereference the LHS SVal if this is an assign operation |
| 158 | switch (Op) { |
| 159 | default: |
| 160 | break; |
| 161 | |
| 162 | // Fall through intentional |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 163 | case BO_AddAssign: |
| 164 | case BO_SubAssign: |
| 165 | case BO_MulAssign: |
| 166 | case BO_DivAssign: |
| 167 | case BO_AndAssign: |
| 168 | case BO_OrAssign: |
| 169 | case BO_XorAssign: |
| 170 | case BO_ShlAssign: |
| 171 | case BO_ShrAssign: |
| 172 | case BO_Assign: |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 173 | // Assign statements have one extra level of indirection |
| 174 | if (!isa<Loc>(LHSVal)) { |
| 175 | A = Impossible; |
| 176 | return; |
| 177 | } |
Ted Kremenek | 96ebad6 | 2010-09-09 07:13:00 +0000 | [diff] [blame] | 178 | LHSVal = state->getSVal(cast<Loc>(LHSVal), LHS->getType()); |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | |
| 182 | // We now check for various cases which result in an idempotent operation. |
| 183 | |
| 184 | // x op x |
| 185 | switch (Op) { |
| 186 | default: |
| 187 | break; // We don't care about any other operators. |
| 188 | |
| 189 | // Fall through intentional |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 190 | case BO_Assign: |
Tom Care | 6216dc0 | 2010-08-30 19:25:43 +0000 | [diff] [blame] | 191 | // x Assign x can be used to silence unused variable warnings intentionally. |
| 192 | // If this is a self assignment and the variable is referenced elsewhere, |
Tom Care | 84c24ed | 2010-09-07 20:27:56 +0000 | [diff] [blame] | 193 | // and the assignment is not a truncation or extension, then it is a false |
| 194 | // positive. |
Tom Care | 6216dc0 | 2010-08-30 19:25:43 +0000 | [diff] [blame] | 195 | if (isSelfAssign(LHS, RHS)) { |
Tom Care | 84c24ed | 2010-09-07 20:27:56 +0000 | [diff] [blame] | 196 | if (!isUnused(LHS, AC) && !isTruncationExtensionAssignment(LHS, RHS)) { |
Tom Care | 6216dc0 | 2010-08-30 19:25:43 +0000 | [diff] [blame] | 197 | UpdateAssumption(A, Equal); |
| 198 | return; |
| 199 | } |
| 200 | else { |
| 201 | A = Impossible; |
| 202 | return; |
| 203 | } |
Tom Care | df4ca42 | 2010-07-16 20:41:41 +0000 | [diff] [blame] | 204 | } |
| 205 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 206 | case BO_SubAssign: |
| 207 | case BO_DivAssign: |
| 208 | case BO_AndAssign: |
| 209 | case BO_OrAssign: |
| 210 | case BO_XorAssign: |
| 211 | case BO_Sub: |
| 212 | case BO_Div: |
| 213 | case BO_And: |
| 214 | case BO_Or: |
| 215 | case BO_Xor: |
| 216 | case BO_LOr: |
| 217 | case BO_LAnd: |
Tom Care | 9edd4d0 | 2010-08-27 22:50:47 +0000 | [diff] [blame] | 218 | case BO_EQ: |
| 219 | case BO_NE: |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 220 | if (LHSVal != RHSVal || LHSContainsFalsePositive |
| 221 | || RHSContainsFalsePositive) |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 222 | break; |
| 223 | UpdateAssumption(A, Equal); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | // x op 1 |
| 228 | switch (Op) { |
| 229 | default: |
| 230 | break; // We don't care about any other operators. |
| 231 | |
| 232 | // Fall through intentional |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 233 | case BO_MulAssign: |
| 234 | case BO_DivAssign: |
| 235 | case BO_Mul: |
| 236 | case BO_Div: |
| 237 | case BO_LOr: |
| 238 | case BO_LAnd: |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 239 | if (!RHSVal.isConstant(1) || RHSContainsFalsePositive) |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 240 | break; |
| 241 | UpdateAssumption(A, RHSis1); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | // 1 op x |
| 246 | switch (Op) { |
| 247 | default: |
| 248 | break; // We don't care about any other operators. |
| 249 | |
| 250 | // Fall through intentional |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 251 | case BO_MulAssign: |
| 252 | case BO_Mul: |
| 253 | case BO_LOr: |
| 254 | case BO_LAnd: |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 255 | if (!LHSVal.isConstant(1) || LHSContainsFalsePositive) |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 256 | break; |
| 257 | UpdateAssumption(A, LHSis1); |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | // x op 0 |
| 262 | switch (Op) { |
| 263 | default: |
| 264 | break; // We don't care about any other operators. |
| 265 | |
| 266 | // Fall through intentional |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 267 | case BO_AddAssign: |
| 268 | case BO_SubAssign: |
| 269 | case BO_MulAssign: |
| 270 | case BO_AndAssign: |
| 271 | case BO_OrAssign: |
| 272 | case BO_XorAssign: |
| 273 | case BO_Add: |
| 274 | case BO_Sub: |
| 275 | case BO_Mul: |
| 276 | case BO_And: |
| 277 | case BO_Or: |
| 278 | case BO_Xor: |
| 279 | case BO_Shl: |
| 280 | case BO_Shr: |
| 281 | case BO_LOr: |
| 282 | case BO_LAnd: |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 283 | if (!RHSVal.isConstant(0) || RHSContainsFalsePositive) |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 284 | break; |
| 285 | UpdateAssumption(A, RHSis0); |
| 286 | return; |
| 287 | } |
| 288 | |
| 289 | // 0 op x |
| 290 | switch (Op) { |
| 291 | default: |
| 292 | break; // We don't care about any other operators. |
| 293 | |
| 294 | // Fall through intentional |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 295 | //case BO_AddAssign: // Common false positive |
| 296 | case BO_SubAssign: // Check only if unsigned |
| 297 | case BO_MulAssign: |
| 298 | case BO_DivAssign: |
| 299 | case BO_AndAssign: |
| 300 | //case BO_OrAssign: // Common false positive |
| 301 | //case BO_XorAssign: // Common false positive |
| 302 | case BO_ShlAssign: |
| 303 | case BO_ShrAssign: |
| 304 | case BO_Add: |
| 305 | case BO_Sub: |
| 306 | case BO_Mul: |
| 307 | case BO_Div: |
| 308 | case BO_And: |
| 309 | case BO_Or: |
| 310 | case BO_Xor: |
| 311 | case BO_Shl: |
| 312 | case BO_Shr: |
| 313 | case BO_LOr: |
| 314 | case BO_LAnd: |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 315 | if (!LHSVal.isConstant(0) || LHSContainsFalsePositive) |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 316 | break; |
| 317 | UpdateAssumption(A, LHSis0); |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | // If we get to this point, there has been a valid use of this operation. |
| 322 | A = Impossible; |
| 323 | } |
| 324 | |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 325 | // At the post visit stage, the predecessor ExplodedNode will be the |
| 326 | // BinaryOperator that was just created. We use this hook to collect the |
| 327 | // ExplodedNode. |
Argyrios Kyrtzidis | ecc4d33 | 2011-02-23 21:04:44 +0000 | [diff] [blame] | 328 | void IdempotentOperationChecker::checkPostStmt(const BinaryOperator *B, |
| 329 | CheckerContext &C) const { |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 330 | // Add the ExplodedNode we just visited |
| 331 | BinaryOperatorData &Data = hash[B]; |
Ted Kremenek | 020c374 | 2011-02-12 18:50:03 +0000 | [diff] [blame] | 332 | |
| 333 | const Stmt *predStmt |
| 334 | = cast<StmtPoint>(C.getPredecessor()->getLocation()).getStmt(); |
| 335 | |
| 336 | // Ignore implicit calls to setters. |
Ted Kremenek | cf995d3 | 2011-03-15 19:27:57 +0000 | [diff] [blame] | 337 | if (!isa<BinaryOperator>(predStmt)) |
Ted Kremenek | 020c374 | 2011-02-12 18:50:03 +0000 | [diff] [blame] | 338 | return; |
Ted Kremenek | cf995d3 | 2011-03-15 19:27:57 +0000 | [diff] [blame] | 339 | |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 340 | Data.explodedNodes.Add(C.getPredecessor()); |
| 341 | } |
| 342 | |
Argyrios Kyrtzidis | ecc4d33 | 2011-02-23 21:04:44 +0000 | [diff] [blame] | 343 | void IdempotentOperationChecker::checkEndAnalysis(ExplodedGraph &G, |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 344 | BugReporter &BR, |
Argyrios Kyrtzidis | ecc4d33 | 2011-02-23 21:04:44 +0000 | [diff] [blame] | 345 | ExprEngine &Eng) const { |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 346 | BugType *BT = new BugType("Idempotent operation", "Dead code"); |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 347 | // Iterate over the hash to see if we have any paths with definite |
| 348 | // idempotent operations. |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 349 | for (AssumptionMap::const_iterator i = hash.begin(); i != hash.end(); ++i) { |
| 350 | // Unpack the hash contents |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 351 | const BinaryOperatorData &Data = i->second; |
| 352 | const Assumption &A = Data.assumption; |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 353 | const ExplodedNodeSet &ES = Data.explodedNodes; |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 354 | |
Anna Zaks | 3381a73 | 2011-10-03 23:07:13 +0000 | [diff] [blame] | 355 | // If there are no nodes accosted with the expression, nothing to report. |
| 356 | // FIXME: This is possible because the checker does part of processing in |
| 357 | // checkPreStmt and part in checkPostStmt. |
| 358 | if (ES.begin() == ES.end()) |
| 359 | continue; |
| 360 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 361 | const BinaryOperator *B = i->first; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 362 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 363 | if (A == Impossible) |
| 364 | continue; |
| 365 | |
| 366 | // If the analyzer did not finish, check to see if we can still emit this |
| 367 | // warning |
| 368 | if (Eng.hasWorkRemaining()) { |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 369 | // If we can trace back |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 370 | AnalysisDeclContext *AC = (*ES.begin())->getLocationContext() |
| 371 | ->getAnalysisDeclContext(); |
Ted Kremenek | 42461ee | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 372 | if (!pathWasCompletelyAnalyzed(AC, |
| 373 | AC->getCFGStmtMap()->getBlock(B), |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 374 | Eng.getCoreEngine())) |
| 375 | continue; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 376 | } |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 377 | |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 378 | // Select the error message and SourceRanges to report. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 379 | SmallString<128> buf; |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 380 | llvm::raw_svector_ostream os(buf); |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 381 | bool LHSRelevant = false, RHSRelevant = false; |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 382 | switch (A) { |
| 383 | case Equal: |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 384 | LHSRelevant = true; |
| 385 | RHSRelevant = true; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 386 | if (B->getOpcode() == BO_Assign) |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 387 | os << "Assigned value is always the same as the existing value"; |
| 388 | else |
| 389 | os << "Both operands to '" << B->getOpcodeStr() |
| 390 | << "' always have the same value"; |
| 391 | break; |
| 392 | case LHSis1: |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 393 | LHSRelevant = true; |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 394 | os << "The left operand to '" << B->getOpcodeStr() << "' is always 1"; |
| 395 | break; |
| 396 | case RHSis1: |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 397 | RHSRelevant = true; |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 398 | os << "The right operand to '" << B->getOpcodeStr() << "' is always 1"; |
| 399 | break; |
| 400 | case LHSis0: |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 401 | LHSRelevant = true; |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 402 | os << "The left operand to '" << B->getOpcodeStr() << "' is always 0"; |
| 403 | break; |
| 404 | case RHSis0: |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 405 | RHSRelevant = true; |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 406 | os << "The right operand to '" << B->getOpcodeStr() << "' is always 0"; |
| 407 | break; |
| 408 | case Possible: |
| 409 | llvm_unreachable("Operation was never marked with an assumption"); |
| 410 | case Impossible: |
| 411 | llvm_unreachable(0); |
| 412 | } |
| 413 | |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 414 | // Add a report for each ExplodedNode |
| 415 | for (ExplodedNodeSet::iterator I = ES.begin(), E = ES.end(); I != E; ++I) { |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 416 | BugReport *report = new BugReport(*BT, os.str(), *I); |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 417 | |
| 418 | // Add source ranges and visitor hooks |
| 419 | if (LHSRelevant) { |
| 420 | const Expr *LHS = i->first->getLHS(); |
| 421 | report->addRange(LHS->getSourceRange()); |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 422 | FindLastStoreBRVisitor::registerStatementVarDecls(*report, LHS); |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 423 | } |
| 424 | if (RHSRelevant) { |
| 425 | const Expr *RHS = i->first->getRHS(); |
| 426 | report->addRange(i->first->getRHS()->getSourceRange()); |
Anna Zaks | 50bbc16 | 2011-08-19 22:33:38 +0000 | [diff] [blame] | 427 | FindLastStoreBRVisitor::registerStatementVarDecls(*report, RHS); |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | BR.EmitReport(report); |
| 431 | } |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 432 | } |
Argyrios Kyrtzidis | ecc4d33 | 2011-02-23 21:04:44 +0000 | [diff] [blame] | 433 | |
| 434 | hash.clear(); |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | // Updates the current assumption given the new assumption |
| 438 | inline void IdempotentOperationChecker::UpdateAssumption(Assumption &A, |
| 439 | const Assumption &New) { |
Tom Care | d8421ed | 2010-08-27 22:35:28 +0000 | [diff] [blame] | 440 | // If the assumption is the same, there is nothing to do |
| 441 | if (A == New) |
| 442 | return; |
| 443 | |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 444 | switch (A) { |
| 445 | // If we don't currently have an assumption, set it |
| 446 | case Possible: |
| 447 | A = New; |
| 448 | return; |
| 449 | |
| 450 | // If we have determined that a valid state happened, ignore the new |
| 451 | // assumption. |
| 452 | case Impossible: |
| 453 | return; |
| 454 | |
| 455 | // Any other case means that we had a different assumption last time. We don't |
| 456 | // currently support mixing assumptions for diagnostic reasons, so we set |
| 457 | // our assumption to be impossible. |
| 458 | default: |
| 459 | A = Impossible; |
| 460 | return; |
| 461 | } |
| 462 | } |
| 463 | |
Tom Care | 6216dc0 | 2010-08-30 19:25:43 +0000 | [diff] [blame] | 464 | // Check for a statement where a variable is self assigned to possibly avoid an |
| 465 | // unused variable warning. |
| 466 | bool IdempotentOperationChecker::isSelfAssign(const Expr *LHS, const Expr *RHS) { |
Tom Care | df4ca42 | 2010-07-16 20:41:41 +0000 | [diff] [blame] | 467 | LHS = LHS->IgnoreParenCasts(); |
| 468 | RHS = RHS->IgnoreParenCasts(); |
| 469 | |
| 470 | const DeclRefExpr *LHS_DR = dyn_cast<DeclRefExpr>(LHS); |
| 471 | if (!LHS_DR) |
| 472 | return false; |
| 473 | |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 474 | const VarDecl *VD = dyn_cast<VarDecl>(LHS_DR->getDecl()); |
| 475 | if (!VD) |
Tom Care | df4ca42 | 2010-07-16 20:41:41 +0000 | [diff] [blame] | 476 | return false; |
| 477 | |
| 478 | const DeclRefExpr *RHS_DR = dyn_cast<DeclRefExpr>(RHS); |
| 479 | if (!RHS_DR) |
| 480 | return false; |
| 481 | |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 482 | if (VD != RHS_DR->getDecl()) |
| 483 | return false; |
| 484 | |
Tom Care | 6216dc0 | 2010-08-30 19:25:43 +0000 | [diff] [blame] | 485 | return true; |
| 486 | } |
| 487 | |
| 488 | // Returns true if the Expr points to a VarDecl that is not read anywhere |
| 489 | // outside of self-assignments. |
| 490 | bool IdempotentOperationChecker::isUnused(const Expr *E, |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 491 | AnalysisDeclContext *AC) { |
Tom Care | 6216dc0 | 2010-08-30 19:25:43 +0000 | [diff] [blame] | 492 | if (!E) |
| 493 | return false; |
| 494 | |
| 495 | const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); |
| 496 | if (!DR) |
| 497 | return false; |
| 498 | |
| 499 | const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 500 | if (!VD) |
| 501 | return false; |
| 502 | |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 503 | if (AC->getPseudoConstantAnalysis()->wasReferenced(VD)) |
| 504 | return false; |
| 505 | |
| 506 | return true; |
Tom Care | df4ca42 | 2010-07-16 20:41:41 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | // Check for self casts truncating/extending a variable |
| 510 | bool IdempotentOperationChecker::isTruncationExtensionAssignment( |
| 511 | const Expr *LHS, |
| 512 | const Expr *RHS) { |
| 513 | |
| 514 | const DeclRefExpr *LHS_DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParenCasts()); |
| 515 | if (!LHS_DR) |
| 516 | return false; |
| 517 | |
| 518 | const VarDecl *VD = dyn_cast<VarDecl>(LHS_DR->getDecl()); |
| 519 | if (!VD) |
| 520 | return false; |
| 521 | |
| 522 | const DeclRefExpr *RHS_DR = dyn_cast<DeclRefExpr>(RHS->IgnoreParenCasts()); |
| 523 | if (!RHS_DR) |
| 524 | return false; |
| 525 | |
| 526 | if (VD != RHS_DR->getDecl()) |
| 527 | return false; |
| 528 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 529 | return dyn_cast<DeclRefExpr>(RHS->IgnoreParenLValueCasts()) == NULL; |
Tom Care | df4ca42 | 2010-07-16 20:41:41 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 532 | // Returns false if a path to this block was not completely analyzed, or true |
| 533 | // otherwise. |
Ted Kremenek | 8e37677 | 2011-02-14 17:59:20 +0000 | [diff] [blame] | 534 | bool |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 535 | IdempotentOperationChecker::pathWasCompletelyAnalyzed(AnalysisDeclContext *AC, |
Ted Kremenek | 8e37677 | 2011-02-14 17:59:20 +0000 | [diff] [blame] | 536 | const CFGBlock *CB, |
Ted Kremenek | 8e37677 | 2011-02-14 17:59:20 +0000 | [diff] [blame] | 537 | const CoreEngine &CE) { |
Ted Kremenek | b531891 | 2011-02-15 02:20:03 +0000 | [diff] [blame] | 538 | |
Ted Kremenek | af13d5b | 2011-03-19 01:00:33 +0000 | [diff] [blame] | 539 | CFGReverseBlockReachabilityAnalysis *CRA = AC->getCFGReachablityAnalysis(); |
Ted Kremenek | 8e37677 | 2011-02-14 17:59:20 +0000 | [diff] [blame] | 540 | |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 541 | // Test for reachability from any aborted blocks to this block |
Ted Kremenek | 422ab7a | 2011-04-02 02:56:23 +0000 | [diff] [blame] | 542 | typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator; |
| 543 | for (ExhaustedIterator I = CE.blocks_exhausted_begin(), |
| 544 | E = CE.blocks_exhausted_end(); I != E; ++I) { |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 545 | const BlockEdge &BE = I->first; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 546 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 547 | // The destination block on the BlockEdge is the first block that was not |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 548 | // analyzed. If we can reach this block from the aborted block, then this |
| 549 | // block was not completely analyzed. |
Ted Kremenek | e8350c6 | 2011-02-14 17:59:23 +0000 | [diff] [blame] | 550 | // |
| 551 | // Also explicitly check if the current block is the destination block. |
| 552 | // While technically reachable, it means we aborted the analysis on |
| 553 | // a path that included that block. |
| 554 | const CFGBlock *destBlock = BE.getDst(); |
| 555 | if (destBlock == CB || CRA->isReachable(destBlock, CB)) |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 556 | return false; |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 557 | } |
Ted Kremenek | 422ab7a | 2011-04-02 02:56:23 +0000 | [diff] [blame] | 558 | |
| 559 | // Test for reachability from blocks we just gave up on. |
| 560 | typedef CoreEngine::BlocksAborted::const_iterator AbortedIterator; |
| 561 | for (AbortedIterator I = CE.blocks_aborted_begin(), |
| 562 | E = CE.blocks_aborted_end(); I != E; ++I) { |
| 563 | const CFGBlock *destBlock = I->first; |
| 564 | if (destBlock == CB || CRA->isReachable(destBlock, CB)) |
| 565 | return false; |
| 566 | } |
Ted Kremenek | 33d4626 | 2010-11-13 05:04:52 +0000 | [diff] [blame] | 567 | |
| 568 | // For the items still on the worklist, see if they are in blocks that |
| 569 | // can eventually reach 'CB'. |
Argyrios Kyrtzidis | d2592a3 | 2010-12-22 18:53:44 +0000 | [diff] [blame] | 570 | class VisitWL : public WorkList::Visitor { |
Ted Kremenek | 33d4626 | 2010-11-13 05:04:52 +0000 | [diff] [blame] | 571 | const CFGStmtMap *CBM; |
| 572 | const CFGBlock *TargetBlock; |
Ted Kremenek | af13d5b | 2011-03-19 01:00:33 +0000 | [diff] [blame] | 573 | CFGReverseBlockReachabilityAnalysis &CRA; |
Ted Kremenek | 33d4626 | 2010-11-13 05:04:52 +0000 | [diff] [blame] | 574 | public: |
| 575 | VisitWL(const CFGStmtMap *cbm, const CFGBlock *targetBlock, |
Ted Kremenek | af13d5b | 2011-03-19 01:00:33 +0000 | [diff] [blame] | 576 | CFGReverseBlockReachabilityAnalysis &cra) |
Ted Kremenek | 33d4626 | 2010-11-13 05:04:52 +0000 | [diff] [blame] | 577 | : CBM(cbm), TargetBlock(targetBlock), CRA(cra) {} |
Ted Kremenek | 55825aa | 2011-01-11 02:34:50 +0000 | [diff] [blame] | 578 | virtual bool visit(const WorkListUnit &U) { |
Ted Kremenek | 33d4626 | 2010-11-13 05:04:52 +0000 | [diff] [blame] | 579 | ProgramPoint P = U.getNode()->getLocation(); |
| 580 | const CFGBlock *B = 0; |
| 581 | if (StmtPoint *SP = dyn_cast<StmtPoint>(&P)) { |
| 582 | B = CBM->getBlock(SP->getStmt()); |
| 583 | } |
Ted Kremenek | ed02366 | 2010-11-13 05:12:26 +0000 | [diff] [blame] | 584 | else if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
| 585 | B = BE->getDst(); |
| 586 | } |
| 587 | else if (BlockEntrance *BEnt = dyn_cast<BlockEntrance>(&P)) { |
| 588 | B = BEnt->getBlock(); |
| 589 | } |
| 590 | else if (BlockExit *BExit = dyn_cast<BlockExit>(&P)) { |
| 591 | B = BExit->getBlock(); |
| 592 | } |
Ted Kremenek | 33d4626 | 2010-11-13 05:04:52 +0000 | [diff] [blame] | 593 | if (!B) |
| 594 | return true; |
| 595 | |
Ted Kremenek | 1212f80 | 2011-04-12 21:47:02 +0000 | [diff] [blame] | 596 | return B == TargetBlock || CRA.isReachable(B, TargetBlock); |
Ted Kremenek | 33d4626 | 2010-11-13 05:04:52 +0000 | [diff] [blame] | 597 | } |
| 598 | }; |
Ted Kremenek | 42461ee | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 599 | VisitWL visitWL(AC->getCFGStmtMap(), CB, *CRA); |
Ted Kremenek | 33d4626 | 2010-11-13 05:04:52 +0000 | [diff] [blame] | 600 | // Were there any items in the worklist that could potentially reach |
| 601 | // this block? |
Ted Kremenek | 55825aa | 2011-01-11 02:34:50 +0000 | [diff] [blame] | 602 | if (CE.getWorkList()->visitItemsInWorkList(visitWL)) |
Ted Kremenek | 33d4626 | 2010-11-13 05:04:52 +0000 | [diff] [blame] | 603 | return false; |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 604 | |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 605 | // Verify that this block is reachable from the entry block |
Ted Kremenek | 42461ee | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 606 | if (!CRA->isReachable(&AC->getCFG()->getEntry(), CB)) |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 607 | return false; |
| 608 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 609 | // If we get to this point, there is no connection to the entry block or an |
| 610 | // aborted block. This path is unreachable and we can report the error. |
| 611 | return true; |
| 612 | } |
| 613 | |
| 614 | // Recursive function that determines whether an expression contains any element |
| 615 | // that varies. This could be due to a compile-time constant like sizeof. An |
| 616 | // expression may also involve a variable that behaves like a constant. The |
| 617 | // function returns true if the expression varies, and false otherwise. |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 618 | bool IdempotentOperationChecker::CanVary(const Expr *Ex, |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 619 | AnalysisDeclContext *AC) { |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 620 | // Parentheses and casts are irrelevant here |
| 621 | Ex = Ex->IgnoreParenCasts(); |
| 622 | |
| 623 | if (Ex->getLocStart().isMacroID()) |
| 624 | return false; |
| 625 | |
| 626 | switch (Ex->getStmtClass()) { |
| 627 | // Trivially true cases |
| 628 | case Stmt::ArraySubscriptExprClass: |
| 629 | case Stmt::MemberExprClass: |
| 630 | case Stmt::StmtExprClass: |
| 631 | case Stmt::CallExprClass: |
| 632 | case Stmt::VAArgExprClass: |
| 633 | case Stmt::ShuffleVectorExprClass: |
| 634 | return true; |
| 635 | default: |
| 636 | return true; |
| 637 | |
| 638 | // Trivially false cases |
| 639 | case Stmt::IntegerLiteralClass: |
| 640 | case Stmt::CharacterLiteralClass: |
| 641 | case Stmt::FloatingLiteralClass: |
| 642 | case Stmt::PredefinedExprClass: |
| 643 | case Stmt::ImaginaryLiteralClass: |
| 644 | case Stmt::StringLiteralClass: |
| 645 | case Stmt::OffsetOfExprClass: |
| 646 | case Stmt::CompoundLiteralExprClass: |
| 647 | case Stmt::AddrLabelExprClass: |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 648 | case Stmt::BinaryTypeTraitExprClass: |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 649 | case Stmt::GNUNullExprClass: |
| 650 | case Stmt::InitListExprClass: |
| 651 | case Stmt::DesignatedInitExprClass: |
| 652 | case Stmt::BlockExprClass: |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 653 | return false; |
| 654 | |
| 655 | // Cases requiring custom logic |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 656 | case Stmt::UnaryExprOrTypeTraitExprClass: { |
| 657 | const UnaryExprOrTypeTraitExpr *SE = |
| 658 | cast<const UnaryExprOrTypeTraitExpr>(Ex); |
| 659 | if (SE->getKind() != UETT_SizeOf) |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 660 | return false; |
| 661 | return SE->getTypeOfArgument()->isVariableArrayType(); |
| 662 | } |
| 663 | case Stmt::DeclRefExprClass: |
Tom Care | 6216dc0 | 2010-08-30 19:25:43 +0000 | [diff] [blame] | 664 | // Check for constants/pseudoconstants |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 665 | return !isConstantOrPseudoConstant(cast<DeclRefExpr>(Ex), AC); |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 666 | |
| 667 | // The next cases require recursion for subexpressions |
| 668 | case Stmt::BinaryOperatorClass: { |
| 669 | const BinaryOperator *B = cast<const BinaryOperator>(Ex); |
Ted Kremenek | 74faec2 | 2010-10-29 01:06:54 +0000 | [diff] [blame] | 670 | |
| 671 | // Exclude cases involving pointer arithmetic. These are usually |
| 672 | // false positives. |
| 673 | if (B->getOpcode() == BO_Sub || B->getOpcode() == BO_Add) |
| 674 | if (B->getLHS()->getType()->getAs<PointerType>()) |
| 675 | return false; |
| 676 | |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 677 | return CanVary(B->getRHS(), AC) |
| 678 | || CanVary(B->getLHS(), AC); |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 679 | } |
| 680 | case Stmt::UnaryOperatorClass: { |
| 681 | const UnaryOperator *U = cast<const UnaryOperator>(Ex); |
Eli Friedman | de7e662 | 2010-08-13 01:36:11 +0000 | [diff] [blame] | 682 | // Handle trivial case first |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 683 | switch (U->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 684 | case UO_Extension: |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 685 | return false; |
| 686 | default: |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 687 | return CanVary(U->getSubExpr(), AC); |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | case Stmt::ChooseExprClass: |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 691 | return CanVary(cast<const ChooseExpr>(Ex)->getChosenSubExpr( |
| 692 | AC->getASTContext()), AC); |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 693 | case Stmt::ConditionalOperatorClass: |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 694 | case Stmt::BinaryConditionalOperatorClass: |
| 695 | return CanVary(cast<AbstractConditionalOperator>(Ex)->getCond(), AC); |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 696 | } |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 699 | // Returns true if a DeclRefExpr is or behaves like a constant. |
| 700 | bool IdempotentOperationChecker::isConstantOrPseudoConstant( |
Tom Care | 6216dc0 | 2010-08-30 19:25:43 +0000 | [diff] [blame] | 701 | const DeclRefExpr *DR, |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 702 | AnalysisDeclContext *AC) { |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 703 | // Check if the type of the Decl is const-qualified |
| 704 | if (DR->getType().isConstQualified()) |
| 705 | return true; |
| 706 | |
Tom Care | 50e8ac2 | 2010-08-16 21:43:52 +0000 | [diff] [blame] | 707 | // Check for an enum |
| 708 | if (isa<EnumConstantDecl>(DR->getDecl())) |
| 709 | return true; |
| 710 | |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 711 | const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 712 | if (!VD) |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 713 | return true; |
| 714 | |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 715 | // Check if the Decl behaves like a constant. This check also takes care of |
| 716 | // static variables, which can only change between function calls if they are |
| 717 | // modified in the AST. |
| 718 | PseudoConstantAnalysis *PCA = AC->getPseudoConstantAnalysis(); |
| 719 | if (PCA->isPseudoConstant(VD)) |
| 720 | return true; |
| 721 | |
| 722 | return false; |
| 723 | } |
| 724 | |
| 725 | // Recursively find any substatements containing VarDecl's with storage other |
| 726 | // than local |
| 727 | bool IdempotentOperationChecker::containsNonLocalVarDecl(const Stmt *S) { |
| 728 | const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S); |
| 729 | |
| 730 | if (DR) |
| 731 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) |
| 732 | if (!VD->hasLocalStorage()) |
| 733 | return true; |
| 734 | |
| 735 | for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end(); |
| 736 | ++I) |
| 737 | if (const Stmt *child = *I) |
| 738 | if (containsNonLocalVarDecl(child)) |
| 739 | return true; |
| 740 | |
Tom Care | 50e8ac2 | 2010-08-16 21:43:52 +0000 | [diff] [blame] | 741 | return false; |
| 742 | } |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 743 | |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 744 | |
Argyrios Kyrtzidis | ecc4d33 | 2011-02-23 21:04:44 +0000 | [diff] [blame] | 745 | void ento::registerIdempotentOperationChecker(CheckerManager &mgr) { |
| 746 | mgr.registerChecker<IdempotentOperationChecker>(); |
| 747 | } |