From f2a06ede62e88ec682f3ad7a2fa65462fbb05114 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Wed, 1 Jul 2020 11:47:55 +0200 Subject: [PATCH] Add mechanism to disable temporary compiler warnings in a sequence of code. --- m-atomic.h | 5 ++++ m-bitset.h | 4 +++ m-c-mempool.h | 3 +++ m-core.h | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ m-genint.h | 4 +++ m-i-shared.h | 4 +++ m-mutex.h | 39 +++++++++++++++++++++++++++- m-serial-bin.h | 4 +++ m-serial-json.h | 4 +++ m-shared.h | 3 +++ m-snapshot.h | 4 +++ m-string.h | 3 +++ m-worker.h | 3 +++ tests/test-obj.h | 4 +++ 14 files changed, 149 insertions(+), 1 deletion(-) diff --git a/m-atomic.h b/m-atomic.h index d5252943..a0376284 100644 --- a/m-atomic.h +++ b/m-atomic.h @@ -135,6 +135,9 @@ using std::memory_order_seq_cst; We also assume that the call to the atomic_* interface is "clean". */ #include "m-mutex.h" +#include "m-core.h" + +M_BEGIN_PROTECTED_CODE /* _Atomic qualifier for a type (emulation). The structure is quite large: @@ -305,6 +308,8 @@ static inline long long atomic_fetch_unlock (m_mutex_t *lock, long long val) /* TODO: Missing atomic_flag. Problem: it is supposed to be lock free! */ +M_END_PROTECTED_CODE + #endif #endif diff --git a/m-bitset.h b/m-bitset.h index 3df06609..f5947b5d 100644 --- a/m-bitset.h +++ b/m-bitset.h @@ -30,6 +30,8 @@ /********************************** INTERNAL ************************************/ +M_BEGIN_PROTECTED_CODE + // Define the basic limb of a bitset typedef uint64_t bitset_limb; // And its size in bits @@ -813,4 +815,6 @@ bitset_get_str(string_t str, const bitset_t set, bool append) string_push_back (str, ']'); } +M_END_PROTECTED_CODE + #endif diff --git a/m-c-mempool.h b/m-c-mempool.h index 64578ce0..74a72528 100644 --- a/m-c-mempool.h +++ b/m-c-mempool.h @@ -29,6 +29,8 @@ #include "m-atomic.h" #include "m-genint.h" +M_BEGIN_PROTECTED_CODE + /* Minimum number of nodes per group of nodes */ #define C_MEMPOOL_MIN_NODE_PER_GROUP 16 @@ -822,5 +824,6 @@ m_vlapool_del(m_vlapool_t mem, void *d, m_gc_tid_t id) m_vlapool_slist_push(mem->thread_data[id].to_be_reclaimed, snode); } +M_END_PROTECTED_CODE #endif diff --git a/m-core.h b/m-core.h index 422a8eea..6d342c35 100644 --- a/m-core.h +++ b/m-core.h @@ -103,6 +103,68 @@ #define M_ATTR_EXTENSION #endif + +/* Ignore some warnings detected by some compilers in the library. + * Whatever we do, there is some warnings that cannot be fixed. + * So they are ignored in order to avoid polluting the user with + * theses warnings. + * They are: + * * If we build in C++ mode, they are warnings about using the C + * dialect. It is expected as M*LIB is a C library. + * + * * For clang, additional warnings are ignored since their detection are broken + * in the context of M*LIB: + * - unused generated inline functions + * https://bugs.llvm.org//show_bug.cgi?id=22712 + */ +#if defined(__clang__) && defined(__cplusplus) + +#if __clang_major__ >= 6 +#define M_BEGIN_PROTECTED_CODE \ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wold-style-cast\"") \ + _Pragma("clang diagnostic ignored \"-Wzero-as-null-pointer-constant\"") \ + _Pragma("clang diagnostic ignored \"-Wunused-function\"") +#else +#define M_BEGIN_PROTECTED_CODE \ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wold-style-cast\"") \ + _Pragma("clang diagnostic ignored \"-Wunused-function\"") +#endif + +#define M_END_PROTECTED_CODE \ + _Pragma("clang diagnostic pop") + +#elif defined(__GNUC__) && defined(__cplusplus) + +/* G++ doesn't support well disabling temporary warnings. + * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=53431 + */ +#define M_BEGIN_PROTECTED_CODE \ + _Pragma("GCC diagnostic push") \ + _Pragma("GCC diagnostic ignored \"-Wold-style-cast\"") \ + _Pragma("GCC diagnostic ignored \"-Wzero-as-null-pointer-constant\"") + +#define M_END_PROTECTED_CODE \ + _Pragma("GCC diagnostic pop") + +#elif defined(__clang__) + +#define M_BEGIN_PROTECTED_CODE \ + _Pragma("clang diagnostic push") \ + _Pragma("clang diagnostic ignored \"-Wunused-function\"") + +#define M_END_PROTECTED_CODE \ + _Pragma("clang diagnostic pop") + +#else + +#define M_BEGIN_PROTECTED_CODE +#define M_END_PROTECTED_CODE + +#endif + + /* * Do not create the following symbols that are defined in GLIBC malloc.h * M_MXFAST @@ -119,6 +181,8 @@ * M_ARENA_MAX */ +M_BEGIN_PROTECTED_CODE + /***************************************************************/ /****************** Preprocessing Times Macro ******************/ /***************************************************************/ @@ -3310,4 +3374,6 @@ m_core_serial_fail(void) return M_SERIAL_FAIL; } +M_END_PROTECTED_CODE + #endif diff --git a/m-genint.h b/m-genint.h index a81f6809..872d0b39 100644 --- a/m-genint.h +++ b/m-genint.h @@ -28,6 +28,8 @@ #include "m-core.h" #include "m-atomic.h" +M_BEGIN_PROTECTED_CODE + /* GENINT is a container providing unique integers. It has the following properties: - it stores integer from [0..N) (N is fixed). @@ -240,4 +242,6 @@ genint_push(genint_t s, unsigned int n) GENINT_CONTRACT(s); } +M_END_PROTECTED_CODE + #endif diff --git a/m-i-shared.h b/m-i-shared.h index a3df6d59..36dd223a 100644 --- a/m-i-shared.h +++ b/m-i-shared.h @@ -28,6 +28,8 @@ #include "m-core.h" #include "m-atomic.h" +M_BEGIN_PROTECTED_CODE + /* Define the oplist of a intrusive shared pointer. USAGE: ISHARED_OPLIST(name [, oplist_of_the_type]) */ #define ISHARED_PTR_OPLIST(...) \ @@ -169,5 +171,7 @@ } \ \ +M_END_PROTECTED_CODE + #endif diff --git a/m-mutex.h b/m-mutex.h index 9f1ba4ed..8eb648b2 100644 --- a/m-mutex.h +++ b/m-mutex.h @@ -36,6 +36,8 @@ #include #include "m-core.h" +M_BEGIN_PROTECTED_CODE + /* Define a mutex type based on C11 definition */ typedef mtx_t m_mutex_t[1]; @@ -155,12 +157,33 @@ static inline void m_oncei_call(m_oncei_t o, void (*func)(void)) // Attribute to use to allocate a global variable to a thread. #define M_THREAD_ATTR _Thread_local +M_END_PROTECTED_CODE -// MSYS2 doesn't define _WIN32 by default. +// MSYS2 doesn't define _WIN32 by default but __CYGWIN__ #elif defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) /****************************** WIN32 version ******************************/ +/* CLANG provides some useless and wrong warnings: + * - _WIN32_WINNT starts with '_' which is reserved by the standard + * as per the MSVC compiler, it is needed to be defined by the user + * to define which version of windows it want to be compatible with. + * - windows.h may be different than the case used by the file sytem + * there is however no normalized case. + * + * So, theses warnings have to be ignored and are disabled. + * + * We cannot add theses warnings in M_BEGIN_PROTECTED_CODE + * as they need to be disabled **BEFORE** including any system header + * and m-core includes some system headers. + * So we need to disable them explictly here. + */ +#if defined(__clang__) && __clang_major__ >= 4 + _Pragma("clang diagnostic push") + _Pragma("clang diagnostic ignore \"-Wreserved-id-macro\"") + _Pragma("clang diagnostic ignore \"-Wnonportable-system-include-path\"") +#endif + /* CriticalSection & ConditionVariable are available from Windows Vista */ #ifndef WINVER #define WINVER _WIN32_WINNT_VISTA @@ -168,11 +191,19 @@ static inline void m_oncei_call(m_oncei_t o, void (*func)(void)) #ifndef _WIN32_WINNT #define _WIN32_WINNT _WIN32_WINNT_VISTA #endif + +/* Include system headers */ #include #include #include #include "m-core.h" +#if defined(__clang__) && __clang_major__ >= 4 + _Pragma("clang diagnostic pop") +#endif + +M_BEGIN_PROTECTED_CODE + /* Define a thread type based on WINDOWS definition */ typedef HANDLE m_thread_t[1]; @@ -300,6 +331,8 @@ static inline void m_oncei_call(m_oncei_t o, void (*func)(void)) # define M_THREAD_ATTR __thread #endif +M_END_PROTECTED_CODE + #else /**************************** PTHREAD version ******************************/ @@ -315,6 +348,8 @@ static inline void m_oncei_call(m_oncei_t o, void (*func)(void)) #include #include "m-core.h" +M_BEGIN_PROTECTED_CODE + /* Define a mutex type based on PTHREAD definition */ typedef pthread_mutex_t m_mutex_t[1]; @@ -440,6 +475,8 @@ static inline void m_oncei_call(m_oncei_t o, void (*func)(void)) #define M_THREAD_ATTR __thread +M_END_PROTECTED_CODE + #endif // TODO: m_thread_sleep doesn't yield thread during waiting, making it a poor choice for active waiting. diff --git a/m-serial-bin.h b/m-serial-bin.h index 2d667b3a..3c551424 100644 --- a/m-serial-bin.h +++ b/m-serial-bin.h @@ -30,6 +30,8 @@ #include "m-core.h" #include "m-string.h" +M_BEGIN_PROTECTED_CODE + /* Internal service: * Write size_t in the stream in a compact form to reduce consumption * (and I/O bandwidth) @@ -586,4 +588,6 @@ typedef m_serial_read_t m_serial_bin_read_t; #define M_OPL_m_serial_bin_read_t() \ (INIT_WITH(m_serial_bin_read_init), CLEAR(m_serial_bin_read_clear), TYPE(m_serial_bin_read_t) ) +M_END_PROTECTED_CODE + #endif diff --git a/m-serial-json.h b/m-serial-json.h index 5e192eca..188fe0f8 100644 --- a/m-serial-json.h +++ b/m-serial-json.h @@ -28,6 +28,8 @@ #include "m-core.h" #include "m-string.h" +M_BEGIN_PROTECTED_CODE + /* Write the boolean 'data' into the serial stream 'serial'. Return M_SERIAL_OK_DONE if it succeeds, M_SERIAL_FAIL otherwise */ static inline m_serial_return_code_t @@ -562,4 +564,6 @@ typedef m_serial_read_t m_serial_json_read_t; #define M_OPL_m_serial_json_read_t() \ (INIT_WITH(m_serial_json_read_init), CLEAR(m_serial_json_read_clear), TYPE(m_serial_json_read_t) ) +M_END_PROTECTED_CODE + #endif diff --git a/m-shared.h b/m-shared.h index d8750d65..5690d79d 100644 --- a/m-shared.h +++ b/m-shared.h @@ -29,6 +29,8 @@ #include "m-atomic.h" #include "m-genint.h" +M_BEGIN_PROTECTED_CODE + /* Define the oplist of a shared pointer. USAGE: SHARED_OPLIST(name [, oplist_of_the_type]) */ #define SHARED_PTR_OPLIST(...) \ @@ -450,5 +452,6 @@ static inline int sharedi_integer_cref(int *p) { return *p; } } \ } \ +M_END_PROTECTED_CODE #endif diff --git a/m-snapshot.h b/m-snapshot.h index c8266c1a..74bff282 100644 --- a/m-snapshot.h +++ b/m-snapshot.h @@ -29,6 +29,8 @@ #include "m-core.h" #include "m-genint.h" +M_BEGIN_PROTECTED_CODE + /* Define a Single Producer Single Consummer snapshot and its functions USAGE: SNAPSHOT_SPSC_DEF(name, type[, oplist]) */ #define SNAPSHOT_SPSC_DEF(name, ...) \ @@ -676,4 +678,6 @@ static inline void snapshot_mrsw_int_read_end(snapshot_mrsw_int_t s, unsigned in //TODO: _set_, _init_set +M_END_PROTECTED_CODE + #endif diff --git a/m-string.h b/m-string.h index 2f3cd60b..03d86f8a 100644 --- a/m-string.h +++ b/m-string.h @@ -33,6 +33,8 @@ #include "m-core.h" +M_BEGIN_PROTECTED_CODE + /********************************** INTERNAL ************************************/ /* If within the tests, perform additional checks */ @@ -2126,5 +2128,6 @@ namespace m_string { ((const struct M_C(name, _s) *)(m_string::m_bounded_string(string).s)) #endif +M_END_PROTECTED_CODE #endif diff --git a/m-worker.h b/m-worker.h index cd0b480b..b701e723 100644 --- a/m-worker.h +++ b/m-worker.h @@ -93,6 +93,8 @@ # error M_USE_WORKER_CPP_FUNCTION and M_USE_WORKER_CLANG_BLOCK are both defined. This is not supported. #endif +M_BEGIN_PROTECTED_CODE + /* Definition of a work order */ typedef struct work_order_s { struct worker_sync_s *block; // Reference to the shared Synchronization block @@ -522,6 +524,7 @@ worker_count(worker_t g) #define WORKER_PROPAGATE_LOCAL_OUTPUT(...) \ M_MAP(WORKER_PROPAGATE_SINGLE_OUTPUT, __VA_ARGS__) +M_END_PROTECTED_CODE #else /* M_USE_WORKER */ diff --git a/tests/test-obj.h b/tests/test-obj.h index 0dc4fd0c..cad49632 100644 --- a/tests/test-obj.h +++ b/tests/test-obj.h @@ -28,6 +28,8 @@ #include "m-string.h" +M_BEGIN_PROTECTED_CODE + /* This is a trivial encapsulation of an opaque structure. Used for test purpose only.*/ typedef struct testobj_s { @@ -170,4 +172,6 @@ static inline void testobj_str(string_t str, const testobj_t z, bool append) CMP(testobj_cmp) \ ) +M_END_PROTECTED_CODE + #endif