LLVM  14.0.0git
Program.cpp
Go to the documentation of this file.
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"
17 #include <system_error>
18 using namespace llvm;
19 using namespace sys;
20 
21 //===----------------------------------------------------------------------===//
22 //=== WARNING: Implementation here must contain only TRULY operating system
23 //=== independent code.
24 //===----------------------------------------------------------------------===//
25 
26 static bool Execute(ProcessInfo &PI, StringRef Program,
28  ArrayRef<Optional<StringRef>> Redirects,
29  unsigned MemoryLimit, std::string *ErrMsg,
30  BitVector *AffinityMask);
31 
34  ArrayRef<Optional<StringRef>> Redirects,
35  unsigned SecondsToWait, unsigned MemoryLimit,
36  std::string *ErrMsg, bool *ExecutionFailed,
38  BitVector *AffinityMask) {
39  assert(Redirects.empty() || Redirects.size() == 3);
40  ProcessInfo PI;
41  if (Execute(PI, Program, Args, Env, Redirects, MemoryLimit, ErrMsg,
42  AffinityMask)) {
43  if (ExecutionFailed)
44  *ExecutionFailed = false;
45  ProcessInfo Result =
46  Wait(PI, SecondsToWait, /*WaitUntilTerminates=*/SecondsToWait == 0,
47  ErrMsg, ProcStat);
48  return Result.ReturnCode;
49  }
50 
51  if (ExecutionFailed)
52  *ExecutionFailed = true;
53 
54  return -1;
55 }
56 
59  ArrayRef<Optional<StringRef>> Redirects,
60  unsigned MemoryLimit, std::string *ErrMsg,
61  bool *ExecutionFailed, BitVector *AffinityMask) {
62  assert(Redirects.empty() || Redirects.size() == 3);
63  ProcessInfo PI;
64  if (ExecutionFailed)
65  *ExecutionFailed = false;
66  if (!Execute(PI, Program, Args, Env, Redirects, MemoryLimit, ErrMsg,
67  AffinityMask))
68  if (ExecutionFailed)
69  *ExecutionFailed = true;
70 
71  return PI;
72 }
73 
76  SmallVector<StringRef, 8> StringRefArgs;
77  StringRefArgs.reserve(Args.size());
78  for (const char *A : Args)
79  StringRefArgs.emplace_back(A);
80  return commandLineFitsWithinSystemLimits(Program, StringRefArgs);
81 }
82 
83 void sys::printArg(raw_ostream &OS, StringRef Arg, bool Quote) {
84  const bool Escape = Arg.find_first_of(" \"\\$") != StringRef::npos;
85 
86  if (!Quote && !Escape) {
87  OS << Arg;
88  return;
89  }
90 
91  // Quote and escape. This isn't really complete, but good enough.
92  OS << '"';
93  for (const auto c : Arg) {
94  if (c == '"' || c == '\\' || c == '$')
95  OS << '\\';
96  OS << c;
97  }
98  OS << '"';
99 }
100 
101 // Include the platform-specific parts of this class.
102 #ifdef LLVM_ON_UNIX
103 #include "Unix/Program.inc"
104 #endif
105 #ifdef _WIN32
106 #include "Windows/Program.inc"
107 #endif
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
Program.inc
llvm::sys::ExecuteNoWait
ProcessInfo ExecuteNoWait(StringRef Program, ArrayRef< StringRef > Args, Optional< ArrayRef< StringRef >> Env, ArrayRef< Optional< StringRef >> Redirects={}, unsigned MemoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr, BitVector *AffinityMask=nullptr)
Similar to ExecuteAndWait, but returns immediately.
Definition: Program.cpp:57
StringRef.h
llvm::StringRef::npos
static constexpr size_t npos
Definition: StringRef.h:60
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::Optional
Definition: APInt.h:33
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:206
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::BitVector
Definition: BitVector.h:74
c
the resulting code requires compare and branches when and if the revised code is with conditional branches instead of More there is a byte word extend before each where there should be only and the condition codes are not remembered when the same two values are compared twice More LSR enhancements i8 and i32 load store addressing modes are identical int int c
Definition: README.txt:418
Program.inc
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
A
* A
Definition: README_ALTIVEC.txt:89
Execute
static bool Execute(ProcessInfo &PI, StringRef Program, ArrayRef< StringRef > Args, Optional< ArrayRef< StringRef >> Env, ArrayRef< Optional< StringRef >> Redirects, unsigned MemoryLimit, std::string *ErrMsg, BitVector *AffinityMask)
llvm::sys::ProcessInfo
This struct encapsulates information about a process.
Definition: Program.h:46
Program.h
llvm::sys::ExecuteAndWait
int ExecuteAndWait(StringRef Program, ArrayRef< StringRef > Args, Optional< ArrayRef< StringRef >> Env=None, ArrayRef< Optional< StringRef >> Redirects={}, unsigned SecondsToWait=0, unsigned MemoryLimit=0, std::string *ErrMsg=nullptr, bool *ExecutionFailed=nullptr, Optional< ProcessStatistics > *ProcStat=nullptr, BitVector *AffinityMask=nullptr)
This function executes the program using the arguments provided.
Definition: Program.cpp:32
llvm::sys::Wait
ProcessInfo Wait(const ProcessInfo &PI, unsigned SecondsToWait, bool WaitUntilTerminates, std::string *ErrMsg=nullptr, Optional< ProcessStatistics > *ProcStat=nullptr)
This function waits for the process specified by PI to finish.
llvm::sys::printArg
void printArg(llvm::raw_ostream &OS, StringRef Arg, bool Quote)
Print a command argument, and optionally quote it.
Definition: Program.cpp:83
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:389
raw_ostream.h
llvm::sys::commandLineFitsWithinSystemLimits
bool commandLineFitsWithinSystemLimits(StringRef Program, ArrayRef< StringRef > Args)
Return true if the given arguments fit within system-specific argument length limits.
llvm::SmallVectorImpl::reserve
void reserve(size_type N)
Definition: SmallVector.h:624
llvm::SmallVectorImpl::emplace_back
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:908