theo-lw created this revision.
theo-lw added a reviewer: aaron.ballman.
Herald added a project: All.
theo-lw requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
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
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D141177
Files:
clang/lib/Sema/SemaDecl.cpp
clang/test/Parser/c1x-alignas.c
Index: clang/test/Parser/c1x-alignas.c
===================================================================
--- clang/test/Parser/c1x-alignas.c
+++ clang/test/Parser/c1x-alignas.c
@@ -9,5 +9,7 @@
char _Alignas(_Alignof(int)) c5;
+_Alignas(int) struct c6; // expected-warning {{1 attribute ignored}}
+
// CHECK-EXT: '_Alignas' is a C11 extension
// CHECK-EXT: '_Alignof' is a C11 extension
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -5291,18 +5291,29 @@
// __attribute__((aligned)) struct A;
// Attributes should be placed after tag to apply to type declaration.
if (!DS.getAttributes().empty() || !DeclAttrs.empty()) {
- DeclSpec::TST TypeSpecType = DS.getTypeSpecType();
+ auto WarnAttributeIgnored = [this, TypeSpecType](const ParsedAttr &AL) {
+ if (AL.isAlignasAttribute()) {
+ // Don't use the message with placement with _Alignas.
+ // This is because C doesnt let you use _Alignas on type declarations.
+ Diag(AL.getLoc(), diag::warn_attribute_ignored)
+ << GetDiagnosticTypeSpecifierID(TypeSpecType);
+ } else {
+ Diag(AL.getLoc(), diag::warn_declspec_attribute_ignored)
+ << AL << GetDiagnosticTypeSpecifierID(TypeSpecType);
+ }
+ };
+
if (TypeSpecType == DeclSpec::TST_class ||
TypeSpecType == DeclSpec::TST_struct ||
TypeSpecType == DeclSpec::TST_interface ||
TypeSpecType == DeclSpec::TST_union ||
TypeSpecType == DeclSpec::TST_enum) {
- for (const ParsedAttr &AL : DS.getAttributes())
- Diag(AL.getLoc(), diag::warn_declspec_attribute_ignored)
- << AL << GetDiagnosticTypeSpecifierID(TypeSpecType);
- for (const ParsedAttr &AL : DeclAttrs)
- Diag(AL.getLoc(), diag::warn_declspec_attribute_ignored)
- << AL << GetDiagnosticTypeSpecifierID(TypeSpecType);
+ for (const ParsedAttr &AL : DS.getAttributes()) {
+ WarnAttributeIgnored(AL);
+ }
+ for (const ParsedAttr &AL : DeclAttrs) {
+ WarnAttributeIgnored(AL);
+ }
}
}
Index: clang/test/Parser/c1x-alignas.c
===================================================================
--- clang/test/Parser/c1x-alignas.c
+++ clang/test/Parser/c1x-alignas.c
@@ -9,5 +9,7 @@
char _Alignas(_Alignof(int)) c5;
+_Alignas(int) struct c6; // expected-warning {{1 attribute ignored}}
+
// CHECK-EXT: '_Alignas' is a C11 extension
// CHECK-EXT: '_Alignof' is a C11 extension
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -5291,18 +5291,29 @@
// __attribute__((aligned)) struct A;
// Attributes should be placed after tag to apply to type declaration.
if (!DS.getAttributes().empty() || !DeclAttrs.empty()) {
- DeclSpec::TST TypeSpecType = DS.getTypeSpecType();
+ auto WarnAttributeIgnored = [this, TypeSpecType](const ParsedAttr &AL) {
+ if (AL.isAlignasAttribute()) {
+ // Don't use the message with placement with _Alignas.
+ // This is because C doesnt let you use _Alignas on type declarations.
+ Diag(AL.getLoc(), diag::warn_attribute_ignored)
+ << GetDiagnosticTypeSpecifierID(TypeSpecType);
+ } else {
+ Diag(AL.getLoc(), diag::warn_declspec_attribute_ignored)
+ << AL << GetDiagnosticTypeSpecifierID(TypeSpecType);
+ }
+ };
+
if (TypeSpecType == DeclSpec::TST_class ||
TypeSpecType == DeclSpec::TST_struct ||
TypeSpecType == DeclSpec::TST_interface ||
TypeSpecType == DeclSpec::TST_union ||
TypeSpecType == DeclSpec::TST_enum) {
- for (const ParsedAttr &AL : DS.getAttributes())
- Diag(AL.getLoc(), diag::warn_declspec_attribute_ignored)
- << AL << GetDiagnosticTypeSpecifierID(TypeSpecType);
- for (const ParsedAttr &AL : DeclAttrs)
- Diag(AL.getLoc(), diag::warn_declspec_attribute_ignored)
- << AL << GetDiagnosticTypeSpecifierID(TypeSpecType);
+ for (const ParsedAttr &AL : DS.getAttributes()) {
+ WarnAttributeIgnored(AL);
+ }
+ for (const ParsedAttr &AL : DeclAttrs) {
+ WarnAttributeIgnored(AL);
+ }
}
}
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits