blob: 92ce2c313e80a678e08811d4dccd6c30c7975e65 [file] [log] [blame]
Tom Caredb2fa8a2010-07-06 21:43:29 +00001//==- 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 Carea7a8a452010-08-12 22:45:47 +000039// Things TODO:
Tom Caredb2fa8a2010-07-06 21:43:29 +000040// - Improved error messages
41// - Handle mixed assumptions (which assumptions can belong together?)
42// - Finer grained false positive control (levels)
Tom Carea7a8a452010-08-12 22:45:47 +000043// - Handling ~0 values
Tom Caredb2fa8a2010-07-06 21:43:29 +000044
Argyrios Kyrtzidisaf1a9332011-02-08 22:30:11 +000045#include "ExperimentalChecks.h"
Tom Carea7a8a452010-08-12 22:45:47 +000046#include "clang/Analysis/CFGStmtMap.h"
Tom Caredb34ab72010-08-23 19:51:57 +000047#include "clang/Analysis/Analyses/PseudoConstantAnalysis.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000048#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
49#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
50#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
51#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerVisitor.h"
52#include "clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h"
53#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
Tom Caredb2fa8a2010-07-06 21:43:29 +000054#include "clang/AST/Stmt.h"
55#include "llvm/ADT/DenseMap.h"
Tom Carea7a8a452010-08-12 22:45:47 +000056#include "llvm/ADT/SmallSet.h"
Ted Kremenek8e376772011-02-14 17:59:20 +000057#include "llvm/ADT/BitVector.h"
Chandler Carruth256565b2010-07-07 00:07:37 +000058#include "llvm/Support/ErrorHandling.h"
Tom Carea7a8a452010-08-12 22:45:47 +000059#include <deque>
Tom Caredb2fa8a2010-07-06 21:43:29 +000060
61using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000062using namespace ento;
Tom Caredb2fa8a2010-07-06 21:43:29 +000063
64namespace {
65class IdempotentOperationChecker
66 : public CheckerVisitor<IdempotentOperationChecker> {
Tom Careb0627952010-09-09 02:04:52 +000067public:
68 static void *getTag();
69 void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
70 void PostVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000071 void VisitEndAnalysis(ExplodedGraph &G, BugReporter &B, ExprEngine &Eng);
Tom Careb0627952010-09-09 02:04:52 +000072
73private:
74 // Our assumption about a particular operation.
75 enum Assumption { Possible = 0, Impossible, Equal, LHSis1, RHSis1, LHSis0,
76 RHSis0 };
77
78 void UpdateAssumption(Assumption &A, const Assumption &New);
79
80 // False positive reduction methods
81 static bool isSelfAssign(const Expr *LHS, const Expr *RHS);
82 static bool isUnused(const Expr *E, AnalysisContext *AC);
83 static bool isTruncationExtensionAssignment(const Expr *LHS,
84 const Expr *RHS);
Ted Kremenek8e376772011-02-14 17:59:20 +000085 bool pathWasCompletelyAnalyzed(const CFG *cfg,
Tom Careb0627952010-09-09 02:04:52 +000086 const CFGBlock *CB,
Ted Kremenek33d46262010-11-13 05:04:52 +000087 const CFGStmtMap *CBM,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000088 const CoreEngine &CE);
Tom Careb0627952010-09-09 02:04:52 +000089 static bool CanVary(const Expr *Ex,
90 AnalysisContext *AC);
91 static bool isConstantOrPseudoConstant(const DeclRefExpr *DR,
92 AnalysisContext *AC);
93 static bool containsNonLocalVarDecl(const Stmt *S);
Ted Kremenek8e376772011-02-14 17:59:20 +000094 void getLastRelevantNodes(const CFGBlock *Begin,
95 const ExplodedNode *N,
96 ExplodedNodeSet &result);
Tom Careb0627952010-09-09 02:04:52 +000097
98 // Hash table and related data structures
99 struct BinaryOperatorData {
100 BinaryOperatorData() : assumption(Possible), analysisContext(0) {}
101
102 Assumption assumption;
103 AnalysisContext *analysisContext;
104 ExplodedNodeSet explodedNodes; // Set of ExplodedNodes that refer to a
105 // BinaryOperator
106 };
107 typedef llvm::DenseMap<const BinaryOperator *, BinaryOperatorData>
108 AssumptionMap;
109 AssumptionMap hash;
110
111 // A class that performs reachability queries for CFGBlocks. Several internal
112 // checks in this checker require reachability information. The requests all
113 // tend to have a common destination, so we lazily do a predecessor search
114 // from the destination node and cache the results to prevent work
115 // duplication.
116 class CFGReachabilityAnalysis {
Ted Kremenek8e376772011-02-14 17:59:20 +0000117 typedef llvm::BitVector ReachableSet;
Tom Careb0627952010-09-09 02:04:52 +0000118 typedef llvm::DenseMap<unsigned, ReachableSet> ReachableMap;
119 ReachableSet analyzed;
120 ReachableMap reachable;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000121 public:
Ted Kremenek8e376772011-02-14 17:59:20 +0000122 CFGReachabilityAnalysis(const CFG &cfg)
123 : analyzed(cfg.getNumBlockIDs(), false) {}
124
Tom Careb0627952010-09-09 02:04:52 +0000125 inline bool isReachable(const CFGBlock *Src, const CFGBlock *Dst);
Tom Caredb2fa8a2010-07-06 21:43:29 +0000126 private:
Tom Careb0627952010-09-09 02:04:52 +0000127 void MapReachability(const CFGBlock *Dst);
128 };
Ted Kremenek8e376772011-02-14 17:59:20 +0000129 llvm::OwningPtr<CFGReachabilityAnalysis> CRA;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000130};
131}
132
133void *IdempotentOperationChecker::getTag() {
134 static int x = 0;
135 return &x;
136}
137
Ted Kremenek9ef65372010-12-23 07:20:52 +0000138void ento::RegisterIdempotentOperationChecker(ExprEngine &Eng) {
Tom Caredb2fa8a2010-07-06 21:43:29 +0000139 Eng.registerCheck(new IdempotentOperationChecker());
140}
141
142void IdempotentOperationChecker::PreVisitBinaryOperator(
143 CheckerContext &C,
144 const BinaryOperator *B) {
Ted Kremenekfe97fa12010-08-02 20:33:02 +0000145 // Find or create an entry in the hash for this BinaryOperator instance.
146 // If we haven't done a lookup before, it will get default initialized to
Tom Care2bbbe502010-09-02 23:30:22 +0000147 // 'Possible'. At this stage we do not store the ExplodedNode, as it has not
148 // been created yet.
149 BinaryOperatorData &Data = hash[B];
150 Assumption &A = Data.assumption;
Tom Care245adab2010-08-18 21:17:24 +0000151 AnalysisContext *AC = C.getCurrentAnalysisContext();
Tom Care2bbbe502010-09-02 23:30:22 +0000152 Data.analysisContext = AC;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000153
154 // If we already have visited this node on a path that does not contain an
155 // idempotent operation, return immediately.
156 if (A == Impossible)
157 return;
158
Tom Carea7a8a452010-08-12 22:45:47 +0000159 // Retrieve both sides of the operator and determine if they can vary (which
160 // may mean this is a false positive.
Tom Caredb2fa8a2010-07-06 21:43:29 +0000161 const Expr *LHS = B->getLHS();
162 const Expr *RHS = B->getRHS();
Tom Care245adab2010-08-18 21:17:24 +0000163
Tom Caredb34ab72010-08-23 19:51:57 +0000164 // At this stage we can calculate whether each side contains a false positive
165 // that applies to all operators. We only need to calculate this the first
166 // time.
167 bool LHSContainsFalsePositive = false, RHSContainsFalsePositive = false;
Tom Care245adab2010-08-18 21:17:24 +0000168 if (A == Possible) {
Tom Caredb34ab72010-08-23 19:51:57 +0000169 // An expression contains a false positive if it can't vary, or if it
170 // contains a known false positive VarDecl.
171 LHSContainsFalsePositive = !CanVary(LHS, AC)
172 || containsNonLocalVarDecl(LHS);
173 RHSContainsFalsePositive = !CanVary(RHS, AC)
174 || containsNonLocalVarDecl(RHS);
Tom Care245adab2010-08-18 21:17:24 +0000175 }
Tom Caredb2fa8a2010-07-06 21:43:29 +0000176
177 const GRState *state = C.getState();
178
179 SVal LHSVal = state->getSVal(LHS);
180 SVal RHSVal = state->getSVal(RHS);
181
182 // If either value is unknown, we can't be 100% sure of all paths.
183 if (LHSVal.isUnknownOrUndef() || RHSVal.isUnknownOrUndef()) {
184 A = Impossible;
185 return;
186 }
187 BinaryOperator::Opcode Op = B->getOpcode();
188
189 // Dereference the LHS SVal if this is an assign operation
190 switch (Op) {
191 default:
192 break;
193
194 // Fall through intentional
John McCall2de56d12010-08-25 11:45:40 +0000195 case BO_AddAssign:
196 case BO_SubAssign:
197 case BO_MulAssign:
198 case BO_DivAssign:
199 case BO_AndAssign:
200 case BO_OrAssign:
201 case BO_XorAssign:
202 case BO_ShlAssign:
203 case BO_ShrAssign:
204 case BO_Assign:
Tom Caredb2fa8a2010-07-06 21:43:29 +0000205 // Assign statements have one extra level of indirection
206 if (!isa<Loc>(LHSVal)) {
207 A = Impossible;
208 return;
209 }
Ted Kremenek96ebad62010-09-09 07:13:00 +0000210 LHSVal = state->getSVal(cast<Loc>(LHSVal), LHS->getType());
Tom Caredb2fa8a2010-07-06 21:43:29 +0000211 }
212
213
214 // We now check for various cases which result in an idempotent operation.
215
216 // x op x
217 switch (Op) {
218 default:
219 break; // We don't care about any other operators.
220
221 // Fall through intentional
John McCall2de56d12010-08-25 11:45:40 +0000222 case BO_Assign:
Tom Care6216dc02010-08-30 19:25:43 +0000223 // x Assign x can be used to silence unused variable warnings intentionally.
224 // If this is a self assignment and the variable is referenced elsewhere,
Tom Care84c24ed2010-09-07 20:27:56 +0000225 // and the assignment is not a truncation or extension, then it is a false
226 // positive.
Tom Care6216dc02010-08-30 19:25:43 +0000227 if (isSelfAssign(LHS, RHS)) {
Tom Care84c24ed2010-09-07 20:27:56 +0000228 if (!isUnused(LHS, AC) && !isTruncationExtensionAssignment(LHS, RHS)) {
Tom Care6216dc02010-08-30 19:25:43 +0000229 UpdateAssumption(A, Equal);
230 return;
231 }
232 else {
233 A = Impossible;
234 return;
235 }
Tom Caredf4ca422010-07-16 20:41:41 +0000236 }
237
John McCall2de56d12010-08-25 11:45:40 +0000238 case BO_SubAssign:
239 case BO_DivAssign:
240 case BO_AndAssign:
241 case BO_OrAssign:
242 case BO_XorAssign:
243 case BO_Sub:
244 case BO_Div:
245 case BO_And:
246 case BO_Or:
247 case BO_Xor:
248 case BO_LOr:
249 case BO_LAnd:
Tom Care9edd4d02010-08-27 22:50:47 +0000250 case BO_EQ:
251 case BO_NE:
Tom Caredb34ab72010-08-23 19:51:57 +0000252 if (LHSVal != RHSVal || LHSContainsFalsePositive
253 || RHSContainsFalsePositive)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000254 break;
255 UpdateAssumption(A, Equal);
256 return;
257 }
258
259 // x op 1
260 switch (Op) {
261 default:
262 break; // We don't care about any other operators.
263
264 // Fall through intentional
John McCall2de56d12010-08-25 11:45:40 +0000265 case BO_MulAssign:
266 case BO_DivAssign:
267 case BO_Mul:
268 case BO_Div:
269 case BO_LOr:
270 case BO_LAnd:
Tom Caredb34ab72010-08-23 19:51:57 +0000271 if (!RHSVal.isConstant(1) || RHSContainsFalsePositive)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000272 break;
273 UpdateAssumption(A, RHSis1);
274 return;
275 }
276
277 // 1 op x
278 switch (Op) {
279 default:
280 break; // We don't care about any other operators.
281
282 // Fall through intentional
John McCall2de56d12010-08-25 11:45:40 +0000283 case BO_MulAssign:
284 case BO_Mul:
285 case BO_LOr:
286 case BO_LAnd:
Tom Caredb34ab72010-08-23 19:51:57 +0000287 if (!LHSVal.isConstant(1) || LHSContainsFalsePositive)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000288 break;
289 UpdateAssumption(A, LHSis1);
290 return;
291 }
292
293 // x op 0
294 switch (Op) {
295 default:
296 break; // We don't care about any other operators.
297
298 // Fall through intentional
John McCall2de56d12010-08-25 11:45:40 +0000299 case BO_AddAssign:
300 case BO_SubAssign:
301 case BO_MulAssign:
302 case BO_AndAssign:
303 case BO_OrAssign:
304 case BO_XorAssign:
305 case BO_Add:
306 case BO_Sub:
307 case BO_Mul:
308 case BO_And:
309 case BO_Or:
310 case BO_Xor:
311 case BO_Shl:
312 case BO_Shr:
313 case BO_LOr:
314 case BO_LAnd:
Tom Caredb34ab72010-08-23 19:51:57 +0000315 if (!RHSVal.isConstant(0) || RHSContainsFalsePositive)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000316 break;
317 UpdateAssumption(A, RHSis0);
318 return;
319 }
320
321 // 0 op x
322 switch (Op) {
323 default:
324 break; // We don't care about any other operators.
325
326 // Fall through intentional
John McCall2de56d12010-08-25 11:45:40 +0000327 //case BO_AddAssign: // Common false positive
328 case BO_SubAssign: // Check only if unsigned
329 case BO_MulAssign:
330 case BO_DivAssign:
331 case BO_AndAssign:
332 //case BO_OrAssign: // Common false positive
333 //case BO_XorAssign: // Common false positive
334 case BO_ShlAssign:
335 case BO_ShrAssign:
336 case BO_Add:
337 case BO_Sub:
338 case BO_Mul:
339 case BO_Div:
340 case BO_And:
341 case BO_Or:
342 case BO_Xor:
343 case BO_Shl:
344 case BO_Shr:
345 case BO_LOr:
346 case BO_LAnd:
Tom Caredb34ab72010-08-23 19:51:57 +0000347 if (!LHSVal.isConstant(0) || LHSContainsFalsePositive)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000348 break;
349 UpdateAssumption(A, LHSis0);
350 return;
351 }
352
353 // If we get to this point, there has been a valid use of this operation.
354 A = Impossible;
355}
356
Tom Care2bbbe502010-09-02 23:30:22 +0000357// At the post visit stage, the predecessor ExplodedNode will be the
358// BinaryOperator that was just created. We use this hook to collect the
359// ExplodedNode.
360void IdempotentOperationChecker::PostVisitBinaryOperator(
361 CheckerContext &C,
362 const BinaryOperator *B) {
363 // Add the ExplodedNode we just visited
364 BinaryOperatorData &Data = hash[B];
Ted Kremenek020c3742011-02-12 18:50:03 +0000365
366 const Stmt *predStmt
367 = cast<StmtPoint>(C.getPredecessor()->getLocation()).getStmt();
368
369 // Ignore implicit calls to setters.
370 if (isa<ObjCPropertyRefExpr>(predStmt))
371 return;
372
373 assert(isa<BinaryOperator>(predStmt));
Tom Care2bbbe502010-09-02 23:30:22 +0000374 Data.explodedNodes.Add(C.getPredecessor());
375}
376
Tom Caredb2fa8a2010-07-06 21:43:29 +0000377void IdempotentOperationChecker::VisitEndAnalysis(ExplodedGraph &G,
Ted Kremenek3e5637f2010-07-27 18:49:08 +0000378 BugReporter &BR,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000379 ExprEngine &Eng) {
Tom Care2bbbe502010-09-02 23:30:22 +0000380 BugType *BT = new BugType("Idempotent operation", "Dead code");
Tom Caredb2fa8a2010-07-06 21:43:29 +0000381 // Iterate over the hash to see if we have any paths with definite
382 // idempotent operations.
Tom Carea7a8a452010-08-12 22:45:47 +0000383 for (AssumptionMap::const_iterator i = hash.begin(); i != hash.end(); ++i) {
384 // Unpack the hash contents
Tom Care2bbbe502010-09-02 23:30:22 +0000385 const BinaryOperatorData &Data = i->second;
386 const Assumption &A = Data.assumption;
387 AnalysisContext *AC = Data.analysisContext;
388 const ExplodedNodeSet &ES = Data.explodedNodes;
Ted Kremenek3e5637f2010-07-27 18:49:08 +0000389
Tom Carea7a8a452010-08-12 22:45:47 +0000390 const BinaryOperator *B = i->first;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000391
Tom Carea7a8a452010-08-12 22:45:47 +0000392 if (A == Impossible)
393 continue;
394
395 // If the analyzer did not finish, check to see if we can still emit this
396 // warning
397 if (Eng.hasWorkRemaining()) {
398 const CFGStmtMap *CBM = CFGStmtMap::Build(AC->getCFG(),
399 &AC->getParentMap());
400
401 // If we can trace back
Ted Kremenek8e376772011-02-14 17:59:20 +0000402 if (!pathWasCompletelyAnalyzed(AC->getCFG(),
Ted Kremenek33d46262010-11-13 05:04:52 +0000403 CBM->getBlock(B), CBM,
Tom Carea7a8a452010-08-12 22:45:47 +0000404 Eng.getCoreEngine()))
405 continue;
406
407 delete CBM;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000408 }
Tom Carea7a8a452010-08-12 22:45:47 +0000409
Tom Care2bbbe502010-09-02 23:30:22 +0000410 // Select the error message and SourceRanges to report.
Tom Carea7a8a452010-08-12 22:45:47 +0000411 llvm::SmallString<128> buf;
412 llvm::raw_svector_ostream os(buf);
Tom Care2bbbe502010-09-02 23:30:22 +0000413 bool LHSRelevant = false, RHSRelevant = false;
Tom Carea7a8a452010-08-12 22:45:47 +0000414 switch (A) {
415 case Equal:
Tom Care2bbbe502010-09-02 23:30:22 +0000416 LHSRelevant = true;
417 RHSRelevant = true;
John McCall2de56d12010-08-25 11:45:40 +0000418 if (B->getOpcode() == BO_Assign)
Tom Carea7a8a452010-08-12 22:45:47 +0000419 os << "Assigned value is always the same as the existing value";
420 else
421 os << "Both operands to '" << B->getOpcodeStr()
422 << "' always have the same value";
423 break;
424 case LHSis1:
Tom Care2bbbe502010-09-02 23:30:22 +0000425 LHSRelevant = true;
Tom Carea7a8a452010-08-12 22:45:47 +0000426 os << "The left operand to '" << B->getOpcodeStr() << "' is always 1";
427 break;
428 case RHSis1:
Tom Care2bbbe502010-09-02 23:30:22 +0000429 RHSRelevant = true;
Tom Carea7a8a452010-08-12 22:45:47 +0000430 os << "The right operand to '" << B->getOpcodeStr() << "' is always 1";
431 break;
432 case LHSis0:
Tom Care2bbbe502010-09-02 23:30:22 +0000433 LHSRelevant = true;
Tom Carea7a8a452010-08-12 22:45:47 +0000434 os << "The left operand to '" << B->getOpcodeStr() << "' is always 0";
435 break;
436 case RHSis0:
Tom Care2bbbe502010-09-02 23:30:22 +0000437 RHSRelevant = true;
Tom Carea7a8a452010-08-12 22:45:47 +0000438 os << "The right operand to '" << B->getOpcodeStr() << "' is always 0";
439 break;
440 case Possible:
441 llvm_unreachable("Operation was never marked with an assumption");
442 case Impossible:
443 llvm_unreachable(0);
444 }
445
Tom Care2bbbe502010-09-02 23:30:22 +0000446 // Add a report for each ExplodedNode
447 for (ExplodedNodeSet::iterator I = ES.begin(), E = ES.end(); I != E; ++I) {
448 EnhancedBugReport *report = new EnhancedBugReport(*BT, os.str(), *I);
449
450 // Add source ranges and visitor hooks
451 if (LHSRelevant) {
452 const Expr *LHS = i->first->getLHS();
453 report->addRange(LHS->getSourceRange());
454 report->addVisitorCreator(bugreporter::registerVarDeclsLastStore, LHS);
455 }
456 if (RHSRelevant) {
457 const Expr *RHS = i->first->getRHS();
458 report->addRange(i->first->getRHS()->getSourceRange());
459 report->addVisitorCreator(bugreporter::registerVarDeclsLastStore, RHS);
460 }
461
462 BR.EmitReport(report);
463 }
Tom Caredb2fa8a2010-07-06 21:43:29 +0000464 }
465}
466
467// Updates the current assumption given the new assumption
468inline void IdempotentOperationChecker::UpdateAssumption(Assumption &A,
469 const Assumption &New) {
Tom Cared8421ed2010-08-27 22:35:28 +0000470// If the assumption is the same, there is nothing to do
471 if (A == New)
472 return;
473
Tom Caredb2fa8a2010-07-06 21:43:29 +0000474 switch (A) {
475 // If we don't currently have an assumption, set it
476 case Possible:
477 A = New;
478 return;
479
480 // If we have determined that a valid state happened, ignore the new
481 // assumption.
482 case Impossible:
483 return;
484
485 // Any other case means that we had a different assumption last time. We don't
486 // currently support mixing assumptions for diagnostic reasons, so we set
487 // our assumption to be impossible.
488 default:
489 A = Impossible;
490 return;
491 }
492}
493
Tom Care6216dc02010-08-30 19:25:43 +0000494// Check for a statement where a variable is self assigned to possibly avoid an
495// unused variable warning.
496bool IdempotentOperationChecker::isSelfAssign(const Expr *LHS, const Expr *RHS) {
Tom Caredf4ca422010-07-16 20:41:41 +0000497 LHS = LHS->IgnoreParenCasts();
498 RHS = RHS->IgnoreParenCasts();
499
500 const DeclRefExpr *LHS_DR = dyn_cast<DeclRefExpr>(LHS);
501 if (!LHS_DR)
502 return false;
503
Tom Careef52bcb2010-08-24 21:09:07 +0000504 const VarDecl *VD = dyn_cast<VarDecl>(LHS_DR->getDecl());
505 if (!VD)
Tom Caredf4ca422010-07-16 20:41:41 +0000506 return false;
507
508 const DeclRefExpr *RHS_DR = dyn_cast<DeclRefExpr>(RHS);
509 if (!RHS_DR)
510 return false;
511
Tom Careef52bcb2010-08-24 21:09:07 +0000512 if (VD != RHS_DR->getDecl())
513 return false;
514
Tom Care6216dc02010-08-30 19:25:43 +0000515 return true;
516}
517
518// Returns true if the Expr points to a VarDecl that is not read anywhere
519// outside of self-assignments.
520bool IdempotentOperationChecker::isUnused(const Expr *E,
521 AnalysisContext *AC) {
522 if (!E)
523 return false;
524
525 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts());
526 if (!DR)
527 return false;
528
529 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
530 if (!VD)
531 return false;
532
Tom Careef52bcb2010-08-24 21:09:07 +0000533 if (AC->getPseudoConstantAnalysis()->wasReferenced(VD))
534 return false;
535
536 return true;
Tom Caredf4ca422010-07-16 20:41:41 +0000537}
538
539// Check for self casts truncating/extending a variable
540bool IdempotentOperationChecker::isTruncationExtensionAssignment(
541 const Expr *LHS,
542 const Expr *RHS) {
543
544 const DeclRefExpr *LHS_DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParenCasts());
545 if (!LHS_DR)
546 return false;
547
548 const VarDecl *VD = dyn_cast<VarDecl>(LHS_DR->getDecl());
549 if (!VD)
550 return false;
551
552 const DeclRefExpr *RHS_DR = dyn_cast<DeclRefExpr>(RHS->IgnoreParenCasts());
553 if (!RHS_DR)
554 return false;
555
556 if (VD != RHS_DR->getDecl())
557 return false;
558
John McCallf6a16482010-12-04 03:47:34 +0000559 return dyn_cast<DeclRefExpr>(RHS->IgnoreParenLValueCasts()) == NULL;
Tom Caredf4ca422010-07-16 20:41:41 +0000560}
561
Tom Carea7a8a452010-08-12 22:45:47 +0000562// Returns false if a path to this block was not completely analyzed, or true
563// otherwise.
Ted Kremenek8e376772011-02-14 17:59:20 +0000564bool
565IdempotentOperationChecker::pathWasCompletelyAnalyzed(const CFG *cfg,
566 const CFGBlock *CB,
567 const CFGStmtMap *CBM,
568 const CoreEngine &CE) {
569
570 CRA.reset(new CFGReachabilityAnalysis(*cfg));
571
Tom Careb0627952010-09-09 02:04:52 +0000572 // Test for reachability from any aborted blocks to this block
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000573 typedef CoreEngine::BlocksAborted::const_iterator AbortedIterator;
Tom Carea7a8a452010-08-12 22:45:47 +0000574 for (AbortedIterator I = CE.blocks_aborted_begin(),
575 E = CE.blocks_aborted_end(); I != E; ++I) {
576 const BlockEdge &BE = I->first;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000577
Tom Carea7a8a452010-08-12 22:45:47 +0000578 // The destination block on the BlockEdge is the first block that was not
Tom Careb0627952010-09-09 02:04:52 +0000579 // analyzed. If we can reach this block from the aborted block, then this
580 // block was not completely analyzed.
Ted Kremenek8e376772011-02-14 17:59:20 +0000581 if (CRA->isReachable(BE.getDst(), CB))
Tom Carea7a8a452010-08-12 22:45:47 +0000582 return false;
Tom Carea7a8a452010-08-12 22:45:47 +0000583 }
Ted Kremenek33d46262010-11-13 05:04:52 +0000584
585 // For the items still on the worklist, see if they are in blocks that
586 // can eventually reach 'CB'.
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000587 class VisitWL : public WorkList::Visitor {
Ted Kremenek33d46262010-11-13 05:04:52 +0000588 const CFGStmtMap *CBM;
589 const CFGBlock *TargetBlock;
590 CFGReachabilityAnalysis &CRA;
591 public:
592 VisitWL(const CFGStmtMap *cbm, const CFGBlock *targetBlock,
593 CFGReachabilityAnalysis &cra)
594 : CBM(cbm), TargetBlock(targetBlock), CRA(cra) {}
Ted Kremenek55825aa2011-01-11 02:34:50 +0000595 virtual bool visit(const WorkListUnit &U) {
Ted Kremenek33d46262010-11-13 05:04:52 +0000596 ProgramPoint P = U.getNode()->getLocation();
597 const CFGBlock *B = 0;
598 if (StmtPoint *SP = dyn_cast<StmtPoint>(&P)) {
599 B = CBM->getBlock(SP->getStmt());
600 }
Ted Kremeneked023662010-11-13 05:12:26 +0000601 else if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
602 B = BE->getDst();
603 }
604 else if (BlockEntrance *BEnt = dyn_cast<BlockEntrance>(&P)) {
605 B = BEnt->getBlock();
606 }
607 else if (BlockExit *BExit = dyn_cast<BlockExit>(&P)) {
608 B = BExit->getBlock();
609 }
Ted Kremenek33d46262010-11-13 05:04:52 +0000610 if (!B)
611 return true;
612
613 return CRA.isReachable(B, TargetBlock);
614 }
615 };
Ted Kremenek8e376772011-02-14 17:59:20 +0000616 VisitWL visitWL(CBM, CB, *CRA.get());
Ted Kremenek33d46262010-11-13 05:04:52 +0000617 // Were there any items in the worklist that could potentially reach
618 // this block?
Ted Kremenek55825aa2011-01-11 02:34:50 +0000619 if (CE.getWorkList()->visitItemsInWorkList(visitWL))
Ted Kremenek33d46262010-11-13 05:04:52 +0000620 return false;
Tom Carea7a8a452010-08-12 22:45:47 +0000621
Tom Careb0627952010-09-09 02:04:52 +0000622 // Verify that this block is reachable from the entry block
Ted Kremenek8e376772011-02-14 17:59:20 +0000623 if (!CRA->isReachable(&cfg->getEntry(), CB))
Tom Careb0627952010-09-09 02:04:52 +0000624 return false;
625
Tom Carea7a8a452010-08-12 22:45:47 +0000626 // If we get to this point, there is no connection to the entry block or an
627 // aborted block. This path is unreachable and we can report the error.
628 return true;
629}
630
631// Recursive function that determines whether an expression contains any element
632// that varies. This could be due to a compile-time constant like sizeof. An
633// expression may also involve a variable that behaves like a constant. The
634// function returns true if the expression varies, and false otherwise.
Tom Care245adab2010-08-18 21:17:24 +0000635bool IdempotentOperationChecker::CanVary(const Expr *Ex,
636 AnalysisContext *AC) {
Tom Carea7a8a452010-08-12 22:45:47 +0000637 // Parentheses and casts are irrelevant here
638 Ex = Ex->IgnoreParenCasts();
639
640 if (Ex->getLocStart().isMacroID())
641 return false;
642
643 switch (Ex->getStmtClass()) {
644 // Trivially true cases
645 case Stmt::ArraySubscriptExprClass:
646 case Stmt::MemberExprClass:
647 case Stmt::StmtExprClass:
648 case Stmt::CallExprClass:
649 case Stmt::VAArgExprClass:
650 case Stmt::ShuffleVectorExprClass:
651 return true;
652 default:
653 return true;
654
655 // Trivially false cases
656 case Stmt::IntegerLiteralClass:
657 case Stmt::CharacterLiteralClass:
658 case Stmt::FloatingLiteralClass:
659 case Stmt::PredefinedExprClass:
660 case Stmt::ImaginaryLiteralClass:
661 case Stmt::StringLiteralClass:
662 case Stmt::OffsetOfExprClass:
663 case Stmt::CompoundLiteralExprClass:
664 case Stmt::AddrLabelExprClass:
Francois Pichetf1872372010-12-08 22:35:30 +0000665 case Stmt::BinaryTypeTraitExprClass:
Tom Carea7a8a452010-08-12 22:45:47 +0000666 case Stmt::GNUNullExprClass:
667 case Stmt::InitListExprClass:
668 case Stmt::DesignatedInitExprClass:
669 case Stmt::BlockExprClass:
670 case Stmt::BlockDeclRefExprClass:
671 return false;
672
673 // Cases requiring custom logic
674 case Stmt::SizeOfAlignOfExprClass: {
675 const SizeOfAlignOfExpr *SE = cast<const SizeOfAlignOfExpr>(Ex);
676 if (!SE->isSizeOf())
677 return false;
678 return SE->getTypeOfArgument()->isVariableArrayType();
679 }
680 case Stmt::DeclRefExprClass:
Tom Care6216dc02010-08-30 19:25:43 +0000681 // Check for constants/pseudoconstants
Tom Care245adab2010-08-18 21:17:24 +0000682 return !isConstantOrPseudoConstant(cast<DeclRefExpr>(Ex), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000683
684 // The next cases require recursion for subexpressions
685 case Stmt::BinaryOperatorClass: {
686 const BinaryOperator *B = cast<const BinaryOperator>(Ex);
Ted Kremenek74faec22010-10-29 01:06:54 +0000687
688 // Exclude cases involving pointer arithmetic. These are usually
689 // false positives.
690 if (B->getOpcode() == BO_Sub || B->getOpcode() == BO_Add)
691 if (B->getLHS()->getType()->getAs<PointerType>())
692 return false;
693
Tom Care245adab2010-08-18 21:17:24 +0000694 return CanVary(B->getRHS(), AC)
695 || CanVary(B->getLHS(), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000696 }
697 case Stmt::UnaryOperatorClass: {
698 const UnaryOperator *U = cast<const UnaryOperator>(Ex);
Eli Friedmande7e6622010-08-13 01:36:11 +0000699 // Handle trivial case first
Tom Carea7a8a452010-08-12 22:45:47 +0000700 switch (U->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +0000701 case UO_Extension:
Tom Carea7a8a452010-08-12 22:45:47 +0000702 return false;
703 default:
Tom Care245adab2010-08-18 21:17:24 +0000704 return CanVary(U->getSubExpr(), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000705 }
706 }
707 case Stmt::ChooseExprClass:
Tom Care245adab2010-08-18 21:17:24 +0000708 return CanVary(cast<const ChooseExpr>(Ex)->getChosenSubExpr(
709 AC->getASTContext()), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000710 case Stmt::ConditionalOperatorClass:
Tom Care6216dc02010-08-30 19:25:43 +0000711 return CanVary(cast<const ConditionalOperator>(Ex)->getCond(), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000712 }
Tom Caredb2fa8a2010-07-06 21:43:29 +0000713}
714
Tom Care245adab2010-08-18 21:17:24 +0000715// Returns true if a DeclRefExpr is or behaves like a constant.
716bool IdempotentOperationChecker::isConstantOrPseudoConstant(
Tom Care6216dc02010-08-30 19:25:43 +0000717 const DeclRefExpr *DR,
718 AnalysisContext *AC) {
Tom Care245adab2010-08-18 21:17:24 +0000719 // Check if the type of the Decl is const-qualified
720 if (DR->getType().isConstQualified())
721 return true;
722
Tom Care50e8ac22010-08-16 21:43:52 +0000723 // Check for an enum
724 if (isa<EnumConstantDecl>(DR->getDecl()))
725 return true;
726
Tom Caredb34ab72010-08-23 19:51:57 +0000727 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
728 if (!VD)
Tom Care245adab2010-08-18 21:17:24 +0000729 return true;
730
Tom Caredb34ab72010-08-23 19:51:57 +0000731 // Check if the Decl behaves like a constant. This check also takes care of
732 // static variables, which can only change between function calls if they are
733 // modified in the AST.
734 PseudoConstantAnalysis *PCA = AC->getPseudoConstantAnalysis();
735 if (PCA->isPseudoConstant(VD))
736 return true;
737
738 return false;
739}
740
741// Recursively find any substatements containing VarDecl's with storage other
742// than local
743bool IdempotentOperationChecker::containsNonLocalVarDecl(const Stmt *S) {
744 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
745
746 if (DR)
747 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
748 if (!VD->hasLocalStorage())
749 return true;
750
751 for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
752 ++I)
753 if (const Stmt *child = *I)
754 if (containsNonLocalVarDecl(child))
755 return true;
756
Tom Care50e8ac22010-08-16 21:43:52 +0000757 return false;
758}
Tom Careb0627952010-09-09 02:04:52 +0000759
760// Returns the successor nodes of N whose CFGBlocks cannot reach N's CFGBlock.
761// This effectively gives us a set of points in the ExplodedGraph where
762// subsequent execution could not affect the idempotent operation on this path.
763// This is useful for displaying paths after the point of the error, providing
764// an example of how this idempotent operation cannot change.
Ted Kremenek8e376772011-02-14 17:59:20 +0000765void IdempotentOperationChecker::getLastRelevantNodes(
766 const CFGBlock *Begin, const ExplodedNode *node,
767 ExplodedNodeSet &result) {
768 llvm::SmallVector<const ExplodedNode *, 11> worklist;
769 llvm::DenseMap<const ExplodedNode *, unsigned> visited;
Tom Careb0627952010-09-09 02:04:52 +0000770
Ted Kremenek8e376772011-02-14 17:59:20 +0000771 worklist.push_back(node);
Tom Careb0627952010-09-09 02:04:52 +0000772
Ted Kremenek8e376772011-02-14 17:59:20 +0000773 while (!worklist.empty()) {
774 node = worklist.back();
775 worklist.pop_back();
Tom Careb0627952010-09-09 02:04:52 +0000776
Ted Kremenek8e376772011-02-14 17:59:20 +0000777 // Was this node previously visited?
778 unsigned &visitFlag = visited[node];
779 if (visitFlag)
780 continue;
781 visitFlag = 1;
782
783 const ProgramPoint &PP = node->getLocation();
Tom Careb0627952010-09-09 02:04:52 +0000784 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&PP)) {
785 // Get the CFGBlock and test the reachability
786 const CFGBlock *CB = BE->getBlock();
787
788 // If we cannot reach the beginning CFGBlock from this block, then we are
789 // finished
Ted Kremenek8e376772011-02-14 17:59:20 +0000790 if (!CRA->isReachable(CB, Begin)) {
791 result.Add(const_cast<ExplodedNode *>(node));
Tom Careb0627952010-09-09 02:04:52 +0000792 continue;
793 }
794 }
795
796 // Add unvisited children to the worklist
Ted Kremenek8e376772011-02-14 17:59:20 +0000797 for (ExplodedNode::const_succ_iterator i = node->succ_begin(),
798 e = node->succ_end(); i != e; ++i)
799 worklist.push_back(*i);
Tom Careb0627952010-09-09 02:04:52 +0000800 }
Tom Careb0627952010-09-09 02:04:52 +0000801}
802
803bool IdempotentOperationChecker::CFGReachabilityAnalysis::isReachable(
804 const CFGBlock *Src,
805 const CFGBlock *Dst) {
806 const unsigned DstBlockID = Dst->getBlockID();
807
808 // If we haven't analyzed the destination node, run the analysis now
Ted Kremenek8e376772011-02-14 17:59:20 +0000809 if (!analyzed[DstBlockID]) {
Tom Careb0627952010-09-09 02:04:52 +0000810 MapReachability(Dst);
Ted Kremenek8e376772011-02-14 17:59:20 +0000811 analyzed[DstBlockID] = true;
Tom Careb0627952010-09-09 02:04:52 +0000812 }
813
814 // Return the cached result
Ted Kremenek8e376772011-02-14 17:59:20 +0000815 return reachable[DstBlockID][Src->getBlockID()];
Tom Careb0627952010-09-09 02:04:52 +0000816}
817
818// Maps reachability to a common node by walking the predecessors of the
819// destination node.
820void IdempotentOperationChecker::CFGReachabilityAnalysis::MapReachability(
821 const CFGBlock *Dst) {
Ted Kremenek8e376772011-02-14 17:59:20 +0000822
823 llvm::SmallVector<const CFGBlock *, 11> worklist;
824 llvm::BitVector visited(analyzed.size());
825
Tom Careb0627952010-09-09 02:04:52 +0000826 ReachableSet &DstReachability = reachable[Dst->getBlockID()];
Ted Kremenek8e376772011-02-14 17:59:20 +0000827 DstReachability.resize(analyzed.size(), false);
Tom Careb0627952010-09-09 02:04:52 +0000828
829 // Start searching from the destination node, since we commonly will perform
830 // multiple queries relating to a destination node.
Ted Kremenek8e376772011-02-14 17:59:20 +0000831 worklist.push_back(Dst);
Tom Careb0627952010-09-09 02:04:52 +0000832 bool firstRun = true;
Tom Careb0627952010-09-09 02:04:52 +0000833
Ted Kremenek8e376772011-02-14 17:59:20 +0000834 while (!worklist.empty()) {
835 const CFGBlock *block = worklist.back();
836 worklist.pop_back();
837
838 if (visited[block->getBlockID()])
839 continue;
840 visited[block->getBlockID()] = true;
841
Tom Careb0627952010-09-09 02:04:52 +0000842 // Update reachability information for this node -> Dst
Ted Kremenek8e376772011-02-14 17:59:20 +0000843 if (!firstRun) {
Tom Careb0627952010-09-09 02:04:52 +0000844 // Don't insert Dst -> Dst unless it was a predecessor of itself
Ted Kremenek8e376772011-02-14 17:59:20 +0000845 DstReachability[block->getBlockID()] = true;
846 }
Tom Careb0627952010-09-09 02:04:52 +0000847 else
848 firstRun = false;
849
Ted Kremenek8e376772011-02-14 17:59:20 +0000850 // Add the predecessors to the worklist.
851 for (CFGBlock::const_pred_iterator i = block->pred_begin(),
852 e = block->pred_end(); i != e; ++i) {
853 worklist.push_back(*i);
854 }
Tom Careb0627952010-09-09 02:04:52 +0000855 }
856}