blob: 0b8ebfd285cfe2e675d0336f9c358864ed3737da [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 Kremeneke8350c62011-02-14 17:59:23 +0000581 //
582 // Also explicitly check if the current block is the destination block.
583 // While technically reachable, it means we aborted the analysis on
584 // a path that included that block.
585 const CFGBlock *destBlock = BE.getDst();
586 if (destBlock == CB || CRA->isReachable(destBlock, CB))
Tom Carea7a8a452010-08-12 22:45:47 +0000587 return false;
Tom Carea7a8a452010-08-12 22:45:47 +0000588 }
Ted Kremenek33d46262010-11-13 05:04:52 +0000589
590 // For the items still on the worklist, see if they are in blocks that
591 // can eventually reach 'CB'.
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000592 class VisitWL : public WorkList::Visitor {
Ted Kremenek33d46262010-11-13 05:04:52 +0000593 const CFGStmtMap *CBM;
594 const CFGBlock *TargetBlock;
595 CFGReachabilityAnalysis &CRA;
596 public:
597 VisitWL(const CFGStmtMap *cbm, const CFGBlock *targetBlock,
598 CFGReachabilityAnalysis &cra)
599 : CBM(cbm), TargetBlock(targetBlock), CRA(cra) {}
Ted Kremenek55825aa2011-01-11 02:34:50 +0000600 virtual bool visit(const WorkListUnit &U) {
Ted Kremenek33d46262010-11-13 05:04:52 +0000601 ProgramPoint P = U.getNode()->getLocation();
602 const CFGBlock *B = 0;
603 if (StmtPoint *SP = dyn_cast<StmtPoint>(&P)) {
604 B = CBM->getBlock(SP->getStmt());
605 }
Ted Kremeneked023662010-11-13 05:12:26 +0000606 else if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
607 B = BE->getDst();
608 }
609 else if (BlockEntrance *BEnt = dyn_cast<BlockEntrance>(&P)) {
610 B = BEnt->getBlock();
611 }
612 else if (BlockExit *BExit = dyn_cast<BlockExit>(&P)) {
613 B = BExit->getBlock();
614 }
Ted Kremenek33d46262010-11-13 05:04:52 +0000615 if (!B)
616 return true;
617
618 return CRA.isReachable(B, TargetBlock);
619 }
620 };
Ted Kremenek8e376772011-02-14 17:59:20 +0000621 VisitWL visitWL(CBM, CB, *CRA.get());
Ted Kremenek33d46262010-11-13 05:04:52 +0000622 // Were there any items in the worklist that could potentially reach
623 // this block?
Ted Kremenek55825aa2011-01-11 02:34:50 +0000624 if (CE.getWorkList()->visitItemsInWorkList(visitWL))
Ted Kremenek33d46262010-11-13 05:04:52 +0000625 return false;
Tom Carea7a8a452010-08-12 22:45:47 +0000626
Tom Careb0627952010-09-09 02:04:52 +0000627 // Verify that this block is reachable from the entry block
Ted Kremenek8e376772011-02-14 17:59:20 +0000628 if (!CRA->isReachable(&cfg->getEntry(), CB))
Tom Careb0627952010-09-09 02:04:52 +0000629 return false;
630
Tom Carea7a8a452010-08-12 22:45:47 +0000631 // If we get to this point, there is no connection to the entry block or an
632 // aborted block. This path is unreachable and we can report the error.
633 return true;
634}
635
636// Recursive function that determines whether an expression contains any element
637// that varies. This could be due to a compile-time constant like sizeof. An
638// expression may also involve a variable that behaves like a constant. The
639// function returns true if the expression varies, and false otherwise.
Tom Care245adab2010-08-18 21:17:24 +0000640bool IdempotentOperationChecker::CanVary(const Expr *Ex,
641 AnalysisContext *AC) {
Tom Carea7a8a452010-08-12 22:45:47 +0000642 // Parentheses and casts are irrelevant here
643 Ex = Ex->IgnoreParenCasts();
644
645 if (Ex->getLocStart().isMacroID())
646 return false;
647
648 switch (Ex->getStmtClass()) {
649 // Trivially true cases
650 case Stmt::ArraySubscriptExprClass:
651 case Stmt::MemberExprClass:
652 case Stmt::StmtExprClass:
653 case Stmt::CallExprClass:
654 case Stmt::VAArgExprClass:
655 case Stmt::ShuffleVectorExprClass:
656 return true;
657 default:
658 return true;
659
660 // Trivially false cases
661 case Stmt::IntegerLiteralClass:
662 case Stmt::CharacterLiteralClass:
663 case Stmt::FloatingLiteralClass:
664 case Stmt::PredefinedExprClass:
665 case Stmt::ImaginaryLiteralClass:
666 case Stmt::StringLiteralClass:
667 case Stmt::OffsetOfExprClass:
668 case Stmt::CompoundLiteralExprClass:
669 case Stmt::AddrLabelExprClass:
Francois Pichetf1872372010-12-08 22:35:30 +0000670 case Stmt::BinaryTypeTraitExprClass:
Tom Carea7a8a452010-08-12 22:45:47 +0000671 case Stmt::GNUNullExprClass:
672 case Stmt::InitListExprClass:
673 case Stmt::DesignatedInitExprClass:
674 case Stmt::BlockExprClass:
675 case Stmt::BlockDeclRefExprClass:
676 return false;
677
678 // Cases requiring custom logic
679 case Stmt::SizeOfAlignOfExprClass: {
680 const SizeOfAlignOfExpr *SE = cast<const SizeOfAlignOfExpr>(Ex);
681 if (!SE->isSizeOf())
682 return false;
683 return SE->getTypeOfArgument()->isVariableArrayType();
684 }
685 case Stmt::DeclRefExprClass:
Tom Care6216dc02010-08-30 19:25:43 +0000686 // Check for constants/pseudoconstants
Tom Care245adab2010-08-18 21:17:24 +0000687 return !isConstantOrPseudoConstant(cast<DeclRefExpr>(Ex), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000688
689 // The next cases require recursion for subexpressions
690 case Stmt::BinaryOperatorClass: {
691 const BinaryOperator *B = cast<const BinaryOperator>(Ex);
Ted Kremenek74faec22010-10-29 01:06:54 +0000692
693 // Exclude cases involving pointer arithmetic. These are usually
694 // false positives.
695 if (B->getOpcode() == BO_Sub || B->getOpcode() == BO_Add)
696 if (B->getLHS()->getType()->getAs<PointerType>())
697 return false;
698
Tom Care245adab2010-08-18 21:17:24 +0000699 return CanVary(B->getRHS(), AC)
700 || CanVary(B->getLHS(), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000701 }
702 case Stmt::UnaryOperatorClass: {
703 const UnaryOperator *U = cast<const UnaryOperator>(Ex);
Eli Friedmande7e6622010-08-13 01:36:11 +0000704 // Handle trivial case first
Tom Carea7a8a452010-08-12 22:45:47 +0000705 switch (U->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +0000706 case UO_Extension:
Tom Carea7a8a452010-08-12 22:45:47 +0000707 return false;
708 default:
Tom Care245adab2010-08-18 21:17:24 +0000709 return CanVary(U->getSubExpr(), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000710 }
711 }
712 case Stmt::ChooseExprClass:
Tom Care245adab2010-08-18 21:17:24 +0000713 return CanVary(cast<const ChooseExpr>(Ex)->getChosenSubExpr(
714 AC->getASTContext()), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000715 case Stmt::ConditionalOperatorClass:
Tom Care6216dc02010-08-30 19:25:43 +0000716 return CanVary(cast<const ConditionalOperator>(Ex)->getCond(), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000717 }
Tom Caredb2fa8a2010-07-06 21:43:29 +0000718}
719
Tom Care245adab2010-08-18 21:17:24 +0000720// Returns true if a DeclRefExpr is or behaves like a constant.
721bool IdempotentOperationChecker::isConstantOrPseudoConstant(
Tom Care6216dc02010-08-30 19:25:43 +0000722 const DeclRefExpr *DR,
723 AnalysisContext *AC) {
Tom Care245adab2010-08-18 21:17:24 +0000724 // Check if the type of the Decl is const-qualified
725 if (DR->getType().isConstQualified())
726 return true;
727
Tom Care50e8ac22010-08-16 21:43:52 +0000728 // Check for an enum
729 if (isa<EnumConstantDecl>(DR->getDecl()))
730 return true;
731
Tom Caredb34ab72010-08-23 19:51:57 +0000732 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
733 if (!VD)
Tom Care245adab2010-08-18 21:17:24 +0000734 return true;
735
Tom Caredb34ab72010-08-23 19:51:57 +0000736 // Check if the Decl behaves like a constant. This check also takes care of
737 // static variables, which can only change between function calls if they are
738 // modified in the AST.
739 PseudoConstantAnalysis *PCA = AC->getPseudoConstantAnalysis();
740 if (PCA->isPseudoConstant(VD))
741 return true;
742
743 return false;
744}
745
746// Recursively find any substatements containing VarDecl's with storage other
747// than local
748bool IdempotentOperationChecker::containsNonLocalVarDecl(const Stmt *S) {
749 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
750
751 if (DR)
752 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
753 if (!VD->hasLocalStorage())
754 return true;
755
756 for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
757 ++I)
758 if (const Stmt *child = *I)
759 if (containsNonLocalVarDecl(child))
760 return true;
761
Tom Care50e8ac22010-08-16 21:43:52 +0000762 return false;
763}
Tom Careb0627952010-09-09 02:04:52 +0000764
765// Returns the successor nodes of N whose CFGBlocks cannot reach N's CFGBlock.
766// This effectively gives us a set of points in the ExplodedGraph where
767// subsequent execution could not affect the idempotent operation on this path.
768// This is useful for displaying paths after the point of the error, providing
769// an example of how this idempotent operation cannot change.
Ted Kremenek8e376772011-02-14 17:59:20 +0000770void IdempotentOperationChecker::getLastRelevantNodes(
771 const CFGBlock *Begin, const ExplodedNode *node,
772 ExplodedNodeSet &result) {
773 llvm::SmallVector<const ExplodedNode *, 11> worklist;
774 llvm::DenseMap<const ExplodedNode *, unsigned> visited;
Tom Careb0627952010-09-09 02:04:52 +0000775
Ted Kremenek8e376772011-02-14 17:59:20 +0000776 worklist.push_back(node);
Tom Careb0627952010-09-09 02:04:52 +0000777
Ted Kremenek8e376772011-02-14 17:59:20 +0000778 while (!worklist.empty()) {
779 node = worklist.back();
780 worklist.pop_back();
Tom Careb0627952010-09-09 02:04:52 +0000781
Ted Kremenek8e376772011-02-14 17:59:20 +0000782 // Was this node previously visited?
783 unsigned &visitFlag = visited[node];
784 if (visitFlag)
785 continue;
786 visitFlag = 1;
787
788 const ProgramPoint &PP = node->getLocation();
Tom Careb0627952010-09-09 02:04:52 +0000789 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&PP)) {
790 // Get the CFGBlock and test the reachability
791 const CFGBlock *CB = BE->getBlock();
792
793 // If we cannot reach the beginning CFGBlock from this block, then we are
794 // finished
Ted Kremenek8e376772011-02-14 17:59:20 +0000795 if (!CRA->isReachable(CB, Begin)) {
796 result.Add(const_cast<ExplodedNode *>(node));
Tom Careb0627952010-09-09 02:04:52 +0000797 continue;
798 }
799 }
800
801 // Add unvisited children to the worklist
Ted Kremenek8e376772011-02-14 17:59:20 +0000802 for (ExplodedNode::const_succ_iterator i = node->succ_begin(),
803 e = node->succ_end(); i != e; ++i)
804 worklist.push_back(*i);
Tom Careb0627952010-09-09 02:04:52 +0000805 }
Tom Careb0627952010-09-09 02:04:52 +0000806}
807
808bool IdempotentOperationChecker::CFGReachabilityAnalysis::isReachable(
809 const CFGBlock *Src,
810 const CFGBlock *Dst) {
811 const unsigned DstBlockID = Dst->getBlockID();
812
813 // If we haven't analyzed the destination node, run the analysis now
Ted Kremenek8e376772011-02-14 17:59:20 +0000814 if (!analyzed[DstBlockID]) {
Tom Careb0627952010-09-09 02:04:52 +0000815 MapReachability(Dst);
Ted Kremenek8e376772011-02-14 17:59:20 +0000816 analyzed[DstBlockID] = true;
Tom Careb0627952010-09-09 02:04:52 +0000817 }
818
819 // Return the cached result
Ted Kremenek8e376772011-02-14 17:59:20 +0000820 return reachable[DstBlockID][Src->getBlockID()];
Tom Careb0627952010-09-09 02:04:52 +0000821}
822
823// Maps reachability to a common node by walking the predecessors of the
824// destination node.
825void IdempotentOperationChecker::CFGReachabilityAnalysis::MapReachability(
826 const CFGBlock *Dst) {
Ted Kremenek8e376772011-02-14 17:59:20 +0000827
828 llvm::SmallVector<const CFGBlock *, 11> worklist;
829 llvm::BitVector visited(analyzed.size());
830
Tom Careb0627952010-09-09 02:04:52 +0000831 ReachableSet &DstReachability = reachable[Dst->getBlockID()];
Ted Kremenek8e376772011-02-14 17:59:20 +0000832 DstReachability.resize(analyzed.size(), false);
Tom Careb0627952010-09-09 02:04:52 +0000833
834 // Start searching from the destination node, since we commonly will perform
835 // multiple queries relating to a destination node.
Ted Kremenek8e376772011-02-14 17:59:20 +0000836 worklist.push_back(Dst);
Tom Careb0627952010-09-09 02:04:52 +0000837 bool firstRun = true;
Tom Careb0627952010-09-09 02:04:52 +0000838
Ted Kremenek8e376772011-02-14 17:59:20 +0000839 while (!worklist.empty()) {
840 const CFGBlock *block = worklist.back();
841 worklist.pop_back();
842
843 if (visited[block->getBlockID()])
844 continue;
845 visited[block->getBlockID()] = true;
846
Tom Careb0627952010-09-09 02:04:52 +0000847 // Update reachability information for this node -> Dst
Ted Kremenek8e376772011-02-14 17:59:20 +0000848 if (!firstRun) {
Tom Careb0627952010-09-09 02:04:52 +0000849 // Don't insert Dst -> Dst unless it was a predecessor of itself
Ted Kremenek8e376772011-02-14 17:59:20 +0000850 DstReachability[block->getBlockID()] = true;
851 }
Tom Careb0627952010-09-09 02:04:52 +0000852 else
853 firstRun = false;
854
Ted Kremenek8e376772011-02-14 17:59:20 +0000855 // Add the predecessors to the worklist.
856 for (CFGBlock::const_pred_iterator i = block->pred_begin(),
857 e = block->pred_end(); i != e; ++i) {
858 worklist.push_back(*i);
859 }
Tom Careb0627952010-09-09 02:04:52 +0000860 }
861}