blob: 4b06856ab5550da522bed33485b3979a5e50f697 [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//
39// Ways to reduce false positives (that need to be implemented):
40// - Don't flag downsizing casts
41// - Improved handling of static/global variables
42// - Per-block marking of incomplete analysis
43// - Handling ~0 values
44// - False positives involving silencing unused variable warnings
45//
46// Other things TODO:
47// - Improved error messages
48// - Handle mixed assumptions (which assumptions can belong together?)
49// - Finer grained false positive control (levels)
50
51#include "GRExprEngineExperimentalChecks.h"
52#include "clang/Checker/BugReporter/BugType.h"
53#include "clang/Checker/PathSensitive/CheckerVisitor.h"
54#include "clang/Checker/PathSensitive/SVals.h"
55#include "clang/AST/Stmt.h"
56#include "llvm/ADT/DenseMap.h"
57
58using namespace clang;
59
60namespace {
61class IdempotentOperationChecker
62 : public CheckerVisitor<IdempotentOperationChecker> {
63 public:
64 static void *getTag();
65 void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
66 void VisitEndAnalysis(ExplodedGraph &G, BugReporter &B,
67 bool hasWorkRemaining);
68
69 private:
70 // Our assumption about a particular operation.
71 enum Assumption { Possible, Impossible, Equal, LHSis1, RHSis1, LHSis0,
72 RHSis0 };
73
74 void UpdateAssumption(Assumption &A, const Assumption &New);
75
76 /// contains* - Useful recursive methods to see if a statement contains an
77 /// element somewhere. Used in static analysis to reduce false positives.
78 static bool containsMacro(const Stmt *S);
79 static bool containsEnum(const Stmt *S);
80 static bool containsBuiltinOffsetOf(const Stmt *S);
81 static bool containsZeroConstant(const Stmt *S);
82 static bool containsOneConstant(const Stmt *S);
83 template <class T> static bool containsStmt(const Stmt *S) {
84 if (isa<T>(S))
85 return true;
86
87 for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
88 ++I)
89 if (const Stmt *child = *I)
90 if (containsStmt<T>(child))
91 return true;
92
93 return false;
94 }
95
96 // Hash table
97 typedef llvm::DenseMap<const BinaryOperator *, Assumption> AssumptionMap;
98 AssumptionMap hash;
99};
100}
101
102void *IdempotentOperationChecker::getTag() {
103 static int x = 0;
104 return &x;
105}
106
107void clang::RegisterIdempotentOperationChecker(GRExprEngine &Eng) {
108 Eng.registerCheck(new IdempotentOperationChecker());
109}
110
111void IdempotentOperationChecker::PreVisitBinaryOperator(
112 CheckerContext &C,
113 const BinaryOperator *B) {
114 // Find or create an entry in the hash for this BinaryOperator instance
115 AssumptionMap::iterator i = hash.find(B);
116 Assumption &A = i == hash.end() ? hash[B] : i->second;
117
118 // If we had to create an entry, initialise the value to Possible
119 if (i == hash.end())
120 A = Possible;
121
122 // If we already have visited this node on a path that does not contain an
123 // idempotent operation, return immediately.
124 if (A == Impossible)
125 return;
126
127 // Skip binary operators containing common false positives
128 if (containsMacro(B) || containsEnum(B) || containsStmt<SizeOfAlignOfExpr>(B)
129 || containsZeroConstant(B) || containsOneConstant(B)
130 || containsBuiltinOffsetOf(B)) {
131 A = Impossible;
132 return;
133 }
134
135 const Expr *LHS = B->getLHS();
136 const Expr *RHS = B->getRHS();
137
138 const GRState *state = C.getState();
139
140 SVal LHSVal = state->getSVal(LHS);
141 SVal RHSVal = state->getSVal(RHS);
142
143 // If either value is unknown, we can't be 100% sure of all paths.
144 if (LHSVal.isUnknownOrUndef() || RHSVal.isUnknownOrUndef()) {
145 A = Impossible;
146 return;
147 }
148 BinaryOperator::Opcode Op = B->getOpcode();
149
150 // Dereference the LHS SVal if this is an assign operation
151 switch (Op) {
152 default:
153 break;
154
155 // Fall through intentional
156 case BinaryOperator::AddAssign:
157 case BinaryOperator::SubAssign:
158 case BinaryOperator::MulAssign:
159 case BinaryOperator::DivAssign:
160 case BinaryOperator::AndAssign:
161 case BinaryOperator::OrAssign:
162 case BinaryOperator::XorAssign:
163 case BinaryOperator::ShlAssign:
164 case BinaryOperator::ShrAssign:
165 case BinaryOperator::Assign:
166 // Assign statements have one extra level of indirection
167 if (!isa<Loc>(LHSVal)) {
168 A = Impossible;
169 return;
170 }
171 LHSVal = state->getSVal(cast<Loc>(LHSVal));
172 }
173
174
175 // We now check for various cases which result in an idempotent operation.
176
177 // x op x
178 switch (Op) {
179 default:
180 break; // We don't care about any other operators.
181
182 // Fall through intentional
183 case BinaryOperator::SubAssign:
184 case BinaryOperator::DivAssign:
185 case BinaryOperator::AndAssign:
186 case BinaryOperator::OrAssign:
187 case BinaryOperator::XorAssign:
188 case BinaryOperator::Assign:
189 case BinaryOperator::Sub:
190 case BinaryOperator::Div:
191 case BinaryOperator::And:
192 case BinaryOperator::Or:
193 case BinaryOperator::Xor:
194 case BinaryOperator::LOr:
195 case BinaryOperator::LAnd:
196 if (LHSVal != RHSVal)
197 break;
198 UpdateAssumption(A, Equal);
199 return;
200 }
201
202 // x op 1
203 switch (Op) {
204 default:
205 break; // We don't care about any other operators.
206
207 // Fall through intentional
208 case BinaryOperator::MulAssign:
209 case BinaryOperator::DivAssign:
210 case BinaryOperator::Mul:
211 case BinaryOperator::Div:
212 case BinaryOperator::LOr:
213 case BinaryOperator::LAnd:
214 if (!RHSVal.isConstant(1))
215 break;
216 UpdateAssumption(A, RHSis1);
217 return;
218 }
219
220 // 1 op x
221 switch (Op) {
222 default:
223 break; // We don't care about any other operators.
224
225 // Fall through intentional
226 case BinaryOperator::MulAssign:
227 case BinaryOperator::Mul:
228 case BinaryOperator::LOr:
229 case BinaryOperator::LAnd:
230 if (!LHSVal.isConstant(1))
231 break;
232 UpdateAssumption(A, LHSis1);
233 return;
234 }
235
236 // x op 0
237 switch (Op) {
238 default:
239 break; // We don't care about any other operators.
240
241 // Fall through intentional
242 case BinaryOperator::AddAssign:
243 case BinaryOperator::SubAssign:
244 case BinaryOperator::MulAssign:
245 case BinaryOperator::AndAssign:
246 case BinaryOperator::OrAssign:
247 case BinaryOperator::XorAssign:
248 case BinaryOperator::Add:
249 case BinaryOperator::Sub:
250 case BinaryOperator::Mul:
251 case BinaryOperator::And:
252 case BinaryOperator::Or:
253 case BinaryOperator::Xor:
254 case BinaryOperator::Shl:
255 case BinaryOperator::Shr:
256 case BinaryOperator::LOr:
257 case BinaryOperator::LAnd:
258 if (!RHSVal.isConstant(0))
259 break;
260 UpdateAssumption(A, RHSis0);
261 return;
262 }
263
264 // 0 op x
265 switch (Op) {
266 default:
267 break; // We don't care about any other operators.
268
269 // Fall through intentional
270 //case BinaryOperator::AddAssign: // Common false positive
271 case BinaryOperator::SubAssign: // Check only if unsigned
272 case BinaryOperator::MulAssign:
273 case BinaryOperator::DivAssign:
274 case BinaryOperator::AndAssign:
275 //case BinaryOperator::OrAssign: // Common false positive
276 //case BinaryOperator::XorAssign: // Common false positive
277 case BinaryOperator::ShlAssign:
278 case BinaryOperator::ShrAssign:
279 case BinaryOperator::Add:
280 case BinaryOperator::Sub:
281 case BinaryOperator::Mul:
282 case BinaryOperator::Div:
283 case BinaryOperator::And:
284 case BinaryOperator::Or:
285 case BinaryOperator::Xor:
286 case BinaryOperator::Shl:
287 case BinaryOperator::Shr:
288 case BinaryOperator::LOr:
289 case BinaryOperator::LAnd:
290 if (!LHSVal.isConstant(0))
291 break;
292 UpdateAssumption(A, LHSis0);
293 return;
294 }
295
296 // If we get to this point, there has been a valid use of this operation.
297 A = Impossible;
298}
299
300void IdempotentOperationChecker::VisitEndAnalysis(ExplodedGraph &G,
301 BugReporter &B,
302 bool hasWorkRemaining) {
303 // If there is any work remaining we cannot be 100% sure about our warnings
304 if (hasWorkRemaining)
305 return;
306
307 // Iterate over the hash to see if we have any paths with definite
308 // idempotent operations.
309 for (AssumptionMap::const_iterator i =
310 hash.begin(); i != hash.end(); ++i) {
311 if (i->second != Impossible) {
312 // Select the error message.
313 const char *msg;
314 switch (i->second) {
315 case Equal:
316 msg = "idempotent operation; both operands are always equal in value";
317 break;
318 case LHSis1:
319 msg = "idempotent operation; the left operand is always 1";
320 break;
321 case RHSis1:
322 msg = "idempotent operation; the right operand is always 1";
323 break;
324 case LHSis0:
325 msg = "idempotent operation; the left operand is always 0";
326 break;
327 case RHSis0:
328 msg = "idempotent operation; the right operand is always 0";
329 break;
330 case Impossible:
331 break;
332 case Possible:
333 assert(0 && "Operation was never marked with an assumption");
334 }
335
336 // Create the SourceRange Arrays
337 SourceRange S[2] = { i->first->getLHS()->getSourceRange(),
338 i->first->getRHS()->getSourceRange() };
339 B.EmitBasicReport("Idempotent operation", msg, i->first->getOperatorLoc(),
340 S, 2);
341 }
342 }
343}
344
345// Updates the current assumption given the new assumption
346inline void IdempotentOperationChecker::UpdateAssumption(Assumption &A,
347 const Assumption &New) {
348 switch (A) {
349 // If we don't currently have an assumption, set it
350 case Possible:
351 A = New;
352 return;
353
354 // If we have determined that a valid state happened, ignore the new
355 // assumption.
356 case Impossible:
357 return;
358
359 // Any other case means that we had a different assumption last time. We don't
360 // currently support mixing assumptions for diagnostic reasons, so we set
361 // our assumption to be impossible.
362 default:
363 A = Impossible;
364 return;
365 }
366}
367
368// Recursively find any substatements containing macros
369bool IdempotentOperationChecker::containsMacro(const Stmt *S) {
370 if (S->getLocStart().isMacroID())
371 return true;
372
373 if (S->getLocEnd().isMacroID())
374 return true;
375
376 for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
377 ++I)
378 if (const Stmt *child = *I)
379 if (containsMacro(child))
380 return true;
381
382 return false;
383}
384
385// Recursively find any substatements containing enum constants
386bool IdempotentOperationChecker::containsEnum(const Stmt *S) {
387 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
388
389 if (DR && isa<EnumConstantDecl>(DR->getDecl()))
390 return true;
391
392 for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
393 ++I)
394 if (const Stmt *child = *I)
395 if (containsEnum(child))
396 return true;
397
398 return false;
399}
400
401// Recursively find any substatements containing __builtin_offset_of
402bool IdempotentOperationChecker::containsBuiltinOffsetOf(const Stmt *S) {
403 const UnaryOperator *UO = dyn_cast<UnaryOperator>(S);
404
405 if (UO && UO->getOpcode() == UnaryOperator::OffsetOf)
406 return true;
407
408 for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
409 ++I)
410 if (const Stmt *child = *I)
411 if (containsBuiltinOffsetOf(child))
412 return true;
413
414 return false;
415}
416
417bool IdempotentOperationChecker::containsZeroConstant(const Stmt *S) {
418 const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(S);
419 if (IL && IL->getValue() == 0)
420 return true;
421
422 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(S);
423 if (FL && FL->getValue().isZero())
424 return true;
425
426 for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
427 ++I)
428 if (const Stmt *child = *I)
429 if (containsZeroConstant(child))
430 return true;
431
432 return false;
433}
434
435bool IdempotentOperationChecker::containsOneConstant(const Stmt *S) {
436 const IntegerLiteral *IL = dyn_cast<IntegerLiteral>(S);
437 if (IL && IL->getValue() == 1)
438 return true;
439
440 const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(S);
441 const llvm::APFloat one(1.0);
442 if (FL && FL->getValue().compare(one) == llvm::APFloat::cmpEqual)
443 return true;
444
445 for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
446 ++I)
447 if (const Stmt *child = *I)
448 if (containsOneConstant(child))
449 return true;
450
451 return false;
452}
453