This is an archive of the discontinued LLVM Phabricator instance.

[C11] Improve the diagnostic when accessing a member of an atomic struct
ClosedPublic

Authored by aaron.ballman on Mar 29 2022, 7:16 AM.

Details

Reviewers
eli.friedman
rjmccall
jyknight
erichkeane
Group Reviewers
Restricted Project
Summary

Member access for an atomic structure or union is unconditional undefined behavior (C11 6.5.2.3p5). However, we would issue a confusing error message about the base expression not being a structure or union type.

GCC issues a warning for this case. Clang now warns as well, but the warning is defaulted to an error because the actual access is still unsafe.

This fixes Issue 54563.

Diff Detail

Event Timeline

aaron.ballman created this revision.Mar 29 2022, 7:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 29 2022, 7:16 AM
aaron.ballman requested review of this revision.Mar 29 2022, 7:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 29 2022, 7:16 AM
erichkeane added inline comments.Mar 29 2022, 7:22 AM
clang/lib/Sema/SemaExprMember.cpp
1300

This seems to apply to both C and C++. I guess "_Atomic" is C only, so we get to define its behavior for C++?

What does GCC do in C++ mode?

clang/test/Sema/atomic-expr.c
85

This still catches RHS access as well, right?

Also, does it still 'work' with the non-qualifier version of _Atomic?

aaron.ballman marked 2 inline comments as done.Mar 29 2022, 8:19 AM
aaron.ballman added inline comments.
clang/lib/Sema/SemaExprMember.cpp
1300

Clang supports _Atomic in C++ as an extension (with the C semantics), GCC does not.

However, after doing some testing, I actually question whether Clang supports _Atomic in C++ more or just "pretends everything will work out fine in C++ mode but nobody ever actually checked that". I'm not going to add additional test coverage for this because I'm going to see just how many issues I spot in C++ first (it may be a better approach to disable the extension in C++).

clang/test/Sema/atomic-expr.c
85

This still catches RHS access as well, right?

Yes, I'll add tests.

Also, does it still 'work' with the non-qualifier version of _Atomic?

Yes, I'll add a test.

aaron.ballman marked 2 inline comments as done.

Added test cases based on review feedback.

erichkeane accepted this revision.Mar 29 2022, 8:22 AM
erichkeane added inline comments.
clang/lib/Sema/SemaExprMember.cpp
1300

Based on the examples I've seen, I think we're better off matching GCC's behavior here of disallowing _Atomic in both forms in C++, unless it is necessary/used to implement the std::atomic version in libc++ (obviously it isn't in libstdc++ if they disable it in c++ mode?).

This revision is now accepted and ready to land.Mar 29 2022, 8:22 AM
aaron.ballman closed this revision.Mar 29 2022, 9:16 AM

Thanks for the review, I've commit in 3c84e4a0dbd08fc03bbcdd8354a984e0efcf7672