2009-09-24 08:23:57 +02:00
|
|
|
//===- not.cpp - The 'not' testing tool -----------------------------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-09-24 08:23:57 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2014-11-26 23:53:46 +01:00
|
|
|
// Usage:
|
|
|
|
// not cmd
|
|
|
|
// Will return true if cmd doesn't crash and returns false.
|
|
|
|
// not --crash cmd
|
|
|
|
// Will return true if cmd crashes (e.g. for testing crash reporting).
|
2009-09-24 08:23:57 +02:00
|
|
|
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/Program.h"
|
2018-11-09 02:17:22 +01:00
|
|
|
#include "llvm/Support/WithColor.h"
|
2010-10-29 22:20:29 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-11-09 02:17:22 +01:00
|
|
|
|
2020-08-20 17:14:10 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2009-09-24 08:23:57 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
int main(int argc, const char **argv) {
|
2013-07-05 04:50:03 +02:00
|
|
|
bool ExpectCrash = false;
|
|
|
|
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
|
|
|
|
if (argc > 0 && StringRef(argv[0]) == "--crash") {
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
ExpectCrash = true;
|
2020-08-20 17:14:10 +02:00
|
|
|
|
|
|
|
// Crash is expected, so disable crash report and symbolization to reduce
|
|
|
|
// output and avoid potentially slow symbolization.
|
|
|
|
#ifdef _WIN32
|
|
|
|
SetEnvironmentVariableA("LLVM_DISABLE_CRASH_REPORT", "1");
|
|
|
|
SetEnvironmentVariableA("LLVM_DISABLE_SYMBOLIZATION", "1");
|
|
|
|
#else
|
|
|
|
setenv("LLVM_DISABLE_CRASH_REPORT", "1", 0);
|
|
|
|
setenv("LLVM_DISABLE_SYMBOLIZATION", "1", 0);
|
|
|
|
#endif
|
2013-07-05 04:50:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
return 1;
|
|
|
|
|
2014-11-04 02:29:59 +01:00
|
|
|
auto Program = sys::findProgramByName(argv[0]);
|
|
|
|
if (!Program) {
|
2018-11-09 02:17:22 +01:00
|
|
|
WithColor::error() << "unable to find `" << argv[0]
|
|
|
|
<< "' in PATH: " << Program.getError().message() << "\n";
|
2014-11-04 02:29:59 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2010-10-29 22:20:29 +02:00
|
|
|
|
2018-06-12 19:43:52 +02:00
|
|
|
std::vector<StringRef> Argv;
|
|
|
|
Argv.reserve(argc);
|
|
|
|
for (int i = 0; i < argc; ++i)
|
|
|
|
Argv.push_back(argv[i]);
|
2010-10-29 22:20:29 +02:00
|
|
|
std::string ErrMsg;
|
2018-06-12 19:43:52 +02:00
|
|
|
int Result = sys::ExecuteAndWait(*Program, Argv, None, {}, 0, 0, &ErrMsg);
|
2014-06-13 14:23:56 +02:00
|
|
|
#ifdef _WIN32
|
2014-06-24 00:54:33 +02:00
|
|
|
// Handle abort() in msvcrt -- It has exit code as 3. abort(), aka
|
|
|
|
// unreachable, should be recognized as a crash. However, some binaries use
|
|
|
|
// exit code 3 on non-crash failure paths, so only do this if we expect a
|
|
|
|
// crash.
|
|
|
|
if (ExpectCrash && Result == 3)
|
2014-06-13 14:23:56 +02:00
|
|
|
Result = -3;
|
|
|
|
#endif
|
2010-10-29 22:20:29 +02:00
|
|
|
if (Result < 0) {
|
2018-11-09 02:17:22 +01:00
|
|
|
WithColor::error() << ErrMsg << "\n";
|
2013-07-05 04:50:03 +02:00
|
|
|
if (ExpectCrash)
|
|
|
|
return 0;
|
2010-10-29 22:20:29 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-05 04:50:03 +02:00
|
|
|
if (ExpectCrash)
|
|
|
|
return 1;
|
|
|
|
|
2010-10-29 22:20:29 +02:00
|
|
|
return Result == 0;
|
2009-09-24 08:23:57 +02:00
|
|
|
}
|