blob: 23062fb323d7fdef7f10591bfa83f82508a54376 [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/Path.h"
16#include "llvm/Support/Program.h"
Dan Gohmanb75ce4f2010-10-29 20:20:29 +000017#include "llvm/Support/raw_ostream.h"
Daniel Dunbar00dd4482009-09-24 06:23:57 +000018using namespace llvm;
19
20int main(int argc, const char **argv) {
Rafael Espindola91487582013-07-05 02:50:03 +000021 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. Spencerf9074b52014-11-04 01:29:59 +000035 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 Gohmanb75ce4f2010-10-29 20:20:29 +000041
42 std::string ErrMsg;
Michael J. Spencerf9074b52014-11-04 01:29:59 +000043 int Result = sys::ExecuteAndWait(*Program, argv, nullptr, nullptr, 0, 0,
Craig Topper66f09ad2014-06-08 22:29:17 +000044 &ErrMsg);
NAKAMURA Takumia2e405c2014-06-13 12:23:56 +000045#ifdef _WIN32
Reid Kleckner43776562014-06-23 22:54:33 +000046 // 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 Takumia2e405c2014-06-13 12:23:56 +000051 Result = -3;
52#endif
Dan Gohmanb75ce4f2010-10-29 20:20:29 +000053 if (Result < 0) {
54 errs() << "Error: " << ErrMsg << "\n";
Rafael Espindola91487582013-07-05 02:50:03 +000055 if (ExpectCrash)
56 return 0;
Dan Gohmanb75ce4f2010-10-29 20:20:29 +000057 return 1;
58 }
59
Rafael Espindola91487582013-07-05 02:50:03 +000060 if (ExpectCrash)
61 return 1;
62
Dan Gohmanb75ce4f2010-10-29 20:20:29 +000063 return Result == 0;
Daniel Dunbar00dd4482009-09-24 06:23:57 +000064}