@@ -194,7 +194,7 @@ static void DefineType(const Twine &MacroName, TargetInfo::IntType Ty,
194
194
Builder.defineMacro (MacroName, TargetInfo::getTypeName (Ty));
195
195
}
196
196
197
- static void DefineTypeWidth (StringRef MacroName, TargetInfo::IntType Ty,
197
+ static void DefineTypeWidth (const Twine & MacroName, TargetInfo::IntType Ty,
198
198
const TargetInfo &TI, MacroBuilder &Builder) {
199
199
Builder.defineMacro (MacroName, Twine (TI.getTypeWidth (Ty)));
200
200
}
@@ -205,6 +205,16 @@ static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth,
205
205
Twine (BitWidth / TI.getCharWidth ()));
206
206
}
207
207
208
+ // This will generate a macro based on the prefix with `_MAX__` as the suffix
209
+ // for the max value representable for the type, and a macro with a `_WIDTH__`
210
+ // suffix for the width of the type.
211
+ static void DefineTypeSizeAndWidth (const Twine &Prefix, TargetInfo::IntType Ty,
212
+ const TargetInfo &TI,
213
+ MacroBuilder &Builder) {
214
+ DefineTypeSize (Prefix + " _MAX__" , Ty, TI, Builder);
215
+ DefineTypeWidth (Prefix + " _WIDTH__" , Ty, TI, Builder);
216
+ }
217
+
208
218
static void DefineExactWidthIntType (TargetInfo::IntType Ty,
209
219
const TargetInfo &TI,
210
220
MacroBuilder &Builder) {
@@ -241,6 +251,8 @@ static void DefineExactWidthIntTypeSize(TargetInfo::IntType Ty,
241
251
if (TypeWidth == 64 )
242
252
Ty = IsSigned ? TI.getInt64Type () : TI.getUInt64Type ();
243
253
254
+ // We don't need to define a _WIDTH macro for the exact-width types because
255
+ // we already know the width.
244
256
const char *Prefix = IsSigned ? " __INT" : " __UINT" ;
245
257
DefineTypeSize (Prefix + Twine (TypeWidth) + " _MAX__" , Ty, TI, Builder);
246
258
}
@@ -254,7 +266,12 @@ static void DefineLeastWidthIntType(unsigned TypeWidth, bool IsSigned,
254
266
255
267
const char *Prefix = IsSigned ? " __INT_LEAST" : " __UINT_LEAST" ;
256
268
DefineType (Prefix + Twine (TypeWidth) + " _TYPE__" , Ty, Builder);
257
- DefineTypeSize (Prefix + Twine (TypeWidth) + " _MAX__" , Ty, TI, Builder);
269
+ // We only want the *_WIDTH macro for the signed types to avoid too many
270
+ // predefined macros (the unsigned width and the signed width are identical.)
271
+ if (IsSigned)
272
+ DefineTypeSizeAndWidth (Prefix + Twine (TypeWidth), Ty, TI, Builder);
273
+ else
274
+ DefineTypeSize (Prefix + Twine (TypeWidth) + " _MAX__" , Ty, TI, Builder);
258
275
DefineFmt (Prefix + Twine (TypeWidth), Ty, TI, Builder);
259
276
}
260
277
@@ -268,8 +285,12 @@ static void DefineFastIntType(unsigned TypeWidth, bool IsSigned,
268
285
269
286
const char *Prefix = IsSigned ? " __INT_FAST" : " __UINT_FAST" ;
270
287
DefineType (Prefix + Twine (TypeWidth) + " _TYPE__" , Ty, Builder);
271
- DefineTypeSize (Prefix + Twine (TypeWidth) + " _MAX__" , Ty, TI, Builder);
272
-
288
+ // We only want the *_WIDTH macro for the signed types to avoid too many
289
+ // predefined macros (the unsigned width and the signed width are identical.)
290
+ if (IsSigned)
291
+ DefineTypeSizeAndWidth (Prefix + Twine (TypeWidth), Ty, TI, Builder);
292
+ else
293
+ DefineTypeSize (Prefix + Twine (TypeWidth) + " _MAX__" , Ty, TI, Builder);
273
294
DefineFmt (Prefix + Twine (TypeWidth), Ty, TI, Builder);
274
295
}
275
296
@@ -887,20 +908,26 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
887
908
assert (TI.getCharWidth () == 8 && " Only support 8-bit char so far" );
888
909
Builder.defineMacro (" __CHAR_BIT__" , Twine (TI.getCharWidth ()));
889
910
911
+ Builder.defineMacro (" __BOOL_WIDTH__" , Twine (TI.getBoolWidth ()));
912
+ Builder.defineMacro (" __SHRT_WIDTH__" , Twine (TI.getShortWidth ()));
913
+ Builder.defineMacro (" __INT_WIDTH__" , Twine (TI.getIntWidth ()));
914
+ Builder.defineMacro (" __LONG_WIDTH__" , Twine (TI.getLongWidth ()));
915
+ Builder.defineMacro (" __LLONG_WIDTH__" , Twine (TI.getLongLongWidth ()));
916
+
890
917
DefineTypeSize (" __SCHAR_MAX__" , TargetInfo::SignedChar, TI, Builder);
891
918
DefineTypeSize (" __SHRT_MAX__" , TargetInfo::SignedShort, TI, Builder);
892
919
DefineTypeSize (" __INT_MAX__" , TargetInfo::SignedInt, TI, Builder);
893
920
DefineTypeSize (" __LONG_MAX__" , TargetInfo::SignedLong, TI, Builder);
894
921
DefineTypeSize (" __LONG_LONG_MAX__" , TargetInfo::SignedLongLong, TI, Builder);
895
- DefineTypeSize ( " __WCHAR_MAX__ " , TI.getWCharType (), TI, Builder);
896
- DefineTypeSize ( " __WINT_MAX__ " , TI.getWIntType (), TI, Builder);
897
- DefineTypeSize ( " __INTMAX_MAX__ " , TI.getIntMaxType (), TI, Builder);
898
- DefineTypeSize ( " __SIZE_MAX__ " , TI.getSizeType (), TI, Builder);
922
+ DefineTypeSizeAndWidth ( " __WCHAR " , TI.getWCharType (), TI, Builder);
923
+ DefineTypeSizeAndWidth ( " __WINT " , TI.getWIntType (), TI, Builder);
924
+ DefineTypeSizeAndWidth ( " __INTMAX " , TI.getIntMaxType (), TI, Builder);
925
+ DefineTypeSizeAndWidth ( " __SIZE " , TI.getSizeType (), TI, Builder);
899
926
900
- DefineTypeSize ( " __UINTMAX_MAX__ " , TI.getUIntMaxType (), TI, Builder);
901
- DefineTypeSize ( " __PTRDIFF_MAX__ " , TI.getPtrDiffType (0 ), TI, Builder);
902
- DefineTypeSize ( " __INTPTR_MAX__ " , TI.getIntPtrType (), TI, Builder);
903
- DefineTypeSize ( " __UINTPTR_MAX__ " , TI.getUIntPtrType (), TI, Builder);
927
+ DefineTypeSizeAndWidth ( " __UINTMAX " , TI.getUIntMaxType (), TI, Builder);
928
+ DefineTypeSizeAndWidth ( " __PTRDIFF " , TI.getPtrDiffType (0 ), TI, Builder);
929
+ DefineTypeSizeAndWidth ( " __INTPTR " , TI.getIntPtrType (), TI, Builder);
930
+ DefineTypeSizeAndWidth ( " __UINTPTR " , TI.getUIntPtrType (), TI, Builder);
904
931
905
932
DefineTypeSizeof (" __SIZEOF_DOUBLE__" , TI.getDoubleWidth (), TI, Builder);
906
933
DefineTypeSizeof (" __SIZEOF_FLOAT__" , TI.getFloatWidth (), TI, Builder);
@@ -929,29 +956,29 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
929
956
DefineFmt (" __UINTMAX" , TI.getUIntMaxType (), TI, Builder);
930
957
Builder.defineMacro (" __UINTMAX_C_SUFFIX__" ,
931
958
TI.getTypeConstantSuffix (TI.getUIntMaxType ()));
932
- DefineTypeWidth (" __INTMAX_WIDTH__" , TI.getIntMaxType (), TI, Builder);
933
959
DefineType (" __PTRDIFF_TYPE__" , TI.getPtrDiffType (0 ), Builder);
934
960
DefineFmt (" __PTRDIFF" , TI.getPtrDiffType (0 ), TI, Builder);
935
- DefineTypeWidth (" __PTRDIFF_WIDTH__" , TI.getPtrDiffType (0 ), TI, Builder);
936
961
DefineType (" __INTPTR_TYPE__" , TI.getIntPtrType (), Builder);
937
962
DefineFmt (" __INTPTR" , TI.getIntPtrType (), TI, Builder);
938
- DefineTypeWidth (" __INTPTR_WIDTH__" , TI.getIntPtrType (), TI, Builder);
939
963
DefineType (" __SIZE_TYPE__" , TI.getSizeType (), Builder);
940
964
DefineFmt (" __SIZE" , TI.getSizeType (), TI, Builder);
941
- DefineTypeWidth (" __SIZE_WIDTH__" , TI.getSizeType (), TI, Builder);
942
965
DefineType (" __WCHAR_TYPE__" , TI.getWCharType (), Builder);
943
- DefineTypeWidth (" __WCHAR_WIDTH__" , TI.getWCharType (), TI, Builder);
944
966
DefineType (" __WINT_TYPE__" , TI.getWIntType (), Builder);
945
- DefineTypeWidth (" __WINT_WIDTH__" , TI.getWIntType (), TI, Builder);
946
- DefineTypeWidth (" __SIG_ATOMIC_WIDTH__" , TI.getSigAtomicType (), TI, Builder);
947
- DefineTypeSize (" __SIG_ATOMIC_MAX__" , TI.getSigAtomicType (), TI, Builder);
967
+ DefineTypeSizeAndWidth (" __SIG_ATOMIC" , TI.getSigAtomicType (), TI, Builder);
948
968
DefineType (" __CHAR16_TYPE__" , TI.getChar16Type (), Builder);
949
969
DefineType (" __CHAR32_TYPE__" , TI.getChar32Type (), Builder);
950
970
951
- DefineTypeWidth (" __UINTMAX_WIDTH__" , TI.getUIntMaxType (), TI, Builder);
952
971
DefineType (" __UINTPTR_TYPE__" , TI.getUIntPtrType (), Builder);
953
972
DefineFmt (" __UINTPTR" , TI.getUIntPtrType (), TI, Builder);
954
- DefineTypeWidth (" __UINTPTR_WIDTH__" , TI.getUIntPtrType (), TI, Builder);
973
+
974
+ // The C standard requires the width of uintptr_t and intptr_t to be the same,
975
+ // per 7.20.2.4p1. Same for intmax_t and uintmax_t, per 7.20.2.5p1.
976
+ assert (TI.getTypeWidth (TI.getUIntPtrType ()) ==
977
+ TI.getTypeWidth (TI.getIntPtrType ()) &&
978
+ " uintptr_t and intptr_t have different widths?" );
979
+ assert (TI.getTypeWidth (TI.getUIntMaxType ()) ==
980
+ TI.getTypeWidth (TI.getIntMaxType ()) &&
981
+ " uintmax_t and intmax_t have different widths?" );
955
982
956
983
if (TI.hasFloat16Type ())
957
984
DefineFloatMacros (Builder, " FLT16" , &TI.getHalfFormat (), " F16" );
0 commit comments