Daniel Dunbar | 00dd448 | 2009-09-24 06:23:57 +0000 | [diff] [blame] | 1 | //===- not.cpp - The 'not' testing tool -----------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 10 | #include "llvm/Support/Path.h" |
| 11 | #include "llvm/Support/Program.h" |
Dan Gohman | b75ce4f | 2010-10-29 20:20:29 +0000 | [diff] [blame] | 12 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 00dd448 | 2009-09-24 06:23:57 +0000 | [diff] [blame] | 13 | using namespace llvm; |
| 14 | |
| 15 | int main(int argc, const char **argv) { |
Rafael Espindola | 9148758 | 2013-07-05 02:50:03 +0000 | [diff] [blame] | 16 | bool ExpectCrash = false; |
| 17 | |
| 18 | ++argv; |
| 19 | --argc; |
| 20 | |
| 21 | if (argc > 0 && StringRef(argv[0]) == "--crash") { |
| 22 | ++argv; |
| 23 | --argc; |
| 24 | ExpectCrash = true; |
| 25 | } |
| 26 | |
| 27 | if (argc == 0) |
| 28 | return 1; |
| 29 | |
Michael J. Spencer | f9074b5 | 2014-11-04 01:29:59 +0000 | [diff] [blame] | 30 | auto Program = sys::findProgramByName(argv[0]); |
| 31 | if (!Program) { |
| 32 | errs() << "Error: Unable to find `" << argv[0] |
| 33 | << "' in PATH: " << Program.getError().message() << "\n"; |
| 34 | return 1; |
| 35 | } |
Dan Gohman | b75ce4f | 2010-10-29 20:20:29 +0000 | [diff] [blame] | 36 | |
| 37 | std::string ErrMsg; |
Michael J. Spencer | f9074b5 | 2014-11-04 01:29:59 +0000 | [diff] [blame] | 38 | int Result = sys::ExecuteAndWait(*Program, argv, nullptr, nullptr, 0, 0, |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 39 | &ErrMsg); |
NAKAMURA Takumi | a2e405c | 2014-06-13 12:23:56 +0000 | [diff] [blame] | 40 | #ifdef _WIN32 |
Reid Kleckner | 4377656 | 2014-06-23 22:54:33 +0000 | [diff] [blame] | 41 | // Handle abort() in msvcrt -- It has exit code as 3. abort(), aka |
| 42 | // unreachable, should be recognized as a crash. However, some binaries use |
| 43 | // exit code 3 on non-crash failure paths, so only do this if we expect a |
| 44 | // crash. |
| 45 | if (ExpectCrash && Result == 3) |
NAKAMURA Takumi | a2e405c | 2014-06-13 12:23:56 +0000 | [diff] [blame] | 46 | Result = -3; |
| 47 | #endif |
Dan Gohman | b75ce4f | 2010-10-29 20:20:29 +0000 | [diff] [blame] | 48 | if (Result < 0) { |
| 49 | errs() << "Error: " << ErrMsg << "\n"; |
Rafael Espindola | 9148758 | 2013-07-05 02:50:03 +0000 | [diff] [blame] | 50 | if (ExpectCrash) |
| 51 | return 0; |
Dan Gohman | b75ce4f | 2010-10-29 20:20:29 +0000 | [diff] [blame] | 52 | return 1; |
| 53 | } |
| 54 | |
Rafael Espindola | 9148758 | 2013-07-05 02:50:03 +0000 | [diff] [blame] | 55 | if (ExpectCrash) |
| 56 | return 1; |
| 57 | |
Dan Gohman | b75ce4f | 2010-10-29 20:20:29 +0000 | [diff] [blame] | 58 | return Result == 0; |
Daniel Dunbar | 00dd448 | 2009-09-24 06:23:57 +0000 | [diff] [blame] | 59 | } |