From e77a3aff6f92d39aba5404827817a52dc6a2c1d7 Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Thu, 27 Apr 2017 21:49:45 +0000 Subject: [PATCH] Headers: Make the type of SIZE_MAX the same as size_t 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. This changes SIZE_MAX to reuse the compiler-provided __SIZE_MAX__, and provides similar fixes for the other integers: - INTPTR_MIN - INTPTR_MAX - UINTPTR_MAX - PTRDIFF_MIN - PTRDIFF_MAX - INTMAX_MIN - INTMAX_MAX - UINTMAX_MAX - INTMAX_C() - UINTMAX_C() ... and fixes the typedefs for intptr_t and uintptr_t to use __INTPTR_TYPE__ and __UINTPTR_TYPE__ instead of int32_t, effectively reverting r89224, r89226, and r89237 (r89221 already having been effectively reverted). We can probably also kill __INTPTR_WIDTH__, __INTMAX_WIDTH__, and __UINTMAX_WIDTH__ in a follow-up, but I was hesitant to delete all the per-target CHECK lines in this commit since those might serve their own purpose. rdar://problem/11811377 llvm-svn: 301593 --- clang/lib/Headers/stdint.h | 29 ++++---- clang/test/Headers/stdint-typeof-MINMAX.cpp | 32 +++++++++ clang/test/Preprocessor/stdint.c | 105 +++++++++++++++------------- 3 files changed, 100 insertions(+), 66 deletions(-) create mode 100644 clang/test/Headers/stdint-typeof-MINMAX.cpp diff --git a/clang/lib/Headers/stdint.h b/clang/lib/Headers/stdint.h index 3f2fcbc..c488153 100644 --- a/clang/lib/Headers/stdint.h +++ b/clang/lib/Headers/stdint.h @@ -255,19 +255,16 @@ typedef __uint_least8_t uint_fast8_t; */ #define __stdint_join3(a,b,c) a ## b ## c -#define __intn_t(n) __stdint_join3( int, n, _t) -#define __uintn_t(n) __stdint_join3(uint, n, _t) - #ifndef _INTPTR_T #ifndef __intptr_t_defined -typedef __intn_t(__INTPTR_WIDTH__) intptr_t; +typedef __INTPTR_TYPE__ intptr_t; #define __intptr_t_defined #define _INTPTR_T #endif #endif #ifndef _UINTPTR_T -typedef __uintn_t(__INTPTR_WIDTH__) uintptr_t; +typedef __UINTPTR_TYPE__ uintptr_t; #define _UINTPTR_T #endif @@ -659,12 +656,12 @@ typedef __UINTMAX_TYPE__ uintmax_t; /* C99 7.18.2.4 Limits of integer types capable of holding object pointers. */ /* C99 7.18.3 Limits of other integer types. */ -#define INTPTR_MIN __INTN_MIN(__INTPTR_WIDTH__) -#define INTPTR_MAX __INTN_MAX(__INTPTR_WIDTH__) -#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 INTPTR_MIN (-__INTPTR_MAX__-1) +#define INTPTR_MAX __INTPTR_MAX__ +#define UINTPTR_MAX __UINTPTR_MAX__ +#define PTRDIFF_MIN (-__PTRDIFF_MAX__-1) +#define PTRDIFF_MAX __PTRDIFF_MAX__ +#define SIZE_MAX __SIZE_MAX__ /* ISO9899:2011 7.20 (C11 Annex K): Define RSIZE_MAX if __STDC_WANT_LIB_EXT1__ * is enabled. */ @@ -673,9 +670,9 @@ typedef __UINTMAX_TYPE__ uintmax_t; #endif /* C99 7.18.2.5 Limits of greatest-width integer types. */ -#define INTMAX_MIN __INTN_MIN(__INTMAX_WIDTH__) -#define INTMAX_MAX __INTN_MAX(__INTMAX_WIDTH__) -#define UINTMAX_MAX __UINTN_MAX(__INTMAX_WIDTH__) +#define INTMAX_MIN (-__INTMAX_MAX__-1) +#define INTMAX_MAX __INTMAX_MAX__ +#define UINTMAX_MAX __UINTMAX_MAX__ /* C99 7.18.3 Limits of other integer types. */ #define SIG_ATOMIC_MIN __INTN_MIN(__SIG_ATOMIC_WIDTH__) @@ -700,8 +697,8 @@ typedef __UINTMAX_TYPE__ uintmax_t; #endif /* 7.18.4.2 Macros for greatest-width integer constants. */ -#define INTMAX_C(v) __INTN_C(__INTMAX_WIDTH__, v) -#define UINTMAX_C(v) __UINTN_C(__INTMAX_WIDTH__, v) +#define INTMAX_C(v) __int_c(v, __INTMAX_C_SUFFIX__) +#define UINTMAX_C(v) __int_c(v, __UINTMAX_C_SUFFIX__) #endif /* __STDC_HOSTED__ */ #endif /* __CLANG_STDINT_H */ diff --git a/clang/test/Headers/stdint-typeof-MINMAX.cpp b/clang/test/Headers/stdint-typeof-MINMAX.cpp new file mode 100644 index 0000000..7014259 --- /dev/null +++ b/clang/test/Headers/stdint-typeof-MINMAX.cpp @@ -0,0 +1,32 @@ +// RUN: %clang_cc1 %s -ffreestanding -std=c++1z -fsyntax-only -triple=aarch64-none-none +// RUN: %clang_cc1 %s -ffreestanding -std=c++1z -fsyntax-only -triple=arm-none-none +// RUN: %clang_cc1 %s -ffreestanding -std=c++1z -fsyntax-only -triple=i386-none-none +// RUN: %clang_cc1 %s -ffreestanding -std=c++1z -fsyntax-only -triple=mips-none-none +// RUN: %clang_cc1 %s -ffreestanding -std=c++1z -fsyntax-only -triple=mips64-none-none +// RUN: %clang_cc1 %s -ffreestanding -std=c++1z -fsyntax-only -triple=msp430-none-none +// RUN: %clang_cc1 %s -ffreestanding -std=c++1z -fsyntax-only -triple=powerpc64-none-none +// RUN: %clang_cc1 %s -ffreestanding -std=c++1z -fsyntax-only -triple=powerpc64-none-netbsd +// RUN: %clang_cc1 %s -ffreestanding -std=c++1z -fsyntax-only -triple=powerpc-none-none +// RUN: %clang_cc1 %s -ffreestanding -std=c++1z -fsyntax-only -triple=s390x-none-none +// RUN: %clang_cc1 %s -ffreestanding -std=c++1z -fsyntax-only -triple=sparc-none-none +// RUN: %clang_cc1 %s -ffreestanding -std=c++1z -fsyntax-only -triple=tce-none-none +// RUN: %clang_cc1 %s -ffreestanding -std=c++1z -fsyntax-only -triple=x86_64-none-none +// RUN: %clang_cc1 %s -ffreestanding -std=c++1z -fsyntax-only -triple=x86_64-pc-linux-gnu +// RUN: %clang_cc1 %s -ffreestanding -std=c++1z -fsyntax-only -triple=i386-mingw32 +// RUN: %clang_cc1 %s -ffreestanding -std=c++1z -fsyntax-only -triple=xcore-none-none +// RUN: %clang_cc1 %s -ffreestanding -std=c++1z -fsyntax-only + +#include +#include + +static_assert(__is_same(__typeof__(INTPTR_MIN), intptr_t)); +static_assert(__is_same(__typeof__(INTPTR_MAX), intptr_t)); +static_assert(__is_same(__typeof__(UINTPTR_MAX), uintptr_t)); +static_assert(__is_same(__typeof__(PTRDIFF_MIN), ptrdiff_t)); +static_assert(__is_same(__typeof__(PTRDIFF_MAX), ptrdiff_t)); +static_assert(__is_same(__typeof__(SIZE_MAX), size_t)); +static_assert(__is_same(__typeof__(INTMAX_MIN), intmax_t)); +static_assert(__is_same(__typeof__(INTMAX_MAX), intmax_t)); +static_assert(__is_same(__typeof__(UINTMAX_MAX), uintmax_t)); +static_assert(__is_same(__typeof__(INTMAX_C(5)), intmax_t)); +static_assert(__is_same(__typeof__(UINTMAX_C(5)), uintmax_t)); diff --git a/clang/test/Preprocessor/stdint.c b/clang/test/Preprocessor/stdint.c index 28ccfef..01b0da7 100644 --- a/clang/test/Preprocessor/stdint.c +++ b/clang/test/Preprocessor/stdint.c @@ -28,8 +28,8 @@ // ARM:typedef int8_t int_fast8_t; // ARM:typedef uint8_t uint_fast8_t; // -// ARM:typedef int32_t intptr_t; -// ARM:typedef uint32_t uintptr_t; +// ARM:typedef long int intptr_t; +// ARM:typedef long unsigned int uintptr_t; // // ARM:typedef long long int intmax_t; // ARM:typedef long long unsigned int uintmax_t; @@ -74,9 +74,9 @@ // ARM:INT_FAST64_MAX_ 9223372036854775807LL // ARM:UINT_FAST64_MAX_ 18446744073709551615ULL // -// ARM:INTPTR_MIN_ (-2147483647 -1) -// ARM:INTPTR_MAX_ 2147483647 -// ARM:UINTPTR_MAX_ 4294967295U +// ARM:INTPTR_MIN_ (-2147483647L -1) +// ARM:INTPTR_MAX_ 2147483647L +// ARM:UINTPTR_MAX_ 4294967295UL // ARM:PTRDIFF_MIN_ (-2147483647 -1) // ARM:PTRDIFF_MAX_ 2147483647 // ARM:SIZE_MAX_ 4294967295U @@ -136,8 +136,8 @@ // I386:typedef int8_t int_fast8_t; // I386:typedef uint8_t uint_fast8_t; // -// I386:typedef int32_t intptr_t; -// I386:typedef uint32_t uintptr_t; +// I386:typedef int intptr_t; +// I386:typedef unsigned int uintptr_t; // // I386:typedef long long int intmax_t; // I386:typedef long long unsigned int uintmax_t; @@ -243,8 +243,8 @@ // MIPS:typedef int8_t int_fast8_t; // MIPS:typedef uint8_t uint_fast8_t; // -// MIPS:typedef int32_t intptr_t; -// MIPS:typedef uint32_t uintptr_t; +// MIPS:typedef long int intptr_t; +// MIPS:typedef long unsigned int uintptr_t; // // MIPS:typedef long long int intmax_t; // MIPS:typedef long long unsigned int uintmax_t; @@ -289,9 +289,9 @@ // MIPS:INT_FAST64_MAX_ 9223372036854775807LL // MIPS:UINT_FAST64_MAX_ 18446744073709551615ULL // -// MIPS:INTPTR_MIN_ (-2147483647 -1) -// MIPS:INTPTR_MAX_ 2147483647 -// MIPS:UINTPTR_MAX_ 4294967295U +// MIPS:INTPTR_MIN_ (-2147483647L -1) +// MIPS:INTPTR_MAX_ 2147483647L +// MIPS:UINTPTR_MAX_ 4294967295UL // MIPS:PTRDIFF_MIN_ (-2147483647 -1) // MIPS:PTRDIFF_MAX_ 2147483647 // MIPS:SIZE_MAX_ 4294967295U @@ -350,8 +350,8 @@ // MIPS64:typedef int8_t int_fast8_t; // MIPS64:typedef uint8_t uint_fast8_t; // -// MIPS64:typedef int64_t intptr_t; -// MIPS64:typedef uint64_t uintptr_t; +// MIPS64:typedef long int intptr_t; +// MIPS64:typedef long unsigned int uintptr_t; // // MIPS64:typedef long int intmax_t; // MIPS64:typedef long unsigned int uintmax_t; @@ -450,8 +450,8 @@ // MSP430:typedef int8_t int_fast8_t; // MSP430:typedef uint8_t uint_fast8_t; // -// MSP430:typedef int16_t intptr_t; -// MSP430:typedef uint16_t uintptr_t; +// MSP430:typedef int intptr_t; +// MSP430:typedef unsigned int uintptr_t; // // MSP430:typedef long long int intmax_t; // MSP430:typedef long long unsigned int uintmax_t; @@ -557,8 +557,8 @@ // PPC64:typedef int8_t int_fast8_t; // PPC64:typedef uint8_t uint_fast8_t; // -// PPC64:typedef int64_t intptr_t; -// PPC64:typedef uint64_t uintptr_t; +// PPC64:typedef long int intptr_t; +// PPC64:typedef long unsigned int uintptr_t; // // PPC64:typedef long int intmax_t; // PPC64:typedef long unsigned int uintmax_t; @@ -664,8 +664,8 @@ // PPC64-NETBSD:typedef int8_t int_fast8_t; // PPC64-NETBSD:typedef uint8_t uint_fast8_t; // -// PPC64-NETBSD:typedef int64_t intptr_t; -// PPC64-NETBSD:typedef uint64_t uintptr_t; +// PPC64-NETBSD:typedef long int intptr_t; +// PPC64-NETBSD:typedef long unsigned int uintptr_t; // // PPC64-NETBSD:typedef long long int intmax_t; // PPC64-NETBSD:typedef long long unsigned int uintmax_t; @@ -710,12 +710,12 @@ // PPC64-NETBSD:INT_FAST64_MAX_ 9223372036854775807LL // PPC64-NETBSD:UINT_FAST64_MAX_ 18446744073709551615ULL // -// PPC64-NETBSD:INTPTR_MIN_ (-9223372036854775807LL -1) -// PPC64-NETBSD:INTPTR_MAX_ 9223372036854775807LL -// PPC64-NETBSD:UINTPTR_MAX_ 18446744073709551615ULL -// PPC64-NETBSD:PTRDIFF_MIN_ (-9223372036854775807LL -1) -// PPC64-NETBSD:PTRDIFF_MAX_ 9223372036854775807LL -// PPC64-NETBSD:SIZE_MAX_ 18446744073709551615ULL +// PPC64-NETBSD:INTPTR_MIN_ (-9223372036854775807L -1) +// PPC64-NETBSD:INTPTR_MAX_ 9223372036854775807L +// PPC64-NETBSD:UINTPTR_MAX_ 18446744073709551615UL +// PPC64-NETBSD:PTRDIFF_MIN_ (-9223372036854775807L -1) +// PPC64-NETBSD:PTRDIFF_MAX_ 9223372036854775807L +// PPC64-NETBSD:SIZE_MAX_ 18446744073709551615UL // // PPC64-NETBSD:INTMAX_MIN_ (-9223372036854775807LL -1) // PPC64-NETBSD:INTMAX_MAX_ 9223372036854775807LL @@ -772,8 +772,8 @@ // PPC:typedef int8_t int_fast8_t; // PPC:typedef uint8_t uint_fast8_t; // -// PPC:typedef int32_t intptr_t; -// PPC:typedef uint32_t uintptr_t; +// PPC:typedef long int intptr_t; +// PPC:typedef long unsigned int uintptr_t; // // PPC:typedef long long int intmax_t; // PPC:typedef long long unsigned int uintmax_t; @@ -818,12 +818,12 @@ // PPC:INT_FAST64_MAX_ 9223372036854775807LL // PPC:UINT_FAST64_MAX_ 18446744073709551615ULL // -// PPC:INTPTR_MIN_ (-2147483647 -1) -// PPC:INTPTR_MAX_ 2147483647 -// PPC:UINTPTR_MAX_ 4294967295U -// PPC:PTRDIFF_MIN_ (-2147483647 -1) -// PPC:PTRDIFF_MAX_ 2147483647 -// PPC:SIZE_MAX_ 4294967295U +// PPC:INTPTR_MIN_ (-2147483647L -1) +// PPC:INTPTR_MAX_ 2147483647L +// PPC:UINTPTR_MAX_ 4294967295UL +// PPC:PTRDIFF_MIN_ (-2147483647L -1) +// PPC:PTRDIFF_MAX_ 2147483647L +// PPC:SIZE_MAX_ 4294967295UL // // PPC:INTMAX_MIN_ (-9223372036854775807LL -1) // PPC:INTMAX_MAX_ 9223372036854775807LL @@ -879,8 +879,8 @@ // S390X:typedef int8_t int_fast8_t; // S390X:typedef uint8_t uint_fast8_t; // -// S390X:typedef int64_t intptr_t; -// S390X:typedef uint64_t uintptr_t; +// S390X:typedef long int intptr_t; +// S390X:typedef long unsigned int uintptr_t; // // S390X:typedef long int intmax_t; // S390X:typedef long unsigned int uintmax_t; @@ -986,8 +986,8 @@ // SPARC:typedef int8_t int_fast8_t; // SPARC:typedef uint8_t uint_fast8_t; // -// SPARC:typedef int32_t intptr_t; -// SPARC:typedef uint32_t uintptr_t; +// SPARC:typedef int intptr_t; +// SPARC:typedef unsigned int uintptr_t; // // SPARC:typedef long long int intmax_t; // SPARC:typedef long long unsigned int uintmax_t; @@ -1086,8 +1086,8 @@ // TCE:typedef int8_t int_fast8_t; // TCE:typedef uint8_t uint_fast8_t; // -// TCE:typedef int32_t intptr_t; -// TCE:typedef uint32_t uintptr_t; +// TCE:typedef int intptr_t; +// TCE:typedef unsigned int uintptr_t; // // TCE:typedef long int intmax_t; // TCE:typedef long unsigned int uintmax_t; @@ -1139,9 +1139,9 @@ // TCE:PTRDIFF_MAX_ 2147483647 // TCE:SIZE_MAX_ 4294967295U // -// TCE:INTMAX_MIN_ (-2147483647 -1) -// TCE:INTMAX_MAX_ 2147483647 -// TCE:UINTMAX_MAX_ 4294967295U +// TCE:INTMAX_MIN_ (-2147483647L -1) +// TCE:INTMAX_MAX_ 2147483647L +// TCE:UINTMAX_MAX_ 4294967295UL // // TCE:SIG_ATOMIC_MIN_ (-2147483647 -1) // TCE:SIG_ATOMIC_MAX_ 2147483647 @@ -1194,8 +1194,8 @@ // X86_64:typedef int8_t int_fast8_t; // X86_64:typedef uint8_t uint_fast8_t; // -// X86_64:typedef int64_t intptr_t; -// X86_64:typedef uint64_t uintptr_t; +// X86_64:typedef long int intptr_t; +// X86_64:typedef long unsigned int uintptr_t; // // X86_64:typedef long int intmax_t; // X86_64:typedef long unsigned int uintmax_t; @@ -1314,8 +1314,8 @@ // XCORE:typedef int8_t int_fast8_t; // XCORE:typedef uint8_t uint_fast8_t; // -// XCORE:typedef int32_t intptr_t; -// XCORE:typedef uint32_t uintptr_t; +// XCORE:typedef int intptr_t; +// XCORE:typedef unsigned int uintptr_t; // // XCORE:typedef long long int intmax_t; // XCORE:typedef long long unsigned int uintmax_t; @@ -1398,9 +1398,14 @@ // the identifiers used in the operations (int, uint, _t, INT, UINT, _MIN, // _MAX, and _C(v)) are themselves macros. // -// RUN: %clang_cc1 -E -ffreestanding -U__UINTMAX_TYPE__ -U__INTMAX_TYPE__ -Dint=a -Duint=b -D_t=c -DINT=d -DUINT=e -D_MIN=f -D_MAX=g '-D_C(v)=h' -triple=i386-none-none %s | FileCheck -check-prefix JOIN %s -// JOIN:typedef int32_t intptr_t; -// JOIN:typedef uint32_t uintptr_t; +// RUN: %clang_cc1 -E -ffreestanding \ +// RUN: -U__UINTPTR_TYPE__ -U__INTPTR_TYPE__ \ +// RUN: -U__UINTMAX_TYPE__ -U__INTMAX_TYPE__ \ +// RUN: -Dint=a -Duint=b -D_t=c -DINT=d -DUINT=e -D_MIN=f -D_MAX=g \ +// RUN: '-D_C(v)=h' -triple=i386-none-none %s \ +// RUN: | FileCheck -check-prefix JOIN %s +// JOIN:typedef __INTPTR_TYPE__ intptr_t; +// JOIN:typedef __UINTPTR_TYPE__ uintptr_t; // JOIN:typedef __INTMAX_TYPE__ intmax_t; // JOIN:typedef __UINTMAX_TYPE__ uintmax_t; // JOIN:INTPTR_MIN_ (-2147483647 -1) -- 2.7.4