Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/Program.cpp
Line
Count
Source
1
//===-- Program.cpp - Implement OS Program Concept --------------*- C++ -*-===//
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
//
9
//  This file implements the operating system Program concept.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/Support/Program.h"
14
#include "llvm/ADT/StringRef.h"
15
#include "llvm/Config/llvm-config.h"
16
#include <system_error>
17
using namespace llvm;
18
using namespace sys;
19
20
//===----------------------------------------------------------------------===//
21
//=== WARNING: Implementation here must contain only TRULY operating system
22
//===          independent code.
23
//===----------------------------------------------------------------------===//
24
25
static bool Execute(ProcessInfo &PI, StringRef Program,
26
                    ArrayRef<StringRef> Args, Optional<ArrayRef<StringRef>> Env,
27
                    ArrayRef<Optional<StringRef>> Redirects,
28
                    unsigned MemoryLimit, std::string *ErrMsg);
29
30
int sys::ExecuteAndWait(StringRef Program, ArrayRef<StringRef> Args,
31
                        Optional<ArrayRef<StringRef>> Env,
32
                        ArrayRef<Optional<StringRef>> Redirects,
33
                        unsigned SecondsToWait, unsigned MemoryLimit,
34
18.3k
                        std::string *ErrMsg, bool *ExecutionFailed) {
35
18.3k
  assert(Redirects.empty() || Redirects.size() == 3);
36
18.3k
  ProcessInfo PI;
37
18.3k
  if (Execute(PI, Program, Args, Env, Redirects, MemoryLimit, ErrMsg)) {
38
18.3k
    if (ExecutionFailed)
39
18.1k
      *ExecutionFailed = false;
40
18.3k
    ProcessInfo Result = Wait(
41
18.3k
        PI, SecondsToWait, /*WaitUntilTerminates=*/SecondsToWait == 0, ErrMsg);
42
18.3k
    return Result.ReturnCode;
43
18.3k
  }
44
2
45
2
  if (ExecutionFailed)
46
2
    *ExecutionFailed = true;
47
2
48
2
  return -1;
49
2
}
50
51
ProcessInfo sys::ExecuteNoWait(StringRef Program, ArrayRef<StringRef> Args,
52
                               Optional<ArrayRef<StringRef>> Env,
53
                               ArrayRef<Optional<StringRef>> Redirects,
54
                               unsigned MemoryLimit, std::string *ErrMsg,
55
3
                               bool *ExecutionFailed) {
56
3
  assert(Redirects.empty() || Redirects.size() == 3);
57
3
  ProcessInfo PI;
58
3
  if (ExecutionFailed)
59
3
    *ExecutionFailed = false;
60
3
  if (!Execute(PI, Program, Args, Env, Redirects, MemoryLimit, ErrMsg))
61
1
    if (ExecutionFailed)
62
1
      *ExecutionFailed = true;
63
3
64
3
  return PI;
65
3
}
66
67
bool sys::commandLineFitsWithinSystemLimits(StringRef Program,
68
17.2k
                                            ArrayRef<const char *> Args) {
69
17.2k
  SmallVector<StringRef, 8> StringRefArgs;
70
17.2k
  StringRefArgs.reserve(Args.size());
71
17.2k
  for (const char *A : Args)
72
1.94M
    StringRefArgs.emplace_back(A);
73
17.2k
  return commandLineFitsWithinSystemLimits(Program, StringRefArgs);
74
17.2k
}
75
76
// Include the platform-specific parts of this class.
77
#ifdef LLVM_ON_UNIX
78
#include "Unix/Program.inc"
79
#endif
80
#ifdef _WIN32
81
#include "Windows/Program.inc"
82
#endif