[clang] [Clang] Make the result type of sizeof/pointer subtraction/size_t lit… (PR #136542)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 21 08:29:04 PDT 2025


AaronBallman wrote:

> > I don't think we can do that, at least for C, because that means this code would then be accepted when it shouldn't be, right?
> 
> Yeah, I am not sure we can do this now then. It seems odd to allow calls to builtins to change their return 'type' (or at least, return sugar) based on when they are included. Even if we did, we would have to make sure it actually WAS SIZE_TYPE and not a different typedef.
> 
> So I guess we're back to: are we ok with this?
> 
> ```
> sizeof(int); // returns an unsigned integer type
> #include <stddef.h>
> sizeof(int); // now returns a typedef to an unsigned integer type
> ```

That's why I was thinking the better approach is for both to return `__SIZE_TYPE__` but instead of `__SIZE_TYPE__` being explicitly `unsigned int` (or similar), it is a new type named `__size_t` which is compatible with what `__SIZE_TYPE__` used to expand to for each target. Then diagnostics, clangd, other tooling, etc has a way to differentiate the size (and ptrdiff) type from the underlying type when there are no typedefs present.

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


More information about the cfe-commits mailing list