106f32e7eSjoerg //== IdenticalExprChecker.cpp - Identical expression checker----------------==//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg ///
906f32e7eSjoerg /// \file
1006f32e7eSjoerg /// This defines IdenticalExprChecker, a check that warns about
1106f32e7eSjoerg /// unintended use of identical expressions.
1206f32e7eSjoerg ///
1306f32e7eSjoerg /// It checks for use of identical expressions with comparison operators and
1406f32e7eSjoerg /// inside conditional expressions.
1506f32e7eSjoerg ///
1606f32e7eSjoerg //===----------------------------------------------------------------------===//
1706f32e7eSjoerg 
1806f32e7eSjoerg #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
1906f32e7eSjoerg #include "clang/AST/RecursiveASTVisitor.h"
2006f32e7eSjoerg #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
2106f32e7eSjoerg #include "clang/StaticAnalyzer/Core/Checker.h"
2206f32e7eSjoerg #include "clang/StaticAnalyzer/Core/CheckerManager.h"
2306f32e7eSjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
2406f32e7eSjoerg 
2506f32e7eSjoerg using namespace clang;
2606f32e7eSjoerg using namespace ento;
2706f32e7eSjoerg 
2806f32e7eSjoerg static bool isIdenticalStmt(const ASTContext &Ctx, const Stmt *Stmt1,
2906f32e7eSjoerg                             const Stmt *Stmt2, bool IgnoreSideEffects = false);
3006f32e7eSjoerg //===----------------------------------------------------------------------===//
3106f32e7eSjoerg // FindIdenticalExprVisitor - Identify nodes using identical expressions.
3206f32e7eSjoerg //===----------------------------------------------------------------------===//
3306f32e7eSjoerg 
3406f32e7eSjoerg namespace {
3506f32e7eSjoerg class FindIdenticalExprVisitor
3606f32e7eSjoerg     : public RecursiveASTVisitor<FindIdenticalExprVisitor> {
3706f32e7eSjoerg   BugReporter &BR;
3806f32e7eSjoerg   const CheckerBase *Checker;
3906f32e7eSjoerg   AnalysisDeclContext *AC;
4006f32e7eSjoerg public:
FindIdenticalExprVisitor(BugReporter & B,const CheckerBase * Checker,AnalysisDeclContext * A)4106f32e7eSjoerg   explicit FindIdenticalExprVisitor(BugReporter &B,
4206f32e7eSjoerg                                     const CheckerBase *Checker,
4306f32e7eSjoerg                                     AnalysisDeclContext *A)
4406f32e7eSjoerg       : BR(B), Checker(Checker), AC(A) {}
4506f32e7eSjoerg   // FindIdenticalExprVisitor only visits nodes
4606f32e7eSjoerg   // that are binary operators, if statements or
4706f32e7eSjoerg   // conditional operators.
4806f32e7eSjoerg   bool VisitBinaryOperator(const BinaryOperator *B);
4906f32e7eSjoerg   bool VisitIfStmt(const IfStmt *I);
5006f32e7eSjoerg   bool VisitConditionalOperator(const ConditionalOperator *C);
5106f32e7eSjoerg 
5206f32e7eSjoerg private:
5306f32e7eSjoerg   void reportIdenticalExpr(const BinaryOperator *B, bool CheckBitwise,
5406f32e7eSjoerg                            ArrayRef<SourceRange> Sr);
5506f32e7eSjoerg   void checkBitwiseOrLogicalOp(const BinaryOperator *B, bool CheckBitwise);
5606f32e7eSjoerg   void checkComparisonOp(const BinaryOperator *B);
5706f32e7eSjoerg };
5806f32e7eSjoerg } // end anonymous namespace
5906f32e7eSjoerg 
reportIdenticalExpr(const BinaryOperator * B,bool CheckBitwise,ArrayRef<SourceRange> Sr)6006f32e7eSjoerg void FindIdenticalExprVisitor::reportIdenticalExpr(const BinaryOperator *B,
6106f32e7eSjoerg                                                    bool CheckBitwise,
6206f32e7eSjoerg                                                    ArrayRef<SourceRange> Sr) {
6306f32e7eSjoerg   StringRef Message;
6406f32e7eSjoerg   if (CheckBitwise)
6506f32e7eSjoerg     Message = "identical expressions on both sides of bitwise operator";
6606f32e7eSjoerg   else
6706f32e7eSjoerg     Message = "identical expressions on both sides of logical operator";
6806f32e7eSjoerg 
6906f32e7eSjoerg   PathDiagnosticLocation ELoc =
7006f32e7eSjoerg       PathDiagnosticLocation::createOperatorLoc(B, BR.getSourceManager());
7106f32e7eSjoerg   BR.EmitBasicReport(AC->getDecl(), Checker,
7206f32e7eSjoerg                      "Use of identical expressions",
7306f32e7eSjoerg                      categories::LogicError,
7406f32e7eSjoerg                      Message, ELoc, Sr);
7506f32e7eSjoerg }
7606f32e7eSjoerg 
checkBitwiseOrLogicalOp(const BinaryOperator * B,bool CheckBitwise)7706f32e7eSjoerg void FindIdenticalExprVisitor::checkBitwiseOrLogicalOp(const BinaryOperator *B,
7806f32e7eSjoerg                                                        bool CheckBitwise) {
7906f32e7eSjoerg   SourceRange Sr[2];
8006f32e7eSjoerg 
8106f32e7eSjoerg   const Expr *LHS = B->getLHS();
8206f32e7eSjoerg   const Expr *RHS = B->getRHS();
8306f32e7eSjoerg 
8406f32e7eSjoerg   // Split operators as long as we still have operators to split on. We will
8506f32e7eSjoerg   // get called for every binary operator in an expression so there is no need
8606f32e7eSjoerg   // to check every one against each other here, just the right most one with
8706f32e7eSjoerg   // the others.
8806f32e7eSjoerg   while (const BinaryOperator *B2 = dyn_cast<BinaryOperator>(LHS)) {
8906f32e7eSjoerg     if (B->getOpcode() != B2->getOpcode())
9006f32e7eSjoerg       break;
9106f32e7eSjoerg     if (isIdenticalStmt(AC->getASTContext(), RHS, B2->getRHS())) {
9206f32e7eSjoerg       Sr[0] = RHS->getSourceRange();
9306f32e7eSjoerg       Sr[1] = B2->getRHS()->getSourceRange();
9406f32e7eSjoerg       reportIdenticalExpr(B, CheckBitwise, Sr);
9506f32e7eSjoerg     }
9606f32e7eSjoerg     LHS = B2->getLHS();
9706f32e7eSjoerg   }
9806f32e7eSjoerg 
9906f32e7eSjoerg   if (isIdenticalStmt(AC->getASTContext(), RHS, LHS)) {
10006f32e7eSjoerg     Sr[0] = RHS->getSourceRange();
10106f32e7eSjoerg     Sr[1] = LHS->getSourceRange();
10206f32e7eSjoerg     reportIdenticalExpr(B, CheckBitwise, Sr);
10306f32e7eSjoerg   }
10406f32e7eSjoerg }
10506f32e7eSjoerg 
VisitIfStmt(const IfStmt * I)10606f32e7eSjoerg bool FindIdenticalExprVisitor::VisitIfStmt(const IfStmt *I) {
10706f32e7eSjoerg   const Stmt *Stmt1 = I->getThen();
10806f32e7eSjoerg   const Stmt *Stmt2 = I->getElse();
10906f32e7eSjoerg 
11006f32e7eSjoerg   // Check for identical inner condition:
11106f32e7eSjoerg   //
11206f32e7eSjoerg   // if (x<10) {
11306f32e7eSjoerg   //   if (x<10) {
11406f32e7eSjoerg   //   ..
11506f32e7eSjoerg   if (const CompoundStmt *CS = dyn_cast<CompoundStmt>(Stmt1)) {
11606f32e7eSjoerg     if (!CS->body_empty()) {
11706f32e7eSjoerg       const IfStmt *InnerIf = dyn_cast<IfStmt>(*CS->body_begin());
11806f32e7eSjoerg       if (InnerIf && isIdenticalStmt(AC->getASTContext(), I->getCond(), InnerIf->getCond(), /*IgnoreSideEffects=*/ false)) {
11906f32e7eSjoerg         PathDiagnosticLocation ELoc(InnerIf->getCond(), BR.getSourceManager(), AC);
12006f32e7eSjoerg         BR.EmitBasicReport(AC->getDecl(), Checker, "Identical conditions",
12106f32e7eSjoerg           categories::LogicError,
12206f32e7eSjoerg           "conditions of the inner and outer statements are identical",
12306f32e7eSjoerg           ELoc);
12406f32e7eSjoerg       }
12506f32e7eSjoerg     }
12606f32e7eSjoerg   }
12706f32e7eSjoerg 
12806f32e7eSjoerg   // Check for identical conditions:
12906f32e7eSjoerg   //
13006f32e7eSjoerg   // if (b) {
13106f32e7eSjoerg   //   foo1();
13206f32e7eSjoerg   // } else if (b) {
13306f32e7eSjoerg   //   foo2();
13406f32e7eSjoerg   // }
13506f32e7eSjoerg   if (Stmt1 && Stmt2) {
13606f32e7eSjoerg     const Expr *Cond1 = I->getCond();
13706f32e7eSjoerg     const Stmt *Else = Stmt2;
13806f32e7eSjoerg     while (const IfStmt *I2 = dyn_cast_or_null<IfStmt>(Else)) {
13906f32e7eSjoerg       const Expr *Cond2 = I2->getCond();
14006f32e7eSjoerg       if (isIdenticalStmt(AC->getASTContext(), Cond1, Cond2, false)) {
14106f32e7eSjoerg         SourceRange Sr = Cond1->getSourceRange();
14206f32e7eSjoerg         PathDiagnosticLocation ELoc(Cond2, BR.getSourceManager(), AC);
14306f32e7eSjoerg         BR.EmitBasicReport(AC->getDecl(), Checker, "Identical conditions",
14406f32e7eSjoerg                            categories::LogicError,
14506f32e7eSjoerg                            "expression is identical to previous condition",
14606f32e7eSjoerg                            ELoc, Sr);
14706f32e7eSjoerg       }
14806f32e7eSjoerg       Else = I2->getElse();
14906f32e7eSjoerg     }
15006f32e7eSjoerg   }
15106f32e7eSjoerg 
15206f32e7eSjoerg   if (!Stmt1 || !Stmt2)
15306f32e7eSjoerg     return true;
15406f32e7eSjoerg 
15506f32e7eSjoerg   // Special handling for code like:
15606f32e7eSjoerg   //
15706f32e7eSjoerg   // if (b) {
15806f32e7eSjoerg   //   i = 1;
15906f32e7eSjoerg   // } else
16006f32e7eSjoerg   //   i = 1;
16106f32e7eSjoerg   if (const CompoundStmt *CompStmt = dyn_cast<CompoundStmt>(Stmt1)) {
16206f32e7eSjoerg     if (CompStmt->size() == 1)
16306f32e7eSjoerg       Stmt1 = CompStmt->body_back();
16406f32e7eSjoerg   }
16506f32e7eSjoerg   if (const CompoundStmt *CompStmt = dyn_cast<CompoundStmt>(Stmt2)) {
16606f32e7eSjoerg     if (CompStmt->size() == 1)
16706f32e7eSjoerg       Stmt2 = CompStmt->body_back();
16806f32e7eSjoerg   }
16906f32e7eSjoerg 
17006f32e7eSjoerg   if (isIdenticalStmt(AC->getASTContext(), Stmt1, Stmt2, true)) {
17106f32e7eSjoerg       PathDiagnosticLocation ELoc =
17206f32e7eSjoerg           PathDiagnosticLocation::createBegin(I, BR.getSourceManager(), AC);
17306f32e7eSjoerg       BR.EmitBasicReport(AC->getDecl(), Checker,
17406f32e7eSjoerg                          "Identical branches",
17506f32e7eSjoerg                          categories::LogicError,
17606f32e7eSjoerg                          "true and false branches are identical", ELoc);
17706f32e7eSjoerg   }
17806f32e7eSjoerg   return true;
17906f32e7eSjoerg }
18006f32e7eSjoerg 
VisitBinaryOperator(const BinaryOperator * B)18106f32e7eSjoerg bool FindIdenticalExprVisitor::VisitBinaryOperator(const BinaryOperator *B) {
18206f32e7eSjoerg   BinaryOperator::Opcode Op = B->getOpcode();
18306f32e7eSjoerg 
18406f32e7eSjoerg   if (BinaryOperator::isBitwiseOp(Op))
18506f32e7eSjoerg     checkBitwiseOrLogicalOp(B, true);
18606f32e7eSjoerg 
18706f32e7eSjoerg   if (BinaryOperator::isLogicalOp(Op))
18806f32e7eSjoerg     checkBitwiseOrLogicalOp(B, false);
18906f32e7eSjoerg 
19006f32e7eSjoerg   if (BinaryOperator::isComparisonOp(Op))
19106f32e7eSjoerg     checkComparisonOp(B);
19206f32e7eSjoerg 
19306f32e7eSjoerg   // We want to visit ALL nodes (subexpressions of binary comparison
19406f32e7eSjoerg   // expressions too) that contains comparison operators.
19506f32e7eSjoerg   // True is always returned to traverse ALL nodes.
19606f32e7eSjoerg   return true;
19706f32e7eSjoerg }
19806f32e7eSjoerg 
checkComparisonOp(const BinaryOperator * B)19906f32e7eSjoerg void FindIdenticalExprVisitor::checkComparisonOp(const BinaryOperator *B) {
20006f32e7eSjoerg   BinaryOperator::Opcode Op = B->getOpcode();
20106f32e7eSjoerg 
20206f32e7eSjoerg   //
20306f32e7eSjoerg   // Special case for floating-point representation.
20406f32e7eSjoerg   //
20506f32e7eSjoerg   // If expressions on both sides of comparison operator are of type float,
20606f32e7eSjoerg   // then for some comparison operators no warning shall be
20706f32e7eSjoerg   // reported even if the expressions are identical from a symbolic point of
20806f32e7eSjoerg   // view. Comparison between expressions, declared variables and literals
20906f32e7eSjoerg   // are treated differently.
21006f32e7eSjoerg   //
21106f32e7eSjoerg   // != and == between float literals that have the same value should NOT warn.
21206f32e7eSjoerg   // < > between float literals that have the same value SHOULD warn.
21306f32e7eSjoerg   //
21406f32e7eSjoerg   // != and == between the same float declaration should NOT warn.
21506f32e7eSjoerg   // < > between the same float declaration SHOULD warn.
21606f32e7eSjoerg   //
21706f32e7eSjoerg   // != and == between eq. expressions that evaluates into float
21806f32e7eSjoerg   //           should NOT warn.
21906f32e7eSjoerg   // < >       between eq. expressions that evaluates into float
22006f32e7eSjoerg   //           should NOT warn.
22106f32e7eSjoerg   //
22206f32e7eSjoerg   const Expr *LHS = B->getLHS()->IgnoreParenImpCasts();
22306f32e7eSjoerg   const Expr *RHS = B->getRHS()->IgnoreParenImpCasts();
22406f32e7eSjoerg 
22506f32e7eSjoerg   const DeclRefExpr *DeclRef1 = dyn_cast<DeclRefExpr>(LHS);
22606f32e7eSjoerg   const DeclRefExpr *DeclRef2 = dyn_cast<DeclRefExpr>(RHS);
22706f32e7eSjoerg   const FloatingLiteral *FloatLit1 = dyn_cast<FloatingLiteral>(LHS);
22806f32e7eSjoerg   const FloatingLiteral *FloatLit2 = dyn_cast<FloatingLiteral>(RHS);
22906f32e7eSjoerg   if ((DeclRef1) && (DeclRef2)) {
23006f32e7eSjoerg     if ((DeclRef1->getType()->hasFloatingRepresentation()) &&
23106f32e7eSjoerg         (DeclRef2->getType()->hasFloatingRepresentation())) {
23206f32e7eSjoerg       if (DeclRef1->getDecl() == DeclRef2->getDecl()) {
23306f32e7eSjoerg         if ((Op == BO_EQ) || (Op == BO_NE)) {
23406f32e7eSjoerg           return;
23506f32e7eSjoerg         }
23606f32e7eSjoerg       }
23706f32e7eSjoerg     }
23806f32e7eSjoerg   } else if ((FloatLit1) && (FloatLit2)) {
23906f32e7eSjoerg     if (FloatLit1->getValue().bitwiseIsEqual(FloatLit2->getValue())) {
24006f32e7eSjoerg       if ((Op == BO_EQ) || (Op == BO_NE)) {
24106f32e7eSjoerg         return;
24206f32e7eSjoerg       }
24306f32e7eSjoerg     }
24406f32e7eSjoerg   } else if (LHS->getType()->hasFloatingRepresentation()) {
24506f32e7eSjoerg     // If any side of comparison operator still has floating-point
24606f32e7eSjoerg     // representation, then it's an expression. Don't warn.
24706f32e7eSjoerg     // Here only LHS is checked since RHS will be implicit casted to float.
24806f32e7eSjoerg     return;
24906f32e7eSjoerg   } else {
25006f32e7eSjoerg     // No special case with floating-point representation, report as usual.
25106f32e7eSjoerg   }
25206f32e7eSjoerg 
25306f32e7eSjoerg   if (isIdenticalStmt(AC->getASTContext(), B->getLHS(), B->getRHS())) {
25406f32e7eSjoerg     PathDiagnosticLocation ELoc =
25506f32e7eSjoerg         PathDiagnosticLocation::createOperatorLoc(B, BR.getSourceManager());
25606f32e7eSjoerg     StringRef Message;
25706f32e7eSjoerg     if (Op == BO_Cmp)
25806f32e7eSjoerg       Message = "comparison of identical expressions always evaluates to "
25906f32e7eSjoerg                 "'equal'";
26006f32e7eSjoerg     else if (((Op == BO_EQ) || (Op == BO_LE) || (Op == BO_GE)))
26106f32e7eSjoerg       Message = "comparison of identical expressions always evaluates to true";
26206f32e7eSjoerg     else
26306f32e7eSjoerg       Message = "comparison of identical expressions always evaluates to false";
26406f32e7eSjoerg     BR.EmitBasicReport(AC->getDecl(), Checker,
26506f32e7eSjoerg                        "Compare of identical expressions",
26606f32e7eSjoerg                        categories::LogicError, Message, ELoc);
26706f32e7eSjoerg   }
26806f32e7eSjoerg }
26906f32e7eSjoerg 
VisitConditionalOperator(const ConditionalOperator * C)27006f32e7eSjoerg bool FindIdenticalExprVisitor::VisitConditionalOperator(
27106f32e7eSjoerg     const ConditionalOperator *C) {
27206f32e7eSjoerg 
27306f32e7eSjoerg   // Check if expressions in conditional expression are identical
27406f32e7eSjoerg   // from a symbolic point of view.
27506f32e7eSjoerg 
27606f32e7eSjoerg   if (isIdenticalStmt(AC->getASTContext(), C->getTrueExpr(),
27706f32e7eSjoerg                       C->getFalseExpr(), true)) {
27806f32e7eSjoerg     PathDiagnosticLocation ELoc =
27906f32e7eSjoerg         PathDiagnosticLocation::createConditionalColonLoc(
28006f32e7eSjoerg             C, BR.getSourceManager());
28106f32e7eSjoerg 
28206f32e7eSjoerg     SourceRange Sr[2];
28306f32e7eSjoerg     Sr[0] = C->getTrueExpr()->getSourceRange();
28406f32e7eSjoerg     Sr[1] = C->getFalseExpr()->getSourceRange();
28506f32e7eSjoerg     BR.EmitBasicReport(
28606f32e7eSjoerg         AC->getDecl(), Checker,
28706f32e7eSjoerg         "Identical expressions in conditional expression",
28806f32e7eSjoerg         categories::LogicError,
28906f32e7eSjoerg         "identical expressions on both sides of ':' in conditional expression",
29006f32e7eSjoerg         ELoc, Sr);
29106f32e7eSjoerg   }
29206f32e7eSjoerg   // We want to visit ALL nodes (expressions in conditional
29306f32e7eSjoerg   // expressions too) that contains conditional operators,
29406f32e7eSjoerg   // thus always return true to traverse ALL nodes.
29506f32e7eSjoerg   return true;
29606f32e7eSjoerg }
29706f32e7eSjoerg 
29806f32e7eSjoerg /// Determines whether two statement trees are identical regarding
29906f32e7eSjoerg /// operators and symbols.
30006f32e7eSjoerg ///
30106f32e7eSjoerg /// Exceptions: expressions containing macros or functions with possible side
30206f32e7eSjoerg /// effects are never considered identical.
30306f32e7eSjoerg /// Limitations: (t + u) and (u + t) are not considered identical.
30406f32e7eSjoerg /// t*(u + t) and t*u + t*t are not considered identical.
30506f32e7eSjoerg ///
isIdenticalStmt(const ASTContext & Ctx,const Stmt * Stmt1,const Stmt * Stmt2,bool IgnoreSideEffects)30606f32e7eSjoerg static bool isIdenticalStmt(const ASTContext &Ctx, const Stmt *Stmt1,
30706f32e7eSjoerg                             const Stmt *Stmt2, bool IgnoreSideEffects) {
30806f32e7eSjoerg 
30906f32e7eSjoerg   if (!Stmt1 || !Stmt2) {
31006f32e7eSjoerg     return !Stmt1 && !Stmt2;
31106f32e7eSjoerg   }
31206f32e7eSjoerg 
31306f32e7eSjoerg   // If Stmt1 & Stmt2 are of different class then they are not
31406f32e7eSjoerg   // identical statements.
31506f32e7eSjoerg   if (Stmt1->getStmtClass() != Stmt2->getStmtClass())
31606f32e7eSjoerg     return false;
31706f32e7eSjoerg 
31806f32e7eSjoerg   const Expr *Expr1 = dyn_cast<Expr>(Stmt1);
31906f32e7eSjoerg   const Expr *Expr2 = dyn_cast<Expr>(Stmt2);
32006f32e7eSjoerg 
32106f32e7eSjoerg   if (Expr1 && Expr2) {
32206f32e7eSjoerg     // If Stmt1 has side effects then don't warn even if expressions
32306f32e7eSjoerg     // are identical.
32406f32e7eSjoerg     if (!IgnoreSideEffects && Expr1->HasSideEffects(Ctx))
32506f32e7eSjoerg       return false;
32606f32e7eSjoerg     // If either expression comes from a macro then don't warn even if
32706f32e7eSjoerg     // the expressions are identical.
32806f32e7eSjoerg     if ((Expr1->getExprLoc().isMacroID()) || (Expr2->getExprLoc().isMacroID()))
32906f32e7eSjoerg       return false;
33006f32e7eSjoerg 
33106f32e7eSjoerg     // If all children of two expressions are identical, return true.
33206f32e7eSjoerg     Expr::const_child_iterator I1 = Expr1->child_begin();
33306f32e7eSjoerg     Expr::const_child_iterator I2 = Expr2->child_begin();
33406f32e7eSjoerg     while (I1 != Expr1->child_end() && I2 != Expr2->child_end()) {
33506f32e7eSjoerg       if (!*I1 || !*I2 || !isIdenticalStmt(Ctx, *I1, *I2, IgnoreSideEffects))
33606f32e7eSjoerg         return false;
33706f32e7eSjoerg       ++I1;
33806f32e7eSjoerg       ++I2;
33906f32e7eSjoerg     }
34006f32e7eSjoerg     // If there are different number of children in the statements, return
34106f32e7eSjoerg     // false.
34206f32e7eSjoerg     if (I1 != Expr1->child_end())
34306f32e7eSjoerg       return false;
34406f32e7eSjoerg     if (I2 != Expr2->child_end())
34506f32e7eSjoerg       return false;
34606f32e7eSjoerg   }
34706f32e7eSjoerg 
34806f32e7eSjoerg   switch (Stmt1->getStmtClass()) {
34906f32e7eSjoerg   default:
35006f32e7eSjoerg     return false;
35106f32e7eSjoerg   case Stmt::CallExprClass:
35206f32e7eSjoerg   case Stmt::ArraySubscriptExprClass:
35306f32e7eSjoerg   case Stmt::OMPArraySectionExprClass:
354*13fbcb42Sjoerg   case Stmt::OMPArrayShapingExprClass:
355*13fbcb42Sjoerg   case Stmt::OMPIteratorExprClass:
35606f32e7eSjoerg   case Stmt::ImplicitCastExprClass:
35706f32e7eSjoerg   case Stmt::ParenExprClass:
35806f32e7eSjoerg   case Stmt::BreakStmtClass:
35906f32e7eSjoerg   case Stmt::ContinueStmtClass:
36006f32e7eSjoerg   case Stmt::NullStmtClass:
36106f32e7eSjoerg     return true;
36206f32e7eSjoerg   case Stmt::CStyleCastExprClass: {
36306f32e7eSjoerg     const CStyleCastExpr* CastExpr1 = cast<CStyleCastExpr>(Stmt1);
36406f32e7eSjoerg     const CStyleCastExpr* CastExpr2 = cast<CStyleCastExpr>(Stmt2);
36506f32e7eSjoerg 
36606f32e7eSjoerg     return CastExpr1->getTypeAsWritten() == CastExpr2->getTypeAsWritten();
36706f32e7eSjoerg   }
36806f32e7eSjoerg   case Stmt::ReturnStmtClass: {
36906f32e7eSjoerg     const ReturnStmt *ReturnStmt1 = cast<ReturnStmt>(Stmt1);
37006f32e7eSjoerg     const ReturnStmt *ReturnStmt2 = cast<ReturnStmt>(Stmt2);
37106f32e7eSjoerg 
37206f32e7eSjoerg     return isIdenticalStmt(Ctx, ReturnStmt1->getRetValue(),
37306f32e7eSjoerg                            ReturnStmt2->getRetValue(), IgnoreSideEffects);
37406f32e7eSjoerg   }
37506f32e7eSjoerg   case Stmt::ForStmtClass: {
37606f32e7eSjoerg     const ForStmt *ForStmt1 = cast<ForStmt>(Stmt1);
37706f32e7eSjoerg     const ForStmt *ForStmt2 = cast<ForStmt>(Stmt2);
37806f32e7eSjoerg 
37906f32e7eSjoerg     if (!isIdenticalStmt(Ctx, ForStmt1->getInit(), ForStmt2->getInit(),
38006f32e7eSjoerg                          IgnoreSideEffects))
38106f32e7eSjoerg       return false;
38206f32e7eSjoerg     if (!isIdenticalStmt(Ctx, ForStmt1->getCond(), ForStmt2->getCond(),
38306f32e7eSjoerg                          IgnoreSideEffects))
38406f32e7eSjoerg       return false;
38506f32e7eSjoerg     if (!isIdenticalStmt(Ctx, ForStmt1->getInc(), ForStmt2->getInc(),
38606f32e7eSjoerg                          IgnoreSideEffects))
38706f32e7eSjoerg       return false;
38806f32e7eSjoerg     if (!isIdenticalStmt(Ctx, ForStmt1->getBody(), ForStmt2->getBody(),
38906f32e7eSjoerg                          IgnoreSideEffects))
39006f32e7eSjoerg       return false;
39106f32e7eSjoerg     return true;
39206f32e7eSjoerg   }
39306f32e7eSjoerg   case Stmt::DoStmtClass: {
39406f32e7eSjoerg     const DoStmt *DStmt1 = cast<DoStmt>(Stmt1);
39506f32e7eSjoerg     const DoStmt *DStmt2 = cast<DoStmt>(Stmt2);
39606f32e7eSjoerg 
39706f32e7eSjoerg     if (!isIdenticalStmt(Ctx, DStmt1->getCond(), DStmt2->getCond(),
39806f32e7eSjoerg                          IgnoreSideEffects))
39906f32e7eSjoerg       return false;
40006f32e7eSjoerg     if (!isIdenticalStmt(Ctx, DStmt1->getBody(), DStmt2->getBody(),
40106f32e7eSjoerg                          IgnoreSideEffects))
40206f32e7eSjoerg       return false;
40306f32e7eSjoerg     return true;
40406f32e7eSjoerg   }
40506f32e7eSjoerg   case Stmt::WhileStmtClass: {
40606f32e7eSjoerg     const WhileStmt *WStmt1 = cast<WhileStmt>(Stmt1);
40706f32e7eSjoerg     const WhileStmt *WStmt2 = cast<WhileStmt>(Stmt2);
40806f32e7eSjoerg 
40906f32e7eSjoerg     if (!isIdenticalStmt(Ctx, WStmt1->getCond(), WStmt2->getCond(),
41006f32e7eSjoerg                          IgnoreSideEffects))
41106f32e7eSjoerg       return false;
41206f32e7eSjoerg     if (!isIdenticalStmt(Ctx, WStmt1->getBody(), WStmt2->getBody(),
41306f32e7eSjoerg                          IgnoreSideEffects))
41406f32e7eSjoerg       return false;
41506f32e7eSjoerg     return true;
41606f32e7eSjoerg   }
41706f32e7eSjoerg   case Stmt::IfStmtClass: {
41806f32e7eSjoerg     const IfStmt *IStmt1 = cast<IfStmt>(Stmt1);
41906f32e7eSjoerg     const IfStmt *IStmt2 = cast<IfStmt>(Stmt2);
42006f32e7eSjoerg 
42106f32e7eSjoerg     if (!isIdenticalStmt(Ctx, IStmt1->getCond(), IStmt2->getCond(),
42206f32e7eSjoerg                          IgnoreSideEffects))
42306f32e7eSjoerg       return false;
42406f32e7eSjoerg     if (!isIdenticalStmt(Ctx, IStmt1->getThen(), IStmt2->getThen(),
42506f32e7eSjoerg                          IgnoreSideEffects))
42606f32e7eSjoerg       return false;
42706f32e7eSjoerg     if (!isIdenticalStmt(Ctx, IStmt1->getElse(), IStmt2->getElse(),
42806f32e7eSjoerg                          IgnoreSideEffects))
42906f32e7eSjoerg       return false;
43006f32e7eSjoerg     return true;
43106f32e7eSjoerg   }
43206f32e7eSjoerg   case Stmt::CompoundStmtClass: {
43306f32e7eSjoerg     const CompoundStmt *CompStmt1 = cast<CompoundStmt>(Stmt1);
43406f32e7eSjoerg     const CompoundStmt *CompStmt2 = cast<CompoundStmt>(Stmt2);
43506f32e7eSjoerg 
43606f32e7eSjoerg     if (CompStmt1->size() != CompStmt2->size())
43706f32e7eSjoerg       return false;
43806f32e7eSjoerg 
43906f32e7eSjoerg     CompoundStmt::const_body_iterator I1 = CompStmt1->body_begin();
44006f32e7eSjoerg     CompoundStmt::const_body_iterator I2 = CompStmt2->body_begin();
44106f32e7eSjoerg     while (I1 != CompStmt1->body_end() && I2 != CompStmt2->body_end()) {
44206f32e7eSjoerg       if (!isIdenticalStmt(Ctx, *I1, *I2, IgnoreSideEffects))
44306f32e7eSjoerg         return false;
44406f32e7eSjoerg       ++I1;
44506f32e7eSjoerg       ++I2;
44606f32e7eSjoerg     }
44706f32e7eSjoerg 
44806f32e7eSjoerg     return true;
44906f32e7eSjoerg   }
45006f32e7eSjoerg   case Stmt::CompoundAssignOperatorClass:
45106f32e7eSjoerg   case Stmt::BinaryOperatorClass: {
45206f32e7eSjoerg     const BinaryOperator *BinOp1 = cast<BinaryOperator>(Stmt1);
45306f32e7eSjoerg     const BinaryOperator *BinOp2 = cast<BinaryOperator>(Stmt2);
45406f32e7eSjoerg     return BinOp1->getOpcode() == BinOp2->getOpcode();
45506f32e7eSjoerg   }
45606f32e7eSjoerg   case Stmt::CharacterLiteralClass: {
45706f32e7eSjoerg     const CharacterLiteral *CharLit1 = cast<CharacterLiteral>(Stmt1);
45806f32e7eSjoerg     const CharacterLiteral *CharLit2 = cast<CharacterLiteral>(Stmt2);
45906f32e7eSjoerg     return CharLit1->getValue() == CharLit2->getValue();
46006f32e7eSjoerg   }
46106f32e7eSjoerg   case Stmt::DeclRefExprClass: {
46206f32e7eSjoerg     const DeclRefExpr *DeclRef1 = cast<DeclRefExpr>(Stmt1);
46306f32e7eSjoerg     const DeclRefExpr *DeclRef2 = cast<DeclRefExpr>(Stmt2);
46406f32e7eSjoerg     return DeclRef1->getDecl() == DeclRef2->getDecl();
46506f32e7eSjoerg   }
46606f32e7eSjoerg   case Stmt::IntegerLiteralClass: {
46706f32e7eSjoerg     const IntegerLiteral *IntLit1 = cast<IntegerLiteral>(Stmt1);
46806f32e7eSjoerg     const IntegerLiteral *IntLit2 = cast<IntegerLiteral>(Stmt2);
46906f32e7eSjoerg 
47006f32e7eSjoerg     llvm::APInt I1 = IntLit1->getValue();
47106f32e7eSjoerg     llvm::APInt I2 = IntLit2->getValue();
47206f32e7eSjoerg     if (I1.getBitWidth() != I2.getBitWidth())
47306f32e7eSjoerg       return false;
47406f32e7eSjoerg     return  I1 == I2;
47506f32e7eSjoerg   }
47606f32e7eSjoerg   case Stmt::FloatingLiteralClass: {
47706f32e7eSjoerg     const FloatingLiteral *FloatLit1 = cast<FloatingLiteral>(Stmt1);
47806f32e7eSjoerg     const FloatingLiteral *FloatLit2 = cast<FloatingLiteral>(Stmt2);
47906f32e7eSjoerg     return FloatLit1->getValue().bitwiseIsEqual(FloatLit2->getValue());
48006f32e7eSjoerg   }
48106f32e7eSjoerg   case Stmt::StringLiteralClass: {
48206f32e7eSjoerg     const StringLiteral *StringLit1 = cast<StringLiteral>(Stmt1);
48306f32e7eSjoerg     const StringLiteral *StringLit2 = cast<StringLiteral>(Stmt2);
48406f32e7eSjoerg     return StringLit1->getBytes() == StringLit2->getBytes();
48506f32e7eSjoerg   }
48606f32e7eSjoerg   case Stmt::MemberExprClass: {
48706f32e7eSjoerg     const MemberExpr *MemberStmt1 = cast<MemberExpr>(Stmt1);
48806f32e7eSjoerg     const MemberExpr *MemberStmt2 = cast<MemberExpr>(Stmt2);
48906f32e7eSjoerg     return MemberStmt1->getMemberDecl() == MemberStmt2->getMemberDecl();
49006f32e7eSjoerg   }
49106f32e7eSjoerg   case Stmt::UnaryOperatorClass: {
49206f32e7eSjoerg     const UnaryOperator *UnaryOp1 = cast<UnaryOperator>(Stmt1);
49306f32e7eSjoerg     const UnaryOperator *UnaryOp2 = cast<UnaryOperator>(Stmt2);
49406f32e7eSjoerg     return UnaryOp1->getOpcode() == UnaryOp2->getOpcode();
49506f32e7eSjoerg   }
49606f32e7eSjoerg   }
49706f32e7eSjoerg }
49806f32e7eSjoerg 
49906f32e7eSjoerg //===----------------------------------------------------------------------===//
50006f32e7eSjoerg // FindIdenticalExprChecker
50106f32e7eSjoerg //===----------------------------------------------------------------------===//
50206f32e7eSjoerg 
50306f32e7eSjoerg namespace {
50406f32e7eSjoerg class FindIdenticalExprChecker : public Checker<check::ASTCodeBody> {
50506f32e7eSjoerg public:
checkASTCodeBody(const Decl * D,AnalysisManager & Mgr,BugReporter & BR) const50606f32e7eSjoerg   void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
50706f32e7eSjoerg                         BugReporter &BR) const {
50806f32e7eSjoerg     FindIdenticalExprVisitor Visitor(BR, this, Mgr.getAnalysisDeclContext(D));
50906f32e7eSjoerg     Visitor.TraverseDecl(const_cast<Decl *>(D));
51006f32e7eSjoerg   }
51106f32e7eSjoerg };
51206f32e7eSjoerg } // end anonymous namespace
51306f32e7eSjoerg 
registerIdenticalExprChecker(CheckerManager & Mgr)51406f32e7eSjoerg void ento::registerIdenticalExprChecker(CheckerManager &Mgr) {
51506f32e7eSjoerg   Mgr.registerChecker<FindIdenticalExprChecker>();
51606f32e7eSjoerg }
51706f32e7eSjoerg 
shouldRegisterIdenticalExprChecker(const CheckerManager & mgr)518*13fbcb42Sjoerg bool ento::shouldRegisterIdenticalExprChecker(const CheckerManager &mgr) {
51906f32e7eSjoerg   return true;
52006f32e7eSjoerg }
521