This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Correctly handle evaluation order of designated initializers.
ClosedPublic

Authored by mboehme on Mar 13 2023, 2:15 AM.

Details

Summary

As designated initializers show up only in the syntactic form of the
InitListExpr, we need to make sure we're searching both forms of the
InitListExpr when determining successors in the evaluation order.

This fixes a bug in bugprone-use-after-move where previously we erroneously
concluded that two designated initializers were unsequenced. The newly added
tests fail without the fix.

Diff Detail

Event Timeline

mboehme created this revision.Mar 13 2023, 2:15 AM
mboehme requested review of this revision.Mar 13 2023, 2:15 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 13 2023, 2:15 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
PiotrZSL added inline comments.Mar 13 2023, 10:21 AM
clang-tools-extra/clang-tidy/utils/ExprSequence.cpp
67

maybe some other name instead of allInitListExprForms, like getAllInitForms

69–72

this looks like typo... but it isn't, it's just unnecessary.

`InitList->isSemanticForm() && InitList->getSyntacticForm()` is equal to:
`AltForm.getInt() && AltForm.getPointer()`

`InitList->isSyntacticForm() && InitList->getSemanticForm()` is equal to:
`(!AltForm.getInt() || !AltForm.getPointer()) && (!AltForm.getInt()) && AltForm.getPointer()`

Why we coudn't have "getAnyForm".

any code could just look like this:
`if (const InitListExpr * Form = InitList->getSyntacticForm())`
` result.push_back(Form);`
`if (const InitListExpr * Form = InitList->getSemanticForm())`
` result.push_back(Form);`

mboehme updated this revision to Diff 505083.Mar 14 2023, 7:07 AM

Changes in response to review comments.

mboehme added inline comments.Mar 14 2023, 7:11 AM
clang-tools-extra/clang-tidy/utils/ExprSequence.cpp
67

How about this?

69–72

this looks like typo... but it isn't, it's just unnecessary.

Ah, of course, thanks for pointing this out to me.

Why we coudn't have "getAnyForm".

Not sure exactly what you mean?

I've updated the code to do what you suggested above (i.e. don't call isSemanticForm(), just call getSyntacticForm() and see if returns non-null).

We still need to initialize result with InitList because InitList->getSyntacticForm() returns null if InitList is already in syntactic form.

I hope that makes sense? Please let me know if there's anything I missed from your original response.

PiotrZSL accepted this revision.Mar 14 2023, 7:37 AM

Looks good to me, consider adding info to Release notes, that this check has been improved/fixed support for designated initializers.

This revision is now accepted and ready to land.Mar 14 2023, 7:37 AM
mboehme updated this revision to Diff 505714.Mar 16 2023, 12:34 AM

Added release notes

Looks good to me, consider adding info to Release notes, that this check has been improved/fixed support for designated initializers.

Good point, done!

Regarding release-notes:
I would probably wrote something like "fixed handling for designated initializers", nonene will understand things like "sequence point".
If you decide to change this, the just commit this as an [NFC] without review.
And checks in this section should be in alphabetical order (so this one shouldn't be at the end), but that's a different story that can be corrected later for all those entrys.

Regarding release-notes:
I would probably wrote something like "fixed handling for designated initializers", nonene will understand things like "sequence point".

Thanks, makes sense.

I've got another fix for the same checker in the pipeline anyway, so I've decided to roll this rewording into that patch: https://reviews.llvm.org/D145581

If you decide to change this, the just commit this as an [NFC] without review.
And checks in this section should be in alphabetical order (so this one shouldn't be at the end), but that's a different story that can be corrected later for all those entrys.

Right, I remember that too now...

As there are other entries that are also out of order, I'll just leave this where it is for now.