xref: /llvm-project-15.0.7/llvm/utils/not/not.cpp (revision 052d95a6)
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 // 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).
14 
15 #include "llvm/Support/Program.h"
16 #include "llvm/Support/raw_ostream.h"
17 using namespace llvm;
18 
19 int main(int argc, const char **argv) {
20   bool ExpectCrash = false;
21 
22   ++argv;
23   --argc;
24 
25   if (argc > 0 && StringRef(argv[0]) == "--crash") {
26     ++argv;
27     --argc;
28     ExpectCrash = true;
29   }
30 
31   if (argc == 0)
32     return 1;
33 
34   auto Program = sys::findProgramByName(argv[0]);
35   if (!Program) {
36     errs() << "Error: Unable to find `" << argv[0]
37            << "' in PATH: " << Program.getError().message() << "\n";
38     return 1;
39   }
40 
41   std::string ErrMsg;
42   int Result = sys::ExecuteAndWait(*Program, argv, nullptr, nullptr, 0, 0,
43                                    &ErrMsg);
44 #ifdef _WIN32
45   // Handle abort() in msvcrt -- It has exit code as 3.  abort(), aka
46   // unreachable, should be recognized as a crash.  However, some binaries use
47   // exit code 3 on non-crash failure paths, so only do this if we expect a
48   // crash.
49   if (ExpectCrash && Result == 3)
50     Result = -3;
51 #endif
52   if (Result < 0) {
53     errs() << "Error: " << ErrMsg << "\n";
54     if (ExpectCrash)
55       return 0;
56     return 1;
57   }
58 
59   if (ExpectCrash)
60     return 1;
61 
62   return Result == 0;
63 }
64