28 #if !defined(__MINGW32__) 29 #pragma comment(lib, "psapi.lib") 30 #pragma comment(lib, "shell32.lib") 48 static unsigned computePageSize() {
52 GetNativeSystemInfo(&info);
55 return static_cast<unsigned>(info.dwPageSize);
59 static unsigned Ret = computePageSize();
71 while (_heapwalk(&hinfo) == _HEAPOK)
78 std::chrono::nanoseconds &sys_time) {
81 FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
82 if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
104 SetErrorMode(SEM_FAILCRITICALERRORS |
105 SEM_NOGPFAULTERRORBOX |
106 SEM_NOOPENFILEERRORBOX);
116 if (windows::UTF8ToUTF16(Name, NameUTF16))
123 size_t Size = MAX_PATH;
126 SetLastError(NO_ERROR);
129 if (Size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND)
138 if (windows::UTF16ToUTF8(Buf.
data(),
Size, Res))
140 return std::string(Res.
data());
145 char *Buf =
reinterpret_cast<char *
>(Alloc.
Allocate(S.
size() + 1, 1));
147 Buf[S.
size()] =
'\0';
152 static std::error_code ConvertAndPushArg(
const wchar_t *
Arg,
156 if (std::error_code ec = windows::UTF16ToUTF8(Arg, wcslen(Arg), ArgString))
158 Args.
push_back(AllocateString(ArgString, Alloc));
159 return std::error_code();
164 static std::error_code WildcardExpand(
const wchar_t *Arg,
167 if (!wcspbrk(Arg, L
"*?")) {
169 return ConvertAndPushArg(Arg, Args, Alloc);
172 if (wcscmp(Arg, L
"/?") == 0 || wcscmp(Arg, L
"-?") == 0) {
174 return ConvertAndPushArg(Arg, Args, Alloc);
179 if (std::error_code ec = windows::UTF16ToUTF8(Arg, wcslen(Arg), Dir))
182 const int DirSize = Dir.
size();
188 WIN32_FIND_DATAW FileData;
189 HANDLE FindHandle = FindFirstFileW(Arg, &FileData);
190 if (FindHandle == INVALID_HANDLE_VALUE) {
191 return ConvertAndPushArg(Arg, Args, Alloc);
197 ec = windows::UTF16ToUTF8(FileData.cFileName, wcslen(FileData.cFileName),
204 Args.
push_back(AllocateString(Dir, Alloc));
206 }
while (FindNextFileW(FindHandle, &FileData));
208 FindClose(FindHandle);
215 wchar_t ModuleName[MAX_PATH];
216 size_t Length = ::GetModuleFileNameW(NULL, ModuleName, MAX_PATH);
217 if (Length == 0 || Length == MAX_PATH) {
224 Length = GetLongPathNameW(ModuleName, ModuleName, MAX_PATH);
227 if (Length > MAX_PATH) {
234 std::error_code
EC = windows::UTF16ToUTF8(ModuleName, Length, Filename);
240 return std::error_code();
247 std::unique_ptr<wchar_t *[], decltype(&LocalFree)> UnicodeCommandLine{
248 CommandLineToArgvW(GetCommandLineW(), &ArgCount), &LocalFree};
249 if (!UnicodeCommandLine)
256 for (
int I = 0;
I < ArgCount; ++
I) {
257 EC = WildcardExpand(UnicodeCommandLine[
I], Args, Alloc);
265 EC = GetExecutableName(Filename);
269 Args[0] = AllocateString(Arg0, Alloc);
270 return std::error_code();
274 return std::error_code();
279 return std::error_code(errno, std::generic_category());
280 return std::error_code();
297 return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
301 unsigned Columns = 0;
302 CONSOLE_SCREEN_BUFFER_INFO csbi;
303 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
304 Columns = csbi.dwSize.X;
309 unsigned Columns = 0;
310 CONSOLE_SCREEN_BUFFER_INFO csbi;
311 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi))
312 Columns = csbi.dwSize.X;
329 static bool UseANSI =
false;
341 :defaultColor(GetCurrentColor()) {}
342 static unsigned GetCurrentColor() {
343 CONSOLE_SCREEN_BUFFER_INFO csbi;
344 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
345 return csbi.wAttributes;
348 WORD operator()()
const {
return defaultColor; }
351 DefaultColors defaultColors;
353 WORD fg_color(WORD color) {
354 return color & (FOREGROUND_BLUE | FOREGROUND_GREEN |
355 FOREGROUND_INTENSITY | FOREGROUND_RED);
358 WORD bg_color(WORD color) {
359 return color & (BACKGROUND_BLUE | BACKGROUND_GREEN |
360 BACKGROUND_INTENSITY | BACKGROUND_RED);
369 if (UseANSI)
return "\033[1m";
371 WORD colors = DefaultColors::GetCurrentColor();
373 colors |= BACKGROUND_INTENSITY;
375 colors |= FOREGROUND_INTENSITY;
376 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
381 if (UseANSI)
return colorcodes[bg?1:0][bold?1:0][code&7];
383 WORD current = DefaultColors::GetCurrentColor();
386 colors = ((code&1) ? BACKGROUND_RED : 0) |
387 ((code&2) ? BACKGROUND_GREEN : 0 ) |
388 ((code&4) ? BACKGROUND_BLUE : 0);
390 colors |= BACKGROUND_INTENSITY;
391 colors |= fg_color(current);
393 colors = ((code&1) ? FOREGROUND_RED : 0) |
394 ((code&2) ? FOREGROUND_GREEN : 0 ) |
395 ((code&4) ? FOREGROUND_BLUE : 0);
397 colors |= FOREGROUND_INTENSITY;
398 colors |= bg_color(current);
400 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
404 static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
405 CONSOLE_SCREEN_BUFFER_INFO
info;
406 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
407 return info.wAttributes;
411 if (UseANSI)
return "\033[7m";
414 = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
416 const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN |
417 FOREGROUND_RED | FOREGROUND_INTENSITY;
418 const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN |
419 BACKGROUND_RED | BACKGROUND_INTENSITY;
420 const WORD color_mask = foreground_mask | background_mask;
422 WORD new_attributes =
423 ((attributes & FOREGROUND_BLUE )?BACKGROUND_BLUE :0) |
424 ((attributes & FOREGROUND_GREEN )?BACKGROUND_GREEN :0) |
425 ((attributes & FOREGROUND_RED )?BACKGROUND_RED :0) |
426 ((attributes & FOREGROUND_INTENSITY)?BACKGROUND_INTENSITY:0) |
427 ((attributes & BACKGROUND_BLUE )?FOREGROUND_BLUE :0) |
428 ((attributes & BACKGROUND_GREEN )?FOREGROUND_GREEN :0) |
429 ((attributes & BACKGROUND_RED )?FOREGROUND_RED :0) |
430 ((attributes & BACKGROUND_INTENSITY)?FOREGROUND_INTENSITY:0) |
432 new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask);
434 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes);
439 if (UseANSI)
return "\033[0m";
440 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());
445 static void ReportLastErrorFatal(
const char *Msg) {
453 if (!::CryptAcquireContextW(&HCPC, NULL, NULL, PROV_RSA_FULL,
454 CRYPT_VERIFYCONTEXT))
455 ReportLastErrorFatal(
"Could not acquire a cryptographic context");
459 if (!::CryptGenRandom(CryptoProvider,
sizeof(Ret),
460 reinterpret_cast<BYTE *>(&Ret)))
461 ReportLastErrorFatal(
"Could not generate a random number");
void push_back(const T &Elt)
std::error_code GetCommandLineArguments(SmallVectorImpl< const char *> &Args, BumpPtrAllocator &Alloc)
void remove_filename(SmallVectorImpl< char > &path, Style style=Style::native)
Remove the last component from path unless it is the root dir.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Compute iterated dominance frontiers using a linear time algorithm.
static const char * ResetColor()
Resets the terminals colors, or returns an escape sequence to do so.
static const char * OutputColor(char c, bool bold, bool bg)
This function returns the colorcode escape sequences.
static bool StandardErrHasColors()
This function determines whether the terminal connected to standard error supports colors...
static bool FileDescriptorHasColors(int fd)
This function determines if the given file descriptor is displayd and supports colors.
static bool FileDescriptorIsDisplayed(int fd)
This function determines if the given file descriptor is connected to a "tty" or "console" window...
void reserve(size_type N)
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
static std::error_code FixupStandardFileDescriptors()
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
static bool ColorNeedsFlush()
Whether changing colors requires the output to be flushed.
static const char * OutputReverse()
This function returns the escape sequence to reverse forground and background colors.
static const char colorcodes[2][2][8][10]
void assign(size_type NumElts, const T &Elt)
static bool StandardInIsUserInput()
This function determines if the standard input is connected directly to a user's input (keyboard prob...
static bool StandardOutIsDisplayed()
This function determines if the standard output is connected to a "tty" or "console" window...
static void UseANSIEscapeCodes(bool enable)
Enables or disables whether ANSI escape sequences are used to output colors.
static unsigned StandardOutColumns()
This function determines the number of columns in the window if standard output is connected to a "tt...
Allocate memory in an ever growing pool, as if by bump-pointer.
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ATTRIBUTE_RETURNS_NOALIAS void * Allocate(size_t Size, size_t Alignment)
Allocate space at the specified alignment.
static sys::TimePoint< std::chrono::seconds > now(bool Deterministic)
static bool coreFilesPrevented
std::error_code mapWindowsError(unsigned EV)
static size_t GetMallocUsage()
Return process memory usage.
static unsigned getPageSize()
static bool StandardErrIsDisplayed()
This function determines if the standard error is connected to a "tty" or "console" window...
static void PreventCoreFiles()
This function makes the necessary calls to the operating system to prevent core files or any other ki...
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
static unsigned StandardErrColumns()
This function determines the number of columns in the window if standard error is connected to a "tty...
static bool StandardOutHasColors()
This function determines whether the terminal connected to standard output supports colors...
bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix)
amdgpu Simplify well known AMD library false Value Value * Arg
static unsigned GetRandomNumber()
Get the result of a process wide random number generator.
pointer data()
Return a pointer to the vector's buffer, even if empty().
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
std::chrono::microseconds toDuration(const struct timeval &TV)
Convert a struct timeval to a duration.
Deduce function attributes
static void GetTimeUsage(TimePoint<> &elapsed, std::chrono::nanoseconds &user_time, std::chrono::nanoseconds &sys_time)
This static function will set user_time to the amount of CPU time spent in user (non-kernel) mode and...
static const char * OutputBold(bool bg)
Same as OutputColor, but only enables the bold attribute.
void set_size(size_t Size)
Set the array size to N, which the current array must have enough capacity for.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
static std::error_code SafelyCloseFileDescriptor(int FD)
StringRef - Represent a constant reference to a string, i.e.
static Optional< std::string > GetEnv(StringRef name)
constexpr char Args[]
Key for Kernel::Metadata::mArgs.