LLVM  14.0.0git
Process.cpp
Go to the documentation of this file.
1 //===-- Process.cpp - Implement OS Process 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 Process concept.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Support/Process.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/Config/llvm-config.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/Program.h"
22 
23 #include <stdlib.h> // for _Exit
24 
25 using namespace llvm;
26 using namespace sys;
27 
28 //===----------------------------------------------------------------------===//
29 //=== WARNING: Implementation here must contain only TRULY operating system
30 //=== independent code.
31 //===----------------------------------------------------------------------===//
32 
34 Process::FindInEnvPath(StringRef EnvName, StringRef FileName, char Separator) {
35  return FindInEnvPath(EnvName, FileName, {}, Separator);
36 }
37 
39  StringRef FileName,
40  ArrayRef<std::string> IgnoreList,
41  char Separator) {
42  assert(!path::is_absolute(FileName));
43  Optional<std::string> FoundPath;
44  Optional<std::string> OptPath = Process::GetEnv(EnvName);
45  if (!OptPath.hasValue())
46  return FoundPath;
47 
48  const char EnvPathSeparatorStr[] = {Separator, '\0'};
50  SplitString(OptPath.getValue(), Dirs, EnvPathSeparatorStr);
51 
52  for (StringRef Dir : Dirs) {
53  if (Dir.empty())
54  continue;
55 
56  if (any_of(IgnoreList, [&](StringRef S) { return fs::equivalent(S, Dir); }))
57  continue;
58 
59  SmallString<128> FilePath(Dir);
60  path::append(FilePath, FileName);
61  if (fs::exists(Twine(FilePath))) {
62  FoundPath = std::string(FilePath.str());
63  break;
64  }
65  }
66 
67  return FoundPath;
68 }
69 
70 
71 #define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m"
72 
73 #define ALLCOLORS(FGBG,BOLD) {\
74  COLOR(FGBG, "0", BOLD),\
75  COLOR(FGBG, "1", BOLD),\
76  COLOR(FGBG, "2", BOLD),\
77  COLOR(FGBG, "3", BOLD),\
78  COLOR(FGBG, "4", BOLD),\
79  COLOR(FGBG, "5", BOLD),\
80  COLOR(FGBG, "6", BOLD),\
81  COLOR(FGBG, "7", BOLD)\
82  }
83 
84 static const char colorcodes[2][2][8][10] = {
85  { ALLCOLORS("3",""), ALLCOLORS("3","1;") },
86  { ALLCOLORS("4",""), ALLCOLORS("4","1;") }
87 };
88 
89 // A CMake option controls wheter we emit core dumps by default. An application
90 // may disable core dumps by calling Process::PreventCoreFiles().
91 static bool coreFilesPrevented = !LLVM_ENABLE_CRASH_DUMPS;
92 
94 
95 [[noreturn]] void Process::Exit(int RetCode, bool NoCleanup) {
97  CRC->HandleExit(RetCode);
98 
99  if (NoCleanup)
100  ExitNoCleanup(RetCode);
101  else
102  ::exit(RetCode);
103 }
104 
105 // Include the platform-specific parts of this class.
106 #ifdef LLVM_ON_UNIX
107 #include "Unix/Process.inc"
108 #endif
109 #ifdef _WIN32
110 #include "Windows/Process.inc"
111 #endif
ALLCOLORS
#define ALLCOLORS(FGBG, BOLD)
Definition: Process.cpp:73
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
FileSystem.h
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
Path.h
llvm::sys::path::is_absolute
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition: Path.cpp:669
llvm::Optional< std::string >
STLExtras.h
llvm::Optional::hasValue
constexpr bool hasValue() const
Definition: Optional.h:288
llvm::sys::path::append
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:454
llvm::sys::fs::equivalent
bool equivalent(file_status A, file_status B)
Do file_status's represent the same thing?
Process.h
colorcodes
static const char colorcodes[2][2][8][10]
Definition: Process.cpp:84
llvm::SmallString< 128 >
llvm::sys::fs::exists
bool exists(const basic_file_status &status)
Does file exist?
Definition: Path.cpp:1073
StringExtras.h
Process.inc
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::sys::Process::GetEnv
static Optional< std::string > GetEnv(StringRef name)
llvm::CrashRecoveryContext::GetCurrent
static CrashRecoveryContext * GetCurrent()
Return the active context, if the code is currently executing in a thread which is in a protected con...
Definition: CrashRecoveryContext.cpp:128
llvm::sys::Process::AreCoreFilesPrevented
static bool AreCoreFilesPrevented()
true if PreventCoreFiles has been called, false otherwise.
Definition: Process.cpp:93
llvm::ArrayRef< std::string >
llvm::any_of
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1558
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::sys::Process::Exit
static void Exit(int RetCode, bool NoCleanup=false)
Equivalent to exit(), except when running inside a CrashRecoveryContext.
Definition: Process.cpp:95
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::SmallString::str
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:259
CrashRecoveryContext.h
exit
declare void exit(i32) noreturn nounwind This compiles into
Definition: README.txt:1072
Program.h
coreFilesPrevented
static bool coreFilesPrevented
Definition: Process.cpp:91
llvm::CrashRecoveryContext
Crash recovery helper object.
Definition: CrashRecoveryContext.h:46
Process.inc
llvm::sys::Process::FindInEnvPath
static Optional< std::string > FindInEnvPath(StringRef EnvName, StringRef FileName, ArrayRef< std::string > IgnoreList, char Separator=EnvPathSeparator)
This function searches for an existing file in the list of directories in a PATH like environment var...
Definition: Process.cpp:38
llvm::Optional::getValue
constexpr const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:282