diff --git a/Transmission.xcodeproj/project.pbxproj b/Transmission.xcodeproj/project.pbxproj index 7d3a6358d..6f12fd3ac 100644 --- a/Transmission.xcodeproj/project.pbxproj +++ b/Transmission.xcodeproj/project.pbxproj @@ -347,6 +347,7 @@ C1305EBE186A13B100F03351 /* file.c in Sources */ = {isa = PBXBuildFile; fileRef = C1305EB8186A134000F03351 /* file.c */; }; C1425B351EE9C5F5001DB85F /* tr-assert.c in Sources */ = {isa = PBXBuildFile; fileRef = C1425B321EE9C5EA001DB85F /* tr-assert.c */; }; C1425B361EE9C605001DB85F /* tr-assert.h in Headers */ = {isa = PBXBuildFile; fileRef = C1425B331EE9C5EA001DB85F /* tr-assert.h */; }; + C1425B371EE9C705001DB85F /* tr-macros.h in Headers */ = {isa = PBXBuildFile; fileRef = C1425B341EE9C5EA001DB85F /* tr-macros.h */; }; C1639A741A55F4E000E42033 /* libb64.a in Frameworks */ = {isa = PBXBuildFile; fileRef = C1639A6F1A55F4D600E42033 /* libb64.a */; }; C1639A781A55F56600E42033 /* cdecode.c in Sources */ = {isa = PBXBuildFile; fileRef = C1639A761A55F56600E42033 /* cdecode.c */; }; C1639A791A55F56600E42033 /* cencode.c in Sources */ = {isa = PBXBuildFile; fileRef = C1639A771A55F56600E42033 /* cencode.c */; }; @@ -990,6 +991,7 @@ C1305EB8186A134000F03351 /* file.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; name = file.c; path = libtransmission/file.c; sourceTree = ""; }; C1425B321EE9C5EA001DB85F /* tr-assert.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = "tr-assert.c"; path = "libtransmission/tr-assert.c"; sourceTree = ""; }; C1425B331EE9C5EA001DB85F /* tr-assert.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "tr-assert.h"; path = "libtransmission/tr-assert.h"; sourceTree = ""; }; + C1425B341EE9C5EA001DB85F /* tr-macros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "tr-macros.h"; path = "libtransmission/tr-macros.h"; sourceTree = ""; }; C1639A6F1A55F4D600E42033 /* libb64.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libb64.a; sourceTree = BUILT_PRODUCTS_DIR; }; C1639A761A55F56600E42033 /* cdecode.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; name = cdecode.c; path = "third-party/libb64/src/cdecode.c"; sourceTree = ""; }; C1639A771A55F56600E42033 /* cencode.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; name = cencode.c; path = "third-party/libb64/src/cencode.c"; sourceTree = ""; }; @@ -1361,6 +1363,7 @@ C1425B331EE9C5EA001DB85F /* tr-assert.h */, A22CFCA60FC24ED80009BD3E /* tr-dht.c */, A22CFCA70FC24ED80009BD3E /* tr-dht.h */, + C1425B341EE9C5EA001DB85F /* tr-macros.h */, A284214212DA663E00FBDDBB /* tr-udp.c */, A284214312DA663E00FBDDBB /* tr-udp.h */, A2679292130E00A000CB7464 /* tr-utp.c */, @@ -1781,6 +1784,7 @@ BEFC1E350C07861A00B0BB3C /* port-forwarding.h in Headers */, BEFC1E3B0C07861A00B0BB3C /* platform.h in Headers */, C1425B361EE9C605001DB85F /* tr-assert.h in Headers */, + C1425B371EE9C705001DB85F /* tr-macros.h in Headers */, BEFC1E450C07861A00B0BB3C /* net.h in Headers */, BEFC1E490C07861A00B0BB3C /* metainfo.h in Headers */, BEFC1E4D0C07861A00B0BB3C /* session.h in Headers */, diff --git a/gtk/actions.c b/gtk/actions.c index 5c71046b2..864ad1d88 100644 --- a/gtk/actions.c +++ b/gtk/actions.c @@ -18,8 +18,6 @@ #include "tr-core.h" #include "tr-prefs.h" -#define UNUSED G_GNUC_UNUSED - static TrCore* myCore = NULL; static GtkActionGroup* myGroup = NULL; diff --git a/gtk/util.h b/gtk/util.h index 517779f22..ba0383a63 100644 --- a/gtk/util.h +++ b/gtk/util.h @@ -53,11 +53,6 @@ extern char const* speed_T_str; #endif -/* macro to shut up "unused parameter" warnings */ -#ifndef UNUSED -#define UNUSED G_GNUC_UNUSED -#endif - enum { GTR_UNICODE_UP, diff --git a/libtransmission/CMakeLists.txt b/libtransmission/CMakeLists.txt index eba345f60..8b9fe33b7 100644 --- a/libtransmission/CMakeLists.txt +++ b/libtransmission/CMakeLists.txt @@ -110,6 +110,7 @@ set(${PROJECT_NAME}_PUBLIC_HEADERS session-id.h tr-assert.h tr-getopt.h + tr-macros.h transmission.h utils.h variant.h diff --git a/libtransmission/Makefile.am b/libtransmission/Makefile.am index 01c16a7c4..f835d22b7 100644 --- a/libtransmission/Makefile.am +++ b/libtransmission/Makefile.am @@ -162,6 +162,7 @@ noinst_HEADERS = \ transmission.h \ tr-assert.h \ tr-dht.h \ + tr-macros.h \ tr-udp.h \ tr-utp.h \ tr-lpd.h \ diff --git a/libtransmission/error.h b/libtransmission/error.h index d30ff989d..dda40cb0c 100644 --- a/libtransmission/error.h +++ b/libtransmission/error.h @@ -10,19 +10,13 @@ #include +#include "tr-macros.h" + #ifdef __cplusplus extern "C" { #endif -#ifndef TR_GNUC_PRINTF -#ifdef __GNUC__ -#define TR_GNUC_PRINTF(fmt, args) __attribute__((format(printf, fmt, args))) -#else -#define TR_GNUC_PRINTF(fmt, args) -#endif -#endif - /** * @addtogroup error Error reporting * @{ diff --git a/libtransmission/session.h b/libtransmission/session.h index df548a8be..c4158f075 100644 --- a/libtransmission/session.h +++ b/libtransmission/session.h @@ -14,14 +14,6 @@ #define TR_NAME "Transmission" -#ifndef UNUSED -#ifdef __GNUC__ -#define UNUSED __attribute__((unused)) -#else -#define UNUSED -#endif -#endif - #include "bandwidth.h" #include "bitfield.h" #include "net.h" diff --git a/libtransmission/tr-assert.h b/libtransmission/tr-assert.h index 69d1ce47f..19feec786 100644 --- a/libtransmission/tr-assert.h +++ b/libtransmission/tr-assert.h @@ -12,31 +12,7 @@ #include -#ifndef TR_LIKELY -#if defined(__GNUC__) -#define TR_LIKELY(x) __builtin_expect(!!(x), true) -#else -#define TR_LIKELY(x) (x) -#endif -#endif - -#ifndef TR_NORETURN -#if defined(__GNUC__) -#define TR_NORETURN __attribute__((noreturn)) -#elif defined(_MSC_VER) -#define TR_NORETURN __declspec(noreturn) -#else -#define TR_NORETURN -#endif -#endif - -#ifndef TR_GNUC_PRINTF -#if defined(__GNUC__) -#define TR_GNUC_PRINTF(fmt, args) __attribute__((format(printf, fmt, args))) -#else -#define TR_GNUC_PRINTF(fmt, args) -#endif -#endif +#include "tr-macros.h" bool TR_NORETURN tr_assert_report(char const* file, int line, char const* message_fmt, ...) TR_GNUC_PRINTF(3, 4); diff --git a/libtransmission/tr-getopt.c b/libtransmission/tr-getopt.c index f26e585cb..a41b67a82 100644 --- a/libtransmission/tr-getopt.c +++ b/libtransmission/tr-getopt.c @@ -12,10 +12,7 @@ #include #include "tr-getopt.h" - -#ifndef MAX -#define MAX(a, b) (((a) > (b)) ? (a) : (b)) -#endif +#include "tr-macros.h" int tr_optind = 1; diff --git a/libtransmission/tr-macros.h b/libtransmission/tr-macros.h new file mode 100644 index 000000000..44cc0dc07 --- /dev/null +++ b/libtransmission/tr-macros.h @@ -0,0 +1,160 @@ +/* + * This file Copyright (C) 2017 Mnemosyne LLC + * + * It may be used under the GNU GPL versions 2 or 3 + * or any future license endorsed by Mnemosyne LLC. + * + */ + +#pragma once + +/*** +**** +***/ + +#ifndef __has_feature +#define __has_feature(x) 0 +#endif + +#ifndef __has_extension +#define __has_extension __has_feature +#endif + +#ifndef __has_attribute +#define __has_attribute(x) 0 +#endif + +#ifndef __has_builtin +#define __has_builtin(x) 0 +#endif + +#ifdef __GNUC__ +#define TR_GNUC_CHECK_VERSION(major, minor) \ + (__GNUC__ > (major) || \ + (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) +#else +#define TR_GNUC_CHECK_VERSION(major, minor) 0 +#endif + +#ifdef __UCLIBC__ +#define TR_UCLIBC_CHECK_VERSION(major, minor, micro) \ + (__UCLIBC_MAJOR__ > (major) || \ + (__UCLIBC_MAJOR__ == (major) && __UCLIBC_MINOR__ > (minor)) || \ + (__UCLIBC_MAJOR__ == (major) && __UCLIBC_MINOR__ == (minor) && __UCLIBC_SUBLEVEL__ >= (micro))) +#else +#define TR_UCLIBC_CHECK_VERSION(major, minor, micro) 0 +#endif + +/*** +**** +***/ + +#ifndef UNUSED +#if __has_attribute(__unused__) || TR_GNUC_CHECK_VERSION(2, 7) +#define UNUSED __attribute__((__unused__)) +#else +#define UNUSED +#endif +#endif + +/*** +**** +***/ + +#if __has_builtin(__builtin_expect) || TR_GNUC_CHECK_VERSION(3, 0) +#define TR_LIKELY(x) __builtin_expect(!!(x), 1) +#define TR_UNLIKELY(x) __builtin_expect(!!(x), 0) +#else +#define TR_LIKELY(x) (x) +#define TR_UNLIKELY(x) (x) +#endif + +/*** +**** +***/ + +#if __has_attribute(__noreturn__) || TR_GNUC_CHECK_VERSION(2, 5) +#define TR_NORETURN __attribute__((__noreturn__)) +#elif defined(_MSC_VER) +#define TR_NORETURN __declspec(noreturn) +#else +#define TR_NORETURN +#endif + +#if __has_attribute(__deprecated__) || TR_GNUC_CHECK_VERSION(3, 1) +#define TR_DEPRECATED __attribute__((__deprecated__)) +#elif defined(_MSC_VER) +#define TR_DEPRECATED __declspec(deprecated) +#else +#define TR_DEPRECATED +#endif + +#if __has_attribute(__format__) || TR_GNUC_CHECK_VERSION(2, 3) +#define TR_GNUC_PRINTF(fmt, args) __attribute__((__format__(printf, fmt, args))) +#else +#define TR_GNUC_PRINTF(fmt, args) +#endif + +#if __has_attribute(__nonnull__) || TR_GNUC_CHECK_VERSION(3, 3) +#define TR_GNUC_NONNULL(...) __attribute__((__nonnull__(__VA_ARGS__))) +#else +#define TR_GNUC_NONNULL(...) +#endif + +#if __has_attribute(__sentinel__) || TR_GNUC_CHECK_VERSION(4, 3) +#define TR_GNUC_NULL_TERMINATED __attribute__((__sentinel__)) +#else +#define TR_GNUC_NULL_TERMINATED +#endif + +#if __has_attribute(__hot__) || TR_GNUC_CHECK_VERSION(4, 3) +#define TR_GNUC_HOT __attribute__((__hot__)) +#else +#define TR_GNUC_HOT +#endif + +#if __has_attribute(__malloc__) || TR_GNUC_CHECK_VERSION(2, 96) +#define TR_GNUC_MALLOC __attribute__((__malloc__)) +#else +#define TR_GNUC_MALLOC +#endif + +/*** +**** +***/ + +/** + * @def TR_STATIC_ASSERT + * @brief This helper allows to perform static checks at compile time + */ +#if defined(static_assert) +#define TR_STATIC_ASSERT static_assert +#elif __has_feature(c_static_assert) || __has_extension(c_static_assert) +#define TR_STATIC_ASSERT _Static_assert +#else +#define TR_STATIC_ASSERT(x, msg) \ + { \ + typedef char __tr_static_check__[(x) ? 1 : -1] UNUSED; \ + } +#endif + +/* Sometimes the system defines MAX/MIN, sometimes not. + In the latter case, define those here since we will use them */ + +#ifndef MAX +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#endif + +#ifndef MIN +#define MIN(a, b) ((a) > (b) ? (b) : (a)) +#endif + +/*** +**** +***/ + +#define SHA_DIGEST_LENGTH 20 + +#define TR_INET6_ADDRSTRLEN 46 + +#define TR_BAD_SIZE ((size_t)-1) diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index 57ed77be5..ac1871adb 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -19,28 +19,18 @@ extern "C" { #endif -#if defined(__GNUC__) -#define TR_DEPRECATED __attribute__((deprecated)) -#elif defined(_MSC_VER) -#define TR_DEPRECATED __declspec(deprecated) -#else -#define TR_DEPRECATED -#endif - /*** **** **** Basic Types **** ***/ -#include /* uintN_t */ #include /* bool */ +#include /* size_t */ +#include /* uintN_t */ #include /* time_t */ -#define SHA_DIGEST_LENGTH 20 -#define TR_INET6_ADDRSTRLEN 46 - -#define TR_BAD_SIZE ((size_t)-1) +#include "tr-macros.h" typedef uint32_t tr_file_index_t; typedef uint32_t tr_piece_index_t; diff --git a/libtransmission/utils.h b/libtransmission/utils.h index 93bcf7ae1..628684264 100644 --- a/libtransmission/utils.h +++ b/libtransmission/utils.h @@ -33,81 +33,6 @@ struct tr_error; * @{ */ -#ifndef UNUSED -#ifdef __GNUC__ -#define UNUSED __attribute__((unused)) -#else -#define UNUSED -#endif -#endif - -#ifndef TR_GNUC_PRINTF -#ifdef __GNUC__ -#define TR_GNUC_PRINTF(fmt, args) __attribute__((format(printf, fmt, args))) -#else -#define TR_GNUC_PRINTF(fmt, args) -#endif -#endif - -#ifndef TR_GNUC_NONNULL -#ifdef __GNUC__ -#define TR_GNUC_NONNULL(...) __attribute__((nonnull(__VA_ARGS__))) -#else -#define TR_GNUC_NONNULL(...) -#endif -#endif - -#ifndef TR_GNUC_NULL_TERMINATED -#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) -#define TR_GNUC_NULL_TERMINATED __attribute__((__sentinel__)) -#define TR_GNUC_HOT __attribute((hot)) -#else -#define TR_GNUC_NULL_TERMINATED -#define TR_GNUC_HOT -#endif -#endif - -#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 96) -#define TR_GNUC_MALLOC __attribute__((__malloc__)) -#else -#define TR_GNUC_MALLOC -#endif - -#ifndef __has_feature -#define __has_feature(x) 0 -#endif -#ifndef __has_extension -#define __has_extension __has_feature -#endif - -#ifdef __UCLIBC__ -#define TR_UCLIBC_CHECK_VERSION(major, minor, micro) \ - (__UCLIBC_MAJOR__ > (major) || \ - (__UCLIBC_MAJOR__ == (major) && __UCLIBC_MINOR__ > (minor)) || \ - (__UCLIBC_MAJOR__ == (major) && __UCLIBC_MINOR__ == (minor) && __UCLIBC_SUBLEVEL__ >= (micro))) -#else -#define TR_UCLIBC_CHECK_VERSION(major, minor, micro) 0 -#endif - -/** - * @def TR_STATIC_ASSERT - * @brief This helper allows to perform static checks at compile time - */ -#if defined(static_assert) -#define TR_STATIC_ASSERT static_assert -#elif __has_feature(c_static_assert) || __has_extension(c_static_assert) -#define TR_STATIC_ASSERT _Static_assert -#else -#define TR_STATIC_ASSERT(x, msg) \ - { \ - typedef char __tr_static_check__[(x) ? 1 : -1] UNUSED; \ - } -#endif - -/*** -**** -***/ - char const* tr_strip_positional_args(char const* fmt); #if !defined(_) @@ -217,19 +142,6 @@ int tr_main_win32(int argc, char** argv, int (* real_main)(int, char**)); **** ***/ -/* Sometimes the system defines MAX/MIN, sometimes not. - In the latter case, define those here since we will use them */ -#ifndef MAX -#define MAX(a, b) ((a) > (b) ? (a) : (b)) -#endif -#ifndef MIN -#define MIN(a, b) ((a) > (b) ? (b) : (a)) -#endif - -/*** -**** -***/ - /** @brief Portability wrapper around malloc() in which `0' is a safe argument */ void* tr_malloc(size_t size);