diff options
| author | Christian Blichmann <cblichmann@google.com> | 2022-10-20 12:07:38 +0200 |
|---|---|---|
| committer | Peter LaFosse <peter@vector35.com> | 2022-11-10 16:51:49 -0500 |
| commit | c47424d39b9b2f782adc1abf2e2e9090fc2ec4e6 (patch) | |
| tree | ecfdfb4fdf01d6188911a5f1188e5c542beb2ebf | |
| parent | fc3a056c61845fa48dacbde967ddb5cc98c06392 (diff) | |
Introduce a macro to implement `printf` attribute
This lets us remove a lot of `#ifdef __GNUC__` in function declarations,
which makes them more readable.
| -rw-r--r-- | binaryninjaapi.h | 28 | ||||
| -rw-r--r-- | binaryninjacore.h | 64 |
2 files changed, 37 insertions, 55 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 0488803e..a6a98da2 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -516,9 +516,7 @@ namespace BinaryNinja { \param fmt C-style format string. \param ... Variable arguments corresponding to the format string. */ -#ifdef __GNUC__ - __attribute__((format(printf, 2, 3))) -#endif + BN_PRINTF_ATTRIBUTE(2, 3) void Log(BNLogLevel level, const char* fmt, ...); /*! LogTrace only writes text to the error console if the console is set to log level: DebugLog @@ -529,9 +527,7 @@ namespace BinaryNinja { \param fmt C-style format string. \param ... Variable arguments corresponding to the format string. */ -#ifdef __GNUC__ - __attribute__((format(printf, 1, 2))) -#endif + BN_PRINTF_ATTRIBUTE(1, 2) void LogTrace(const char* fmt, ...); @@ -543,9 +539,7 @@ namespace BinaryNinja { \param fmt C-style format string. \param ... Variable arguments corresponding to the format string. */ -#ifdef __GNUC__ - __attribute__((format(printf, 1, 2))) -#endif + BN_PRINTF_ATTRIBUTE(1, 2) void LogDebug(const char* fmt, ...); /*! LogInfo always writes text to the error console, and corresponds to the log level: InfoLog. @@ -556,9 +550,7 @@ namespace BinaryNinja { \param fmt C-style format string. \param ... Variable arguments corresponding to the format string. */ -#ifdef __GNUC__ - __attribute__((format(printf, 1, 2))) -#endif + BN_PRINTF_ATTRIBUTE(1, 2) void LogInfo(const char* fmt, ...); /*! LogWarn writes text to the error console including a warning icon, @@ -569,9 +561,7 @@ namespace BinaryNinja { \param fmt C-style format string. \param ... Variable arguments corresponding to the format string. */ -#ifdef __GNUC__ - __attribute__((format(printf, 1, 2))) -#endif + BN_PRINTF_ATTRIBUTE(1, 2) void LogWarn(const char* fmt, ...); /*! LogError writes text to the error console and pops up the error console. Additionall, @@ -582,9 +572,7 @@ namespace BinaryNinja { \param fmt C-style format string. \param ... Variable arguments corresponding to the format string. */ -#ifdef __GNUC__ - __attribute__((format(printf, 1, 2))) -#endif + BN_PRINTF_ATTRIBUTE(1, 2) void LogError(const char* fmt, ...); /*! LogAlert pops up a message box displaying the alert message and logs to the error console. @@ -595,9 +583,7 @@ namespace BinaryNinja { \param fmt C-style format string. \param ... Variable arguments corresponding to the format string. */ -#ifdef __GNUC__ - __attribute__((format(printf, 1, 2))) -#endif + BN_PRINTF_ATTRIBUTE(1, 2) void LogAlert(const char* fmt, ...); /*! Redirects the minimum level passed to standard out diff --git a/binaryninjacore.h b/binaryninjacore.h index 8fee4b80..b6b76d49 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -145,6 +145,20 @@ #endif +#ifdef __has_attribute + #define BN_HAVE_ATTRIBUTE(x) __has_attribute(x) +#else + #define BN_HAVE_ATTRIBUTE(x) 0 +#endif + +#if BN_HAVE_ATTRIBUTE(format) || (defined(__GNUC__) && !defined(__clang__)) + #define BN_PRINTF_ATTRIBUTE(string_index, first_to_check) \ + __attribute__((format(__printf__, string_index, first_to_check))) +#else + #define BN_PRINTF_ATTRIBUTE(string_index, first_to_check) +#endif + + #ifdef __cplusplus extern "C" { @@ -2982,51 +2996,33 @@ extern "C" BINARYNINJACOREAPI void BNAddOptionalPluginDependency(const char* name); // Logging -#ifdef __GNUC__ - __attribute__((format(printf, 5, 6))) -#endif - BINARYNINJACOREAPI void - BNLog(size_t session, BNLogLevel level, const char* logger_name, size_t tid, const char* fmt, ...); + BN_PRINTF_ATTRIBUTE(5, 6) + BINARYNINJACOREAPI void BNLog( + size_t session, BNLogLevel level, const char* logger_name, size_t tid, const char* fmt, ...); -#ifdef __GNUC__ - __attribute__((format(printf, 1, 2))) -#endif - BINARYNINJACOREAPI void - BNLogDebug(const char* fmt, ...); + BN_PRINTF_ATTRIBUTE(1, 2) + BINARYNINJACOREAPI void BNLogDebug(const char* fmt, ...); -#ifdef __GNUC__ - __attribute__((format(printf, 1, 2))) -#endif - BINARYNINJACOREAPI void - BNLogInfo(const char* fmt, ...); + BN_PRINTF_ATTRIBUTE(1, 2) + BINARYNINJACOREAPI void BNLogInfo(const char* fmt, ...); -#ifdef __GNUC__ - __attribute__((format(printf, 1, 2))) -#endif - BINARYNINJACOREAPI void - BNLogWarn(const char* fmt, ...); + BN_PRINTF_ATTRIBUTE(1, 2) + BINARYNINJACOREAPI void BNLogWarn(const char* fmt, ...); -#ifdef __GNUC__ - __attribute__((format(printf, 1, 2))) -#endif - BINARYNINJACOREAPI void - BNLogError(const char* fmt, ...); + BN_PRINTF_ATTRIBUTE(1, 2) + BINARYNINJACOREAPI void BNLogError(const char* fmt, ...); -#ifdef __GNUC__ - __attribute__((format(printf, 1, 2))) -#endif - BINARYNINJACOREAPI void - BNLogAlert(const char* fmt, ...); + BN_PRINTF_ATTRIBUTE(1, 2) + BINARYNINJACOREAPI void BNLogAlert(const char* fmt, ...); - BINARYNINJACOREAPI void BNLogString(size_t session, BNLogLevel level, const char* logger_name, size_t tid, const char* str); + BINARYNINJACOREAPI void BNLogString( + size_t session, BNLogLevel level, const char* logger_name, size_t tid, const char* str); BINARYNINJACOREAPI BNLogger* BNNewLoggerReference(BNLogger* logger); BINARYNINJACOREAPI void BNFreeLogger(BNLogger* logger); -#ifdef __GNUC__ - __attribute__((format(printf, 3, 4))) -#endif + BN_PRINTF_ATTRIBUTE(3, 4) BINARYNINJACOREAPI void BNLoggerLog(BNLogger* logger, BNLogLevel level, const char* fmt, ...); BINARYNINJACOREAPI void BNLoggerLogString(BNLogger* logger, BNLogLevel level, const char* msg); |
