[PATCH] D31856: Headers: Make the type of SIZE_MAX the same as size_t

Duncan P. N. Exon Smith via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 10 11:07:13 PDT 2017


dexonsmith created this revision.

size_t is usually defined as unsigned long, but on 64-bit platforms,
stdint.h currently defines SIZE_MAX using "ull" (unsigned long long).
Although this is the same width, it doesn't necessarily have the same
alignment or calling convention.  It also triggers printf warnings when
using the format flag "%zu" to print SIZE_MAX.

1. Is there a better way to get the right type for SIZE_MAX?
2. Should we do this for ptrdiff_t as well?


https://reviews.llvm.org/D31856

Files:
  clang/lib/Headers/stdint.h
  clang/test/Headers/stdint-type-of-SIZE_MAX.cpp


Index: clang/test/Headers/stdint-type-of-SIZE_MAX.cpp
===================================================================
--- /dev/null
+++ clang/test/Headers/stdint-type-of-SIZE_MAX.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only %s
+#include <stdint.h>
+#include <stddef.h>
+
+static_assert(__is_same(__typeof__(SIZE_MAX), size_t),
+              "SIZE_MAX should be size_t");
Index: clang/lib/Headers/stdint.h
===================================================================
--- clang/lib/Headers/stdint.h
+++ clang/lib/Headers/stdint.h
@@ -664,7 +664,7 @@
 #define UINTPTR_MAX __UINTN_MAX(__INTPTR_WIDTH__)
 #define PTRDIFF_MIN  __INTN_MIN(__PTRDIFF_WIDTH__)
 #define PTRDIFF_MAX  __INTN_MAX(__PTRDIFF_WIDTH__)
-#define    SIZE_MAX __UINTN_MAX(__SIZE_WIDTH__)
+#define    SIZE_MAX ((__SIZE_TYPE__)__UINTN_MAX(__SIZE_WIDTH__))
 
 /* ISO9899:2011 7.20 (C11 Annex K): Define RSIZE_MAX if __STDC_WANT_LIB_EXT1__
  * is enabled. */


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31856.94607.patch
Type: text/x-patch
Size: 951 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170410/496c72a2/attachment.bin>


More information about the cfe-commits mailing list