[InstCombine] Signed saturation patterns
[llvm-complete.git] / utils / not / not.cpp
blobf00b9c8dde42b7081e2e130baaf522ceab2c9202
1 //===- not.cpp - The 'not' testing tool -----------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 // Usage:
9 // not cmd
10 // Will return true if cmd doesn't crash and returns false.
11 // not --crash cmd
12 // Will return true if cmd crashes (e.g. for testing crash reporting).
14 #include "llvm/Support/Program.h"
15 #include "llvm/Support/WithColor.h"
16 #include "llvm/Support/raw_ostream.h"
18 using namespace llvm;
20 int main(int argc, const char **argv) {
21 bool ExpectCrash = false;
23 ++argv;
24 --argc;
26 if (argc > 0 && StringRef(argv[0]) == "--crash") {
27 ++argv;
28 --argc;
29 ExpectCrash = true;
32 if (argc == 0)
33 return 1;
35 auto Program = sys::findProgramByName(argv[0]);
36 if (!Program) {
37 WithColor::error() << "unable to find `" << argv[0]
38 << "' in PATH: " << Program.getError().message() << "\n";
39 return 1;
42 std::vector<StringRef> Argv;
43 Argv.reserve(argc);
44 for (int i = 0; i < argc; ++i)
45 Argv.push_back(argv[i]);
46 std::string ErrMsg;
47 int Result = sys::ExecuteAndWait(*Program, Argv, None, {}, 0, 0, &ErrMsg);
48 #ifdef _WIN32
49 // Handle abort() in msvcrt -- It has exit code as 3. abort(), aka
50 // unreachable, should be recognized as a crash. However, some binaries use
51 // exit code 3 on non-crash failure paths, so only do this if we expect a
52 // crash.
53 if (ExpectCrash && Result == 3)
54 Result = -3;
55 #endif
56 if (Result < 0) {
57 WithColor::error() << ErrMsg << "\n";
58 if (ExpectCrash)
59 return 0;
60 return 1;
63 if (ExpectCrash)
64 return 1;
66 return Result == 0;