Skip to content

Commit

Permalink
Add warning suppression macros
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanoBilenchi committed Jun 7, 2024
1 parent 65b8f5e commit 8706bac
Show file tree
Hide file tree
Showing 22 changed files with 157 additions and 56 deletions.
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ TypenameMacros:
- UVec
WhitespaceSensitiveMacros:
- ULIB_DEPRECATED
- ULIB_SUPPRESS
- ULIB_SUPPRESS_ONE
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ repos:
- id: mixed-line-ending
- id: end-of-file-fixer
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v18.1.5
rev: v18.1.6
hooks:
- id: clang-format
6 changes: 6 additions & 0 deletions docs/api/macros.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ API attributes
.. doxygengroup:: attributes
:content-only:

Warning suppression
===================

.. doxygengroup:: warnings
:content-only:

Utilities
=========

Expand Down
12 changes: 2 additions & 10 deletions include/uattrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#ifndef UATTRS_H
#define UATTRS_H

#include "uutils.h"

/**
* @defgroup attributes API attributes
* @{
Expand Down Expand Up @@ -120,7 +122,6 @@
#ifndef ULIB_NO_DEPRECATED
#if defined(__GNUC__) || defined(__clang__)
#define ULIB_DEPRECATED(msg) __attribute__((__deprecated__(#msg)))
#define P_ULIB_PRAGMA(msg) _Pragma(#msg)
#define ULIB_DEPRECATED_MACRO \
P_ULIB_PRAGMA(GCC warning "Deprecated. See the docstring for a possible replacement.")
#elif defined(_MSC_VER)
Expand All @@ -136,15 +137,6 @@
#define ULIB_DEPRECATED_MACRO
#endif

/// Suppresses unused variable warnings.
#if defined(__GNUC__) || defined(__clang__)
#define ulib_unused __attribute__((__unused__))
#elif defined(_MSC_VER)
#define ulib_unused __pragma(warning(suppress : 4100))
#else
#define ulib_unused
#endif

/// @}

#endif // UATTRS_H
7 changes: 6 additions & 1 deletion include/uhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
#define UHASH_H

#include "ualloc.h"
#include "uhash_func.h" // IWYU pragma: export
#include "uattrs.h"
#include "unumber.h"
#include "uutils.h"
#include "uwarning.h"
#include <stdint.h>
#include <string.h>

ULIB_BEGIN_DECLS

// Types

/**
Expand Down Expand Up @@ -1465,4 +1468,6 @@ typedef enum uhash_ret {

/// @}

ULIB_END_DECLS

#endif // UHASH_H
1 change: 1 addition & 0 deletions include/uhash_builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#ifndef UHASH_BUILTIN_H
#define UHASH_BUILTIN_H

#include "uattrs.h"
#include "uhash.h" // IWYU pragma: export
#include "unumber.h"
#include "ustring.h"
Expand Down
1 change: 1 addition & 0 deletions include/ulib.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "uvec.h"
#include "uvec_builtin.h"
#include "uversion.h"
#include "uwarning.h"

// IWYU pragma: end_exports

Expand Down
4 changes: 3 additions & 1 deletion include/ustring.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ ulib_uint p_ustring_large_size(struct p_ustring_large string) {
#define p_ustring_is_large(str) p_ustring_last_byte_is_large(p_ustring_last_byte(str))
#define p_ustring_length_is_small(l) ((l) < P_USTRING_SIZE)
#define p_ustring_init_small(size) \
{ ._s = { [P_USTRING_SIZE - 1] = (ulib_byte)((P_USTRING_SIZE - (size))) }, }
{ \
._s = { [P_USTRING_SIZE - 1] = (ulib_byte)((P_USTRING_SIZE - (size))) }, \
}
#define p_ustring_init_large(buf, size) \
{ \
._l = { ._data = (buf), \
Expand Down
8 changes: 8 additions & 0 deletions include/uutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@
#define ULIB_MACRO_CONCAT(a, b) P_ULIB_MACRO_CONCAT_INNER(a, b)
#define P_ULIB_MACRO_CONCAT_INNER(a, b) a##b

/**
* Pragma directive that allows macro expansion.
*
* @param msg Pragma directive.
*/
#define ULIB_PRAGMA(msg) P_ULIB_PRAGMA(msg)
#define P_ULIB_PRAGMA(msg) _Pragma(#msg)

/// @}

#endif // UUTILS_H
2 changes: 2 additions & 0 deletions include/uvec.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
#define UVEC_H

