Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Checkers/UndefinedAssignmentChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- UndefinedAssignmentChecker.h ---------------------------*- 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 UndefinedAssignmentChecker, a builtin check in ExprEngine that
10
// checks for assigning undefined values.
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 UndefinedAssignmentChecker
25
  : public Checker<check::Bind> {
26
  mutable std::unique_ptr<BugType> BT;
27
28
public:
29
  void checkBind(SVal location, SVal val, const Stmt *S,
30
                 CheckerContext &C) const;
31
};
32
}
33
34
void UndefinedAssignmentChecker::checkBind(SVal location, SVal val,
35
                                           const Stmt *StoreE,
36
65.4k
                                           CheckerContext &C) const {
37
65.4k
  if (!val.isUndef())
38
65.4k
    return;
39
56
40
56
  // Do not report assignments of uninitialized values inside swap functions.
41
56
  // This should allow to swap partially uninitialized structs
42
56
  // (radar://14129997)
43
56
  if (const FunctionDecl *EnclosingFunctionDecl =
44
55
      dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl()))
45
55
    if (C.getCalleeName(EnclosingFunctionDecl) == "swap")
46
1
      return;
47
55
48
55
  ExplodedNode *N = C.generateErrorNode();
49
55
50
55
  if (!N)
51
0
    return;
52
55
53
55
  static const char *const DefaultMsg =
54
55
      "Assigned value is garbage or undefined";
55
55
  if (!BT)
56
18
    BT.reset(new BuiltinBug(this, DefaultMsg));
57
55
58
55
  // Generate a report for this bug.
59
55
  llvm::SmallString<128> Str;
60
55
  llvm::raw_svector_ostream OS(Str);
61
55
62
55
  const Expr *ex = nullptr;
63
55
64
55
  while (StoreE) {
65
55
    if (const UnaryOperator *U = dyn_cast<UnaryOperator>(StoreE)) {
66
4
      OS << "The expression is an uninitialized value. "
67
4
            "The computed value will also be garbage";
68
4
69
4
      ex = U->getSubExpr();
70
4
      break;
71
4
    }
72
51
73
51
    if (const BinaryOperator *B = dyn_cast<BinaryOperator>(StoreE)) {
74
23
      if (B->isCompoundAssignmentOp()) {
75
4
        if (C.getSVal(B->getLHS()).isUndef()) {
76
3
          OS << "The left expression of the compound assignment is an "
77
3
                "uninitialized value. The computed value will also be garbage";
78
3
          ex = B->getLHS();
79
3
          break;
80
3
        }
81
20
      }
82
20
83
20
      ex = B->getRHS();
84
20
      break;
85
20
    }
86
28
87
28
    if (const DeclStmt *DS = dyn_cast<DeclStmt>(StoreE)) {
88
9
      const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
89
9
      ex = VD->getInit();
90
9
    }
91
28
92
28
    if (const auto *CD =
93
19
            dyn_cast<CXXConstructorDecl>(C.getStackFrame()->getDecl())) {
94
19
      if (CD->isImplicit()) {
95
4
        for (auto I : CD->inits()) {
96
4
          if (I->getInit()->IgnoreImpCasts() == StoreE) {
97
2
            OS << "Value assigned to field '" << I->getMember()->getName()
98
2
               << "' in implicit constructor is garbage or undefined";
99
2
            break;
100
2
          }
101
4
        }
102
2
      }
103
19
    }
104
28
105
28
    break;
106
28
  }
107
55
108
55
  if (OS.str().empty())
109
46
    OS << DefaultMsg;
110
55
111
55
  auto R = llvm::make_unique<BugReport>(*BT, OS.str(), N);
112
55
  if (ex) {
113
36
    R->addRange(ex->getSourceRange());
114
36
    bugreporter::trackExpressionValue(N, ex, *R);
115
36
  }
116
55
  C.emitReport(std::move(R));
117
55
}
118
119
675
void ento::registerUndefinedAssignmentChecker(CheckerManager &mgr) {
120
675
  mgr.registerChecker<UndefinedAssignmentChecker>();
121
675
}
122
123
676
bool ento::shouldRegisterUndefinedAssignmentChecker(const LangOptions &LO) {
124
676
  return true;
125
676
}