Daniel Dunbar | 48f7ce8 | 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 | 6487f4b | 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 | 48f7ce8 | 2009-09-24 06:23:57 +0000 | [diff] [blame] | 14 | |
Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Program.h" |
Jonas Devlieghere | 409a350 | 2018-11-09 01:17:22 +0000 | [diff] [blame] | 16 | #include "llvm/Support/WithColor.h" |
Dan Gohman | 9dbb79a | 2010-10-29 20:20:29 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Jonas Devlieghere | 409a350 | 2018-11-09 01:17:22 +0000 | [diff] [blame] | 18 | |
Daniel Dunbar | 48f7ce8 | 2009-09-24 06:23:57 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
| 21 | int main(int argc, const char **argv) { |
Rafael Espindola | a5db79d | 2013-07-05 02:50:03 +0000 | [diff] [blame] | 22 | bool ExpectCrash = false; |
| 23 | |
| 24 | ++argv; |
| 25 | --argc; |
| 26 | |
| 27 | if (argc > 0 && StringRef(argv[0]) == "--crash") { |
| 28 | ++argv; |
| 29 | --argc; |
| 30 | ExpectCrash = true; |
| 31 | } |
| 32 | |
| 33 | if (argc == 0) |
| 34 | return 1; |
| 35 | |
Michael J. Spencer | 58206dd | 2014-11-04 01:29:59 +0000 | [diff] [blame] | 36 | auto Program = sys::findProgramByName(argv[0]); |
| 37 | if (!Program) { |
Jonas Devlieghere | 409a350 | 2018-11-09 01:17:22 +0000 | [diff] [blame] | 38 | WithColor::error() << "unable to find `" << argv[0] |
| 39 | << "' in PATH: " << Program.getError().message() << "\n"; |
Michael J. Spencer | 58206dd | 2014-11-04 01:29:59 +0000 | [diff] [blame] | 40 | return 1; |
| 41 | } |
Dan Gohman | 9dbb79a | 2010-10-29 20:20:29 +0000 | [diff] [blame] | 42 | |
Zachary Turner | 0dcc115 | 2018-06-12 17:43:52 +0000 | [diff] [blame] | 43 | std::vector<StringRef> Argv; |
| 44 | Argv.reserve(argc); |
| 45 | for (int i = 0; i < argc; ++i) |
| 46 | Argv.push_back(argv[i]); |
Dan Gohman | 9dbb79a | 2010-10-29 20:20:29 +0000 | [diff] [blame] | 47 | std::string ErrMsg; |
Zachary Turner | 0dcc115 | 2018-06-12 17:43:52 +0000 | [diff] [blame] | 48 | int Result = sys::ExecuteAndWait(*Program, Argv, None, {}, 0, 0, &ErrMsg); |
NAKAMURA Takumi | 94afbc6 | 2014-06-13 12:23:56 +0000 | [diff] [blame] | 49 | #ifdef _WIN32 |
Reid Kleckner | 71c2411 | 2014-06-23 22:54:33 +0000 | [diff] [blame] | 50 | // Handle abort() in msvcrt -- It has exit code as 3. abort(), aka |
| 51 | // unreachable, should be recognized as a crash. However, some binaries use |
| 52 | // exit code 3 on non-crash failure paths, so only do this if we expect a |
| 53 | // crash. |
| 54 | if (ExpectCrash && Result == 3) |
NAKAMURA Takumi | 94afbc6 | 2014-06-13 12:23:56 +0000 | [diff] [blame] | 55 | Result = -3; |
| 56 | #endif |
Dan Gohman | 9dbb79a | 2010-10-29 20:20:29 +0000 | [diff] [blame] | 57 | if (Result < 0) { |
Jonas Devlieghere | 409a350 | 2018-11-09 01:17:22 +0000 | [diff] [blame] | 58 | WithColor::error() << ErrMsg << "\n"; |
Rafael Espindola | a5db79d | 2013-07-05 02:50:03 +0000 | [diff] [blame] | 59 | if (ExpectCrash) |
| 60 | return 0; |
Dan Gohman | 9dbb79a | 2010-10-29 20:20:29 +0000 | [diff] [blame] | 61 | return 1; |
| 62 | } |
| 63 | |
Rafael Espindola | a5db79d | 2013-07-05 02:50:03 +0000 | [diff] [blame] | 64 | if (ExpectCrash) |
| 65 | return 1; |
| 66 | |
Dan Gohman | 9dbb79a | 2010-10-29 20:20:29 +0000 | [diff] [blame] | 67 | return Result == 0; |
Daniel Dunbar | 48f7ce8 | 2009-09-24 06:23:57 +0000 | [diff] [blame] | 68 | } |