relibc: guard size_t typedef against C++ name collision
When a C++ translation unit transitively includes this header (e.g. via <string> pulling in <bits/basic_string.h>), the unqualified 'size_t' references inside libstdc++'s internal templates resolved to this typedef instead of std::size_t, producing 'unterminated #ifndef' template parse errors during hosted-mode C++ compilation. Mirror the pattern already used in bits/wchar-t.h: wrap the typedef in '#ifndef __cplusplus / #endif' so C compilation sees the typedef and C++ falls back to the libstdc++/libc++-provided std::size_t via the standard type machinery.
This commit is contained in:
@@ -1,7 +1,26 @@
|
||||
# POSIX header spec: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/stddef.h.html
|
||||
#
|
||||
# This type is split out to prevent importing all of stddef.h into other headers.
|
||||
#
|
||||
# C++ compatibility: in C++, `size_t` is defined by the standard library
|
||||
# in namespace `std::`. A bare `typedef unsigned long size_t;` at file
|
||||
# scope in a C++ translation unit collides with `std::size_t` whenever
|
||||
# a host stdlib header (e.g. `<string>`, `<bits/basic_string.h>`) is
|
||||
# transitively included — the libstdc++ internal code references
|
||||
# unqualified `size_t` and resolves it to the first visible one, which
|
||||
# would otherwise be this typedef. The `__cplusplus` guard makes the
|
||||
# typedef a no-op under C++ compilation; the libc++/libstdc++ headers
|
||||
# then provide their own `std::size_t` via `__SIZE_TYPE__` or the
|
||||
# standard type machinery.
|
||||
include_guard = "_RELIBC_BITS_SIZE_T_H"
|
||||
after_includes = """
|
||||
/**
|
||||
* Unsigned integer type of the result of the sizeof operator.
|
||||
*/
|
||||
#ifndef __cplusplus
|
||||
typedef unsigned long size_t;
|
||||
#endif
|
||||
"""
|
||||
language = "C"
|
||||
no_includes = true
|
||||
cpp_compat = true
|
||||
|
||||
Reference in New Issue
Block a user