30 #if !defined(__MINGW32__) 31 #pragma comment(lib, "psapi.lib") 32 #pragma comment(lib, "shell32.lib") 50 static unsigned computePageSize() {
54 GetNativeSystemInfo(&info);
57 return static_cast<unsigned>(info.dwPageSize);
61 static unsigned Ret = computePageSize();
73 while (_heapwalk(&hinfo) == _HEAPOK)
80 std::chrono::nanoseconds &sys_time) {
83 FILETIME ProcCreate, ProcExit, KernelTime, UserTime;
84 if (GetProcessTimes(GetCurrentProcess(), &ProcCreate, &ProcExit, &KernelTime,
106 SetErrorMode(SEM_FAILCRITICALERRORS |
107 SEM_NOGPFAULTERRORBOX |
108 SEM_NOOPENFILEERRORBOX);
118 if (windows::UTF8ToUTF16(Name, NameUTF16))
125 size_t Size = MAX_PATH;
128 SetLastError(NO_ERROR);
131 if (Size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND)
140 if (windows::UTF16ToUTF8(Buf.
data(),
Size, Res))
142 return std::string(Res.
data());
163 EC = windows::UTF8ToUTF16(Arg, ArgW);
171 WIN32_FIND_DATAW FileData;
172 HANDLE FindHandle = FindFirstFileW(ArgW.
data(), &FileData);
173 if (FindHandle == INVALID_HANDLE_VALUE) {
181 const int DirSize = Dir.
size();
185 EC = windows::UTF16ToUTF8(FileData.cFileName, wcslen(FileData.cFileName),
194 }
while (FindNextFileW(FindHandle, &FileData));
196 FindClose(FindHandle);
203 wchar_t ModuleName[MAX_PATH];
204 size_t Length = ::GetModuleFileNameW(NULL, ModuleName, MAX_PATH);
205 if (Length == 0 || Length == MAX_PATH) {
212 Length = GetLongPathNameW(ModuleName, ModuleName, MAX_PATH);
215 if (Length > MAX_PATH) {
222 std::error_code EC = windows::UTF16ToUTF8(ModuleName, Length, Filename);
228 return std::error_code();
234 const wchar_t *CmdW = GetCommandLineW();
238 EC = windows::UTF16ToUTF8(CmdW, wcslen(CmdW), Cmd);
246 for (
const char *Arg : TmpArgs) {
247 EC = WildcardExpand(Arg, Args, Saver);
255 EC = GetExecutableName(Filename);
260 return std::error_code();
264 return std::error_code();
269 return std::error_code(errno, std::generic_category());
270 return std::error_code();
287 return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
291 unsigned Columns = 0;
292 CONSOLE_SCREEN_BUFFER_INFO csbi;
293 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
294 Columns = csbi.dwSize.X;
299 unsigned Columns = 0;
300 CONSOLE_SCREEN_BUFFER_INFO csbi;
301 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi))
302 Columns = csbi.dwSize.X;
319 static bool UseANSI =
false;
321 #if defined(ENABLE_VIRTUAL_TERMINAL_PROCESSING) 323 HANDLE Console = GetStdHandle(STD_OUTPUT_HANDLE);
325 GetConsoleMode(Console, &Mode);
326 Mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
327 SetConsoleMode(Console, Mode);
340 :defaultColor(GetCurrentColor()) {}
341 static unsigned GetCurrentColor() {
342 CONSOLE_SCREEN_BUFFER_INFO csbi;
343 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
344 return csbi.wAttributes;
347 WORD operator()()
const {
return defaultColor; }
350 DefaultColors defaultColors;
352 WORD fg_color(WORD color) {
353 return color & (FOREGROUND_BLUE | FOREGROUND_GREEN |
354 FOREGROUND_INTENSITY | FOREGROUND_RED);
357 WORD bg_color(WORD color) {
358 return color & (BACKGROUND_BLUE | BACKGROUND_GREEN |
359 BACKGROUND_INTENSITY | BACKGROUND_RED);
368 if (UseANSI)
return "\033[1m";
370 WORD colors = DefaultColors::GetCurrentColor();
372 colors |= BACKGROUND_INTENSITY;
374 colors |= FOREGROUND_INTENSITY;
375 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
380 if (UseANSI)
return colorcodes[bg?1:0][bold?1:0][code&7];
382 WORD current = DefaultColors::GetCurrentColor();
385 colors = ((code&1) ? BACKGROUND_RED : 0) |
386 ((code&2) ? BACKGROUND_GREEN : 0 ) |
387 ((code&4) ? BACKGROUND_BLUE : 0);
389 colors |= BACKGROUND_INTENSITY;
390 colors |= fg_color(current);
392 colors = ((code&1) ? FOREGROUND_RED : 0) |
393 ((code&2) ? FOREGROUND_GREEN : 0 ) |
394 ((code&4) ? FOREGROUND_BLUE : 0);
396 colors |= FOREGROUND_INTENSITY;
397 colors |= bg_color(current);
399 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors);
403 static WORD GetConsoleTextAttribute(HANDLE hConsoleOutput) {
404 CONSOLE_SCREEN_BUFFER_INFO
info;
405 GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info);
406 return info.wAttributes;
410 if (UseANSI)
return "\033[7m";
413 = GetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE));
415 const WORD foreground_mask = FOREGROUND_BLUE | FOREGROUND_GREEN |
416 FOREGROUND_RED | FOREGROUND_INTENSITY;
417 const WORD background_mask = BACKGROUND_BLUE | BACKGROUND_GREEN |
418 BACKGROUND_RED | BACKGROUND_INTENSITY;
419 const WORD color_mask = foreground_mask | background_mask;
421 WORD new_attributes =
422 ((attributes & FOREGROUND_BLUE )?BACKGROUND_BLUE :0) |
423 ((attributes & FOREGROUND_GREEN )?BACKGROUND_GREEN :0) |
424 ((attributes & FOREGROUND_RED )?BACKGROUND_RED :0) |
425 ((attributes & FOREGROUND_INTENSITY)?BACKGROUND_INTENSITY:0) |
426 ((attributes & BACKGROUND_BLUE )?FOREGROUND_BLUE :0) |
427 ((attributes & BACKGROUND_GREEN )?FOREGROUND_GREEN :0) |
428 ((attributes & BACKGROUND_RED )?FOREGROUND_RED :0) |
429 ((attributes & BACKGROUND_INTENSITY)?FOREGROUND_INTENSITY:0) |
431 new_attributes = (attributes & ~color_mask) | (new_attributes & color_mask);
433 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), new_attributes);
438 if (UseANSI)
return "\033[0m";
439 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());
444 static void ReportLastErrorFatal(
const char *Msg) {
452 if (!::CryptAcquireContextW(&HCPC, NULL, NULL, PROV_RSA_FULL,
453 CRYPT_VERIFYCONTEXT))
454 ReportLastErrorFatal(
"Could not acquire a cryptographic context");
458 if (!::CryptGenRandom(CryptoProvider,
sizeof(Ret),
459 reinterpret_cast<BYTE *>(&Ret)))
460 ReportLastErrorFatal(
"Could not generate a random number");
464 typedef NTSTATUS(WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
465 #define STATUS_SUCCESS ((NTSTATUS)0x00000000L) 468 HMODULE hMod = ::GetModuleHandleW(L
"ntdll.dll");
470 auto getVer = (RtlGetVersionPtr)::GetProcAddress(hMod,
"RtlGetVersion");
472 RTL_OSVERSIONINFOEXW info{};
473 info.dwOSVersionInfoSize =
sizeof(
info);
474 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.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
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.
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)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
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.
amdgpu Simplify well known AMD library false Value Value const Twine & Name
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 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.
static sys::TimePoint< std::chrono::seconds > now(bool Deterministic)
static bool coreFilesPrevented
std::error_code mapWindowsError(unsigned EV)
bool RunningWindows8OrGreater()
Determines if the program is running on Windows 8 or newer.
static size_t GetMallocUsage()
Return process memory usage.
static unsigned getPageSize()
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)
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.
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.
std::chrono::microseconds toDuration(const struct timeval &TV)
Convert a struct timeval to a duration.
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.
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.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void set_size(size_t Size)
Set the array size to N, which the current array must have enough capacity for.
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.