File: lsda-section-name.cpp

package info (click to toggle)
llvm-toolchain-21 1%3A21.1.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,245,028 kB
  • sloc: cpp: 7,619,726; ansic: 1,434,018; asm: 1,058,748; python: 252,740; f90: 94,671; objc: 70,685; lisp: 42,813; pascal: 18,401; sh: 8,601; ml: 5,111; perl: 4,720; makefile: 3,675; awk: 3,523; javascript: 2,409; xml: 892; fortran: 770
file content (47 lines) | stat: -rw-r--r-- 1,317 bytes parent folder | download | duplicates (8)
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
// This test check that LSDA section named by .gcc_except_table.main is
// disassembled by BOLT.

// RUN: %clang++ %cxxflags -O3 -no-pie -c %s -o %t.o
// RUN: %clang++ %cxxflags -O3 -no-pie -fuse-ld=lld %t.o -o %t
// RUN: llvm-objcopy --rename-section .gcc_except_table=.gcc_except_table.main %t
// RUN: llvm-readelf -SW %t | FileCheck %s
// RUN: llvm-bolt %t -o %t.bolt

// CHECK: .gcc_except_table.main

#include <iostream>

class MyException : public std::exception {
public:
  const char *what() const throw() {
    return "Custom Exception: an error occurred!";
  }
};

int divide(int a, int b) {
  if (b == 0) {
    throw MyException();
  }
  return a / b;
}

int main() {
  try {
    int result = divide(10, 2); // normal case
    std::cout << "Result: " << result << std::endl;
    result = divide(5, 0); // will cause exception
    std::cout << "Result: " << result << std::endl;
    // this line will not execute
  } catch (const MyException &e) {
    // catch custom exception
    std::cerr << "Caught exception: " << e.what() << std::endl;
  } catch (const std::exception &e) {
    // catch other C++ exceptions
    std::cerr << "Caught exception: " << e.what() << std::endl;
  } catch (...) {
    // catch all other exceptions
    std::cerr << "Caught unknown exception" << std::endl;
  }

  return 0;
}