#include "ualloc.h"
#include "uattrs.h"
#include "unumber.h"
#include "urand.h" // IWYU pragma: keep, needed for urand_range
#include "uutils.h"
#include "uwarning.h"

ULIB_BEGIN_DECLS

Expand Down
114 changes: 114 additions & 0 deletions include/uwarning.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* Defines compiler-specific warning macros.
*
* @author Ivano Bilenchi
*
* @copyright Copyright (c) 2024 Ivano Bilenchi <https://ivanobilenchi.com>
* @copyright SPDX-License-Identifier: ISC
*
* @file
*/

#ifndef UWARNING_H
#define UWARNING_H

#include "uutils.h"

/**
* @defgroup warnings Warning macros
* @{
*/

// clang-format off

/// Suppresses unused variable warnings.
#if defined(__GNUC__) || defined(__clang__)
#define ulib_unused __attribute__((__unused__))
#elif defined(_MSC_VER)
#define ulib_unused __pragma(warning(suppress : 4100))
#else
#define ulib_unused
#endif

#if defined(__GNUC__) && !defined(__clang__)
#define P_ULIB_GCC_SUPPRESS_BEGIN _Pragma("GCC diagnostic push")
#define P_ULIB_GCC_SUPPRESS(warning) ULIB_PRAGMA(GCC diagnostic ignored warning)
#define P_ULIB_GCC_SUPPRESS_END _Pragma("GCC diagnostic pop")
#else
#define P_ULIB_GCC_SUPPRESS_BEGIN
#define P_ULIB_GCC_SUPPRESS(warning)
#define P_ULIB_GCC_SUPPRESS_END
#endif

#if defined(__clang__)
#define P_ULIB_CLANG_SUPPRESS_BEGIN _Pragma("clang diagnostic push")
#define P_ULIB_CLANG_SUPPRESS(warning) ULIB_PRAGMA(clang diagnostic ignored warning)
#define P_ULIB_CLANG_SUPPRESS_END _Pragma("clang diagnostic pop")
#else
#define P_ULIB_CLANG_SUPPRESS_BEGIN
#define P_ULIB_CLANG_SUPPRESS(warning)
#define P_ULIB_CLANG_SUPPRESS_END
#endif

#if defined(_MSC_VER)
#define P_ULIB_MSVC_SUPPRESS_BEGIN __pragma(warning(push))
#define P_ULIB_MSVC_SUPPRESS(warning) __pragma(warning(disable : warnings))
#define P_ULIB_MSVC_SUPPRESS_END __pragma(warning(pop))
#else
#define P_ULIB_MSVC_SUPPRESS_BEGIN
#define P_ULIB_MSVC_SUPPRESS(warning)
#define P_ULIB_MSVC_SUPPRESS_END
#endif

#if defined(__clang__)
#define P_ULIB_GNUC_SUPPRESS_BEGIN P_ULIB_CLANG_SUPPRESS_BEGIN
#define P_ULIB_GNUC_SUPPRESS(warning) P_ULIB_CLANG_SUPPRESS(warning)
#define P_ULIB_GNUC_SUPPRESS_END P_ULIB_CLANG_SUPPRESS_END
#elif defined(__GNUC__)
#define P_ULIB_GNUC_SUPPRESS_BEGIN P_ULIB_GCC_SUPPRESS_BEGIN
#define P_ULIB_GNUC_SUPPRESS(warning) P_ULIB_GCC_SUPPRESS(warning)
#define P_ULIB_GNUC_SUPPRESS_END P_ULIB_GCC_SUPPRESS_END
#else
#define P_ULIB_GNUC_SUPPRESS_BEGIN
#define P_ULIB_GNUC_SUPPRESS(warning)
#define P_ULIB_GNUC_SUPPRESS_END
#endif

/**
* Begins the suppression of compiler warnings.
*
* @param compiler The compiler for which the warnings are being suppressed.
* Allowed values: GCC, CLANG, GNUC, MSVC.
*/
#define ULIB_SUPPRESS_BEGIN(compiler) P_ULIB_##compiler##_SUPPRESS_BEGIN

/**
* Suppresses the specified compiler warning.
*
* @param compiler The compiler for which the warnings are being suppressed.
* Allowed values: GCC, CLANG, GNUC, MSVC.
* @param warning The warning to suppress.
*/
#define ULIB_SUPPRESS(compiler, warning) P_ULIB_##compiler##_SUPPRESS(warning)

/**
* Ends the suppression of compiler warnings.
*
* @param compiler The compiler for which the warnings are being suppressed.
* Allowed values: GCC, CLANG, GNUC, MSVC.
*/
#define ULIB_SUPPRESS_END(compiler) P_ULIB_##compiler##_SUPPRESS_END

/**
* Begins the suppression of the specified compiler warning.
*
* @param compiler The compiler for which the warnings are being suppressed.
* Allowed values: GCC, CLANG, GNUC, MSVC.
* @param warning The warning to suppress.
*/
#define ULIB_SUPPRESS_ONE(compiler, warning) \
ULIB_SUPPRESS_BEGIN(compiler) ULIB_SUPPRESS(compiler, warning)

/// @}

#endif // UWARNING_H
1 change: 1 addition & 0 deletions src/uhash_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

#include "uhash_builtin.h"
#include "uhash_func.h"
#include "unumber.h" // IWYU pragma: keep, required for ulib_equals
#include "ustring.h"

Expand Down
2 changes: 1 addition & 1 deletion src/ustream.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

#include "ustream.h"
#include "ualloc.h"
#include "uattrs.h"
#include "unumber.h"
#include "ustrbuf.h"
#include "ustring.h"
#include "ustring_raw.h"
#include "utime.h"
#include "uvec_builtin.h"
#include "uversion.h"
#include "uwarning.h"
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
Expand Down
2 changes: 1 addition & 1 deletion test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

#include "ubit_tests.h"
#include "uhash_tests.h"
#include "ulib.h"
#include "urand_tests.h"
#include "ustream_tests.h"
#include "ustring_tests.h"
#include "utest.h"
#include "utime_tests.h"
#include "uvec_tests.h"
#include "uversion_tests.h"
Expand Down
3 changes: 1 addition & 2 deletions test/tests/ubit_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
* @file
*/

#include "ubit.h"
#include "utest.h"
#include "ulib.h"
#include <stdint.h>

#define ubit_test_impl(N) \
Expand Down
4 changes: 1 addition & 3 deletions test/tests/uhash_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
* @file
*/

#include "uhash.h"
#include "unumber.h"
#include "utest.h"
#include "ulib.h"
#include <stddef.h>
#include <stdint.h>

Expand Down
7 changes: 1 addition & 6 deletions test/tests/urand_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
* @file
*/

#include "urand_tests.h"
#include "unumber.h"
#include "urand.h"
#include "ustring.h"
#include "utest.h"
#include "uvec_builtin.h"
#include "ulib.h"
#include <stddef.h>
#include <string.h>

Expand Down
5 changes: 1 addition & 4 deletions test/tests/ustream_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
* @file
*/

#include "ustream_tests.h"
#include "ualloc.h"
#include "ustream.h"
#include "utest.h"
#include "ulib.h"
#include <stddef.h>
#include <stdio.h>
#include <string.h>
Expand Down
11 changes: 1 addition & 10 deletions test/tests/ustring_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@
* @file
*/

#include "ustring_tests.h"
#include "ualloc.h"
#include "ulib_ret.h"
#include "unumber.h"
#include "ustrbuf.h"
#include "ustring.h"
#include "ustring_raw.h"
#include "utest.h"
#include "uutils.h"
#include "uvec.h"
#include "ulib.h"
#include <stddef.h>

#define MAX_ASCII 127
Expand Down
6 changes: 1 addition & 5 deletions test/tests/utime_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
* @file
*/

#include "utime_tests.h"
#include "ustring.h"
#include "utest.h"
#include "utime.h"
#include "uutils.h"
#include "ulib.h"
#include <time.h>

struct utime_test_s {
Expand Down
7 changes: 1 addition & 6 deletions test/tests/uvec_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
* @file
*/

#include "ualloc.h"
#include "unumber.h"
#include "urand.h"
#include "utest.h"
#include "uutils.h"
#include "uvec_builtin.h"
#include "ulib.h"
#include <stdlib.h>

// Utility macros
Expand Down
6 changes: 1 addition & 5 deletions test/tests/uversion_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
* @file
*/

#include "uversion_tests.h"
#include "umeta.h"
#include "ustring.h"
#include "utest.h"
#include "uversion.h"
#include "ulib.h"

bool uversion_test(void) {
UVersion a = ulib_get_version(), b = uversion(0, 0, 0);
Expand Down

0 comments on commit 8706bac

Please sign in to comment.