| 1 | //===-- ExecuteFunction.h ---------------------------------------*- C++ -*-===// |
| 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 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_LIBC_TEST_UNITTEST_EXECUTEFUNCTION_H |
| 10 | #define LLVM_LIBC_TEST_UNITTEST_EXECUTEFUNCTION_H |
| 11 | |
| 12 | #include "src/__support/CPP/limits.h" |
| 13 | #include "src/__support/macros/config.h" |
| 14 | #include <stdint.h> |
| 15 | |
| 16 | namespace LIBC_NAMESPACE_DECL { |
| 17 | namespace testutils { |
| 18 | |
| 19 | class FunctionCaller { |
| 20 | public: |
| 21 | virtual ~FunctionCaller() {} |
| 22 | virtual void operator()() = 0; |
| 23 | }; |
| 24 | |
| 25 | struct ProcessStatus { |
| 26 | int platform_defined; |
| 27 | const char *failure = nullptr; |
| 28 | |
| 29 | static constexpr int TIMEOUT = cpp::numeric_limits<int>::max(); |
| 30 | |
| 31 | static ProcessStatus error(const char *error) { return {.platform_defined: 0, .failure: error}; } |
| 32 | static ProcessStatus timed_out_ps() { |
| 33 | return {.platform_defined: 0, .failure: reinterpret_cast<const char *>(TIMEOUT)}; |
| 34 | } |
| 35 | |
| 36 | bool timed_out() const { |
| 37 | return failure == reinterpret_cast<const char *>(TIMEOUT); |
| 38 | } |
| 39 | const char *get_error() const { return timed_out() ? nullptr : failure; } |
| 40 | bool exited_normally(); |
| 41 | int get_exit_code(); |
| 42 | int get_fatal_signal(); |
| 43 | }; |
| 44 | |
| 45 | ProcessStatus invoke_in_subprocess(FunctionCaller *func, |
| 46 | int timeout_ms = ProcessStatus::TIMEOUT); |
| 47 | |
| 48 | const char *signal_as_string(int signum); |
| 49 | |
| 50 | } // namespace testutils |
| 51 | } // namespace LIBC_NAMESPACE_DECL |
| 52 | |
| 53 | #endif // LLVM_LIBC_TEST_UNITTEST_EXECUTEFUNCTION_H |
| 54 | |