File: cxx20-consteval-crash.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (142 lines) | stat: -rw-r--r-- 3,333 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 %s -emit-llvm -o - | FileCheck %s
// RUN: %clang_cc1 -emit-obj -debug-info-kind=constructor -std=c++20 %s -o -

namespace PR50787 {
// This code would previously cause a crash.
extern int x_;
consteval auto& X() { return x_; }
constexpr auto& x1 = X();
auto x2 = X();

// CHECK: @_ZN7PR507872x_E = external global i32, align 4
// CHECK-NEXT: @_ZN7PR507872x1E = constant ptr @_ZN7PR507872x_E, align 8
// CHECK-NEXT: @_ZN7PR507872x2E = global i32 0, align 4
}

namespace PR51484 {
// This code would previously cause a crash.
struct X { int val; };
consteval X g() { return {0}; }
void f() { g(); }

// CHECK: define dso_local void @_ZN7PR514841fEv() #1 {
// CHECK: entry:
// CHECK-NOT: call i32 @_ZN7PR514841gEv()
// CHECK:  ret void
// CHECK: }
}

namespace Issue54578 {
inline consteval unsigned char operator""_UC(const unsigned long long n) {
  return static_cast<unsigned char>(n);
}

inline constexpr char f1(const auto octet) {
  return 4_UC;
}

template <typename Ty>
inline constexpr char f2(const Ty octet) {
  return 4_UC;
}

int foo() {
  return f1('a') + f2('a');
}

// Because the consteval functions are inline (implicitly as well as
// explicitly), we need to defer the CHECK lines until this point to get the
// order correct. We want to ensure there is no definition of the consteval
// UDL function, and that the constexpr f1 and f2 functions both return a
// constant value.

// CHECK-NOT: define{{.*}} zeroext i8 @_ZN10Issue54578li3_UCEy
// CHECK: define{{.*}} i32 @_ZN10Issue545783fooEv(
// CHECK: define{{.*}} signext i8 @_ZN10Issue545782f1IcEEcT_(
// CHECK: ret i8 4
// CHECK: define{{.*}} signext i8 @_ZN10Issue545782f2IcEEcT_(
// CHECK: ret i8 4
}

namespace Issue55871 {
struct Item {
  consteval Item(char c) :_char{c}{}
  char _char;
};

int function(const Item& item1, const Item& item2) {
  return 0;
}

int foo() {
  return function(Item{'a'}, Item{'a'});
}
} // namespace Issue58871

namespace Issue55065 {
struct Base {
  consteval virtual int Get() const = 0;
};

struct Derived : Base {
  consteval int Get() const override {
    return 42;
  }
};

int foo() {
  constexpr Derived a;

  auto val = a.Get();
  return val;
}
} // namespace Issue55065

namespace GH60166 {

struct Base {
   void* one = nullptr;
   void* two = nullptr;
};

struct Derived : Base {
   void* three = nullptr;
   consteval Derived() = default;
};

void method() {
  // CHECK: %agg.tmp.ensured = alloca %"struct.GH60166::Derived"
  // CHECK: %0 = getelementptr inbounds { ptr, ptr, ptr }, ptr %agg.tmp.ensured, i32 0, i32 0
  // CHECK: store ptr null, ptr %0, align 8
  // CHECK: %1 = getelementptr inbounds { ptr, ptr, ptr }, ptr %agg.tmp.ensured, i32 0, i32 1
  // CHECK: store ptr null, ptr %1, align 8
  // CHECK: %2 = getelementptr inbounds { ptr, ptr, ptr }, ptr %agg.tmp.ensured, i32 0, i32 2
  // CHECK: store ptr null, ptr %2, align 8
   (void)Derived();
}

} // namespace GH60166

namespace GH61142 {

template <typename T>
struct Test {
  constexpr static void bar() {
    foo();
  }
  consteval static void foo() {};
};

consteval void a() {
  Test<int>::bar();
}

void b() {
  Test<int>::bar();
}

// Make sure consteval function is not emitted.
// CHECK-NOT: call {{.*}}foo{{.*}}()
// CHECK-NOT: define {{.*}}foo{{.*}}()

} // namespace GH61142