1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-31 20:16:57 +00:00

refactor: remove varargs from TR_ASSERT_MSG() (#2861)

This commit is contained in:
Charles Kerr 2022-04-01 18:00:51 -05:00 committed by GitHub
parent bfd7797c5e
commit 33cbe33229
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 27 additions and 47 deletions

View file

@ -218,12 +218,7 @@ void tr_bitfield::freeArray()
void tr_bitfield::setTrueCount(size_t n)
{
TR_ASSERT_MSG(
bit_count_ == 0 || n <= bit_count_,
"bit_count_:%zu, n:%zu, std::size(flags_):%zu",
bit_count_,
n,
size_t(std::size(flags_)));
TR_ASSERT(bit_count_ == 0 || n <= bit_count_);
true_count_ = n;
have_all_hint_ = n == bit_count_;

View file

@ -582,7 +582,7 @@ tr_sys_file_t tr_sys_file_get_std(tr_std_sys_file_t std_file, tr_error** error)
break;
default:
TR_ASSERT_MSG(false, "unknown standard file %d", (int)std_file);
TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unknown standard file {:d}"), std_file));
set_system_error(error, EINVAL);
}

View file

@ -12,7 +12,7 @@
#include <shlobj.h> /* SHCreateDirectoryEx() */
#include <winioctl.h> /* FSCTL_SET_SPARSE */
#include <fmt/core.h>
#include <fmt/format.h>
#include "transmission.h"
#include "crypto-utils.h" /* tr_rand_int() */
@ -894,7 +894,7 @@ tr_sys_file_t tr_sys_file_get_std(tr_std_sys_file_t std_file, tr_error** error)
break;
default:
TR_ASSERT_MSG(false, "unknown standard file %d", (int)std_file);
TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unknown standard file {:d}"), std_file));
set_system_error(error, ERROR_INVALID_PARAMETER);
return TR_BAD_SYS_FILE;
}

View file

@ -12,7 +12,7 @@
#include <event2/buffer.h>
#include <event2/event.h>
#include <fmt/core.h>
#include <fmt/format.h>
#include "transmission.h"
#include "clients.h"
@ -1055,7 +1055,7 @@ static ReadState canRead(tr_peerIo* io, void* vhandshake, size_t* piece)
default:
#ifdef TR_ENABLE_ASSERTS
TR_ASSERT_MSG(false, "unhandled handshake state %d", (int)handshake->state);
TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unhandled handshake state {:d}"), handshake->state));
#else
ret = READ_ERR;
break;

View file

@ -456,7 +456,7 @@ void tr_netClosePeerSocket(tr_session* session, tr_peer_socket socket)
#endif
default:
TR_ASSERT_MSG(false, "unsupported peer socket type %d", socket.type);
TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unsupported peer socket type {:d}"), socket.type));
}
}

View file

@ -15,8 +15,7 @@
#include <libutp/utp.h>
#include <fmt/core.h>
#include <fmt/format.h> // fmt::ptr
#include <fmt/format.h>
#include "transmission.h"
#include "session.h"
@ -648,7 +647,7 @@ static tr_peerIo* tr_peerIoNew(
#endif
default:
TR_ASSERT_MSG(false, "unsupported peer socket type %d", socket.type);
TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unsupported peer socket type {:d}"), socket.type));
}
return io;
@ -1190,7 +1189,7 @@ void tr_peerIoReadBytes(tr_peerIo* io, struct evbuffer* inbuf, void* bytes, size
break;
default:
TR_ASSERT_MSG(false, "unhandled encryption type %d", (int)io->encryption_type);
TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unhandled encryption type {:d}"), io->encryption_type));
}
}

View file

@ -16,7 +16,7 @@
#include <event2/event.h>
#include <fmt/core.h>
#include <fmt/format.h>
#define LIBTRANSMISSION_PEER_MODULE
#include "transmission.h"
@ -863,7 +863,7 @@ static void peerCallbackFunc(tr_peer* peer, tr_peer_event const* e, void* vs)
break;
default:
TR_ASSERT_MSG(false, "%s", fmt::format("unhandled peer event type {}", e->eventType).c_str());
TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unhandled peer event type {:d}"), e->eventType));
}
}

