//=== ConversionChecker.cpp -------------------------------------*- C++ -*-===// // // 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 // //===----------------------------------------------------------------------===// // // Check that there is no loss of sign/precision in assignments, comparisons // and multiplications. // // ConversionChecker uses path sensitive analysis to determine possible values // of expressions. A warning is reported when: // * a negative value is implicitly converted to an unsigned value in an // assignment, comparison or multiplication. // * assignment / initialization when the source value is greater than the max // value of the target integer type // * assignment / initialization when the source integer is above the range // where the target floating point type can represent all integers // // Many compilers and tools have similar checks that are based on semantic // analysis. Those checks are sound but have poor precision. ConversionChecker // is an alternative to those checks. // //===----------------------------------------------------------------------===// #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" #include "clang/AST/ParentMap.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" #include "llvm/ADT/APFloat.h" #include <climits> usingnamespaceclang; usingnamespaceento; namespace { class ConversionChecker : public Checker<check::PreStmt<ImplicitCastExpr>> { … }; } void ConversionChecker::checkPreStmt(const ImplicitCastExpr *Cast, CheckerContext &C) const { … } void ConversionChecker::reportBug(ExplodedNode *N, const Expr *E, CheckerContext &C, const char Msg[]) const { … } bool ConversionChecker::isLossOfPrecision(const ImplicitCastExpr *Cast, QualType DestType, CheckerContext &C) const { … } bool ConversionChecker::isLossOfSign(const ImplicitCastExpr *Cast, CheckerContext &C) const { … } void ento::registerConversionChecker(CheckerManager &mgr) { … } bool ento::shouldRegisterConversionChecker(const CheckerManager &mgr) { … }