2009-09-24 08:23:57 +02:00
|
|
|
//===- not.cpp - The 'not' testing tool -----------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
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"
|
2010-10-29 22:20:29 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == 0)
|
|
|
|
return 1;
|
|
|
|
|
2014-11-04 02:29:59 +01:00
|
|
|
auto Program = sys::findProgramByName(argv[0]);
|
|
|
|
if (!Program) {
|
|
|
|
errs() << "Error: Unable to find `" << argv[0]
|
|
|
|
<< "' in PATH: " << Program.getError().message() << "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2010-10-29 22:20:29 +02:00
|
|
|
|
|
|
|
std::string ErrMsg;
|
2017-09-13 19:03:37 +02:00
|
|
|
int Result = sys::ExecuteAndWait(*Program, argv, nullptr, {}, 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) {
|
|
|
|
errs() << "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
|
|
|
}
|