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")
54 static unsigned computePageSize() {
58 GetNativeSystemInfo(&info);
61 return static_cast<unsigned>(info.dwPageSize);
65 static unsigned Ret = computePageSize();
77 while (_heapwalk(&hinfo) == _HEAPOK)
84 std::chrono::nanoseconds &sys_time) {
87 FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
88 if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
110 SetErrorMode(SEM_FAILCRITICALERRORS |
111 SEM_NOGPFAULTERRORBOX |
112 SEM_NOOPENFILEERRORBOX);
129 size_t Size = MAX_PATH;
145 return std::string(Res.
data());
152 ::memcpy(Buffer, S.
data(), S.
size());
153 Buffer[S.
size()] =
'\0';
158 static std::error_code
164 AllocateAndPush(ArgString, Args, Allocator);
165 return std::error_code();
170 static std::error_code
173 if (!wcspbrk(Arg,
L"*?")) {
175 return ConvertAndPushArg(Arg, Args, Allocator);
178 if (wcscmp(Arg,
L"/?") == 0 || wcscmp(Arg,
L"-?") == 0) {
180 return ConvertAndPushArg(Arg, Args, Allocator);
188 const int DirSize = Dir.
size();
194 WIN32_FIND_DATAW FileData;
195 HANDLE FindHandle = FindFirstFileW(Arg, &FileData);
196 if (FindHandle == INVALID_HANDLE_VALUE) {
197 return ConvertAndPushArg(Arg, Args, Allocator);
210 AllocateAndPush(Dir, Args, Allocator);
212 }
while (FindNextFileW(FindHandle, &FileData));
214 FindClose(FindHandle);
218 static std::error_code
240 wchar_t **UnicodeCommandLine =
241 CommandLineToArgvW(GetCommandLineW(), &ArgCount);
242 if (!UnicodeCommandLine)
250 wchar_t ModuleName[MAX_PATH];
251 int Length = ::GetModuleFileNameW(NULL, ModuleName, MAX_PATH);
252 if (0 < Length && Length < MAX_PATH)
253 UnicodeCommandLine[0] = ModuleName;
258 ec = ExpandShortFileName(UnicodeCommandLine[0], Args, ArgAllocator);
260 for (
int i = 1; i < ArgCount && !ec; ++i) {
261 ec = WildcardExpand(UnicodeCommandLine[i], Args, ArgAllocator);
266 LocalFree(UnicodeCommandLine);
271 return std::error_code();
276 return std::error_code(errno, std::generic_category());
277 return std::error_code();
294 return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
298 unsigned Columns = 0;
299 CONSOLE_SCREEN_BUFFER_INFO csbi;
300 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
301 Columns = csbi.dwSize.X;
306 unsigned Columns = 0;
307 CONSOLE_SCREEN_BUFFER_INFO csbi;
308 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi))
309 Columns = csbi.dwSize.X;
326 static bool UseANSI =
false;
338 :defaultColor(GetCurrentColor()) {}
339 static unsigned GetCurrentColor() {
340 CONSOLE_SCREEN_BUFFER_INFO csbi;
341 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
342 return csbi.wAttributes;
345 WORD operator()()
const {
return defaultColor; }
348 DefaultColors defaultColors;
350 WORD fg_color(WORD color) {
351 return color & (FOREGROUND_BLUE | FOREGROUND_GREEN |
352 FOREGROUND_INTENSITY | FOREGROUND_RED);
355 WORD bg_color(WORD color) {
356 return color & (BACKGROUND_BLUE | BACKGROUND_GREEN |
357 BACKGROUND_INTENSITY | BACKGROUND_RED);
366 if (UseANSI)
return "\033[1m";
368 WORD colors = DefaultColors::GetCurrentColor();
370 colors |= BACKGROUND_INTENSITY;
372 colors |= FOREGROUND_INTENSITY;
373 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
378 if (UseANSI)
return colorcodes[bg?1:0][bold?1:0][code&7];
380 WORD current = DefaultColors::GetCurrentColor();
383 colors = ((code&1) ? BACKGROUND_RED : 0) |
384 ((code&2) ? BACKGROUND_GREEN : 0 ) |
385 ((code&4) ? BACKGROUND_BLUE : 0);
387 colors |= BACKGROUND_INTENSITY;
388 colors |= fg_color(current);
390 colors = ((code&1) ? FOREGROUND_RED : 0) |
391 ((code&2) ? FOREGROUND_GREEN : 0 ) |
392 ((code&4) ? FOREGROUND_BLUE : 0);
394 colors |= FOREGROUND_INTENSITY;
395 colors |= bg_color(current);
397 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
401 static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
402 CONSOLE_SCREEN_BUFFER_INFO
info;
403 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
404 return info.wAttributes;
408 if (UseANSI)
return "\033[7m";
411 = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
413 const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN |
414 FOREGROUND_RED | FOREGROUND_INTENSITY;
415 const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN |
416 BACKGROUND_RED | BACKGROUND_INTENSITY;
417 const WORD color_mask = foreground_mask | background_mask;
419 WORD new_attributes =
420 ((attributes & FOREGROUND_BLUE )?BACKGROUND_BLUE :0) |
421 ((attributes & FOREGROUND_GREEN )?BACKGROUND_GREEN :0) |
422 ((attributes & FOREGROUND_RED )?BACKGROUND_RED :0) |
423 ((attributes & FOREGROUND_INTENSITY)?BACKGROUND_INTENSITY:0) |
424 ((attributes & BACKGROUND_BLUE )?FOREGROUND_BLUE :0) |
425 ((attributes & BACKGROUND_GREEN )?FOREGROUND_GREEN :0) |
426 ((attributes & BACKGROUND_RED )?FOREGROUND_RED :0) |
427 ((attributes & BACKGROUND_INTENSITY)?FOREGROUND_INTENSITY:0) |
429 new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask);
431 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes);
436 if (UseANSI)
return "\033[0m";
437 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());
442 static void ReportLastErrorFatal(
const char *Msg) {
450 if (!::CryptAcquireContextW(&HCPC, NULL, NULL, PROV_RSA_FULL,
451 CRYPT_VERIFYCONTEXT))
452 ReportLastErrorFatal(
"Could not acquire a cryptographic context");
456 if (!::CryptGenRandom(CryptoProvider,
sizeof(Ret),
457 reinterpret_cast<BYTE *>(&Ret)))
458 ReportLastErrorFatal(
"Could not generate a random number");
void push_back(const T &Elt)
void remove_filename(SmallVectorImpl< char > &path, Style style=Style::native)
Remove the last component from path unless it is the root dir.
size_t capacity() const
Return the total number of elements in the currently allocated buffer.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
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]
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 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 ...
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
static sys::TimePoint< std::chrono::seconds > now(bool Deterministic)
static bool coreFilesPrevented
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...
bool MakeErrMsg(std::string *ErrMsg, const std::string &prefix)
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
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().
std::chrono::microseconds toDuration(const struct timeval &TV)
Convert a struct timeval to a duration.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
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.
static std::error_code SafelyCloseFileDescriptor(int FD)
StringRef - Represent a constant reference to a string, i.e.
static Optional< std::string > GetEnv(StringRef name)