Skip to content

Commit fe952c0

Browse files
committed
fix: use check for definition instead of definition and its value
`#elifdef` is introduced since C++23 but we target at most, C++17 and since a lot of conditional logic relies on else statements that should only check for presence rather than evaluate its value, it's better to consistently use `#if defined(__MACRO__)` instead of `#ifdef __MACRO__` for checking platform flags until the time we bump to C++23
1 parent 4e1d37f commit fe952c0

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/stacktraces.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <memory>
1919
#include <atomic>
2020

21-
#if WIN32
21+
#if defined(WIN32)
2222
#include <windows.h>
2323
#include <dbghelp.h>
2424
#include <thread>
@@ -30,14 +30,14 @@
3030
#include <csignal>
3131
#endif
3232

33-
#if !WIN32
33+
#if !defined(WIN32)
3434
#include <dlfcn.h>
35-
#if !__APPLE__
35+
#if !defined(__APPLE__)
3636
#include <link.h>
3737
#endif
3838
#endif
3939

40-
#if __APPLE__
40+
#if defined(__APPLE__)
4141
#include <mach-o/dyld.h>
4242
#include <mach/mach_init.h>
4343
#include <sys/sysctl.h>
@@ -52,7 +52,7 @@
5252

5353
std::string DemangleSymbol(const std::string& name)
5454
{
55-
#if __GNUC__ || __clang__
55+
#if defined(__GNUC__) || defined(__clang__)
5656
int status = -4; // some arbitrary value to eliminate the compiler warning
5757
char* str = abi::__cxa_demangle(name.c_str(), nullptr, nullptr, &status);
5858
if (status != 0) {
@@ -74,7 +74,7 @@ static std::atomic<bool> skipAbortSignal(false);
7474

7575
static ssize_t GetExeFileNameImpl(char* buf, size_t bufSize)
7676
{
77-
#if WIN32
77+
#if defined(WIN32)
7878
std::vector<TCHAR> tmp(bufSize);
7979
DWORD len = GetModuleFileName(nullptr, tmp.data(), bufSize);
8080
if (len >= bufSize) {
@@ -84,7 +84,7 @@ static ssize_t GetExeFileNameImpl(char* buf, size_t bufSize)
8484
buf[i] = (char)tmp[i];
8585
}
8686
return len;
87-
#elif __APPLE__
87+
#elif defined(__APPLE__)
8888
uint32_t bufSize2 = (uint32_t)bufSize;
8989
if (_NSGetExecutablePath(buf, &bufSize2) != 0) {
9090
// it's not entirely clear if the value returned by _NSGetExecutablePath includes the null character
@@ -127,7 +127,7 @@ static void my_backtrace_error_callback (void *data, const char *msg,
127127

128128
static backtrace_state* GetLibBacktraceState()
129129
{
130-
#if WIN32
130+
#if defined(WIN32)
131131
// libbacktrace is not able to handle the DWARF debuglink in the .exe
132132
// but luckily we can just specify the .dbg file here as it's a valid PE/XCOFF file
133133
static std::string debugFileName = g_exeFileName + ".dbg";
@@ -140,7 +140,7 @@ static backtrace_state* GetLibBacktraceState()
140140
}
141141
#endif // ENABLE_STACKTRACES
142142

143-
#if WIN32
143+
#if defined(WIN32)
144144
static uint64_t GetBaseAddress()
145145
{
146146
return 0;
@@ -188,15 +188,15 @@ static __attribute__((noinline)) std::vector<uint64_t> GetStackFrames(size_t ski
188188
STACKFRAME64 stackframe;
189189
ZeroMemory(&stackframe, sizeof(STACKFRAME64));
190190

191-
#ifdef __i386__
191+
#if defined(__i386__)
192192
image = IMAGE_FILE_MACHINE_I386;
193193
stackframe.AddrPC.Offset = context.Eip;
194194
stackframe.AddrPC.Mode = AddrModeFlat;
195195
stackframe.AddrFrame.Offset = context.Ebp;
196196
stackframe.AddrFrame.Mode = AddrModeFlat;
197197
stackframe.AddrStack.Offset = context.Esp;
198198
stackframe.AddrStack.Mode = AddrModeFlat;
199-
#elif __x86_64__
199+
#elif defined(__x86_64__)
200200
image = IMAGE_FILE_MACHINE_AMD64;
201201
stackframe.AddrPC.Offset = context.Rip;
202202
stackframe.AddrPC.Mode = AddrModeFlat;
@@ -240,7 +240,7 @@ static __attribute__((noinline)) std::vector<uint64_t> GetStackFrames(size_t ski
240240
}
241241
#else
242242

243-
#if __APPLE__
243+
#if defined(__APPLE__)
244244
static uint64_t GetBaseAddress()
245245
{
246246
mach_port_name_t target_task;
@@ -534,11 +534,11 @@ static void PrintCrashInfo(const crash_info& ci)
534534
static StdMutex g_stacktraces_mutex;
535535
static std::map<void*, std::shared_ptr<std::vector<uint64_t>>> g_stacktraces;
536536

537-
#if CRASH_HOOKS_WRAPPED_CXX_ABI
537+
#ifdef CRASH_HOOKS_WRAPPED_CXX_ABI
538538
// These come in through -Wl,-wrap
539539
extern "C" void* __real___cxa_allocate_exception(size_t thrown_size);
540540
extern "C" void __real___cxa_free_exception(void * thrown_exception);
541-
#if WIN32
541+
#if defined(WIN32)
542542
extern "C" void __real__assert(const char *assertion, const char *file, unsigned int line);
543543
extern "C" void __real__wassert(const wchar_t *assertion, const wchar_t *file, unsigned int line);
544544
#else
@@ -557,13 +557,13 @@ extern "C" void __real___cxa_free_exception(void * thrown_exception)
557557
static auto f = (void(*)(void*))dlsym(RTLD_NEXT, "__cxa_free_exception");
558558
return f(thrown_exception);
559559
}
560-
#if __clang__
560+
#if defined(__clang__)
561561
extern "C" void __attribute__((noreturn)) __real___assert_rtn(const char *function, const char *file, int line, const char *assertion)
562562
{
563563
static auto f = (void(__attribute__((noreturn)) *) (const char*, const char*, int, const char*))dlsym(RTLD_NEXT, "__assert_rtn");
564564
f(function, file, line, assertion);
565565
}
566-
#elif WIN32
566+
#elif defined(WIN32)
567567
#error not supported on WIN32 (no dlsym support)
568568
#else
569569
extern "C" void __real___assert_fail(const char *assertion, const char *file, unsigned int line, const char *function)
@@ -574,7 +574,7 @@ extern "C" void __real___assert_fail(const char *assertion, const char *file, un
574574
#endif
575575
#endif
576576

577-
#if CRASH_HOOKS_WRAPPED_CXX_ABI
577+
#ifdef CRASH_HOOKS_WRAPPED_CXX_ABI
578578
#define WRAPPED_NAME(x) __wrap_##x
579579
#else
580580
#define WRAPPED_NAME(x) x
@@ -622,15 +622,15 @@ static __attribute__((noinline)) crash_info GetCrashInfoFromAssertion(const char
622622
return ci;
623623
}
624624

625-
#if __clang__
625+
#if defined(__clang__)
626626
extern "C" void __attribute__((noinline)) WRAPPED_NAME(__assert_rtn)(const char *function, const char *file, int line, const char *assertion)
627627
{
628628
auto ci = GetCrashInfoFromAssertion(assertion, file, line, function);
629629
PrintCrashInfo(ci);
630630
skipAbortSignal = true;
631631
__real___assert_rtn(function, file, line, assertion);
632632
}
633-
#elif WIN32
633+
#elif defined(WIN32)
634634
extern "C" void __attribute__((noinline)) WRAPPED_NAME(_assert)(const char *assertion, const char *file, unsigned int line)
635635
{
636636
auto ci = GetCrashInfoFromAssertion(assertion, file, line, nullptr);
@@ -760,7 +760,7 @@ void RegisterPrettyTerminateHander()
760760
std::set_terminate(terminate_handler);
761761
}
762762

763-
#if !WIN32
763+
#if !defined(WIN32)
764764
static void HandlePosixSignal(int s)
765765
{
766766
if (s == SIGABRT && skipAbortSignal) {
@@ -837,7 +837,7 @@ LONG WINAPI HandleWindowsException(EXCEPTION_POINTERS * ExceptionInfo)
837837

838838
void RegisterPrettySignalHandlers()
839839
{
840-
#if WIN32
840+
#if defined(WIN32)
841841
SetUnhandledExceptionFilter(HandleWindowsException);
842842
#else
843843
const std::vector<int> posix_signals = {
@@ -853,7 +853,7 @@ void RegisterPrettySignalHandlers()
853853
SIGTRAP, // Trace/breakpoint trap
854854
SIGXCPU, // CPU time limit exceeded (4.2BSD)
855855
SIGXFSZ, // File size limit exceeded (4.2BSD)
856-
#if __APPLE__
856+
#if defined(__APPLE__)
857857
SIGEMT, // emulation instruction executed
858858
#endif
859859
};

0 commit comments

Comments
 (0)