blob: 885123ae241a7d059926510f146fd2b284fa4cda [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
Tom Care1fafd1d2010-08-06 22:23:07 +000045#include "GRExprEngineExperimentalChecks.h"
Tom Carea7a8a452010-08-12 22:45:47 +000046#include "clang/Analysis/CFGStmtMap.h"
Tom Care245adab2010-08-18 21:17:24 +000047#include "clang/Analysis/Analyses/PsuedoConstantAnalysis.h"
Tom Caredb2fa8a2010-07-06 21:43:29 +000048#include "clang/Checker/BugReporter/BugType.h"
Tom Carea9fbf5b2010-07-27 23:26:07 +000049#include "clang/Checker/PathSensitive/CheckerHelpers.h"
Tom Caredb2fa8a2010-07-06 21:43:29 +000050#include "clang/Checker/PathSensitive/CheckerVisitor.h"
Tom Carea7a8a452010-08-12 22:45:47 +000051#include "clang/Checker/PathSensitive/GRCoreEngine.h"
Tom Caredb2fa8a2010-07-06 21:43:29 +000052#include "clang/Checker/PathSensitive/SVals.h"
53#include "clang/AST/Stmt.h"
54#include "llvm/ADT/DenseMap.h"
Tom Carea7a8a452010-08-12 22:45:47 +000055#include "llvm/ADT/SmallSet.h"
Chandler Carruth256565b2010-07-07 00:07:37 +000056#include "llvm/Support/ErrorHandling.h"
Tom Carea7a8a452010-08-12 22:45:47 +000057#include <deque>
Tom Caredb2fa8a2010-07-06 21:43:29 +000058
59using namespace clang;
60
61namespace {
62class IdempotentOperationChecker
63 : public CheckerVisitor<IdempotentOperationChecker> {
64 public:
65 static void *getTag();
66 void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
Tom Carebc42c532010-08-03 01:55:07 +000067 void VisitEndAnalysis(ExplodedGraph &G, BugReporter &B, GRExprEngine &Eng);
Tom Caredb2fa8a2010-07-06 21:43:29 +000068
69 private:
70 // Our assumption about a particular operation.
Ted Kremenekfe97fa12010-08-02 20:33:02 +000071 enum Assumption { Possible = 0, Impossible, Equal, LHSis1, RHSis1, LHSis0,
Tom Caredb2fa8a2010-07-06 21:43:29 +000072 RHSis0 };
73
74 void UpdateAssumption(Assumption &A, const Assumption &New);
75
Tom Care245adab2010-08-18 21:17:24 +000076 // False positive reduction methods
Tom Caredf4ca422010-07-16 20:41:41 +000077 static bool isParameterSelfAssign(const Expr *LHS, const Expr *RHS);
78 static bool isTruncationExtensionAssignment(const Expr *LHS,
79 const Expr *RHS);
Tom Carea7a8a452010-08-12 22:45:47 +000080 static bool PathWasCompletelyAnalyzed(const CFG *C,
81 const CFGBlock *CB,
82 const GRCoreEngine &CE);
Tom Care245adab2010-08-18 21:17:24 +000083 static bool CanVary(const Expr *Ex, AnalysisContext *AC);
84 static bool isConstantOrPseudoConstant(const DeclRefExpr *DR,
85 AnalysisContext *AC);
Tom Caredb2fa8a2010-07-06 21:43:29 +000086
87 // Hash table
Tom Carea7a8a452010-08-12 22:45:47 +000088 typedef llvm::DenseMap<const BinaryOperator *,
89 std::pair<Assumption, AnalysisContext*> >
90 AssumptionMap;
Tom Caredb2fa8a2010-07-06 21:43:29 +000091 AssumptionMap hash;
92};
93}
94
95void *IdempotentOperationChecker::getTag() {
96 static int x = 0;
97 return &x;
98}
99
100void clang::RegisterIdempotentOperationChecker(GRExprEngine &Eng) {
101 Eng.registerCheck(new IdempotentOperationChecker());
102}
103
104void IdempotentOperationChecker::PreVisitBinaryOperator(
105 CheckerContext &C,
106 const BinaryOperator *B) {
Ted Kremenekfe97fa12010-08-02 20:33:02 +0000107 // Find or create an entry in the hash for this BinaryOperator instance.
108 // If we haven't done a lookup before, it will get default initialized to
109 // 'Possible'.
Tom Carea7a8a452010-08-12 22:45:47 +0000110 std::pair<Assumption, AnalysisContext *> &Data = hash[B];
111 Assumption &A = Data.first;
Tom Care245adab2010-08-18 21:17:24 +0000112 AnalysisContext *AC = C.getCurrentAnalysisContext();
113 Data.second = AC;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000114
115 // If we already have visited this node on a path that does not contain an
116 // idempotent operation, return immediately.
117 if (A == Impossible)
118 return;
119
Tom Carea7a8a452010-08-12 22:45:47 +0000120 // Retrieve both sides of the operator and determine if they can vary (which
121 // may mean this is a false positive.
Tom Caredb2fa8a2010-07-06 21:43:29 +0000122 const Expr *LHS = B->getLHS();
123 const Expr *RHS = B->getRHS();
Tom Care245adab2010-08-18 21:17:24 +0000124
125 // Check if either side can vary. We only need to calculate this when we have
126 // no assumption.
127 bool LHSCanVary = true, RHSCanVary = true;
128 if (A == Possible) {
129 LHSCanVary = CanVary(LHS, AC);
130 RHSCanVary = CanVary(RHS, AC);
131 }
Tom Caredb2fa8a2010-07-06 21:43:29 +0000132
133 const GRState *state = C.getState();
134
135 SVal LHSVal = state->getSVal(LHS);
136 SVal RHSVal = state->getSVal(RHS);
137
138 // If either value is unknown, we can't be 100% sure of all paths.
139 if (LHSVal.isUnknownOrUndef() || RHSVal.isUnknownOrUndef()) {
140 A = Impossible;
141 return;
142 }
143 BinaryOperator::Opcode Op = B->getOpcode();
144
145 // Dereference the LHS SVal if this is an assign operation
146 switch (Op) {
147 default:
148 break;
149
150 // Fall through intentional
151 case BinaryOperator::AddAssign:
152 case BinaryOperator::SubAssign:
153 case BinaryOperator::MulAssign:
154 case BinaryOperator::DivAssign:
155 case BinaryOperator::AndAssign:
156 case BinaryOperator::OrAssign:
157 case BinaryOperator::XorAssign:
158 case BinaryOperator::ShlAssign:
159 case BinaryOperator::ShrAssign:
160 case BinaryOperator::Assign:
161 // Assign statements have one extra level of indirection
162 if (!isa<Loc>(LHSVal)) {
163 A = Impossible;
164 return;
165 }
166 LHSVal = state->getSVal(cast<Loc>(LHSVal));
167 }
168
169
170 // We now check for various cases which result in an idempotent operation.
171
172 // x op x
173 switch (Op) {
174 default:
175 break; // We don't care about any other operators.
176
177 // Fall through intentional
Tom Caredf4ca422010-07-16 20:41:41 +0000178 case BinaryOperator::Assign:
179 // x Assign x has a few more false positives we can check for
180 if (isParameterSelfAssign(RHS, LHS)
181 || isTruncationExtensionAssignment(RHS, LHS)) {
182 A = Impossible;
183 return;
184 }
185
Tom Caredb2fa8a2010-07-06 21:43:29 +0000186 case BinaryOperator::SubAssign:
187 case BinaryOperator::DivAssign:
188 case BinaryOperator::AndAssign:
189 case BinaryOperator::OrAssign:
190 case BinaryOperator::XorAssign:
Tom Caredb2fa8a2010-07-06 21:43:29 +0000191 case BinaryOperator::Sub:
192 case BinaryOperator::Div:
193 case BinaryOperator::And:
194 case BinaryOperator::Or:
195 case BinaryOperator::Xor:
196 case BinaryOperator::LOr:
197 case BinaryOperator::LAnd:
Tom Carea7a8a452010-08-12 22:45:47 +0000198 if (LHSVal != RHSVal || !LHSCanVary || !RHSCanVary)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000199 break;
200 UpdateAssumption(A, Equal);
201 return;
202 }
203
204 // x op 1
205 switch (Op) {
206 default:
207 break; // We don't care about any other operators.
208
209 // Fall through intentional
210 case BinaryOperator::MulAssign:
211 case BinaryOperator::DivAssign:
212 case BinaryOperator::Mul:
213 case BinaryOperator::Div:
214 case BinaryOperator::LOr:
215 case BinaryOperator::LAnd:
Tom Carea7a8a452010-08-12 22:45:47 +0000216 if (!RHSVal.isConstant(1) || !RHSCanVary)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000217 break;
218 UpdateAssumption(A, RHSis1);
219 return;
220 }
221
222 // 1 op x
223 switch (Op) {
224 default:
225 break; // We don't care about any other operators.
226
227 // Fall through intentional
228 case BinaryOperator::MulAssign:
229 case BinaryOperator::Mul:
230 case BinaryOperator::LOr:
231 case BinaryOperator::LAnd:
Tom Carea7a8a452010-08-12 22:45:47 +0000232 if (!LHSVal.isConstant(1) || !LHSCanVary)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000233 break;
234 UpdateAssumption(A, LHSis1);
235 return;
236 }
237
238 // x op 0
239 switch (Op) {
240 default:
241 break; // We don't care about any other operators.
242
243 // Fall through intentional
244 case BinaryOperator::AddAssign:
245 case BinaryOperator::SubAssign:
246 case BinaryOperator::MulAssign:
247 case BinaryOperator::AndAssign:
248 case BinaryOperator::OrAssign:
249 case BinaryOperator::XorAssign:
250 case BinaryOperator::Add:
251 case BinaryOperator::Sub:
252 case BinaryOperator::Mul:
253 case BinaryOperator::And:
254 case BinaryOperator::Or:
255 case BinaryOperator::Xor:
256 case BinaryOperator::Shl:
257 case BinaryOperator::Shr:
258 case BinaryOperator::LOr:
259 case BinaryOperator::LAnd:
Tom Carea7a8a452010-08-12 22:45:47 +0000260 if (!RHSVal.isConstant(0) || !RHSCanVary)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000261 break;
262 UpdateAssumption(A, RHSis0);
263 return;
264 }
265
266 // 0 op x
267 switch (Op) {
268 default:
269 break; // We don't care about any other operators.
270
271 // Fall through intentional
272 //case BinaryOperator::AddAssign: // Common false positive
273 case BinaryOperator::SubAssign: // Check only if unsigned
274 case BinaryOperator::MulAssign:
275 case BinaryOperator::DivAssign:
276 case BinaryOperator::AndAssign:
277 //case BinaryOperator::OrAssign: // Common false positive
278 //case BinaryOperator::XorAssign: // Common false positive
279 case BinaryOperator::ShlAssign:
280 case BinaryOperator::ShrAssign:
281 case BinaryOperator::Add:
282 case BinaryOperator::Sub:
283 case BinaryOperator::Mul:
284 case BinaryOperator::Div:
285 case BinaryOperator::And:
286 case BinaryOperator::Or:
287 case BinaryOperator::Xor:
288 case BinaryOperator::Shl:
289 case BinaryOperator::Shr:
290 case BinaryOperator::LOr:
291 case BinaryOperator::LAnd:
Tom Carea7a8a452010-08-12 22:45:47 +0000292 if (!LHSVal.isConstant(0) || !LHSCanVary)
Tom Caredb2fa8a2010-07-06 21:43:29 +0000293 break;
294 UpdateAssumption(A, LHSis0);
295 return;
296 }
297
298 // If we get to this point, there has been a valid use of this operation.
299 A = Impossible;
300}
301
302void IdempotentOperationChecker::VisitEndAnalysis(ExplodedGraph &G,
Ted Kremenek3e5637f2010-07-27 18:49:08 +0000303 BugReporter &BR,
Tom Carebc42c532010-08-03 01:55:07 +0000304 GRExprEngine &Eng) {
Tom Caredb2fa8a2010-07-06 21:43:29 +0000305 // Iterate over the hash to see if we have any paths with definite
306 // idempotent operations.
Tom Carea7a8a452010-08-12 22:45:47 +0000307 for (AssumptionMap::const_iterator i = hash.begin(); i != hash.end(); ++i) {
308 // Unpack the hash contents
309 const std::pair<Assumption, AnalysisContext *> &Data = i->second;
310 const Assumption &A = Data.first;
311 AnalysisContext *AC = Data.second;
Ted Kremenek3e5637f2010-07-27 18:49:08 +0000312
Tom Carea7a8a452010-08-12 22:45:47 +0000313 const BinaryOperator *B = i->first;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000314
Tom Carea7a8a452010-08-12 22:45:47 +0000315 if (A == Impossible)
316 continue;
317
318 // If the analyzer did not finish, check to see if we can still emit this
319 // warning
320 if (Eng.hasWorkRemaining()) {
321 const CFGStmtMap *CBM = CFGStmtMap::Build(AC->getCFG(),
322 &AC->getParentMap());
323
324 // If we can trace back
325 if (!PathWasCompletelyAnalyzed(AC->getCFG(),
326 CBM->getBlock(B),
327 Eng.getCoreEngine()))
328 continue;
329
330 delete CBM;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000331 }
Tom Carea7a8a452010-08-12 22:45:47 +0000332
333 // Select the error message.
334 llvm::SmallString<128> buf;
335 llvm::raw_svector_ostream os(buf);
336 switch (A) {
337 case Equal:
338 if (B->getOpcode() == BinaryOperator::Assign)
339 os << "Assigned value is always the same as the existing value";
340 else
341 os << "Both operands to '" << B->getOpcodeStr()
342 << "' always have the same value";
343 break;
344 case LHSis1:
345 os << "The left operand to '" << B->getOpcodeStr() << "' is always 1";
346 break;
347 case RHSis1:
348 os << "The right operand to '" << B->getOpcodeStr() << "' is always 1";
349 break;
350 case LHSis0:
351 os << "The left operand to '" << B->getOpcodeStr() << "' is always 0";
352 break;
353 case RHSis0:
354 os << "The right operand to '" << B->getOpcodeStr() << "' is always 0";
355 break;
356 case Possible:
357 llvm_unreachable("Operation was never marked with an assumption");
358 case Impossible:
359 llvm_unreachable(0);
360 }
361
362 // Create the SourceRange Arrays
363 SourceRange S[2] = { i->first->getLHS()->getSourceRange(),
364 i->first->getRHS()->getSourceRange() };
365 BR.EmitBasicReport("Idempotent operation", "Dead code",
366 os.str(), i->first->getOperatorLoc(), S, 2);
Tom Caredb2fa8a2010-07-06 21:43:29 +0000367 }
368}
369
370// Updates the current assumption given the new assumption
371inline void IdempotentOperationChecker::UpdateAssumption(Assumption &A,
372 const Assumption &New) {
373 switch (A) {
374 // If we don't currently have an assumption, set it
375 case Possible:
376 A = New;
377 return;
378
379 // If we have determined that a valid state happened, ignore the new
380 // assumption.
381 case Impossible:
382 return;
383
384 // Any other case means that we had a different assumption last time. We don't
385 // currently support mixing assumptions for diagnostic reasons, so we set
386 // our assumption to be impossible.
387 default:
388 A = Impossible;
389 return;
390 }
391}
392
Tom Caredf4ca422010-07-16 20:41:41 +0000393// Check for a statement were a parameter is self assigned (to avoid an unused
394// variable warning)
395bool IdempotentOperationChecker::isParameterSelfAssign(const Expr *LHS,
396 const Expr *RHS) {
397 LHS = LHS->IgnoreParenCasts();
398 RHS = RHS->IgnoreParenCasts();
399
400 const DeclRefExpr *LHS_DR = dyn_cast<DeclRefExpr>(LHS);
401 if (!LHS_DR)
402 return false;
403
404 const ParmVarDecl *PD = dyn_cast<ParmVarDecl>(LHS_DR->getDecl());
405 if (!PD)
406 return false;
407
408 const DeclRefExpr *RHS_DR = dyn_cast<DeclRefExpr>(RHS);
409 if (!RHS_DR)
410 return false;
411
412 return PD == RHS_DR->getDecl();
413}
414
415// Check for self casts truncating/extending a variable
416bool IdempotentOperationChecker::isTruncationExtensionAssignment(
417 const Expr *LHS,
418 const Expr *RHS) {
419
420 const DeclRefExpr *LHS_DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParenCasts());
421 if (!LHS_DR)
422 return false;
423
424 const VarDecl *VD = dyn_cast<VarDecl>(LHS_DR->getDecl());
425 if (!VD)
426 return false;
427
428 const DeclRefExpr *RHS_DR = dyn_cast<DeclRefExpr>(RHS->IgnoreParenCasts());
429 if (!RHS_DR)
430 return false;
431
432 if (VD != RHS_DR->getDecl())
433 return false;
434
435 return dyn_cast<DeclRefExpr>(RHS->IgnoreParens()) == NULL;
436}
437
Tom Carea7a8a452010-08-12 22:45:47 +0000438// Returns false if a path to this block was not completely analyzed, or true
439// otherwise.
440bool IdempotentOperationChecker::PathWasCompletelyAnalyzed(
441 const CFG *C,
442 const CFGBlock *CB,
443 const GRCoreEngine &CE) {
444 std::deque<const CFGBlock *> WorkList;
445 llvm::SmallSet<unsigned, 8> Aborted;
446 llvm::SmallSet<unsigned, 128> Visited;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000447
Tom Carea7a8a452010-08-12 22:45:47 +0000448 // Create a set of all aborted blocks
449 typedef GRCoreEngine::BlocksAborted::const_iterator AbortedIterator;
450 for (AbortedIterator I = CE.blocks_aborted_begin(),
451 E = CE.blocks_aborted_end(); I != E; ++I) {
452 const BlockEdge &BE = I->first;
Tom Caredb2fa8a2010-07-06 21:43:29 +0000453
Tom Carea7a8a452010-08-12 22:45:47 +0000454 // The destination block on the BlockEdge is the first block that was not
455 // analyzed.
456 Aborted.insert(BE.getDst()->getBlockID());
Ted Kremenek45329312010-07-17 00:40:32 +0000457 }
Tom Caredb2fa8a2010-07-06 21:43:29 +0000458
Tom Carea7a8a452010-08-12 22:45:47 +0000459 // Save the entry block ID for early exiting
460 unsigned EntryBlockID = C->getEntry().getBlockID();
Tom Caredb2fa8a2010-07-06 21:43:29 +0000461
Tom Carea7a8a452010-08-12 22:45:47 +0000462 // Create initial node
463 WorkList.push_back(CB);
464
465 while (!WorkList.empty()) {
466 const CFGBlock *Head = WorkList.front();
467 WorkList.pop_front();
468 Visited.insert(Head->getBlockID());
469
470 // If we found the entry block, then there exists a path from the target
471 // node to the entry point of this function -> the path was completely
472 // analyzed.
473 if (Head->getBlockID() == EntryBlockID)
474 return true;
475
476 // If any of the aborted blocks are on the path to the beginning, then all
477 // paths to this block were not analyzed.
478 if (Aborted.count(Head->getBlockID()))
479 return false;
480
481 // Add the predecessors to the worklist unless we have already visited them
482 for (CFGBlock::const_pred_iterator I = Head->pred_begin();
483 I != Head->pred_end(); ++I)
484 if (!Visited.count((*I)->getBlockID()))
485 WorkList.push_back(*I);
486 }
487
488 // If we get to this point, there is no connection to the entry block or an
489 // aborted block. This path is unreachable and we can report the error.
490 return true;
491}
492
493// Recursive function that determines whether an expression contains any element
494// that varies. This could be due to a compile-time constant like sizeof. An
495// expression may also involve a variable that behaves like a constant. The
496// function returns true if the expression varies, and false otherwise.
Tom Care245adab2010-08-18 21:17:24 +0000497bool IdempotentOperationChecker::CanVary(const Expr *Ex,
498 AnalysisContext *AC) {
Tom Carea7a8a452010-08-12 22:45:47 +0000499 // Parentheses and casts are irrelevant here
500 Ex = Ex->IgnoreParenCasts();
501
502 if (Ex->getLocStart().isMacroID())
503 return false;
504
505 switch (Ex->getStmtClass()) {
506 // Trivially true cases
507 case Stmt::ArraySubscriptExprClass:
508 case Stmt::MemberExprClass:
509 case Stmt::StmtExprClass:
510 case Stmt::CallExprClass:
511 case Stmt::VAArgExprClass:
512 case Stmt::ShuffleVectorExprClass:
513 return true;
514 default:
515 return true;
516
517 // Trivially false cases
518 case Stmt::IntegerLiteralClass:
519 case Stmt::CharacterLiteralClass:
520 case Stmt::FloatingLiteralClass:
521 case Stmt::PredefinedExprClass:
522 case Stmt::ImaginaryLiteralClass:
523 case Stmt::StringLiteralClass:
524 case Stmt::OffsetOfExprClass:
525 case Stmt::CompoundLiteralExprClass:
526 case Stmt::AddrLabelExprClass:
527 case Stmt::TypesCompatibleExprClass:
528 case Stmt::GNUNullExprClass:
529 case Stmt::InitListExprClass:
530 case Stmt::DesignatedInitExprClass:
531 case Stmt::BlockExprClass:
532 case Stmt::BlockDeclRefExprClass:
533 return false;
534
535 // Cases requiring custom logic
536 case Stmt::SizeOfAlignOfExprClass: {
537 const SizeOfAlignOfExpr *SE = cast<const SizeOfAlignOfExpr>(Ex);
538 if (!SE->isSizeOf())
539 return false;
540 return SE->getTypeOfArgument()->isVariableArrayType();
541 }
542 case Stmt::DeclRefExprClass:
Tom Care245adab2010-08-18 21:17:24 +0000543 return !isConstantOrPseudoConstant(cast<DeclRefExpr>(Ex), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000544
545 // The next cases require recursion for subexpressions
546 case Stmt::BinaryOperatorClass: {
547 const BinaryOperator *B = cast<const BinaryOperator>(Ex);
Tom Care245adab2010-08-18 21:17:24 +0000548 return CanVary(B->getRHS(), AC)
549 || CanVary(B->getLHS(), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000550 }
551 case Stmt::UnaryOperatorClass: {
552 const UnaryOperator *U = cast<const UnaryOperator>(Ex);
Eli Friedmande7e6622010-08-13 01:36:11 +0000553 // Handle trivial case first
Tom Carea7a8a452010-08-12 22:45:47 +0000554 switch (U->getOpcode()) {
555 case UnaryOperator::Extension:
Tom Carea7a8a452010-08-12 22:45:47 +0000556 return false;
557 default:
Tom Care245adab2010-08-18 21:17:24 +0000558 return CanVary(U->getSubExpr(), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000559 }
560 }
561 case Stmt::ChooseExprClass:
Tom Care245adab2010-08-18 21:17:24 +0000562 return CanVary(cast<const ChooseExpr>(Ex)->getChosenSubExpr(
563 AC->getASTContext()), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000564 case Stmt::ConditionalOperatorClass:
Tom Care245adab2010-08-18 21:17:24 +0000565 return CanVary(cast<const ConditionalOperator>(Ex)->getCond(), AC);
Tom Carea7a8a452010-08-12 22:45:47 +0000566 }
Tom Caredb2fa8a2010-07-06 21:43:29 +0000567}
568
Tom Care245adab2010-08-18 21:17:24 +0000569// Returns true if a DeclRefExpr is or behaves like a constant.
570bool IdempotentOperationChecker::isConstantOrPseudoConstant(
571 const DeclRefExpr *DR,
572 AnalysisContext *AC) {
573 // Check if the type of the Decl is const-qualified
574 if (DR->getType().isConstQualified())
575 return true;
576
Tom Care50e8ac22010-08-16 21:43:52 +0000577 // Check for an enum
578 if (isa<EnumConstantDecl>(DR->getDecl()))
579 return true;
580
581 // Check for a static variable
582 // FIXME: Analysis should model static vars
583 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
584 if (VD->isStaticLocal())
585 return true;
586
Tom Care245adab2010-08-18 21:17:24 +0000587 // Check if the Decl behaves like a constant
588 PsuedoConstantAnalysis *PCA = AC->getPsuedoConstantAnalysis();
589 if (PCA->isPsuedoConstant(DR->getDecl()))
590 return true;
591
Tom Care50e8ac22010-08-16 21:43:52 +0000592 return false;
593}