1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-22 04:22:57 +02:00
llvm-mirror/utils/not/not.cpp
Zachary Turner 0b859bfff5 Refactor ExecuteAndWait to take StringRefs.
This simplifies some code which had StringRefs to begin with, and
makes other code more complicated which had const char* to begin
with.

In the end, I think this makes for a more idiomatic and platform
agnostic API.  Not all platforms launch process with null terminated
c-string arrays for the environment pointer and argv, but the api
was designed that way because it allowed easy pass-through for
posix-based platforms.  There's a little additional overhead now
since on posix based platforms we'll be takign StringRefs which
were constructed from null terminated strings and then copying
them to null terminate them again, but from a readability and
usability standpoint of the API user, I think this API signature
is strictly better.

llvm-svn: 334518
2018-06-12 17:43:52 +00:00

67 lines
1.7 KiB
C++

//===- not.cpp - The 'not' testing tool -----------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// Usage:
// not cmd
// Will return true if cmd doesn't crash and returns false.
// not --crash cmd
// Will return true if cmd crashes (e.g. for testing crash reporting).
#include "llvm/Support/Program.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
int main(int argc, const char **argv) {
bool ExpectCrash = false;
++argv;
--argc;
if (argc > 0 && StringRef(argv[0]) == "--crash") {
++argv;
--argc;
ExpectCrash = true;
}
if (argc == 0)
return 1;
auto Program = sys::findProgramByName(argv[0]);
if (!Program) {
errs() << "Error: Unable to find `" << argv[0]
<< "' in PATH: " << Program.getError().message() << "\n";
return 1;
}
std::vector<StringRef> Argv;
Argv.reserve(argc);
for (int i = 0; i < argc; ++i)
Argv.push_back(argv[i]);
std::string ErrMsg;
int Result = sys::ExecuteAndWait(*Program, Argv, None, {}, 0, 0, &ErrMsg);
#ifdef _WIN32
// Handle abort() in msvcrt -- It has exit code as 3. abort(), aka
// unreachable, should be recognized as a crash. However, some binaries use
// exit code 3 on non-crash failure paths, so only do this if we expect a
// crash.
if (ExpectCrash && Result == 3)
Result = -3;
#endif
if (Result < 0) {
errs() << "Error: " << ErrMsg << "\n";
if (ExpectCrash)
return 0;
return 1;
}
if (ExpectCrash)
return 1;
return Result == 0;
}