[PATCH] D24903: [libcxx] [include] Declare __STDC_*_MACROS for C++11 compat in old libc

Michał Górny via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 25 13:42:54 PDT 2016


mgorny created this revision.
mgorny added a reviewer: EricWF.
mgorny added a subscriber: cfe-commits.

Declare __STDC_FORMAT_MACROS, __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS before including real inttypes.h/stdint.h when the wrapper-header is included in C++11, in order to enable the necessary macros in C99-compliant libc.

The C99 standard defined that the format macros in inttypes.h should be defined by the C++ implementations only when __STDC_FORMAT_MACROS is defined, and the limit and constant macros in stdint.h should be defined only when __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are defined appropriately. Following this specification, multiple old versions of glibc up to 2.17 do not define those macros by default for C++, rendering the libc++ headers non-compliant to the C++11 standard.

In order to achieve the necessary compliance, __STDC_FORMAT_MACROS is defined in wrapped inttypes.h just before including the system inttypes.h, when C++11 or newer is used. Both __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS are defined in newly-wrapped stdint.h. This fixes the C++11 compliance while preserving the current behavior for C++03.


https://reviews.llvm.org/D24903

Files:
  include/inttypes.h
  include/stdint.h

Index: include/stdint.h
===================================================================
--- /dev/null
+++ include/stdint.h
@@ -0,0 +1,33 @@
+// -*- C++ -*-
+//===---------------------------- stdint.h --------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef _LIBCPP_STDINT_H
+#define _LIBCPP_STDINT_H
+
+#include <__config>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#pragma GCC system_header
+#endif
+
+/* C99 stdlib (e.g. glibc < 2.18) does not provide macros needed
+   for C++11 unless __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS
+   are defined
+*/
+#if __cplusplus >= 201103L && !defined(__STDC_LIMIT_MACROS)
+#	define __STDC_LIMIT_MACROS
+#endif
+#if __cplusplus >= 201103L && !defined(__STDC_CONSTANT_MACROS)
+#	define __STDC_CONSTANT_MACROS
+#endif
+
+#include_next <stdint.h>
+
+#endif  // _LIBCPP_STDINT_H
Index: include/inttypes.h
===================================================================
--- include/inttypes.h
+++ include/inttypes.h
@@ -237,6 +237,13 @@
 #pragma GCC system_header
 #endif
 
+/* C99 stdlib (e.g. glibc < 2.18) does not provide format macros needed
+   for C++11 unless __STDC_FORMAT_MACROS is defined
+*/
+#if __cplusplus >= 201103L && !defined(__STDC_FORMAT_MACROS)
+#	define __STDC_FORMAT_MACROS
+#endif
+
 #include_next <inttypes.h>
 
 #ifdef __cplusplus


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24903.72434.patch
Type: text/x-patch
Size: 1574 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160925/0ca77c9d/attachment.bin>


More information about the cfe-commits mailing list