infix
A JIT-Powered FFI Library for C
Loading...
Searching...
No Matches
compat_c23.h File Reference

Provides forward compatibility macros for C23 features. More...

#include <stdbool.h>
#include <stddef.h>
Include dependency graph for compat_c23.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define nullptr   ((void *)0)
 Defines nullptr as a standard C-style null pointer constant ((void*)0).
 
#define COMPAT_HAS_C_ATTRIBUTE(x)   0
 A utility macro to safely check for the existence of a C attribute.
 
#define c23_nodiscard
 A compatibility macro for the C23 [[nodiscard]] attribute.
 
#define c23_deprecated
 A compatibility macro for the C23 [[deprecated]] attribute.
 
#define c23_fallthrough
 A compatibility macro for the C23 [[fallthrough]] attribute.
 
#define c23_maybe_unused
 A compatibility macro for the C23 [[maybe_unused]] attribute.
 

Detailed Description

Provides forward compatibility macros for C23 features.

Copyright (c) 2025 Sanko Robinson

This source code is dual-licensed under the Artistic License 2.0 or the MIT License. You may choose to use this code under the terms of either license.

SPDX-License-Identifier: (Artistic-2.0 OR MIT)

The documentation blocks within this file are licensed under the Creative Commons Attribution 4.0 International License (CC BY 4.0).

SPDX-License-Identifier: CC-BY-4.0

This header is a crucial component for maintaining source code compatibility across a wide range of compilers that may not fully support the latest C23 standard. It acts as a "shim" or compatibility layer, allowing the rest of the codebase to be written using modern, standards-compliant syntax while ensuring it still compiles on older C11/C17 compilers.

The primary purpose is to define a set of c23_* macros (e.g., c23_nodiscard) that translate modern C23 attribute syntax ([[attribute]]) into older, compiler-specific equivalents (like __attribute__((...)) for GCC/Clang or __declspec(...) for MSVC). If a compiler supports none of these, the macros expand to nothing, ensuring compilation succeeds at the cost of losing the specific compiler check (a principle known as graceful degradation).

This approach centralizes all compiler-specific feature detection, keeping the rest of the library's code clean and free of #ifdef clutter.

Macro Definition Documentation

◆ c23_deprecated

#define c23_deprecated

A compatibility macro for the C23 [[deprecated]] attribute.

This attribute is used to mark a function or type as obsolete. The compiler will issue a warning if any code attempts to use a deprecated entity, guiding users toward newer APIs and helping to manage API evolution.

This macro expands to:

  • [[deprecated]] on compilers that support the C23 standard syntax.
  • __attribute__((deprecated)) on GCC and Clang.
  • __declspec(deprecated) on Microsoft Visual C++.
  • Nothing on other compilers.

◆ c23_fallthrough

#define c23_fallthrough

A compatibility macro for the C23 [[fallthrough]] attribute.

This attribute is placed in a switch statement to explicitly indicate that a case is intended to fall through to the next one. It suppresses compiler warnings that would otherwise be generated for this pattern (e.g., -Wimplicit-fallthrough), making the code's intent clearer.

This macro expands to:

  • [[fallthrough]] on compilers that support the C23 standard syntax.
  • __attribute__((fallthrough)) on GCC and Clang.
  • Nothing on other compilers (including MSVC, which uses a different mechanism or lacks the warning).

◆ c23_maybe_unused

#define c23_maybe_unused

A compatibility macro for the C23 [[maybe_unused]] attribute.

This attribute suppresses compiler warnings about unused variables, parameters, or functions. It is useful for parameters that are only used in certain build configurations (e.g., in an #ifdef DEBUG block) or for functions that are part of a public API but not used internally.

This macro expands to:

  • [[maybe_unused]] on compilers that support the C23 standard syntax.
  • __attribute__((unused)) on GCC and Clang.
  • Nothing on other compilers.

◆ c23_nodiscard

#define c23_nodiscard

A compatibility macro for the C23 [[nodiscard]] attribute.

This attribute is used to issue a compiler warning if the return value of a function is ignored by the caller. This is extremely useful for catching bugs where an error code or an important result is not checked.

This macro expands to:

  • [[nodiscard]] on compilers that support the C23 standard syntax.
  • __attribute__((warn_unused_result)) on GCC and Clang.
  • _Check_return_ on Microsoft Visual C++.
  • Nothing on other compilers.

◆ COMPAT_HAS_C_ATTRIBUTE

#define COMPAT_HAS_C_ATTRIBUTE (   x)    0

A utility macro to safely check for the existence of a C attribute.

This macro wraps the __has_c_attribute feature test macro, which is not universally available across all compilers. By defining this wrapper, the code can safely check for attribute support without causing a preprocessor error on compilers that do not define __has_c_attribute.

Parameters
[in]xThe name of the attribute to check (e.g., nodiscard).
Returns
1 if the attribute is supported, 0 otherwise.

◆ nullptr

#define nullptr   ((void *)0)

Defines nullptr as a standard C-style null pointer constant ((void*)0).

This provides a consistent null pointer constant across C and C++ compilation environments. While NULL is standard in C, nullptr is preferred in modern C++ and is being adopted in newer C standards. This macro ensures the codebase can use nullptr consistently without causing compilation errors in a strict C11/C17 environment.