[clang-repl] [codegen] Reduce the state in TBAA. NFC for static compilation. (#98138)
[llvm-project.git] / bolt / test / runtime / X86 / rethrow.cpp
blob0b1ea4319ececb0fbbcf081d9ae8c8ec2ef9161e
1 #include <iostream>
2 #include <stdexcept>
4 void erringFunc() { throw std::runtime_error("Hello"); }
6 void libCallA() { erringFunc(); }
8 void libCallB() { throw std::runtime_error("World"); }
10 void handleEventA() {
11 try {
12 libCallA();
13 } catch (std::runtime_error &E) {
14 std::cout << "handleEventA: unhandled error " << E.what() << "\n";
15 throw;
19 void handleEventB() {
20 try {
21 libCallB();
22 } catch (std::runtime_error &E) {
23 std::cout << "handleEventB: handle error " << E.what() << "\n";
27 class EventGen {
28 unsigned RemainingEvents = 5;
30 public:
31 int generateEvent() {
32 if (RemainingEvents > 0) {
33 --RemainingEvents;
34 return (RemainingEvents % 3) + 1;
36 return 0;
40 class TerminateException : public std::runtime_error {
41 public:
42 TerminateException() : std::runtime_error("Time to stop!") {}
45 void runEventLoop(EventGen &EG) {
46 while (true) {
47 try {
48 int Ev = EG.generateEvent();
49 switch (Ev) {
50 case 0:
51 throw TerminateException();
52 case 1:
53 handleEventA();
54 break;
55 case 2:
56 handleEventB();
57 break;
59 } catch (TerminateException &E) {
60 std::cout << "Terminated?\n";
61 throw;
62 } catch (std::runtime_error &E) {
63 std::cout << "Unhandled error: " << E.what() << "\n";
68 struct CleanUp {
69 ~CleanUp() { std::cout << "Cleanup\n"; }
72 int main() {
73 EventGen EG;
74 try {
75 CleanUp CU;
76 runEventLoop(EG);
77 } catch (TerminateException &E) {
78 std::cout << "Terminated!\n";
80 return 0;
84 REQUIRES: system-linux
86 RUN: %clang++ %cflags %s -o %t.exe -Wl,-q
87 RUN: llvm-bolt %t.exe --split-functions --split-strategy=randomN \
88 RUN: --split-all-cold --split-eh --bolt-seed=7 -o %t.bolt
89 RUN: %t.bolt | FileCheck %s
91 CHECK: handleEventB: handle error World
92 CHECK-NEXT: handleEventA: unhandled error Hello
93 CHECK-NEXT: Unhandled error: Hello
94 CHECK-NEXT: handleEventB: handle error World
95 CHECK-NEXT: handleEventA: unhandled error Hello
96 CHECK-NEXT: Unhandled error: Hello
97 CHECK-NEXT: Terminated?
98 CHECK-NEXT: Cleanup
99 CHECK-NEXT: Terminated!