blob: de71b4c68878f281bb0f3437ee6f36d5dbde1d1a [file] [log] [blame]
Daniel Dunbar00dd4482009-09-24 06:23:57 +00001//===- 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 Silva1af78862014-11-26 22:53:46 +00009// 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 Dunbar00dd4482009-09-24 06:23:57 +000014
Michael J. Spencer447762d2010-11-29 18:16:10 +000015#include "llvm/Support/Program.h"
Dan Gohmanb75ce4f2010-10-29 20:20:29 +000016#include "llvm/Support/raw_ostream.h"
Daniel Dunbar00dd4482009-09-24 06:23:57 +000017using namespace llvm;
18
19int main(int argc, const char **argv) {
Rafael Espindola91487582013-07-05 02:50:03 +000020 bool ExpectCrash = false;
21
22 ++argv;
23 --argc;
24
25 if (argc > 0 && StringRef(argv[0]) == "--crash") {
26 ++argv;
27 --argc;
28 ExpectCrash = true;
29 }
30
31 if (argc == 0)
32 return 1;
33
Michael J. Spencerf9074b52014-11-04 01:29:59 +000034 auto Program = sys::findProgramByName(argv[0]);
35 if (!Program) {
36 errs() << "Error: Unable to find `" << argv[0]
37 << "' in PATH: " << Program.getError().message() << "\n";
38 return 1;
39 }
Dan Gohmanb75ce4f2010-10-29 20:20:29 +000040
41 std::string ErrMsg;
Alexander Kornienko208eecd2017-09-13 17:03:37 +000042 int Result = sys::ExecuteAndWait(*Program, argv, nullptr, {}, 0, 0, &ErrMsg);
NAKAMURA Takumia2e405c2014-06-13 12:23:56 +000043#ifdef _WIN32
Reid Kleckner43776562014-06-23 22:54:33 +000044 // Handle abort() in msvcrt -- It has exit code as 3. abort(), aka
45 // unreachable, should be recognized as a crash. However, some binaries use
46 // exit code 3 on non-crash failure paths, so only do this if we expect a
47 // crash.
48 if (ExpectCrash && Result == 3)
NAKAMURA Takumia2e405c2014-06-13 12:23:56 +000049 Result = -3;
50#endif
Dan Gohmanb75ce4f2010-10-29 20:20:29 +000051 if (Result < 0) {
52 errs() << "Error: " << ErrMsg << "\n";
Rafael Espindola91487582013-07-05 02:50:03 +000053 if (ExpectCrash)
54 return 0;
Dan Gohmanb75ce4f2010-10-29 20:20:29 +000055 return 1;
56 }
57
Rafael Espindola91487582013-07-05 02:50:03 +000058 if (ExpectCrash)
59 return 1;
60
Dan Gohmanb75ce4f2010-10-29 20:20:29 +000061 return Result == 0;
Daniel Dunbar00dd4482009-09-24 06:23:57 +000062}