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_UTILS_TESTUTILS_EXECUTEFUNCTION_H 10 #define LLVM_LIBC_UTILS_TESTUTILS_EXECUTEFUNCTION_H 11 12 #include <stdint.h> 13 14 namespace __llvm_libc { 15 namespace testutils { 16 17 class FunctionCaller { 18 public: ~FunctionCaller()19 virtual ~FunctionCaller() {} 20 virtual void operator()() = 0; 21 }; 22 23 struct ProcessStatus { 24 int PlatformDefined; 25 const char *failure = nullptr; 26 27 static constexpr uintptr_t timeout = -1L; 28 ErrorProcessStatus29 static ProcessStatus Error(const char *error) { return {0, error}; } TimedOutProcessStatus30 static ProcessStatus TimedOut() { 31 return {0, reinterpret_cast<const char *>(timeout)}; 32 } 33 timedOutProcessStatus34 bool timedOut() const { 35 return failure == reinterpret_cast<const char *>(timeout); 36 } getErrorProcessStatus37 const char *getError() const { return timedOut() ? nullptr : failure; } 38 bool exitedNormally() const; 39 int getExitCode() const; 40 int getFatalSignal() const; 41 }; 42 43 ProcessStatus invokeInSubprocess(FunctionCaller *Func, unsigned TimeoutMS = -1); 44 45 const char *signalAsString(int Signum); 46 47 } // namespace testutils 48 } // namespace __llvm_libc 49 50 #endif // LLVM_LIBC_UTILS_TESTUTILS_EXECUTEFUNCTION_H 51