LLVM  12.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 using namespace llvm;
24 using namespace sys;
25 
26 //===----------------------------------------------------------------------===//
27 //=== WARNING: Implementation here must contain only TRULY operating system
28 //=== independent code.
29 //===----------------------------------------------------------------------===//
30 
32  StringRef FileName) {
33  return FindInEnvPath(EnvName, FileName, {});
34 }
35 
37  StringRef FileName,
38  ArrayRef<std::string> IgnoreList) {
39  assert(!path::is_absolute(FileName));
40  Optional<std::string> FoundPath;
41  Optional<std::string> OptPath = Process::GetEnv(EnvName);
42  if (!OptPath.hasValue())
43  return FoundPath;
44 
45  const char EnvPathSeparatorStr[] = {EnvPathSeparator, '\0'};
47  SplitString(OptPath.getValue(), Dirs, EnvPathSeparatorStr);
48 
49  for (StringRef Dir : Dirs) {
50  if (Dir.empty())
51  continue;
52 
53  if (any_of(IgnoreList, [&](StringRef S) { return fs::equivalent(S, Dir); }))
54  continue;
55 
56  SmallString<128> FilePath(Dir);
57  path::append(FilePath, FileName);
58  if (fs::exists(Twine(FilePath))) {
59  FoundPath = std::string(FilePath.str());
60  break;
61  }
62  }
63 
64  return FoundPath;
65 }
66 
67 
68 #define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m"
69 
70 #define ALLCOLORS(FGBG,BOLD) {\
71  COLOR(FGBG, "0", BOLD),\
72  COLOR(FGBG, "1", BOLD),\
73  COLOR(FGBG, "2", BOLD),\
74  COLOR(FGBG, "3", BOLD),\
75  COLOR(FGBG, "4", BOLD),\
76  COLOR(FGBG, "5", BOLD),\
77  COLOR(FGBG, "6", BOLD),\
78  COLOR(FGBG, "7", BOLD)\
79  }
80 
81 static const char colorcodes[2][2][8][10] = {
82  { ALLCOLORS("3",""), ALLCOLORS("3","1;") },
83  { ALLCOLORS("4",""), ALLCOLORS("4","1;") }
84 };
85 
86 // A CMake option controls wheter we emit core dumps by default. An application
87 // may disable core dumps by calling Process::PreventCoreFiles().
88 static bool coreFilesPrevented = !LLVM_ENABLE_CRASH_DUMPS;
89 
91 
93 void Process::Exit(int RetCode) {
95  CRC->HandleExit(RetCode);
96  ::exit(RetCode);
97 }
98 
99 // Include the platform-specific parts of this class.
100 #ifdef LLVM_ON_UNIX
101 #include "Unix/Process.inc"
102 #endif
103 #ifdef _WIN32
104 #include "Windows/Process.inc"
105 #endif
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static bool AreCoreFilesPrevented()
true if PreventCoreFiles has been called, false otherwise.
Definition: Process.cpp:90
static CrashRecoveryContext * GetCurrent()
Return the active context, if the code is currently executing in a thread which is in a protected con...
const char EnvPathSeparator
This is the OS-specific separator for PATH like environment variables:
Definition: Program.h:30
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:455
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition: Path.cpp:675
static const char colorcodes[2][2][8][10]
Definition: Process.cpp:81
static LLVM_ATTRIBUTE_NORETURN void Exit(int RetCode)
Equivalent to ::exit(), except when running inside a CrashRecoveryContext.
Definition: Process.cpp:93
void SplitString(StringRef Source, SmallVectorImpl< StringRef > &OutFragments, StringRef Delimiters=" \\\)
SplitString - Split up the specified string according to the specified delimiters, appending the result fragments to the output list.
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:266
const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:255
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
static bool coreFilesPrevented
Definition: Process.cpp:88
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:1498
static Optional< std::string > FindInEnvPath(StringRef EnvName, StringRef FileName, ArrayRef< std::string > IgnoreList)
This function searches for an existing file in the list of directories in a PATH like environment var...
Definition: Process.cpp:36
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:883
#define LLVM_ATTRIBUTE_NORETURN
Definition: Compiler.h:249
Crash recovery helper object.
bool hasValue() const
Definition: Optional.h:259
Provides a library for accessing information about this process and other processes on the operating ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool equivalent(file_status A, file_status B)
Do file_status&#39;s represent the same thing?
#define ALLCOLORS(FGBG, BOLD)
Definition: Process.cpp:70
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
bool exists(const basic_file_status &status)
Does file exist?
Definition: Path.cpp:1063
static Optional< std::string > GetEnv(StringRef name)