1 //==- IdempotentOperationChecker.cpp - Idempotent Operations ----*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
37 //===----------------------------------------------------------------------===//
40 // - Improved error messages
41 // - Handle mixed assumptions (which assumptions can belong together?)
42 // - Finer grained false positive control (levels)
43 // - Handling ~0 values
45 #include "ClangSACheckers.h"
46 #include "clang/Analysis/CFGStmtMap.h"
47 #include "clang/Analysis/Analyses/PseudoConstantAnalysis.h"
48 #include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h"
49 #include "clang/StaticAnalyzer/Core/Checker.h"
50 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
51 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
52 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
53 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
54 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
55 #include "clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h"
56 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
57 #include "clang/AST/Stmt.h"
58 #include "llvm/ADT/DenseMap.h"
59 #include "llvm/ADT/SmallSet.h"
60 #include "llvm/ADT/SmallString.h"
61 #include "llvm/ADT/BitVector.h"
62 #include "llvm/Support/ErrorHandling.h"
64 using namespace clang;
68 class IdempotentOperationChecker
69 : public Checker<check::PreStmt<BinaryOperator>,
70 check::PostStmt<BinaryOperator>,
73 void checkPreStmt(const BinaryOperator *B, CheckerContext &C) const;
74 void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const;
75 void checkEndAnalysis(ExplodedGraph &G, BugReporter &B,ExprEngine &Eng) const;
78 // Our assumption about a particular operation.
79 enum Assumption { Possible = 0, Impossible, Equal, LHSis1, RHSis1, LHSis0,
82 static void UpdateAssumption(Assumption &A, const Assumption &New);
84 // False positive reduction methods
85 static bool isSelfAssign(const Expr *LHS, const Expr *RHS);
86 static bool isUnused(const Expr *E, AnalysisDeclContext *AC);
87 static bool isTruncationExtensionAssignment(const Expr *LHS,
89 static bool pathWasCompletelyAnalyzed(AnalysisDeclContext *AC,
91 const CoreEngine &CE);
92 static bool CanVary(const Expr *Ex,
93 AnalysisDeclContext *AC);
94 static bool isConstantOrPseudoConstant(const DeclRefExpr *DR,
95 AnalysisDeclContext *AC);
96 static bool containsNonLocalVarDecl(const Stmt *S);
98 // Hash table and related data structures
99 struct BinaryOperatorData {
100 BinaryOperatorData() : assumption(Possible) {}
102 Assumption assumption;
103 ExplodedNodeSet explodedNodes; // Set of ExplodedNodes that refer to a
106 typedef llvm::DenseMap<const BinaryOperator *, BinaryOperatorData>
108 mutable AssumptionMap hash;
112 void IdempotentOperationChecker::checkPreStmt(const BinaryOperator *B,
113 CheckerContext &C) const {
114 // 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
116 // 'Possible'. At this stage we do not store the ExplodedNode, as it has not
118 BinaryOperatorData &Data = hash[B];
119 Assumption &A = Data.assumption;
120 AnalysisDeclContext *AC = C.getCurrentAnalysisDeclContext();
122 // If we already have visited this node on a path that does not contain an
123 // idempotent operation, return immediately.
127 // Retrieve both sides of the operator and determine if they can vary (which
128 // may mean this is a false positive.
129 const Expr *LHS = B->getLHS();
130 const Expr *RHS = B->getRHS();
132 // At this stage we can calculate whether each side contains a false positive
133 // that applies to all operators. We only need to calculate this the first
135 bool LHSContainsFalsePositive = false, RHSContainsFalsePositive = false;
137 // An expression contains a false positive if it can't vary, or if it
138 // contains a known false positive VarDecl.
139 LHSContainsFalsePositive = !CanVary(LHS, AC)
140 || containsNonLocalVarDecl(LHS);
141 RHSContainsFalsePositive = !CanVary(RHS, AC)
142 || containsNonLocalVarDecl(RHS);
145 ProgramStateRef state = C.getState();
146 const LocationContext *LCtx = C.getLocationContext();
147 SVal LHSVal = state->getSVal(LHS, LCtx);
148 SVal RHSVal = state->getSVal(RHS, LCtx);
150 // If either value is unknown, we can't be 100% sure of all paths.
151 if (LHSVal.isUnknownOrUndef() || RHSVal.isUnknownOrUndef()) {
155 BinaryOperator::Opcode Op = B->getOpcode();
157 // Dereference the LHS SVal if this is an assign operation
162 // Fall through intentional
173 // Assign statements have one extra level of indirection
174 if (!isa<Loc>(LHSVal)) {
178 LHSVal = state->getSVal(cast<Loc>(LHSVal), LHS->getType());
182 // We now check for various cases which result in an idempotent operation.
187 break; // We don't care about any other operators.
189 // Fall through intentional
191 // x Assign x can be used to silence unused variable warnings intentionally.
192 // If this is a self assignment and the variable is referenced elsewhere,
193 // and the assignment is not a truncation or extension, then it is a false
195 if (isSelfAssign(LHS, RHS)) {
196 if (!isUnused(LHS, AC) && !isTruncationExtensionAssignment(LHS, RHS)) {
197 UpdateAssumption(A, Equal);
220 if (LHSVal != RHSVal || LHSContainsFalsePositive
221 || RHSContainsFalsePositive)
223 UpdateAssumption(A, Equal);
230 break; // We don't care about any other operators.
232 // Fall through intentional
239 if (!RHSVal.isConstant(1) || RHSContainsFalsePositive)
241 UpdateAssumption(A, RHSis1);
248 break; // We don't care about any other operators.
250 // Fall through intentional
255 if (!LHSVal.isConstant(1) || LHSContainsFalsePositive)
257 UpdateAssumption(A, LHSis1);
264 break; // We don't care about any other operators.
266 // Fall through intentional
283 if (!RHSVal.isConstant(0) || RHSContainsFalsePositive)
285 UpdateAssumption(A, RHSis0);
292 break; // We don't care about any other operators.
294 // Fall through intentional
295 //case BO_AddAssign: // Common false positive
296 case BO_SubAssign: // Check only if unsigned
300 //case BO_OrAssign: // Common false positive
301 //case BO_XorAssign: // Common false positive
315 if (!LHSVal.isConstant(0) || LHSContainsFalsePositive)
317 UpdateAssumption(A, LHSis0);
321 // If we get to this point, there has been a valid use of this operation.
325 // At the post visit stage, the predecessor ExplodedNode will be the
326 // BinaryOperator that was just created. We use this hook to collect the
328 void IdempotentOperationChecker::checkPostStmt(const BinaryOperator *B,
329 CheckerContext &C) const {
330 // Add the ExplodedNode we just visited
331 BinaryOperatorData &Data = hash[B];
334 = cast<StmtPoint>(C.getPredecessor()->getLocation()).getStmt();
336 // Ignore implicit calls to setters.
337 if (!isa<BinaryOperator>(predStmt))
340 Data.explodedNodes.Add(C.getPredecessor());
343 void IdempotentOperationChecker::checkEndAnalysis(ExplodedGraph &G,
345 ExprEngine &Eng) const {
346 BugType *BT = new BugType("Idempotent operation", "Dead code");
347 // Iterate over the hash to see if we have any paths with definite
348 // idempotent operations.
349 for (AssumptionMap::const_iterator i = hash.begin(); i != hash.end(); ++i) {
350 // Unpack the hash contents
351 const BinaryOperatorData &Data = i->second;
352 const Assumption &A = Data.assumption;
353 const ExplodedNodeSet &ES = Data.explodedNodes;
355 // If there are no nodes accosted with the expression, nothing to report.
356 // FIXME: This is possible because the checker does part of processing in
357 // checkPreStmt and part in checkPostStmt.
358 if (ES.begin() == ES.end())
361 const BinaryOperator *B = i->first;
366 // If the analyzer did not finish, check to see if we can still emit this
368 if (Eng.hasWorkRemaining()) {
369 // If we can trace back
370 AnalysisDeclContext *AC = (*ES.begin())->getLocationContext()
371 ->getAnalysisDeclContext();
372 if (!pathWasCompletelyAnalyzed(AC,
373 AC->getCFGStmtMap()->getBlock(B),
374 Eng.getCoreEngine()))
378 // Select the error message and SourceRanges to report.
379 SmallString<128> buf;
380 llvm::raw_svector_ostream os(buf);
381 bool LHSRelevant = false, RHSRelevant = false;
386 if (B->getOpcode() == BO_Assign)
387 os << "Assigned value is always the same as the existing value";
389 os << "Both operands to '" << B->getOpcodeStr()
390 << "' always have the same value";
394 os << "The left operand to '" << B->getOpcodeStr() << "' is always 1";
398 os << "The right operand to '" << B->getOpcodeStr() << "' is always 1";
402 os << "The left operand to '" << B->getOpcodeStr() << "' is always 0";
406 os << "The right operand to '" << B->getOpcodeStr() << "' is always 0";
409 llvm_unreachable("Operation was never marked with an assumption");
414 // Add a report for each ExplodedNode
415 for (ExplodedNodeSet::iterator I = ES.begin(), E = ES.end(); I != E; ++I) {
416 BugReport *report = new BugReport(*BT, os.str(), *I);
418 // Add source ranges and visitor hooks
420 const Expr *LHS = i->first->getLHS();
421 report->addRange(LHS->getSourceRange());
422 FindLastStoreBRVisitor::registerStatementVarDecls(*report, LHS);
425 const Expr *RHS = i->first->getRHS();
426 report->addRange(i->first->getRHS()->getSourceRange());
427 FindLastStoreBRVisitor::registerStatementVarDecls(*report, RHS);
430 BR.EmitReport(report);
437 // Updates the current assumption given the new assumption
438 inline void IdempotentOperationChecker::UpdateAssumption(Assumption &A,
439 const Assumption &New) {
440 // If the assumption is the same, there is nothing to do
445 // If we don't currently have an assumption, set it
450 // If we have determined that a valid state happened, ignore the new
455 // Any other case means that we had a different assumption last time. We don't
456 // currently support mixing assumptions for diagnostic reasons, so we set
457 // our assumption to be impossible.
464 // Check for a statement where a variable is self assigned to possibly avoid an
465 // unused variable warning.
466 bool IdempotentOperationChecker::isSelfAssign(const Expr *LHS, const Expr *RHS) {
467 LHS = LHS->IgnoreParenCasts();
468 RHS = RHS->IgnoreParenCasts();
470 const DeclRefExpr *LHS_DR = dyn_cast<DeclRefExpr>(LHS);
474 const VarDecl *VD = dyn_cast<VarDecl>(LHS_DR->getDecl());
478 const DeclRefExpr *RHS_DR = dyn_cast<DeclRefExpr>(RHS);
482 if (VD != RHS_DR->getDecl())
488 // Returns true if the Expr points to a VarDecl that is not read anywhere
489 // outside of self-assignments.
490 bool IdempotentOperationChecker::isUnused(const Expr *E,
491 AnalysisDeclContext *AC) {
495 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts());
499 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
503 if (AC->getPseudoConstantAnalysis()->wasReferenced(VD))
509 // Check for self casts truncating/extending a variable
510 bool IdempotentOperationChecker::isTruncationExtensionAssignment(
514 const DeclRefExpr *LHS_DR = dyn_cast<DeclRefExpr>(LHS->IgnoreParenCasts());
518 const VarDecl *VD = dyn_cast<VarDecl>(LHS_DR->getDecl());
522 const DeclRefExpr *RHS_DR = dyn_cast<DeclRefExpr>(RHS->IgnoreParenCasts());
526 if (VD != RHS_DR->getDecl())
529 return dyn_cast<DeclRefExpr>(RHS->IgnoreParenLValueCasts()) == NULL;
532 // Returns false if a path to this block was not completely analyzed, or true
535 IdempotentOperationChecker::pathWasCompletelyAnalyzed(AnalysisDeclContext *AC,
537 const CoreEngine &CE) {
539 CFGReverseBlockReachabilityAnalysis *CRA = AC->getCFGReachablityAnalysis();
541 // Test for reachability from any aborted blocks to this block
542 typedef CoreEngine::BlocksExhausted::const_iterator ExhaustedIterator;
543 for (ExhaustedIterator I = CE.blocks_exhausted_begin(),
544 E = CE.blocks_exhausted_end(); I != E; ++I) {
545 const BlockEdge &BE = I->first;
547 // The destination block on the BlockEdge is the first block that was not
548 // analyzed. If we can reach this block from the aborted block, then this
549 // block was not completely analyzed.
551 // Also explicitly check if the current block is the destination block.
552 // While technically reachable, it means we aborted the analysis on
553 // a path that included that block.
554 const CFGBlock *destBlock = BE.getDst();
555 if (destBlock == CB || CRA->isReachable(destBlock, CB))
559 // Test for reachability from blocks we just gave up on.
560 typedef CoreEngine::BlocksAborted::const_iterator AbortedIterator;
561 for (AbortedIterator I = CE.blocks_aborted_begin(),
562 E = CE.blocks_aborted_end(); I != E; ++I) {
563 const CFGBlock *destBlock = I->first;
564 if (destBlock == CB || CRA->isReachable(destBlock, CB))
568 // For the items still on the worklist, see if they are in blocks that
569 // can eventually reach 'CB'.
570 class VisitWL : public WorkList::Visitor {
571 const CFGStmtMap *CBM;
572 const CFGBlock *TargetBlock;
573 CFGReverseBlockReachabilityAnalysis &CRA;
575 VisitWL(const CFGStmtMap *cbm, const CFGBlock *targetBlock,
576 CFGReverseBlockReachabilityAnalysis &cra)
577 : CBM(cbm), TargetBlock(targetBlock), CRA(cra) {}
578 virtual bool visit(const WorkListUnit &U) {
579 ProgramPoint P = U.getNode()->getLocation();
580 const CFGBlock *B = 0;
581 if (StmtPoint *SP = dyn_cast<StmtPoint>(&P)) {
582 B = CBM->getBlock(SP->getStmt());
584 else if (BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
587 else if (BlockEntrance *BEnt = dyn_cast<BlockEntrance>(&P)) {
588 B = BEnt->getBlock();
590 else if (BlockExit *BExit = dyn_cast<BlockExit>(&P)) {
591 B = BExit->getBlock();
596 return B == TargetBlock || CRA.isReachable(B, TargetBlock);
599 VisitWL visitWL(AC->getCFGStmtMap(), CB, *CRA);
600 // Were there any items in the worklist that could potentially reach
602 if (CE.getWorkList()->visitItemsInWorkList(visitWL))
605 // Verify that this block is reachable from the entry block
606 if (!CRA->isReachable(&AC->getCFG()->getEntry(), CB))
609 // If we get to this point, there is no connection to the entry block or an
610 // aborted block. This path is unreachable and we can report the error.
614 // Recursive function that determines whether an expression contains any element
615 // that varies. This could be due to a compile-time constant like sizeof. An
616 // expression may also involve a variable that behaves like a constant. The
617 // function returns true if the expression varies, and false otherwise.
618 bool IdempotentOperationChecker::CanVary(const Expr *Ex,
619 AnalysisDeclContext *AC) {
620 // Parentheses and casts are irrelevant here
621 Ex = Ex->IgnoreParenCasts();
623 if (Ex->getLocStart().isMacroID())
626 switch (Ex->getStmtClass()) {
627 // Trivially true cases
628 case Stmt::ArraySubscriptExprClass:
629 case Stmt::MemberExprClass:
630 case Stmt::StmtExprClass:
631 case Stmt::CallExprClass:
632 case Stmt::VAArgExprClass:
633 case Stmt::ShuffleVectorExprClass:
638 // Trivially false cases
639 case Stmt::IntegerLiteralClass:
640 case Stmt::CharacterLiteralClass:
641 case Stmt::FloatingLiteralClass:
642 case Stmt::PredefinedExprClass:
643 case Stmt::ImaginaryLiteralClass:
644 case Stmt::StringLiteralClass:
645 case Stmt::OffsetOfExprClass:
646 case Stmt::CompoundLiteralExprClass:
647 case Stmt::AddrLabelExprClass:
648 case Stmt::BinaryTypeTraitExprClass:
649 case Stmt::GNUNullExprClass:
650 case Stmt::InitListExprClass:
651 case Stmt::DesignatedInitExprClass:
652 case Stmt::BlockExprClass:
655 // Cases requiring custom logic
656 case Stmt::UnaryExprOrTypeTraitExprClass: {
657 const UnaryExprOrTypeTraitExpr *SE =
658 cast<const UnaryExprOrTypeTraitExpr>(Ex);
659 if (SE->getKind() != UETT_SizeOf)
661 return SE->getTypeOfArgument()->isVariableArrayType();
663 case Stmt::DeclRefExprClass:
664 // Check for constants/pseudoconstants
665 return !isConstantOrPseudoConstant(cast<DeclRefExpr>(Ex), AC);
667 // The next cases require recursion for subexpressions
668 case Stmt::BinaryOperatorClass: {
669 const BinaryOperator *B = cast<const BinaryOperator>(Ex);
671 // Exclude cases involving pointer arithmetic. These are usually
673 if (B->getOpcode() == BO_Sub || B->getOpcode() == BO_Add)
674 if (B->getLHS()->getType()->getAs<PointerType>())
677 return CanVary(B->getRHS(), AC)
678 || CanVary(B->getLHS(), AC);
680 case Stmt::UnaryOperatorClass: {
681 const UnaryOperator *U = cast<const UnaryOperator>(Ex);
682 // Handle trivial case first
683 switch (U->getOpcode()) {
687 return CanVary(U->getSubExpr(), AC);
690 case Stmt::ChooseExprClass:
691 return CanVary(cast<const ChooseExpr>(Ex)->getChosenSubExpr(
692 AC->getASTContext()), AC);
693 case Stmt::ConditionalOperatorClass:
694 case Stmt::BinaryConditionalOperatorClass:
695 return CanVary(cast<AbstractConditionalOperator>(Ex)->getCond(), AC);
699 // Returns true if a DeclRefExpr is or behaves like a constant.
700 bool IdempotentOperationChecker::isConstantOrPseudoConstant(
701 const DeclRefExpr *DR,
702 AnalysisDeclContext *AC) {
703 // Check if the type of the Decl is const-qualified
704 if (DR->getType().isConstQualified())
708 if (isa<EnumConstantDecl>(DR->getDecl()))
711 const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl());
715 // Check if the Decl behaves like a constant. This check also takes care of
716 // static variables, which can only change between function calls if they are
717 // modified in the AST.
718 PseudoConstantAnalysis *PCA = AC->getPseudoConstantAnalysis();
719 if (PCA->isPseudoConstant(VD))
725 // Recursively find any substatements containing VarDecl's with storage other
727 bool IdempotentOperationChecker::containsNonLocalVarDecl(const Stmt *S) {
728 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
731 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
732 if (!VD->hasLocalStorage())
735 for (Stmt::const_child_iterator I = S->child_begin(); I != S->child_end();
737 if (const Stmt *child = *I)
738 if (containsNonLocalVarDecl(child))
745 void ento::registerIdempotentOperationChecker(CheckerManager &mgr) {
746 mgr.registerChecker<IdempotentOperationChecker>();