This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Disallow assigning record lvalues with nested const-qualified fields.
ClosedPublic

Authored by bevinh on Aug 29 2017, 1:40 AM.

Details

Summary

According to C99 6.3.2.1p1, structs and unions with nested
const-qualified fields (that is, const-qualified fields
declared at some recursive level of the aggregate) are not
modifiable lvalues. However, Clang permits assignments of
these lvalues.

With this patch, we both prohibit the assignment of records
with const-qualified fields and emit a best-effort diagnostic.
This fixes https://bugs.llvm.org/show_bug.cgi?id=31796 .

Diff Detail

Repository
rL LLVM

Event Timeline

bevinh created this revision.Aug 29 2017, 1:40 AM
fhahn added a subscriber: fhahn.Aug 29 2017, 3:51 AM
bjope added a subscriber: bjope.Sep 6 2017, 6:41 AM
bjope added inline comments.Sep 6 2017, 11:35 PM
include/clang/Basic/DiagnosticSemaKinds.td
5888 ↗(On Diff #113038)

Shouldn't there be one more entry here as well?
(see comment about updating both err_typecheck_assign_const and note_typecheck_assign_const when adding things to the enum at line 10181).

If I understand it correctly you never print notes for the new NestedConstMember, so there isn't really a need for a fifth entry in the select today.
But if someone adds new enum values later (putting them after NestedContMember) then it might be nice if the size of this select matches with the old size of the enum.

lib/Sema/SemaExpr.cpp
10329 ↗(On Diff #113038)

If adding a new entry to note_typecheck_assign_const (identical to the ConstMember entry) then you can use NestedConstMember here. It would make it possible to change the string for NestedConstMember notes without also changing all ConstMember notes.

Now it kind of looks like an error that the error diagnostic is for a NestedConstMember, but the note is about a ConstMember. That might confuse someone, since it looks like a typo unless you know what is going on here.

bevinh updated this revision to Diff 114340.Sep 8 2017, 4:29 AM
bevinh marked an inline comment as done.

Added a diag note for NestedConstMember.

bjope added a comment.Sep 12 2017, 6:54 AM

This patch looks good to me now.
But I'm currently working in the same out-of-tree project as the author, so it would be nice if someone else could have a look as well and accept this.

Ka-Ka added a subscriber: Ka-Ka.Sep 13 2017, 10:37 AM
bjope accepted this revision.Sep 19 2017, 4:36 AM

Nobody else seems to bother. At least no objections, so I think we can land this now.

This revision is now accepted and ready to land.Sep 19 2017, 4:36 AM
This revision was automatically updated to reflect the committed changes.