blob: 7f64f8e2c40acca623d802379c49750d660cd08a [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"
Chandler Carruth256565b2010-07-07 00:07:37 +000057#include "llvm/Support/ErrorHandling.h"
Tom Carea7a8a452010-08-12 22:45:47 +000058#include <deque>
Tom Caredb2fa8a2010-07-06 21:43:29 +000059
60using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000061using namespace ento;
Tom Caredb2fa8a2010-07-06 21:43:29 +000062
63namespace {
64class IdempotentOperationChecker
65 : public CheckerVisitor<IdempotentOperationChecker> {
Tom Careb0627952010-09-09 02:04:52 +000066public:
67 static void *getTag();
68 void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
69 void PostVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000070 void VisitEndAnalysis(ExplodedGraph &G, BugReporter &B, ExprEngine &Eng);
Tom Careb0627952010-09-09 02:04:52 +000071
72private:
73 // Our assumption about a particular operation.
74 enum Assumption { Possible = 0, Impossible, Equal, LHSis1, RHSis1, LHSis0,
75 RHSis0 };
76
77 void UpdateAssumption(Assumption &A, const Assumption &New);
78
79 // False positive reduction methods
80 static bool isSelfAssign(const Expr *LHS, const Expr *RHS);
81 static bool isUnused(const Expr *E, AnalysisContext *AC);
82 static bool isTruncationExtensionAssignment(const Expr *LHS,
83 const Expr *RHS);
84 bool PathWasCompletelyAnalyzed(const CFG *C,
85 const CFGBlock *CB,
Ted Kremenek33d46262010-11-13 05:04:52 +000086 const CFGStmtMap *CBM,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +000087 const CoreEngine &CE);
Tom Careb0627952010-09-09 02:04:52 +000088 static bool CanVary(const Expr *Ex,
89 AnalysisContext *AC);
90 static bool isConstantOrPseudoConstant(const DeclRefExpr *DR,
91 AnalysisContext *AC);
92 static bool containsNonLocalVarDecl(const Stmt *S);
93 const ExplodedNodeSet getLastRelevantNodes(const CFGBlock *Begin,
94 const ExplodedNode *N);
95
96 // Hash table and related data structures
97 struct BinaryOperatorData {
98 BinaryOperatorData() : assumption(Possible), analysisContext(0) {}
99
100 Assumption assumption;
101 AnalysisContext *analysisContext;
102 ExplodedNodeSet explodedNodes; // Set of ExplodedNodes that refer to a
103 // BinaryOperator
104 };
105 typedef llvm::DenseMap<const BinaryOperator *, BinaryOperatorData>
106 AssumptionMap;
107 AssumptionMap hash;
108
109 // A class that performs reachability queries for CFGBlocks. Several internal
110 // checks in this checker require reachability information. The requests all
111 // tend to have a common destination, so we lazily do a predecessor search
112 // from the destination node and cache the results to prevent work
113 // duplication.
114 class CFGReachabilityAnalysis {
115 typedef llvm::SmallSet<unsigned, 32> ReachableSet;
116 typedef llvm::DenseMap<unsigned, ReachableSet> ReachableMap;
117 ReachableSet analyzed;
118 ReachableMap reachable;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000119 public:
Tom Careb0627952010-09-09 02:04:52 +0000120 inline bool isReachable(const CFGBlock *Src, const CFGBlock *Dst);
Tom Caredb2fa8a2010-07-06 21:43:29 +0000121 private:
Tom Careb0627952010-09-09 02:04:52 +0000122 void MapReachability(const CFGBlock *Dst);
123 };
124 CFGReachabilityAnalysis CRA;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000125};
126}
127
128void *IdempotentOperationChecker::getTag() {
129 static int x = 0;
130 return &x;
131}
132
Ted Kremenek9ef65372010-12-23 07:20:52 +0000133void ento::RegisterIdempotentOperationChecker(ExprEngine &Eng) {
Tom Caredb2fa8a2010-07-06 21:43:29 +0000134 Eng.registerCheck(new IdempotentOperationChecker());
135}
136
137void IdempotentOperationChecker::PreVisitBinaryOperator(
138 CheckerContext &C,
139 const BinaryOperator *B) {
Ted Kremenekfe97fa12010-08-02 20:33:02 +0000140 // Find or create an entry in the hash for this BinaryOperator instance.
141 // If we haven't done a lookup before, it will get default initialized to
Tom Care2bbbe502010-09-02 23:30:22 +0000142 // 'Possible'. At this stage we do not store the ExplodedNode, as it has not
143 // been created yet.
144 BinaryOperatorData &Data = hash[B];
145 Assumption &A = Data.assumption;
Tom Care245adab2010-08-18 21:17:24 +0000146 AnalysisContext *AC = C.getCurrentAnalysisContext();
Tom Care2bbbe502010-09-02 23:30:22 +0000147 Data.analysisContext = AC;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000148
149 // If we already have visited this node on a path that does not contain an
150 // idempotent operation, return immediately.
151 if (A == Impossible)
152 return;
153
Tom Carea7a8a452010-08-12 22:45:47 +0000154 // Retrieve both sides of the operator and determine if they can vary (which
155 // may mean this is a false positive.
Tom Caredb2fa8a2010-07-06 21:43:29 +0000156 const Expr *LHS = B->getLHS();
157 const Expr *RHS = B->getRHS();
Tom Care245adab2010-08-18 21:17:24 +0000158
Tom Caredb34ab72010-08-23 19:51:57 +0000159 // At this stage we can calculate whether each side contains a false positive
160 // that applies to all operators. We only need to calculate this the first
161 // time.
162 bool LHSContainsFalsePositive = false, RHSContainsFalsePositive = false;
Tom Care245adab2010-08-18 21:17:24 +0000163 if (A == Possible) {
Tom Caredb34ab72010-08-23 19:51:57 +0000164 // An expression contains a false positive if it can't vary, or if it
165 // contains a known false positive VarDecl.
166 LHSContainsFalsePositive = !CanVary(LHS, AC)
167 || containsNonLocalVarDecl(LHS);
168 RHSContainsFalsePositive = !CanVary(RHS, AC)
169 || containsNonLocalVarDecl(RHS);
Tom Care245adab2010-08-18 21:17:24 +0000170 }
Tom Caredb2fa8a2010-07-06 21:43:29 +0000171
172 const GRState *state = C.getState();
173
174 SVal LHSVal = state->getSVal(LHS);
175 SVal RHSVal = state->getSVal(RHS);
176
177 // If either value is unknown, we can't be 100% sure of all paths.
178 if (LHSVal.isUnknownOrUndef() || RHSVal.isUnknownOrUndef()) {
179 A = Impossible;
180 return;
181 }
182 BinaryOperator::Opcode Op = B->getOpcode();
183
184 // Dereference the LHS SVal if this is an assign operation
185 switch (Op) {
186 default:
187 break;
188
189 // Fall through intentional
John McCall2de56d12010-08-25 11:45:40 +0000190 case BO_AddAssign:
191 case BO_SubAssign:
192 case BO_MulAssign:
193 case BO_DivAssign:
194 case BO_AndAssign:
195 case BO_OrAssign:
196 case BO_XorAssign:
197 case BO_ShlAssign:
198 case BO_ShrAssign:
199 case BO_Assign:
Tom Caredb2fa8a2010-07-06 21:43:29 +0000200 // Assign statements have one extra level of indirection
201 if (!isa<Loc>(LHSVal)) {
202 A = Impossible;
203 return;
204 }
Ted Kremenek96ebad62010-09-09 07:13:00 +0000205 LHSVal = state->getSVal(cast<Loc>(LHSVal), LHS->getType());
Tom Caredb2fa8a2010-07-06 21:43:29 +0000206 }
207
208
209 // We now check for various cases which result in an idempotent operation.
210
211 // x op x
212 switch (Op) {
213 default:
214 break; // We don't care about any other operators.
215
216 // Fall through intentional
John McCall2de56d12010-08-25 11:45:40 +0000217 case BO_Assign:
Tom Care6216dc02010-08-30 19:25:43 +0000218 // x Assign x can be used to silence unused variable warnings intentionally.
219 // If this is a self assignment and the variable is referenced elsewhere,
Tom Care84c24ed2010-09-07 20:27:56 +0000220 // and the assignment is not a truncation or extension, then it is a false
221 // positive.
Tom Care6216dc02010-08-30 19:25:43 +0000222 if (isSelfAssign(LHS, RHS)) {
Tom Care84c24ed2010-09-07 20:27:56 +0000223 if (!isUnused(LHS, AC) && !isTruncationExtensionAssignment(LHS, RHS)) {
Tom Care6216dc02010-08-30 19:25:43 +0000224 UpdateAssumption(A, Equal);
225 return;
226 }
227 else {
228 A = Impossible;
229 return;
230 }
Tom Caredf4ca422010-07-16 20:41:41 +0000231 }
232
John McCall2de56d12010-08-25 11:45:40 +0000233 case BO_SubAssign:
234 case BO_DivAssign:
235 case BO_AndAssign:
236 case BO_OrAssign:
237 case BO_XorAssign:
238 case BO_Sub:
239 case BO_Div:
240 case BO_And:
241 case BO_Or:
242 case BO_Xor:
243 case BO_LOr:
244 case BO_LAnd:
Tom Care9edd4d02010-08-27 22:50:47 +0000245 case BO_EQ:
246 case BO_NE:
Tom Caredb34ab72010-08-23 19:51:57 +0000247 if (LHSVal != RHSVal || LHSContainsFalsePositive
248 || RHSContainsFalsePositive)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000249 break;
250 UpdateAssumption(A, Equal);
251 return;
252 }
253
254 // x op 1
255 switch (Op) {
256 default:
257 break; // We don't care about any other operators.
258
259 // Fall through intentional
John McCall2de56d12010-08-25 11:45:40 +0000260 case BO_MulAssign:
261 case BO_DivAssign:
262 case BO_Mul:
263 case BO_Div:
264 case BO_LOr:
265 case BO_LAnd:
Tom Caredb34ab72010-08-23 19:51:57 +0000266 if (!RHSVal.isConstant(1) || RHSContainsFalsePositive)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000267 break;
268 UpdateAssumption(A, RHSis1);
269 return;
270 }
271
272 // 1 op x
273 switch (Op) {
274 default:
275 break; // We don't care about any other operators.
276
277 // Fall through intentional
John McCall2de56d12010-08-25 11:45:40 +0000278 case BO_MulAssign:
279 case BO_Mul:
280 case BO_LOr:
281 case BO_LAnd:
Tom Caredb34ab72010-08-23 19:51:57 +0000282 if (!LHSVal.isConstant(1) || LHSContainsFalsePositive)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000283 break;
284 UpdateAssumption(A, LHSis1);
285 return;
286 }
287
288 // x op 0
289 switch (Op) {
290 default:
291 break; // We don't care about any other operators.
292
293 // Fall through intentional
John McCall2de56d12010-08-25 11:45:40 +0000294 case BO_AddAssign:
295 case BO_SubAssign:
296 case BO_MulAssign:
297 case BO_AndAssign:
298 case BO_OrAssign:
299 case BO_XorAssign:
300 case BO_Add:
301 case BO_Sub:
302 case BO_Mul:
303 case BO_And:
304 case BO_Or:
305 case BO_Xor:
306 case BO_Shl:
307 case BO_Shr:
308 case BO_LOr:
309 case BO_LAnd:
Tom Caredb34ab72010-08-23 19:51:57 +0000310 if (!RHSVal.isConstant(0) || RHSContainsFalsePositive)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000311 break;
312 UpdateAssumption(A, RHSis0);
313 return;
314 }
315
316 // 0 op x
317 switch (Op) {
318 default:
319 break; // We don't care about any other operators.
320
321 // Fall through intentional
John McCall2de56d12010-08-25 11:45:40 +0000322 //case BO_AddAssign: // Common false positive
323 case BO_SubAssign: // Check only if unsigned
324 case BO_MulAssign:
325 case BO_DivAssign:
326 case BO_AndAssign:
327 //case BO_OrAssign: // Common false positive
328 //case BO_XorAssign: // Common false positive
329 case BO_ShlAssign:
330 case BO_ShrAssign:
331 case BO_Add:
332 case BO_Sub:
333 case BO_Mul:
334 case BO_Div:
335 case BO_And:
336 case BO_Or:
337 case BO_Xor:
338 case BO_Shl:
339 case BO_Shr:
340 case BO_LOr:
341 case BO_LAnd:
Tom Caredb34ab72010-08-23 19:51:57 +0000342 if (!LHSVal.isConstant(0) || LHSContainsFalsePositive)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000343 break;
344 UpdateAssumption(A, LHSis0);
345 return;
346 }
347
348 // If we get to this point, there has been a valid use of this operation.
349 A = Impossible;
350}
351
Tom Care2bbbe502010-09-02 23:30:22 +0000352// At the post visit stage, the predecessor ExplodedNode will be the
353// BinaryOperator that was just created. We use this hook to collect the
354// ExplodedNode.
355void IdempotentOperationChecker::PostVisitBinaryOperator(
356 CheckerContext &C,
357 const BinaryOperator *B) {
358 // Add the ExplodedNode we just visited
359 BinaryOperatorData &Data = hash[B];
Ted Kremenek020c3742011-02-12 18:50:03 +0000360
361 const Stmt *predStmt
362 = cast<StmtPoint>(C.getPredecessor()->getLocation()).getStmt();
363
364 // Ignore implicit calls to setters.
365 if (isa<ObjCPropertyRefExpr>(predStmt))
366 return;
367
368 assert(isa<BinaryOperator>(predStmt));
Tom Care2bbbe502010-09-02 23:30:22 +0000369 Data.explodedNodes.Add(C.getPredecessor());
370}
371
Tom Caredb2fa8a2010-07-06 21:43:29 +0000372void IdempotentOperationChecker::VisitEndAnalysis(ExplodedGraph &G,
Ted Kremenek3e5637f2010-07-27 18:49:08 +0000373 BugReporter &BR,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000374 ExprEngine &Eng) {
Tom Care2bbbe502010-09-02 23:30:22 +0000375 BugType *BT = new BugType("Idempotent operation", "Dead code");
Tom Caredb2fa8a2010-07-06 21:43:29 +0000376 // Iterate over the hash to see if we have any paths with definite
377 // idempotent operations.
Tom Carea7a8a452010-08-12 22:45:47 +0000378 for (AssumptionMap::const_iterator i = hash.begin(); i != hash.end(); ++i) {
379 // Unpack the hash contents
Tom Care2bbbe502010-09-02 23:30:22 +0000380 const BinaryOperatorData &Data = i->second;
381 const Assumption &A = Data.assumption;
382 AnalysisContext *AC = Data.analysisContext;
383 const ExplodedNodeSet &ES = Data.explodedNodes;
Ted Kremenek3e5637f2010-07-27 18:49:08 +0000384
Tom Carea7a8a452010-08-12 22:45:47 +0000385 const BinaryOperator *B = i->first;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000386
Tom Carea7a8a452010-08-12 22:45:47 +0000387 if (A == Impossible)
388 continue;
389
390 // If the analyzer did not finish, check to see if we can still emit this
391 // warning
392 if (Eng.hasWorkRemaining()) {
393 const CFGStmtMap *CBM = CFGStmtMap::Build(AC->getCFG(),
394 &AC->getParentMap());
395
396 // If we can trace back
397 if (!PathWasCompletelyAnalyzed(AC->getCFG(),
Ted Kremenek33d46262010-11-13 05:04:52 +0000398 CBM->getBlock(B), CBM,
Tom Carea7a8a452010-08-12 22:45:47 +0000399 Eng.getCoreEngine()))
400 continue;
401
402 delete CBM;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000403 }
Tom Carea7a8a452010-08-12 22:45:47 +0000404
Tom Care2bbbe502010-09-02 23:30:22 +0000405 // Select the error message and SourceRanges to report.
Tom Carea7a8a452010-08-12 22:45:47 +0000406 llvm::SmallString<128> buf;
407 llvm::raw_svector_ostream os(buf);
Tom Care2bbbe502010-09-02 23:30:22 +0000408 bool LHSRelevant = false, RHSRelevant = false;
Tom Carea7a8a452010-08-12 22:45:47 +0000409 switch (A) {
410 case Equal:
Tom Care2bbbe502010-09-02 23:30:22 +0000411 LHSRelevant = true;
412 RHSRelevant = true;
John McCall2de56d12010-08-25 11:45:40 +0000413 if (B->getOpcode() == BO_Assign)
Tom Carea7a8a452010-08-12 22:45:47 +0000414 os << "Assigned value is always the same as the existing value";
415 else
416 os << "Both operands to '" << B->getOpcodeStr()
417 << "' always have the same value";
418 break;
419 case LHSis1:
Tom Care2bbbe502010-09-02 23:30:22 +0000420 LHSRelevant = true;
Tom Carea7a8a452010-08-12 22:45:47 +0000421 os << "The left operand to '" << B->getOpcodeStr() << "' is always 1";
422 break;
423 case RHSis1:
Tom Care2bbbe502010-09-02 23:30:22 +0000424 RHSRelevant = true;
Tom Carea7a8a452010-08-12 22:45:47 +0000425 os << "The right operand to '" << B->getOpcodeStr() << "' is always 1";
426 break;
427 case LHSis0:
Tom Care2bbbe502010-09-02 23:30:22 +0000428 LHSRelevant = true;
Tom Carea7a8a452010-08-12 22:45:47 +0000429 os << "The left operand to '" << B->getOpcodeStr() << "' is always 0";
430 break;
431 case RHSis0:
Tom Care2bbbe502010-09-02 23:30:22 +0000432 RHSRelevant = true;
Tom Carea7a8a452010-08-12 22:45:47 +0000433 os << "The right operand to '" << B->getOpcodeStr() << "' is always 0";
434 break;
435 case Possible:
436 llvm_unreachable("Operation was never marked with an assumption");
437 case Impossible:
438 llvm_unreachable(0);
439 }
440
Tom Care2bbbe502010-09-02 23:30:22 +0000441 // Add a report for each ExplodedNode
442 for (ExplodedNodeSet::iterator I = ES.begin(), E = ES.end(); I != E; ++I) {
443 EnhancedBugReport *report = new EnhancedBugReport(*BT, os.str(), *I);
444
445 // Add source ranges and visitor hooks
446 if (LHSRelevant) {
447 const Expr *LHS = i->first->getLHS();
448 report->addRange(LHS->getSourceRange());
449 report->addVisitorCreator(bugreporter::registerVarDeclsLastStore, LHS);
450 }
451 if (RHSRelevant) {
452 const Expr *RHS = i->first->getRHS();
453 report->addRange(i->first->getRHS()->getSourceRange());
454 report->addVisitorCreator(bugreporter::registerVarDeclsLastStore, RHS);
455 }
456
457 BR.EmitReport(report);
458 }
Tom Caredb2fa8a2010-07-06 21:43:29 +0000459 }
460}
461
462// Updates the current assumption given the new assumption
463inline void IdempotentOperationChecker::UpdateAssumption(Assumption &A,
464 const Assumption &New) {
Tom Cared8421ed2010-08-27 22:35:28 +0000465// If the assumption is the same, there is nothing to do
466 if (A == New)
467 return;
468
Tom Caredb2fa8a2010-07-06 21:43:29 +0000469 switch (A) {
470 // If we don't currently have an assumption, set it
471 case Possible:
472 A = New;
473 return;
474
475 // If we have determined that a valid state happened, ignore the new
476 // assumption.
477 case Impossible:
478 return;
479
480 // Any other case means that we had a different assumption last time. We don't
481 // currently support mixing assumptions for diagnostic reasons, so we set
482 // our assumption to be impossible.
483 default:
484 A = Impossible;
485 return;
486 }
487}
488
Tom Care6216dc02010-08-30 19:25:43 +0000489// Check for a statement where a variable is self assigned to possibly avoid an
490// unused variable warning.
491bool IdempotentOperationChecker::isSelfAssign(const Expr *LHS, const Expr *RHS) {
Tom Caredf4ca422010-07-16 20:41:41 +0000492 LHS = LHS->IgnoreParenCasts();
493 RHS = RHS->IgnoreParenCasts();
494
495 const DeclRefExpr *LHS_DR = dyn_cast<DeclRefExpr>(LHS);
496 if (!LHS_DR)
497 return false;
498
Tom Careef52bcb2010-08-24 21:09:07 +0000499 const VarDecl *VD = dyn_cast<VarDecl>(LHS_DR->getDecl());
500 if (!VD)
Tom Caredf4ca422010-07-16 20:41:41 +0000501 return false;
502
503 const DeclRefExpr *RHS_DR = dyn_cast<DeclRefExpr>(RHS);
504 if (!RHS_DR)
505 return false;
506
Tom Careef52bcb2010-08-24 21:09:07 +0000507 if (VD != RHS_DR->getDecl())
508 return false;
509
Tom Care6216dc02010-08-30 19:25:43 +0000510 return true;
511}
512
513// Returns true if the Expr points to a VarDecl that is not read anywhere
514// outside of self-assignments.
515bool IdempotentOperationChecker::isUnused(const Expr *E,
516 AnalysisContext *AC) {
517 if (!E)
518 return false;
519
520 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts());
521 if (!DR)
522 return false;
523
524 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
525 if (!VD)
526 return false;
527
Tom Careef52bcb2010-08-24 21:09:07 +0000528 if (AC->getPseudoConstantAnalysis()->wasReferenced(VD))
529 return false;
530
531 return true;
Tom Caredf4ca422010-07-16 20:41:41 +0000532}
533
534// Check for self casts truncating/extending a variable
535bool IdempotentOperationChecker::isTruncationExtensionAssignment(
536 const Expr *LHS,
537 const Expr *RHS) {
538
539 const DeclRefExpr *LHS_DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParenCasts());
540 if (!LHS_DR)
541 return false;
542
543 const VarDecl *VD = dyn_cast<VarDecl>(LHS_DR->getDecl());
544 if (!VD)
545 return false;
546
547 const DeclRefExpr *RHS_DR = dyn_cast<DeclRefExpr>(RHS->IgnoreParenCasts());
548 if (!RHS_DR)
549 return false;
550
551 if (VD != RHS_DR->getDecl())
552 return false;
553
John McCallf6a16482010-12-04 03:47:34 +0000554 return dyn_cast<DeclRefExpr>(RHS->IgnoreParenLValueCasts()) == NULL;
Tom Caredf4ca422010-07-16 20:41:41 +0000555}
556
Tom Carea7a8a452010-08-12 22:45:47 +0000557// Returns false if a path to this block was not completely analyzed, or true
558// otherwise.
559bool IdempotentOperationChecker::PathWasCompletelyAnalyzed(
560 const CFG *C,
561 const CFGBlock *CB,
Ted Kremenek33d46262010-11-13 05:04:52 +0000562 const CFGStmtMap *CBM,
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000563 const CoreEngine &CE) {
Tom Careb0627952010-09-09 02:04:52 +0000564 // Test for reachability from any aborted blocks to this block
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000565 typedef CoreEngine::BlocksAborted::const_iterator AbortedIterator;
Tom Carea7a8a452010-08-12 22:45:47 +0000566 for (AbortedIterator I = CE.blocks_aborted_begin(),
567 E = CE.blocks_aborted_end(); I != E; ++I) {
568 const BlockEdge &BE = I->first;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000569
Tom Carea7a8a452010-08-12 22:45:47 +0000570 // The destination block on the BlockEdge is the first block that was not
Tom Careb0627952010-09-09 02:04:52 +0000571 // analyzed. If we can reach this block from the aborted block, then this
572 // block was not completely analyzed.
573 if (CRA.isReachable(BE.getDst(), CB))
Tom Carea7a8a452010-08-12 22:45:47 +0000574 return false;
Tom Carea7a8a452010-08-12 22:45:47 +0000575 }
Ted Kremenek33d46262010-11-13 05:04:52 +0000576
577 // For the items still on the worklist, see if they are in blocks that
578 // can eventually reach 'CB'.
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000579 class VisitWL : public WorkList::Visitor {
Ted Kremenek33d46262010-11-13 05:04:52 +0000580 const CFGStmtMap *CBM;
581 const CFGBlock *TargetBlock;
582 CFGReachabilityAnalysis &CRA;
583 public:
584 VisitWL(const CFGStmtMap *cbm, const CFGBlock *targetBlock,
585 CFGReachabilityAnalysis &cra)
586 : CBM(cbm), TargetBlock(targetBlock), CRA(cra) {}
Ted Kremenek55825aa2011-01-11 02:34:50 +0000587 virtual bool visit(const WorkListUnit &U) {
Ted Kremenek33d46262010-11-13 05:04:52 +0000588 ProgramPoint P = U.getNode()->getLocation();
589 const CFGBlock *B = 0;
590 if (StmtPoint *SP = dyn_cast<StmtPoint>(&P)) {
591 B = CBM->getBlock(SP->getStmt());
592 }
Ted Kremeneked023662010-11-13 05:12:26 +0000593 else if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
594 B = BE->getDst();
595 }
596 else if (BlockEntrance *BEnt = dyn_cast<BlockEntrance>(&P)) {
597 B = BEnt->getBlock();
598 }
599 else if (BlockExit *BExit = dyn_cast<BlockExit>(&P)) {
600 B = BExit->getBlock();
601 }
Ted Kremenek33d46262010-11-13 05:04:52 +0000602 if (!B)
603 return true;
604
605 return CRA.isReachable(B, TargetBlock);
606 }
607 };
608 VisitWL visitWL(CBM, CB, CRA);
609 // Were there any items in the worklist that could potentially reach
610 // this block?
Ted Kremenek55825aa2011-01-11 02:34:50 +0000611 if (CE.getWorkList()->visitItemsInWorkList(visitWL))
Ted Kremenek33d46262010-11-13 05:04:52 +0000612 return false;
Tom Carea7a8a452010-08-12 22:45:47 +0000613
Tom Careb0627952010-09-09 02:04:52 +0000614 // Verify that this block is reachable from the entry block
615 if (!CRA.isReachable(&C->getEntry(), CB))
616 return false;
617
Tom Carea7a8a452010-08-12 22:45:47 +0000618 // If we get to this point, there is no connection to the entry block or an
619 // aborted block. This path is unreachable and we can report the error.
620 return true;
621}
622
623// Recursive function that determines whether an expression contains any element
624// that varies. This could be due to a compile-time constant like sizeof. An
625// expression may also involve a variable that behaves like a constant. The
626// function returns true if the expression varies, and false otherwise.
Tom Care245adab2010-08-18 21:17:24 +0000627bool IdempotentOperationChecker::CanVary(const Expr *Ex,
628 AnalysisContext *AC) {
Tom Carea7a8a452010-08-12 22:45:47 +0000629 // Parentheses and casts are irrelevant here
630 Ex = Ex->IgnoreParenCasts();
631
632 if (Ex->getLocStart().isMacroID())
633 return false;
634
635 switch (Ex->getStmtClass()) {
636 // Trivially true cases
637 case Stmt::ArraySubscriptExprClass:
638 case Stmt::MemberExprClass:
639 case Stmt::StmtExprClass:
640 case Stmt::CallExprClass:
641 case Stmt::VAArgExprClass:
642 case Stmt::ShuffleVectorExprClass:
643 return true;
644 default:
645 return true;
646
647 // Trivially false cases
648 case Stmt::IntegerLiteralClass:
649 case Stmt::CharacterLiteralClass:
650 case Stmt::FloatingLiteralClass:
651 case Stmt::PredefinedExprClass:
652 case Stmt::ImaginaryLiteralClass:
653 case Stmt::StringLiteralClass:
654 case Stmt::OffsetOfExprClass:
655 case Stmt::CompoundLiteralExprClass:
656 case Stmt::AddrLabelExprClass:
Francois Pichetf1872372010-12-08 22:35:30 +0000657 case Stmt::BinaryTypeTraitExprClass:
Tom Carea7a8a452010-08-12 22:45:47 +0000658 case Stmt::GNUNullExprClass:
659 case Stmt::InitListExprClass:
660 case Stmt::DesignatedInitExprClass:
661 case Stmt::BlockExprClass:
662 case Stmt::BlockDeclRefExprClass:
663 return false;
664
665 // Cases requiring custom logic
666 case Stmt::SizeOfAlignOfExprClass: {
667 const SizeOfAlignOfExpr *SE = cast<const SizeOfAlignOfExpr>(Ex);
668 if (!SE->isSizeOf())
669 return false;
670 return SE->getTypeOfArgument()->isVariableArrayType();
671 }
672 case Stmt::DeclRefExprClass:
Tom Care6216dc02010-08-30 19:25:43 +0000673 // Check for constants/pseudoconstants
Tom Care245adab2010-08-18 21:17:24 +0000674 return !isConstantOrPseudoConstant(cast<DeclRefExpr>(Ex), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000675
676 // The next cases require recursion for subexpressions
677 case Stmt::BinaryOperatorClass: {
678 const BinaryOperator *B = cast<const BinaryOperator>(Ex);
Ted Kremenek74faec22010-10-29 01:06:54 +0000679
680 // Exclude cases involving pointer arithmetic. These are usually
681 // false positives.
682 if (B->getOpcode() == BO_Sub || B->getOpcode() == BO_Add)
683 if (B->getLHS()->getType()->getAs<PointerType>())
684 return false;
685
Tom Care245adab2010-08-18 21:17:24 +0000686 return CanVary(B->getRHS(), AC)
687 || CanVary(B->getLHS(), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000688 }
689 case Stmt::UnaryOperatorClass: {
690 const UnaryOperator *U = cast<const UnaryOperator>(Ex);
Eli Friedmande7e6622010-08-13 01:36:11 +0000691 // Handle trivial case first
Tom Carea7a8a452010-08-12 22:45:47 +0000692 switch (U->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +0000693 case UO_Extension:
Tom Carea7a8a452010-08-12 22:45:47 +0000694 return false;
695 default:
Tom Care245adab2010-08-18 21:17:24 +0000696 return CanVary(U->getSubExpr(), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000697 }
698 }
699 case Stmt::ChooseExprClass:
Tom Care245adab2010-08-18 21:17:24 +0000700 return CanVary(cast<const ChooseExpr>(Ex)->getChosenSubExpr(
701 AC->getASTContext()), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000702 case Stmt::ConditionalOperatorClass:
Tom Care6216dc02010-08-30 19:25:43 +0000703 return CanVary(cast<const ConditionalOperator>(Ex)->getCond(), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000704 }
Tom Caredb2fa8a2010-07-06 21:43:29 +0000705}
706
Tom Care245adab2010-08-18 21:17:24 +0000707// Returns true if a DeclRefExpr is or behaves like a constant.
708bool IdempotentOperationChecker::isConstantOrPseudoConstant(
Tom Care6216dc02010-08-30 19:25:43 +0000709 const DeclRefExpr *DR,
710 AnalysisContext *AC) {
Tom Care245adab2010-08-18 21:17:24 +0000711 // Check if the type of the Decl is const-qualified
712 if (DR->getType().isConstQualified())
713 return true;
714
Tom Care50e8ac22010-08-16 21:43:52 +0000715 // Check for an enum
716 if (isa<EnumConstantDecl>(DR->getDecl()))
717 return true;
718
Tom Caredb34ab72010-08-23 19:51:57 +0000719 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
720 if (!VD)
Tom Care245adab2010-08-18 21:17:24 +0000721 return true;
722
Tom Caredb34ab72010-08-23 19:51:57 +0000723 // Check if the Decl behaves like a constant. This check also takes care of
724 // static variables, which can only change between function calls if they are
725 // modified in the AST.
726 PseudoConstantAnalysis *PCA = AC->getPseudoConstantAnalysis();
727 if (PCA->isPseudoConstant(VD))
728 return true;
729
730 return false;
731}
732
733// Recursively find any substatements containing VarDecl's with storage other
734// than local
735bool IdempotentOperationChecker::containsNonLocalVarDecl(const Stmt *S) {
736 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
737
738 if (DR)
739 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
740 if (!VD->hasLocalStorage())
741 return true;
742
743 for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
744 ++I)
745 if (const Stmt *child = *I)
746 if (containsNonLocalVarDecl(child))
747 return true;
748
Tom Care50e8ac22010-08-16 21:43:52 +0000749 return false;
750}
Tom Careb0627952010-09-09 02:04:52 +0000751
752// Returns the successor nodes of N whose CFGBlocks cannot reach N's CFGBlock.
753// This effectively gives us a set of points in the ExplodedGraph where
754// subsequent execution could not affect the idempotent operation on this path.
755// This is useful for displaying paths after the point of the error, providing
756// an example of how this idempotent operation cannot change.
757const ExplodedNodeSet IdempotentOperationChecker::getLastRelevantNodes(
758 const CFGBlock *Begin, const ExplodedNode *N) {
759 std::deque<const ExplodedNode *> WorkList;
760 llvm::SmallPtrSet<const ExplodedNode *, 32> Visited;
761 ExplodedNodeSet Result;
762
763 WorkList.push_back(N);
764
765 while (!WorkList.empty()) {
766 const ExplodedNode *Head = WorkList.front();
767 WorkList.pop_front();
768 Visited.insert(Head);
769
770 const ProgramPoint &PP = Head->getLocation();
771 if (const BlockEntrance *BE = dyn_cast<BlockEntrance>(&PP)) {
772 // Get the CFGBlock and test the reachability
773 const CFGBlock *CB = BE->getBlock();
774
775 // If we cannot reach the beginning CFGBlock from this block, then we are
776 // finished
777 if (!CRA.isReachable(CB, Begin)) {
778 Result.Add(const_cast<ExplodedNode *>(Head));
779 continue;
780 }
781 }
782
783 // Add unvisited children to the worklist
784 for (ExplodedNode::const_succ_iterator I = Head->succ_begin(),
785 E = Head->succ_end(); I != E; ++I)
786 if (!Visited.count(*I))
787 WorkList.push_back(*I);
788 }
789
790 // Return the ExplodedNodes that were found
791 return Result;
792}
793
794bool IdempotentOperationChecker::CFGReachabilityAnalysis::isReachable(
795 const CFGBlock *Src,
796 const CFGBlock *Dst) {
797 const unsigned DstBlockID = Dst->getBlockID();
798
799 // If we haven't analyzed the destination node, run the analysis now
800 if (!analyzed.count(DstBlockID)) {
801 MapReachability(Dst);
802 analyzed.insert(DstBlockID);
803 }
804
805 // Return the cached result
806 return reachable[DstBlockID].count(Src->getBlockID());
807}
808
809// Maps reachability to a common node by walking the predecessors of the
810// destination node.
811void IdempotentOperationChecker::CFGReachabilityAnalysis::MapReachability(
812 const CFGBlock *Dst) {
813 std::deque<const CFGBlock *> WorkList;
814 // Maintain a visited list to ensure we don't get stuck on cycles
815 llvm::SmallSet<unsigned, 32> Visited;
816 ReachableSet &DstReachability = reachable[Dst->getBlockID()];
817
818 // Start searching from the destination node, since we commonly will perform
819 // multiple queries relating to a destination node.
820 WorkList.push_back(Dst);
821
822 bool firstRun = true;
823 while (!WorkList.empty()) {
824 const CFGBlock *Head = WorkList.front();
825 WorkList.pop_front();
826 Visited.insert(Head->getBlockID());
827
828 // Update reachability information for this node -> Dst
829 if (!firstRun)
830 // Don't insert Dst -> Dst unless it was a predecessor of itself
831 DstReachability.insert(Head->getBlockID());
832 else
833 firstRun = false;
834
835 // Add the predecessors to the worklist unless we have already visited them
836 for (CFGBlock::const_pred_iterator I = Head->pred_begin();
837 I != Head->pred_end(); ++I)
838 if (!Visited.count((*I)->getBlockID()))
839 WorkList.push_back(*I);
840 }
841}