blob: a5c7183bd2c9b019c07eadd4f5e5abfe80c31d85 [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//===----------------------------------------------------------------------===//
9
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000010#include "llvm/Support/Path.h"
11#include "llvm/Support/Program.h"
Dan Gohman9dbb79a2010-10-29 20:20:29 +000012#include "llvm/Support/raw_ostream.h"
Daniel Dunbar48f7ce82009-09-24 06:23:57 +000013using namespace llvm;
14
15int main(int argc, const char **argv) {
Rafael Espindolaa5db79d2013-07-05 02:50:03 +000016 bool ExpectCrash = false;
17
18 ++argv;
19 --argc;
20
21 if (argc > 0 && StringRef(argv[0]) == "--crash") {
22 ++argv;
23 --argc;
24 ExpectCrash = true;
25 }
26
27 if (argc == 0)
28 return 1;
29
30 std::string Program = sys::FindProgramByName(argv[0]);
Dan Gohman9dbb79a2010-10-29 20:20:29 +000031
32 std::string ErrMsg;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070033 int Result = sys::ExecuteAndWait(Program, argv, nullptr, nullptr, 0, 0,
34 &ErrMsg);
35#ifdef _WIN32
36 // Handle abort() in msvcrt -- It has exit code as 3. abort(), aka
37 // unreachable, should be recognized as a crash. However, some binaries use
38 // exit code 3 on non-crash failure paths, so only do this if we expect a
39 // crash.
40 if (ExpectCrash && Result == 3)
41 Result = -3;
42#endif
Dan Gohman9dbb79a2010-10-29 20:20:29 +000043 if (Result < 0) {
44 errs() << "Error: " << ErrMsg << "\n";
Rafael Espindolaa5db79d2013-07-05 02:50:03 +000045 if (ExpectCrash)
46 return 0;
Dan Gohman9dbb79a2010-10-29 20:20:29 +000047 return 1;
48 }
49
Rafael Espindolaa5db79d2013-07-05 02:50:03 +000050 if (ExpectCrash)
51 return 1;
52
Dan Gohman9dbb79a2010-10-29 20:20:29 +000053 return Result == 0;
Daniel Dunbar48f7ce82009-09-24 06:23:57 +000054}