Skip to content

Consider forbidding non-constant expressions in offsetof #47751

@trofi

Description

@trofi
Bugzilla Link 48407
Version unspecified
OS Linux
CC @AaronBallman,@DougGregor,@efriedma-quic,@zygoloid

Extended Description

gcc-11 will deliberately become stricter and will disallow non-constant expressions in offsetof: https://gcc.gnu.org/#95942

Complete example:

  // $ cat.bug.cc
  struct a {
    int b;
    char c[100];
  };
  unsigned long d(long e) { return __builtin_offsetof(a, c[e]); }
$ g++-11.0.0 -c bug.cc -o bug.o
bug.cc: In function 'long unsigned int d(long int)':
bug.cc:6:60: error: 'e' is not a constant expression
    6 |   unsigned long d(long e) { return __builtin_offsetof(a, c[e]); }
      |                                                            ^
$  clang++-11 -c bug.cc -o bug.o
<ok>

Should clang also reject the code above? If not should clang emit warning on -Wall that non-standard extension is being used?

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillac++clang:frontendLanguage frontend issues, e.g. anything involving "Sema"cwg-issueAn issue that was filed to the Core Working Groupwaiting-for-wg21Blocked on C++ Standards Committee

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions