[PATCH] D133934: [clang][Interp] Handle sizeof() expressions

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 15 23:01:23 PDT 2022


tbaeder added a comment.

I can see that `HandleSizeOf()` uses 1 for void and function types as a gcc extension, but I can't reproduce that: https://godbolt.org/z/njG9zh6PM



================
Comment at: clang/lib/AST/Interp/ByteCodeExprGen.cpp:290
+    return this->emitConst(
+        E, Ctx.getASTContext().getTypeSizeInChars(ArgType).getQuantity());
+  }
----------------
shafik wrote:
> aaron.ballman wrote:
> > erichkeane wrote:
> > > shafik wrote:
> > > > I notice that `HandleSizeof` special cases `void` and function types. 
> > > OOOH, DEFINITELY make sure you test function types here, and figure out how HandleSizeof does it.
> > > 
> > > BUT, I think 'void' should error/be an invalid before we get here?
> > > 
> > > ALSO, now that I think further, I'm sure @aaron.ballman has a bunch of runtime-evaluated sizeof examples to share around vlas.
> > I was just about to comment about everyone's favorite horrible C++ extension. :-D We should make sure we properly reject code like:
> > ```
> > void func() {
> >   int n = 12;
> >   constexpr int oofda = sizeof(int[n++]);
> > }
> > ```
> > while accepting code like:
> > ```
> > consteval int foo(int n) {
> >   return sizeof(int[n]);
> > }
> > constinit int var = foo(5);
> > ```
> Note, that clang currently treats that as ill-formed and there is divergence with how gcc and clang treat the `constexpr` case as well.
> 
> So if we are going to say we want this to work then we should file a bug against clang. 
Right, the second example doesn't seem to be accepted by current clang.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133934/new/

https://reviews.llvm.org/D133934



More information about the cfe-commits mailing list