Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/Interp/Context.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- Context.cpp - Context for the constexpr VM -------------*- 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
#include "Context.h"
10
#include "ByteCodeEmitter.h"
11
#include "ByteCodeExprGen.h"
12
#include "ByteCodeStmtGen.h"
13
#include "EvalEmitter.h"
14
#include "Interp.h"
15
#include "InterpFrame.h"
16
#include "InterpStack.h"
17
#include "PrimType.h"
18
#include "Program.h"
19
#include "clang/AST/Expr.h"
20
21
using namespace clang;
22
using namespace clang::interp;
23
24
1
Context::Context(ASTContext &Ctx) : Ctx(Ctx), P(new Program(*this)) {}
25
26
1
Context::~Context() {}
27
28
1
bool Context::isPotentialConstantExpr(State &Parent, const FunctionDecl *FD) {
29
1
  Function *Func = P->getFunction(FD);
30
1
  if (!Func) {
31
1
    if (auto R = ByteCodeStmtGen<ByteCodeEmitter>(*this, *P).compileFunc(FD)) {
32
1
      Func = *R;
33
1
    } else {
34
0
      handleAllErrors(R.takeError(), [&Parent](ByteCodeGenError &Err) {
35
0
        Parent.FFDiag(Err.getLoc(), diag::err_experimental_clang_interp_failed);
36
0
      });
37
0
      return false;
38
0
    }
39
1
  }
40
1
41
1
  if (!Func->isConstexpr())
42
0
    return false;
43
1
44
1
  APValue Dummy;
45
1
  return Run(Parent, Func, Dummy);
46
1
}
47
48
7
bool Context::evaluateAsRValue(State &Parent, const Expr *E, APValue &Result) {
49
7
  ByteCodeExprGen<EvalEmitter> C(*this, *P, Parent, Stk, Result);
50
7
  return Check(Parent, C.interpretExpr(E));
51
7
}
52
53
bool Context::evaluateAsInitializer(State &Parent, const VarDecl *VD,
54
0
                                    APValue &Result) {
55
0
  ByteCodeExprGen<EvalEmitter> C(*this, *P, Parent, Stk, Result);
56
0
  return Check(Parent, C.interpretDecl(VD));
57
0
}
58
59
0
const LangOptions &Context::getLangOpts() const { return Ctx.getLangOpts(); }
60
61
42
llvm::Optional<PrimType> Context::classify(QualType T) {
62
42
  if (T->isReferenceType() || T->isPointerType()) {
63
0
    return PT_Ptr;
64
0
  }
65
42
66
42
  if (T->isBooleanType())
67
5
    return PT_Bool;
68
37
69
37
  if (T->isSignedIntegerOrEnumerationType()) {
70
37
    switch (Ctx.getIntWidth(T)) {
71
0
    case 64:
72
0
      return PT_Sint64;
73
37
    case 32:
74
37
      return PT_Sint32;
75
0
    case 16:
76
0
      return PT_Sint16;
77
0
    case 8:
78
0
      return PT_Sint8;
79
0
    default:
80
0
      return {};
81
0
    }
82
0
  }
83
0
84
0
  if (T->isUnsignedIntegerOrEnumerationType()) {
85
0
    switch (Ctx.getIntWidth(T)) {
86
0
    case 64:
87
0
      return PT_Uint64;
88
0
    case 32:
89
0
      return PT_Uint32;
90
0
    case 16:
91
0
      return PT_Uint16;
92
0
    case 8:
93
0
      return PT_Uint8;
94
0
    default:
95
0
      return {};
96
0
    }
97
0
  }
98
0
99
0
  if (T->isNullPtrType())
100
0
    return PT_Ptr;
101
0
102
0
  if (auto *AT = dyn_cast<AtomicType>(T))
103
0
    return classify(AT->getValueType());
104
0
105
0
  return {};
106
0
}
107
108
0
unsigned Context::getCharBit() const {
109
0
  return Ctx.getTargetInfo().getCharWidth();
110
0
}
111
112
1
bool Context::Run(State &Parent, Function *Func, APValue &Result) {
113
1
  InterpState State(Parent, *P, Stk, *this);
114
1
  State.Current = new InterpFrame(State, Func, nullptr, {}, {});
115
1
  if (Interpret(State, Result))
116
0
    return true;
117
1
  Stk.clear();
118
1
  return false;
119
1
}
120
121
7
bool Context::Check(State &Parent, llvm::Expected<bool> &&Flag) {
122
7
  if (Flag)
123
7
    return *Flag;
124
0
  handleAllErrors(Flag.takeError(), [&Parent](ByteCodeGenError &Err) {
125
0
    Parent.FFDiag(Err.getLoc(), diag::err_experimental_clang_interp_failed);
126
0
  });
127
0
  return false;
128
0
}