//== IdenticalExprChecker.cpp - Identical expression checker----------------==// // // 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 // //===----------------------------------------------------------------------===// /// /// \file /// This defines IdenticalExprChecker, a check that warns about /// unintended use of identical expressions. /// /// It checks for use of identical expressions with comparison operators and /// inside conditional expressions. /// //===----------------------------------------------------------------------===// #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" #include "clang/AST/RecursiveASTVisitor.h" #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" usingnamespaceclang; usingnamespaceento; static bool isIdenticalStmt(const ASTContext &Ctx, const Stmt *Stmt1, const Stmt *Stmt2, bool IgnoreSideEffects = false); //===----------------------------------------------------------------------===// // FindIdenticalExprVisitor - Identify nodes using identical expressions. //===----------------------------------------------------------------------===// namespace { class FindIdenticalExprVisitor : public RecursiveASTVisitor<FindIdenticalExprVisitor> { … }; } // end anonymous namespace void FindIdenticalExprVisitor::reportIdenticalExpr(const BinaryOperator *B, bool CheckBitwise, ArrayRef<SourceRange> Sr) { … } void FindIdenticalExprVisitor::checkBitwiseOrLogicalOp(const BinaryOperator *B, bool CheckBitwise) { … } bool FindIdenticalExprVisitor::VisitIfStmt(const IfStmt *I) { … } bool FindIdenticalExprVisitor::VisitBinaryOperator(const BinaryOperator *B) { … } void FindIdenticalExprVisitor::checkComparisonOp(const BinaryOperator *B) { … } bool FindIdenticalExprVisitor::VisitConditionalOperator( const ConditionalOperator *C) { … } /// Determines whether two statement trees are identical regarding /// 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. /// static bool isIdenticalStmt(const ASTContext &Ctx, const Stmt *Stmt1, const Stmt *Stmt2, bool IgnoreSideEffects) { … } //===----------------------------------------------------------------------===// // FindIdenticalExprChecker //===----------------------------------------------------------------------===// namespace { class FindIdenticalExprChecker : public Checker<check::ASTCodeBody> { … }; } // end anonymous namespace void ento::registerIdenticalExprChecker(CheckerManager &Mgr) { … } bool ento::shouldRegisterIdenticalExprChecker(const CheckerManager &mgr) { … }