[PATCH] D124694: [randstruct] Move initializer check to be more effective

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 3 04:35:03 PDT 2022


aaron.ballman accepted this revision.
aaron.ballman added a comment.
This revision is now accepted and ready to land.

In D124694#3486547 <https://reviews.llvm.org/D124694#3486547>, @void wrote:

> In D124694#3485585 <https://reviews.llvm.org/D124694#3485585>, @aaron.ballman wrote:
>
>>   struct t {
>>      int a, b, c, d, e;
>>   } x = { .a = 2, 4, 5, 6 };
>>
>> This situation seems like it should be an error, shouldn't it? The user specified one designated initializer (yay, that one is correct), but the rest are positional initializers and so there's no telling what they actually initialize due to field randomization.
>
> That is diagnosed as an error. The issue is that after randomization, the `a` field is placed at the end of the structure. The initializer checker then sees the `.a = 2` and says, "Ah! That's the one at the end of the structure. Any non-designated initializers afterwards will be excess ones," which is what happens. But that warning is completely mysterious to the end users who isn't told that they can't have a non-designated initializer on a randomized structure. Moving the diagnostic check allows the correct warning to be emitted instead of the "excess elements" one.

Ohhhhh! Thank you for the extra explanation, that makes a lot more sense to me now. LGTM with a commenting request.



================
Comment at: clang/test/Sema/init-randomized-struct.c:1
-// RUN: %clang_cc1 -triple=x86_64-unknown-linux -frandomize-layout-seed=1234567890abcdef \
+// RUN: %clang_cc1 -triple=x86_64-unknown-linux -frandomize-layout-seed=1234567890abcded \
 // RUN:  -verify -fsyntax-only -Werror %s
----------------
void wrote:
> aaron.ballman wrote:
> > Why is this change needed?
> > 
> > (Also, shouldn't there be other test coverage added to the file?)
> That seed is how to replicate this issue. The test exists and is triggered by this change. I can add a comment to that effect.
Yeah, adding such a comment would be helpful, both for code archeology and as a hint to others editing the file in the future that this seed value is special and should not be changed.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D124694



More information about the cfe-commits mailing list