]> git.proxmox.com Git - rustc.git/blame - src/llvm/utils/not/not.cpp
Imported Upstream version 1.0.0+dfsg1
[rustc.git] / src / llvm / utils / not / not.cpp
CommitLineData
223e47cc
LB
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//===----------------------------------------------------------------------===//
85aaf69f
SL
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).
223e47cc
LB
14
15#include "llvm/Support/Path.h"
16#include "llvm/Support/Program.h"
17#include "llvm/Support/raw_ostream.h"
18using namespace llvm;
19
20int main(int argc, const char **argv) {
1a4d82fc
JJ
21 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
85aaf69f
SL
35 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 }
223e47cc
LB
41
42 std::string ErrMsg;
85aaf69f 43 int Result = sys::ExecuteAndWait(*Program, argv, nullptr, nullptr, 0, 0,
1a4d82fc
JJ
44 &ErrMsg);
45#ifdef _WIN32
46 // 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)
51 Result = -3;
52#endif
223e47cc
LB
53 if (Result < 0) {
54 errs() << "Error: " << ErrMsg << "\n";
1a4d82fc
JJ
55 if (ExpectCrash)
56 return 0;
223e47cc
LB
57 return 1;
58 }
59
1a4d82fc
JJ
60 if (ExpectCrash)
61 return 1;
62
223e47cc
LB
63 return Result == 0;
64}