From a3182c31cb2bb56a530a1107326f569005c3480e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Fri, 25 Jul 2025 14:53:04 +0800 Subject: [PATCH] Chore: add FF_C_NONNULL --- src/common/io/io.h | 20 +++++++++++++++++++- src/util/FFcheckmacros.h | 20 ++++++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/common/io/io.h b/src/common/io/io.h index 4eabaed04..c783e5323 100644 --- a/src/common/io/io.h +++ b/src/common/io/io.h @@ -31,6 +31,7 @@ static inline FFNativeFD FFUnixFD2NativeFD(int unixfd) #endif } +FF_C_NONNULL(3) static inline bool ffWriteFDData(FFNativeFD fd, size_t dataSize, const void* data) { #ifndef _WIN32 @@ -41,18 +42,22 @@ static inline bool ffWriteFDData(FFNativeFD fd, size_t dataSize, const void* dat #endif } +FF_C_NONNULL(2) static inline bool ffWriteFDBuffer(FFNativeFD fd, const FFstrbuf* content) { return ffWriteFDData(fd, content->length, content->chars); } +FF_C_NONNULL(1, 3) bool ffWriteFileData(const char* fileName, size_t dataSize, const void* data); +FF_C_NONNULL(1, 2) static inline bool ffWriteFileBuffer(const char* fileName, const FFstrbuf* buffer) { return ffWriteFileData(fileName, buffer->length, buffer->chars); } +FF_C_NONNULL(3) static inline ssize_t ffReadFDData(FFNativeFD fd, size_t dataSize, void* data) { #ifndef _WIN32 @@ -66,26 +71,32 @@ static inline ssize_t ffReadFDData(FFNativeFD fd, size_t dataSize, void* data) #endif } +FF_C_NONNULL(1, 3) ssize_t ffReadFileData(const char* fileName, size_t dataSize, void* data); +FF_C_NONNULL(2, 4) ssize_t ffReadFileDataRelative(FFNativeFD dfd, const char* fileName, size_t dataSize, void* data); +FF_C_NONNULL(2) bool ffAppendFDBuffer(FFNativeFD fd, FFstrbuf* buffer); +FF_C_NONNULL(1, 2) bool ffAppendFileBuffer(const char* fileName, FFstrbuf* buffer); +FF_C_NONNULL(2, 3) bool ffAppendFileBufferRelative(FFNativeFD dfd, const char* fileName, FFstrbuf* buffer); +FF_C_NONNULL(1, 2) static inline bool ffReadFileBuffer(const char* fileName, FFstrbuf* buffer) { ffStrbufClear(buffer); return ffAppendFileBuffer(fileName, buffer); } +FF_C_NONNULL(2, 3) static inline bool ffReadFileBufferRelative(FFNativeFD dfd, const char* fileName, FFstrbuf* buffer) { ffStrbufClear(buffer); return ffAppendFileBufferRelative(dfd, fileName, buffer); } -//Bit flags, combine with | typedef enum __attribute__((__packed__)) FFPathType { FF_PATHTYPE_FILE = 1 << 0, @@ -94,6 +105,7 @@ typedef enum __attribute__((__packed__)) FFPathType FF_PATHTYPE_FORCE_UNSIGNED = UINT8_MAX, } FFPathType; +FF_C_NONNULL(1) static inline bool ffPathExists(const char* path, FFPathType pathType) { #ifdef _WIN32 @@ -136,11 +148,13 @@ static inline bool ffPathExists(const char* path, FFPathType pathType) return false; } +FF_C_NONNULL(1, 2) bool ffPathExpandEnv(const char* in, FFstrbuf* out); #define FF_IO_TERM_RESP_WAIT_MS 100 // #554 FF_C_SCANF(3, 4) +FF_C_NONNULL(1, 3) const char* ffGetTerminalResponse(const char* request, int nParams, const char* format, ...); // Not thread safe! @@ -157,6 +171,7 @@ static inline void ffUnsuppressIO(bool* suppressed) void ffListFilesRecursively(const char* path, bool pretty); +FF_C_NONNULL(1) static inline bool wrapClose(FFNativeFD* pfd) { assert(pfd); @@ -176,6 +191,7 @@ static inline bool wrapClose(FFNativeFD* pfd) } #define FF_AUTO_CLOSE_FD __attribute__((__cleanup__(wrapClose))) +FF_C_NONNULL(1) static inline bool wrapFclose(FILE** pfile) { assert(pfile); @@ -186,6 +202,7 @@ static inline bool wrapFclose(FILE** pfile) } #define FF_AUTO_CLOSE_FILE __attribute__((__cleanup__(wrapFclose))) +FF_C_NONNULL(1) #ifndef _WIN32 static inline bool wrapClosedir(DIR** pdir) { @@ -207,6 +224,7 @@ static inline bool wrapClosedir(HANDLE* pdir) #endif #define FF_AUTO_CLOSE_DIR __attribute__((__cleanup__(wrapClosedir))) +FF_C_NONNULL(1, 2, 3) static inline bool ffSearchUserConfigFile(const FFlist* configDirs, const char* fileSubpath, FFstrbuf* result) { // configDirs is a list of FFstrbufs include the trailing slash diff --git a/src/util/FFcheckmacros.h b/src/util/FFcheckmacros.h index 4a6243a72..00d34b2d3 100644 --- a/src/util/FFcheckmacros.h +++ b/src/util/FFcheckmacros.h @@ -4,22 +4,34 @@ #include #endif -#if defined(__GNUC__) || defined(__clang__) - #define FF_C_NODISCARD __attribute__((warn_unused_result)) +#if defined(__has_attribute) && __has_attribute(__warn_unused_result__) + #define FF_C_NODISCARD __attribute__((__warn_unused_result__)) #elif defined(_MSC_VER) #define FF_C_NODISCARD _Check_return_ #else #define FF_C_NODISCARD #endif -#if defined(__GNUC__) || defined(__clang__) +#if defined(__has_attribute) && __has_attribute(__format__) #define FF_C_PRINTF(formatStrIndex, argsStartIndex) __attribute__((__format__ (printf, formatStrIndex, argsStartIndex))) #else #define FF_C_PRINTF(formatStrIndex, argsStartIndex) #endif -#if defined(__GNUC__) || defined(__clang__) +#if defined(__has_attribute) && __has_attribute(__format__) #define FF_C_SCANF(formatStrIndex, argsStartIndex) __attribute__((__format__ (scanf, formatStrIndex, argsStartIndex))) #else #define FF_C_SCANF(formatStrIndex, argsStartIndex) #endif + +#if defined(__has_attribute) && __has_attribute(__nonnull__) + #define FF_C_NONNULL(argIndex, ...) __attribute__((__nonnull__(argIndex, ##__VA_ARGS__))) +#else + #define FF_C_NONNULL(argIndex, ...) +#endif + +#if defined(__has_attribute) && __has_attribute(__returns_nonnull__) + #define FF_C_RETURNS_NONNULL __attribute__((__returns_nonnull__)) +#else + #define FF_C_RETURNS_NONNULL +#endif