[clang] 9b10e2b - [PowerPC][AIX] Warn when using pragma align(packed) on AIX.
Sean Fertile via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 29 08:55:10 PDT 2021
Author: Sean Fertile
Date: 2021-09-29T11:53:46-04:00
New Revision: 9b10e2b1cf01b37f441d83ebc41d2c2f9f81831e
URL: https://github.com/llvm/llvm-project/commit/9b10e2b1cf01b37f441d83ebc41d2c2f9f81831e
DIFF: https://github.com/llvm/llvm-project/commit/9b10e2b1cf01b37f441d83ebc41d2c2f9f81831e.diff
LOG: [PowerPC][AIX] Warn when using pragma align(packed) on AIX.
With xlc and xlC pragma align(packed) will pack bitfields the same way
as pragma align(bit_packed). xlclang, xlclang++ and clang will
pack bitfields the same way as pragma pack(1). Issue a warning when
source code using pragma align(packed) is used to alert the user it
may not be compatable with xlc/xlC.
Differential Revision: https://reviews.llvm.org/D107506
Added:
clang/test/Sema/aix-pragma-align-packed-warn.c
Modified:
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaDecl.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 00b6acf8bbe68..a3944f7a6054b 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -916,6 +916,9 @@ def warn_pragma_options_align_reset_failed : Warning<
InGroup<IgnoredPragmas>;
def err_pragma_options_align_mac68k_target_unsupported : Error<
"mac68k alignment pragma is not supported on this target">;
+def warn_pragma_align_not_xl_compatible : Warning<
+ "#pragma align(packed) may not be compatible with objects generated with AIX XL C/C++">,
+ InGroup<AIXCompat>;
def warn_pragma_pack_invalid_alignment : Warning<
"expected #pragma pack parameter to be '1', '2', '4', '8', or '16'">,
InGroup<IgnoredPragmas>;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index db7c9f9418042..48f298f2ce823 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -16613,6 +16613,23 @@ void Sema::ActOnTagFinishDefinition(Scope *S, Decl *TagD,
// Notify the consumer that we've defined a tag.
if (!Tag->isInvalidDecl())
Consumer.HandleTagDeclDefinition(Tag);
+
+ // Clangs implementation of #pragma align(packed)
diff ers in bitfield layout
+ // from XLs and instead matches the XL #pragma pack(1) behavior.
+ if (Context.getTargetInfo().getTriple().isOSAIX() &&
+ AlignPackStack.hasValue()) {
+ AlignPackInfo APInfo = AlignPackStack.CurrentValue;
+ // Only diagnose #pragma align(packed).
+ if (!APInfo.IsAlignAttr() || APInfo.getAlignMode() != AlignPackInfo::Packed)
+ return;
+ const RecordDecl *RD = dyn_cast<RecordDecl>(Tag);
+ if (!RD)
+ return;
+ // Only warn if there is at least 1 bitfield member.
+ if (llvm::any_of(RD->fields(),
+ [](const FieldDecl *FD) { return FD->isBitField(); }))
+ Diag(BraceRange.getBegin(), diag::warn_pragma_align_not_xl_compatible);
+ }
}
void Sema::ActOnObjCContainerFinishDefinition() {
diff --git a/clang/test/Sema/aix-pragma-align-packed-warn.c b/clang/test/Sema/aix-pragma-align-packed-warn.c
new file mode 100644
index 0000000000000..acbca792e37a3
--- /dev/null
+++ b/clang/test/Sema/aix-pragma-align-packed-warn.c
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 -triple powerpc64-ibm-aix-xcoff -fxl-pragma-pack -verify -fsyntax-only %s
+// RUN: %clang_cc1 -triple powerpc-ibm-aix-xcoff -fxl-pragma-pack -verify -fsyntax-only %s
+
+#pragma align(packed)
+struct A { // expected-warning {{#pragma align(packed) may not be compatible with objects generated with AIX XL C/C++}}
+ short s1;
+ int : 0;
+ short s2;
+};
+
+struct B { // expected-warning {{#pragma align(packed) may not be compatible with objects generated with AIX XL C/C++}}
+ short a : 8;
+ short b : 8;
+ int c;
+};
+
+struct C {
+ int x, y, z;
+};
+
+struct D {
+ double d;
+ struct A a;
+};
+#pragma align(reset)
+
+struct E {
+ int a : 28;
+ int : 0;
+ int b : 16;
+};
More information about the cfe-commits
mailing list