[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / libcxxabi / test / backtrace_test.pass.cpp
blobb95ce4f33be8edb5280438d746cf14675ac64455
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: no-exceptions
11 #include <assert.h>
12 #include <stddef.h>
13 #include <unwind.h>
15 extern "C" _Unwind_Reason_Code
16 trace_function(struct _Unwind_Context*, void* ntraced) {
17 (*reinterpret_cast<size_t*>(ntraced))++;
18 // We should never have a call stack this deep...
19 assert(*reinterpret_cast<size_t*>(ntraced) < 20);
20 return _URC_NO_REASON;
23 __attribute__ ((__noinline__))
24 void call3_throw(size_t* ntraced) {
25 try {
26 _Unwind_Backtrace(trace_function, ntraced);
27 } catch (...) {
28 assert(false);
32 __attribute__ ((__noinline__, __disable_tail_calls__))
33 void call3_nothrow(size_t* ntraced) {
34 _Unwind_Backtrace(trace_function, ntraced);
37 __attribute__ ((__noinline__, __disable_tail_calls__))
38 void call2(size_t* ntraced, bool do_throw) {
39 if (do_throw) {
40 call3_throw(ntraced);
41 } else {
42 call3_nothrow(ntraced);
46 __attribute__ ((__noinline__, __disable_tail_calls__))
47 void call1(size_t* ntraced, bool do_throw) {
48 call2(ntraced, do_throw);
51 int main(int, char**) {
52 size_t throw_ntraced = 0;
53 size_t nothrow_ntraced = 0;
55 call1(&nothrow_ntraced, false);
57 try {
58 call1(&throw_ntraced, true);
59 } catch (...) {
60 assert(false);
63 // Different platforms (and different runtimes) will unwind a different number
64 // of times, so we can't make any better assumptions than this.
65 assert(nothrow_ntraced > 1);
66 assert(throw_ntraced == nothrow_ntraced); // Make sure we unwind through catch
67 return 0;