This is an archive of the discontinued LLVM Phabricator instance.

[Clang] Don't tell people to place _Alignas on a struct in diagnostics
Needs ReviewPublic

Authored by theo-lw on Jan 6 2023, 7:34 PM.

Details

Reviewers
aaron.ballman
Summary

This changes the diagnostic for _Alignas so it no longer tells people to place _Alignas after the "struct" keyword.

Fixes https://github.com/llvm/llvm-project/issues/58637

Diff Detail

Event Timeline

theo-lw created this revision.Jan 6 2023, 7:34 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 6 2023, 7:34 PM
theo-lw requested review of this revision.Jan 6 2023, 7:34 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 6 2023, 7:34 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
theo-lw updated this revision to Diff 487103.Jan 7 2023, 10:00 AM

Accidentally deleted a line causing this to not compile :(

theo-lw updated this revision to Diff 487119.Jan 7 2023, 1:33 PM

Didn't realize this test was failing so I've updated it.

Thank you for the fix, please be sure to add a release note for the fix as well. Also, I'd like to see some additional C++ test coverage for: alignas(int) struct S {}; to demonstrate we still suggest moving the keyword in that case.

clang/lib/Sema/SemaDecl.cpp
5296

I think this isn't quite right -- it'll return true in C++ for alignas: alignas(int) struct S {}; will give the wrong diagnostic now, I suspect. You need to check the language mode as well to ensure we're not in C++ mode.

5299–5304
5312–5317

Pretty sure you can use something like this instead now.

clang/test/C/drs/dr4xx.c
167

This diagnostic is incorrect but should be fixed with the suggested edit above.

clang/test/Parser/c1x-alignas.c
12–13

Same here.

@theo-lw -- are you planning to continue working on this review? (It's okay if you're not, I'm more wondering if I should remove you as the assignee on https://github.com/llvm/llvm-project/issues/58637)

@theo-lw -- are you planning to continue working on this review? (It's okay if you're not, I'm more wondering if I should remove you as the assignee on https://github.com/llvm/llvm-project/issues/58637)

Hey! Sorry, got busy with school. I can continue working on this.

@theo-lw -- are you planning to continue working on this review? (It's okay if you're not, I'm more wondering if I should remove you as the assignee on https://github.com/llvm/llvm-project/issues/58637)

Hey! Sorry, got busy with school. I can continue working on this.

No worries, it happens! Thank you for continuing work on this -- if you find you don't have the time, it's not a big deal, just let us know. :-)

theo-lw updated this revision to Diff 552061.Aug 21 2023, 9:44 AM

Rebased on the main branch & addressed comments.

Added additional test case for C++ alignas.

theo-lw updated this revision to Diff 552181.Aug 21 2023, 6:22 PM

Ran clang-format on this.

aaron.ballman added inline comments.Aug 22 2023, 5:43 AM
clang/lib/Sema/SemaDecl.cpp
5337–5346

I think this might be a bit more clear:

unsigned DiagID;
if (AL.isC11AlignasAttribute()) {
  // Don't use the message with placement with _Alignas.
  // This is because C doesnt let you use _Alignas on type declarations.
  DiagID = diag::warn_attribute_ignored;
} else if (AL.isRegularKeywordAttribute())
  DiagID = diag::err_declspec_keyword_has_no_effect;
else
  DiagID = diag::warn_declspec_attribute_ignored;

Diag(AL.getLoc(), DiagID) << AL << GetDiagnosticTypeSpecifierID(DS);
clang/test/Parser/c1x-alignas.c
12

I'd like to see a C23-specific test that does:

alignas(int) struct c7;

(the test doesn't have to be in this file, but it's to validate that we correctly handle alignas when it's a keyword that aliases to _Alignas.)