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