blob: 819591b376e89fec14a9c1a00536f92171995452 [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 Kyrtzidisc9f2e0f2011-02-15 22:55:14 +000045#include "ClangSACheckers.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 Kremenek42461ee2011-02-23 01:51:59 +000048#include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h"
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000049#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis695fb502011-02-17 21:39:17 +000050#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidisecc4d332011-02-23 21:04:44 +000051#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000052#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
53#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
54#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
Ted Kremenek9b663712011-02-10 01:03:03 +000055#include "clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h"
56#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
Tom Caredb2fa8a2010-07-06 21:43:29 +000057#include "clang/AST/Stmt.h"
58#include "llvm/ADT/DenseMap.h"
Tom Carea7a8a452010-08-12 22:45:47 +000059#include "llvm/ADT/SmallSet.h"
Ted Kremenek8e376772011-02-14 17:59:20 +000060#include "llvm/ADT/BitVector.h"
Chandler Carruth256565b2010-07-07 00:07:37 +000061#include "llvm/Support/ErrorHandling.h"
Tom Caredb2fa8a2010-07-06 21:43:29 +000062
63using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000064using namespace ento;
Tom Caredb2fa8a2010-07-06 21:43:29 +000065
66namespace {
67class IdempotentOperationChecker
Argyrios Kyrtzidisec8605f2011-03-01 01:16:21 +000068 : public Checker<check::PreStmt<BinaryOperator>,
Argyrios Kyrtzidisecc4d332011-02-23 21:04:44 +000069 check::PostStmt<BinaryOperator>,
70 check::EndAnalysis> {
Tom Careb0627952010-09-09 02:04:52 +000071public:
Argyrios Kyrtzidisecc4d332011-02-23 21:04:44 +000072 void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
73 void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const;
74 void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const;
Tom Careb0627952010-09-09 02:04:52 +000075
76private:
77 // Our assumption about a particular operation.
78 enum Assumption { Possible = 0, Impossible, Equal, LHSis1, RHSis1, LHSis0,
79 RHSis0 };
80
Argyrios Kyrtzidisecc4d332011-02-23 21:04:44 +000081 static void UpdateAssumption(Assumption &A, const Assumption &New);
Tom Careb0627952010-09-09 02:04:52 +000082
83 // False positive reduction methods
84 static bool isSelfAssign(const Expr *LHS, const Expr *RHS);
85 static bool isUnused(const Expr *E, AnalysisContext *AC);
86 static bool isTruncationExtensionAssignment(const Expr *LHS,
87 const Expr *RHS);
Argyrios Kyrtzidisecc4d332011-02-23 21:04:44 +000088 static bool pathWasCompletelyAnalyzed(AnalysisContext *AC,
89 const CFGBlock *CB,
90 const CoreEngine &CE);
Tom Careb0627952010-09-09 02:04:52 +000091 static bool CanVary(const Expr *Ex,
92 AnalysisContext *AC);
93 static bool isConstantOrPseudoConstant(const DeclRefExpr *DR,
94 AnalysisContext *AC);
95 static bool containsNonLocalVarDecl(const Stmt *S);
Tom Careb0627952010-09-09 02:04:52 +000096
97 // Hash table and related data structures
98 struct BinaryOperatorData {
99 BinaryOperatorData() : assumption(Possible), analysisContext(0) {}
100
101 Assumption assumption;
102 AnalysisContext *analysisContext;
103 ExplodedNodeSet explodedNodes; // Set of ExplodedNodes that refer to a
104 // BinaryOperator
105 };
106 typedef llvm::DenseMap<const BinaryOperator *, BinaryOperatorData>
107 AssumptionMap;
Argyrios Kyrtzidisecc4d332011-02-23 21:04:44 +0000108 mutable AssumptionMap hash;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000109};
110}
111
Argyrios Kyrtzidisecc4d332011-02-23 21:04:44 +0000112void IdempotentOperationChecker::checkPreStmt(const BinaryOperator *B,
113 CheckerContext &C) const {
Ted Kremenekfe97fa12010-08-02 20:33:02 +0000114 // Find or create an entry in the hash for this BinaryOperator instance.
115 // If we haven't done a lookup before, it will get default initialized to
Tom Care2bbbe502010-09-02 23:30:22 +0000116 // 'Possible'. At this stage we do not store the ExplodedNode, as it has not
117 // been created yet.
118 BinaryOperatorData &Data = hash[B];
119 Assumption &A = Data.assumption;
Tom Care245adab2010-08-18 21:17:24 +0000120 AnalysisContext *AC = C.getCurrentAnalysisContext();
Tom Care2bbbe502010-09-02 23:30:22 +0000121 Data.analysisContext = AC;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000122
123 // If we already have visited this node on a path that does not contain an
124 // idempotent operation, return immediately.
125 if (A == Impossible)
126 return;
127
Tom Carea7a8a452010-08-12 22:45:47 +0000128 // Retrieve both sides of the operator and determine if they can vary (which
129 // may mean this is a false positive.
Tom Caredb2fa8a2010-07-06 21:43:29 +0000130 const Expr *LHS = B->getLHS();
131 const Expr *RHS = B->getRHS();
Tom Care245adab2010-08-18 21:17:24 +0000132
Tom Caredb34ab72010-08-23 19:51:57 +0000133 // At this stage we can calculate whether each side contains a false positive
134 // that applies to all operators. We only need to calculate this the first
135 // time.
136 bool LHSContainsFalsePositive = false, RHSContainsFalsePositive = false;
Tom Care245adab2010-08-18 21:17:24 +0000137 if (A == Possible) {
Tom Caredb34ab72010-08-23 19:51:57 +0000138 // An expression contains a false positive if it can't vary, or if it
139 // contains a known false positive VarDecl.
140 LHSContainsFalsePositive = !CanVary(LHS, AC)
141 || containsNonLocalVarDecl(LHS);
142 RHSContainsFalsePositive = !CanVary(RHS, AC)
143 || containsNonLocalVarDecl(RHS);
Tom Care245adab2010-08-18 21:17:24 +0000144 }
Tom Caredb2fa8a2010-07-06 21:43:29 +0000145
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000146 const ProgramState *state = C.getState();
Tom Caredb2fa8a2010-07-06 21:43:29 +0000147
148 SVal LHSVal = state->getSVal(LHS);
149 SVal RHSVal = state->getSVal(RHS);
150
151 // If either value is unknown, we can't be 100% sure of all paths.
152 if (LHSVal.isUnknownOrUndef() || RHSVal.isUnknownOrUndef()) {
153 A = Impossible;
154 return;
155 }
156 BinaryOperator::Opcode Op = B->getOpcode();
157
158 // Dereference the LHS SVal if this is an assign operation
159 switch (Op) {
160 default:
161 break;
162
163 // Fall through intentional
John McCall2de56d12010-08-25 11:45:40 +0000164 case BO_AddAssign:
165 case BO_SubAssign:
166 case BO_MulAssign:
167 case BO_DivAssign:
168 case BO_AndAssign:
169 case BO_OrAssign:
170 case BO_XorAssign:
171 case BO_ShlAssign:
172 case BO_ShrAssign:
173 case BO_Assign:
Tom Caredb2fa8a2010-07-06 21:43:29 +0000174 // Assign statements have one extra level of indirection
175 if (!isa<Loc>(LHSVal)) {
176 A = Impossible;
177 return;
178 }
Ted Kremenek96ebad62010-09-09 07:13:00 +0000179 LHSVal = state->getSVal(cast<Loc>(LHSVal), LHS->getType());
Tom Caredb2fa8a2010-07-06 21:43:29 +0000180 }
181
182
183 // We now check for various cases which result in an idempotent operation.
184
185 // x op x
186 switch (Op) {
187 default:
188 break; // We don't care about any other operators.
189
190 // Fall through intentional
John McCall2de56d12010-08-25 11:45:40 +0000191 case BO_Assign:
Tom Care6216dc02010-08-30 19:25:43 +0000192 // x Assign x can be used to silence unused variable warnings intentionally.
193 // If this is a self assignment and the variable is referenced elsewhere,
Tom Care84c24ed2010-09-07 20:27:56 +0000194 // and the assignment is not a truncation or extension, then it is a false
195 // positive.
Tom Care6216dc02010-08-30 19:25:43 +0000196 if (isSelfAssign(LHS, RHS)) {
Tom Care84c24ed2010-09-07 20:27:56 +0000197 if (!isUnused(LHS, AC) && !isTruncationExtensionAssignment(LHS, RHS)) {
Tom Care6216dc02010-08-30 19:25:43 +0000198 UpdateAssumption(A, Equal);
199 return;
200 }
201 else {
202 A = Impossible;
203 return;
204 }
Tom Caredf4ca422010-07-16 20:41:41 +0000205 }
206
John McCall2de56d12010-08-25 11:45:40 +0000207 case BO_SubAssign:
208 case BO_DivAssign:
209 case BO_AndAssign:
210 case BO_OrAssign:
211 case BO_XorAssign:
212 case BO_Sub:
213 case BO_Div:
214 case BO_And:
215 case BO_Or:
216 case BO_Xor:
217 case BO_LOr:
218 case BO_LAnd:
Tom Care9edd4d02010-08-27 22:50:47 +0000219 case BO_EQ:
220 case BO_NE:
Tom Caredb34ab72010-08-23 19:51:57 +0000221 if (LHSVal != RHSVal || LHSContainsFalsePositive
222 || RHSContainsFalsePositive)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000223 break;
224 UpdateAssumption(A, Equal);
225 return;
226 }
227
228 // x op 1
229 switch (Op) {
230 default:
231 break; // We don't care about any other operators.
232
233 // Fall through intentional
John McCall2de56d12010-08-25 11:45:40 +0000234 case BO_MulAssign:
235 case BO_DivAssign:
236 case BO_Mul:
237 case BO_Div:
238 case BO_LOr:
239 case BO_LAnd:
Tom Caredb34ab72010-08-23 19:51:57 +0000240 if (!RHSVal.isConstant(1) || RHSContainsFalsePositive)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000241 break;
242 UpdateAssumption(A, RHSis1);
243 return;
244 }
245
246 // 1 op x
247 switch (Op) {
248 default:
249 break; // We don't care about any other operators.
250
251 // Fall through intentional
John McCall2de56d12010-08-25 11:45:40 +0000252 case BO_MulAssign:
253 case BO_Mul:
254 case BO_LOr:
255 case BO_LAnd:
Tom Caredb34ab72010-08-23 19:51:57 +0000256 if (!LHSVal.isConstant(1) || LHSContainsFalsePositive)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000257 break;
258 UpdateAssumption(A, LHSis1);
259 return;
260 }
261
262 // x op 0
263 switch (Op) {
264 default:
265 break; // We don't care about any other operators.
266
267 // Fall through intentional
John McCall2de56d12010-08-25 11:45:40 +0000268 case BO_AddAssign:
269 case BO_SubAssign:
270 case BO_MulAssign:
271 case BO_AndAssign:
272 case BO_OrAssign:
273 case BO_XorAssign:
274 case BO_Add:
275 case BO_Sub:
276 case BO_Mul:
277 case BO_And:
278 case BO_Or:
279 case BO_Xor:
280 case BO_Shl:
281 case BO_Shr:
282 case BO_LOr:
283 case BO_LAnd:
Tom Caredb34ab72010-08-23 19:51:57 +0000284 if (!RHSVal.isConstant(0) || RHSContainsFalsePositive)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000285 break;
286 UpdateAssumption(A, RHSis0);
287 return;
288 }
289
290 // 0 op x
291 switch (Op) {
292 default:
293 break; // We don't care about any other operators.
294
295 // Fall through intentional
John McCall2de56d12010-08-25 11:45:40 +0000296 //case BO_AddAssign: // Common false positive
297 case BO_SubAssign: // Check only if unsigned
298 case BO_MulAssign:
299 case BO_DivAssign:
300 case BO_AndAssign:
301 //case BO_OrAssign: // Common false positive
302 //case BO_XorAssign: // Common false positive
303 case BO_ShlAssign:
304 case BO_ShrAssign:
305 case BO_Add:
306 case BO_Sub:
307 case BO_Mul:
308 case BO_Div:
309 case BO_And:
310 case BO_Or:
311 case BO_Xor:
312 case BO_Shl:
313 case BO_Shr:
314 case BO_LOr:
315 case BO_LAnd:
Tom Caredb34ab72010-08-23 19:51:57 +0000316 if (!LHSVal.isConstant(0) || LHSContainsFalsePositive)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000317 break;
318 UpdateAssumption(A, LHSis0);
319 return;
320 }
321
322 // If we get to this point, there has been a valid use of this operation.
323 A = Impossible;
324}
325
Tom Care2bbbe502010-09-02 23:30:22 +0000326// At the post visit stage, the predecessor ExplodedNode will be the
327// BinaryOperator that was just created. We use this hook to collect the
328// ExplodedNode.
Argyrios Kyrtzidisecc4d332011-02-23 21:04:44 +0000329void IdempotentOperationChecker::checkPostStmt(const BinaryOperator *B,
330 CheckerContext &C) const {
Tom Care2bbbe502010-09-02 23:30:22 +0000331 // Add the ExplodedNode we just visited
332 BinaryOperatorData &Data = hash[B];
Ted Kremenek020c3742011-02-12 18:50:03 +0000333
334 const Stmt *predStmt
335 = cast<StmtPoint>(C.getPredecessor()->getLocation()).getStmt();
336
337 // Ignore implicit calls to setters.
Ted Kremenekcf995d32011-03-15 19:27:57 +0000338 if (!isa<BinaryOperator>(predStmt))
Ted Kremenek020c3742011-02-12 18:50:03 +0000339 return;
Ted Kremenekcf995d32011-03-15 19:27:57 +0000340
Tom Care2bbbe502010-09-02 23:30:22 +0000341 Data.explodedNodes.Add(C.getPredecessor());
342}
343
Argyrios Kyrtzidisecc4d332011-02-23 21:04:44 +0000344void IdempotentOperationChecker::checkEndAnalysis(ExplodedGraph &G,
Ted Kremenek3e5637f2010-07-27 18:49:08 +0000345 BugReporter &BR,
Argyrios Kyrtzidisecc4d332011-02-23 21:04:44 +0000346 ExprEngine &Eng) const {
Tom Care2bbbe502010-09-02 23:30:22 +0000347 BugType *BT = new BugType("Idempotent operation", "Dead code");
Tom Caredb2fa8a2010-07-06 21:43:29 +0000348 // Iterate over the hash to see if we have any paths with definite
349 // idempotent operations.
Tom Carea7a8a452010-08-12 22:45:47 +0000350 for (AssumptionMap::const_iterator i = hash.begin(); i != hash.end(); ++i) {
351 // Unpack the hash contents
Tom Care2bbbe502010-09-02 23:30:22 +0000352 const BinaryOperatorData &Data = i->second;
353 const Assumption &A = Data.assumption;
354 AnalysisContext *AC = Data.analysisContext;
355 const ExplodedNodeSet &ES = Data.explodedNodes;
Ted Kremenek3e5637f2010-07-27 18:49:08 +0000356
Tom Carea7a8a452010-08-12 22:45:47 +0000357 const BinaryOperator *B = i->first;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000358
Tom Carea7a8a452010-08-12 22:45:47 +0000359 if (A == Impossible)
360 continue;
361
362 // If the analyzer did not finish, check to see if we can still emit this
363 // warning
364 if (Eng.hasWorkRemaining()) {
Tom Carea7a8a452010-08-12 22:45:47 +0000365 // If we can trace back
Ted Kremenek42461ee2011-02-23 01:51:59 +0000366 if (!pathWasCompletelyAnalyzed(AC,
367 AC->getCFGStmtMap()->getBlock(B),
Tom Carea7a8a452010-08-12 22:45:47 +0000368 Eng.getCoreEngine()))
369 continue;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000370 }
Tom Carea7a8a452010-08-12 22:45:47 +0000371
Tom Care2bbbe502010-09-02 23:30:22 +0000372 // Select the error message and SourceRanges to report.
Tom Carea7a8a452010-08-12 22:45:47 +0000373 llvm::SmallString<128> buf;
374 llvm::raw_svector_ostream os(buf);
Tom Care2bbbe502010-09-02 23:30:22 +0000375 bool LHSRelevant = false, RHSRelevant = false;
Tom Carea7a8a452010-08-12 22:45:47 +0000376 switch (A) {
377 case Equal:
Tom Care2bbbe502010-09-02 23:30:22 +0000378 LHSRelevant = true;
379 RHSRelevant = true;
John McCall2de56d12010-08-25 11:45:40 +0000380 if (B->getOpcode() == BO_Assign)
Tom Carea7a8a452010-08-12 22:45:47 +0000381 os << "Assigned value is always the same as the existing value";
382 else
383 os << "Both operands to '" << B->getOpcodeStr()
384 << "' always have the same value";
385 break;
386 case LHSis1:
Tom Care2bbbe502010-09-02 23:30:22 +0000387 LHSRelevant = true;
Tom Carea7a8a452010-08-12 22:45:47 +0000388 os << "The left operand to '" << B->getOpcodeStr() << "' is always 1";
389 break;
390 case RHSis1:
Tom Care2bbbe502010-09-02 23:30:22 +0000391 RHSRelevant = true;
Tom Carea7a8a452010-08-12 22:45:47 +0000392 os << "The right operand to '" << B->getOpcodeStr() << "' is always 1";
393 break;
394 case LHSis0:
Tom Care2bbbe502010-09-02 23:30:22 +0000395 LHSRelevant = true;
Tom Carea7a8a452010-08-12 22:45:47 +0000396 os << "The left operand to '" << B->getOpcodeStr() << "' is always 0";
397 break;
398 case RHSis0:
Tom Care2bbbe502010-09-02 23:30:22 +0000399 RHSRelevant = true;
Tom Carea7a8a452010-08-12 22:45:47 +0000400 os << "The right operand to '" << B->getOpcodeStr() << "' is always 0";
401 break;
402 case Possible:
403 llvm_unreachable("Operation was never marked with an assumption");
404 case Impossible:
405 llvm_unreachable(0);
406 }
407
Tom Care2bbbe502010-09-02 23:30:22 +0000408 // Add a report for each ExplodedNode
409 for (ExplodedNodeSet::iterator I = ES.begin(), E = ES.end(); I != E; ++I) {
410 EnhancedBugReport *report = new EnhancedBugReport(*BT, os.str(), *I);
411
412 // Add source ranges and visitor hooks
413 if (LHSRelevant) {
414 const Expr *LHS = i->first->getLHS();
415 report->addRange(LHS->getSourceRange());
416 report->addVisitorCreator(bugreporter::registerVarDeclsLastStore, LHS);
417 }
418 if (RHSRelevant) {
419 const Expr *RHS = i->first->getRHS();
420 report->addRange(i->first->getRHS()->getSourceRange());
421 report->addVisitorCreator(bugreporter::registerVarDeclsLastStore, RHS);
422 }
423
424 BR.EmitReport(report);
425 }
Tom Caredb2fa8a2010-07-06 21:43:29 +0000426 }
Argyrios Kyrtzidisecc4d332011-02-23 21:04:44 +0000427
428 hash.clear();
Tom Caredb2fa8a2010-07-06 21:43:29 +0000429}
430
431// Updates the current assumption given the new assumption
432inline void IdempotentOperationChecker::UpdateAssumption(Assumption &A,
433 const Assumption &New) {
Tom Cared8421ed2010-08-27 22:35:28 +0000434// If the assumption is the same, there is nothing to do
435 if (A == New)
436 return;
437
Tom Caredb2fa8a2010-07-06 21:43:29 +0000438 switch (A) {
439 // If we don't currently have an assumption, set it
440 case Possible:
441 A = New;
442 return;
443
444 // If we have determined that a valid state happened, ignore the new
445 // assumption.
446 case Impossible:
447 return;
448
449 // Any other case means that we had a different assumption last time. We don't
450 // currently support mixing assumptions for diagnostic reasons, so we set
451 // our assumption to be impossible.
452 default:
453 A = Impossible;
454 return;
455 }
456}
457
Tom Care6216dc02010-08-30 19:25:43 +0000458// Check for a statement where a variable is self assigned to possibly avoid an
459// unused variable warning.
460bool IdempotentOperationChecker::isSelfAssign(const Expr *LHS, const Expr *RHS) {
Tom Caredf4ca422010-07-16 20:41:41 +0000461 LHS = LHS->IgnoreParenCasts();
462 RHS = RHS->IgnoreParenCasts();
463
464 const DeclRefExpr *LHS_DR = dyn_cast<DeclRefExpr>(LHS);
465 if (!LHS_DR)
466 return false;
467
Tom Careef52bcb2010-08-24 21:09:07 +0000468 const VarDecl *VD = dyn_cast<VarDecl>(LHS_DR->getDecl());
469 if (!VD)
Tom Caredf4ca422010-07-16 20:41:41 +0000470 return false;
471
472 const DeclRefExpr *RHS_DR = dyn_cast<DeclRefExpr>(RHS);
473 if (!RHS_DR)
474 return false;
475
Tom Careef52bcb2010-08-24 21:09:07 +0000476 if (VD != RHS_DR->getDecl())
477 return false;
478
Tom Care6216dc02010-08-30 19:25:43 +0000479 return true;
480}
481
482// Returns true if the Expr points to a VarDecl that is not read anywhere
483// outside of self-assignments.
484bool IdempotentOperationChecker::isUnused(const Expr *E,
485 AnalysisContext *AC) {
486 if (!E)
487 return false;
488
489 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts());
490 if (!DR)
491 return false;
492
493 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
494 if (!VD)
495 return false;
496
Tom Careef52bcb2010-08-24 21:09:07 +0000497 if (AC->getPseudoConstantAnalysis()->wasReferenced(VD))
498 return false;
499
500 return true;
Tom Caredf4ca422010-07-16 20:41:41 +0000501}
502
503// Check for self casts truncating/extending a variable
504bool IdempotentOperationChecker::isTruncationExtensionAssignment(
505 const Expr *LHS,
506 const Expr *RHS) {
507
508 const DeclRefExpr *LHS_DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParenCasts());
509 if (!LHS_DR)
510 return false;
511
512 const VarDecl *VD = dyn_cast<VarDecl>(LHS_DR->getDecl());
513 if (!VD)
514 return false;
515
516 const DeclRefExpr *RHS_DR = dyn_cast<DeclRefExpr>(RHS->IgnoreParenCasts());
517 if (!RHS_DR)
518 return false;
519
520 if (VD != RHS_DR->getDecl())
521 return false;
522
John McCallf6a16482010-12-04 03:47:34 +0000523 return dyn_cast<DeclRefExpr>(RHS->IgnoreParenLValueCasts()) == NULL;
Tom Caredf4ca422010-07-16 20:41:41 +0000524}
525
Tom Carea7a8a452010-08-12 22:45:47 +0000526// Returns false if a path to this block was not completely analyzed, or true
527// otherwise.
Ted Kremenek8e376772011-02-14 17:59:20 +0000528bool
Ted Kremenek42461ee2011-02-23 01:51:59 +0000529IdempotentOperationChecker::pathWasCompletelyAnalyzed(AnalysisContext *AC,
Ted Kremenek8e376772011-02-14 17:59:20 +0000530 const CFGBlock *CB,
Ted Kremenek8e376772011-02-14 17:59:20 +0000531 const CoreEngine &CE) {
Ted Kremenekb5318912011-02-15 02:20:03 +0000532
Ted Kremenekaf13d5b2011-03-19 01:00:33 +0000533 CFGReverseBlockReachabilityAnalysis *CRA = AC->getCFGReachablityAnalysis();
Ted Kremenek8e376772011-02-14 17:59:20 +0000534
Tom Careb0627952010-09-09 02:04:52 +0000535 // Test for reachability from any aborted blocks to this block
Ted Kremenek422ab7a2011-04-02 02:56:23 +0000536 typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator;
537 for (ExhaustedIterator I = CE.blocks_exhausted_begin(),
538 E = CE.blocks_exhausted_end(); I != E; ++I) {
Tom Carea7a8a452010-08-12 22:45:47 +0000539 const BlockEdge &BE = I->first;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000540
Tom Carea7a8a452010-08-12 22:45:47 +0000541 // The destination block on the BlockEdge is the first block that was not
Tom Careb0627952010-09-09 02:04:52 +0000542 // analyzed. If we can reach this block from the aborted block, then this
543 // block was not completely analyzed.
Ted Kremeneke8350c62011-02-14 17:59:23 +0000544 //
545 // Also explicitly check if the current block is the destination block.
546 // While technically reachable, it means we aborted the analysis on
547 // a path that included that block.
548 const CFGBlock *destBlock = BE.getDst();
549 if (destBlock == CB || CRA->isReachable(destBlock, CB))
Tom Carea7a8a452010-08-12 22:45:47 +0000550 return false;
Tom Carea7a8a452010-08-12 22:45:47 +0000551 }
Ted Kremenek422ab7a2011-04-02 02:56:23 +0000552
553 // Test for reachability from blocks we just gave up on.
554 typedef CoreEngine::BlocksAborted::const_iterator AbortedIterator;
555 for (AbortedIterator I = CE.blocks_aborted_begin(),
556 E = CE.blocks_aborted_end(); I != E; ++I) {
557 const CFGBlock *destBlock = I->first;
558 if (destBlock == CB || CRA->isReachable(destBlock, CB))
559 return false;
560 }
Ted Kremenek33d46262010-11-13 05:04:52 +0000561
562 // For the items still on the worklist, see if they are in blocks that
563 // can eventually reach 'CB'.
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000564 class VisitWL : public WorkList::Visitor {
Ted Kremenek33d46262010-11-13 05:04:52 +0000565 const CFGStmtMap *CBM;
566 const CFGBlock *TargetBlock;
Ted Kremenekaf13d5b2011-03-19 01:00:33 +0000567 CFGReverseBlockReachabilityAnalysis &CRA;
Ted Kremenek33d46262010-11-13 05:04:52 +0000568 public:
569 VisitWL(const CFGStmtMap *cbm, const CFGBlock *targetBlock,
Ted Kremenekaf13d5b2011-03-19 01:00:33 +0000570 CFGReverseBlockReachabilityAnalysis &cra)
Ted Kremenek33d46262010-11-13 05:04:52 +0000571 : CBM(cbm), TargetBlock(targetBlock), CRA(cra) {}
Ted Kremenek55825aa2011-01-11 02:34:50 +0000572 virtual bool visit(const WorkListUnit &U) {
Ted Kremenek33d46262010-11-13 05:04:52 +0000573 ProgramPoint P = U.getNode()->getLocation();
574 const CFGBlock *B = 0;
575 if (StmtPoint *SP = dyn_cast<StmtPoint>(&P)) {
576 B = CBM->getBlock(SP->getStmt());
577 }
Ted Kremeneked023662010-11-13 05:12:26 +0000578 else if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
579 B = BE->getDst();
580 }
581 else if (BlockEntrance *BEnt = dyn_cast<BlockEntrance>(&P)) {
582 B = BEnt->getBlock();
583 }
584 else if (BlockExit *BExit = dyn_cast<BlockExit>(&P)) {
585 B = BExit->getBlock();
586 }
Ted Kremenek33d46262010-11-13 05:04:52 +0000587 if (!B)
588 return true;
589
Ted Kremenek1212f802011-04-12 21:47:02 +0000590 return B == TargetBlock || CRA.isReachable(B, TargetBlock);
Ted Kremenek33d46262010-11-13 05:04:52 +0000591 }
592 };
Ted Kremenek42461ee2011-02-23 01:51:59 +0000593 VisitWL visitWL(AC->getCFGStmtMap(), CB, *CRA);
Ted Kremenek33d46262010-11-13 05:04:52 +0000594 // Were there any items in the worklist that could potentially reach
595 // this block?
Ted Kremenek55825aa2011-01-11 02:34:50 +0000596 if (CE.getWorkList()->visitItemsInWorkList(visitWL))
Ted Kremenek33d46262010-11-13 05:04:52 +0000597 return false;
Tom Carea7a8a452010-08-12 22:45:47 +0000598
Tom Careb0627952010-09-09 02:04:52 +0000599 // Verify that this block is reachable from the entry block
Ted Kremenek42461ee2011-02-23 01:51:59 +0000600 if (!CRA->isReachable(&AC->getCFG()->getEntry(), CB))
Tom Careb0627952010-09-09 02:04:52 +0000601 return false;
602
Tom Carea7a8a452010-08-12 22:45:47 +0000603 // If we get to this point, there is no connection to the entry block or an
604 // aborted block. This path is unreachable and we can report the error.
605 return true;
606}
607
608// Recursive function that determines whether an expression contains any element
609// that varies. This could be due to a compile-time constant like sizeof. An
610// expression may also involve a variable that behaves like a constant. The
611// function returns true if the expression varies, and false otherwise.
Tom Care245adab2010-08-18 21:17:24 +0000612bool IdempotentOperationChecker::CanVary(const Expr *Ex,
613 AnalysisContext *AC) {
Tom Carea7a8a452010-08-12 22:45:47 +0000614 // Parentheses and casts are irrelevant here
615 Ex = Ex->IgnoreParenCasts();
616
617 if (Ex->getLocStart().isMacroID())
618 return false;
619
620 switch (Ex->getStmtClass()) {
621 // Trivially true cases
622 case Stmt::ArraySubscriptExprClass:
623 case Stmt::MemberExprClass:
624 case Stmt::StmtExprClass:
625 case Stmt::CallExprClass:
626 case Stmt::VAArgExprClass:
627 case Stmt::ShuffleVectorExprClass:
628 return true;
629 default:
630 return true;
631
632 // Trivially false cases
633 case Stmt::IntegerLiteralClass:
634 case Stmt::CharacterLiteralClass:
635 case Stmt::FloatingLiteralClass:
636 case Stmt::PredefinedExprClass:
637 case Stmt::ImaginaryLiteralClass:
638 case Stmt::StringLiteralClass:
639 case Stmt::OffsetOfExprClass:
640 case Stmt::CompoundLiteralExprClass:
641 case Stmt::AddrLabelExprClass:
Francois Pichetf1872372010-12-08 22:35:30 +0000642 case Stmt::BinaryTypeTraitExprClass:
Tom Carea7a8a452010-08-12 22:45:47 +0000643 case Stmt::GNUNullExprClass:
644 case Stmt::InitListExprClass:
645 case Stmt::DesignatedInitExprClass:
646 case Stmt::BlockExprClass:
647 case Stmt::BlockDeclRefExprClass:
648 return false;
649
650 // Cases requiring custom logic
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +0000651 case Stmt::UnaryExprOrTypeTraitExprClass: {
652 const UnaryExprOrTypeTraitExpr *SE =
653 cast<const UnaryExprOrTypeTraitExpr>(Ex);
654 if (SE->getKind() != UETT_SizeOf)
Tom Carea7a8a452010-08-12 22:45:47 +0000655 return false;
656 return SE->getTypeOfArgument()->isVariableArrayType();
657 }
658 case Stmt::DeclRefExprClass:
Tom Care6216dc02010-08-30 19:25:43 +0000659 // Check for constants/pseudoconstants
Tom Care245adab2010-08-18 21:17:24 +0000660 return !isConstantOrPseudoConstant(cast<DeclRefExpr>(Ex), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000661
662 // The next cases require recursion for subexpressions
663 case Stmt::BinaryOperatorClass: {
664 const BinaryOperator *B = cast<const BinaryOperator>(Ex);
Ted Kremenek74faec22010-10-29 01:06:54 +0000665
666 // Exclude cases involving pointer arithmetic. These are usually
667 // false positives.
668 if (B->getOpcode() == BO_Sub || B->getOpcode() == BO_Add)
669 if (B->getLHS()->getType()->getAs<PointerType>())
670 return false;
671
Tom Care245adab2010-08-18 21:17:24 +0000672 return CanVary(B->getRHS(), AC)
673 || CanVary(B->getLHS(), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000674 }
675 case Stmt::UnaryOperatorClass: {
676 const UnaryOperator *U = cast<const UnaryOperator>(Ex);
Eli Friedmande7e6622010-08-13 01:36:11 +0000677 // Handle trivial case first
Tom Carea7a8a452010-08-12 22:45:47 +0000678 switch (U->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +0000679 case UO_Extension:
Tom Carea7a8a452010-08-12 22:45:47 +0000680 return false;
681 default:
Tom Care245adab2010-08-18 21:17:24 +0000682 return CanVary(U->getSubExpr(), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000683 }
684 }
685 case Stmt::ChooseExprClass:
Tom Care245adab2010-08-18 21:17:24 +0000686 return CanVary(cast<const ChooseExpr>(Ex)->getChosenSubExpr(
687 AC->getASTContext()), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000688 case Stmt::ConditionalOperatorClass:
John McCall56ca35d2011-02-17 10:25:35 +0000689 case Stmt::BinaryConditionalOperatorClass:
690 return CanVary(cast<AbstractConditionalOperator>(Ex)->getCond(), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000691 }
Tom Caredb2fa8a2010-07-06 21:43:29 +0000692}
693
Tom Care245adab2010-08-18 21:17:24 +0000694// Returns true if a DeclRefExpr is or behaves like a constant.
695bool IdempotentOperationChecker::isConstantOrPseudoConstant(
Tom Care6216dc02010-08-30 19:25:43 +0000696 const DeclRefExpr *DR,
697 AnalysisContext *AC) {
Tom Care245adab2010-08-18 21:17:24 +0000698 // Check if the type of the Decl is const-qualified
699 if (DR->getType().isConstQualified())
700 return true;
701
Tom Care50e8ac22010-08-16 21:43:52 +0000702 // Check for an enum
703 if (isa<EnumConstantDecl>(DR->getDecl()))
704 return true;
705
Tom Caredb34ab72010-08-23 19:51:57 +0000706 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
707 if (!VD)
Tom Care245adab2010-08-18 21:17:24 +0000708 return true;
709
Tom Caredb34ab72010-08-23 19:51:57 +0000710 // Check if the Decl behaves like a constant. This check also takes care of
711 // static variables, which can only change between function calls if they are
712 // modified in the AST.
713 PseudoConstantAnalysis *PCA = AC->getPseudoConstantAnalysis();
714 if (PCA->isPseudoConstant(VD))
715 return true;
716
717 return false;
718}
719
720// Recursively find any substatements containing VarDecl's with storage other
721// than local
722bool IdempotentOperationChecker::containsNonLocalVarDecl(const Stmt *S) {
723 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
724
725 if (DR)
726 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
727 if (!VD->hasLocalStorage())
728 return true;
729
730 for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
731 ++I)
732 if (const Stmt *child = *I)
733 if (containsNonLocalVarDecl(child))
734 return true;
735
Tom Care50e8ac22010-08-16 21:43:52 +0000736 return false;
737}
Tom Careb0627952010-09-09 02:04:52 +0000738
Tom Careb0627952010-09-09 02:04:52 +0000739
Argyrios Kyrtzidisecc4d332011-02-23 21:04:44 +0000740void ento::registerIdempotentOperationChecker(CheckerManager &mgr) {
741 mgr.registerChecker<IdempotentOperationChecker>();
742}