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 | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 48 | #include "clang/Checker/BugReporter/BugType.h" |
Tom Care | a9fbf5b | 2010-07-27 23:26:07 +0000 | [diff] [blame] | 49 | #include "clang/Checker/PathSensitive/CheckerHelpers.h" |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 50 | #include "clang/Checker/PathSensitive/CheckerVisitor.h" |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 51 | #include "clang/Checker/PathSensitive/GRCoreEngine.h" |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 52 | #include "clang/Checker/PathSensitive/SVals.h" |
| 53 | #include "clang/AST/Stmt.h" |
| 54 | #include "llvm/ADT/DenseMap.h" |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/SmallSet.h" |
Chandler Carruth | 256565b | 2010-07-07 00:07:37 +0000 | [diff] [blame] | 56 | #include "llvm/Support/ErrorHandling.h" |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 57 | #include <deque> |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 58 | |
| 59 | using namespace clang; |
| 60 | |
| 61 | namespace { |
| 62 | class IdempotentOperationChecker |
| 63 | : public CheckerVisitor<IdempotentOperationChecker> { |
| 64 | public: |
| 65 | static void *getTag(); |
| 66 | void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B); |
Tom Care | bc42c53 | 2010-08-03 01:55:07 +0000 | [diff] [blame] | 67 | void VisitEndAnalysis(ExplodedGraph &G, BugReporter &B, GRExprEngine &Eng); |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 68 | |
| 69 | private: |
| 70 | // Our assumption about a particular operation. |
Ted Kremenek | fe97fa1 | 2010-08-02 20:33:02 +0000 | [diff] [blame] | 71 | enum Assumption { Possible = 0, Impossible, Equal, LHSis1, RHSis1, LHSis0, |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 72 | RHSis0 }; |
| 73 | |
| 74 | void UpdateAssumption(Assumption &A, const Assumption &New); |
| 75 | |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 76 | // False positive reduction methods |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 77 | static bool isUnusedSelfAssign(const Expr *LHS, |
| 78 | const Expr *RHS, |
| 79 | AnalysisContext *AC); |
Tom Care | df4ca42 | 2010-07-16 20:41:41 +0000 | [diff] [blame] | 80 | static bool isTruncationExtensionAssignment(const Expr *LHS, |
| 81 | const Expr *RHS); |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 82 | static bool PathWasCompletelyAnalyzed(const CFG *C, |
| 83 | const CFGBlock *CB, |
| 84 | const GRCoreEngine &CE); |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 85 | static bool CanVary(const Expr *Ex, AnalysisContext *AC); |
| 86 | static bool isConstantOrPseudoConstant(const DeclRefExpr *DR, |
| 87 | AnalysisContext *AC); |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 88 | static bool containsNonLocalVarDecl(const Stmt *S); |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 89 | |
| 90 | // Hash table |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 91 | typedef llvm::DenseMap<const BinaryOperator *, |
| 92 | std::pair<Assumption, AnalysisContext*> > |
| 93 | AssumptionMap; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 94 | AssumptionMap hash; |
| 95 | }; |
| 96 | } |
| 97 | |
| 98 | void *IdempotentOperationChecker::getTag() { |
| 99 | static int x = 0; |
| 100 | return &x; |
| 101 | } |
| 102 | |
| 103 | void clang::RegisterIdempotentOperationChecker(GRExprEngine &Eng) { |
| 104 | Eng.registerCheck(new IdempotentOperationChecker()); |
| 105 | } |
| 106 | |
| 107 | void IdempotentOperationChecker::PreVisitBinaryOperator( |
| 108 | CheckerContext &C, |
| 109 | const BinaryOperator *B) { |
Ted Kremenek | fe97fa1 | 2010-08-02 20:33:02 +0000 | [diff] [blame] | 110 | // Find or create an entry in the hash for this BinaryOperator instance. |
| 111 | // If we haven't done a lookup before, it will get default initialized to |
| 112 | // 'Possible'. |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 113 | std::pair<Assumption, AnalysisContext *> &Data = hash[B]; |
| 114 | Assumption &A = Data.first; |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 115 | AnalysisContext *AC = C.getCurrentAnalysisContext(); |
| 116 | Data.second = AC; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 117 | |
| 118 | // If we already have visited this node on a path that does not contain an |
| 119 | // idempotent operation, return immediately. |
| 120 | if (A == Impossible) |
| 121 | return; |
| 122 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 123 | // Retrieve both sides of the operator and determine if they can vary (which |
| 124 | // may mean this is a false positive. |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 125 | const Expr *LHS = B->getLHS(); |
| 126 | const Expr *RHS = B->getRHS(); |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 127 | |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 128 | // At this stage we can calculate whether each side contains a false positive |
| 129 | // that applies to all operators. We only need to calculate this the first |
| 130 | // time. |
| 131 | bool LHSContainsFalsePositive = false, RHSContainsFalsePositive = false; |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 132 | if (A == Possible) { |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 133 | // An expression contains a false positive if it can't vary, or if it |
| 134 | // contains a known false positive VarDecl. |
| 135 | LHSContainsFalsePositive = !CanVary(LHS, AC) |
| 136 | || containsNonLocalVarDecl(LHS); |
| 137 | RHSContainsFalsePositive = !CanVary(RHS, AC) |
| 138 | || containsNonLocalVarDecl(RHS); |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 139 | } |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 140 | |
| 141 | const GRState *state = C.getState(); |
| 142 | |
| 143 | SVal LHSVal = state->getSVal(LHS); |
| 144 | SVal RHSVal = state->getSVal(RHS); |
| 145 | |
| 146 | // If either value is unknown, we can't be 100% sure of all paths. |
| 147 | if (LHSVal.isUnknownOrUndef() || RHSVal.isUnknownOrUndef()) { |
| 148 | A = Impossible; |
| 149 | return; |
| 150 | } |
| 151 | BinaryOperator::Opcode Op = B->getOpcode(); |
| 152 | |
| 153 | // Dereference the LHS SVal if this is an assign operation |
| 154 | switch (Op) { |
| 155 | default: |
| 156 | break; |
| 157 | |
| 158 | // Fall through intentional |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 159 | case BO_AddAssign: |
| 160 | case BO_SubAssign: |
| 161 | case BO_MulAssign: |
| 162 | case BO_DivAssign: |
| 163 | case BO_AndAssign: |
| 164 | case BO_OrAssign: |
| 165 | case BO_XorAssign: |
| 166 | case BO_ShlAssign: |
| 167 | case BO_ShrAssign: |
| 168 | case BO_Assign: |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 169 | // Assign statements have one extra level of indirection |
| 170 | if (!isa<Loc>(LHSVal)) { |
| 171 | A = Impossible; |
| 172 | return; |
| 173 | } |
| 174 | LHSVal = state->getSVal(cast<Loc>(LHSVal)); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | // We now check for various cases which result in an idempotent operation. |
| 179 | |
| 180 | // x op x |
| 181 | switch (Op) { |
| 182 | default: |
| 183 | break; // We don't care about any other operators. |
| 184 | |
| 185 | // Fall through intentional |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 186 | case BO_Assign: |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 187 | // x Assign x can be used to silence unused variable warnings intentionally, |
| 188 | // and has a slightly different definition for false positives. |
| 189 | if (isUnusedSelfAssign(RHS, LHS, AC) |
| 190 | || isTruncationExtensionAssignment(RHS, LHS) |
| 191 | || containsNonLocalVarDecl(RHS) |
| 192 | || containsNonLocalVarDecl(LHS)) { |
Tom Care | df4ca42 | 2010-07-16 20:41:41 +0000 | [diff] [blame] | 193 | A = Impossible; |
| 194 | return; |
| 195 | } |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 196 | if (LHSVal != RHSVal) |
| 197 | break; |
| 198 | UpdateAssumption(A, Equal); |
| 199 | return; |
Tom Care | df4ca42 | 2010-07-16 20:41:41 +0000 | [diff] [blame] | 200 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 201 | case BO_SubAssign: |
| 202 | case BO_DivAssign: |
| 203 | case BO_AndAssign: |
| 204 | case BO_OrAssign: |
| 205 | case BO_XorAssign: |
| 206 | case BO_Sub: |
| 207 | case BO_Div: |
| 208 | case BO_And: |
| 209 | case BO_Or: |
| 210 | case BO_Xor: |
| 211 | case BO_LOr: |
| 212 | case BO_LAnd: |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 213 | if (LHSVal != RHSVal || LHSContainsFalsePositive |
| 214 | || RHSContainsFalsePositive) |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 215 | break; |
| 216 | UpdateAssumption(A, Equal); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | // x op 1 |
| 221 | switch (Op) { |
| 222 | default: |
| 223 | break; // We don't care about any other operators. |
| 224 | |
| 225 | // Fall through intentional |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 226 | case BO_MulAssign: |
| 227 | case BO_DivAssign: |
| 228 | case BO_Mul: |
| 229 | case BO_Div: |
| 230 | case BO_LOr: |
| 231 | case BO_LAnd: |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 232 | if (!RHSVal.isConstant(1) || RHSContainsFalsePositive) |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 233 | break; |
| 234 | UpdateAssumption(A, RHSis1); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | // 1 op x |
| 239 | switch (Op) { |
| 240 | default: |
| 241 | break; // We don't care about any other operators. |
| 242 | |
| 243 | // Fall through intentional |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 244 | case BO_MulAssign: |
| 245 | case BO_Mul: |
| 246 | case BO_LOr: |
| 247 | case BO_LAnd: |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 248 | if (!LHSVal.isConstant(1) || LHSContainsFalsePositive) |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 249 | break; |
| 250 | UpdateAssumption(A, LHSis1); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | // x op 0 |
| 255 | switch (Op) { |
| 256 | default: |
| 257 | break; // We don't care about any other operators. |
| 258 | |
| 259 | // Fall through intentional |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 260 | case BO_AddAssign: |
| 261 | case BO_SubAssign: |
| 262 | case BO_MulAssign: |
| 263 | case BO_AndAssign: |
| 264 | case BO_OrAssign: |
| 265 | case BO_XorAssign: |
| 266 | case BO_Add: |
| 267 | case BO_Sub: |
| 268 | case BO_Mul: |
| 269 | case BO_And: |
| 270 | case BO_Or: |
| 271 | case BO_Xor: |
| 272 | case BO_Shl: |
| 273 | case BO_Shr: |
| 274 | case BO_LOr: |
| 275 | case BO_LAnd: |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 276 | if (!RHSVal.isConstant(0) || RHSContainsFalsePositive) |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 277 | break; |
| 278 | UpdateAssumption(A, RHSis0); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | // 0 op x |
| 283 | switch (Op) { |
| 284 | default: |
| 285 | break; // We don't care about any other operators. |
| 286 | |
| 287 | // Fall through intentional |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 288 | //case BO_AddAssign: // Common false positive |
| 289 | case BO_SubAssign: // Check only if unsigned |
| 290 | case BO_MulAssign: |
| 291 | case BO_DivAssign: |
| 292 | case BO_AndAssign: |
| 293 | //case BO_OrAssign: // Common false positive |
| 294 | //case BO_XorAssign: // Common false positive |
| 295 | case BO_ShlAssign: |
| 296 | case BO_ShrAssign: |
| 297 | case BO_Add: |
| 298 | case BO_Sub: |
| 299 | case BO_Mul: |
| 300 | case BO_Div: |
| 301 | case BO_And: |
| 302 | case BO_Or: |
| 303 | case BO_Xor: |
| 304 | case BO_Shl: |
| 305 | case BO_Shr: |
| 306 | case BO_LOr: |
| 307 | case BO_LAnd: |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 308 | if (!LHSVal.isConstant(0) || LHSContainsFalsePositive) |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 309 | break; |
| 310 | UpdateAssumption(A, LHSis0); |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | // If we get to this point, there has been a valid use of this operation. |
| 315 | A = Impossible; |
| 316 | } |
| 317 | |
| 318 | void IdempotentOperationChecker::VisitEndAnalysis(ExplodedGraph &G, |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 319 | BugReporter &BR, |
Tom Care | bc42c53 | 2010-08-03 01:55:07 +0000 | [diff] [blame] | 320 | GRExprEngine &Eng) { |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 321 | // Iterate over the hash to see if we have any paths with definite |
| 322 | // idempotent operations. |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 323 | for (AssumptionMap::const_iterator i = hash.begin(); i != hash.end(); ++i) { |
| 324 | // Unpack the hash contents |
| 325 | const std::pair<Assumption, AnalysisContext *> &Data = i->second; |
| 326 | const Assumption &A = Data.first; |
| 327 | AnalysisContext *AC = Data.second; |
Ted Kremenek | 3e5637f | 2010-07-27 18:49:08 +0000 | [diff] [blame] | 328 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 329 | const BinaryOperator *B = i->first; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 330 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 331 | if (A == Impossible) |
| 332 | continue; |
| 333 | |
| 334 | // If the analyzer did not finish, check to see if we can still emit this |
| 335 | // warning |
| 336 | if (Eng.hasWorkRemaining()) { |
| 337 | const CFGStmtMap *CBM = CFGStmtMap::Build(AC->getCFG(), |
| 338 | &AC->getParentMap()); |
| 339 | |
| 340 | // If we can trace back |
| 341 | if (!PathWasCompletelyAnalyzed(AC->getCFG(), |
| 342 | CBM->getBlock(B), |
| 343 | Eng.getCoreEngine())) |
| 344 | continue; |
| 345 | |
| 346 | delete CBM; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 347 | } |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 348 | |
| 349 | // Select the error message. |
| 350 | llvm::SmallString<128> buf; |
| 351 | llvm::raw_svector_ostream os(buf); |
| 352 | switch (A) { |
| 353 | case Equal: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 354 | if (B->getOpcode() == BO_Assign) |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 355 | os << "Assigned value is always the same as the existing value"; |
| 356 | else |
| 357 | os << "Both operands to '" << B->getOpcodeStr() |
| 358 | << "' always have the same value"; |
| 359 | break; |
| 360 | case LHSis1: |
| 361 | os << "The left operand to '" << B->getOpcodeStr() << "' is always 1"; |
| 362 | break; |
| 363 | case RHSis1: |
| 364 | os << "The right operand to '" << B->getOpcodeStr() << "' is always 1"; |
| 365 | break; |
| 366 | case LHSis0: |
| 367 | os << "The left operand to '" << B->getOpcodeStr() << "' is always 0"; |
| 368 | break; |
| 369 | case RHSis0: |
| 370 | os << "The right operand to '" << B->getOpcodeStr() << "' is always 0"; |
| 371 | break; |
| 372 | case Possible: |
| 373 | llvm_unreachable("Operation was never marked with an assumption"); |
| 374 | case Impossible: |
| 375 | llvm_unreachable(0); |
| 376 | } |
| 377 | |
| 378 | // Create the SourceRange Arrays |
| 379 | SourceRange S[2] = { i->first->getLHS()->getSourceRange(), |
| 380 | i->first->getRHS()->getSourceRange() }; |
| 381 | BR.EmitBasicReport("Idempotent operation", "Dead code", |
| 382 | os.str(), i->first->getOperatorLoc(), S, 2); |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | |
| 386 | // Updates the current assumption given the new assumption |
| 387 | inline void IdempotentOperationChecker::UpdateAssumption(Assumption &A, |
| 388 | const Assumption &New) { |
Tom Care | d8421ed | 2010-08-27 22:35:28 +0000 | [diff] [blame^] | 389 | // If the assumption is the same, there is nothing to do |
| 390 | if (A == New) |
| 391 | return; |
| 392 | |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 393 | switch (A) { |
| 394 | // If we don't currently have an assumption, set it |
| 395 | case Possible: |
| 396 | A = New; |
| 397 | return; |
| 398 | |
| 399 | // If we have determined that a valid state happened, ignore the new |
| 400 | // assumption. |
| 401 | case Impossible: |
| 402 | return; |
| 403 | |
| 404 | // Any other case means that we had a different assumption last time. We don't |
| 405 | // currently support mixing assumptions for diagnostic reasons, so we set |
| 406 | // our assumption to be impossible. |
| 407 | default: |
| 408 | A = Impossible; |
| 409 | return; |
| 410 | } |
| 411 | } |
| 412 | |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 413 | // Check for a statement where a variable is self assigned to avoid an unused |
| 414 | // variable warning. We still report if the variable is used after the self |
| 415 | // assignment. |
| 416 | bool IdempotentOperationChecker::isUnusedSelfAssign(const Expr *LHS, |
| 417 | const Expr *RHS, |
| 418 | AnalysisContext *AC) { |
Tom Care | df4ca42 | 2010-07-16 20:41:41 +0000 | [diff] [blame] | 419 | LHS = LHS->IgnoreParenCasts(); |
| 420 | RHS = RHS->IgnoreParenCasts(); |
| 421 | |
| 422 | const DeclRefExpr *LHS_DR = dyn_cast<DeclRefExpr>(LHS); |
| 423 | if (!LHS_DR) |
| 424 | return false; |
| 425 | |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 426 | const VarDecl *VD = dyn_cast<VarDecl>(LHS_DR->getDecl()); |
| 427 | if (!VD) |
Tom Care | df4ca42 | 2010-07-16 20:41:41 +0000 | [diff] [blame] | 428 | return false; |
| 429 | |
| 430 | const DeclRefExpr *RHS_DR = dyn_cast<DeclRefExpr>(RHS); |
| 431 | if (!RHS_DR) |
| 432 | return false; |
| 433 | |
Tom Care | ef52bcb | 2010-08-24 21:09:07 +0000 | [diff] [blame] | 434 | if (VD != RHS_DR->getDecl()) |
| 435 | return false; |
| 436 | |
| 437 | // If the var was used outside of a selfassign, then we should still report |
| 438 | if (AC->getPseudoConstantAnalysis()->wasReferenced(VD)) |
| 439 | return false; |
| 440 | |
| 441 | return true; |
Tom Care | df4ca42 | 2010-07-16 20:41:41 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // Check for self casts truncating/extending a variable |
| 445 | bool IdempotentOperationChecker::isTruncationExtensionAssignment( |
| 446 | const Expr *LHS, |
| 447 | const Expr *RHS) { |
| 448 | |
| 449 | const DeclRefExpr *LHS_DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParenCasts()); |
| 450 | if (!LHS_DR) |
| 451 | return false; |
| 452 | |
| 453 | const VarDecl *VD = dyn_cast<VarDecl>(LHS_DR->getDecl()); |
| 454 | if (!VD) |
| 455 | return false; |
| 456 | |
| 457 | const DeclRefExpr *RHS_DR = dyn_cast<DeclRefExpr>(RHS->IgnoreParenCasts()); |
| 458 | if (!RHS_DR) |
| 459 | return false; |
| 460 | |
| 461 | if (VD != RHS_DR->getDecl()) |
| 462 | return false; |
| 463 | |
| 464 | return dyn_cast<DeclRefExpr>(RHS->IgnoreParens()) == NULL; |
| 465 | } |
| 466 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 467 | // Returns false if a path to this block was not completely analyzed, or true |
| 468 | // otherwise. |
| 469 | bool IdempotentOperationChecker::PathWasCompletelyAnalyzed( |
| 470 | const CFG *C, |
| 471 | const CFGBlock *CB, |
| 472 | const GRCoreEngine &CE) { |
| 473 | std::deque<const CFGBlock *> WorkList; |
| 474 | llvm::SmallSet<unsigned, 8> Aborted; |
| 475 | llvm::SmallSet<unsigned, 128> Visited; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 476 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 477 | // Create a set of all aborted blocks |
| 478 | typedef GRCoreEngine::BlocksAborted::const_iterator AbortedIterator; |
| 479 | for (AbortedIterator I = CE.blocks_aborted_begin(), |
| 480 | E = CE.blocks_aborted_end(); I != E; ++I) { |
| 481 | const BlockEdge &BE = I->first; |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 482 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 483 | // The destination block on the BlockEdge is the first block that was not |
| 484 | // analyzed. |
| 485 | Aborted.insert(BE.getDst()->getBlockID()); |
Ted Kremenek | 4532931 | 2010-07-17 00:40:32 +0000 | [diff] [blame] | 486 | } |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 487 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 488 | // Save the entry block ID for early exiting |
| 489 | unsigned EntryBlockID = C->getEntry().getBlockID(); |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 490 | |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 491 | // Create initial node |
| 492 | WorkList.push_back(CB); |
| 493 | |
| 494 | while (!WorkList.empty()) { |
| 495 | const CFGBlock *Head = WorkList.front(); |
| 496 | WorkList.pop_front(); |
| 497 | Visited.insert(Head->getBlockID()); |
| 498 | |
| 499 | // If we found the entry block, then there exists a path from the target |
| 500 | // node to the entry point of this function -> the path was completely |
| 501 | // analyzed. |
| 502 | if (Head->getBlockID() == EntryBlockID) |
| 503 | return true; |
| 504 | |
| 505 | // If any of the aborted blocks are on the path to the beginning, then all |
| 506 | // paths to this block were not analyzed. |
| 507 | if (Aborted.count(Head->getBlockID())) |
| 508 | return false; |
| 509 | |
| 510 | // Add the predecessors to the worklist unless we have already visited them |
| 511 | for (CFGBlock::const_pred_iterator I = Head->pred_begin(); |
| 512 | I != Head->pred_end(); ++I) |
| 513 | if (!Visited.count((*I)->getBlockID())) |
| 514 | WorkList.push_back(*I); |
| 515 | } |
| 516 | |
| 517 | // If we get to this point, there is no connection to the entry block or an |
| 518 | // aborted block. This path is unreachable and we can report the error. |
| 519 | return true; |
| 520 | } |
| 521 | |
| 522 | // Recursive function that determines whether an expression contains any element |
| 523 | // that varies. This could be due to a compile-time constant like sizeof. An |
| 524 | // expression may also involve a variable that behaves like a constant. The |
| 525 | // function returns true if the expression varies, and false otherwise. |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 526 | bool IdempotentOperationChecker::CanVary(const Expr *Ex, |
| 527 | AnalysisContext *AC) { |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 528 | // Parentheses and casts are irrelevant here |
| 529 | Ex = Ex->IgnoreParenCasts(); |
| 530 | |
| 531 | if (Ex->getLocStart().isMacroID()) |
| 532 | return false; |
| 533 | |
| 534 | switch (Ex->getStmtClass()) { |
| 535 | // Trivially true cases |
| 536 | case Stmt::ArraySubscriptExprClass: |
| 537 | case Stmt::MemberExprClass: |
| 538 | case Stmt::StmtExprClass: |
| 539 | case Stmt::CallExprClass: |
| 540 | case Stmt::VAArgExprClass: |
| 541 | case Stmt::ShuffleVectorExprClass: |
| 542 | return true; |
| 543 | default: |
| 544 | return true; |
| 545 | |
| 546 | // Trivially false cases |
| 547 | case Stmt::IntegerLiteralClass: |
| 548 | case Stmt::CharacterLiteralClass: |
| 549 | case Stmt::FloatingLiteralClass: |
| 550 | case Stmt::PredefinedExprClass: |
| 551 | case Stmt::ImaginaryLiteralClass: |
| 552 | case Stmt::StringLiteralClass: |
| 553 | case Stmt::OffsetOfExprClass: |
| 554 | case Stmt::CompoundLiteralExprClass: |
| 555 | case Stmt::AddrLabelExprClass: |
| 556 | case Stmt::TypesCompatibleExprClass: |
| 557 | case Stmt::GNUNullExprClass: |
| 558 | case Stmt::InitListExprClass: |
| 559 | case Stmt::DesignatedInitExprClass: |
| 560 | case Stmt::BlockExprClass: |
| 561 | case Stmt::BlockDeclRefExprClass: |
| 562 | return false; |
| 563 | |
| 564 | // Cases requiring custom logic |
| 565 | case Stmt::SizeOfAlignOfExprClass: { |
| 566 | const SizeOfAlignOfExpr *SE = cast<const SizeOfAlignOfExpr>(Ex); |
| 567 | if (!SE->isSizeOf()) |
| 568 | return false; |
| 569 | return SE->getTypeOfArgument()->isVariableArrayType(); |
| 570 | } |
| 571 | case Stmt::DeclRefExprClass: |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 572 | return !isConstantOrPseudoConstant(cast<DeclRefExpr>(Ex), AC); |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 573 | |
| 574 | // The next cases require recursion for subexpressions |
| 575 | case Stmt::BinaryOperatorClass: { |
| 576 | const BinaryOperator *B = cast<const BinaryOperator>(Ex); |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 577 | return CanVary(B->getRHS(), AC) |
| 578 | || CanVary(B->getLHS(), AC); |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 579 | } |
| 580 | case Stmt::UnaryOperatorClass: { |
| 581 | const UnaryOperator *U = cast<const UnaryOperator>(Ex); |
Eli Friedman | de7e662 | 2010-08-13 01:36:11 +0000 | [diff] [blame] | 582 | // Handle trivial case first |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 583 | switch (U->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 584 | case UO_Extension: |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 585 | return false; |
| 586 | default: |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 587 | return CanVary(U->getSubExpr(), AC); |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 588 | } |
| 589 | } |
| 590 | case Stmt::ChooseExprClass: |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 591 | return CanVary(cast<const ChooseExpr>(Ex)->getChosenSubExpr( |
| 592 | AC->getASTContext()), AC); |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 593 | case Stmt::ConditionalOperatorClass: |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 594 | return CanVary(cast<const ConditionalOperator>(Ex)->getCond(), AC); |
Tom Care | a7a8a45 | 2010-08-12 22:45:47 +0000 | [diff] [blame] | 595 | } |
Tom Care | db2fa8a | 2010-07-06 21:43:29 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 598 | // Returns true if a DeclRefExpr is or behaves like a constant. |
| 599 | bool IdempotentOperationChecker::isConstantOrPseudoConstant( |
| 600 | const DeclRefExpr *DR, |
| 601 | AnalysisContext *AC) { |
| 602 | // Check if the type of the Decl is const-qualified |
| 603 | if (DR->getType().isConstQualified()) |
| 604 | return true; |
| 605 | |
Tom Care | 50e8ac2 | 2010-08-16 21:43:52 +0000 | [diff] [blame] | 606 | // Check for an enum |
| 607 | if (isa<EnumConstantDecl>(DR->getDecl())) |
| 608 | return true; |
| 609 | |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 610 | const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()); |
| 611 | if (!VD) |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 612 | return true; |
| 613 | |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 614 | // Check if the Decl behaves like a constant. This check also takes care of |
| 615 | // static variables, which can only change between function calls if they are |
| 616 | // modified in the AST. |
| 617 | PseudoConstantAnalysis *PCA = AC->getPseudoConstantAnalysis(); |
| 618 | if (PCA->isPseudoConstant(VD)) |
| 619 | return true; |
| 620 | |
| 621 | return false; |
| 622 | } |
| 623 | |
| 624 | // Recursively find any substatements containing VarDecl's with storage other |
| 625 | // than local |
| 626 | bool IdempotentOperationChecker::containsNonLocalVarDecl(const Stmt *S) { |
| 627 | const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S); |
| 628 | |
| 629 | if (DR) |
| 630 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) |
| 631 | if (!VD->hasLocalStorage()) |
| 632 | return true; |
| 633 | |
| 634 | for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end(); |
| 635 | ++I) |
| 636 | if (const Stmt *child = *I) |
| 637 | if (containsNonLocalVarDecl(child)) |
| 638 | return true; |
| 639 | |
Tom Care | 50e8ac2 | 2010-08-16 21:43:52 +0000 | [diff] [blame] | 640 | return false; |
| 641 | } |