diff options
author | Aaron Ballman <aaron@aaronballman.com> | 2022-01-13 11:45:27 -0500 |
---|---|---|
committer | Aaron Ballman <aaron@aaronballman.com> | 2022-01-13 11:46:34 -0500 |
commit | bf7d9970ba0ac5ecfa1a469086f5789de5c94e3f (patch) | |
tree | 97e2f79a2f782595b2472aa1457327e4b3617b33 /clang/lib/Frontend/InitPreprocessor.cpp | |
parent | 7b9f5cbfa71f56173e7acb028ca28d15635ddfa8 (diff) | |
download | llvm-bf7d9970ba0ac5ecfa1a469086f5789de5c94e3f.zip llvm-bf7d9970ba0ac5ecfa1a469086f5789de5c94e3f.tar.gz llvm-bf7d9970ba0ac5ecfa1a469086f5789de5c94e3f.tar.bz2 |
Support the *_WIDTH macros in limits.h and stdint.h
This completes the implementation of
WG14 N2412 (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2412.pdf),
which standardizes C on a twos complement representation for integer
types. The only work that remained there was to define the correct
macros in the standard headers, which this patch does.
Diffstat (limited to 'clang/lib/Frontend/InitPreprocessor.cpp')
-rw-r--r-- | clang/lib/Frontend/InitPreprocessor.cpp | 71 |
1 files changed, 49 insertions, 22 deletions
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index ec40170..a9023a7 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -194,7 +194,7 @@ static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty, Builder.defineMacro(MacroName, TargetInfo::getTypeName(Ty)); } -static void DefineTypeWidth(StringRef MacroName, TargetInfo::IntType Ty, +static void DefineTypeWidth(const Twine &MacroName, TargetInfo::IntType Ty, const TargetInfo &TI, MacroBuilder &Builder) { Builder.defineMacro(MacroName, Twine(TI.getTypeWidth(Ty))); } @@ -205,6 +205,16 @@ static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth, Twine(BitWidth / TI.getCharWidth())); } +// This will generate a macro based on the prefix with `_MAX__` as the suffix +// for the max value representable for the type, and a macro with a `_WIDTH__` +// suffix for the width of the type. +static void DefineTypeSizeAndWidth(const Twine &Prefix, TargetInfo::IntType Ty, + const TargetInfo &TI, + MacroBuilder &Builder) { + DefineTypeSize(Prefix + "_MAX__", Ty, TI, Builder); + DefineTypeWidth(Prefix + "_WIDTH__", Ty, TI, Builder); +} + static void DefineExactWidthIntType(TargetInfo::IntType Ty, const TargetInfo &TI, MacroBuilder &Builder) { @@ -241,6 +251,8 @@ static void DefineExactWidthIntTypeSize(TargetInfo::IntType Ty, if (TypeWidth == 64) Ty = IsSigned ? TI.getInt64Type() : TI.getUInt64Type(); + // We don't need to define a _WIDTH macro for the exact-width types because + // we already know the width. const char *Prefix = IsSigned ? "__INT" : "__UINT"; DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder); } @@ -254,7 +266,12 @@ static void DefineLeastWidthIntType(unsigned TypeWidth, bool IsSigned, const char *Prefix = IsSigned ? "__INT_LEAST" : "__UINT_LEAST"; DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder); - DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder); + // We only want the *_WIDTH macro for the signed types to avoid too many + // predefined macros (the unsigned width and the signed width are identical.) + if (IsSigned) + DefineTypeSizeAndWidth(Prefix + Twine(TypeWidth), Ty, TI, Builder); + else + DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder); DefineFmt(Prefix + Twine(TypeWidth), Ty, TI, Builder); } @@ -268,8 +285,12 @@ static void DefineFastIntType(unsigned TypeWidth, bool IsSigned, const char *Prefix = IsSigned ? "__INT_FAST" : "__UINT_FAST"; DefineType(Prefix + Twine(TypeWidth) + "_TYPE__", Ty, Builder); - DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder); - + // We only want the *_WIDTH macro for the signed types to avoid too many + // predefined macros (the unsigned width and the signed width are identical.) + if (IsSigned) + DefineTypeSizeAndWidth(Prefix + Twine(TypeWidth), Ty, TI, Builder); + else + DefineTypeSize(Prefix + Twine(TypeWidth) + "_MAX__", Ty, TI, Builder); DefineFmt(Prefix + Twine(TypeWidth), Ty, TI, Builder); } @@ -887,20 +908,26 @@ static void InitializePredefinedMacros(const TargetInfo &TI, assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far"); Builder.defineMacro("__CHAR_BIT__", Twine(TI.getCharWidth())); + Builder.defineMacro("__BOOL_WIDTH__", Twine(TI.getBoolWidth())); + Builder.defineMacro("__SHRT_WIDTH__", Twine(TI.getShortWidth())); + Builder.defineMacro("__INT_WIDTH__", Twine(TI.getIntWidth())); + Builder.defineMacro("__LONG_WIDTH__", Twine(TI.getLongWidth())); + Builder.defineMacro("__LLONG_WIDTH__", Twine(TI.getLongLongWidth())); + DefineTypeSize("__SCHAR_MAX__", TargetInfo::SignedChar, TI, Builder); DefineTypeSize("__SHRT_MAX__", TargetInfo::SignedShort, TI, Builder); DefineTypeSize("__INT_MAX__", TargetInfo::SignedInt, TI, Builder); DefineTypeSize("__LONG_MAX__", TargetInfo::SignedLong, TI, Builder); DefineTypeSize("__LONG_LONG_MAX__", TargetInfo::SignedLongLong, TI, Builder); - DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder); - DefineTypeSize("__WINT_MAX__", TI.getWIntType(), TI, Builder); - DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder); - DefineTypeSize("__SIZE_MAX__", TI.getSizeType(), TI, Builder); + DefineTypeSizeAndWidth("__WCHAR", TI.getWCharType(), TI, Builder); + DefineTypeSizeAndWidth("__WINT", TI.getWIntType(), TI, Builder); + DefineTypeSizeAndWidth("__INTMAX", TI.getIntMaxType(), TI, Builder); + DefineTypeSizeAndWidth("__SIZE", TI.getSizeType(), TI, Builder); - DefineTypeSize("__UINTMAX_MAX__", TI.getUIntMaxType(), TI, Builder); - DefineTypeSize("__PTRDIFF_MAX__", TI.getPtrDiffType(0), TI, Builder); - DefineTypeSize("__INTPTR_MAX__", TI.getIntPtrType(), TI, Builder); - DefineTypeSize("__UINTPTR_MAX__", TI.getUIntPtrType(), TI, Builder); + DefineTypeSizeAndWidth("__UINTMAX", TI.getUIntMaxType(), TI, Builder); + DefineTypeSizeAndWidth("__PTRDIFF", TI.getPtrDiffType(0), TI, Builder); + DefineTypeSizeAndWidth("__INTPTR", TI.getIntPtrType(), TI, Builder); + DefineTypeSizeAndWidth("__UINTPTR", TI.getUIntPtrType(), TI, Builder); DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder); DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder); @@ -929,29 +956,29 @@ static void InitializePredefinedMacros(const TargetInfo &TI, DefineFmt("__UINTMAX", TI.getUIntMaxType(), TI, Builder); Builder.defineMacro("__UINTMAX_C_SUFFIX__", TI.getTypeConstantSuffix(TI.getUIntMaxType())); - DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder); DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Builder); DefineFmt("__PTRDIFF", TI.getPtrDiffType(0), TI, Builder); - DefineTypeWidth("__PTRDIFF_WIDTH__", TI.getPtrDiffType(0), TI, Builder); DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder); DefineFmt("__INTPTR", TI.getIntPtrType(), TI, Builder); - DefineTypeWidth("__INTPTR_WIDTH__", TI.getIntPtrType(), TI, Builder); DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder); DefineFmt("__SIZE", TI.getSizeType(), TI, Builder); - DefineTypeWidth("__SIZE_WIDTH__", TI.getSizeType(), TI, Builder); DefineType("__WCHAR_TYPE__", TI.getWCharType(), Builder); - DefineTypeWidth("__WCHAR_WIDTH__", TI.getWCharType(), TI, Builder); DefineType("__WINT_TYPE__", TI.getWIntType(), Builder); - DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder); - DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder); - DefineTypeSize("__SIG_ATOMIC_MAX__", TI.getSigAtomicType(), TI, Builder); + DefineTypeSizeAndWidth("__SIG_ATOMIC", TI.getSigAtomicType(), TI, Builder); DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder); DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder); - DefineTypeWidth("__UINTMAX_WIDTH__", TI.getUIntMaxType(), TI, Builder); DefineType("__UINTPTR_TYPE__", TI.getUIntPtrType(), Builder); DefineFmt("__UINTPTR", TI.getUIntPtrType(), TI, Builder); - DefineTypeWidth("__UINTPTR_WIDTH__", TI.getUIntPtrType(), TI, Builder); + + // The C standard requires the width of uintptr_t and intptr_t to be the same, + // per 7.20.2.4p1. Same for intmax_t and uintmax_t, per 7.20.2.5p1. + assert(TI.getTypeWidth(TI.getUIntPtrType()) == + TI.getTypeWidth(TI.getIntPtrType()) && + "uintptr_t and intptr_t have different widths?"); + assert(TI.getTypeWidth(TI.getUIntMaxType()) == + TI.getTypeWidth(TI.getIntMaxType()) && + "uintmax_t and intmax_t have different widths?"); if (TI.hasFloat16Type()) DefineFloatMacros(Builder, "FLT16", &TI.getHalfFormat(), "F16"); |