Skip to content

Commit 1ecaa61

Browse files
committed
[clang][Interp] Handle enums
Handle DeclRefExprs of enum types. They are otherwise handled like integers. Differential Revision: https://reviews.llvm.org/D134020
1 parent 052da73 commit 1ecaa61

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,11 @@ bool ByteCodeExprGen<Emitter>::VisitDeclRefExpr(const DeclRefExpr *E) {
861861

862862
FoundDecl = true;
863863
}
864+
} else if (const auto *ECD = dyn_cast<EnumConstantDecl>(Decl)) {
865+
PrimType T = *classify(ECD->getType());
866+
867+
return this->emitConst(T, getIntWidth(ECD->getType()), ECD->getInitVal(),
868+
E);
864869
}
865870

866871
// References are implemented using pointers, so when we get here,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %clang_cc1 -triple i686-pc-linux -fexperimental-new-constant-interpreter -verify %s
2+
// RUN: %clang_cc1 -triple i686-pc-linux -verify %s
3+
// RUN: %clang_cc1 -triple x86_64-pc-linux -fexperimental-new-constant-interpreter -verify=warn %s
4+
// RUN: %clang_cc1 -triple x86_64-pc-linux -verify=warn %s
5+
// RUN: %clang_cc1 -triple x86_64-windows-msvc -fexperimental-new-constant-interpreter -verify %s
6+
// RUN: %clang_cc1 -triple x86_64-windows-msvc -verify %s
7+
// RUN: %clang_cc1 -triple hexagon -fexperimental-new-constant-interpreter -verify %s
8+
// RUN: %clang_cc1 -triple hexagon -verify %s
9+
10+
// expected-no-diagnostics
11+
12+
/// This test is split out from the rest since the output is target dependent.
13+
14+
enum E { // warn-warning {{enumeration values exceed range of largest integer}}
15+
E1 = -__LONG_MAX__ -1L,
16+
E2 = __LONG_MAX__ *2UL+1UL
17+
};
18+

clang/test/AST/Interp/enums.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify %s
2+
// RUN: %clang_cc1 -verify=ref %s
3+
4+
enum class EC : short {
5+
A, B, C
6+
};
7+
static_assert(static_cast<int>(EC::A) == 0, "");
8+
static_assert(static_cast<int>(EC::B) == 1, "");
9+
static_assert(static_cast<int>(EC::C) == 2, "");
10+
static_assert(sizeof(EC) == sizeof(short), "");
11+
12+
constexpr EC ec = EC::C;
13+
static_assert(static_cast<int>(ec) == 2, "");
14+
15+
constexpr int N = 12;
16+
constexpr int M = 2;
17+
18+
enum CE {
19+
ONE = -1,
20+
TWO = 2,
21+
THREE,
22+
FOUR = 4,
23+
FIVE = N + M,
24+
SIX = FIVE + 2,
25+
MAX = __INT_MAX__ * 2U + 1U
26+
};
27+
static_assert(ONE == -1, "");
28+
static_assert(THREE == 3, "");
29+
static_assert(FIVE == 14, "");
30+
static_assert(SIX == 16, "");
31+
32+
constexpr EC testEnums() {
33+
EC e = EC::C;
34+
35+
e = EC::B;
36+
37+
EC::B = e; // expected-error{{expression is not assignable}} \
38+
// ref-error{{expression is not assignable}}
39+
40+
return e;
41+
}
42+
43+
constexpr EC getB() {
44+
EC e = EC::C;
45+
e = EC::B;
46+
return e;
47+
}
48+
49+
50+
static_assert(getB() == EC::B, "");

0 commit comments

Comments
 (0)