2009-09-24 14:23:57 +08:00
|
|
|
//===- not.cpp - The 'not' testing tool -----------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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 14:23:57 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2014-11-27 06:53:46 +08: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 14:23:57 +08:00
|
|
|
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Program.h"
|
2018-11-09 09:17:22 +08:00
|
|
|
#include "llvm/Support/WithColor.h"
|
2010-10-30 04:20:29 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-11-09 09:17:22 +08:00
|
|
|
|
2009-09-24 14:23:57 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
int main(int argc, const char **argv) {
|
2013-07-05 10:50:03 +08:00
|
|
|
bool ExpectCrash = false;
|
|
|
|
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
|
|
|
|
if (argc > 0 && StringRef(argv[0]) == "--crash") {
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
ExpectCrash = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
return 1;
|
|
|
|
|
2014-11-04 09:29:59 +08:00
|
|
|
auto Program = sys::findProgramByName(argv[0]);
|
|
|
|
if (!Program) {
|
2018-11-09 09:17:22 +08:00
|
|
|
WithColor::error() << "unable to find `" << argv[0]
|
|
|
|
<< "' in PATH: " << Program.getError().message() << "\n";
|
2014-11-04 09:29:59 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2010-10-30 04:20:29 +08:00
|
|
|
|
2018-06-13 01:43:52 +08:00
|
|
|
std::vector<StringRef> Argv;
|
|
|
|
Argv.reserve(argc);
|
|
|
|
for (int i = 0; i < argc; ++i)
|
|
|
|
Argv.push_back(argv[i]);
|
2010-10-30 04:20:29 +08:00
|
|
|
std::string ErrMsg;
|
2018-06-13 01:43:52 +08:00
|
|
|
int Result = sys::ExecuteAndWait(*Program, Argv, None, {}, 0, 0, &ErrMsg);
|
2014-06-13 20:23:56 +08:00
|
|
|
#ifdef _WIN32
|
2014-06-24 06:54:33 +08: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 20:23:56 +08:00
|
|
|
Result = -3;
|
|
|
|
#endif
|
2010-10-30 04:20:29 +08:00
|
|
|
if (Result < 0) {
|
2018-11-09 09:17:22 +08:00
|
|
|
WithColor::error() << ErrMsg << "\n";
|
2013-07-05 10:50:03 +08:00
|
|
|
if (ExpectCrash)
|
|
|
|
return 0;
|
2010-10-30 04:20:29 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-05 10:50:03 +08:00
|
|
|
if (ExpectCrash)
|
|
|
|
return 1;
|
|
|
|
|
2010-10-30 04:20:29 +08:00
|
|
|
return Result == 0;
|
2009-09-24 14:23:57 +08:00
|
|
|
}
|