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//===----------------------------------------------------------------------===//
9
10#include "llvm/Support/Path.h"
11#include "llvm/Support/Program.h"
12#include "llvm/Support/raw_ostream.h"
13using namespace llvm;
14
15int main(int argc, const char **argv) {
16  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]);
31
32  std::string ErrMsg;
33  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
43  if (Result < 0) {
44    errs() << "Error: " << ErrMsg << "\n";
45    if (ExpectCrash)
46      return 0;
47    return 1;
48  }
49
50  if (ExpectCrash)
51    return 1;
52
53  return Result == 0;
54}
55