[PATCH] D125167: Fix member access of anonymous struct/union fields in C

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat May 7 07:39:56 PDT 2022


aaron.ballman created this revision.
aaron.ballman added reviewers: rsmith, nickdesaulniers, erichkeane, jyknight, clang-language-wg.
Herald added a project: All.
aaron.ballman requested review of this revision.
Herald added a project: clang.

We were accepting invalid code where the qualifiers of the anonymous structure were not taken into account when forming a member access to one of the indirect fields, as in:

  struct S {
    const struct {
      int i;
    };
  } s;
  
  void foo(void) {
    s.i = 12; // previously accepted, now rejected
  }

We now expose the anonymous structure's field as having a qualified type instead of an unqualified type so that access checking notes that an intermediate object in the expression is qualified. We also adjusted the diagnostic so that it's slightly more user friendly despite this being a rather rare case.

Note, this only impacts C; in C++ and with the Microsoft C anonymous structure extension, the qualifiers are stripped.

Fixes #48099


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125167

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/test/Sema/anonymous-struct-union.c
  clang/test/Sema/anonymous-struct-union.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125167.427864.patch
Type: text/x-patch
Size: 8912 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220507/19552b4b/attachment.bin>


More information about the cfe-commits mailing list