Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Core/DynamicSize.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- DynamicSize.cpp - Dynamic size related APIs --------------*- 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 file defines APIs that track and query dynamic size information.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicSize.h"
14
#include "clang/AST/Expr.h"
15
#include "clang/Basic/LLVM.h"
16
#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
17
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
18
#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
19
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
20
#include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
21
22
namespace clang {
23
namespace ento {
24
25
DefinedOrUnknownSVal getDynamicSize(ProgramStateRef State, const MemRegion *MR,
26
4.05k
                                    SValBuilder &SVB) {
27
4.05k
  return MR->getMemRegionManager().getStaticSize(MR, SVB);
28
4.05k
}
29
30
DefinedOrUnknownSVal getDynamicElementCount(ProgramStateRef State,
31
                                            const MemRegion *MR,
32
                                            SValBuilder &SVB,
33
427
                                            QualType ElementTy) {
34
427
  MemRegionManager &MemMgr = MR->getMemRegionManager();
35
427
  ASTContext &Ctx = MemMgr.getContext();
36
427
37
427
  DefinedOrUnknownSVal Size = getDynamicSize(State, MR, SVB);
38
427
  SVal ElementSizeV = SVB.makeIntVal(
39
427
      Ctx.getTypeSizeInChars(ElementTy).getQuantity(), SVB.getArrayIndexType());
40
427
41
427
  SVal DivisionV =
42
427
      SVB.evalBinOp(State, BO_Div, Size, ElementSizeV, SVB.getArrayIndexType());
43
427
44
427
  return DivisionV.castAs<DefinedOrUnknownSVal>();
45
427
}
46
47
114
SVal getDynamicSizeWithOffset(ProgramStateRef State, const SVal &BufV) {
48
114
  SValBuilder &SvalBuilder = State->getStateManager().getSValBuilder();
49
114
  const MemRegion *MRegion = BufV.getAsRegion();
50
114
  if (!MRegion)
51
0
    return UnknownVal();
52
114
  RegionOffset Offset = MRegion->getAsOffset();
53
114
  if (Offset.hasSymbolicOffset())
54
0
    return UnknownVal();
55
114
  const MemRegion *BaseRegion = MRegion->getBaseRegion();
56
114
  if (!BaseRegion)
57
0
    return UnknownVal();
58
114
59
114
  NonLoc OffsetInBytes = SvalBuilder.makeArrayIndex(
60
114
      Offset.getOffset() /
61
114
      MRegion->getMemRegionManager().getContext().getCharWidth());
62
114
  DefinedOrUnknownSVal ExtentInBytes =
63
114
      getDynamicSize(State, BaseRegion, SvalBuilder);
64
114
65
114
  return SvalBuilder.evalBinOp(State, BinaryOperator::Opcode::BO_Sub,
66
114
                               ExtentInBytes, OffsetInBytes,
67
114
                               SvalBuilder.getArrayIndexType());
68
114
}
69
70
} // namespace ento
71
} // namespace clang