Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Checkers/BoolAssignmentChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//== BoolAssignmentChecker.cpp - Boolean assignment checker -----*- C++ -*--==//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This defines BoolAssignmentChecker, a builtin check in ExprEngine that
10
// performs checks for assignment of non-Boolean values to Boolean variables.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
16
#include "clang/StaticAnalyzer/Core/Checker.h"
17
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
18
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19
20
using namespace clang;
21
using namespace ento;
22
23
namespace {
24
  class BoolAssignmentChecker : public Checker< check::Bind > {
25
    mutable std::unique_ptr<BuiltinBug> BT;
26
    void emitReport(ProgramStateRef state, CheckerContext &C) const;
27
  public:
28
    void checkBind(SVal loc, SVal val, const Stmt *S, CheckerContext &C) const;
29
  };
30
} // end anonymous namespace
31
32
void BoolAssignmentChecker::emitReport(ProgramStateRef state,
33
22
                                       CheckerContext &C) const {
34
22
  if (ExplodedNode *N = C.generateNonFatalErrorNode(state)) {
35
22
    if (!BT)
36
2
      BT.reset(new BuiltinBug(this, "Assignment of a non-Boolean value"));
37
22
38
22
    C.emitReport(
39
22
        std::make_unique<PathSensitiveBugReport>(*BT, BT->getDescription(), N));
40
22
  }
41
22
}
42
43
1.84k
static bool isBooleanType(QualType Ty) {
44
1.84k
  if (Ty->isBooleanType()) // C++ or C99
45
17
    return true;
46
1.82k
47
1.82k
  if (const TypedefType *TT = Ty->getAs<TypedefType>())
48
116
    return TT->getDecl()->getName() == "BOOL"   || // Objective-C
49
85
           TT->getDecl()->getName() == "_Bool"  || // stdbool.h < C99
50
85
           TT->getDecl()->getName() == "Boolean";  // MacTypes.h
51
1.70k
52
1.70k
  return false;
53
1.70k
}
54
55
void BoolAssignmentChecker::checkBind(SVal loc, SVal val, const Stmt *S,
56
1.95k
                                      CheckerContext &C) const {
57
1.95k
58
  // We are only interested in stores into Booleans.
59
1.95k
  const TypedValueRegion *TR =
60
1.95k
    dyn_cast_or_null<TypedValueRegion>(loc.getAsRegion());
61
1.95k
62
1.95k
  if (!TR)
63
110
    return;
64
1.84k
65
1.84k
  QualType valTy = TR->getValueType();
66
1.84k
67
1.84k
  if (!isBooleanType(valTy))
68
1.77k
    return;
69
64
70
  // Get the value of the right-hand side.  We only care about values
71
  // that are defined (UnknownVals and UndefinedVals are handled by other
72
  // checkers).
73
64
  Optional<NonLoc> NV = val.getAs<NonLoc>();
74
64
  if (!NV)
75
0
    return;
76
64
77
  // Check if the assigned value meets our criteria for correctness.  It must
78
  // be a value that is either 0 or 1.  One way to check this is to see if
79
  // the value is possibly < 0 (for a negative value) or greater than 1.
80
64
  ProgramStateRef state = C.getState();
81
64
  SValBuilder &svalBuilder = C.getSValBuilder();
82
64
  BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
83
64
  ConstraintManager &CM = C.getConstraintManager();
84
64
85
64
  llvm::APSInt Zero = BVF.getValue(0, valTy);
86
64
  llvm::APSInt One = BVF.getValue(1, valTy);
87
64
88
64
  ProgramStateRef StIn, StOut;
89
64
  std::tie(StIn, StOut) = CM.assumeInclusiveRangeDual(state, *NV, Zero, One);
90
64
91
64
  if (!StIn)
92
22
    emitReport(StOut, C);
93
64
}
94
95
70
void ento::registerBoolAssignmentChecker(CheckerManager &mgr) {
96
70
    mgr.registerChecker<BoolAssignmentChecker>();
97
70
}
98
99
140
bool ento::shouldRegisterBoolAssignmentChecker(const CheckerManager &mgr) {
100
140
  return true;
101
140
}