brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.1 KiB · 0b1ea43 Raw
101 lines · cpp
1#include <iostream>2#include <stdexcept>3 4void erringFunc() { throw std::runtime_error("Hello"); }5 6void libCallA() { erringFunc(); }7 8void libCallB() { throw std::runtime_error("World"); }9 10void handleEventA() {11  try {12    libCallA();13  } catch (std::runtime_error &E) {14    std::cout << "handleEventA: unhandled error " << E.what() << "\n";15    throw;16  }17}18 19void handleEventB() {20  try {21    libCallB();22  } catch (std::runtime_error &E) {23    std::cout << "handleEventB: handle error " << E.what() << "\n";24  }25}26 27class EventGen {28  unsigned RemainingEvents = 5;29 30public:31  int generateEvent() {32    if (RemainingEvents > 0) {33      --RemainingEvents;34      return (RemainingEvents % 3) + 1;35    }36    return 0;37  }38};39 40class TerminateException : public std::runtime_error {41public:42  TerminateException() : std::runtime_error("Time to stop!") {}43};44 45void 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;58      }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";64    }65  }66}67 68struct CleanUp {69  ~CleanUp() { std::cout << "Cleanup\n"; }70};71 72int main() {73  EventGen EG;74  try {75    CleanUp CU;76    runEventLoop(EG);77  } catch (TerminateException &E) {78    std::cout << "Terminated!\n";79  }80  return 0;81}82 83/*84REQUIRES: system-linux85 86RUN: %clang++ %cflags %s -o %t.exe -Wl,-q87RUN: llvm-bolt %t.exe --split-functions --split-strategy=randomN \88RUN:         --split-all-cold --split-eh --bolt-seed=7 -o %t.bolt89RUN: %t.bolt | FileCheck %s90 91CHECK: handleEventB: handle error World92CHECK-NEXT: handleEventA: unhandled error Hello93CHECK-NEXT: Unhandled error: Hello94CHECK-NEXT: handleEventB: handle error World95CHECK-NEXT: handleEventA: unhandled error Hello96CHECK-NEXT: Unhandled error: Hello97CHECK-NEXT: Terminated?98CHECK-NEXT: Cleanup99CHECK-NEXT: Terminated!100*/101