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