blob: e35c884944ce645c9f0617de0671c5f3ceec1c3d [file] [log] [blame]
Daniel Dunbar48f7ce82009-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 Silva6487f4b2014-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 Dunbar48f7ce82009-09-24 06:23:57 +000014
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000015#include "llvm/Support/Program.h"
Jonas Devlieghere409a3502018-11-09 01:17:22 +000016#include "llvm/Support/WithColor.h"
Dan Gohman9dbb79a2010-10-29 20:20:29 +000017#include "llvm/Support/raw_ostream.h"
Jonas Devlieghere409a3502018-11-09 01:17:22 +000018
Daniel Dunbar48f7ce82009-09-24 06:23:57 +000019using namespace llvm;
20
21int main(int argc, const char **argv) {
Rafael Espindolaa5db79d2013-07-05 02:50:03 +000022 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. Spencer58206dd2014-11-04 01:29:59 +000036 auto Program = sys::findProgramByName(argv[0]);
37 if (!Program) {
Jonas Devlieghere409a3502018-11-09 01:17:22 +000038 WithColor::error() << "unable to find `" << argv[0]
39 << "' in PATH: " << Program.getError().message() << "\n";
Michael J. Spencer58206dd2014-11-04 01:29:59 +000040 return 1;
41 }
Dan Gohman9dbb79a2010-10-29 20:20:29 +000042
Zachary Turner0dcc1152018-06-12 17:43:52 +000043 std::vector<StringRef> Argv;
44 Argv.reserve(argc);
45 for (int i = 0; i < argc; ++i)
46 Argv.push_back(argv[i]);
Dan Gohman9dbb79a2010-10-29 20:20:29 +000047 std::string ErrMsg;
Zachary Turner0dcc1152018-06-12 17:43:52 +000048 int Result = sys::ExecuteAndWait(*Program, Argv, None, {}, 0, 0, &ErrMsg);
NAKAMURA Takumi94afbc62014-06-13 12:23:56 +000049#ifdef _WIN32
Reid Kleckner71c24112014-06-23 22:54:33 +000050 // 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 Takumi94afbc62014-06-13 12:23:56 +000055 Result = -3;
56#endif
Dan Gohman9dbb79a2010-10-29 20:20:29 +000057 if (Result < 0) {
Jonas Devlieghere409a3502018-11-09 01:17:22 +000058 WithColor::error() << ErrMsg << "\n";
Rafael Espindolaa5db79d2013-07-05 02:50:03 +000059 if (ExpectCrash)
60 return 0;
Dan Gohman9dbb79a2010-10-29 20:20:29 +000061 return 1;
62 }
63
Rafael Espindolaa5db79d2013-07-05 02:50:03 +000064 if (ExpectCrash)
65 return 1;
66
Dan Gohman9dbb79a2010-10-29 20:20:29 +000067 return Result == 0;
Daniel Dunbar48f7ce82009-09-24 06:23:57 +000068}