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 | |
Tom Care | 1fafd1d | 2010-08-06 22:23:07 +0000 | [diff] [blame] | 45 | #include "GRExprEngineExperimentalChecks.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" |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 48 | #include "clang/Checker/BugReporter/BugReporter.h" |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 49 | #include "clang/Checker/BugReporter/BugType.h" |
Tom Care | a9fbf5b | 2010-07-27 23:26:07 +0000 | [diff] [blame] | 50 | #include "clang/Checker/PathSensitive/CheckerHelpers.h" |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 51 | #include "clang/Checker/PathSensitive/CheckerVisitor.h" |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 52 | #include "clang/Checker/PathSensitive/GRCoreEngine.h" |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 53 | #include "clang/Checker/PathSensitive/SVals.h" |
| 54 | #include "clang/AST/Stmt.h" |
| 55 | #include "llvm/ADT/DenseMap.h" |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/SmallSet.h" |
Chandler Carruth | 256565b | 2010-07-07 00:07:37 +0000 | [diff] [blame] | 57 | #include "llvm/Support/ErrorHandling.h" |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 58 | #include <deque> |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 59 | |
| 60 | using namespace clang; |
| 61 | |
| 62 | namespace { |
| 63 | class IdempotentOperationChecker |
| 64 | : public CheckerVisitor<IdempotentOperationChecker> { |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 65 | public: |
| 66 | static void *getTag(); |
| 67 | void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B); |
| 68 | void PostVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B); |
| 69 | void VisitEndAnalysis(ExplodedGraph &G, BugReporter &B, GRExprEngine &Eng); |
| 70 | |
| 71 | private: |
| 72 | // Our assumption about a particular operation. |
| 73 | enum Assumption { Possible = 0, Impossible, Equal, LHSis1, RHSis1, LHSis0, |
| 74 | RHSis0 }; |
| 75 | |
| 76 | void UpdateAssumption(Assumption &A, const Assumption &New); |
| 77 | |
| 78 | // False positive reduction methods |
| 79 | static bool isSelfAssign(const Expr *LHS, const Expr *RHS); |
| 80 | static bool isUnused(const Expr *E, AnalysisContext *AC); |
| 81 | static bool isTruncationExtensionAssignment(const Expr *LHS, |
| 82 | const Expr *RHS); |
| 83 | bool PathWasCompletelyAnalyzed(const CFG *C, |
| 84 | const CFGBlock *CB, |
Ted Kremenek | 33d4626 | 2010-11-13 05:04:52 +0000 | [diff] [blame] | 85 | const CFGStmtMap *CBM, |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 86 | const GRCoreEngine &CE); |
| 87 | static bool CanVary(const Expr *Ex, |
| 88 | AnalysisContext *AC); |
| 89 | static bool isConstantOrPseudoConstant(const DeclRefExpr *DR, |
| 90 | AnalysisContext *AC); |
| 91 | static bool containsNonLocalVarDecl(const Stmt *S); |
| 92 | const ExplodedNodeSet getLastRelevantNodes(const CFGBlock *Begin, |
| 93 | const ExplodedNode *N); |
| 94 | |
| 95 | // Hash table and related data structures |
| 96 | struct BinaryOperatorData { |
| 97 | BinaryOperatorData() : assumption(Possible), analysisContext(0) {} |
| 98 | |
| 99 | Assumption assumption; |
| 100 | AnalysisContext *analysisContext; |
| 101 | ExplodedNodeSet explodedNodes; // Set of ExplodedNodes that refer to a |
| 102 | // BinaryOperator |
| 103 | }; |
| 104 | typedef llvm::DenseMap<const BinaryOperator *, BinaryOperatorData> |
| 105 | AssumptionMap; |
| 106 | AssumptionMap hash; |
| 107 | |
| 108 | // A class that performs reachability queries for CFGBlocks. Several internal |
| 109 | // checks in this checker require reachability information. The requests all |
| 110 | // tend to have a common destination, so we lazily do a predecessor search |
| 111 | // from the destination node and cache the results to prevent work |
| 112 | // duplication. |
| 113 | class CFGReachabilityAnalysis { |
| 114 | typedef llvm::SmallSet<unsigned, 32> ReachableSet; |
| 115 | typedef llvm::DenseMap<unsigned, ReachableSet> ReachableMap; |
| 116 | ReachableSet analyzed; |
| 117 | ReachableMap reachable; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 118 | public: |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 119 | inline bool isReachable(const CFGBlock *Src, const CFGBlock *Dst); |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 120 | private: |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 121 | void MapReachability(const CFGBlock *Dst); |
| 122 | }; |
| 123 | CFGReachabilityAnalysis CRA; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 124 | }; |
| 125 | } |
| 126 | |
| 127 | void *IdempotentOperationChecker::getTag() { |
| 128 | static int x = 0; |
| 129 | return &x; |
| 130 | } |
| 131 | |
| 132 | void clang::RegisterIdempotentOperationChecker(GRExprEngine &Eng) { |
| 133 | Eng.registerCheck(new IdempotentOperationChecker()); |
| 134 | } |
| 135 | |
| 136 | void IdempotentOperationChecker::PreVisitBinaryOperator( |
| 137 | CheckerContext &C, |
| 138 | const BinaryOperator *B) { |
Ted Kremenek | fe97fa1 | 2010-08-02 20:33:02 +0000 | [diff] [blame] | 139 | // Find or create an entry in the hash for this BinaryOperator instance. |
| 140 | // 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] | 141 | // 'Possible'. At this stage we do not store the ExplodedNode, as it has not |
| 142 | // been created yet. |
| 143 | BinaryOperatorData &Data = hash[B]; |
| 144 | Assumption &A = Data.assumption; |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 145 | AnalysisContext *AC = C.getCurrentAnalysisContext(); |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 146 | Data.analysisContext = AC; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 147 | |
| 148 | // If we already have visited this node on a path that does not contain an |
| 149 | // idempotent operation, return immediately. |
| 150 | if (A == Impossible) |
| 151 | return; |
| 152 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 153 | // Retrieve both sides of the operator and determine if they can vary (which |
| 154 | // may mean this is a false positive. |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 155 | const Expr *LHS = B->getLHS(); |
| 156 | const Expr *RHS = B->getRHS(); |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 157 | |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 158 | // At this stage we can calculate whether each side contains a false positive |
| 159 | // that applies to all operators. We only need to calculate this the first |
| 160 | // time. |
| 161 | bool LHSContainsFalsePositive = false, RHSContainsFalsePositive = false; |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 162 | if (A == Possible) { |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 163 | // An expression contains a false positive if it can't vary, or if it |
| 164 | // contains a known false positive VarDecl. |
| 165 | LHSContainsFalsePositive = !CanVary(LHS, AC) |
| 166 | || containsNonLocalVarDecl(LHS); |
| 167 | RHSContainsFalsePositive = !CanVary(RHS, AC) |
| 168 | || containsNonLocalVarDecl(RHS); |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 169 | } |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 170 | |
| 171 | const GRState *state = C.getState(); |
| 172 | |
| 173 | SVal LHSVal = state->getSVal(LHS); |
| 174 | SVal RHSVal = state->getSVal(RHS); |
| 175 | |
| 176 | // If either value is unknown, we can't be 100% sure of all paths. |
| 177 | if (LHSVal.isUnknownOrUndef() || RHSVal.isUnknownOrUndef()) { |
| 178 | A = Impossible; |
| 179 | return; |
| 180 | } |
| 181 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 182 | |
| 183 | // Dereference the LHS SVal if this is an assign operation |
| 184 | switch (Op) { |
| 185 | default: |
| 186 | break; |
| 187 | |
| 188 | // Fall through intentional |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 189 | case BO_AddAssign: |
| 190 | case BO_SubAssign: |
| 191 | case BO_MulAssign: |
| 192 | case BO_DivAssign: |
| 193 | case BO_AndAssign: |
| 194 | case BO_OrAssign: |
| 195 | case BO_XorAssign: |
| 196 | case BO_ShlAssign: |
| 197 | case BO_ShrAssign: |
| 198 | case BO_Assign: |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 199 | // Assign statements have one extra level of indirection |
| 200 | if (!isa<Loc>(LHSVal)) { |
| 201 | A = Impossible; |
| 202 | return; |
| 203 | } |
Ted Kremenek | 96ebad6 | 2010-09-09 07:13:00 +0000 | [diff] [blame] | 204 | LHSVal = state->getSVal(cast<Loc>(LHSVal), LHS->getType()); |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | |
| 208 | // We now check for various cases which result in an idempotent operation. |
| 209 | |
| 210 | // x op x |
| 211 | switch (Op) { |
| 212 | default: |
| 213 | break; // We don't care about any other operators. |
| 214 | |
| 215 | // Fall through intentional |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 216 | case BO_Assign: |
Tom Care | 6216dc0 | 2010-08-30 19:25:43 +0000 | [diff] [blame] | 217 | // x Assign x can be used to silence unused variable warnings intentionally. |
| 218 | // If this is a self assignment and the variable is referenced elsewhere, |
Tom Care | 84c24ed | 2010-09-07 20:27:56 +0000 | [diff] [blame] | 219 | // and the assignment is not a truncation or extension, then it is a false |
| 220 | // positive. |
Tom Care | 6216dc0 | 2010-08-30 19:25:43 +0000 | [diff] [blame] | 221 | if (isSelfAssign(LHS, RHS)) { |
Tom Care | 84c24ed | 2010-09-07 20:27:56 +0000 | [diff] [blame] | 222 | if (!isUnused(LHS, AC) && !isTruncationExtensionAssignment(LHS, RHS)) { |
Tom Care | 6216dc0 | 2010-08-30 19:25:43 +0000 | [diff] [blame] | 223 | UpdateAssumption(A, Equal); |
| 224 | return; |
| 225 | } |
| 226 | else { |
| 227 | A = Impossible; |
| 228 | return; |
| 229 | } |
Tom Care | df4ca42 | 2010-07-16 20:41:41 +0000 | [diff] [blame] | 230 | } |
| 231 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 232 | case BO_SubAssign: |
| 233 | case BO_DivAssign: |
| 234 | case BO_AndAssign: |
| 235 | case BO_OrAssign: |
| 236 | case BO_XorAssign: |
| 237 | case BO_Sub: |
| 238 | case BO_Div: |
| 239 | case BO_And: |
| 240 | case BO_Or: |
| 241 | case BO_Xor: |
| 242 | case BO_LOr: |
| 243 | case BO_LAnd: |
Tom Care | 9edd4d0 | 2010-08-27 22:50:47 +0000 | [diff] [blame] | 244 | case BO_EQ: |
| 245 | case BO_NE: |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 246 | if (LHSVal != RHSVal || LHSContainsFalsePositive |
| 247 | || RHSContainsFalsePositive) |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 248 | break; |
| 249 | UpdateAssumption(A, Equal); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | // x op 1 |
| 254 | switch (Op) { |
| 255 | default: |
| 256 | break; // We don't care about any other operators. |
| 257 | |
| 258 | // Fall through intentional |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 259 | case BO_MulAssign: |
| 260 | case BO_DivAssign: |
| 261 | case BO_Mul: |
| 262 | case BO_Div: |
| 263 | case BO_LOr: |
| 264 | case BO_LAnd: |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 265 | if (!RHSVal.isConstant(1) || RHSContainsFalsePositive) |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 266 | break; |
| 267 | UpdateAssumption(A, RHSis1); |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | // 1 op x |
| 272 | switch (Op) { |
| 273 | default: |
| 274 | break; // We don't care about any other operators. |
| 275 | |
| 276 | // Fall through intentional |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 277 | case BO_MulAssign: |
| 278 | case BO_Mul: |
| 279 | case BO_LOr: |
| 280 | case BO_LAnd: |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 281 | if (!LHSVal.isConstant(1) || LHSContainsFalsePositive) |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 282 | break; |
| 283 | UpdateAssumption(A, LHSis1); |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | // x op 0 |
| 288 | switch (Op) { |
| 289 | default: |
| 290 | break; // We don't care about any other operators. |
| 291 | |
| 292 | // Fall through intentional |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 293 | case BO_AddAssign: |
| 294 | case BO_SubAssign: |
| 295 | case BO_MulAssign: |
| 296 | case BO_AndAssign: |
| 297 | case BO_OrAssign: |
| 298 | case BO_XorAssign: |
| 299 | case BO_Add: |
| 300 | case BO_Sub: |
| 301 | case BO_Mul: |
| 302 | case BO_And: |
| 303 | case BO_Or: |
| 304 | case BO_Xor: |
| 305 | case BO_Shl: |
| 306 | case BO_Shr: |
| 307 | case BO_LOr: |
| 308 | case BO_LAnd: |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 309 | if (!RHSVal.isConstant(0) || RHSContainsFalsePositive) |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 310 | break; |
| 311 | UpdateAssumption(A, RHSis0); |
| 312 | return; |
| 313 | } |
| 314 | |
| 315 | // 0 op x |
| 316 | switch (Op) { |
| 317 | default: |
| 318 | break; // We don't care about any other operators. |
| 319 | |
| 320 | // Fall through intentional |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 321 | //case BO_AddAssign: // Common false positive |
| 322 | case BO_SubAssign: // Check only if unsigned |
| 323 | case BO_MulAssign: |
| 324 | case BO_DivAssign: |
| 325 | case BO_AndAssign: |
| 326 | //case BO_OrAssign: // Common false positive |
| 327 | //case BO_XorAssign: // Common false positive |
| 328 | case BO_ShlAssign: |
| 329 | case BO_ShrAssign: |
| 330 | case BO_Add: |
| 331 | case BO_Sub: |
| 332 | case BO_Mul: |
| 333 | case BO_Div: |
| 334 | case BO_And: |
| 335 | case BO_Or: |
| 336 | case BO_Xor: |
| 337 | case BO_Shl: |
| 338 | case BO_Shr: |
| 339 | case BO_LOr: |
| 340 | case BO_LAnd: |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 341 | if (!LHSVal.isConstant(0) || LHSContainsFalsePositive) |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 342 | break; |
| 343 | UpdateAssumption(A, LHSis0); |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | // If we get to this point, there has been a valid use of this operation. |
| 348 | A = Impossible; |
| 349 | } |
| 350 | |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 351 | // At the post visit stage, the predecessor ExplodedNode will be the |
| 352 | // BinaryOperator that was just created. We use this hook to collect the |
| 353 | // ExplodedNode. |
| 354 | void IdempotentOperationChecker::PostVisitBinaryOperator( |
| 355 | CheckerContext &C, |
| 356 | const BinaryOperator *B) { |
| 357 | // Add the ExplodedNode we just visited |
| 358 | BinaryOperatorData &Data = hash[B]; |
Ted Kremenek | 96ebad6 | 2010-09-09 07:13:00 +0000 | [diff] [blame] | 359 | assert(isa<BinaryOperator>(cast<StmtPoint>(C.getPredecessor() |
| 360 | ->getLocation()).getStmt())); |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 361 | Data.explodedNodes.Add(C.getPredecessor()); |
| 362 | } |
| 363 | |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 364 | void IdempotentOperationChecker::VisitEndAnalysis(ExplodedGraph &G, |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 365 | BugReporter &BR, |
Tom Care | bc42c53 | 2010-08-03 01:55:07 +0000 | [diff] [blame] | 366 | GRExprEngine &Eng) { |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 367 | BugType *BT = new BugType("Idempotent operation", "Dead code"); |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 368 | // Iterate over the hash to see if we have any paths with definite |
| 369 | // idempotent operations. |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 370 | for (AssumptionMap::const_iterator i = hash.begin(); i != hash.end(); ++i) { |
| 371 | // Unpack the hash contents |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 372 | const BinaryOperatorData &Data = i->second; |
| 373 | const Assumption &A = Data.assumption; |
| 374 | AnalysisContext *AC = Data.analysisContext; |
| 375 | const ExplodedNodeSet &ES = Data.explodedNodes; |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 376 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 377 | const BinaryOperator *B = i->first; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 378 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 379 | if (A == Impossible) |
| 380 | continue; |
| 381 | |
| 382 | // If the analyzer did not finish, check to see if we can still emit this |
| 383 | // warning |
| 384 | if (Eng.hasWorkRemaining()) { |
| 385 | const CFGStmtMap *CBM = CFGStmtMap::Build(AC->getCFG(), |
| 386 | &AC->getParentMap()); |
| 387 | |
| 388 | // If we can trace back |
| 389 | if (!PathWasCompletelyAnalyzed(AC->getCFG(), |
Ted Kremenek | 33d4626 | 2010-11-13 05:04:52 +0000 | [diff] [blame] | 390 | CBM->getBlock(B), CBM, |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 391 | Eng.getCoreEngine())) |
| 392 | continue; |
| 393 | |
| 394 | delete CBM; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 395 | } |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 396 | |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 397 | // Select the error message and SourceRanges to report. |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 398 | llvm::SmallString<128> buf; |
| 399 | llvm::raw_svector_ostream os(buf); |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 400 | bool LHSRelevant = false, RHSRelevant = false; |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 401 | switch (A) { |
| 402 | case Equal: |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 403 | LHSRelevant = true; |
| 404 | RHSRelevant = true; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 405 | if (B->getOpcode() == BO_Assign) |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 406 | os << "Assigned value is always the same as the existing value"; |
| 407 | else |
| 408 | os << "Both operands to '" << B->getOpcodeStr() |
| 409 | << "' always have the same value"; |
| 410 | break; |
| 411 | case LHSis1: |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 412 | LHSRelevant = true; |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 413 | os << "The left operand to '" << B->getOpcodeStr() << "' is always 1"; |
| 414 | break; |
| 415 | case RHSis1: |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 416 | RHSRelevant = true; |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 417 | os << "The right operand to '" << B->getOpcodeStr() << "' is always 1"; |
| 418 | break; |
| 419 | case LHSis0: |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 420 | LHSRelevant = true; |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 421 | os << "The left operand to '" << B->getOpcodeStr() << "' is always 0"; |
| 422 | break; |
| 423 | case RHSis0: |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 424 | RHSRelevant = true; |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 425 | os << "The right operand to '" << B->getOpcodeStr() << "' is always 0"; |
| 426 | break; |
| 427 | case Possible: |
| 428 | llvm_unreachable("Operation was never marked with an assumption"); |
| 429 | case Impossible: |
| 430 | llvm_unreachable(0); |
| 431 | } |
| 432 | |
Tom Care | 2bbbe50 | 2010-09-02 23:30:22 +0000 | [diff] [blame] | 433 | // Add a report for each ExplodedNode |
| 434 | for (ExplodedNodeSet::iterator I = ES.begin(), E = ES.end(); I != E; ++I) { |
| 435 | EnhancedBugReport *report = new EnhancedBugReport(*BT, os.str(), *I); |
| 436 | |
| 437 | // Add source ranges and visitor hooks |
| 438 | if (LHSRelevant) { |
| 439 | const Expr *LHS = i->first->getLHS(); |
| 440 | report->addRange(LHS->getSourceRange()); |
| 441 | report->addVisitorCreator(bugreporter::registerVarDeclsLastStore, LHS); |
| 442 | } |
| 443 | if (RHSRelevant) { |
| 444 | const Expr *RHS = i->first->getRHS(); |
| 445 | report->addRange(i->first->getRHS()->getSourceRange()); |
| 446 | report->addVisitorCreator(bugreporter::registerVarDeclsLastStore, RHS); |
| 447 | } |
| 448 | |
| 449 | BR.EmitReport(report); |
| 450 | } |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
| 454 | // Updates the current assumption given the new assumption |
| 455 | inline void IdempotentOperationChecker::UpdateAssumption(Assumption &A, |
| 456 | const Assumption &New) { |
Tom Care | d8421ed | 2010-08-27 22:35:28 +0000 | [diff] [blame] | 457 | // If the assumption is the same, there is nothing to do |
| 458 | if (A == New) |
| 459 | return; |
| 460 | |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 461 | switch (A) { |
| 462 | // If we don't currently have an assumption, set it |
| 463 | case Possible: |
| 464 | A = New; |
| 465 | return; |
| 466 | |
| 467 | // If we have determined that a valid state happened, ignore the new |
| 468 | // assumption. |
| 469 | case Impossible: |
| 470 | return; |
| 471 | |
| 472 | // Any other case means that we had a different assumption last time. We don't |
| 473 | // currently support mixing assumptions for diagnostic reasons, so we set |
| 474 | // our assumption to be impossible. |
| 475 | default: |
| 476 | A = Impossible; |
| 477 | return; |
| 478 | } |
| 479 | } |
| 480 | |
Tom Care | 6216dc0 | 2010-08-30 19:25:43 +0000 | [diff] [blame] | 481 | // Check for a statement where a variable is self assigned to possibly avoid an |
| 482 | // unused variable warning. |
| 483 | bool IdempotentOperationChecker::isSelfAssign(const Expr *LHS, const Expr *RHS) { |
Tom Care | df4ca42 | 2010-07-16 20:41:41 +0000 | [diff] [blame] | 484 | LHS = LHS->IgnoreParenCasts(); |
| 485 | RHS = RHS->IgnoreParenCasts(); |
| 486 | |
| 487 | const DeclRefExpr *LHS_DR = dyn_cast<DeclRefExpr>(LHS); |
| 488 | if (!LHS_DR) |
| 489 | return false; |
| 490 | |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 491 | const VarDecl *VD = dyn_cast<VarDecl>(LHS_DR->getDecl()); |
| 492 | if (!VD) |
Tom Care | df4ca42 | 2010-07-16 20:41:41 +0000 | [diff] [blame] | 493 | return false; |
| 494 | |
| 495 | const DeclRefExpr *RHS_DR = dyn_cast<DeclRefExpr>(RHS); |
| 496 | if (!RHS_DR) |
| 497 | return false; |
| 498 | |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 499 | if (VD != RHS_DR->getDecl()) |
| 500 | return false; |
| 501 | |
Tom Care | 6216dc0 | 2010-08-30 19:25:43 +0000 | [diff] [blame] | 502 | return true; |
| 503 | } |
| 504 | |
| 505 | // Returns true if the Expr points to a VarDecl that is not read anywhere |
| 506 | // outside of self-assignments. |
| 507 | bool IdempotentOperationChecker::isUnused(const Expr *E, |
| 508 | AnalysisContext *AC) { |
| 509 | if (!E) |
| 510 | return false; |
| 511 | |
| 512 | const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); |
| 513 | if (!DR) |
| 514 | return false; |
| 515 | |
| 516 | const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 517 | if (!VD) |
| 518 | return false; |
| 519 | |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 520 | if (AC->getPseudoConstantAnalysis()->wasReferenced(VD)) |
| 521 | return false; |
| 522 | |
| 523 | return true; |
Tom Care | df4ca42 | 2010-07-16 20:41:41 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | // Check for self casts truncating/extending a variable |
| 527 | bool IdempotentOperationChecker::isTruncationExtensionAssignment( |
| 528 | const Expr *LHS, |
| 529 | const Expr *RHS) { |
| 530 | |
| 531 | const DeclRefExpr *LHS_DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParenCasts()); |
| 532 | if (!LHS_DR) |
| 533 | return false; |
| 534 | |
| 535 | const VarDecl *VD = dyn_cast<VarDecl>(LHS_DR->getDecl()); |
| 536 | if (!VD) |
| 537 | return false; |
| 538 | |
| 539 | const DeclRefExpr *RHS_DR = dyn_cast<DeclRefExpr>(RHS->IgnoreParenCasts()); |
| 540 | if (!RHS_DR) |
| 541 | return false; |
| 542 | |
| 543 | if (VD != RHS_DR->getDecl()) |
| 544 | return false; |
| 545 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 546 | return dyn_cast<DeclRefExpr>(RHS->IgnoreParenLValueCasts()) == NULL; |
Tom Care | df4ca42 | 2010-07-16 20:41:41 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 549 | // Returns false if a path to this block was not completely analyzed, or true |
| 550 | // otherwise. |
| 551 | bool IdempotentOperationChecker::PathWasCompletelyAnalyzed( |
| 552 | const CFG *C, |
| 553 | const CFGBlock *CB, |
Ted Kremenek | 33d4626 | 2010-11-13 05:04:52 +0000 | [diff] [blame] | 554 | const CFGStmtMap *CBM, |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 555 | const GRCoreEngine &CE) { |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 556 | // Test for reachability from any aborted blocks to this block |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 557 | typedef GRCoreEngine::BlocksAborted::const_iterator AbortedIterator; |
| 558 | for (AbortedIterator I = CE.blocks_aborted_begin(), |
| 559 | E = CE.blocks_aborted_end(); I != E; ++I) { |
| 560 | const BlockEdge &BE = I->first; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 561 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 562 | // 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] | 563 | // analyzed. If we can reach this block from the aborted block, then this |
| 564 | // block was not completely analyzed. |
| 565 | if (CRA.isReachable(BE.getDst(), CB)) |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 566 | return false; |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 567 | } |
Ted Kremenek | 33d4626 | 2010-11-13 05:04:52 +0000 | [diff] [blame] | 568 | |
| 569 | // For the items still on the worklist, see if they are in blocks that |
| 570 | // can eventually reach 'CB'. |
| 571 | class VisitWL : public GRWorkList::Visitor { |
| 572 | const CFGStmtMap *CBM; |
| 573 | const CFGBlock *TargetBlock; |
| 574 | CFGReachabilityAnalysis &CRA; |
| 575 | public: |
| 576 | VisitWL(const CFGStmtMap *cbm, const CFGBlock *targetBlock, |
| 577 | CFGReachabilityAnalysis &cra) |
| 578 | : CBM(cbm), TargetBlock(targetBlock), CRA(cra) {} |
| 579 | virtual bool Visit(const GRWorkListUnit &U) { |
| 580 | ProgramPoint P = U.getNode()->getLocation(); |
| 581 | const CFGBlock *B = 0; |
| 582 | if (StmtPoint *SP = dyn_cast<StmtPoint>(&P)) { |
| 583 | B = CBM->getBlock(SP->getStmt()); |
| 584 | } |
Ted Kremenek | ed02366 | 2010-11-13 05:12:26 +0000 | [diff] [blame] | 585 | else if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
| 586 | B = BE->getDst(); |
| 587 | } |
| 588 | else if (BlockEntrance *BEnt = dyn_cast<BlockEntrance>(&P)) { |
| 589 | B = BEnt->getBlock(); |
| 590 | } |
| 591 | else if (BlockExit *BExit = dyn_cast<BlockExit>(&P)) { |
| 592 | B = BExit->getBlock(); |
| 593 | } |
Ted Kremenek | 33d4626 | 2010-11-13 05:04:52 +0000 | [diff] [blame] | 594 | if (!B) |
| 595 | return true; |
| 596 | |
| 597 | return CRA.isReachable(B, TargetBlock); |
| 598 | } |
| 599 | }; |
| 600 | VisitWL visitWL(CBM, CB, CRA); |
| 601 | // Were there any items in the worklist that could potentially reach |
| 602 | // this block? |
| 603 | if (CE.getWorkList()->VisitItemsInWorkList(visitWL)) |
| 604 | return false; |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 605 | |
Tom Care | b062795 | 2010-09-09 02:04:52 +0000 | [diff] [blame] | 606 | // Verify that this block is reachable from the entry block |
| 607 | if (!CRA.isReachable(&C->getEntry(), CB)) |
| 608 | return false; |
| 609 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 610 | // If we get to this point, there is no connection to the entry block or an |
| 611 | // aborted block. This path is unreachable and we can report the error. |
| 612 | return true; |
| 613 | } |
| 614 | |
| 615 | // Recursive function that determines whether an expression contains any element |
| 616 | // that varies. This could be due to a compile-time constant like sizeof. An |
| 617 | // expression may also involve a variable that behaves like a constant. The |
| 618 | // function returns true if the expression varies, and false otherwise. |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 619 | bool IdempotentOperationChecker::CanVary(const Expr *Ex, |
| 620 | AnalysisContext *AC) { |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 621 | // Parentheses and casts are irrelevant here |
| 622 | Ex = Ex->IgnoreParenCasts(); |
| 623 | |
| 624 | if (Ex->getLocStart().isMacroID()) |
| 625 | return false; |
| 626 | |
| 627 | switch (Ex->getStmtClass()) { |
| 628 | // Trivially true cases |
| 629 | case Stmt::ArraySubscriptExprClass: |
| 630 | case Stmt::MemberExprClass: |
| 631 | case Stmt::StmtExprClass: |
| 632 | case Stmt::CallExprClass: |
| 633 | case Stmt::VAArgExprClass: |
| 634 | case Stmt::ShuffleVectorExprClass: |
| 635 | return true; |
| 636 | default: |
| 637 | return true; |
| 638 | |
| 639 | // Trivially false cases |
| 640 | case Stmt::IntegerLiteralClass: |
| 641 | case Stmt::CharacterLiteralClass: |
| 642 | case Stmt::FloatingLiteralClass: |
| 643 | case Stmt::PredefinedExprClass: |
| 644 | case Stmt::ImaginaryLiteralClass: |
| 645 | case Stmt::StringLiteralClass: |
| 646 | case Stmt::OffsetOfExprClass: |
| 647 | case Stmt::CompoundLiteralExprClass: |
| 648 | case Stmt::AddrLabelExprClass: |
Francois Pichet | f187237 | 2010-12-08 22:35:30 +0000 | [diff] [blame] | 649 | case Stmt::BinaryTypeTraitExprClass: |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 650 | case Stmt::GNUNullExprClass: |
| 651 | case Stmt::InitListExprClass: |
| 652 | case Stmt::DesignatedInitExprClass: |
| 653 | case Stmt::BlockExprClass: |
| 654 | case Stmt::BlockDeclRefExprClass: |
| 655 | return false; |
| 656 | |
| 657 | // Cases requiring custom logic |
| 658 | case Stmt::SizeOfAlignOfExprClass: { |
| 659 | const SizeOfAlignOfExpr *SE = cast<const SizeOfAlignOfExpr>(Ex); |
| 660 | if (!SE->isSizeOf()) |
| 661 | return false; |
| 662 | return SE->getTypeOfArgument()->isVariableArrayType(); |
| 663 | } |
| 664 | case Stmt::DeclRefExprClass: |
Tom Care | 6216dc0 | 2010-08-30 19:25:43 +0000 | [diff] [blame] | 665 | // Check for constants/pseudoconstants |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 666 | return !isConstantOrPseudoConstant(cast<DeclRefExpr>(Ex), AC); |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 667 | |
| 668 | // The next cases require recursion for subexpressions |
| 669 | case Stmt::BinaryOperatorClass: { |
| 670 | const BinaryOperator *B = cast<const BinaryOperator>(Ex); |
Ted Kremenek | 74faec2 | 2010-10-29 01:06:54 +0000 | [diff] [blame] | 671 | |
| 672 | // Exclude cases involving pointer arithmetic. These are usually |
| 673 | // false positives. |
| 674 | if (B->getOpcode() == BO_Sub || B->getOpcode() == BO_Add) |
| 675 | if (B->getLHS()->getType()->getAs<PointerType>()) |
| 676 | return false; |
| 677 | |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 678 | return CanVary(B->getRHS(), AC) |
| 679 | || CanVary(B->getLHS(), AC); |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 680 | } |
| 681 | case Stmt::UnaryOperatorClass: { |
| 682 | const UnaryOperator *U = cast<const UnaryOperator>(Ex); |
Eli Friedman | de7e662 | 2010-08-13 01:36:11 +0000 | [diff] [blame] | 683 | // Handle trivial case first |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 684 | switch (U->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 685 | case UO_Extension: |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 686 | return false; |
| 687 | default: |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 688 | return CanVary(U->getSubExpr(), AC); |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | case Stmt::ChooseExprClass: |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 692 | return CanVary(cast<const ChooseExpr>(Ex)->getChosenSubExpr( |
| 693 | AC->getASTContext()), AC); |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 694 | case Stmt::ConditionalOperatorClass: |
Tom Care | 6216dc0 | 2010-08-30 19:25:43 +0000 | [diff] [blame] | 695 | return CanVary(cast<const ConditionalOperator>(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, |
| 702 | AnalysisContext *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 | |
| 744 | // Returns the successor nodes of N whose CFGBlocks cannot reach N's CFGBlock. |
| 745 | // This effectively gives us a set of points in the ExplodedGraph where |
| 746 | // subsequent execution could not affect the idempotent operation on this path. |
| 747 | // This is useful for displaying paths after the point of the error, providing |
| 748 | // an example of how this idempotent operation cannot change. |
| 749 | const ExplodedNodeSet IdempotentOperationChecker::getLastRelevantNodes( |
| 750 | const CFGBlock *Begin, const ExplodedNode *N) { |
| 751 | std::deque<const ExplodedNode *> WorkList; |
| 752 | llvm::SmallPtrSet<const ExplodedNode *, 32> Visited; |
| 753 | ExplodedNodeSet Result; |
| 754 | |
| 755 | WorkList.push_back(N); |
| 756 | |
| 757 | while (!WorkList.empty()) { |
| 758 | const ExplodedNode *Head = WorkList.front(); |
| 759 | WorkList.pop_front(); |
| 760 | Visited.insert(Head); |
| 761 | |
| 762 | const ProgramPoint &PP = Head->getLocation(); |
| 763 | if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&PP)) { |
| 764 | // Get the CFGBlock and test the reachability |
| 765 | const CFGBlock *CB = BE->getBlock(); |
| 766 | |
| 767 | // If we cannot reach the beginning CFGBlock from this block, then we are |
| 768 | // finished |
| 769 | if (!CRA.isReachable(CB, Begin)) { |
| 770 | Result.Add(const_cast<ExplodedNode *>(Head)); |
| 771 | continue; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | // Add unvisited children to the worklist |
| 776 | for (ExplodedNode::const_succ_iterator I = Head->succ_begin(), |
| 777 | E = Head->succ_end(); I != E; ++I) |
| 778 | if (!Visited.count(*I)) |
| 779 | WorkList.push_back(*I); |
| 780 | } |
| 781 | |
| 782 | // Return the ExplodedNodes that were found |
| 783 | return Result; |
| 784 | } |
| 785 | |
| 786 | bool IdempotentOperationChecker::CFGReachabilityAnalysis::isReachable( |
| 787 | const CFGBlock *Src, |
| 788 | const CFGBlock *Dst) { |
| 789 | const unsigned DstBlockID = Dst->getBlockID(); |
| 790 | |
| 791 | // If we haven't analyzed the destination node, run the analysis now |
| 792 | if (!analyzed.count(DstBlockID)) { |
| 793 | MapReachability(Dst); |
| 794 | analyzed.insert(DstBlockID); |
| 795 | } |
| 796 | |
| 797 | // Return the cached result |
| 798 | return reachable[DstBlockID].count(Src->getBlockID()); |
| 799 | } |
| 800 | |
| 801 | // Maps reachability to a common node by walking the predecessors of the |
| 802 | // destination node. |
| 803 | void IdempotentOperationChecker::CFGReachabilityAnalysis::MapReachability( |
| 804 | const CFGBlock *Dst) { |
| 805 | std::deque<const CFGBlock *> WorkList; |
| 806 | // Maintain a visited list to ensure we don't get stuck on cycles |
| 807 | llvm::SmallSet<unsigned, 32> Visited; |
| 808 | ReachableSet &DstReachability = reachable[Dst->getBlockID()]; |
| 809 | |
| 810 | // Start searching from the destination node, since we commonly will perform |
| 811 | // multiple queries relating to a destination node. |
| 812 | WorkList.push_back(Dst); |
| 813 | |
| 814 | bool firstRun = true; |
| 815 | while (!WorkList.empty()) { |
| 816 | const CFGBlock *Head = WorkList.front(); |
| 817 | WorkList.pop_front(); |
| 818 | Visited.insert(Head->getBlockID()); |
| 819 | |
| 820 | // Update reachability information for this node -> Dst |
| 821 | if (!firstRun) |
| 822 | // Don't insert Dst -> Dst unless it was a predecessor of itself |
| 823 | DstReachability.insert(Head->getBlockID()); |
| 824 | else |
| 825 | firstRun = false; |
| 826 | |
| 827 | // Add the predecessors to the worklist unless we have already visited them |
| 828 | for (CFGBlock::const_pred_iterator I = Head->pred_begin(); |
| 829 | I != Head->pred_end(); ++I) |
| 830 | if (!Visited.count((*I)->getBlockID())) |
| 831 | WorkList.push_back(*I); |
| 832 | } |
| 833 | } |