1 //===-- FPExceptMatchers.cpp ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "FPExceptMatcher.h"
16 namespace LIBC_NAMESPACE
{
20 #define sigjmp_buf jmp_buf
21 #define sigsetjmp(buf, save) setjmp(buf)
22 #define siglongjmp(buf, val) longjmp(buf, val)
25 static thread_local sigjmp_buf jumpBuffer
;
26 static thread_local
bool caughtExcept
;
28 static void sigfpeHandler(int sig
) {
30 siglongjmp(jumpBuffer
, -1);
33 FPExceptMatcher::FPExceptMatcher(FunctionCaller
*func
) {
34 auto oldSIGFPEHandler
= signal(SIGFPE
, &sigfpeHandler
);
35 std::unique_ptr
<FunctionCaller
> funcUP(func
);
40 if (sigsetjmp(jumpBuffer
, 1) == 0)
42 // We restore the previous floating point environment after
43 // the call to the function which can potentially raise SIGFPE.
45 signal(SIGFPE
, oldSIGFPEHandler
);
46 exceptionRaised
= caughtExcept
;
49 } // namespace testing
50 } // namespace LIBC_NAMESPACE