1 //===- not.cpp - The 'not' testing tool -----------------------------------===//
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 //===----------------------------------------------------------------------===//
10 // Will return true if cmd doesn't crash and returns false.
12 // Will return true if cmd crashes (e.g. for testing crash reporting).
14 #include "llvm/Support/Process.h"
15 #include "llvm/Support/Program.h"
16 #include "llvm/Support/WithColor.h"
17 #include "llvm/Support/raw_ostream.h"
25 int main(int argc
, const char **argv
) {
26 bool ExpectCrash
= false;
31 if (argc
> 0 && StringRef(argv
[0]) == "--crash") {
36 // Crash is expected, so disable crash report and symbolization to reduce
37 // output and avoid potentially slow symbolization.
39 SetEnvironmentVariableA("LLVM_DISABLE_CRASH_REPORT", "1");
40 SetEnvironmentVariableA("LLVM_DISABLE_SYMBOLIZATION", "1");
42 setenv("LLVM_DISABLE_CRASH_REPORT", "1", 0);
43 setenv("LLVM_DISABLE_SYMBOLIZATION", "1", 0);
45 // Try to disable coredumps for expected crashes as well since this can
46 // noticeably slow down running the test suite.
47 sys::Process::PreventCoreFiles();
53 auto Program
= sys::findProgramByName(argv
[0]);
55 WithColor::error() << "unable to find `" << argv
[0]
56 << "' in PATH: " << Program
.getError().message() << "\n";
60 std::vector
<StringRef
> Argv
;
62 for (int i
= 0; i
< argc
; ++i
)
63 Argv
.push_back(argv
[i
]);
66 sys::ExecuteAndWait(*Program
, Argv
, std::nullopt
, {}, 0, 0, &ErrMsg
);
68 // Handle abort() in msvcrt -- It has exit code as 3. abort(), aka
69 // unreachable, should be recognized as a crash. However, some binaries use
70 // exit code 3 on non-crash failure paths, so only do this if we expect a
72 if (ExpectCrash
&& Result
== 3)
76 WithColor::error() << ErrMsg
<< "\n";