From 34d16ec53c11edb6a5baaa63fb52993c8ca03c19 Mon Sep 17 00:00:00 2001 From: slangbot <186143334+slangbot@users.noreply.github.com> Date: Tue, 31 Dec 2024 13:52:06 +0000 Subject: [PATCH] format code --- examples/CMakeLists.txt | 52 +++++---------- examples/example-base/example-base.h | 7 +- examples/example-main/main.cpp | 3 +- examples/example-winmain/main.cpp | 15 +++-- examples/stacktrace-windows/common.cpp | 92 ++++++++++++-------------- 5 files changed, 72 insertions(+), 97 deletions(-) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 4896ed4079..759d99994e 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,11 +1,5 @@ function(example dir) - cmake_parse_arguments( - ARG - "WIN32_EXECUTABLE" - "" - "" - ${ARGN} - ) + cmake_parse_arguments(ARG "WIN32_EXECUTABLE" "" "" ${ARGN}) set(debug_dir ${CMAKE_CURRENT_BINARY_DIR}/${dir}) @@ -39,19 +33,19 @@ function(example dir) endif() # Libraries providing a main function that prints stack traces on exceptions - if (CMAKE_SYSTEM_NAME MATCHES "Windows") - # On Windows we have two different versions: main for "console applications" and - # WinMain for normal Windows applications. - if (${ARG_WIN32_EXECUTABLE}) - set (main_wrapper_libraries example-winmain) - else() - set (main_wrapper_libraries example-main) - endif() - # Add stack printing support - set (main_wrapper_libraries ${main_wrapper_libraries} stacktrace-windows) - set (main_wrapper_libraries ${main_wrapper_libraries} dbghelp.lib) + if(CMAKE_SYSTEM_NAME MATCHES "Windows") + # On Windows we have two different versions: main for "console applications" and + # WinMain for normal Windows applications. + if(${ARG_WIN32_EXECUTABLE}) + set(main_wrapper_libraries example-winmain) + else() + set(main_wrapper_libraries example-main) + endif() + # Add stack printing support + set(main_wrapper_libraries ${main_wrapper_libraries} stacktrace-windows) + set(main_wrapper_libraries ${main_wrapper_libraries} dbghelp.lib) else() - set (main_wrapper_libraries example-main) + set(main_wrapper_libraries example-main) endif() slang_add_target( @@ -94,23 +88,9 @@ if(SLANG_ENABLE_EXAMPLES) $<$:CUDA::cuda_driver> FOLDER examples ) - slang_add_target( - example-main - STATIC - FOLDER examples - ) - slang_add_target( - example-winmain - STATIC - FOLDER examples - EXCLUDE_FROM_ALL - ) - slang_add_target( - stacktrace-windows - STATIC - FOLDER examples - EXCLUDE_FROM_ALL - ) + slang_add_target(example-main STATIC FOLDER examples) + slang_add_target(example-winmain STATIC FOLDER examples EXCLUDE_FROM_ALL) + slang_add_target(stacktrace-windows STATIC FOLDER examples EXCLUDE_FROM_ALL) add_custom_target( all-examples diff --git a/examples/example-base/example-base.h b/examples/example-base/example-base.h index 88c1a73c15..b3c2f7ce29 100644 --- a/examples/example-base/example-base.h +++ b/examples/example-base/example-base.h @@ -14,10 +14,9 @@ void _Win32OutputDebugString(const char* str); #define SLANG_EXPAND_STRINGIFY(x) SLANG_STRINGIFY(x) #ifdef _WIN32 -#define EXAMPLE_MAIN(innerMain) \ - extern const char*const g_logFileName = "stdout-" \ - SLANG_EXPAND_STRINGIFY(SLANG_EXAMPLE_NAME) \ - ".txt"; \ +#define EXAMPLE_MAIN(innerMain) \ + extern const char* const g_logFileName = \ + "stdout-" SLANG_EXPAND_STRINGIFY(SLANG_EXAMPLE_NAME) ".txt"; \ PLATFORM_UI_MAIN(innerMain); #else diff --git a/examples/example-main/main.cpp b/examples/example-main/main.cpp index 7a7b4f3cd1..2e70bb9b17 100644 --- a/examples/example-main/main.cpp +++ b/examples/example-main/main.cpp @@ -1,6 +1,7 @@ +#include "../stacktrace-windows/common.h" + #include #include -#include "../stacktrace-windows/common.h" extern int exampleMain(int argc, char** argv); diff --git a/examples/example-winmain/main.cpp b/examples/example-winmain/main.cpp index 110755072a..343040b7de 100644 --- a/examples/example-winmain/main.cpp +++ b/examples/example-winmain/main.cpp @@ -1,16 +1,17 @@ -#include +#include "../stacktrace-windows/common.h" + #include #include -#include "../stacktrace-windows/common.h" +#include extern int exampleMain(int argc, char** argv); -extern const char *const g_logFileName; +extern const char* const g_logFileName; int WinMain( - HINSTANCE /* instance */, - HINSTANCE /* prevInstance */, - LPSTR /* commandLine */, - int /*showCommand*/) + HINSTANCE /* instance */, + HINSTANCE /* prevInstance */, + LPSTR /* commandLine */, + int /*showCommand*/) { __try diff --git a/examples/stacktrace-windows/common.cpp b/examples/stacktrace-windows/common.cpp index 8569d97de9..3de37ad76c 100644 --- a/examples/stacktrace-windows/common.cpp +++ b/examples/stacktrace-windows/common.cpp @@ -1,13 +1,14 @@ -#include +#include "common.h" + +#include +#include #include +#include #include -#include -#include -#include "common.h" -#define SLANG_EXAMPLE_LOG_ERROR(...) \ - printf("error: %s: %d: ", __FILE__, __LINE__); \ - print(__VA_ARGS__); \ +#define SLANG_EXAMPLE_LOG_ERROR(...) \ + printf("error: %s: %d: ", __FILE__, __LINE__); \ + print(__VA_ARGS__); \ printf("\n"); static void print() {} @@ -21,19 +22,16 @@ static bool getModuleFileNameAtAddress(DWORD64 const address, std::string& fileN { HMODULE module = NULL; { - BOOL result = - GetModuleHandleEx( - GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | - GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, - (LPCTSTR)address, - &module - ); - if(result == 0) + BOOL result = GetModuleHandleEx( + GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + (LPCTSTR)address, + &module); + if (result == 0) { SLANG_EXAMPLE_LOG_ERROR(GetLastError()); return false; } - if(module == NULL) + if (module == NULL) { SLANG_EXAMPLE_LOG_ERROR(); return false; @@ -42,15 +40,15 @@ static bool getModuleFileNameAtAddress(DWORD64 const address, std::string& fileN std::vector buffer(1U << 8U); uint32_t constexpr maxBufferSize = 1U << 20; - while(buffer.size() < maxBufferSize) + while (buffer.size() < maxBufferSize) { DWORD result = GetModuleFileNameA(module, buffer.data(), buffer.size()); - if(result == 0) + if (result == 0) { SLANG_EXAMPLE_LOG_ERROR(GetLastError()); return false; } - else if(result == ERROR_INSUFFICIENT_BUFFER) + else if (result == ERROR_INSUFFICIENT_BUFFER) { buffer.resize(buffer.size() << 1U); } @@ -59,7 +57,7 @@ static bool getModuleFileNameAtAddress(DWORD64 const address, std::string& fileN break; } } - if(buffer.size() == maxBufferSize) + if (buffer.size() == maxBufferSize) { SLANG_EXAMPLE_LOG_ERROR(); return false; @@ -87,26 +85,24 @@ static bool printStack(HANDLE process, HANDLE thread, CONTEXT const& context) STACKFRAME64 frame = {}; constexpr uint32_t maxFrameCount = 1U << 10; uint32_t frameIndex = 0U; - while(frameIndex < maxFrameCount) + while (frameIndex < maxFrameCount) { // Use the default routine PREAD_PROCESS_MEMORY_ROUTINE64 readMemoryRoutine = NULL; // Not sure what this is for, but documentation says most callers can pass NULL PTRANSLATE_ADDRESS_ROUTINE64 translateAddressRoutine = NULL; { - BOOL result = - StackWalk64( - machineType, - process, - thread, - &frame, - &contextCopy, - readMemoryRoutine, - SymFunctionTableAccess64, - SymGetModuleBase64, - translateAddressRoutine - ); - if(result == FALSE) + BOOL result = StackWalk64( + machineType, + process, + thread, + &frame, + &contextCopy, + readMemoryRoutine, + SymFunctionTableAccess64, + SymGetModuleBase64, + translateAddressRoutine); + if (result == FALSE) break; } @@ -117,9 +113,8 @@ static bool printStack(HANDLE process, HANDLE thread, CONTEXT const& context) DWORD64 address = frame.AddrPC.Offset; // Not required, we want to look up the symbol exactly at the address PDWORD64 displacement = NULL; - BOOL result = - SymFromAddr(process, address, displacement, maybeSymbol); - if(result == FALSE) + BOOL result = SymFromAddr(process, address, displacement, maybeSymbol); + if (result == FALSE) { SLANG_EXAMPLE_LOG_ERROR(GetLastError()); maybeSymbol = NULL; @@ -129,10 +124,10 @@ static bool printStack(HANDLE process, HANDLE thread, CONTEXT const& context) printf("%u", frameIndex); std::string moduleFileName; - if(getModuleFileNameAtAddress(frame.AddrPC.Offset, moduleFileName)) + if (getModuleFileNameAtAddress(frame.AddrPC.Offset, moduleFileName)) printf(": %s", moduleFileName.c_str()); - if(maybeSymbol) + if (maybeSymbol) { PSYMBOL_INFO& symbol = maybeSymbol; @@ -142,8 +137,7 @@ static bool printStack(HANDLE process, HANDLE thread, CONTEXT const& context) DWORD displacement; if (SymGetLineFromAddr64(process, frame.AddrPC.Offset, &displacement, &line)) { - printf(": %s: %s: %lu", - symbol->Name, line.FileName, line.LineNumber); + printf(": %s: %s: %lu", symbol->Name, line.FileName, line.LineNumber); } else { @@ -162,8 +156,9 @@ static bool printStack(HANDLE process, HANDLE thread, CONTEXT const& context) int exceptionFilter(_EXCEPTION_POINTERS* exception) { - printf("error: Exception 0x%x occurred. Stack trace:\n", - exception->ExceptionRecord->ExceptionCode); + printf( + "error: Exception 0x%x occurred. Stack trace:\n", + exception->ExceptionRecord->ExceptionCode); HANDLE process = GetCurrentProcess(); HANDLE thread = GetCurrentThread(); @@ -173,9 +168,8 @@ int exceptionFilter(_EXCEPTION_POINTERS* exception) // The default search paths should suffice PCSTR symbolFileSearchPath = NULL; BOOL loadSymbolsOfLoadedModules = TRUE; - BOOL result = - SymInitialize(process, symbolFileSearchPath, loadSymbolsOfLoadedModules); - if(result == FALSE) + BOOL result = SymInitialize(process, symbolFileSearchPath, loadSymbolsOfLoadedModules); + if (result == FALSE) { printf("warning: Failed to load symbols\n"); } @@ -185,15 +179,15 @@ int exceptionFilter(_EXCEPTION_POINTERS* exception) } } - if(!printStack(process, thread, *exception->ContextRecord)) + if (!printStack(process, thread, *exception->ContextRecord)) { printf("warning: Failed to print complete stack trace!\n"); } - if(symbolsLoaded) + if (symbolsLoaded) { BOOL result = SymCleanup(process); - if(result == FALSE) + if (result == FALSE) { SLANG_EXAMPLE_LOG_ERROR(GetLastError()); }