View file

@ -15,6 +15,8 @@
#include <event2/bufferevent.h>
#include <event2/event.h>
#include <fmt/format.h>
#include "transmission.h"
#include "cache.h"
@ -1961,7 +1963,7 @@ static ReadState canRead(tr_peerIo* io, void* vmsgs, size_t* piece)
default:
#ifdef TR_ENABLE_ASSERTS
TR_ASSERT_MSG(false, "unhandled peer messages state %d", int(msgs->state));
TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unhandled peer messages state {:d}"), int(msgs->state)));
#else
ret = READ_ERR;
break;

View file

@ -343,7 +343,7 @@ static void append_app_launcher_arguments(enum tr_app_type app_type, char** args
break;
default:
TR_ASSERT_MSG(false, "unsupported application type %d", (int)app_type);
TR_ASSERT_MSG(false, fmt::format(FMT_STRING("unsupported application type {:d}"), app_type));
break;
}
}

View file

@ -3,25 +3,17 @@
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include "tr-assert.h"
#if !defined(NDEBUG) || defined(TR_FORCE_ASSERTIONS)
[[noreturn]] bool tr_assert_report(char const* file, int line, char const* message_fmt, ...)
[[noreturn]] bool tr_assert_report(std::string_view file, int line, std::string_view message)
{
va_list args;
va_start(args, message_fmt);
fprintf(stderr, "assertion failed: ");
vfprintf(stderr, message_fmt, args);
fprintf(stderr, " (%s:%d)\n", file, line);
va_end(args);
std::cerr << "assertion failed: " << message << " (" << file << ':' << line << ')' << std::endl;
abort();
}

View file

@ -7,14 +7,14 @@
#if !defined(NDEBUG) || defined(TR_FORCE_ASSERTIONS)
#include <stdbool.h>
#include <string_view>
#include "tr-macros.h"
[[noreturn]] bool tr_assert_report(char const* file, int line, char const* message_fmt, ...) TR_GNUC_PRINTF(3, 4);
[[noreturn]] bool tr_assert_report(std::string_view file, int line, std::string_view message);
#define TR_ASSERT(x) ((void)(TR_LIKELY(x) || tr_assert_report(__FILE__, __LINE__, "%s", #x)))
#define TR_ASSERT_MSG(x, ...) ((void)(TR_LIKELY(x) || tr_assert_report(__FILE__, __LINE__, __VA_ARGS__)))
#define TR_ASSERT(x) ((void)(TR_LIKELY(x) || tr_assert_report(__FILE__, __LINE__, #x)))
#define TR_ASSERT_MSG(x, message) ((void)(TR_LIKELY(x) || tr_assert_report(__FILE__, __LINE__, message)))
#define TR_UNREACHABLE() tr_assert_report(__FILE__, __LINE__, "Unreachable code")
#define TR_ENABLE_ASSERTS

View file

@ -5,9 +5,7 @@
#import <Foundation/Foundation.h>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <fmt/format.h>
#include "tr-assert.h"
@ -16,16 +14,10 @@
// macOS implementation of tr_assert_report() that provides the message in the crash report
// This replaces the generic implementation of the function in tr-assert.cc
[[noreturn]] bool tr_assert_report(char const* file, int line, char const* message_fmt, ...)
[[noreturn]] bool tr_assert_report(std::string_view file, int line, std::string_view message)
{
char buffer[1024];
va_list args;
va_start(args, message_fmt);
vsnprintf(buffer, sizeof(buffer), message_fmt, args);
va_end(args);
[NSException raise:NSInternalInconsistencyException format:@"assertion failed: %s (%s:%d)", buffer, file, line];
auto const full_text = fmt::format(FMT_STRING("assertion failed: {:s} ({:s}:{:d})"), message, file, line);
[NSException raise:NSInternalInconsistencyException format:@"%s", full_text.c_str()];
// We should not reach this anyway, but it helps mark the function as propertly noreturn
// (the Objective-C NSException method does not).