[clang] Using type deduction to define __SIZE_TYPE__ and __PTRDIFF_TYPE__ (PR #155979)

via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 8 09:02:23 PDT 2025


================
@@ -1175,13 +1175,15 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
   Builder.defineMacro("__UINTMAX_C_SUFFIX__", ConstSuffix);
   Builder.defineMacro("__UINTMAX_C(c)",
                       ConstSuffix.size() ? Twine("c##") + ConstSuffix : "c");
-  DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(LangAS::Default), Builder);
-  DefineFmt(LangOpts, "__PTRDIFF", TI.getPtrDiffType(LangAS::Default), TI,
-            Builder);
   DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Builder);
   DefineFmt(LangOpts, "__INTPTR", TI.getIntPtrType(), TI, Builder);
-  DefineType("__SIZE_TYPE__", TI.getSizeType(), Builder);
+  // Use deduction to obtain the built-in __size_t and __ptrdiff_t types instead
+  // of standard integer types
+  Builder.defineMacro("__SIZE_TYPE__", "__typeof(sizeof(0))");
----------------
YexuanXiao wrote:

I enhanced the check for the `%zu` format specifier in #143653, but [some people](https://github.com/llvm/llvm-project/pull/143653#issuecomment-3086499400) reported that it generated a lot of false positives. As a result, the check was eventually removed. The reason was that calculating `skb->len - sizeof(_arph)` loses all the sugar types. If the PR gets accepted, I can add a new option to enable this enhanced check (under the condition that the libc defines `size_t` in this way).

https://github.com/llvm/llvm-project/pull/155979


More information about the cfe-commits mailing list