From 127243ca9f98bf4cf8a4bdecd768bd08ebdf6577 Mon Sep 17 00:00:00 2001 From: Arkadiusz Piekarz Date: Sat, 14 Apr 2018 10:09:55 +0200 Subject: [PATCH 1/5] Fix compilation on GCC with -Wold-style-cast by using static_cast and reinterpret_cast --- backward.hpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/backward.hpp b/backward.hpp index 237eb32..3bf648c 100644 --- a/backward.hpp +++ b/backward.hpp @@ -631,11 +631,11 @@ class StackTraceImplBase { void load_thread_info() { #ifdef BACKWARD_SYSTEM_LINUX #ifndef __ANDROID__ - _thread_id = (size_t)syscall(SYS_gettid); + _thread_id = static_cast(syscall(SYS_gettid)); #else - _thread_id = (size_t)gettid(); + _thread_id = static_cast(gettid()); #endif - if (_thread_id == (size_t) getpid()) { + if (_thread_id == static_cast(getpid())) { // If the thread is the main one, let's hide that. // I like to keep little secret sometimes. _thread_id = 0; @@ -701,7 +701,7 @@ class Unwinder { static _Unwind_Reason_Code backtrace_trampoline( _Unwind_Context* ctx, void *self) { - return ((Unwinder*)self)->backtrace(ctx); + return (static_cast(self))->backtrace(ctx); } _Unwind_Reason_Code backtrace(_Unwind_Context* ctx) { @@ -721,7 +721,7 @@ class Unwinder { } if (_index >= 0) { // ignore first frame. - (*_f)(_index, (void*)ip); + (*_f)(_index, reinterpret_cast(ip)); } _index += 1; return _URC_NO_REASON; @@ -911,7 +911,7 @@ class TraceResolverLinuxImpl: if(len < 0) { return ""; } - if ((size_t)len == path.size()) { + if (static_cast(len) == path.size()) { path.resize(path.size() * 2); } else { @@ -1163,7 +1163,7 @@ class TraceResolverLinuxImpl: if (symtab_storage_size > 0) { symtab.reset( - (bfd_symbol**) malloc(symtab_storage_size) + static_cast(malloc(symtab_storage_size)) ); symcount = bfd_canonicalize_symtab( bfd_handle.get(), symtab.get() @@ -1172,7 +1172,7 @@ class TraceResolverLinuxImpl: if (dyn_symtab_storage_size > 0) { dynamic_symtab.reset( - (bfd_symbol**) malloc(dyn_symtab_storage_size) + static_cast(malloc(dyn_symtab_storage_size)) ); dyn_symcount = bfd_canonicalize_dynamic_symtab( bfd_handle.get(), dynamic_symtab.get() @@ -1214,7 +1214,7 @@ class TraceResolverLinuxImpl: context.base_addr = base_addr; context.result.found = false; bfd_map_over_sections(fobj.handle.get(), &find_in_section_trampoline, - (void*)&context); + static_cast(&context)); return context.result; } @@ -3665,7 +3665,7 @@ class SignalHandling { bool success = true; const size_t stack_size = 1024 * 1024 * 8; - _stack_content.reset((char*)malloc(stack_size)); + _stack_content.reset(static_cast(malloc(stack_size))); if (_stack_content) { stack_t ss; ss.ss_sp = _stack_content.get(); @@ -3697,7 +3697,7 @@ class SignalHandling { bool loaded() const { return _loaded; } static void handleSignal(int, siginfo_t* info, void* _ctx) { - ucontext_t *uctx = (ucontext_t*) _ctx; + ucontext_t *uctx = static_cast(_ctx); StackTrace st; void* error_addr = 0; From f8b6d76dd2476e89ed62a6f20722123ae39e3e5e Mon Sep 17 00:00:00 2001 From: Arkadiusz Piekarz Date: Sat, 14 Apr 2018 10:49:58 +0200 Subject: [PATCH 2/5] Fix compilation on GCC with -Wsign-conversion by using static_cast --- backward.hpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/backward.hpp b/backward.hpp index 3bf648c..0f28287 100644 --- a/backward.hpp +++ b/backward.hpp @@ -691,7 +691,7 @@ class Unwinder { _index = -1; _depth = depth; _Unwind_Backtrace(&this->backtrace_trampoline, this); - return _index; + return static_cast(_index); } private: @@ -721,7 +721,7 @@ class Unwinder { } if (_index >= 0) { // ignore first frame. - (*_f)(_index, reinterpret_cast(ip)); + (*_f)(static_cast(_index), reinterpret_cast(ip)); } _index += 1; return _URC_NO_REASON; @@ -915,7 +915,7 @@ class TraceResolverLinuxImpl: path.resize(path.size() * 2); } else { - path.resize(len); + path.resize(static_cast(len)); break; } } @@ -1163,7 +1163,7 @@ class TraceResolverLinuxImpl: if (symtab_storage_size > 0) { symtab.reset( - static_cast(malloc(symtab_storage_size)) + static_cast(malloc(static_cast(symtab_storage_size))) ); symcount = bfd_canonicalize_symtab( bfd_handle.get(), symtab.get() @@ -1172,7 +1172,7 @@ class TraceResolverLinuxImpl: if (dyn_symtab_storage_size > 0) { dynamic_symtab.reset( - static_cast(malloc(dyn_symtab_storage_size)) + static_cast(malloc(static_cast(dyn_symtab_storage_size))) ); dyn_symcount = bfd_canonicalize_dynamic_symtab( bfd_handle.get(), dynamic_symtab.get() @@ -3370,7 +3370,7 @@ class cfile_streambuf: public std::streambuf { } std::streamsize xsputn(const char_type* s, std::streamsize count) override { - return fwrite(s, sizeof *s, count, sink); + return static_cast(fwrite(s, sizeof *s, static_cast(count), sink)); } #ifdef BACKWARD_ATLEAST_CXX11 @@ -3594,7 +3594,7 @@ class Printer { typedef SnippetFactory::lines_t lines_t; lines_t lines = _snippets.get_snippet(source_loc.filename, - source_loc.line, context_size); + source_loc.line, static_cast(context_size)); for (lines_t::const_iterator it = lines.begin(); it != lines.end(); ++it) { @@ -3681,7 +3681,7 @@ class SignalHandling { for (size_t i = 0; i < posix_signals.size(); ++i) { struct sigaction action; memset(&action, 0, sizeof action); - action.sa_flags = (SA_SIGINFO | SA_ONSTACK | SA_NODEFER | + action.sa_flags = static_cast(SA_SIGINFO | SA_ONSTACK | SA_NODEFER | SA_RESETHAND); sigfillset(&action.sa_mask); sigdelset(&action.sa_mask, posix_signals[i]); From 9e58734de9026e50f88b389f26d92d5f75be4e79 Mon Sep 17 00:00:00 2001 From: Arkadiusz Piekarz Date: Sat, 14 Apr 2018 11:31:46 +0200 Subject: [PATCH 3/5] Fix compilation on GCC with -Wzero-as-null-pointer-constant by using nullptr --- backward.hpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/backward.hpp b/backward.hpp index 0f28287..cd27b31 100644 --- a/backward.hpp +++ b/backward.hpp @@ -335,6 +335,7 @@ extern "C" uintptr_t _Unwind_GetIPInfo(_Unwind_Context*, int*); } // namespace details } // namespace backward #else // NOT BACKWARD_ATLEAST_CXX11 +# define nullptr NULL # define override # include namespace backward { @@ -472,7 +473,7 @@ class handle { } operator const dummy*() const { if (_empty) { - return 0; + return nullptr; } return reinterpret_cast(_val); } @@ -523,7 +524,7 @@ struct demangler_impl { std::string demangle(const char* funcname) { using namespace details; char* result = abi::__cxa_demangle(funcname, - _demangle_buffer.release(), &_demangle_buffer_length, 0); + _demangle_buffer.release(), &_demangle_buffer_length, nullptr); if(result) { _demangle_buffer.reset(result); return result; @@ -550,7 +551,7 @@ struct Trace { size_t idx; Trace(): - addr(0), idx(0) {} + addr(nullptr), idx(0) {} explicit Trace(void* _addr, size_t _idx): addr(_addr), idx(_idx) {} @@ -671,7 +672,7 @@ class StackTraceImplHolder: public StackTraceImplBase { if (size()) { return &_stacktrace[skip_n_firsts()]; } - return 0; + return nullptr; } protected: @@ -3259,7 +3260,7 @@ class SourceFile { } #ifdef BACKWARD_ATLEAST_CXX11 - SourceFile(SourceFile&& from): _file(0) { + SourceFile(SourceFile&& from): _file(nullptr) { swap(from); } SourceFile& operator=(SourceFile&& from) { @@ -3616,7 +3617,7 @@ class Printer { void print_source_loc(std::ostream& os, const char* indent, const ResolvedTrace::SourceLoc& source_loc, - void* addr=0) { + void* addr=nullptr) { os << indent << "Source \"" << source_loc.filename @@ -3625,7 +3626,7 @@ class Printer { << ", in " << source_loc.function; - if (address && addr != 0) { + if (address && addr != nullptr) { os << " [" << addr << "]"; } os << "\n"; @@ -3671,7 +3672,7 @@ class SignalHandling { ss.ss_sp = _stack_content.get(); ss.ss_size = stack_size; ss.ss_flags = 0; - if (sigaltstack(&ss, 0) < 0) { + if (sigaltstack(&ss, nullptr) < 0) { success = false; } } else { @@ -3687,7 +3688,7 @@ class SignalHandling { sigdelset(&action.sa_mask, posix_signals[i]); action.sa_sigaction = &sig_handler; - int r = sigaction(posix_signals[i], &action, 0); + int r = sigaction(posix_signals[i], &action, nullptr); if (r < 0) success = false; } @@ -3700,7 +3701,7 @@ class SignalHandling { ucontext_t *uctx = static_cast(_ctx); StackTrace st; - void* error_addr = 0; + void* error_addr = nullptr; #ifdef REG_RIP // x86_64 error_addr = reinterpret_cast(uctx->uc_mcontext.gregs[REG_RIP]); #elif defined(REG_EIP) // x86_32 @@ -3731,7 +3732,7 @@ class SignalHandling { printer.print(st, stderr); #if _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L - psiginfo(info, 0); + psiginfo(info, nullptr); #else (void)info; #endif From 30fc52e00274009bc268ed518ee65a7b0ae1e582 Mon Sep 17 00:00:00 2001 From: Arkadiusz Piekarz Date: Sun, 6 May 2018 00:11:08 +0200 Subject: [PATCH 4/5] Silence -Wzero-as-null-pointer-constant with libbfd on Clang --- backward.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/backward.hpp b/backward.hpp index cd27b31..58c28e0 100644 --- a/backward.hpp +++ b/backward.hpp @@ -1250,6 +1250,8 @@ class TraceResolverLinuxImpl: } } +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" if (!result.found && fobj.symtab) { result.found = bfd_find_nearest_line(fobj.handle.get(), section, fobj.symtab.get(), addr - sec_addr, &result.filename, @@ -1261,6 +1263,7 @@ class TraceResolverLinuxImpl: fobj.dynamic_symtab.get(), addr - sec_addr, &result.filename, &result.funcname, &result.line); } +#pragma clang diagnostic pop } From 260c82ad082edc1d058a6e7cad3666f110da475c Mon Sep 17 00:00:00 2001 From: Arkadiusz Piekarz Date: Sun, 6 May 2018 00:15:07 +0200 Subject: [PATCH 5/5] Silence -Wdisabled-macro-expansion on Clang --- backward.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/backward.hpp b/backward.hpp index 58c28e0..4c1b60b 100644 --- a/backward.hpp +++ b/backward.hpp @@ -3689,7 +3689,10 @@ class SignalHandling { SA_RESETHAND); sigfillset(&action.sa_mask); sigdelset(&action.sa_mask, posix_signals[i]); +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdisabled-macro-expansion" action.sa_sigaction = &sig_handler; +#pragma clang diagnostic pop int r = sigaction(posix_signals[i], &action, nullptr); if (r < 0) success = false;