[clang] Use the XL pragma pack semantics on z/OS (PR #111053)

Sean Perry via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 3 12:52:15 PDT 2024


https://github.com/perry-ca created https://github.com/llvm/llvm-project/pull/111053

- set the default on z/OS to use the XL pragma semantics
- add in additional pragma pack values such as twobyte & reset supported by XL on z/OS

>From 8f5b11f92dec5072d50a7930fa501c9039b6af26 Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Thu, 3 Oct 2024 13:46:37 -0500
Subject: [PATCH] initial upstream

---
 clang/lib/Driver/ToolChains/ZOS.cpp         |  4 ++++
 clang/lib/Parse/ParsePragma.cpp             | 26 +++++++++++++++++++++
 clang/test/Driver/zos-pragma-pack.c         |  8 +++++++
 clang/test/SemaCXX/pragma-pack-packed-2.cpp | 12 ++++++++++
 4 files changed, 50 insertions(+)
 create mode 100644 clang/test/Driver/zos-pragma-pack.c
 create mode 100644 clang/test/SemaCXX/pragma-pack-packed-2.cpp

diff --git a/clang/lib/Driver/ToolChains/ZOS.cpp b/clang/lib/Driver/ToolChains/ZOS.cpp
index 074e0556ecd2ad..c5ad3ef1b00f1d 100644
--- a/clang/lib/Driver/ToolChains/ZOS.cpp
+++ b/clang/lib/Driver/ToolChains/ZOS.cpp
@@ -37,6 +37,10 @@ void ZOS::addClangTargetOptions(const ArgList &DriverArgs,
                                 options::OPT_fno_aligned_allocation))
     CC1Args.push_back("-faligned-alloc-unavailable");
 
+  if (DriverArgs.hasFlag(options::OPT_fxl_pragma_pack,
+                         options::OPT_fno_xl_pragma_pack, true))
+    CC1Args.push_back("-fxl-pragma-pack");
+
   // Pass "-fno-sized-deallocation" only when the user hasn't manually enabled
   // or disabled sized deallocations.
   if (!DriverArgs.hasArgNoClaim(options::OPT_fsized_deallocation,
diff --git a/clang/lib/Parse/ParsePragma.cpp b/clang/lib/Parse/ParsePragma.cpp
index cc6f18b5b319f9..12fed448d477c0 100644
--- a/clang/lib/Parse/ParsePragma.cpp
+++ b/clang/lib/Parse/ParsePragma.cpp
@@ -2126,6 +2126,7 @@ void PragmaGCCVisibilityHandler::HandlePragma(Preprocessor &PP,
 //   pack '(' [integer] ')'
 //   pack '(' 'show' ')'
 //   pack '(' ('push' | 'pop') [',' identifier] [, integer] ')'
+//   pack '(' 'packed' | 'full' | 'twobyte' | 'reset' ')' with -fzos-extensions
 void PragmaPackHandler::HandlePragma(Preprocessor &PP,
                                      PragmaIntroducer Introducer,
                                      Token &PackTok) {
@@ -2155,10 +2156,35 @@ void PragmaPackHandler::HandlePragma(Preprocessor &PP,
                  ? Sema::PSK_Push_Set
                  : Sema::PSK_Set;
   } else if (Tok.is(tok::identifier)) {
+    // Map pragma pack options to pack (integer).
+    auto MapPack = [&](const char *Literal) {
+      Action = Sema::PSK_Push_Set;
+      Alignment = Tok;
+      Alignment.setKind(tok::numeric_constant);
+      Alignment.setLiteralData(Literal);
+      Alignment.setLength(1);
+    };
+
     const IdentifierInfo *II = Tok.getIdentifierInfo();
     if (II->isStr("show")) {
       Action = Sema::PSK_Show;
       PP.Lex(Tok);
+    } else if (II->isStr("packed") && PP.getLangOpts().ZOSExt) {
+      // #pragma pack(packed) is the same as #pragma pack(1)
+      MapPack("1");
+      PP.Lex(Tok);
+    } else if (II->isStr("full") && PP.getLangOpts().ZOSExt) {
+      // #pragma pack(full) is the same as #pragma pack(4)
+      MapPack("4");
+      PP.Lex(Tok);
+    } else if (II->isStr("twobyte") && PP.getLangOpts().ZOSExt) {
+      // #pragma pack(twobyte) is the same as #pragma pack(2)
+      MapPack("2");
+      PP.Lex(Tok);
+    } else if (II->isStr("reset") && PP.getLangOpts().ZOSExt) {
+      // #pragma pack(reset) is the same as #pragma pack(pop) on XL
+      Action = Sema::PSK_Pop;
+      PP.Lex(Tok);
     } else {
       if (II->isStr("push")) {
         Action = Sema::PSK_Push;
diff --git a/clang/test/Driver/zos-pragma-pack.c b/clang/test/Driver/zos-pragma-pack.c
new file mode 100644
index 00000000000000..0e04878daba4c5
--- /dev/null
+++ b/clang/test/Driver/zos-pragma-pack.c
@@ -0,0 +1,8 @@
+// REQUIRES: systemz-registered-target
+
+// RUN: %clang -### -target s390x-ibm-zos -c %s -o /dev/null 2>&1 | FileCheck %s
+// CHECK: "-fxl-pragma-pack"
+
+// RUN: %clang -### -fno-xl-pragma-pack -target s390x-ibm-zos -c %s -o /dev/null 2>&1 | FileCheck %s -check-prefix=NOOPT
+// NOOPT-NOT: "-fxl-pragma-pack"
+
diff --git a/clang/test/SemaCXX/pragma-pack-packed-2.cpp b/clang/test/SemaCXX/pragma-pack-packed-2.cpp
new file mode 100644
index 00000000000000..3639addd6fe5fc
--- /dev/null
+++ b/clang/test/SemaCXX/pragma-pack-packed-2.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -triple s390x-ibm-zos -fzos-extensions -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple s390x-ibm-zos -fzos-extensions -fxl-pragma-pack -fsyntax-only -verify %s
+// RUN: %clang -target s390x-ibm-zos -S -emit-llvm -Xclang -verify -fno-xl-pragma-pack %s
+
+#pragma pack(show) // expected-warning {{value of #pragma pack(show) == 8}}
+#pragma pack(twobyte)
+#pragma pack(packed)
+#pragma pack(show) // expected-warning {{value of #pragma pack(show) == 1}}
+#pragma pack(reset)
+#pragma pack(show) // expected-warning {{value of #pragma pack(show) == 2}}
+#pragma pack(pop)
+#pragma pack(show) // expected-warning {{value of #pragma pack(show) == 8}}



More information about the cfe-commits mailing list