Skip to content

Commit fb8b926

Browse files
committed
sys.h: Fix __attribute__ usage for non-GCC compilers
Restructure `MLD_INLINE` and `MLD_ALWAYS_INLINE` into separate blocks, each independently guarded: `MLD_INLINE`: - MSVC: `__inline` - C99 or `inline` macro defined: `inline` - GCC/Clang C90: `__attribute__((unused))` to silence warnings - Other C90: empty `MLD_ALWAYS_INLINE`: - MSVC: `__forceinline` - GCC/Clang C99+: `MLD_INLINE __attribute__((always_inline))` - Other: `MLD_INLINE` (no forced inlining) This fixes two issues: 1. If `inline` was defined as a macro before including sys.h, the MSVC check was bypassed and `__attribute__` was used, causing MSVC failures. 2. For non-MSVC/GCC/Clang compilers like IAR, `__attribute__` was used unconditionally, causing build failures. Also fix typo: `defined(clang)` -> `defined(__clang__)` Signed-off-by: Matthias J. Kannwischer <[email protected]>
1 parent 060fdbd commit fb8b926

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

mldsa/src/sys.h

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,34 +106,42 @@
106106
#endif
107107

108108
/*
109-
* C90 does not have the inline compiler directive yet.
110-
* We don't use it in C90 builds.
111-
* However, in that case the compiler warns about some inline functions in
112-
* header files not being used in every compilation unit that includes that
113-
* header. To work around it we silence that warning in that case using
114-
* __attribute__((unused)).
109+
* MLD_INLINE: Hint for inlining.
110+
* - MSVC: __inline
111+
* - C99+: inline
112+
* - GCC/Clang C90: __attribute__((unused)) to silence warnings
113+
* - Other C90: empty
115114
*/
116-
117-
/* Do not use inline for C90 builds*/
118115
#if !defined(MLD_INLINE)
119-
#if !defined(inline)
120116
#if defined(_MSC_VER)
121117
#define MLD_INLINE __inline
122-
/* Don't combine __inline and __forceinline */
123-
#define MLD_ALWAYS_INLINE __forceinline
124-
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
118+
#elif defined(inline) || \
119+
(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
125120
#define MLD_INLINE inline
126-
#define MLD_ALWAYS_INLINE MLD_INLINE __attribute__((always_inline))
127-
#else
121+
#elif defined(__GNUC__) || defined(__clang__)
128122
#define MLD_INLINE __attribute__((unused))
129-
#define MLD_ALWAYS_INLINE MLD_INLINE
123+
#else
124+
#define MLD_INLINE
130125
#endif
126+
#endif /* !MLD_INLINE */
131127

132-
#else /* !inline */
133-
#define MLD_INLINE inline
128+
/*
129+
* MLD_ALWAYS_INLINE: Force inlining.
130+
* - MSVC: __forceinline
131+
* - GCC/Clang C99+: MLD_INLINE __attribute__((always_inline))
132+
* - Other: MLD_INLINE (no forced inlining)
133+
*/
134+
#if !defined(MLD_ALWAYS_INLINE)
135+
#if defined(_MSC_VER)
136+
#define MLD_ALWAYS_INLINE __forceinline
137+
#elif (defined(__GNUC__) || defined(__clang__)) && \
138+
(defined(inline) || \
139+
(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L))
134140
#define MLD_ALWAYS_INLINE MLD_INLINE __attribute__((always_inline))
135-
#endif /* inline */
136-
#endif /* !MLD_INLINE */
141+
#else
142+
#define MLD_ALWAYS_INLINE MLD_INLINE
143+
#endif
144+
#endif /* !MLD_ALWAYS_INLINE */
137145

138146
#ifndef MLD_STATIC_TESTABLE
139147
#define MLD_STATIC_TESTABLE static
@@ -209,7 +217,7 @@
209217
} while (0)
210218
#endif /* !(MLD_CONFIG_CT_TESTING_ENABLED && !__ASSEMBLER__) */
211219

212-
#if defined(__GNUC__) || defined(clang)
220+
#if defined(__GNUC__) || defined(__clang__)
213221
#define MLD_MUST_CHECK_RETURN_VALUE __attribute__((warn_unused_result))
214222
#else
215223
#define MLD_MUST_CHECK_RETURN_VALUE

0 commit comments

Comments
 (0)