29 #if !defined(__MINGW32__) 30 #pragma comment(lib, "psapi.lib") 31 #pragma comment(lib, "shell32.lib") 49 static unsigned computePageSize() {
53 GetNativeSystemInfo(&info);
56 return static_cast<unsigned>(info.dwPageSize);
60 static unsigned Ret = computePageSize();
72 while (_heapwalk(&hinfo) == _HEAPOK)
79 std::chrono::nanoseconds &sys_time) {
82 FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
83 if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
105 SetErrorMode(SEM_FAILCRITICALERRORS |
106 SEM_NOGPFAULTERRORBOX |
107 SEM_NOOPENFILEERRORBOX);
117 if (windows::UTF8ToUTF16(Name, NameUTF16))
124 size_t Size = MAX_PATH;
127 SetLastError(NO_ERROR);
130 if (Size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND)
139 if (windows::UTF16ToUTF8(Buf.
data(),
Size, Res))
141 return std::string(Res.
data());
162 EC = windows::UTF8ToUTF16(Arg, ArgW);
170 WIN32_FIND_DATAW FileData;
171 HANDLE FindHandle = FindFirstFileW(ArgW.
data(), &FileData);
172 if (FindHandle == INVALID_HANDLE_VALUE) {
180 const int DirSize = Dir.
size();
184 EC = windows::UTF16ToUTF8(FileData.cFileName, wcslen(FileData.cFileName),
193 }
while (FindNextFileW(FindHandle, &FileData));
195 FindClose(FindHandle);
202 wchar_t ModuleName[MAX_PATH];
203 size_t Length = ::GetModuleFileNameW(NULL, ModuleName, MAX_PATH);
204 if (Length == 0 || Length == MAX_PATH) {
211 Length = GetLongPathNameW(ModuleName, ModuleName, MAX_PATH);
214 if (Length > MAX_PATH) {
221 std::error_code EC = windows::UTF16ToUTF8(ModuleName, Length, Filename);
227 return std::error_code();
233 const wchar_t *CmdW = GetCommandLineW();
237 EC = windows::UTF16ToUTF8(CmdW, wcslen(CmdW), Cmd);
245 for (
const char *Arg : TmpArgs) {
246 EC = WildcardExpand(Arg, Args, Saver);
254 EC = GetExecutableName(Filename);
259 return std::error_code();
263 return std::error_code();
268 return std::error_code(errno, std::generic_category());
269 return std::error_code();
286 return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
290 unsigned Columns = 0;
291 CONSOLE_SCREEN_BUFFER_INFO csbi;
292 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
293 Columns = csbi.dwSize.X;
298 unsigned Columns = 0;
299 CONSOLE_SCREEN_BUFFER_INFO csbi;
300 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi))
301 Columns = csbi.dwSize.X;
318 static bool UseANSI =
false;
320 #if defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING) 322 HANDLE Console = GetStdHandle(STD_OUTPUT_HANDLE);
324 GetConsoleMode(Console, &Mode);
325 Mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
326 SetConsoleMode(Console, Mode);
339 :defaultColor(GetCurrentColor()) {}
340 static unsigned GetCurrentColor() {
341 CONSOLE_SCREEN_BUFFER_INFO csbi;
342 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
343 return csbi.wAttributes;
346 WORD operator()()
const {
return defaultColor; }
349 DefaultColors defaultColors;
351 WORD fg_color(WORD color) {
352 return color & (FOREGROUND_BLUE | FOREGROUND_GREEN |
353 FOREGROUND_INTENSITY | FOREGROUND_RED);
356 WORD bg_color(WORD color) {
357 return color & (BACKGROUND_BLUE | BACKGROUND_GREEN |
358 BACKGROUND_INTENSITY | BACKGROUND_RED);
367 if (UseANSI)
return "\033[1m";
369 WORD colors = DefaultColors::GetCurrentColor();
371 colors |= BACKGROUND_INTENSITY;
373 colors |= FOREGROUND_INTENSITY;
374 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
379 if (UseANSI)
return colorcodes[bg?1:0][bold?1:0][code&7];
381 WORD current = DefaultColors::GetCurrentColor();
384 colors = ((code&1) ? BACKGROUND_RED : 0) |
385 ((code&2) ? BACKGROUND_GREEN : 0 ) |
386 ((code&4) ? BACKGROUND_BLUE : 0);
388 colors |= BACKGROUND_INTENSITY;
389 colors |= fg_color(current);
391 colors = ((code&1) ? FOREGROUND_RED : 0) |
392 ((code&2) ? FOREGROUND_GREEN : 0 ) |
393 ((code&4) ? FOREGROUND_BLUE : 0);
395 colors |= FOREGROUND_INTENSITY;
396 colors |= bg_color(current);
398 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
402 static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
403 CONSOLE_SCREEN_BUFFER_INFO
info;
404 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
405 return info.wAttributes;
409 if (UseANSI)
return "\033[7m";
412 = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
414 const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN |
415 FOREGROUND_RED | FOREGROUND_INTENSITY;
416 const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN |
417 BACKGROUND_RED | BACKGROUND_INTENSITY;
418 const WORD color_mask = foreground_mask | background_mask;
420 WORD new_attributes =
421 ((attributes & FOREGROUND_BLUE )?BACKGROUND_BLUE :0) |
422 ((attributes & FOREGROUND_GREEN )?BACKGROUND_GREEN :0) |
423 ((attributes & FOREGROUND_RED )?BACKGROUND_RED :0) |
424 ((attributes & FOREGROUND_INTENSITY)?BACKGROUND_INTENSITY:0) |
425 ((attributes & BACKGROUND_BLUE )?FOREGROUND_BLUE :0) |
426 ((attributes & BACKGROUND_GREEN )?FOREGROUND_GREEN :0) |
427 ((attributes & BACKGROUND_RED )?FOREGROUND_RED :0) |
428 ((attributes & BACKGROUND_INTENSITY)?FOREGROUND_INTENSITY:0) |
430 new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask);
432 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes);
437 if (UseANSI)
return "\033[0m";
438 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());
444 if (!::CryptAcquireContextW(&HCPC, NULL, NULL, PROV_RSA_FULL,
445 CRYPT_VERIFYCONTEXT))
450 if (!::CryptGenRandom(CryptoProvider,
sizeof(Ret),
451 reinterpret_cast<BYTE *>(&Ret)))
456 typedef NTSTATUS(WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
457 #define STATUS_SUCCESS ((NTSTATUS)0x00000000L) 460 HMODULE hMod = ::GetModuleHandleW(L
"ntdll.dll");
462 auto getVer = (RtlGetVersionPtr)::GetProcAddress(hMod,
"RtlGetVersion");
464 RTL_OSVERSIONINFOEXW info{};
465 info.dwOSVersionInfoSize =
sizeof(
info);
466 if (getVer((PRTL_OSVERSIONINFOW)&
info) == STATUS_SUCCESS) {
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.
This class represents lattice values for constants.
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.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
void push_back(const T &Elt)
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]
Tagged union holding either a T or a Error.
void TokenizeWindowsCommandLine(StringRef Source, StringSaver &Saver, SmallVectorImpl< const char *> &NewArgv, bool MarkEOLs=false)
Tokenizes a Windows command line which may contain quotes and escaped quotes.
void assign(size_type NumElts, const T &Elt)
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
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.
void set_size(size_t N)
Set the array size to N, which the current array must have enough capacity for.
static sys::TimePoint< std::chrono::seconds > now(bool Deterministic)
static bool coreFilesPrevented
std::error_code mapWindowsError(unsigned EV)
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
bool RunningWindows8OrGreater()
Determines if the program is running on Windows 8 or newer.
static size_t GetMallocUsage()
Return process memory usage.
llvm::VersionTuple GetWindowsOSVersion()
Returns the Windows version as Major.Minor.0.BuildNumber.
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...
StringRef save(const char *S)
static unsigned GetRandomNumber()
Get the result of a process wide random number generator.
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer...
Represents a version number in the form major[.minor[.subminor[.build]]].
pointer data()
Return a pointer to the vector's buffer, even if empty().
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
LLVM_NODISCARD size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
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...
LLVM_ATTRIBUTE_NORETURN void ReportLastErrorFatal(const char *Msg)
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Deduce and propagate attributes
static const char * OutputBold(bool bg)
Same as OutputColor, but only enables the bold attribute.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static Expected< unsigned > getPageSize()
Get the process's page size.
static std::error_code SafelyCloseFileDescriptor(int FD)
StringRef - Represent a constant reference to a string, i.e.
std::chrono::nanoseconds toDuration(FILETIME Time)
static Optional< std::string > GetEnv(StringRef name)
constexpr char Args[]
Key for Kernel::Metadata::mArgs.