28 #if (HAVE_LIBPSAPI != 1)
29 #error "libpsapi.a should be present"
31 #if (HAVE_LIBSHELL32 != 1)
32 #error "libshell32.a should be present"
35 #pragma comment(lib, "psapi.lib")
36 #pragma comment(lib, "shell32.lib")
52 static TimeValue getTimeValueFromFILETIME(FILETIME Time) {
53 ULARGE_INTEGER TimeInteger;
54 TimeInteger.LowPart = Time.dwLowDateTime;
55 TimeInteger.HighPart = Time.dwHighDateTime;
59 static_cast<TimeValue::SecondsType>(TimeInteger.QuadPart / 10000000),
60 static_cast<TimeValue::NanoSecondsType>(
61 (TimeInteger.QuadPart % 10000000) * 100));
67 static unsigned computePageSize() {
71 GetNativeSystemInfo(&info);
74 return static_cast<unsigned>(info.dwPageSize);
78 static unsigned Ret = computePageSize();
90 while (_heapwalk(&hinfo) == _HEAPOK)
97 TimeValue &sys_time) {
100 FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
101 if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
105 user_time = getTimeValueFromFILETIME(UserTime);
106 sys_time = getTimeValueFromFILETIME(KernelTime);
123 SetErrorMode(SEM_FAILCRITICALERRORS |
124 SEM_NOGPFAULTERRORBOX |
125 SEM_NOOPENFILEERRORBOX);
140 size_t Size = MAX_PATH;
156 return std::string(Res.
data());
163 ::memcpy(Buffer, S.
data(), S.
size());
164 Buffer[S.
size()] =
'\0';
169 static std::error_code
175 AllocateAndPush(ArgString, Args, Allocator);
176 return std::error_code();
181 static std::error_code
184 if (!wcspbrk(Arg, L
"*?")) {
186 return ConvertAndPushArg(Arg, Args, Allocator);
189 if (wcscmp(Arg, L
"/?") == 0 || wcscmp(Arg, L
"-?") == 0) {
191 return ConvertAndPushArg(Arg, Args, Allocator);
199 const int DirSize = Dir.
size();
202 WIN32_FIND_DATAW FileData;
203 HANDLE FindHandle = FindFirstFileW(Arg, &FileData);
204 if (FindHandle == INVALID_HANDLE_VALUE) {
205 return ConvertAndPushArg(Arg, Args, Allocator);
218 AllocateAndPush(Dir, Args, Allocator);
220 }
while (FindNextFileW(FindHandle, &FileData));
222 FindClose(FindHandle);
231 wchar_t **UnicodeCommandLine =
232 CommandLineToArgvW(GetCommandLineW(), &ArgCount);
233 if (!UnicodeCommandLine)
239 for (
int i = 0; i < ArgCount; ++i) {
240 ec = WildcardExpand(UnicodeCommandLine[i], Args, ArgAllocator);
245 LocalFree(UnicodeCommandLine);
250 return std::error_code();
255 return std::error_code(errno, std::generic_category());
256 return std::error_code();
273 return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
277 unsigned Columns = 0;
278 CONSOLE_SCREEN_BUFFER_INFO csbi;
279 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
280 Columns = csbi.dwSize.X;
285 unsigned Columns = 0;
286 CONSOLE_SCREEN_BUFFER_INFO csbi;
287 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi))
288 Columns = csbi.dwSize.X;
305 static bool UseANSI =
false;
317 :defaultColor(GetCurrentColor()) {}
318 static unsigned GetCurrentColor() {
319 CONSOLE_SCREEN_BUFFER_INFO csbi;
320 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
321 return csbi.wAttributes;
324 WORD operator()()
const {
return defaultColor; }
327 DefaultColors defaultColors;
329 WORD fg_color(WORD color) {
330 return color & (FOREGROUND_BLUE | FOREGROUND_GREEN |
331 FOREGROUND_INTENSITY | FOREGROUND_RED);
334 WORD bg_color(WORD color) {
335 return color & (BACKGROUND_BLUE | BACKGROUND_GREEN |
336 BACKGROUND_INTENSITY | BACKGROUND_RED);
345 if (UseANSI)
return "\033[1m";
347 WORD colors = DefaultColors::GetCurrentColor();
349 colors |= BACKGROUND_INTENSITY;
351 colors |= FOREGROUND_INTENSITY;
352 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
357 if (UseANSI)
return colorcodes[bg?1:0][bold?1:0][code&7];
359 WORD current = DefaultColors::GetCurrentColor();
362 colors = ((code&1) ? BACKGROUND_RED : 0) |
363 ((code&2) ? BACKGROUND_GREEN : 0 ) |
364 ((code&4) ? BACKGROUND_BLUE : 0);
366 colors |= BACKGROUND_INTENSITY;
367 colors |= fg_color(current);
369 colors = ((code&1) ? FOREGROUND_RED : 0) |
370 ((code&2) ? FOREGROUND_GREEN : 0 ) |
371 ((code&4) ? FOREGROUND_BLUE : 0);
373 colors |= FOREGROUND_INTENSITY;
374 colors |= bg_color(current);
376 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
380 static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
381 CONSOLE_SCREEN_BUFFER_INFO
info;
382 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
383 return info.wAttributes;
387 if (UseANSI)
return "\033[7m";
390 = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
392 const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN |
393 FOREGROUND_RED | FOREGROUND_INTENSITY;
394 const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN |
395 BACKGROUND_RED | BACKGROUND_INTENSITY;
396 const WORD color_mask = foreground_mask | background_mask;
398 WORD new_attributes =
399 ((attributes & FOREGROUND_BLUE )?BACKGROUND_BLUE :0) |
400 ((attributes & FOREGROUND_GREEN )?BACKGROUND_GREEN :0) |
401 ((attributes & FOREGROUND_RED )?BACKGROUND_RED :0) |
402 ((attributes & FOREGROUND_INTENSITY)?BACKGROUND_INTENSITY:0) |
403 ((attributes & BACKGROUND_BLUE )?FOREGROUND_BLUE :0) |
404 ((attributes & BACKGROUND_GREEN )?FOREGROUND_GREEN :0) |
405 ((attributes & BACKGROUND_RED )?FOREGROUND_RED :0) |
406 ((attributes & BACKGROUND_INTENSITY)?FOREGROUND_INTENSITY:0) |
408 new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask);
410 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes);
415 if (UseANSI)
return "\033[0m";
416 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());
422 if (!::CryptAcquireContextW(&HCPC, NULL, NULL, PROV_RSA_FULL,
423 CRYPT_VERIFYCONTEXT))
428 if (!::CryptGenRandom(CryptoProvider,
sizeof(Ret),
429 reinterpret_cast<BYTE *>(&Ret)))
void push_back(const T &Elt)
static TimeValue now()
This is a static constructor that returns a TimeValue that represents the current time...
size_t capacity() const
Return the total number of elements in the currently allocated buffer.
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.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
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.
void remove_filename(SmallVectorImpl< char > &path)
Remove the last component from path unless it is the root dir.
static const char * OutputReverse()
This function returns the escape sequence to reverse forground and background colors.
static const char colorcodes[2][2][8][10]
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
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...
static void GetTimeUsage(TimeValue &elapsed, TimeValue &user_time, TimeValue &sys_time)
This static function will set user_time to the amount of CPU time spent in user (non-kernel) mode and...
static std::error_code GetArgumentVector(SmallVectorImpl< const char * > &Args, ArrayRef< const char * > ArgsFromMain, SpecificBumpPtrAllocator< char > &ArgAllocator)
This function returns a SmallVector containing the arguments passed from the operating system to the ...
std::error_code mapWindowsError(unsigned EV)
std::error_code UTF8ToUTF16(StringRef utf8, SmallVectorImpl< wchar_t > &utf16)
T * Allocate(size_t num=1)
Allocate space for an array of objects without constructing them.
static size_t GetMallocUsage()
Return process memory usage.
std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len, SmallVectorImpl< char > &utf8)
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...
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...
A BumpPtrAllocator that allows only elements of a specific type to be allocated.
static bool StandardOutHasColors()
This function determines whether the terminal connected to standard output supports colors...
static unsigned GetRandomNumber()
Get the result of a process wide random number generator.
void set_size(size_type N)
Set the array size to N, which the current array must have enough capacity for.
pointer data()
Return a pointer to the vector's buffer, even if empty().
Deduce function attributes
static const char * OutputBold(bool bg)
Same as OutputColor, but only enables the bold attribute.
static std::error_code SafelyCloseFileDescriptor(int FD)
StringRef - Represent a constant reference to a string, i.e.
static Optional< std::string > GetEnv(StringRef name)