2013-11-08 09:15:39 +08:00
|
|
|
//== IdenticalExprChecker.cpp - Identical expression checker----------------==//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-11-08 09:15:39 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
///
|
|
|
|
/// \file
|
2018-05-09 09:00:01 +08:00
|
|
|
/// This defines IdenticalExprChecker, a check that warns about
|
2013-11-08 09:15:39 +08:00
|
|
|
/// unintended use of identical expressions.
|
|
|
|
///
|
2013-12-11 02:18:06 +08:00
|
|
|
/// It checks for use of identical expressions with comparison operators and
|
|
|
|
/// inside conditional expressions.
|
2013-11-08 09:15:39 +08:00
|
|
|
///
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[analyzer][NFC] Move CheckerRegistry from the Core directory to Frontend
ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept.
It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend,
whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can
imagine, used to circumvent the problem that the registry functions of the
checkers are located in the clangStaticAnalyzerCheckers library, but that
library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend
depends on both of those libraries.
One can make the observation however, that CheckerRegistry has no place in Core,
it isn't used there at all! The only place where it is used is Frontend, which
is where it ultimately belongs.
This move implies that since
include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function:
class CheckerRegistry;
void registerBuiltinCheckers(CheckerRegistry ®istry);
it had to re purposed, as CheckerRegistry is no longer available to
clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h,
which actually describes it a lot better -- it does not contain the registration
functions for checkers, but only those generated by the tblgen files.
Differential Revision: https://reviews.llvm.org/D54436
llvm-svn: 349275
2018-12-16 00:23:51 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
2014-01-07 19:51:46 +08:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
2013-11-08 09:15:39 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
|
2014-02-20 01:44:11 +08:00
|
|
|
static bool isIdenticalStmt(const ASTContext &Ctx, const Stmt *Stmt1,
|
|
|
|
const Stmt *Stmt2, bool IgnoreSideEffects = false);
|
2013-11-08 09:15:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FindIdenticalExprVisitor - Identify nodes using identical expressions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-11-14 23:46:10 +08:00
|
|
|
namespace {
|
2013-11-08 09:15:39 +08:00
|
|
|
class FindIdenticalExprVisitor
|
|
|
|
: public RecursiveASTVisitor<FindIdenticalExprVisitor> {
|
2014-02-20 01:44:16 +08:00
|
|
|
BugReporter &BR;
|
|
|
|
const CheckerBase *Checker;
|
|
|
|
AnalysisDeclContext *AC;
|
2013-11-08 09:15:39 +08:00
|
|
|
public:
|
2014-02-12 05:49:21 +08:00
|
|
|
explicit FindIdenticalExprVisitor(BugReporter &B,
|
|
|
|
const CheckerBase *Checker,
|
|
|
|
AnalysisDeclContext *A)
|
|
|
|
: BR(B), Checker(Checker), AC(A) {}
|
2013-11-08 09:15:39 +08:00
|
|
|
// FindIdenticalExprVisitor only visits nodes
|
2014-02-20 01:44:11 +08:00
|
|
|
// that are binary operators, if statements or
|
|
|
|
// conditional operators.
|
2013-11-08 09:15:39 +08:00
|
|
|
bool VisitBinaryOperator(const BinaryOperator *B);
|
2014-02-20 01:44:11 +08:00
|
|
|
bool VisitIfStmt(const IfStmt *I);
|
2013-12-11 02:18:06 +08:00
|
|
|
bool VisitConditionalOperator(const ConditionalOperator *C);
|
2013-11-08 09:15:39 +08:00
|
|
|
|
|
|
|
private:
|
2014-02-20 01:44:16 +08:00
|
|
|
void reportIdenticalExpr(const BinaryOperator *B, bool CheckBitwise,
|
|
|
|
ArrayRef<SourceRange> Sr);
|
|
|
|
void checkBitwiseOrLogicalOp(const BinaryOperator *B, bool CheckBitwise);
|
|
|
|
void checkComparisonOp(const BinaryOperator *B);
|
2013-11-08 09:15:39 +08:00
|
|
|
};
|
2013-11-14 23:46:10 +08:00
|
|
|
} // end anonymous namespace
|
2013-11-08 09:15:39 +08:00
|
|
|
|
2014-02-20 01:44:16 +08:00
|
|
|
void FindIdenticalExprVisitor::reportIdenticalExpr(const BinaryOperator *B,
|
|
|
|
bool CheckBitwise,
|
|
|
|
ArrayRef<SourceRange> Sr) {
|
|
|
|
StringRef Message;
|
|
|
|
if (CheckBitwise)
|
|
|
|
Message = "identical expressions on both sides of bitwise operator";
|
|
|
|
else
|
|
|
|
Message = "identical expressions on both sides of logical operator";
|
|
|
|
|
|
|
|
PathDiagnosticLocation ELoc =
|
|
|
|
PathDiagnosticLocation::createOperatorLoc(B, BR.getSourceManager());
|
|
|
|
BR.EmitBasicReport(AC->getDecl(), Checker,
|
|
|
|
"Use of identical expressions",
|
|
|
|
categories::LogicError,
|
|
|
|
Message, ELoc, Sr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindIdenticalExprVisitor::checkBitwiseOrLogicalOp(const BinaryOperator *B,
|
|
|
|
bool CheckBitwise) {
|
|
|
|
SourceRange Sr[2];
|
|
|
|
|
|
|
|
const Expr *LHS = B->getLHS();
|
|
|
|
const Expr *RHS = B->getRHS();
|
|
|
|
|
|
|
|
// Split operators as long as we still have operators to split on. We will
|
|
|
|
// get called for every binary operator in an expression so there is no need
|
|
|
|
// to check every one against each other here, just the right most one with
|
|
|
|
// the others.
|
|
|
|
while (const BinaryOperator *B2 = dyn_cast<BinaryOperator>(LHS)) {
|
|
|
|
if (B->getOpcode() != B2->getOpcode())
|
|
|
|
break;
|
|
|
|
if (isIdenticalStmt(AC->getASTContext(), RHS, B2->getRHS())) {
|
|
|
|
Sr[0] = RHS->getSourceRange();
|
|
|
|
Sr[1] = B2->getRHS()->getSourceRange();
|
|
|
|
reportIdenticalExpr(B, CheckBitwise, Sr);
|
|
|
|
}
|
|
|
|
LHS = B2->getLHS();
|
|
|
|
}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2014-02-20 01:44:16 +08:00
|
|
|
if (isIdenticalStmt(AC->getASTContext(), RHS, LHS)) {
|
|
|
|
Sr[0] = RHS->getSourceRange();
|
|
|
|
Sr[1] = LHS->getSourceRange();
|
|
|
|
reportIdenticalExpr(B, CheckBitwise, Sr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 01:44:11 +08:00
|
|
|
bool FindIdenticalExprVisitor::VisitIfStmt(const IfStmt *I) {
|
|
|
|
const Stmt *Stmt1 = I->getThen();
|
|
|
|
const Stmt *Stmt2 = I->getElse();
|
|
|
|
|
2015-08-10 15:18:29 +08:00
|
|
|
// Check for identical inner condition:
|
|
|
|
//
|
|
|
|
// if (x<10) {
|
|
|
|
// if (x<10) {
|
|
|
|
// ..
|
|
|
|
if (const CompoundStmt *CS = dyn_cast<CompoundStmt>(Stmt1)) {
|
|
|
|
if (!CS->body_empty()) {
|
|
|
|
const IfStmt *InnerIf = dyn_cast<IfStmt>(*CS->body_begin());
|
|
|
|
if (InnerIf && isIdenticalStmt(AC->getASTContext(), I->getCond(), InnerIf->getCond(), /*ignoreSideEffects=*/ false)) {
|
|
|
|
PathDiagnosticLocation ELoc(InnerIf->getCond(), BR.getSourceManager(), AC);
|
|
|
|
BR.EmitBasicReport(AC->getDecl(), Checker, "Identical conditions",
|
|
|
|
categories::LogicError,
|
|
|
|
"conditions of the inner and outer statements are identical",
|
|
|
|
ELoc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-12 00:52:29 +08:00
|
|
|
// Check for identical conditions:
|
|
|
|
//
|
|
|
|
// if (b) {
|
|
|
|
// foo1();
|
|
|
|
// } else if (b) {
|
|
|
|
// foo2();
|
|
|
|
// }
|
|
|
|
if (Stmt1 && Stmt2) {
|
|
|
|
const Expr *Cond1 = I->getCond();
|
|
|
|
const Stmt *Else = Stmt2;
|
|
|
|
while (const IfStmt *I2 = dyn_cast_or_null<IfStmt>(Else)) {
|
|
|
|
const Expr *Cond2 = I2->getCond();
|
|
|
|
if (isIdenticalStmt(AC->getASTContext(), Cond1, Cond2, false)) {
|
|
|
|
SourceRange Sr = Cond1->getSourceRange();
|
|
|
|
PathDiagnosticLocation ELoc(Cond2, BR.getSourceManager(), AC);
|
|
|
|
BR.EmitBasicReport(AC->getDecl(), Checker, "Identical conditions",
|
|
|
|
categories::LogicError,
|
|
|
|
"expression is identical to previous condition",
|
|
|
|
ELoc, Sr);
|
|
|
|
}
|
|
|
|
Else = I2->getElse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 01:44:11 +08:00
|
|
|
if (!Stmt1 || !Stmt2)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Special handling for code like:
|
|
|
|
//
|
|
|
|
// if (b) {
|
|
|
|
// i = 1;
|
|
|
|
// } else
|
|
|
|
// i = 1;
|
|
|
|
if (const CompoundStmt *CompStmt = dyn_cast<CompoundStmt>(Stmt1)) {
|
|
|
|
if (CompStmt->size() == 1)
|
|
|
|
Stmt1 = CompStmt->body_back();
|
|
|
|
}
|
|
|
|
if (const CompoundStmt *CompStmt = dyn_cast<CompoundStmt>(Stmt2)) {
|
|
|
|
if (CompStmt->size() == 1)
|
|
|
|
Stmt2 = CompStmt->body_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isIdenticalStmt(AC->getASTContext(), Stmt1, Stmt2, true)) {
|
|
|
|
PathDiagnosticLocation ELoc =
|
|
|
|
PathDiagnosticLocation::createBegin(I, BR.getSourceManager(), AC);
|
|
|
|
BR.EmitBasicReport(AC->getDecl(), Checker,
|
|
|
|
"Identical branches",
|
|
|
|
categories::LogicError,
|
|
|
|
"true and false branches are identical", ELoc);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-08 09:15:39 +08:00
|
|
|
bool FindIdenticalExprVisitor::VisitBinaryOperator(const BinaryOperator *B) {
|
|
|
|
BinaryOperator::Opcode Op = B->getOpcode();
|
2014-02-20 01:44:16 +08:00
|
|
|
|
|
|
|
if (BinaryOperator::isBitwiseOp(Op))
|
|
|
|
checkBitwiseOrLogicalOp(B, true);
|
|
|
|
|
|
|
|
if (BinaryOperator::isLogicalOp(Op))
|
|
|
|
checkBitwiseOrLogicalOp(B, false);
|
|
|
|
|
|
|
|
if (BinaryOperator::isComparisonOp(Op))
|
|
|
|
checkComparisonOp(B);
|
|
|
|
|
|
|
|
// We want to visit ALL nodes (subexpressions of binary comparison
|
|
|
|
// expressions too) that contains comparison operators.
|
|
|
|
// True is always returned to traverse ALL nodes.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FindIdenticalExprVisitor::checkComparisonOp(const BinaryOperator *B) {
|
|
|
|
BinaryOperator::Opcode Op = B->getOpcode();
|
|
|
|
|
2013-11-08 09:15:39 +08:00
|
|
|
//
|
|
|
|
// Special case for floating-point representation.
|
|
|
|
//
|
|
|
|
// If expressions on both sides of comparison operator are of type float,
|
|
|
|
// then for some comparison operators no warning shall be
|
|
|
|
// reported even if the expressions are identical from a symbolic point of
|
|
|
|
// view. Comparison between expressions, declared variables and literals
|
|
|
|
// are treated differently.
|
|
|
|
//
|
|
|
|
// != and == between float literals that have the same value should NOT warn.
|
|
|
|
// < > between float literals that have the same value SHOULD warn.
|
|
|
|
//
|
|
|
|
// != and == between the same float declaration should NOT warn.
|
|
|
|
// < > between the same float declaration SHOULD warn.
|
|
|
|
//
|
|
|
|
// != and == between eq. expressions that evaluates into float
|
|
|
|
// should NOT warn.
|
|
|
|
// < > between eq. expressions that evaluates into float
|
|
|
|
// should NOT warn.
|
|
|
|
//
|
|
|
|
const Expr *LHS = B->getLHS()->IgnoreParenImpCasts();
|
|
|
|
const Expr *RHS = B->getRHS()->IgnoreParenImpCasts();
|
|
|
|
|
|
|
|
const DeclRefExpr *DeclRef1 = dyn_cast<DeclRefExpr>(LHS);
|
|
|
|
const DeclRefExpr *DeclRef2 = dyn_cast<DeclRefExpr>(RHS);
|
|
|
|
const FloatingLiteral *FloatLit1 = dyn_cast<FloatingLiteral>(LHS);
|
|
|
|
const FloatingLiteral *FloatLit2 = dyn_cast<FloatingLiteral>(RHS);
|
|
|
|
if ((DeclRef1) && (DeclRef2)) {
|
|
|
|
if ((DeclRef1->getType()->hasFloatingRepresentation()) &&
|
|
|
|
(DeclRef2->getType()->hasFloatingRepresentation())) {
|
|
|
|
if (DeclRef1->getDecl() == DeclRef2->getDecl()) {
|
|
|
|
if ((Op == BO_EQ) || (Op == BO_NE)) {
|
2014-02-20 01:44:16 +08:00
|
|
|
return;
|
2013-11-08 09:15:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if ((FloatLit1) && (FloatLit2)) {
|
|
|
|
if (FloatLit1->getValue().bitwiseIsEqual(FloatLit2->getValue())) {
|
|
|
|
if ((Op == BO_EQ) || (Op == BO_NE)) {
|
2014-02-20 01:44:16 +08:00
|
|
|
return;
|
2013-11-08 09:15:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (LHS->getType()->hasFloatingRepresentation()) {
|
|
|
|
// If any side of comparison operator still has floating-point
|
|
|
|
// representation, then it's an expression. Don't warn.
|
|
|
|
// Here only LHS is checked since RHS will be implicit casted to float.
|
2014-02-20 01:44:16 +08:00
|
|
|
return;
|
2013-11-08 09:15:39 +08:00
|
|
|
} else {
|
|
|
|
// No special case with floating-point representation, report as usual.
|
|
|
|
}
|
|
|
|
|
2014-02-20 01:44:11 +08:00
|
|
|
if (isIdenticalStmt(AC->getASTContext(), B->getLHS(), B->getRHS())) {
|
2013-11-08 09:15:39 +08:00
|
|
|
PathDiagnosticLocation ELoc =
|
|
|
|
PathDiagnosticLocation::createOperatorLoc(B, BR.getSourceManager());
|
|
|
|
StringRef Message;
|
2017-12-14 23:16:18 +08:00
|
|
|
if (Op == BO_Cmp)
|
|
|
|
Message = "comparison of identical expressions always evaluates to "
|
|
|
|
"'equal'";
|
|
|
|
else if (((Op == BO_EQ) || (Op == BO_LE) || (Op == BO_GE)))
|
2013-11-08 09:15:39 +08:00
|
|
|
Message = "comparison of identical expressions always evaluates to true";
|
|
|
|
else
|
|
|
|
Message = "comparison of identical expressions always evaluates to false";
|
2014-02-12 05:49:21 +08:00
|
|
|
BR.EmitBasicReport(AC->getDecl(), Checker,
|
|
|
|
"Compare of identical expressions",
|
2013-11-08 09:15:39 +08:00
|
|
|
categories::LogicError, Message, ELoc);
|
|
|
|
}
|
|
|
|
}
|
2013-12-11 02:18:06 +08:00
|
|
|
|
|
|
|
bool FindIdenticalExprVisitor::VisitConditionalOperator(
|
|
|
|
const ConditionalOperator *C) {
|
|
|
|
|
|
|
|
// Check if expressions in conditional expression are identical
|
|
|
|
// from a symbolic point of view.
|
|
|
|
|
2014-02-20 01:44:11 +08:00
|
|
|
if (isIdenticalStmt(AC->getASTContext(), C->getTrueExpr(),
|
2013-12-11 02:18:06 +08:00
|
|
|
C->getFalseExpr(), true)) {
|
|
|
|
PathDiagnosticLocation ELoc =
|
|
|
|
PathDiagnosticLocation::createConditionalColonLoc(
|
|
|
|
C, BR.getSourceManager());
|
|
|
|
|
|
|
|
SourceRange Sr[2];
|
|
|
|
Sr[0] = C->getTrueExpr()->getSourceRange();
|
|
|
|
Sr[1] = C->getFalseExpr()->getSourceRange();
|
|
|
|
BR.EmitBasicReport(
|
2014-02-12 05:49:21 +08:00
|
|
|
AC->getDecl(), Checker,
|
|
|
|
"Identical expressions in conditional expression",
|
2013-12-11 02:18:06 +08:00
|
|
|
categories::LogicError,
|
|
|
|
"identical expressions on both sides of ':' in conditional expression",
|
|
|
|
ELoc, Sr);
|
|
|
|
}
|
|
|
|
// We want to visit ALL nodes (expressions in conditional
|
|
|
|
// expressions too) that contains conditional operators,
|
|
|
|
// thus always return true to traverse ALL nodes.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Determines whether two statement trees are identical regarding
|
2013-11-08 09:15:39 +08:00
|
|
|
/// operators and symbols.
|
|
|
|
///
|
|
|
|
/// Exceptions: expressions containing macros or functions with possible side
|
|
|
|
/// effects are never considered identical.
|
|
|
|
/// Limitations: (t + u) and (u + t) are not considered identical.
|
|
|
|
/// t*(u + t) and t*u + t*t are not considered identical.
|
|
|
|
///
|
2014-02-20 01:44:11 +08:00
|
|
|
static bool isIdenticalStmt(const ASTContext &Ctx, const Stmt *Stmt1,
|
|
|
|
const Stmt *Stmt2, bool IgnoreSideEffects) {
|
|
|
|
|
|
|
|
if (!Stmt1 || !Stmt2) {
|
2015-12-28 21:06:58 +08:00
|
|
|
return !Stmt1 && !Stmt2;
|
2014-02-20 01:44:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If Stmt1 & Stmt2 are of different class then they are not
|
|
|
|
// identical statements.
|
|
|
|
if (Stmt1->getStmtClass() != Stmt2->getStmtClass())
|
2013-11-08 09:15:39 +08:00
|
|
|
return false;
|
2014-02-20 01:44:11 +08:00
|
|
|
|
|
|
|
const Expr *Expr1 = dyn_cast<Expr>(Stmt1);
|
|
|
|
const Expr *Expr2 = dyn_cast<Expr>(Stmt2);
|
|
|
|
|
|
|
|
if (Expr1 && Expr2) {
|
|
|
|
// If Stmt1 has side effects then don't warn even if expressions
|
|
|
|
// are identical.
|
|
|
|
if (!IgnoreSideEffects && Expr1->HasSideEffects(Ctx))
|
|
|
|
return false;
|
|
|
|
// If either expression comes from a macro then don't warn even if
|
|
|
|
// the expressions are identical.
|
|
|
|
if ((Expr1->getExprLoc().isMacroID()) || (Expr2->getExprLoc().isMacroID()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If all children of two expressions are identical, return true.
|
|
|
|
Expr::const_child_iterator I1 = Expr1->child_begin();
|
|
|
|
Expr::const_child_iterator I2 = Expr2->child_begin();
|
|
|
|
while (I1 != Expr1->child_end() && I2 != Expr2->child_end()) {
|
|
|
|
if (!*I1 || !*I2 || !isIdenticalStmt(Ctx, *I1, *I2, IgnoreSideEffects))
|
|
|
|
return false;
|
|
|
|
++I1;
|
|
|
|
++I2;
|
|
|
|
}
|
|
|
|
// If there are different number of children in the statements, return
|
|
|
|
// false.
|
|
|
|
if (I1 != Expr1->child_end())
|
|
|
|
return false;
|
|
|
|
if (I2 != Expr2->child_end())
|
2013-11-08 09:15:39 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-20 01:44:11 +08:00
|
|
|
switch (Stmt1->getStmtClass()) {
|
2013-11-08 09:15:39 +08:00
|
|
|
default:
|
|
|
|
return false;
|
2013-12-11 02:18:06 +08:00
|
|
|
case Stmt::CallExprClass:
|
2013-11-08 09:15:39 +08:00
|
|
|
case Stmt::ArraySubscriptExprClass:
|
2015-08-25 22:24:04 +08:00
|
|
|
case Stmt::OMPArraySectionExprClass:
|
2013-11-08 09:15:39 +08:00
|
|
|
case Stmt::ImplicitCastExprClass:
|
|
|
|
case Stmt::ParenExprClass:
|
2014-02-20 01:44:11 +08:00
|
|
|
case Stmt::BreakStmtClass:
|
|
|
|
case Stmt::ContinueStmtClass:
|
|
|
|
case Stmt::NullStmtClass:
|
|
|
|
return true;
|
|
|
|
case Stmt::CStyleCastExprClass: {
|
|
|
|
const CStyleCastExpr* CastExpr1 = cast<CStyleCastExpr>(Stmt1);
|
|
|
|
const CStyleCastExpr* CastExpr2 = cast<CStyleCastExpr>(Stmt2);
|
|
|
|
|
|
|
|
return CastExpr1->getTypeAsWritten() == CastExpr2->getTypeAsWritten();
|
|
|
|
}
|
|
|
|
case Stmt::ReturnStmtClass: {
|
|
|
|
const ReturnStmt *ReturnStmt1 = cast<ReturnStmt>(Stmt1);
|
|
|
|
const ReturnStmt *ReturnStmt2 = cast<ReturnStmt>(Stmt2);
|
|
|
|
|
|
|
|
return isIdenticalStmt(Ctx, ReturnStmt1->getRetValue(),
|
|
|
|
ReturnStmt2->getRetValue(), IgnoreSideEffects);
|
|
|
|
}
|
|
|
|
case Stmt::ForStmtClass: {
|
|
|
|
const ForStmt *ForStmt1 = cast<ForStmt>(Stmt1);
|
|
|
|
const ForStmt *ForStmt2 = cast<ForStmt>(Stmt2);
|
|
|
|
|
|
|
|
if (!isIdenticalStmt(Ctx, ForStmt1->getInit(), ForStmt2->getInit(),
|
|
|
|
IgnoreSideEffects))
|
|
|
|
return false;
|
|
|
|
if (!isIdenticalStmt(Ctx, ForStmt1->getCond(), ForStmt2->getCond(),
|
|
|
|
IgnoreSideEffects))
|
|
|
|
return false;
|
|
|
|
if (!isIdenticalStmt(Ctx, ForStmt1->getInc(), ForStmt2->getInc(),
|
|
|
|
IgnoreSideEffects))
|
|
|
|
return false;
|
|
|
|
if (!isIdenticalStmt(Ctx, ForStmt1->getBody(), ForStmt2->getBody(),
|
|
|
|
IgnoreSideEffects))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case Stmt::DoStmtClass: {
|
|
|
|
const DoStmt *DStmt1 = cast<DoStmt>(Stmt1);
|
|
|
|
const DoStmt *DStmt2 = cast<DoStmt>(Stmt2);
|
|
|
|
|
|
|
|
if (!isIdenticalStmt(Ctx, DStmt1->getCond(), DStmt2->getCond(),
|
|
|
|
IgnoreSideEffects))
|
|
|
|
return false;
|
|
|
|
if (!isIdenticalStmt(Ctx, DStmt1->getBody(), DStmt2->getBody(),
|
|
|
|
IgnoreSideEffects))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case Stmt::WhileStmtClass: {
|
|
|
|
const WhileStmt *WStmt1 = cast<WhileStmt>(Stmt1);
|
|
|
|
const WhileStmt *WStmt2 = cast<WhileStmt>(Stmt2);
|
|
|
|
|
2014-02-21 08:18:31 +08:00
|
|
|
if (!isIdenticalStmt(Ctx, WStmt1->getCond(), WStmt2->getCond(),
|
|
|
|
IgnoreSideEffects))
|
|
|
|
return false;
|
|
|
|
if (!isIdenticalStmt(Ctx, WStmt1->getBody(), WStmt2->getBody(),
|
|
|
|
IgnoreSideEffects))
|
|
|
|
return false;
|
|
|
|
return true;
|
2014-02-20 01:44:11 +08:00
|
|
|
}
|
|
|
|
case Stmt::IfStmtClass: {
|
|
|
|
const IfStmt *IStmt1 = cast<IfStmt>(Stmt1);
|
|
|
|
const IfStmt *IStmt2 = cast<IfStmt>(Stmt2);
|
|
|
|
|
|
|
|
if (!isIdenticalStmt(Ctx, IStmt1->getCond(), IStmt2->getCond(),
|
|
|
|
IgnoreSideEffects))
|
|
|
|
return false;
|
|
|
|
if (!isIdenticalStmt(Ctx, IStmt1->getThen(), IStmt2->getThen(),
|
|
|
|
IgnoreSideEffects))
|
|
|
|
return false;
|
|
|
|
if (!isIdenticalStmt(Ctx, IStmt1->getElse(), IStmt2->getElse(),
|
|
|
|
IgnoreSideEffects))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case Stmt::CompoundStmtClass: {
|
|
|
|
const CompoundStmt *CompStmt1 = cast<CompoundStmt>(Stmt1);
|
|
|
|
const CompoundStmt *CompStmt2 = cast<CompoundStmt>(Stmt2);
|
|
|
|
|
|
|
|
if (CompStmt1->size() != CompStmt2->size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CompoundStmt::const_body_iterator I1 = CompStmt1->body_begin();
|
|
|
|
CompoundStmt::const_body_iterator I2 = CompStmt2->body_begin();
|
|
|
|
while (I1 != CompStmt1->body_end() && I2 != CompStmt2->body_end()) {
|
|
|
|
if (!isIdenticalStmt(Ctx, *I1, *I2, IgnoreSideEffects))
|
|
|
|
return false;
|
|
|
|
++I1;
|
|
|
|
++I2;
|
|
|
|
}
|
|
|
|
|
2013-11-08 09:15:39 +08:00
|
|
|
return true;
|
2014-02-20 01:44:11 +08:00
|
|
|
}
|
|
|
|
case Stmt::CompoundAssignOperatorClass:
|
2013-11-08 09:15:39 +08:00
|
|
|
case Stmt::BinaryOperatorClass: {
|
2014-02-20 01:44:11 +08:00
|
|
|
const BinaryOperator *BinOp1 = cast<BinaryOperator>(Stmt1);
|
|
|
|
const BinaryOperator *BinOp2 = cast<BinaryOperator>(Stmt2);
|
2013-11-08 09:15:39 +08:00
|
|
|
return BinOp1->getOpcode() == BinOp2->getOpcode();
|
|
|
|
}
|
|
|
|
case Stmt::CharacterLiteralClass: {
|
2014-02-20 01:44:11 +08:00
|
|
|
const CharacterLiteral *CharLit1 = cast<CharacterLiteral>(Stmt1);
|
|
|
|
const CharacterLiteral *CharLit2 = cast<CharacterLiteral>(Stmt2);
|
2013-11-08 09:15:39 +08:00
|
|
|
return CharLit1->getValue() == CharLit2->getValue();
|
|
|
|
}
|
|
|
|
case Stmt::DeclRefExprClass: {
|
2014-02-20 01:44:11 +08:00
|
|
|
const DeclRefExpr *DeclRef1 = cast<DeclRefExpr>(Stmt1);
|
|
|
|
const DeclRefExpr *DeclRef2 = cast<DeclRefExpr>(Stmt2);
|
2013-11-08 09:15:39 +08:00
|
|
|
return DeclRef1->getDecl() == DeclRef2->getDecl();
|
|
|
|
}
|
|
|
|
case Stmt::IntegerLiteralClass: {
|
2014-02-20 01:44:11 +08:00
|
|
|
const IntegerLiteral *IntLit1 = cast<IntegerLiteral>(Stmt1);
|
|
|
|
const IntegerLiteral *IntLit2 = cast<IntegerLiteral>(Stmt2);
|
2014-08-21 00:51:26 +08:00
|
|
|
|
|
|
|
llvm::APInt I1 = IntLit1->getValue();
|
|
|
|
llvm::APInt I2 = IntLit2->getValue();
|
|
|
|
if (I1.getBitWidth() != I2.getBitWidth())
|
|
|
|
return false;
|
|
|
|
return I1 == I2;
|
2013-11-08 09:15:39 +08:00
|
|
|
}
|
|
|
|
case Stmt::FloatingLiteralClass: {
|
2014-02-20 01:44:11 +08:00
|
|
|
const FloatingLiteral *FloatLit1 = cast<FloatingLiteral>(Stmt1);
|
|
|
|
const FloatingLiteral *FloatLit2 = cast<FloatingLiteral>(Stmt2);
|
2013-11-08 09:15:39 +08:00
|
|
|
return FloatLit1->getValue().bitwiseIsEqual(FloatLit2->getValue());
|
|
|
|
}
|
2014-02-20 01:44:11 +08:00
|
|
|
case Stmt::StringLiteralClass: {
|
|
|
|
const StringLiteral *StringLit1 = cast<StringLiteral>(Stmt1);
|
|
|
|
const StringLiteral *StringLit2 = cast<StringLiteral>(Stmt2);
|
2014-08-21 00:51:18 +08:00
|
|
|
return StringLit1->getBytes() == StringLit2->getBytes();
|
2014-02-20 01:44:11 +08:00
|
|
|
}
|
2013-11-08 09:15:39 +08:00
|
|
|
case Stmt::MemberExprClass: {
|
2014-02-20 01:44:11 +08:00
|
|
|
const MemberExpr *MemberStmt1 = cast<MemberExpr>(Stmt1);
|
|
|
|
const MemberExpr *MemberStmt2 = cast<MemberExpr>(Stmt2);
|
|
|
|
return MemberStmt1->getMemberDecl() == MemberStmt2->getMemberDecl();
|
2013-11-08 09:15:39 +08:00
|
|
|
}
|
|
|
|
case Stmt::UnaryOperatorClass: {
|
2014-02-20 01:44:11 +08:00
|
|
|
const UnaryOperator *UnaryOp1 = cast<UnaryOperator>(Stmt1);
|
|
|
|
const UnaryOperator *UnaryOp2 = cast<UnaryOperator>(Stmt2);
|
2013-12-11 02:18:10 +08:00
|
|
|
return UnaryOp1->getOpcode() == UnaryOp2->getOpcode();
|
2013-11-08 09:15:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// FindIdenticalExprChecker
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-11-14 23:46:10 +08:00
|
|
|
namespace {
|
2013-11-08 09:15:39 +08:00
|
|
|
class FindIdenticalExprChecker : public Checker<check::ASTCodeBody> {
|
|
|
|
public:
|
|
|
|
void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
|
|
|
|
BugReporter &BR) const {
|
2014-02-12 05:49:21 +08:00
|
|
|
FindIdenticalExprVisitor Visitor(BR, this, Mgr.getAnalysisDeclContext(D));
|
2013-11-08 09:15:39 +08:00
|
|
|
Visitor.TraverseDecl(const_cast<Decl *>(D));
|
|
|
|
}
|
|
|
|
};
|
2013-11-14 23:46:10 +08:00
|
|
|
} // end anonymous namespace
|
2013-11-08 09:15:39 +08:00
|
|
|
|
|
|
|
void ento::registerIdenticalExprChecker(CheckerManager &Mgr) {
|
|
|
|
Mgr.registerChecker<FindIdenticalExprChecker>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
|
|
|
bool ento::shouldRegisterIdenticalExprChecker(const LangOptions &LO) {
|
|
|
|
return true;
|
|
|
|
}
|