xref: /llvm-project/bolt/test/runtime/exceptions-no-pie.cpp (revision 553c23895217cfe24404a50dede2416c9e1a3a5e)
1 // REQUIRES: system-linux
2 // RUN: %clangxx -no-pie -Wl,-q %s -o %t.exe
3 // RUN: llvm-bolt %t.exe -o %t.bolt.exe -lite=false
4 // RUN: not --crash %t.bolt.exe 2>&1 | FileCheck %s --check-prefix=CHECK-FAIL
5 // CHECK-FAIL: Should pass one argument
6 // RUN: not %t.bolt.exe -1 | FileCheck %s --check-prefix=CHECK-BAD
7 // CHECK-BAD: Bad value
8 // RUN: not %t.bolt.exe 0 | FileCheck %s --check-prefix=CHECK-ZERO
9 // CHECK-ZERO: Value is zero
10 // RUN: %t.bolt.exe 1 | FileCheck %s --check-prefix=CHECK-GOOD
11 // CHECK-GOOD: Good value
12 #include <exception>
13 #include <iostream>
14 
15 struct ValIsZero {
16   const char *error = "Value is zero\n";
17 };
dummy(int arg)18 int dummy(int arg) {
19   if (arg == 0)
20     throw ValIsZero();
21   if (arg > 0)
22     return 0;
23   else
24     throw std::out_of_range("Bad value");
25 }
26 
main(int argc,char ** argv)27 int main(int argc, char **argv) {
28   if (argc != 2)
29     throw std::invalid_argument("Should pass one argument");
30   try {
31     dummy(std::strtol(argv[1], nullptr, 10));
32   } catch (std::out_of_range &e) {
33     std::cout << e.what() << "\n";
34     return 1;
35   } catch (ValIsZero &e) {
36     std::cout << e.error;
37     return 1;
38   }
39   std::cout << "Good value\n";
40   return 0;
41 }
42