|
| 1 | +//===- not.cpp - The 'not' testing tool -----------------------------------===// |
| 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 | +// Usage: |
| 9 | +// not cmd |
| 10 | +// Will return true if cmd doesn't crash and returns false. |
| 11 | +// not --crash cmd |
| 12 | +// Will return true if cmd crashes (e.g. for testing crash reporting). |
| 13 | + |
| 14 | +// This file is a stripped down version of not.cpp from llvm/utils. This does |
| 15 | +// not depend on any LLVM library. |
| 16 | + |
| 17 | +#include <cstdlib> |
| 18 | +#include <iostream> |
| 19 | +#include <sstream> |
| 20 | + |
| 21 | +#ifdef _WIN32 |
| 22 | +#include <windows.h> |
| 23 | +#endif |
| 24 | + |
| 25 | +int main(int argc, const char **argv) { |
| 26 | + bool expectCrash = false; |
| 27 | + |
| 28 | + ++argv; |
| 29 | + --argc; |
| 30 | + |
| 31 | + if (argc > 0 && std::string(argv[0]) == "--crash") { |
| 32 | + ++argv; |
| 33 | + --argc; |
| 34 | + expectCrash = true; |
| 35 | + |
| 36 | + // Crash is expected, so disable crash report and symbolization to reduce |
| 37 | + // output and avoid potentially slow symbolization. |
| 38 | +#ifdef _WIN32 |
| 39 | + SetEnvironmentVariableA("LLVM_DISABLE_CRASH_REPORT", "1"); |
| 40 | + SetEnvironmentVariableA("LLVM_DISABLE_SYMBOLIZATION", "1"); |
| 41 | +#else |
| 42 | + setenv("LLVM_DISABLE_CRASH_REPORT", "1", 0); |
| 43 | + setenv("LLVM_DISABLE_SYMBOLIZATION", "1", 0); |
| 44 | +#endif |
| 45 | + } |
| 46 | + |
| 47 | + if (argc == 0) |
| 48 | + return 1; |
| 49 | + |
| 50 | + std::stringstream ss; |
| 51 | + ss << argv[0]; |
| 52 | + for (int i = 1; i < argc; ++i) |
| 53 | + ss << " " << argv[i]; |
| 54 | + std::string cmd = ss.str(); |
| 55 | + int result = system(cmd.c_str()); |
| 56 | + |
| 57 | +#ifdef _WIN32 |
| 58 | + // Handle abort() in msvcrt -- It has exit code as 3. abort(), aka |
| 59 | + // unreachable, should be recognized as a crash. However, some binaries use |
| 60 | + // exit code 3 on non-crash failure paths, so only do this if we expect a |
| 61 | + // crash. |
| 62 | + if (expectCrash && result == 3) |
| 63 | + result = -3; |
| 64 | +#endif |
| 65 | + |
| 66 | + if (result < 0) { |
| 67 | + if (expectCrash) |
| 68 | + return 0; |
| 69 | + return 1; |
| 70 | + } |
| 71 | + |
| 72 | + if (expectCrash) |
| 73 | + return 1; |
| 74 | + |
| 75 | + return result == 0; |
| 76 | +} |
0 commit comments