[PATCH] D135750: [clang][Interp] Track initialization state of local variables

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 6 00:22:52 PST 2022


tbaeder marked 9 inline comments as done.
tbaeder added inline comments.


================
Comment at: clang/lib/AST/Interp/ByteCodeExprGen.cpp:835
     IsTemporary = true;
     Ty = E->getType();
   }
----------------
shafik wrote:
> Do we really want to the type of the expression? If we have a `ValueDecl` don't we want that type? I think they should be the same, do you have any examples where the expression is the type we want? I am looking at the AST from ` int x = 1+1L;` 
> 
> https://godbolt.org/z/q3Tr7Kxoq
I don't have a counter example but I didn't write that line either. The Expr case is for temporary values created for AST expressions, not sure what other type to use.


================
Comment at: clang/lib/AST/Interp/InterpBlock.h:97
   void invokeCtor() {
-    std::memset(data(), 0, getSize());
+    std::memset(rawData(), 0, Desc->getAllocSize());
     if (Desc->CtorFn)
----------------
aaron.ballman wrote:
> tbaeder wrote:
> > aaron.ballman wrote:
> > > tbaeder wrote:
> > > > aaron.ballman wrote:
> > > > > Why do we want to overwrite the metadata here?
> > > > This is only called after creating an new block, so nothing is being overwritten, the metadata hasn't been filled-in yet.
> > > Sounds like a good reason not to memset over that block then; it's useless work that will be thrown away anyway (I worry we may come to rely on this zero init accidentally)?
> > FWIW I looked into this and I think zeroing everything is what we want, so the initmap is null initially 
> Hmmm, would it make more sense to have the init map setting that itself (in `allocate()`) rather than relying on `invokeCtor()` to do it?
It's a bit blurry to me regarding the layering with Pointer/Descriptor. Descriptors don't know anything about the metadata, but pointers know that primitive arrays have an initmap. So to keep that consistent, we'd have to create a pointer to create the initmap.

But I don't think it's unreasonable to expect zero-initialization for types used in the interpreter.


================
Comment at: clang/test/AST/Interp/cxx20.cpp:93
+              // ref-note {{read of uninitialized object is not allowed in a constant expression}}
+}
+static_assert(initializedLocal3() == 20); // expected-error {{not an integral constant expression}} \
----------------
It might be interesting to know that the output for this test changes with https://reviews.llvm.org/D135750. After that patch, we only note that the 


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

https://reviews.llvm.org/D135750



More information about the cfe-commits mailing list