clang API Documentation

IdempotentOperationChecker.cpp
Go to the documentation of this file.
00001 //==- IdempotentOperationChecker.cpp - Idempotent Operations ----*- C++ -*-==//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines a set of path-sensitive checks for idempotent and/or
00011 // tautological operations. Each potential operation is checked along all paths
00012 // to see if every path results in a pointless operation.
00013 //                 +-------------------------------------------+
00014 //                 |Table of idempotent/tautological operations|
00015 //                 +-------------------------------------------+
00016 //+--------------------------------------------------------------------------+
00017 //|Operator | x op x | x op 1 | 1 op x | x op 0 | 0 op x | x op ~0 | ~0 op x |
00018 //+--------------------------------------------------------------------------+
00019 //  +, +=   |        |        |        |   x    |   x    |         |
00020 //  -, -=   |        |        |        |   x    |   -x   |         |
00021 //  *, *=   |        |   x    |   x    |   0    |   0    |         |
00022 //  /, /=   |   1    |   x    |        |  N/A   |   0    |         |
00023 //  &, &=   |   x    |        |        |   0    |   0    |   x     |    x
00024 //  |, |=   |   x    |        |        |   x    |   x    |   ~0    |    ~0
00025 //  ^, ^=   |   0    |        |        |   x    |   x    |         |
00026 //  <<, <<= |        |        |        |   x    |   0    |         |
00027 //  >>, >>= |        |        |        |   x    |   0    |         |
00028 //  ||      |   1    |   1    |   1    |   x    |   x    |   1     |    1
00029 //  &&      |   1    |   x    |   x    |   0    |   0    |   x     |    x
00030 //  =       |   x    |        |        |        |        |         |
00031 //  ==      |   1    |        |        |        |        |         |
00032 //  >=      |   1    |        |        |        |        |         |
00033 //  <=      |   1    |        |        |        |        |         |
00034 //  >       |   0    |        |        |        |        |         |
00035 //  <       |   0    |        |        |        |        |         |
00036 //  !=      |   0    |        |        |        |        |         |
00037 //===----------------------------------------------------------------------===//
00038 //
00039 // Things TODO:
00040 // - Improved error messages
00041 // - Handle mixed assumptions (which assumptions can belong together?)
00042 // - Finer grained false positive control (levels)
00043 // - Handling ~0 values
00044 
00045 #include "ClangSACheckers.h"
00046 #include "clang/Analysis/CFGStmtMap.h"
00047 #include "clang/Analysis/Analyses/PseudoConstantAnalysis.h"
00048 #include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h"
00049 #include "clang/StaticAnalyzer/Core/Checker.h"
00050 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
00051 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
00052 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
00053 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
00054 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
00055 #include "clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h"
00056 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
00057 #include "clang/AST/Stmt.h"
00058 #include "llvm/ADT/DenseMap.h"
00059 #include "llvm/ADT/SmallSet.h"
00060 #include "llvm/ADT/SmallString.h"
00061 #include "llvm/ADT/BitVector.h"
00062 #include "llvm/Support/ErrorHandling.h"
00063 
00064 using namespace clang;
00065 using namespace ento;
00066 
00067 namespace {
00068 class IdempotentOperationChecker
00069   : public Checker<check::PreStmt<BinaryOperator>,
00070                      check::PostStmt<BinaryOperator>,
00071                      check::EndAnalysis> {
00072 public:
00073   void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
00074   void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const;
00075   void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const;
00076 
00077 private:
00078   // Our assumption about a particular operation.
00079   enum Assumption { Possible = 0, Impossible, Equal, LHSis1, RHSis1, LHSis0,
00080       RHSis0 };
00081 
00082   static void UpdateAssumption(Assumption &A, const Assumption &New);
00083 
00084   // False positive reduction methods
00085   static bool isSelfAssign(const Expr *LHS, const Expr *RHS);
00086   static bool isUnused(const Expr *E, AnalysisDeclContext *AC);
00087   static bool isTruncationExtensionAssignment(const Expr *LHS,
00088                                               const Expr *RHS);
00089   static bool pathWasCompletelyAnalyzed(AnalysisDeclContext *AC,
00090                                         const CFGBlock *CB,
00091                                         const CoreEngine &CE);
00092   static bool CanVary(const Expr *Ex,
00093                       AnalysisDeclContext *AC);
00094   static bool isConstantOrPseudoConstant(const DeclRefExpr *DR,
00095                                          AnalysisDeclContext *AC);
00096   static bool containsNonLocalVarDecl(const Stmt *S);
00097 
00098   // Hash table and related data structures
00099   struct BinaryOperatorData {
00100     BinaryOperatorData() : assumption(Possible) {}
00101 
00102     Assumption assumption;
00103     ExplodedNodeSet explodedNodes; // Set of ExplodedNodes that refer to a
00104                                    // BinaryOperator
00105   };
00106   typedef llvm::DenseMap<const BinaryOperator *, BinaryOperatorData>
00107       AssumptionMap;
00108   mutable AssumptionMap hash;
00109 };
00110 }
00111 
00112 void IdempotentOperationChecker::checkPreStmt(const BinaryOperator *B,
00113                                               CheckerContext &C) const {
00114   // Find or create an entry in the hash for this BinaryOperator instance.
00115   // If we haven't done a lookup before, it will get default initialized to
00116   // 'Possible'. At this stage we do not store the ExplodedNode, as it has not
00117   // been created yet.
00118   BinaryOperatorData &Data = hash[B];
00119   Assumption &A = Data.assumption;
00120   AnalysisDeclContext *AC = C.getCurrentAnalysisDeclContext();
00121 
00122   // If we already have visited this node on a path that does not contain an
00123   // idempotent operation, return immediately.
00124   if (A == Impossible)
00125     return;
00126 
00127   // Retrieve both sides of the operator and determine if they can vary (which
00128   // may mean this is a false positive.
00129   const Expr *LHS = B->getLHS();
00130   const Expr *RHS = B->getRHS();
00131 
00132   // At this stage we can calculate whether each side contains a false positive
00133   // that applies to all operators. We only need to calculate this the first
00134   // time.
00135   bool LHSContainsFalsePositive = false, RHSContainsFalsePositive = false;
00136   if (A == Possible) {
00137     // An expression contains a false positive if it can't vary, or if it
00138     // contains a known false positive VarDecl.
00139     LHSContainsFalsePositive = !CanVary(LHS, AC)
00140         || containsNonLocalVarDecl(LHS);
00141     RHSContainsFalsePositive = !CanVary(RHS, AC)
00142         || containsNonLocalVarDecl(RHS);
00143   }
00144 
00145   ProgramStateRef state = C.getState();
00146   const LocationContext *LCtx = C.getLocationContext();
00147   SVal LHSVal = state->getSVal(LHS, LCtx);
00148   SVal RHSVal = state->getSVal(RHS, LCtx);
00149 
00150   // If either value is unknown, we can't be 100% sure of all paths.
00151   if (LHSVal.isUnknownOrUndef() || RHSVal.isUnknownOrUndef()) {
00152     A = Impossible;
00153     return;
00154   }
00155   BinaryOperator::Opcode Op = B->getOpcode();
00156 
00157   // Dereference the LHS SVal if this is an assign operation
00158   switch (Op) {
00159   default:
00160     break;
00161 
00162   // Fall through intentional
00163   case BO_AddAssign:
00164   case BO_SubAssign:
00165   case BO_MulAssign:
00166   case BO_DivAssign:
00167   case BO_AndAssign:
00168   case BO_OrAssign:
00169   case BO_XorAssign:
00170   case BO_ShlAssign:
00171   case BO_ShrAssign:
00172   case BO_Assign:
00173   // Assign statements have one extra level of indirection
00174     if (!isa<Loc>(LHSVal)) {
00175       A = Impossible;
00176       return;
00177     }
00178     LHSVal = state->getSVal(cast<Loc>(LHSVal), LHS->getType());
00179   }
00180 
00181 
00182   // We now check for various cases which result in an idempotent operation.
00183 
00184   // x op x
00185   switch (Op) {
00186   default:
00187     break; // We don't care about any other operators.
00188 
00189   // Fall through intentional
00190   case BO_Assign:
00191     // x Assign x can be used to silence unused variable warnings intentionally.
00192     // If this is a self assignment and the variable is referenced elsewhere,
00193     // and the assignment is not a truncation or extension, then it is a false
00194     // positive.
00195     if (isSelfAssign(LHS, RHS)) {
00196       if (!isUnused(LHS, AC) && !isTruncationExtensionAssignment(LHS, RHS)) {
00197         UpdateAssumption(A, Equal);
00198         return;
00199       }
00200       else {
00201         A = Impossible;
00202         return;
00203       }
00204     }
00205 
00206   case BO_SubAssign:
00207   case BO_DivAssign:
00208   case BO_AndAssign:
00209   case BO_OrAssign:
00210   case BO_XorAssign:
00211   case BO_Sub:
00212   case BO_Div:
00213   case BO_And:
00214   case BO_Or:
00215   case BO_Xor:
00216   case BO_LOr:
00217   case BO_LAnd:
00218   case BO_EQ:
00219   case BO_NE:
00220     if (LHSVal != RHSVal || LHSContainsFalsePositive
00221         || RHSContainsFalsePositive)
00222       break;
00223     UpdateAssumption(A, Equal);
00224     return;
00225   }
00226 
00227   // x op 1
00228   switch (Op) {
00229    default:
00230      break; // We don't care about any other operators.
00231 
00232    // Fall through intentional
00233    case BO_MulAssign:
00234    case BO_DivAssign:
00235    case BO_Mul:
00236    case BO_Div:
00237    case BO_LOr:
00238    case BO_LAnd:
00239      if (!RHSVal.isConstant(1) || RHSContainsFalsePositive)
00240        break;
00241      UpdateAssumption(A, RHSis1);
00242      return;
00243   }
00244 
00245   // 1 op x
00246   switch (Op) {
00247   default:
00248     break; // We don't care about any other operators.
00249 
00250   // Fall through intentional
00251   case BO_MulAssign:
00252   case BO_Mul:
00253   case BO_LOr:
00254   case BO_LAnd:
00255     if (!LHSVal.isConstant(1) || LHSContainsFalsePositive)
00256       break;
00257     UpdateAssumption(A, LHSis1);
00258     return;
00259   }
00260 
00261   // x op 0
00262   switch (Op) {
00263   default:
00264     break; // We don't care about any other operators.
00265 
00266   // Fall through intentional
00267   case BO_AddAssign:
00268   case BO_SubAssign:
00269   case BO_MulAssign:
00270   case BO_AndAssign:
00271   case BO_OrAssign:
00272   case BO_XorAssign:
00273   case BO_Add:
00274   case BO_Sub:
00275   case BO_Mul:
00276   case BO_And:
00277   case BO_Or:
00278   case BO_Xor:
00279   case BO_Shl:
00280   case BO_Shr:
00281   case BO_LOr:
00282   case BO_LAnd:
00283     if (!RHSVal.isConstant(0) || RHSContainsFalsePositive)
00284       break;
00285     UpdateAssumption(A, RHSis0);
00286     return;
00287   }
00288 
00289   // 0 op x
00290   switch (Op) {
00291   default:
00292     break; // We don't care about any other operators.
00293 
00294   // Fall through intentional
00295   //case BO_AddAssign: // Common false positive
00296   case BO_SubAssign: // Check only if unsigned
00297   case BO_MulAssign:
00298   case BO_DivAssign:
00299   case BO_AndAssign:
00300   //case BO_OrAssign: // Common false positive
00301   //case BO_XorAssign: // Common false positive
00302   case BO_ShlAssign:
00303   case BO_ShrAssign:
00304   case BO_Add:
00305   case BO_Sub:
00306   case BO_Mul:
00307   case BO_Div:
00308   case BO_And:
00309   case BO_Or:
00310   case BO_Xor:
00311   case BO_Shl:
00312   case BO_Shr:
00313   case BO_LOr:
00314   case BO_LAnd:
00315     if (!LHSVal.isConstant(0) || LHSContainsFalsePositive)
00316       break;
00317     UpdateAssumption(A, LHSis0);
00318     return;
00319   }
00320 
00321   // If we get to this point, there has been a valid use of this operation.
00322   A = Impossible;
00323 }
00324 
00325 // At the post visit stage, the predecessor ExplodedNode will be the
00326 // BinaryOperator that was just created. We use this hook to collect the
00327 // ExplodedNode.
00328 void IdempotentOperationChecker::checkPostStmt(const BinaryOperator *B,
00329                                                CheckerContext &C) const {
00330   // Add the ExplodedNode we just visited
00331   BinaryOperatorData &Data = hash[B];
00332 
00333   const Stmt *predStmt 
00334     = cast<StmtPoint>(C.getPredecessor()->getLocation()).getStmt();
00335   
00336   // Ignore implicit calls to setters.
00337   if (!isa<BinaryOperator>(predStmt))
00338     return;
00339 
00340   Data.explodedNodes.Add(C.getPredecessor());
00341 }
00342 
00343 void IdempotentOperationChecker::checkEndAnalysis(ExplodedGraph &G,
00344                                                   BugReporter &BR,
00345                                                   ExprEngine &Eng) const {
00346   BugType *BT = new BugType("Idempotent operation", "Dead code");
00347   // Iterate over the hash to see if we have any paths with definite
00348   // idempotent operations.
00349   for (AssumptionMap::const_iterator i = hash.begin(); i != hash.end(); ++i) {
00350     // Unpack the hash contents
00351     const BinaryOperatorData &Data = i->second;
00352     const Assumption &A = Data.assumption;
00353     const ExplodedNodeSet &ES = Data.explodedNodes;
00354 
00355     // If there are no nodes accosted with the expression, nothing to report.
00356     // FIXME: This is possible because the checker does part of processing in
00357     // checkPreStmt and part in checkPostStmt.
00358     if (ES.begin() == ES.end())
00359       continue;
00360 
00361     const BinaryOperator *B = i->first;
00362 
00363     if (A == Impossible)
00364       continue;
00365 
00366     // If the analyzer did not finish, check to see if we can still emit this
00367     // warning
00368     if (Eng.hasWorkRemaining()) {
00369       // If we can trace back
00370       AnalysisDeclContext *AC = (*ES.begin())->getLocationContext()
00371                                          ->getAnalysisDeclContext();
00372       if (!pathWasCompletelyAnalyzed(AC,
00373                                      AC->getCFGStmtMap()->getBlock(B),
00374                                      Eng.getCoreEngine()))
00375         continue;
00376     }
00377 
00378     // Select the error message and SourceRanges to report.
00379     SmallString<128> buf;
00380     llvm::raw_svector_ostream os(buf);
00381     bool LHSRelevant = false, RHSRelevant = false;
00382     switch (A) {
00383     case Equal:
00384       LHSRelevant = true;
00385       RHSRelevant = true;
00386       if (B->getOpcode() == BO_Assign)
00387         os << "Assigned value is always the same as the existing value";
00388       else
00389         os << "Both operands to '" << B->getOpcodeStr()
00390            << "' always have the same value";
00391       break;
00392     case LHSis1:
00393       LHSRelevant = true;
00394       os << "The left operand to '" << B->getOpcodeStr() << "' is always 1";
00395       break;
00396     case RHSis1:
00397       RHSRelevant = true;
00398       os << "The right operand to '" << B->getOpcodeStr() << "' is always 1";
00399       break;
00400     case LHSis0:
00401       LHSRelevant = true;
00402       os << "The left operand to '" << B->getOpcodeStr() << "' is always 0";
00403       break;
00404     case RHSis0:
00405       RHSRelevant = true;
00406       os << "The right operand to '" << B->getOpcodeStr() << "' is always 0";
00407       break;
00408     case Possible:
00409       llvm_unreachable("Operation was never marked with an assumption");
00410     case Impossible:
00411       llvm_unreachable(0);
00412     }
00413 
00414     // Add a report for each ExplodedNode
00415     for (ExplodedNodeSet::iterator I = ES.begin(), E = ES.end(); I != E; ++I) {
00416       BugReport *report = new BugReport(*BT, os.str(), *I);
00417 
00418       // Add source ranges and visitor hooks
00419       if (LHSRelevant) {
00420         const Expr *LHS = i->first->getLHS();
00421         report->addRange(LHS->getSourceRange());
00422         FindLastStoreBRVisitor::registerStatementVarDecls(*report, LHS);
00423       }
00424       if (RHSRelevant) {
00425         const Expr *RHS = i->first->getRHS();
00426         report->addRange(i->first->getRHS()->getSourceRange());
00427         FindLastStoreBRVisitor::registerStatementVarDecls(*report, RHS);
00428       }
00429 
00430       BR.EmitReport(report);
00431     }
00432   }
00433 
00434   hash.clear();
00435 }
00436 
00437 // Updates the current assumption given the new assumption
00438 inline void IdempotentOperationChecker::UpdateAssumption(Assumption &A,
00439                                                         const Assumption &New) {
00440 // If the assumption is the same, there is nothing to do
00441   if (A == New)
00442     return;
00443 
00444   switch (A) {
00445   // If we don't currently have an assumption, set it
00446   case Possible:
00447     A = New;
00448     return;
00449 
00450   // If we have determined that a valid state happened, ignore the new
00451   // assumption.
00452   case Impossible:
00453     return;
00454 
00455   // Any other case means that we had a different assumption last time. We don't
00456   // currently support mixing assumptions for diagnostic reasons, so we set
00457   // our assumption to be impossible.
00458   default:
00459     A = Impossible;
00460     return;
00461   }
00462 }
00463 
00464 // Check for a statement where a variable is self assigned to possibly avoid an
00465 // unused variable warning.
00466 bool IdempotentOperationChecker::isSelfAssign(const Expr *LHS, const Expr *RHS) {
00467   LHS = LHS->IgnoreParenCasts();
00468   RHS = RHS->IgnoreParenCasts();
00469 
00470   const DeclRefExpr *LHS_DR = dyn_cast<DeclRefExpr>(LHS);
00471   if (!LHS_DR)
00472     return false;
00473 
00474   const VarDecl *VD = dyn_cast<VarDecl>(LHS_DR->getDecl());
00475   if (!VD)
00476     return false;
00477 
00478   const DeclRefExpr *RHS_DR = dyn_cast<DeclRefExpr>(RHS);
00479   if (!RHS_DR)
00480     return false;
00481 
00482   if (VD != RHS_DR->getDecl())
00483     return false;
00484 
00485   return true;
00486 }
00487 
00488 // Returns true if the Expr points to a VarDecl that is not read anywhere
00489 // outside of self-assignments.
00490 bool IdempotentOperationChecker::isUnused(const Expr *E,
00491                                           AnalysisDeclContext *AC) {
00492   if (!E)
00493     return false;
00494 
00495   const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts());
00496   if (!DR)
00497     return false;
00498 
00499   const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
00500   if (!VD)
00501     return false;
00502 
00503   if (AC->getPseudoConstantAnalysis()->wasReferenced(VD))
00504     return false;
00505 
00506   return true;
00507 }
00508 
00509 // Check for self casts truncating/extending a variable
00510 bool IdempotentOperationChecker::isTruncationExtensionAssignment(
00511                                                               const Expr *LHS,
00512                                                               const Expr *RHS) {
00513 
00514   const DeclRefExpr *LHS_DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParenCasts());
00515   if (!LHS_DR)
00516     return false;
00517 
00518   const VarDecl *VD = dyn_cast<VarDecl>(LHS_DR->getDecl());
00519   if (!VD)
00520     return false;
00521 
00522   const DeclRefExpr *RHS_DR = dyn_cast<DeclRefExpr>(RHS->IgnoreParenCasts());
00523   if (!RHS_DR)
00524     return false;
00525 
00526   if (VD != RHS_DR->getDecl())
00527      return false;
00528 
00529   return dyn_cast<DeclRefExpr>(RHS->IgnoreParenLValueCasts()) == NULL;
00530 }
00531 
00532 // Returns false if a path to this block was not completely analyzed, or true
00533 // otherwise.
00534 bool
00535 IdempotentOperationChecker::pathWasCompletelyAnalyzed(AnalysisDeclContext *AC,
00536                                                       const CFGBlock *CB,
00537                                                       const CoreEngine &CE) {
00538 
00539   CFGReverseBlockReachabilityAnalysis *CRA = AC->getCFGReachablityAnalysis();
00540   
00541   // Test for reachability from any aborted blocks to this block
00542   typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator;
00543   for (ExhaustedIterator I = CE.blocks_exhausted_begin(),
00544       E = CE.blocks_exhausted_end(); I != E; ++I) {
00545     const BlockEdge &BE =  I->first;
00546 
00547     // The destination block on the BlockEdge is the first block that was not
00548     // analyzed. If we can reach this block from the aborted block, then this
00549     // block was not completely analyzed.
00550     //
00551     // Also explicitly check if the current block is the destination block.
00552     // While technically reachable, it means we aborted the analysis on
00553     // a path that included that block.
00554     const CFGBlock *destBlock = BE.getDst();
00555     if (destBlock == CB || CRA->isReachable(destBlock, CB))
00556       return false;
00557   }
00558 
00559   // Test for reachability from blocks we just gave up on.
00560   typedef CoreEngine::BlocksAborted::const_iterator AbortedIterator;
00561   for (AbortedIterator I = CE.blocks_aborted_begin(),
00562        E = CE.blocks_aborted_end(); I != E; ++I) {
00563     const CFGBlock *destBlock = I->first;
00564     if (destBlock == CB || CRA->isReachable(destBlock, CB))
00565       return false;
00566   }
00567   
00568   // For the items still on the worklist, see if they are in blocks that
00569   // can eventually reach 'CB'.
00570   class VisitWL : public WorkList::Visitor {
00571     const CFGStmtMap *CBM;
00572     const CFGBlock *TargetBlock;
00573     CFGReverseBlockReachabilityAnalysis &CRA;
00574   public:
00575     VisitWL(const CFGStmtMap *cbm, const CFGBlock *targetBlock,
00576             CFGReverseBlockReachabilityAnalysis &cra)
00577       : CBM(cbm), TargetBlock(targetBlock), CRA(cra) {}
00578     virtual bool visit(const WorkListUnit &U) {
00579       ProgramPoint P = U.getNode()->getLocation();
00580       const CFGBlock *B = 0;
00581       if (StmtPoint *SP = dyn_cast<StmtPoint>(&P)) {
00582         B = CBM->getBlock(SP->getStmt());
00583       }
00584       else if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
00585         B = BE->getDst();
00586       }
00587       else if (BlockEntrance *BEnt = dyn_cast<BlockEntrance>(&P)) {
00588         B = BEnt->getBlock();
00589       }
00590       else if (BlockExit *BExit = dyn_cast<BlockExit>(&P)) {
00591         B = BExit->getBlock();
00592       }
00593       if (!B)
00594         return true;
00595       
00596       return B == TargetBlock || CRA.isReachable(B, TargetBlock);
00597     }
00598   };
00599   VisitWL visitWL(AC->getCFGStmtMap(), CB, *CRA);
00600   // Were there any items in the worklist that could potentially reach
00601   // this block?
00602   if (CE.getWorkList()->visitItemsInWorkList(visitWL))
00603     return false;
00604 
00605   // Verify that this block is reachable from the entry block
00606   if (!CRA->isReachable(&AC->getCFG()->getEntry(), CB))
00607     return false;
00608 
00609   // If we get to this point, there is no connection to the entry block or an
00610   // aborted block. This path is unreachable and we can report the error.
00611   return true;
00612 }
00613 
00614 // Recursive function that determines whether an expression contains any element
00615 // that varies. This could be due to a compile-time constant like sizeof. An
00616 // expression may also involve a variable that behaves like a constant. The
00617 // function returns true if the expression varies, and false otherwise.
00618 bool IdempotentOperationChecker::CanVary(const Expr *Ex,
00619                                          AnalysisDeclContext *AC) {
00620   // Parentheses and casts are irrelevant here
00621   Ex = Ex->IgnoreParenCasts();
00622 
00623   if (Ex->getLocStart().isMacroID())
00624     return false;
00625 
00626   switch (Ex->getStmtClass()) {
00627   // Trivially true cases
00628   case Stmt::ArraySubscriptExprClass:
00629   case Stmt::MemberExprClass:
00630   case Stmt::StmtExprClass:
00631   case Stmt::CallExprClass:
00632   case Stmt::VAArgExprClass:
00633   case Stmt::ShuffleVectorExprClass:
00634     return true;
00635   default:
00636     return true;
00637 
00638   // Trivially false cases
00639   case Stmt::IntegerLiteralClass:
00640   case Stmt::CharacterLiteralClass:
00641   case Stmt::FloatingLiteralClass:
00642   case Stmt::PredefinedExprClass:
00643   case Stmt::ImaginaryLiteralClass:
00644   case Stmt::StringLiteralClass:
00645   case Stmt::OffsetOfExprClass:
00646   case Stmt::CompoundLiteralExprClass:
00647   case Stmt::AddrLabelExprClass:
00648   case Stmt::BinaryTypeTraitExprClass:
00649   case Stmt::GNUNullExprClass:
00650   case Stmt::InitListExprClass:
00651   case Stmt::DesignatedInitExprClass:
00652   case Stmt::BlockExprClass:
00653     return false;
00654 
00655   // Cases requiring custom logic
00656   case Stmt::UnaryExprOrTypeTraitExprClass: {
00657     const UnaryExprOrTypeTraitExpr *SE = 
00658                        cast<const UnaryExprOrTypeTraitExpr>(Ex);
00659     if (SE->getKind() != UETT_SizeOf)
00660       return false;
00661     return SE->getTypeOfArgument()->isVariableArrayType();
00662   }
00663   case Stmt::DeclRefExprClass:
00664     // Check for constants/pseudoconstants
00665     return !isConstantOrPseudoConstant(cast<DeclRefExpr>(Ex), AC);
00666 
00667   // The next cases require recursion for subexpressions
00668   case Stmt::BinaryOperatorClass: {
00669     const BinaryOperator *B = cast<const BinaryOperator>(Ex);
00670 
00671     // Exclude cases involving pointer arithmetic.  These are usually
00672     // false positives.
00673     if (B->getOpcode() == BO_Sub || B->getOpcode() == BO_Add)
00674       if (B->getLHS()->getType()->getAs<PointerType>())
00675         return false;
00676 
00677     return CanVary(B->getRHS(), AC)
00678         || CanVary(B->getLHS(), AC);
00679    }
00680   case Stmt::UnaryOperatorClass: {
00681     const UnaryOperator *U = cast<const UnaryOperator>(Ex);
00682     // Handle trivial case first
00683     switch (U->getOpcode()) {
00684     case UO_Extension:
00685       return false;
00686     default:
00687       return CanVary(U->getSubExpr(), AC);
00688     }
00689   }
00690   case Stmt::ChooseExprClass:
00691     return CanVary(cast<const ChooseExpr>(Ex)->getChosenSubExpr(
00692         AC->getASTContext()), AC);
00693   case Stmt::ConditionalOperatorClass:
00694   case Stmt::BinaryConditionalOperatorClass:
00695     return CanVary(cast<AbstractConditionalOperator>(Ex)->getCond(), AC);
00696   }
00697 }
00698 
00699 // Returns true if a DeclRefExpr is or behaves like a constant.
00700 bool IdempotentOperationChecker::isConstantOrPseudoConstant(
00701                                                           const DeclRefExpr *DR,
00702                                                           AnalysisDeclContext *AC) {
00703   // Check if the type of the Decl is const-qualified
00704   if (DR->getType().isConstQualified())
00705     return true;
00706 
00707   // Check for an enum
00708   if (isa<EnumConstantDecl>(DR->getDecl()))
00709     return true;
00710 
00711   const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
00712   if (!VD)
00713     return true;
00714 
00715   // Check if the Decl behaves like a constant. This check also takes care of
00716   // static variables, which can only change between function calls if they are
00717   // modified in the AST.
00718   PseudoConstantAnalysis *PCA = AC->getPseudoConstantAnalysis();
00719   if (PCA->isPseudoConstant(VD))
00720     return true;
00721 
00722   return false;
00723 }
00724 
00725 // Recursively find any substatements containing VarDecl's with storage other
00726 // than local
00727 bool IdempotentOperationChecker::containsNonLocalVarDecl(const Stmt *S) {
00728   const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
00729 
00730   if (DR)
00731     if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
00732       if (!VD->hasLocalStorage())
00733         return true;
00734 
00735   for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
00736       ++I)
00737     if (const Stmt *child = *I)
00738       if (containsNonLocalVarDecl(child))
00739         return true;
00740 
00741   return false;
00742 }
00743 
00744 
00745 void ento::registerIdempotentOperationChecker(CheckerManager &mgr) {
00746   mgr.registerChecker<IdempotentOperationChecker>();
00747 }