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