xref: /freebsd/contrib/llvm-project/clang/lib/StaticAnalyzer/Checkers/BoolAssignmentChecker.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
10b57cec5SDimitry Andric //== BoolAssignmentChecker.cpp - Boolean assignment checker -----*- C++ -*--==//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This defines BoolAssignmentChecker, a builtin check in ExprEngine that
100b57cec5SDimitry Andric // performs checks for assignment of non-Boolean values to Boolean variables.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
150b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
160b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h"
170b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h"
180b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric using namespace clang;
210b57cec5SDimitry Andric using namespace ento;
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric namespace {
240b57cec5SDimitry Andric   class BoolAssignmentChecker : public Checker< check::Bind > {
250b57cec5SDimitry Andric     mutable std::unique_ptr<BuiltinBug> BT;
260b57cec5SDimitry Andric     void emitReport(ProgramStateRef state, CheckerContext &C) const;
270b57cec5SDimitry Andric   public:
280b57cec5SDimitry Andric     void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const;
290b57cec5SDimitry Andric   };
300b57cec5SDimitry Andric } // end anonymous namespace
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric void BoolAssignmentChecker::emitReport(ProgramStateRef state,
330b57cec5SDimitry Andric                                        CheckerContext &C) const {
340b57cec5SDimitry Andric   if (ExplodedNode *N = C.generateNonFatalErrorNode(state)) {
350b57cec5SDimitry Andric     if (!BT)
360b57cec5SDimitry Andric       BT.reset(new BuiltinBug(this, "Assignment of a non-Boolean value"));
37a7dea167SDimitry Andric 
38a7dea167SDimitry Andric     C.emitReport(
39a7dea167SDimitry Andric         std::make_unique<PathSensitiveBugReport>(*BT, BT->getDescription(), N));
400b57cec5SDimitry Andric   }
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric static bool isBooleanType(QualType Ty) {
440b57cec5SDimitry Andric   if (Ty->isBooleanType()) // C++ or C99
450b57cec5SDimitry Andric     return true;
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric   if (const TypedefType *TT = Ty->getAs<TypedefType>())
480b57cec5SDimitry Andric     return TT->getDecl()->getName() == "BOOL"   || // Objective-C
490b57cec5SDimitry Andric            TT->getDecl()->getName() == "_Bool"  || // stdbool.h < C99
500b57cec5SDimitry Andric            TT->getDecl()->getName() == "Boolean";  // MacTypes.h
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   return false;
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric void BoolAssignmentChecker::checkBind(SVal loc, SVal val, const Stmt *S,
560b57cec5SDimitry Andric                                       CheckerContext &C) const {
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   // We are only interested in stores into Booleans.
590b57cec5SDimitry Andric   const TypedValueRegion *TR =
600b57cec5SDimitry Andric     dyn_cast_or_null<TypedValueRegion>(loc.getAsRegion());
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric   if (!TR)
630b57cec5SDimitry Andric     return;
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   QualType valTy = TR->getValueType();
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric   if (!isBooleanType(valTy))
680b57cec5SDimitry Andric     return;
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric   // Get the value of the right-hand side.  We only care about values
710b57cec5SDimitry Andric   // that are defined (UnknownVals and UndefinedVals are handled by other
720b57cec5SDimitry Andric   // checkers).
73*5ffd83dbSDimitry Andric   Optional<NonLoc> NV = val.getAs<NonLoc>();
74*5ffd83dbSDimitry Andric   if (!NV)
750b57cec5SDimitry Andric     return;
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   // Check if the assigned value meets our criteria for correctness.  It must
780b57cec5SDimitry Andric   // be a value that is either 0 or 1.  One way to check this is to see if
790b57cec5SDimitry Andric   // the value is possibly < 0 (for a negative value) or greater than 1.
800b57cec5SDimitry Andric   ProgramStateRef state = C.getState();
810b57cec5SDimitry Andric   SValBuilder &svalBuilder = C.getSValBuilder();
82*5ffd83dbSDimitry Andric   BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
830b57cec5SDimitry Andric   ConstraintManager &CM = C.getConstraintManager();
840b57cec5SDimitry Andric 
85*5ffd83dbSDimitry Andric   llvm::APSInt Zero = BVF.getValue(0, valTy);
86*5ffd83dbSDimitry Andric   llvm::APSInt One = BVF.getValue(1, valTy);
870b57cec5SDimitry Andric 
88*5ffd83dbSDimitry Andric   ProgramStateRef StIn, StOut;
89*5ffd83dbSDimitry Andric   std::tie(StIn, StOut) = CM.assumeInclusiveRangeDual(state, *NV, Zero, One);
900b57cec5SDimitry Andric 
91*5ffd83dbSDimitry Andric   if (!StIn)
92*5ffd83dbSDimitry Andric     emitReport(StOut, C);
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric void ento::registerBoolAssignmentChecker(CheckerManager &mgr) {
960b57cec5SDimitry Andric     mgr.registerChecker<BoolAssignmentChecker>();
970b57cec5SDimitry Andric }
980b57cec5SDimitry Andric 
99*5ffd83dbSDimitry Andric bool ento::shouldRegisterBoolAssignmentChecker(const CheckerManager &mgr) {
1000b57cec5SDimitry Andric   return true;
1010b57cec5SDimitry Andric }
102