Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing diagnostic for non-standard layout types in offsetof #64619

Closed
Endilll opened this issue Aug 11, 2023 · 10 comments · Fixed by #65246
Closed

Missing diagnostic for non-standard layout types in offsetof #64619

Endilll opened this issue Aug 11, 2023 · 10 comments · Fixed by #65246
Assignees
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer confirmed Verified by a second party good first issue https://github.com/llvm/llvm-project/contribute

Comments

@Endilll
Copy link
Contributor

Endilll commented Aug 11, 2023

Consider the following example (https://godbolt.org/z/o77v1nad9):

struct X1 { int a; };
struct X2 { int b; };
struct Y : X1, X2 {};

static_assert(__builtin_offsetof(Y, b) == 4);
static_assert(offsetof(Y, b) == 4);

static_assert(!std::is_standard_layout_v<Y>, "");

Only GCC warns that non-standard layout type is passed to offsetof, which is conditionally-supported.

<source>:8:34: warning: 'offsetof' within non-standard-layout type 'Y' is conditionally-supported [-Winvalid-offsetof]
    8 | static_assert(__builtin_offsetof(Y, b) == 4);
      |               ~~~~~~~~~~~~~~~~~~~^~~~~
In file included from /opt/compiler-explorer/gcc-trunk-20230811/include/c++/14.0.0/cstddef:50,
                 from <source>:1:
<source>:9:24: warning: 'offsetof' within non-standard-layout type 'Y' is conditionally-supported [-Winvalid-offsetof]
    9 | static_assert(offsetof(Y, b) == 4);
      |                        ^

GCC issues it by default, whereas Clang, MSVC, and EDG doesn't even in pedantic modes (or /W4).

I'm not sure what the solution should be, but I feel like there is a problem to solve.

@Endilll Endilll added the clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer label Aug 11, 2023
@Endilll
Copy link
Contributor Author

Endilll commented Aug 11, 2023

CC @AaronBallman
This might be a good first issue, depending on how we're going to resolve this.

@shafik
Copy link
Collaborator

shafik commented Aug 11, 2023

Interesting we do have ext_offsetof_non_standardlayout_type and we do use the diagnostic here

// C++11 [support.types]p4:
// If type is not a standard-layout class (Clause 9), the results are
// undefined.
if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) {
bool IsSafe = LangOpts.CPlusPlus11? CRD->isStandardLayout() : CRD->isPOD();
unsigned DiagID =
LangOpts.CPlusPlus11? diag::ext_offsetof_non_standardlayout_type
: diag::ext_offsetof_non_pod_type;

I haven't debugged it to see why we don't catch this case.

@cor3ntin
Copy link
Contributor

We only warn in evaluated context. I don't know if that make sense https://godbolt.org/z/MGTWofPxY
Maybe we don't want to use DiagRuntimeBehavior here

@AaronBallman
Copy link
Collaborator

We only warn in evaluated context. I don't know if that make sense https://godbolt.org/z/MGTWofPxY Maybe we don't want to use DiagRuntimeBehavior here

It's been this way since it was added in 882211c in 2010. That said, I'm not certain why this should be issued only when evaluated at runtime. My guess is that it's a runtime diagnostic because the offset is expected to be used at runtime to offset into some memory buffer; so if you never reach that code, the fact that it's conditionally supported doesn't matter. But I'm not certain if that's an accurate guess -- I could not find a code review to see if this was explicitly discussed.

@cor3ntin
Copy link
Contributor

cor3ntin commented Aug 11, 2023

I can see that reasoning be problematic, for example:

void f() {
   constexpr auto idx = offsetof(nonstandardlayout, foo); 
   loose_a_limb(idx);
}

@AaronBallman
Copy link
Collaborator

Yeah, basically 2010 predates thinking about constexpr too much, so this might have made more sense then and less sense now.

@cor3ntin cor3ntin added the good first issue https://github.com/llvm/llvm-project/contribute label Aug 11, 2023
@llvmbot
Copy link

llvmbot commented Aug 11, 2023

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Assign the issue to you.
  2. Fix the issue locally.
  3. Run the test suite locally.
    3.1) Remember that the subdirectories under test/ create fine-grained testing targets, so you can
    e.g. use make check-clang-ast to only run Clang's AST tests.
  4. Create a git commit
  5. Run git clang-format HEAD~1 to format your changes.
  6. Submit the patch to Phabricator.
    6.1) Detailed instructions can be found here

For more instructions on how to submit a patch to LLVM, see our documentation.

If you have any further questions about this issue, don't hesitate to ask via a comment on this Github issue.

@llvm/issue-subscribers-good-first-issue

@Endilll Endilll added the confirmed Verified by a second party label Aug 11, 2023
@kasuga-fj
Copy link
Contributor

Hi, I am new to LLVM and would like to try to see if this issue is still open. Could someone please set up an assignee for me? (I'll send a patch after the pull request migration is done)

@kawashima-fj
Copy link
Member

Hi, I am new to LLVM and would like to try to see if this issue is still open.

Great.

Could someone please set up an assignee for me?

Done.

@kasuga-fj
Copy link
Contributor

Thank you

kasuga-fj added a commit to kasuga-fj/llvm-project that referenced this issue Oct 6, 2023
`offsetof`

Fixes llvm#64619

Clang warns diagnostic for non-standard layout types in `offsetof` only
if they are in evaluated context. With this patch, you'll also get
diagnostic if you use `offsetof` on non-standard layout types in any
other contexts
kasuga-fj added a commit that referenced this issue Oct 10, 2023
…tof` (#65246)

Fixes #64619

Clang warns diagnostic for non-standard layout types in `offsetof` only
if they are in evaluated context. With this patch, you'll also get
diagnostic if you use `offsetof` on non-standard layout types in any
other contexts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer confirmed Verified by a second party good first issue https://github.com/llvm/llvm-project/contribute
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants