[clang] [lldb] [SystemZ][clang] Add pragma map for z/OS system headers (PR #243723)

Sean Perry via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 25 22:18:05 PST 2026


https://github.com/perry-ca updated https://github.com/llvm/llvm-project/pull/243723

>From beabc56acf39898c1ebf33577fa1947b3a3c8df2 Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Mon, 6 Jul 2026 14:30:34 -0400
Subject: [PATCH 01/13] add support for #pragma map on z/OS

---
 .../clang/Basic/DiagnosticParseKinds.td       |  2 +
 .../clang/Basic/DiagnosticSemaKinds.td        |  6 ++
 clang/include/clang/Basic/TokenKinds.def      |  3 +
 clang/include/clang/Parse/Parser.h            |  8 ++
 clang/include/clang/Sema/Sema.h               | 11 +++
 clang/lib/Parse/ParsePragma.cpp               | 86 +++++++++++++++++--
 clang/lib/Parse/ParseStmt.cpp                 |  8 ++
 clang/lib/Parse/Parser.cpp                    |  3 +
 clang/lib/Sema/Sema.cpp                       |  6 ++
 clang/lib/Sema/SemaAttr.cpp                   | 64 ++++++++++++++
 clang/lib/Sema/SemaDecl.cpp                   | 32 +++++++
 11 files changed, 224 insertions(+), 5 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 2be6ff8344cd6d..deaddc85aed48d 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -1095,6 +1095,8 @@ def warn_pragma_missing_argument : Warning<
   "missing argument to '#pragma %0'%select{|; expected %2}1">, InGroup<IgnoredPragmas>;
 def warn_pragma_invalid_argument : Warning<
   "unexpected argument '%0' to '#pragma %1'%select{|; expected %3}2">, InGroup<IgnoredPragmas>;
+def warn_pragma_ignore_args : Warning<
+  "argument list in '#pragma %0' - ignoring">, InGroup<IgnoredPragmas>;
 
 def err_pragma_misplaced_in_decl : Error<"this pragma cannot appear in %0 declaration">;
 
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index dff45a081f2e8c..98c5a09a7a08e6 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1300,6 +1300,9 @@ def note_surrounding_namespace_starts_here : Note<
 def warn_failed_to_resolve_pragma : Warning<
   "failed to resolve '#pragma %0' to a declaration">,
   InGroup<IgnoredPragmas>;
+def warn_pragma_different_asm_label : Warning<
+  "#pragma map has conflicting asm label; not applied to %0">,
+  InGroup<IgnoredPragmas>;
 def warn_pragma_not_applied : Warning<
   "#pragma %0 is applicable to symbols with external linkage only; "
   "not applied to %1">,
@@ -1307,6 +1310,9 @@ def warn_pragma_not_applied : Warning<
 def warn_pragma_not_applied_to_defined_symbol : Warning<
   "#pragma %0 can only applied before a symbol is defined">,
   InGroup<IgnoredPragmas>;
+def warn_pragma_not_applied_to_used_symbol : Warning<
+  "#pragma %0 can only applied to unused symbols">,
+  InGroup<IgnoredPragmas>;
 def err_pragma_loop_invalid_argument_type : Error<
   "invalid argument of type %0; expected an integer type">;
 def err_pragma_loop_compatibility : Error<
diff --git a/clang/include/clang/Basic/TokenKinds.def b/clang/include/clang/Basic/TokenKinds.def
index 802947e31cbc18..a667a71663ae9b 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -902,6 +902,9 @@ PRAGMA_ANNOTATION(pragma_attribute)
 // Annotation for C/C++ #pragma export(ident)
 PRAGMA_ANNOTATION(pragma_export)
 
+// Annotation for #pragma map(ident, str-lit)
+PRAGMA_ANNOTATION(pragma_map)
+
 // Annotation for the riscv pragma directives - #pragma clang riscv intrinsic ...
 PRAGMA_ANNOTATION(pragma_riscv)
 
diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h
index 5c1cf9b687291e..2dc8ea69feea63 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -7199,6 +7199,7 @@ class Parser : public CodeCompletionHandler {
   std::unique_ptr<PragmaHandler> MaxTokensHerePragmaHandler;
   std::unique_ptr<PragmaHandler> MaxTokensTotalPragmaHandler;
   std::unique_ptr<PragmaHandler> ExportHandler;
+  std::unique_ptr<PragmaHandler> MapHandler;
   std::unique_ptr<PragmaHandler> RISCVPragmaHandler;
 
   /// Initialize all pragma handlers.
@@ -7322,10 +7323,17 @@ class Parser : public CodeCompletionHandler {
 
   void zOSHandlePragmaHelper(tok::TokenKind);
 
+  bool zOSParseParameterList(StringRef PragmaName,
+                             std::optional<SmallVector<QualType, 4>> &TypeList);
+
   /// Handle the annotation token produced for
   /// #pragma export ...
   void HandlePragmaExport();
 
+  /// Handle the annotation token produced for
+  /// #pragma map ...
+  void HandlePragmaMap();
+
   ///@}
 
   //
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index bc8793c4b7a533..f93c24296092ab 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -2403,12 +2403,22 @@ class Sema final : public SemaBase {
     bool Used;
   };
 
+  struct PendingPragmaMapInfo : public PendingPragmaInfo {
+    AsmLabelAttr *Attr; // The attribute to attach.
+  };
+
   llvm::DenseMap<IdentifierInfo *, PendingPragmaInfo> PendingExportedNames;
 
+  llvm::DenseMap<IdentifierInfo *, PendingPragmaMapInfo> PendingMappedNames;
+
   /// ActonPragmaExport - called on well-formed '\#pragma export'.
   void ActOnPragmaExport(IdentifierInfo *IdentId, SourceLocation ExportNameLoc,
                          Scope *curScope);
 
+  /// ActOnPragmaMap - called on well-formed '\#pragma map'.
+  void ActOnPragmaMap(IdentifierInfo *IdentId, SourceLocation NameLoc,
+                      Scope *curScope, const StringRef MappedName);
+
   /// Only called on function definitions; if there is a pragma in scope
   /// with the effect of a range-based optnone, consider marking the function
   /// with attribute optnone.
@@ -4034,6 +4044,7 @@ class Sema final : public SemaBase {
   void warnOnCTypeHiddenInCPlusPlus(const NamedDecl *D);
 
   void ProcessPragmaExport(DeclaratorDecl *newDecl);
+  void ProcessPragmaMap(DeclaratorDecl *NewDecl);
 
   Decl *ActOnDeclarator(Scope *S, Declarator &D);
 
diff --git a/clang/lib/Parse/ParsePragma.cpp b/clang/lib/Parse/ParsePragma.cpp
index dd05bd3c21442a..e17064f910ed6c 100644
--- a/clang/lib/Parse/ParsePragma.cpp
+++ b/clang/lib/Parse/ParsePragma.cpp
@@ -395,6 +395,12 @@ struct PragmaExportHandler : public PragmaHandler {
                     Token &FirstToken) override;
 };
 
+struct PragmaMapHandler : public PragmaHandler {
+  PragmaMapHandler() : PragmaHandler("map") {}
+  void HandlePragma(Preprocessor &PP, PragmaIntroducer Introducer,
+                    Token &FirstToken) override;
+};
+
 struct PragmaRISCVHandler : public PragmaHandler {
   PragmaRISCVHandler(Sema &Actions)
       : PragmaHandler("riscv"), Actions(Actions) {}
@@ -562,6 +568,8 @@ void Parser::initializePragmaHandlers() {
   if (getLangOpts().ZOSExt) {
     ExportHandler = std::make_unique<PragmaExportHandler>();
     PP.AddPragmaHandler(ExportHandler.get());
+    MapHandler = std::make_unique<PragmaMapHandler>();
+    PP.AddPragmaHandler(MapHandler.get());
   }
 
   if (getTargetInfo().getTriple().isRISCV()) {
@@ -702,6 +710,8 @@ void Parser::resetPragmaHandlers() {
   if (getLangOpts().ZOSExt) {
     PP.RemovePragmaHandler(ExportHandler.get());
     ExportHandler.reset();
+    PP.RemovePragmaHandler(MapHandler.get());
+    MapHandler.reset();
   }
 
   if (getTargetInfo().getTriple().isRISCV()) {
@@ -1412,8 +1422,6 @@ bool Parser::HandlePragmaMSAllocText(StringRef PragmaName,
 void Parser::zOSHandlePragmaHelper(tok::TokenKind PragmaKind) {
   assert(Tok.is(PragmaKind));
 
-  StringRef PragmaName = "export";
-
   using namespace clang::charinfo;
   auto *TheTokens = static_cast<std::pair<std::unique_ptr<Token[]>, size_t> *>(
       Tok.getAnnotationValue());
@@ -1429,6 +1437,11 @@ void Parser::zOSHandlePragmaHelper(tok::TokenKind PragmaKind) {
   });
 
   do {
+    const bool IsPragmaMap = PragmaKind == tok::annot_pragma_map;
+    StringRef PragmaName = IsPragmaMap ? "map" : "export";
+    const bool AcceptCxxAndFunctionIdents =
+        IsPragmaMap && PP.getLangOpts().CPlusPlus;
+
     PP.Lex(Tok);
     if (Tok.isNot(tok::l_paren)) {
       PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_lparen)
@@ -1447,6 +1460,46 @@ void Parser::zOSHandlePragmaHelper(tok::TokenKind PragmaKind) {
     SourceLocation IdentNameLoc = Tok.getLocation();
     PP.Lex(Tok);
 
+    if (AcceptCxxAndFunctionIdents && Tok.is(tok::l_paren)) {
+      SourceLocation LparenLoc = Tok.getLocation();
+      PP.Lex(Tok);
+      if (!SkipUntil(tok::r_paren)) {
+        PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
+            << PragmaName;
+        return;
+      }
+      PP.Diag(LparenLoc, diag::warn_pragma_ignore_args) << PragmaName;
+    }
+
+    // If we are parsing string literal for this pragma.
+    StringLiteral *MappedName = nullptr;
+    if (IsPragmaMap) {
+      if (Tok.isNot(tok::comma)) {
+        PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_comma)
+            << PragmaName;
+        return;
+      }
+      PP.Lex(Tok);
+
+      if (Tok.isNot(tok::string_literal)) {
+        PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_string)
+            << PragmaName;
+        return;
+      }
+
+#if 0 // TODO:
+      ExprResult StringResult = (SaveAndRestore<ConversionAction>(
+                                     ParserConversionAction, ToSystemCharset),
+                                 ParseStringLiteralExpression());
+#else
+      // TODO: The string literal needs to be converted into ebcdic.
+      ExprResult StringResult = ParseUnevaluatedStringLiteralExpression();
+#endif
+      if (StringResult.isInvalid())
+        return; // Already diagnosed.
+      MappedName = cast<StringLiteral>(StringResult.get());
+    }
+
     if (Tok.isNot(tok::r_paren)) {
       PP.Diag(Tok.getLocation(), diag::warn_pragma_expected_rparen)
           << PragmaName;
@@ -1454,14 +1507,26 @@ void Parser::zOSHandlePragmaHelper(tok::TokenKind PragmaKind) {
     }
 
     PP.Lex(Tok);
-    Actions.ActOnPragmaExport(IdentName, IdentNameLoc, getCurScope());
+    if (IsPragmaMap) {
+      assert(MappedName && "MappedName was not set.");
+      if (MappedName->getLength() != 0)
+        Actions.ActOnPragmaMap(IdentName, IdentNameLoc, getCurScope(),
+                               MappedName->getString());
+    } else
+      Actions.ActOnPragmaExport(IdentName, IdentNameLoc, getCurScope());
 
     // Because export is also a C++ keyword, we also check for that.
     if (Tok.is(tok::identifier) || Tok.is(tok::kw_export)) {
-      PragmaName = Tok.getIdentifierInfo()->getName();
-      if (PragmaName != "export")
+      StringRef TokenName = Tok.getIdentifierInfo()->getName();
+      if (TokenName == "map")
+        PragmaKind = tok::annot_pragma_map;
+      else if (TokenName == "export")
+        PragmaKind = tok::annot_pragma_export;
+      else {
         PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
             << PragmaName;
+        return;
+      }
     } else if (Tok.isNot(tok::eof)) {
       PP.Diag(Tok.getLocation(), diag::warn_pragma_extra_tokens_at_eol)
           << PragmaName;
@@ -1471,12 +1536,16 @@ void Parser::zOSHandlePragmaHelper(tok::TokenKind PragmaKind) {
   return;
 }
 
+/// Handle #pragma export.
 void Parser::HandlePragmaExport() {
   assert(Tok.is(tok::annot_pragma_export));
 
   zOSHandlePragmaHelper(tok::annot_pragma_export);
 }
 
+/// Handle #pragma map.
+void Parser::HandlePragmaMap() { zOSHandlePragmaHelper(tok::annot_pragma_map); }
+
 static std::string PragmaLoopHintString(Token PragmaName, Token Option) {
   StringRef Str = PragmaName.getIdentifierInfo()->getName();
   std::string ClangLoopStr("clang loop ");
@@ -4231,6 +4300,13 @@ void PragmaExportHandler::HandlePragma(Preprocessor &PP,
                                        Token &FirstToken) {
   zOSPragmaHandlerHelper(PP, FirstToken, tok::annot_pragma_export);
 }
+//
+/// Handle #pragma map.
+void PragmaMapHandler::HandlePragma(Preprocessor &PP,
+                                    PragmaIntroducer Introducer,
+                                    Token &FirstToken) {
+  zOSPragmaHandlerHelper(PP, FirstToken, tok::annot_pragma_map);
+}
 
 // Handle '#pragma clang riscv intrinsic vector'.
 //        '#pragma clang riscv intrinsic sifive_vector'.
diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp
index 4c3c4e5117813d..260266baf8f35f 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -537,6 +537,11 @@ StmtResult Parser::ParseStatementOrDeclarationAfterAttributes(
     ProhibitAttributes(GNUAttrs);
     HandlePragmaExport();
     return StmtEmpty();
+  case tok::annot_pragma_map:
+    ProhibitAttributes(CXX11Attrs);
+    ProhibitAttributes(GNUAttrs);
+    HandlePragmaMap();
+    return StmtEmpty();
   }
 
   // If we reached this code, the statement must end in a semicolon.
@@ -1076,6 +1081,9 @@ void Parser::ParseCompoundStatementLeadingPragmas() {
     case tok::annot_pragma_export:
       HandlePragmaExport();
       break;
+    case tok::annot_pragma_map:
+      HandlePragmaMap();
+      break;
     default:
       checkForPragmas = false;
       break;
diff --git a/clang/lib/Parse/Parser.cpp b/clang/lib/Parse/Parser.cpp
index fbfc632c47e45b..30cdaef5a44a6a 100644
--- a/clang/lib/Parse/Parser.cpp
+++ b/clang/lib/Parse/Parser.cpp
@@ -821,6 +821,9 @@ Parser::ParseExternalDeclaration(ParsedAttributes &Attrs,
   case tok::annot_pragma_export:
     HandlePragmaExport();
     return nullptr;
+  case tok::annot_pragma_map:
+    HandlePragmaMap();
+    return nullptr;
   case tok::semi:
     // Either a C++11 empty-declaration or attribute-declaration.
     SingleDecl =
diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index ebc8b85cf8a628..b79e95824d1aa8 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -1586,6 +1586,12 @@ void Sema::ActOnEndOfTranslationUnit() {
       Diag(Exported.NameLoc, diag::warn_failed_to_resolve_pragma) << "export";
   }
 
+  // Visit all pending #pragma map.
+  for (const PendingPragmaMapInfo &Mapped : PendingMappedNames.values()) {
+    if (!Mapped.Used)
+      Diag(Mapped.NameLoc, diag::warn_failed_to_resolve_pragma) << "map";
+  }
+
   if (LangOpts.HLSL)
     HLSL().ActOnEndOfTranslationUnit(getASTContext().getTranslationUnitDecl());
   if (LangOpts.OpenACC)
diff --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp
index e5253e2a311747..28d0b940bfd9dd 100644
--- a/clang/lib/Sema/SemaAttr.cpp
+++ b/clang/lib/Sema/SemaAttr.cpp
@@ -19,6 +19,8 @@
 #include "clang/Basic/TargetInfo.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Sema/Lookup.h"
+#include "llvm/Support/ConvertEBCDIC.h"
+
 #include <optional>
 using namespace clang;
 
@@ -1458,6 +1460,68 @@ void Sema::ActOnPragmaExport(IdentifierInfo *IdentId, SourceLocation NameLoc,
                       VisibilityAttr::Default);
 }
 
+void Sema::ActOnPragmaMap(IdentifierInfo *IdentId, SourceLocation NameLoc,
+                          Scope *curScope, const StringRef MappedName) {
+  assert(!MappedName.empty());
+
+  // All identifiers must be in UTF-8.
+  // TODO Is this the right place for the conversion?.
+  llvm::SmallString<16> MappedNameUTF;
+  llvm::ConverterEBCDIC::convertToUTF8(MappedName, MappedNameUTF);
+
+  AttributeCommonInfo AttrInfo(IdentId, NameLoc,
+                               AttributeCommonInfo::Form::Pragma());
+  AsmLabelAttr *Attr =
+      AsmLabelAttr::CreateImplicit(Context, MappedNameUTF.str(), AttrInfo);
+
+  PendingPragmaMapInfo Info;
+  Info.NameLoc = NameLoc;
+  Info.Attr = Attr;
+  Info.Used = false;
+
+  NamedDecl *PrevDecl =
+      lookupExternCFunctionOrVariable(IdentId, NameLoc, curScope);
+  if (!PrevDecl) {
+    auto PendingName = PendingMappedNames.find(IdentId);
+    if (PendingName != PendingMappedNames.end()) {
+      auto &Info = PendingName->second;
+      if (!Info.Attr->isEquivalent(Attr)) {
+        Diag(NameLoc, diag::warn_pragma_different_asm_label) << IdentId;
+        return;
+      }
+    }
+    PendingMappedNames[IdentId] = Info;
+    return;
+  }
+
+  if (auto *FD = dyn_cast<FunctionDecl>(PrevDecl->getCanonicalDecl())) {
+    if (FD->hasBody()) {
+      Diag(NameLoc, diag::warn_pragma_not_applied_to_used_symbol)
+          << "map" << PrevDecl;
+      return;
+    }
+  }
+
+  if (!PrevDecl->hasExternalFormalLinkage()) {
+    Diag(NameLoc, diag::warn_pragma_not_applied) << "map" << PrevDecl;
+    return;
+  }
+
+  if (AsmLabelAttr *PrevAsmLabel = PrevDecl->getAttr<AsmLabelAttr>()) {
+    if (!PrevAsmLabel->isEquivalent(Attr))
+      Diag(NameLoc, diag::warn_pragma_different_asm_label) << PrevDecl;
+    return;
+  }
+
+  if (PrevDecl->isUsed()) {
+    Diag(NameLoc, diag::warn_pragma_not_applied_to_used_symbol)
+        << "map" << PrevDecl;
+    return;
+  }
+
+  PrevDecl->addAttr(Attr);
+}
+
 typedef std::vector<std::pair<unsigned, SourceLocation> > VisStack;
 enum : unsigned { NoVisibility = ~0U };
 
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 5a70ab0162f14c..7c2f093cfd732f 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -7788,6 +7788,36 @@ void Sema::ProcessPragmaExport(DeclaratorDecl *NewD) {
   }
 }
 
+void Sema::ProcessPragmaMap(DeclaratorDecl *NewD) {
+  if (PendingMappedNames.empty())
+    return;
+  IdentifierInfo *IdentName = NewD->getIdentifier();
+  if (IdentName == nullptr)
+    return;
+  auto PendingName = PendingMappedNames.find(IdentName);
+  if (PendingName != PendingMappedNames.end()) {
+    auto &Info = PendingName->second;
+    if (!Info.Used) {
+      if (FunctionDecl *FD = dyn_cast<FunctionDecl>(NewD)) {
+        if (getLangOpts().CPlusPlus) {
+          if (!FD->isExternC())
+            return;
+        }
+      }
+      Info.Used = true;
+      if (NewD->hasExternalFormalLinkage()) {
+        if (AsmLabelAttr *AsmLabel = NewD->getAttr<AsmLabelAttr>()) {
+          if (!AsmLabel->isEquivalent(Info.Attr))
+            Diag(Info.NameLoc, diag::warn_pragma_different_asm_label) << NewD;
+          return;
+        }
+        NewD->addAttr(Info.Attr);
+      } else
+        Diag(Info.NameLoc, diag::warn_pragma_not_applied) << "map" << NewD;
+    }
+  }
+}
+
 // Checks if VD is declared at global scope or with C language linkage.
 static bool isMainVar(DeclarationName Name, VarDecl *VD) {
   return Name.getAsIdentifierInfo() &&
@@ -8534,6 +8564,7 @@ NamedDecl *Sema::ActOnVariableDeclarator(
 
   ProcessPragmaWeak(S, NewVD);
   ProcessPragmaExport(NewVD);
+  ProcessPragmaMap(NewVD);
 
   // If this is the first declaration of an extern C variable, update
   // the map of such variables.
@@ -11302,6 +11333,7 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
 
   ProcessPragmaWeak(S, NewFD);
   ProcessPragmaExport(NewFD);
+  ProcessPragmaMap(NewFD);
   checkAttributesAfterMerging(*this, *NewFD);
 
   AddKnownFunctionAttributes(NewFD);

>From c6c415a1c9695e8fcf2b6150492b5622e47a4356 Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Mon, 6 Jul 2026 23:01:12 -0400
Subject: [PATCH 02/13] Add #pragma map & the tests

---
 clang/lib/CodeGen/CodeGenModule.h             |   4 +
 clang/lib/Sema/SemaAttr.cpp                   |   3 +-
 .../CodeGen/SystemZ/pragma-map-conflict.c     |  19 ++
 .../test/CodeGen/SystemZ/pragma-map-noproto.c |  19 ++
 clang/test/CodeGen/SystemZ/pragma-map-proto.c |  18 ++
 clang/test/CodeGen/SystemZ/pragma-map.c       |  65 ++++++
 clang/test/CodeGen/SystemZ/pragma-map.cpp     | 195 ++++++++++++++++++
 clang/test/Parser/pragma-map.c                |  15 ++
 clang/test/Parser/pragma-map.cpp              |  10 +
 clang/test/Sema/pragma-map-conflict.c         |  16 ++
 clang/test/Sema/pragma-map-conflict.cpp       |  20 ++
 clang/test/Sema/pragma-map.c                  |  56 +++++
 clang/test/Sema/pragma-map.cpp                |  79 +++++++
 13 files changed, 518 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/CodeGen/SystemZ/pragma-map-conflict.c
 create mode 100644 clang/test/CodeGen/SystemZ/pragma-map-noproto.c
 create mode 100644 clang/test/CodeGen/SystemZ/pragma-map-proto.c
 create mode 100644 clang/test/CodeGen/SystemZ/pragma-map.c
 create mode 100644 clang/test/CodeGen/SystemZ/pragma-map.cpp
 create mode 100644 clang/test/Parser/pragma-map.c
 create mode 100644 clang/test/Parser/pragma-map.cpp
 create mode 100644 clang/test/Sema/pragma-map-conflict.c
 create mode 100644 clang/test/Sema/pragma-map-conflict.cpp
 create mode 100644 clang/test/Sema/pragma-map.c
 create mode 100644 clang/test/Sema/pragma-map.cpp

diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h
index c6b13f58817843..5debe4433fd3b9 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -868,6 +868,10 @@ class CodeGenModule : public CodeGenTypeCache {
   bool lookupRepresentativeDecl(StringRef MangledName,
                                 GlobalDecl &Result) const;
 
+  std::string getMangledNameImpl(GlobalDecl GD, const NamedDecl *ND,
+                                 bool OmitMultiVersionMangling = false,
+                                 bool WantAsmLabel = true);
+
   llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
     return AtomicSetterHelperFnMap[Ty];
   }
diff --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp
index 28d0b940bfd9dd..69a13d7fde8972 100644
--- a/clang/lib/Sema/SemaAttr.cpp
+++ b/clang/lib/Sema/SemaAttr.cpp
@@ -1467,7 +1467,8 @@ void Sema::ActOnPragmaMap(IdentifierInfo *IdentId, SourceLocation NameLoc,
   // All identifiers must be in UTF-8.
   // TODO Is this the right place for the conversion?.
   llvm::SmallString<16> MappedNameUTF;
-  llvm::ConverterEBCDIC::convertToUTF8(MappedName, MappedNameUTF);
+  // SDP llvm::ConverterEBCDIC::convertToUTF8(MappedName, MappedNameUTF);
+  MappedNameUTF = MappedName;
 
   AttributeCommonInfo AttrInfo(IdentId, NameLoc,
                                AttributeCommonInfo::Form::Pragma());
diff --git a/clang/test/CodeGen/SystemZ/pragma-map-conflict.c b/clang/test/CodeGen/SystemZ/pragma-map-conflict.c
new file mode 100644
index 00000000000000..62adfbd04ea1b2
--- /dev/null
+++ b/clang/test/CodeGen/SystemZ/pragma-map-conflict.c
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 %s -triple s390x-ibm-zos -emit-llvm -O1 -o - | FileCheck %s
+// Give a warning and ignore the pragma map.
+void rp(void) asm("rp_ext");
+void rp(void);
+#pragma map(rp, "a187")
+
+void rp_ext(void);
+
+
+void rp_orig(void) asm("a187");
+
+void rp_ext(void) {
+  rp_orig();
+}
+//CHECK: define void @rp_ext() local_unnamed_addr #0 {
+//CHECK:     call void @a187()
+//CHECK: }
+
+//CHECK: declare void @a187()
diff --git a/clang/test/CodeGen/SystemZ/pragma-map-noproto.c b/clang/test/CodeGen/SystemZ/pragma-map-noproto.c
new file mode 100644
index 00000000000000..1b11543bc375be
--- /dev/null
+++ b/clang/test/CodeGen/SystemZ/pragma-map-noproto.c
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 %s -triple s390x-ibm-zos -emit-llvm -o - | FileCheck %s
+// REQUIRES: systemz-registered-target
+
+// No arguments on the declaration.  Expect warnings.
+int printf();
+void perror();
+#pragma map(printf, "PRINTF")
+#pragma map(perror, "PERROR")
+
+int main() {
+  printf("hello");
+  perror("hello");
+}
+
+// CHECK: call {{.*}} @PRINTF({{.*}} @.str)
+// CHECK: call {{.*}} @PERROR({{.*}} @.str)
+
+// CHECK: declare {{.*}} @PRINTF(...)
+// CHECK: declare {{.*}} @PERROR(...)
diff --git a/clang/test/CodeGen/SystemZ/pragma-map-proto.c b/clang/test/CodeGen/SystemZ/pragma-map-proto.c
new file mode 100644
index 00000000000000..e8025f4bab667a
--- /dev/null
+++ b/clang/test/CodeGen/SystemZ/pragma-map-proto.c
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 %s -triple s390x-ibm-zos -emit-llvm -o - | FileCheck %s
+// REQUIRES: systemz-registered-target
+
+int printf(const char *, ...);
+void perror(const char *);
+#pragma map(printf, "PRINTF")
+#pragma map(perror, "PERROR")
+
+int main() {
+  printf("hello");
+  perror("hello");
+}
+
+// CHECK: call {{.*}} @PRINTF({{.*}} @.str)
+// CHECK: call {{.*}} @PERROR({{.*}} @.str)
+
+// CHECK: declare {{.*}} @PRINTF(ptr noundef, ...)
+// CHECK: declare {{.*}} @PERROR(ptr noundef)
diff --git a/clang/test/CodeGen/SystemZ/pragma-map.c b/clang/test/CodeGen/SystemZ/pragma-map.c
new file mode 100644
index 00000000000000..1fb84460c59076
--- /dev/null
+++ b/clang/test/CodeGen/SystemZ/pragma-map.c
@@ -0,0 +1,65 @@
+// RUN: %clang_cc1 %s -triple s390x-ibm-zos -emit-llvm -O1 -o - | FileCheck %s
+// REQUIRES: systemz-registered-target
+
+// Testing pragma map after decl.
+void f0(void);
+int v0;
+extern int e0;
+#pragma map(f0, "F0")
+#pragma map(v0, "V0")
+#pragma map(e0, "E0")
+
+// Testing pragma map before decl.
+#pragma map(f1, "F1")
+#pragma map(v1, "V1")
+#pragma map(e1, "E1")
+void f1(void);
+int v1;
+extern int e1;
+
+void f2(void);
+
+// Test that mapping to empty string is ignored.
+void f3(void);
+#pragma map(f3, "")
+
+int t0() {
+  f0();
+  f1();
+  f2();
+  f3();
+  return e0 + e1;
+}
+
+// Testing pragma map after decl and usage.
+#pragma map(f2, "F2")
+
+// Testing builtin function.
+#pragma map(__builtin_sin, "SIN")
+double __builtin_sin(double);
+
+double t1(double var) {
+  return __builtin_sin(0.0) + __builtin_sin(var);
+}
+// CHECK:       @E0 = external local_unnamed_addr global i32, align 4
+// CHECK-NEXT:  @E1 = external local_unnamed_addr global i32, align 4
+// CHECK:       @V0 = local_unnamed_addr global i32 0, align 4
+// CHECK-NEXT:  @V1 = local_unnamed_addr global i32 0, align 4
+// CHECK:       call void @F0()
+// CHECK:       call void @F1()
+// CHECK:       call void @f2()
+// CHECK:       declare void @F0{{.*}}
+// CHECK:       declare void @F1{{.*}}
+// CHECK:       declare void @f2{{.*}}
+// This change turns off optimization of math functions, therefore the next 2 lines change.
+// CHECK:       %call1 = tail call double @SIN(double noundef %var)
+// CHECK-NEXT:  %add = fadd double %call, %call1
+// CHECK:       declare double @SIN(double noundef) {{.*}}
+// CHECK:       !zos_mapped_names = !{![[n1:[0-9]+]], ![[n2:[0-9]+]], ![[n3:[0-9]+]], ![[n4:[0-9]+]], ![[n5:[0-9]+]], ![[n6:[0-9]+]], ![[n7:[0-9]+]]}
+// CHECK:       ![[n1]] = !{!"F0", !"f0"}
+// CHECK-NEXT:  ![[n2]] = !{!"F1", !"f1"}
+// CHECK-NEXT:  ![[n3]] = !{!"E0", !"e0"}
+// CHECK-NEXT:  ![[n4]] = !{!"E1", !"e1"}
+// CHECK-NEXT:  ![[n5]] = !{!"SIN", !"__builtin_sin"}
+// CHECK-NEXT:  ![[n6]] = !{!"V0", !"v0"}
+// CHECK-NEXT:  ![[n7]] = !{!"V1", !"v1"}
diff --git a/clang/test/CodeGen/SystemZ/pragma-map.cpp b/clang/test/CodeGen/SystemZ/pragma-map.cpp
new file mode 100644
index 00000000000000..0d8adbac38ea9e
--- /dev/null
+++ b/clang/test/CodeGen/SystemZ/pragma-map.cpp
@@ -0,0 +1,195 @@
+// RUN: %clang_cc1 %s -triple s390x-ibm-zos -emit-llvm -o - | FileCheck %s
+// REQUIRES: systemz-registered-target
+
+// Testing pragma map after decl.
+extern "C" void gf0(void);
+int v0;
+extern int e0;
+#pragma map(gf0, "F0")
+#pragma map(v0, "V0")
+#pragma map(e0, "E0")
+
+// Testing pragma map before decl.
+#pragma map(gf1, "F1")
+#pragma map(v1, "V1")
+#pragma map(e1, "E1")
+extern "C" void gf1(void);
+int v1;
+extern int e1;
+
+#pragma map(gf2, "F2I")
+extern "C" void gf2(int);
+void gf2(void);
+
+void f3(double);
+extern "C" void f3(int, double);
+#pragma map(f3, "F3ID")
+
+extern "C" void f4(void);
+extern "C" double cos(double);
+double cos(double, double);
+
+int t0() {
+  gf0();
+  gf1();
+  gf2();
+  gf2(0);
+  f3(0.1);
+  f3(0, 0.1);
+  f4();
+  return e0 + e1;
+}
+
+// Testing builtin function.
+#pragma map(__builtin_sin, "SIN")
+double __builtin_sin(double);
+
+double t1(double var) {
+  return __builtin_sin(0.0) + __builtin_sin(var) + cos(var) + cos(var, var);
+}
+
+// Testing pragma map after decl and usage.
+#pragma map(f4, "F4")
+#pragma map(cos, "COS")
+
+// Testing pragma map with namespace.
+void f5(void);
+namespace N0 {
+extern "C" void f0(void);
+extern "C" void f1(void);
+extern "C" void f2(void);
+extern "C" void f5(void);
+extern "C" void f6(void);
+#pragma map(f0, "N0F0")
+#pragma map(f1, "N0F1")
+#pragma map(f5, "N0F5")
+#pragma map(f2, "N0F2")
+} // namespace N0
+
+namespace N0 {
+void f6(void) {}
+#pragma map(f6, "N0F6")
+}
+
+void t2(void) {
+  N0::f0();
+  N0::f1();
+  N0::f2();
+  N0::f5();
+  N0::f6();
+  f5();
+}
+
+extern "C" void func(int);
+#pragma map(func, "FUNC")
+
+
+void call_func() {
+  func(1);
+}
+
+void func(int) { }
+
+// Pragma before declarations
+
+void func1(int);
+#pragma map(func1, "FUNC1")
+extern "C" void func1(double);
+
+void call_func1() {
+  func1(1);
+}
+void func1(int) { }
+void func1(double) { }
+
+// Pragma between  declarations
+
+#pragma map(func2, "FUNC2")
+void func2(int);
+extern "C" void func2(double);
+
+void call_func2() {
+  func2(1);
+  func2(1.0);
+}
+void func2(int) { }
+void func2(double) { }
+
+// Pragma after  declarations
+
+void func3(int);
+extern "C" void func3(double);
+#pragma map(func3, "FUNC3")
+
+void call_func3() {
+  func3(1);
+}
+void func3(int) { }
+void func3(double) { }
+
+// CHECK:       @v0 = global i32 0, align 4
+// CHECK-NEXT:  @V1 = global i32 0, align 4
+// CHECK-NEXT:  @E0 = external global i32, align 4
+// CHECK-NEXT:  @E1 = external global i32, align 4
+
+// CHECK-LABEL: _Z2t0v
+// CHECK:       call void @F0{{.*}}
+// CHECK:       call void @F1{{.*}}
+// CHECK:       call void @_Z3gf2v{{.*}}
+// CHECK:       call void @F2I{{.*}}
+// CHECK:       call void @_Z2f3d{{.*}}
+// CHECK:       call void @F3ID{{.*}}
+// CHECK:       call void @f4{{.*}}
+
+
+// CHECK-LABEL: _Z2t1d
+// This change turns off optimization of math functions, therefore the next 2 lines change.
+// CHECK:       %call{{.*}} call double @SIN{{.*}}
+// CHECK:       %call1{{.*}} call double @SIN{{.*}}
+// CHECK:       %add = fadd double %call, %call1
+// CHECK:       call double @llvm.cos{{.*}}
+// CHECK:       call noundef double @_Z3cosdd{{.*}}
+
+// CHECK:      declare double @SIN(double noundef){{.*}}
+// CHECK:      declare double @llvm.cos.f64(double){{.*}}
+// CHECK:      declare noundef double @_Z3cosdd(double noundef, double noundef){{.*}}
+
+// CHECK-LABEL: _Z2t2v
+// CHECK:       call void @N0F0()
+// CHECK:       call void @N0F1()
+// CHECK:       call void @N0F2()
+// CHECK:       call void @N0F5()
+// CHECK:       call void @f6()
+// CHECK:       call void @_Z2f5v()
+
+// CHECK-LABEL: _Z9call_funcv
+// CHECK:       call void @FUNC
+
+// CHECK-LABEL: _Z10call_func1v
+// CHECK:       call void @_Z5func1i
+
+// CHECK-LABEL: _Z10call_func2v
+// CHECK:       call void @_Z5func2i
+// CHECK:       call void @FUNC2
+
+// CHECK-LABEL: _Z10call_func3v
+// CHECK:       call void @_Z5func3i
+
+// CHECK:       !zos_mapped_names = !{![[n1:[0-9]+]], ![[n2:[0-9]+]], ![[n3:[0-9]+]], ![[n4:[0-9]+]], ![[n5:[0-9]+]], ![[n6:[0-9]+]], ![[n7:[0-9]+]], ![[n8:[0-9]+]], ![[n9:[0-9]+]], ![[n10:[0-9]+]], ![[n11:[0-9]+]], ![[n12:[0-9]+]], ![[n13:[0-9]+]], ![[n14:[0-9]+]], ![[n15:[0-9]+]], ![[n16:[0-9]+]], ![[n17:[0-9]+]]}
+// CHECK: ![[n1]] = !{!"v0", !"v0"}
+// CHECK-NEXT: ![[n2]] = !{!"V1", !"v1"}
+// CHECK-NEXT: ![[n3]] = !{!"F0", !"gf0"}
+// CHECK-NEXT: ![[n4]] = !{!"F1", !"gf1"}
+// CHECK-NEXT: ![[n5]] = !{!"F2I", !"gf2"}
+// CHECK-NEXT: ![[n6]] = !{!"F3ID", !"f3"}
+// CHECK-NEXT: ![[n7]] = !{!"E0", !"e0"}
+// CHECK-NEXT: ![[n8]] = !{!"E1", !"e1"}
+// CHECK-NEXT: ![[n9]] = !{!"SIN", !"__builtin_sin"}
+// CHECK-NEXT: ![[n10]] = !{!"N0F0", !"f0"}
+// CHECK-NEXT: ![[n11]] = !{!"N0F1", !"f1"}
+// CHECK-NEXT: ![[n12]] = !{!"N0F2", !"f2"}
+// CHECK-NEXT: ![[n13]] = !{!"N0F5", !"f5"}
+// CHECK-NEXT: ![[n14]] = !{!"FUNC", !"func"}
+// CHECK-NEXT: ![[n15]] = !{!"FUNC1", !"func1"}
+// CHECK-NEXT: ![[n16]] = !{!"FUNC2", !"func2"}
+// CHECK-NEXT: ![[n17]] = !{!"FUNC3", !"func3"}
diff --git a/clang/test/Parser/pragma-map.c b/clang/test/Parser/pragma-map.c
new file mode 100644
index 00000000000000..5156d0165fbcff
--- /dev/null
+++ b/clang/test/Parser/pragma-map.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -triple s390x-ibm-zos -fsyntax-only -verify %s
+// REQUIRES: systemz-registered-target
+
+int x;
+int x1;
+
+#pragma map x  // expected-warning {{missing '(' after '#pragma map' - ignoring}}
+#pragma map  // expected-warning {{missing '(' after '#pragma map' - ignoring}}
+#pragma map(  // expected-warning {{expected identifier in '#pragma map' - ignored}}
+#pragma map(x  // expected-warning {{expected ',' in '#pragma map'}}
+#pragma map(x,  // expected-warning {{expected string literal in '#pragma map'}}
+#pragma map(x,"foo"  // expected-warning {{missing ')' after '#pragma map'}}
+#pragma map(::x) // expected-warning {{expected identifier in '#pragma map' - ignored}}
+#pragma map(x, "abc")
+#pragma map(x1, "abc") blah // expected-warning {{extra tokens at end of '#pragma map'}}
diff --git a/clang/test/Parser/pragma-map.cpp b/clang/test/Parser/pragma-map.cpp
new file mode 100644
index 00000000000000..286c0b485b92b2
--- /dev/null
+++ b/clang/test/Parser/pragma-map.cpp
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -x c++ -triple s390x-ibm-zos -fsyntax-only -verify %s
+// REQUIRES: systemz-registered-target
+
+#pragma map(f // expected-warning {{expected ',' in '#pragma map'}}
+#pragma map(f( // expected-warning {{missing ')' after '#pragma map' - ignoring}}
+#pragma map(f(T // expected-warning {{missing ')' after '#pragma map' - ignoring}}
+#pragma map(f(T, "def") // expected-warning {{argument list in '#pragma map' - ignoring}} 
+  // expected-warning at -1 {{expected ',' in '#pragma map'}}
+extern "C" void f(double, double) {
+}
diff --git a/clang/test/Sema/pragma-map-conflict.c b/clang/test/Sema/pragma-map-conflict.c
new file mode 100644
index 00000000000000..72a16cb1ea3650
--- /dev/null
+++ b/clang/test/Sema/pragma-map-conflict.c
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 %s -triple s390x-ibm-zos -fsyntax-only -verify
+// REQUIRES: systemz-registered-target
+// Generate an error on the asm label because pragma map was previously seen
+void rp(void);
+#pragma map(rp, "a187") // expected-note {{previous declaration is here}}
+
+void rp_ext(void);
+
+void rp(void) asm("rp_ext"); // expected-error {{conflicting asm label}}
+
+void rp_orig(void) asm("a187");
+
+void rp_ext(void) {
+  rp_orig();
+}
+
diff --git a/clang/test/Sema/pragma-map-conflict.cpp b/clang/test/Sema/pragma-map-conflict.cpp
new file mode 100644
index 00000000000000..fef6ae8ee8b04c
--- /dev/null
+++ b/clang/test/Sema/pragma-map-conflict.cpp
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -x c++ %s -triple s390x-ibm-zos -fsyntax-only -verify
+// REQUIRES: systemz-registered-target
+#pragma map(func2, "FUNC2A")
+
+extern "C" void func2(int);
+#pragma map(func2, "FUNC2") // expected-warning {{#pragma map has conflicting asm label; not applied to 'func2'}}
+
+void func2(double);
+
+void call_func2() {
+  func2(1);
+}
+
+
+extern "C" void f1() asm("FUNC1");
+#pragma map(f1, "F1") // expected-warning {{#pragma map has conflicting asm label; not applied to 'f1'}}
+
+void call_f1() {
+  f1();
+}
diff --git a/clang/test/Sema/pragma-map.c b/clang/test/Sema/pragma-map.c
new file mode 100644
index 00000000000000..166883663469d4
--- /dev/null
+++ b/clang/test/Sema/pragma-map.c
@@ -0,0 +1,56 @@
+// RUN: %clang_cc1 %s -triple s390x-ibm-zos -fsyntax-only -verify
+// REQUIRES: systemz-registered-target
+
+// Testing missing declarations.
+#pragma map(d0, "D0") // expected-warning{{failed to resolve '#pragma map' to a declaration}}
+#pragma map(f9, "F9") // expected-warning{{failed to resolve '#pragma map' to a declaration}}
+
+// Testing pragma map after decl.
+static void sf0(void);
+static int s0;
+#pragma map(sf0, "SF0") // expected-warning{{#pragma map is applicable to symbols with external linkage only; not applied to 'sf0'}}
+#pragma map(s0, "S0") // expected-warning{{#pragma map is applicable to symbols with external linkage only; not applied to 's0'}}
+
+// Testing pragma map before decl.
+#pragma map(sf1, "SF1") // expected-warning{{#pragma map is applicable to symbols with external linkage only; not applied to 'sf1'}}
+#pragma map(s1, "S1") // expected-warning{{#pragma map is applicable to symbols with external linkage only; not applied to 's1'}}
+static void sf1(void);
+static int s1;
+
+#pragma map(foo, "FOO1")
+#pragma map(foo, "FOO2") // expected-warning {{#pragma map has conflicting asm label; not applied to 'foo'}}
+int foo();
+
+int bar();
+#pragma map(bar, "BAR1")
+#pragma map(bar, "BAR2") // expected-warning {{#pragma map has conflicting asm label; not applied to 'bar'}}
+
+#pragma map(f, "F1") // expected-warning {{#pragma map has conflicting asm label; not applied to 'f'}}
+int f() asm("F2");
+
+int g() asm("G1");
+#pragma map(g, "G2") // expected-warning {{#pragma map has conflicting asm label; not applied to 'g'}}
+
+#pragma map(h, "H1") // expected-note {{previous declaration is here}}
+int h();
+int h() asm("H2"); // expected-error {{conflicting asm label}}
+
+void func_defined() {
+}
+#pragma map(func_defined, "FUNC") // expected-warning {{#pragma map can only applied to unused symbols}}
+
+void func_used() {
+}
+int var_used;
+
+int func() {
+  func_used();
+  return var_used;
+}
+#pragma map(func_used, "FUNC1") // expected-warning {{#pragma map can only applied to unused symbols}}
+#pragma map(var_used, "VAR1") // expected-warning {{#pragma map can only applied to unused symbols}}
+
+static int var_static;
+#pragma map(var_static,"VAR2") // expected-warning {{#pragma map is applicable to symbols with external linkage only; not applied to 'var_static'}}
+
+
diff --git a/clang/test/Sema/pragma-map.cpp b/clang/test/Sema/pragma-map.cpp
new file mode 100644
index 00000000000000..6b4336038da7b6
--- /dev/null
+++ b/clang/test/Sema/pragma-map.cpp
@@ -0,0 +1,79 @@
+// RUN: %clang_cc1 %s -triple s390x-ibm-zos -fsyntax-only -verify
+// REQUIRES: systemz-registered-target
+
+// Testing missing declarations.
+#pragma map(d0, "D0") // expected-warning{{failed to resolve '#pragma map' to a declaration}}
+#pragma map(f0, "F0I") // expected-warning{{failed to resolve '#pragma map' to a declaration}}
+#pragma map(f3, "F3DD") // expected-warning{{failed to resolve '#pragma map' to a declaration}}
+
+// Testing pragma map after decl.
+static void sf0(void);
+static int s0;
+#pragma map(sf0, "SF0") // expected-warning{{failed to resolve '#pragma map' to a declaration}}
+#pragma map(s0, "S0") // expected-warning{{#pragma map is applicable to symbols with external linkage only; not applied to 's0'}}
+
+#pragma map(sf1, "SF1") // expected-warning{{failed to resolve '#pragma map' to a declaration}}
+#pragma map(s1, "S1") // expected-warning{{#pragma map is applicable to symbols with external linkage only; not applied to 's1'}}
+static void sf1(void);
+static int s1;
+
+// Testing pragma map after decl and usage.
+extern "C" void f4(void);
+extern "C" double cos(double);
+double cos(double, double);
+
+void t0() {
+  f4();
+  double d = cos(3.14, 1.414);
+}
+#pragma map(f4, "F4") // expected-warning{{#pragma map can only applied to unused symbols}}
+#pragma map(cos, "COS")
+
+namespace N0 {
+extern "C" void f6(void) {}
+#pragma map(f6, "N0F6") // expected-warning{{#pragma map can only applied to unused symbols}}
+}
+
+
+void func1(int);
+#pragma map(func1, "FUNC1")
+extern "C" void func1(double);
+
+void call_func1() {
+  func1(1);
+}
+void func1(int) { }
+void func1(double) { }
+
+// Pragma between  declarations
+
+#pragma map(func2, "FUNC2")
+void func2(int);
+extern "C" void func2(double);
+
+void call_func2() {
+  func2(1);
+}
+void func2(int) { }
+void func2(double) { }
+
+// Pragma after  declarations
+
+void func3(int);
+extern "C" void func3(double);
+#pragma map(func3, "FUNC3")
+
+void call_func3() {
+  func3(1);
+}
+void func3(int) { }
+void func3(double) { }
+
+static void func4(int);
+#pragma map(func4, "FUNC4")
+extern "C" void func4(double);
+void func4(int) { }
+void call_func4() {
+  func4(1);
+  func4(1.0);
+}

>From 1b26f110a41d62994fb1690b13cf32f330ef5c9b Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Fri, 6 Nov 2026 17:52:33 -0500
Subject: [PATCH 03/13] fix build

---
 clang/lib/CodeGen/CodeGenModule.h | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h
index 5debe4433fd3b9..c6b13f58817843 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -868,10 +868,6 @@ class CodeGenModule : public CodeGenTypeCache {
   bool lookupRepresentativeDecl(StringRef MangledName,
                                 GlobalDecl &Result) const;
 
-  std::string getMangledNameImpl(GlobalDecl GD, const NamedDecl *ND,
-                                 bool OmitMultiVersionMangling = false,
-                                 bool WantAsmLabel = true);
-
   llvm::Constant *getAtomicSetterHelperFnMap(QualType Ty) {
     return AtomicSetterHelperFnMap[Ty];
   }

>From 163062be3a1149b53cde916ca7e54a4d1d075497 Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Mon, 16 Nov 2026 12:14:35 -0500
Subject: [PATCH 04/13] [SystemZ] Parse #pragma map name as an asm literal in
 IBM-1047

Parse the mapped name with CA_ToLiteralEncodingForAsm so that numeric escape
sequences are IBM-1047 code points, and convert it back to UTF-8 in
ActOnPragmaMap. Drop the obsolete zos_mapped_names checks from the tests, check
the zos-ppa1-name function attribute instead, and add a test for numeric
escapes.
---
 clang/lib/Parse/ParsePragma.cpp               | 11 ++-----
 clang/lib/Sema/SemaAttr.cpp                   | 16 +++++++---
 .../test/CodeGen/SystemZ/pragma-map-escape.c  | 31 +++++++++++++++++++
 clang/test/CodeGen/SystemZ/pragma-map.c       | 16 +++++-----
 clang/test/CodeGen/SystemZ/pragma-map.cpp     | 28 ++++++-----------
 5 files changed, 64 insertions(+), 38 deletions(-)
 create mode 100644 clang/test/CodeGen/SystemZ/pragma-map-escape.c

diff --git a/clang/lib/Parse/ParsePragma.cpp b/clang/lib/Parse/ParsePragma.cpp
index e17064f910ed6c..166e0a8432b923 100644
--- a/clang/lib/Parse/ParsePragma.cpp
+++ b/clang/lib/Parse/ParsePragma.cpp
@@ -1487,14 +1487,9 @@ void Parser::zOSHandlePragmaHelper(tok::TokenKind PragmaKind) {
         return;
       }
 
-#if 0 // TODO:
-      ExprResult StringResult = (SaveAndRestore<ConversionAction>(
-                                     ParserConversionAction, ToSystemCharset),
-                                 ParseStringLiteralExpression());
-#else
-      // TODO: The string literal needs to be converted into ebcdic.
-      ExprResult StringResult = ParseUnevaluatedStringLiteralExpression();
-#endif
+      ExprResult StringResult = ParseStringLiteralExpression(
+          /*AllowUserDefinedLiteral=*/false,
+          /*Unevaluated=*/false, CA_ToLiteralEncodingForAsm);
       if (StringResult.isInvalid())
         return; // Already diagnosed.
       MappedName = cast<StringLiteral>(StringResult.get());
diff --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp
index 69a13d7fde8972..18d2b50a5afc86 100644
--- a/clang/lib/Sema/SemaAttr.cpp
+++ b/clang/lib/Sema/SemaAttr.cpp
@@ -18,8 +18,10 @@
 #include "clang/Analysis/Analyses/LifetimeSafety/LifetimeAnnotations.h"
 #include "clang/Basic/TargetInfo.h"
 #include "clang/Lex/Preprocessor.h"
+#include "clang/Lex/TextEncoding.h"
 #include "clang/Sema/Lookup.h"
 #include "llvm/Support/ConvertEBCDIC.h"
+#include "llvm/Support/TextEncoding.h"
 
 #include <optional>
 using namespace clang;
@@ -1464,11 +1466,17 @@ void Sema::ActOnPragmaMap(IdentifierInfo *IdentId, SourceLocation NameLoc,
                           Scope *curScope, const StringRef MappedName) {
   assert(!MappedName.empty());
 
-  // All identifiers must be in UTF-8.
-  // TODO Is this the right place for the conversion?.
+  // All identifiers must be in UTF-8. On z/OS the mapped name was parsed with
+  // CA_ToLiteralEncodingForAsm, so it is in IBM-1047 and must be converted back.
   llvm::SmallString<16> MappedNameUTF;
-  // SDP llvm::ConverterEBCDIC::convertToUTF8(MappedName, MappedNameUTF);
-  MappedNameUTF = MappedName;
+  if (llvm::TextEncodingConverter *Conv =
+          PP.getTextEncoding().getzOSFromAsmLiteralEncodingConverter()) {
+    [[maybe_unused]] std::error_code EC =
+        Conv->convert(MappedName, MappedNameUTF);
+    assert(!EC && "This conversion should always succeed");
+  } else {
+    MappedNameUTF = MappedName;
+  }
 
   AttributeCommonInfo AttrInfo(IdentId, NameLoc,
                                AttributeCommonInfo::Form::Pragma());
diff --git a/clang/test/CodeGen/SystemZ/pragma-map-escape.c b/clang/test/CodeGen/SystemZ/pragma-map-escape.c
new file mode 100644
index 00000000000000..9c2ba4e94920d5
--- /dev/null
+++ b/clang/test/CodeGen/SystemZ/pragma-map-escape.c
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 %s -triple s390x-ibm-zos -fexec-charset IBM-1047 -emit-llvm -o - | FileCheck %s
+// RUN: %clang_cc1 %s -triple s390x-ibm-zos -fexec-charset UTF-8 -emit-llvm -o - | FileCheck %s
+// REQUIRES: systemz-registered-target
+
+// The mapped name of '#pragma map' is parsed like an asm label: numeric
+// escape sequences are IBM-1047 code points, regardless of -fexec-charset.
+// '@' is 0x7C (octal 174) in IBM-1047.
+
+void f0(void);
+void f1(void);
+void f2(void);
+
+// Plain '@' character.
+#pragma map(f0, "@F0")
+// Octal escape sequence for '@' in IBM-1047.
+#pragma map(f1, "\174F1")
+// Hex escape sequence for '@' in IBM-1047 ('G' is not a hex digit).
+#pragma map(f2, "\x7cG2")
+
+void f0(void) {}
+void f1(void) {}
+void f2(void) {}
+
+// The mapped name is the symbol name, and the source name is kept in the
+// "zos-ppa1-name" function attribute.
+// CHECK:      define void @"@F0"() #[[A0:[0-9]+]]
+// CHECK:      define void @"@F1"() #[[A1:[0-9]+]]
+// CHECK:      define void @"@G2"() #[[A2:[0-9]+]]
+// CHECK-DAG:  attributes #[[A0]] = {{{.*}}"zos-ppa1-name"="f0"{{.*}}}
+// CHECK-DAG:  attributes #[[A1]] = {{{.*}}"zos-ppa1-name"="f1"{{.*}}}
+// CHECK-DAG:  attributes #[[A2]] = {{{.*}}"zos-ppa1-name"="f2"{{.*}}}
diff --git a/clang/test/CodeGen/SystemZ/pragma-map.c b/clang/test/CodeGen/SystemZ/pragma-map.c
index 1fb84460c59076..f25d07758ec26e 100644
--- a/clang/test/CodeGen/SystemZ/pragma-map.c
+++ b/clang/test/CodeGen/SystemZ/pragma-map.c
@@ -41,6 +41,11 @@ double __builtin_sin(double);
 double t1(double var) {
   return __builtin_sin(0.0) + __builtin_sin(var);
 }
+
+// Testing that the source name is kept for a mapped function definition.
+#pragma map(f4, "F4")
+void f4(void) {}
+
 // CHECK:       @E0 = external local_unnamed_addr global i32, align 4
 // CHECK-NEXT:  @E1 = external local_unnamed_addr global i32, align 4
 // CHECK:       @V0 = local_unnamed_addr global i32 0, align 4
@@ -55,11 +60,6 @@ double t1(double var) {
 // CHECK:       %call1 = tail call double @SIN(double noundef %var)
 // CHECK-NEXT:  %add = fadd double %call, %call1
 // CHECK:       declare double @SIN(double noundef) {{.*}}
-// CHECK:       !zos_mapped_names = !{![[n1:[0-9]+]], ![[n2:[0-9]+]], ![[n3:[0-9]+]], ![[n4:[0-9]+]], ![[n5:[0-9]+]], ![[n6:[0-9]+]], ![[n7:[0-9]+]]}
-// CHECK:       ![[n1]] = !{!"F0", !"f0"}
-// CHECK-NEXT:  ![[n2]] = !{!"F1", !"f1"}
-// CHECK-NEXT:  ![[n3]] = !{!"E0", !"e0"}
-// CHECK-NEXT:  ![[n4]] = !{!"E1", !"e1"}
-// CHECK-NEXT:  ![[n5]] = !{!"SIN", !"__builtin_sin"}
-// CHECK-NEXT:  ![[n6]] = !{!"V0", !"v0"}
-// CHECK-NEXT:  ![[n7]] = !{!"V1", !"v1"}
+// The mapped name is the symbol name; the source name is in "zos-ppa1-name".
+// CHECK:       define{{.*}} void @F4(){{.*}} #[[F4:[0-9]+]]
+// CHECK:       attributes #[[F4]] = {{{.*}}"zos-ppa1-name"="f4"{{.*}}}
diff --git a/clang/test/CodeGen/SystemZ/pragma-map.cpp b/clang/test/CodeGen/SystemZ/pragma-map.cpp
index 0d8adbac38ea9e..a56de3e57e0ab3 100644
--- a/clang/test/CodeGen/SystemZ/pragma-map.cpp
+++ b/clang/test/CodeGen/SystemZ/pragma-map.cpp
@@ -162,34 +162,26 @@ void func3(double) { }
 // CHECK:       call void @f6()
 // CHECK:       call void @_Z2f5v()
 
+// The mapped name is the symbol name; the source name is in "zos-ppa1-name".
 // CHECK-LABEL: _Z9call_funcv
 // CHECK:       call void @FUNC
+// CHECK:       define{{.*}} void @FUNC({{.*}}) #[[FUNC:[0-9]+]]
 
 // CHECK-LABEL: _Z10call_func1v
 // CHECK:       call void @_Z5func1i
+// CHECK:       define{{.*}} void @FUNC1({{.*}}) #[[FUNC1:[0-9]+]]
 
 // CHECK-LABEL: _Z10call_func2v
 // CHECK:       call void @_Z5func2i
 // CHECK:       call void @FUNC2
+// CHECK:       define{{.*}} void @FUNC2({{.*}}) #[[FUNC2:[0-9]+]]
 
 // CHECK-LABEL: _Z10call_func3v
 // CHECK:       call void @_Z5func3i
+// CHECK:       define{{.*}} void @FUNC3({{.*}}) #[[FUNC3:[0-9]+]]
 
-// CHECK:       !zos_mapped_names = !{![[n1:[0-9]+]], ![[n2:[0-9]+]], ![[n3:[0-9]+]], ![[n4:[0-9]+]], ![[n5:[0-9]+]], ![[n6:[0-9]+]], ![[n7:[0-9]+]], ![[n8:[0-9]+]], ![[n9:[0-9]+]], ![[n10:[0-9]+]], ![[n11:[0-9]+]], ![[n12:[0-9]+]], ![[n13:[0-9]+]], ![[n14:[0-9]+]], ![[n15:[0-9]+]], ![[n16:[0-9]+]], ![[n17:[0-9]+]]}
-// CHECK: ![[n1]] = !{!"v0", !"v0"}
-// CHECK-NEXT: ![[n2]] = !{!"V1", !"v1"}
-// CHECK-NEXT: ![[n3]] = !{!"F0", !"gf0"}
-// CHECK-NEXT: ![[n4]] = !{!"F1", !"gf1"}
-// CHECK-NEXT: ![[n5]] = !{!"F2I", !"gf2"}
-// CHECK-NEXT: ![[n6]] = !{!"F3ID", !"f3"}
-// CHECK-NEXT: ![[n7]] = !{!"E0", !"e0"}
-// CHECK-NEXT: ![[n8]] = !{!"E1", !"e1"}
-// CHECK-NEXT: ![[n9]] = !{!"SIN", !"__builtin_sin"}
-// CHECK-NEXT: ![[n10]] = !{!"N0F0", !"f0"}
-// CHECK-NEXT: ![[n11]] = !{!"N0F1", !"f1"}
-// CHECK-NEXT: ![[n12]] = !{!"N0F2", !"f2"}
-// CHECK-NEXT: ![[n13]] = !{!"N0F5", !"f5"}
-// CHECK-NEXT: ![[n14]] = !{!"FUNC", !"func"}
-// CHECK-NEXT: ![[n15]] = !{!"FUNC1", !"func1"}
-// CHECK-NEXT: ![[n16]] = !{!"FUNC2", !"func2"}
-// CHECK-NEXT: ![[n17]] = !{!"FUNC3", !"func3"}
+// The attribute groups are printed after all the functions.
+// CHECK-DAG:   attributes #[[FUNC]] = {{{.*}}"zos-ppa1-name"="func"{{.*}}}
+// CHECK-DAG:   attributes #[[FUNC1]] = {{{.*}}"zos-ppa1-name"="func1"{{.*}}}
+// CHECK-DAG:   attributes #[[FUNC2]] = {{{.*}}"zos-ppa1-name"="func2"{{.*}}}
+// CHECK-DAG:   attributes #[[FUNC3]] = {{{.*}}"zos-ppa1-name"="func3"{{.*}}}

>From 2604954eb1c51a70817a767f53b4ce11bc1a12d2 Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Tue, 17 Nov 2026 17:24:01 -0500
Subject: [PATCH 05/13] [SystemZ] Require systemz-registered-target in
 pragma-map-conflict.c

---
 clang/test/CodeGen/SystemZ/pragma-map-conflict.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/test/CodeGen/SystemZ/pragma-map-conflict.c b/clang/test/CodeGen/SystemZ/pragma-map-conflict.c
index 62adfbd04ea1b2..57fbaa899a8992 100644
--- a/clang/test/CodeGen/SystemZ/pragma-map-conflict.c
+++ b/clang/test/CodeGen/SystemZ/pragma-map-conflict.c
@@ -1,4 +1,6 @@
 // RUN: %clang_cc1 %s -triple s390x-ibm-zos -emit-llvm -O1 -o - | FileCheck %s
+// REQUIRES: systemz-registered-target
+
 // Give a warning and ignore the pragma map.
 void rp(void) asm("rp_ext");
 void rp(void);

>From cafb79c1e4b49bfc81d5d2b23d5cf106007704ae Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Tue, 17 Nov 2026 22:53:43 -0500
Subject: [PATCH 06/13] [SystemZ] Document #pragma map

---
 clang/docs/LanguageExtensions.md | 26 ++++++++++++++++++++++++++
 clang/docs/ReleaseNotes.md       |  4 ++++
 2 files changed, 30 insertions(+)

diff --git a/clang/docs/LanguageExtensions.md b/clang/docs/LanguageExtensions.md
index cf453ac2e3b69b..8adc4c70867e7e 100644
--- a/clang/docs/LanguageExtensions.md
+++ b/clang/docs/LanguageExtensions.md
@@ -1128,6 +1128,32 @@ generated saying the pragma didn't resolve to a declaration. For example:
 int func(double) { return 0; } // warning: failed to resolve '#pragma export' to a declaration
 ```
 
+### #pragma map
+
+Clang supports the map pragma as a way to change the name of a declaration
+within the source to to another name for external symbols.  This pragma is provided
+mainly for compatibility with the z/OS system headers.  This pragma behaves
+as if the declaration had an asm label.  The syntax for the pragma is:
+
+.. code-block:: c++
+
+  #pragma map ( name , string-literal )
+
+where `name` is the name used in the source code.  This can be an external function or
+variable declared or defined within the translation unit and `string-literal` is a
+string literal that is the external name for the declaration.  The string literal is
+encoded in IBM-1047.
+
+The pragma is only applied to declarations with external linkage and the declaration needs
+to have "C" linkage in C++.  A warning will be generated if the pragma is applied to a
+declaration that  does not have external linkage or no declaration was found for the
+name by the end of the translation unit.
+
+The pragma must appear before the first use of the declaration in the pragma.
+
+Pragma map and asm label can not be specified for the same declaration with different
+external names.
+
 ## Overflow behavior types
 
 Clang provides an extension that allows developers to annotate integer types
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index f2397bc8dcd594..1800beb34d3ef7 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1526,6 +1526,10 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 
 #### SystemZ Support
 
+- Add support for `#pragma map` for z/OS.  This pragma is used by the z/OS
+  system headers to define the external name for declarations in the translation
+  unit.  This is very similar to asm labels and `#pragma redfine_extname`.
+
 ### DWARF Support in Clang
 
 ### Floating Point Support in Clang

>From dea3ac0bf88dc9c7a0307fde3d47a9c7ec886a0b Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Tue, 17 Nov 2026 22:55:18 -0500
Subject: [PATCH 07/13] [SystemZ] Fix typos and code block in #pragma map docs

---
 clang/docs/LanguageExtensions.md | 10 +++++-----
 clang/docs/ReleaseNotes.md       |  2 +-
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/clang/docs/LanguageExtensions.md b/clang/docs/LanguageExtensions.md
index 8adc4c70867e7e..240527c4b174ca 100644
--- a/clang/docs/LanguageExtensions.md
+++ b/clang/docs/LanguageExtensions.md
@@ -1131,13 +1131,13 @@ int func(double) { return 0; } // warning: failed to resolve '#pragma export' to
 ### #pragma map
 
 Clang supports the map pragma as a way to change the name of a declaration
-within the source to to another name for external symbols.  This pragma is provided
+within the source to another name for external symbols.  This pragma is provided
 mainly for compatibility with the z/OS system headers.  This pragma behaves
 as if the declaration had an asm label.  The syntax for the pragma is:
 
-.. code-block:: c++
-
-  #pragma map ( name , string-literal )
+```c++
+#pragma map ( name , string-literal )
+```
 
 where `name` is the name used in the source code.  This can be an external function or
 variable declared or defined within the translation unit and `string-literal` is a
@@ -1146,7 +1146,7 @@ encoded in IBM-1047.
 
 The pragma is only applied to declarations with external linkage and the declaration needs
 to have "C" linkage in C++.  A warning will be generated if the pragma is applied to a
-declaration that  does not have external linkage or no declaration was found for the
+declaration that does not have external linkage or no declaration was found for the
 name by the end of the translation unit.
 
 The pragma must appear before the first use of the declaration in the pragma.
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 1800beb34d3ef7..e47064bd35bba4 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1528,7 +1528,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 
 - Add support for `#pragma map` for z/OS.  This pragma is used by the z/OS
   system headers to define the external name for declarations in the translation
-  unit.  This is very similar to asm labels and `#pragma redfine_extname`.
+  unit.  This is very similar to asm labels and `#pragma redefine_extname`.
 
 ### DWARF Support in Clang
 

>From 0c805a44049204abcc085980c66ef28740b4d7ac Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Tue, 17 Nov 2026 23:06:20 -0500
Subject: [PATCH 08/13] [SystemZ] Serialize unresolved #pragma map in PCH

Write the pending #pragma map entries that have not yet resolved to a
declaration into the AST file (MAPPED_UNDECLARED_IDENTIFIERS), and read them
back through ExternalSemaSource::ReadMappedUndeclaredIdentifiers, so a pragma
in a PCH still applies to a declaration that appears later. This mirrors the
handling of #pragma redefine_extname. Add a PCH test.
---
 clang/include/clang/Sema/ExternalSemaSource.h | 10 ++++
 .../clang/Sema/MultiplexExternalSemaSource.h  | 11 ++++
 clang/include/clang/Sema/Sema.h               |  3 ++
 .../include/clang/Serialization/ASTBitCodes.h |  3 ++
 clang/include/clang/Serialization/ASTReader.h |  9 ++++
 .../lib/Sema/MultiplexExternalSemaSource.cpp  |  6 +++
 clang/lib/Sema/Sema.cpp                       | 17 +++++++
 clang/lib/Sema/SemaAttr.cpp                   |  1 +
 clang/lib/Sema/SemaDecl.cpp                   |  1 +
 clang/lib/Serialization/ASTReader.cpp         | 41 +++++++++++++++
 clang/lib/Serialization/ASTWriter.cpp         | 28 +++++++++++
 clang/test/PCH/pragma-map.c                   | 50 +++++++++++++++++++
 clang/test/PCH/pragma-map.h                   | 18 +++++++
 .../Plugins/ExpressionParser/Clang/ASTUtils.h |  7 +++
 14 files changed, 205 insertions(+)
 create mode 100644 clang/test/PCH/pragma-map.c
 create mode 100644 clang/test/PCH/pragma-map.h

diff --git a/clang/include/clang/Sema/ExternalSemaSource.h b/clang/include/clang/Sema/ExternalSemaSource.h
index 022294e9dbee32..992323c058b128 100644
--- a/clang/include/clang/Sema/ExternalSemaSource.h
+++ b/clang/include/clang/Sema/ExternalSemaSource.h
@@ -173,6 +173,16 @@ class ExternalSemaSource : public ExternalASTSource {
   virtual void ReadExtnameUndeclaredIdentifiers(
       SmallVectorImpl<std::pair<IdentifierInfo *, AsmLabelAttr *>> &EI) {}
 
+  /// Read the set of #pragma map'd, undeclared identifiers known to the
+  /// external Sema source.
+  ///
+  /// The external source should append its own #pragma map'd, undeclared
+  /// identifiers to the given vector. Note that this routine may be invoked
+  /// multiple times; the external source should take care not to introduce the
+  /// same identifiers repeatedly.
+  virtual void ReadMappedUndeclaredIdentifiers(
+      SmallVectorImpl<std::pair<IdentifierInfo *, AsmLabelAttr *>> &MI) {}
+
   /// Read the set of used vtables known to the external Sema source.
   ///
   /// The external source should append its own used vtables to the given
diff --git a/clang/include/clang/Sema/MultiplexExternalSemaSource.h b/clang/include/clang/Sema/MultiplexExternalSemaSource.h
index ad75a1d97d4182..f9149f172b232b 100644
--- a/clang/include/clang/Sema/MultiplexExternalSemaSource.h
+++ b/clang/include/clang/Sema/MultiplexExternalSemaSource.h
@@ -334,6 +334,17 @@ class MultiplexExternalSemaSource : public ExternalSemaSource {
       SmallVectorImpl<std::pair<IdentifierInfo *, AsmLabelAttr *>> &EI)
       override;
 
+  /// Read the set of #pragma map'd, undeclared identifiers known to the
+  /// external Sema source.
+  ///
+  /// The external source should append its own #pragma map'd, undeclared
+  /// identifiers to the given vector. Note that this routine may be invoked
+  /// multiple times; the external source should take care not to introduce the
+  /// same identifiers repeatedly.
+  void ReadMappedUndeclaredIdentifiers(
+      SmallVectorImpl<std::pair<IdentifierInfo *, AsmLabelAttr *>> &MI)
+      override;
+
   /// Read the set of used vtables known to the external Sema source.
   ///
   /// The external source should append its own used vtables to the given
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index f93c24296092ab..f7ea610ad65fe3 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -968,6 +968,9 @@ class Sema final : public SemaBase {
   /// source.
   void LoadExternalExtnameUndeclaredIdentifiers();
 
+  /// Load #pragma map'd undeclared identifiers from the external source.
+  void LoadExternalMappedUndeclaredIdentifiers();
+
   /// Determine if VD, which must be a variable or function, is an external
   /// symbol that nonetheless can't be referenced from outside this translation
   /// unit because its type has no linkage and it's not extern "C".
diff --git a/clang/include/clang/Serialization/ASTBitCodes.h b/clang/include/clang/Serialization/ASTBitCodes.h
index 04cc0c5af89d15..3a3b90ea481645 100644
--- a/clang/include/clang/Serialization/ASTBitCodes.h
+++ b/clang/include/clang/Serialization/ASTBitCodes.h
@@ -754,6 +754,9 @@ enum ASTRecordTypes {
 
   /// Record code for the OpenMP 'requires' directives seen in the TU.
   OMP_REQUIRES_DECLS = 81,
+
+  /// Record code for #pragma map'd undeclared identifiers.
+  MAPPED_UNDECLARED_IDENTIFIERS = 82,
 };
 
 /// Record types used within a source manager block.
diff --git a/clang/include/clang/Serialization/ASTReader.h b/clang/include/clang/Serialization/ASTReader.h
index 275383d40cf808..53f5a863021113 100644
--- a/clang/include/clang/Serialization/ASTReader.h
+++ b/clang/include/clang/Serialization/ASTReader.h
@@ -944,6 +944,11 @@ class ASTReader : public ExternalPreprocessorSource,
   /// triplets in the order of C name, asm name, and source location.
   SmallVector<serialization::IdentifierID, 64> ExtnameUndeclaredIdentifiers;
 
+  /// A snapshot of Sema's #pragma map'd undeclared identifier tracking. Note
+  /// that this vector has 3n entries, being triplets in the order of C name,
+  /// mapped name, and source location.
+  SmallVector<serialization::IdentifierID, 64> MappedUndeclaredIdentifiers;
+
   /// The IDs of type aliases for ext_vectors that exist in the chain.
   ///
   /// Used by Sema for finding sugared names for ext_vectors in diagnostics.
@@ -2335,6 +2340,10 @@ class ASTReader : public ExternalPreprocessorSource,
       SmallVectorImpl<std::pair<IdentifierInfo *, AsmLabelAttr *>> &ExtnameIDs)
       override;
 
+  void ReadMappedUndeclaredIdentifiers(
+      SmallVectorImpl<std::pair<IdentifierInfo *, AsmLabelAttr *>> &MappedIDs)
+      override;
+
   void ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) override;
 
   void ReadPendingInstantiations(
diff --git a/clang/lib/Sema/MultiplexExternalSemaSource.cpp b/clang/lib/Sema/MultiplexExternalSemaSource.cpp
index bf2b19e143a4d8..827d3b04c777bf 100644
--- a/clang/lib/Sema/MultiplexExternalSemaSource.cpp
+++ b/clang/lib/Sema/MultiplexExternalSemaSource.cpp
@@ -326,6 +326,12 @@ void MultiplexExternalSemaSource::ReadExtnameUndeclaredIdentifiers(
     Sources[i]->ReadExtnameUndeclaredIdentifiers(EI);
 }
 
+void MultiplexExternalSemaSource::ReadMappedUndeclaredIdentifiers(
+    SmallVectorImpl<std::pair<IdentifierInfo *, AsmLabelAttr *>> &MI) {
+  for (size_t i = 0; i < Sources.size(); ++i)
+    Sources[i]->ReadMappedUndeclaredIdentifiers(MI);
+}
+
 void MultiplexExternalSemaSource::ReadUsedVTables(
                                   SmallVectorImpl<ExternalVTableUse> &VTables) {
   for(size_t i = 0; i < Sources.size(); ++i)
diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index b79e95824d1aa8..e7b814294594a1 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -1116,6 +1116,22 @@ void Sema::LoadExternalExtnameUndeclaredIdentifiers() {
     ExtnameUndeclaredIdentifiers[ExtnameID.first] = ExtnameID.second;
 }
 
+void Sema::LoadExternalMappedUndeclaredIdentifiers() {
+  if (!ExternalSource)
+    return;
+
+  SmallVector<std::pair<IdentifierInfo *, AsmLabelAttr *>, 4> MappedIDs;
+  ExternalSource->ReadMappedUndeclaredIdentifiers(MappedIDs);
+  for (auto &[II, Attr] : MappedIDs) {
+    PendingPragmaMapInfo Info;
+    Info.NameLoc = Attr->getLocation();
+    Info.Used = false;
+    Info.Attr = Attr;
+    // A pragma seen in this translation unit takes precedence.
+    (void)PendingMappedNames.try_emplace(II, Info);
+  }
+}
+
 typedef llvm::DenseMap<const CXXRecordDecl*, bool> RecordCompleteMap;
 
 /// Returns true, if all methods and nested classes of the given
@@ -1587,6 +1603,7 @@ void Sema::ActOnEndOfTranslationUnit() {
   }
 
   // Visit all pending #pragma map.
+  LoadExternalMappedUndeclaredIdentifiers();
   for (const PendingPragmaMapInfo &Mapped : PendingMappedNames.values()) {
     if (!Mapped.Used)
       Diag(Mapped.NameLoc, diag::warn_failed_to_resolve_pragma) << "map";
diff --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp
index 18d2b50a5afc86..785be78f0d4943 100644
--- a/clang/lib/Sema/SemaAttr.cpp
+++ b/clang/lib/Sema/SemaAttr.cpp
@@ -1491,6 +1491,7 @@ void Sema::ActOnPragmaMap(IdentifierInfo *IdentId, SourceLocation NameLoc,
   NamedDecl *PrevDecl =
       lookupExternCFunctionOrVariable(IdentId, NameLoc, curScope);
   if (!PrevDecl) {
+    LoadExternalMappedUndeclaredIdentifiers();
     auto PendingName = PendingMappedNames.find(IdentId);
     if (PendingName != PendingMappedNames.end()) {
       auto &Info = PendingName->second;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 7c2f093cfd732f..9b8dc619cb5bfb 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -7789,6 +7789,7 @@ void Sema::ProcessPragmaExport(DeclaratorDecl *NewD) {
 }
 
 void Sema::ProcessPragmaMap(DeclaratorDecl *NewD) {
+  LoadExternalMappedUndeclaredIdentifiers();
   if (PendingMappedNames.empty())
     return;
   IdentifierInfo *IdentName = NewD->getIdentifier();
diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp
index db04c06f3cee73..fbfcc5c03c0210 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -4170,6 +4170,26 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
       }
       break;
 
+    case MAPPED_UNDECLARED_IDENTIFIERS:
+      if (Record.size() % 3 != 0)
+        return llvm::createStringError(std::errc::illegal_byte_sequence,
+                                       "invalid mapped identifiers record");
+
+      // FIXME: Ignore #pragma map'd, undeclared identifiers from non-original
+      // PCH files, as is done for #pragma redefine_extname.
+      MappedUndeclaredIdentifiers.clear();
+
+      // Translate the #pragma map'd, undeclared identifiers into global IDs.
+      for (unsigned I = 0, N = Record.size(); I < N; /* in loop */) {
+        MappedUndeclaredIdentifiers.push_back(
+            getGlobalIdentifierID(F, Record[I++]));
+        MappedUndeclaredIdentifiers.push_back(
+            getGlobalIdentifierID(F, Record[I++]));
+        MappedUndeclaredIdentifiers.push_back(
+            ReadSourceLocation(F, Record, I).getRawEncoding());
+      }
+      break;
+
     case SELECTOR_OFFSETS: {
       F.SelectorOffsets = (const uint32_t *)Blob.data();
       F.LocalNumSelectors = Record[0];
@@ -9949,6 +9969,27 @@ void ASTReader::ReadExtnameUndeclaredIdentifiers(
   ExtnameUndeclaredIdentifiers.clear();
 }
 
+void ASTReader::ReadMappedUndeclaredIdentifiers(
+    SmallVectorImpl<std::pair<IdentifierInfo *, AsmLabelAttr *>> &MappedIDs) {
+  if (MappedUndeclaredIdentifiers.empty())
+    return;
+
+  for (unsigned I = 0, N = MappedUndeclaredIdentifiers.size(); I < N; I += 3) {
+    IdentifierInfo *NameId =
+        DecodeIdentifierInfo(MappedUndeclaredIdentifiers[I]);
+    IdentifierInfo *MappedId =
+        DecodeIdentifierInfo(MappedUndeclaredIdentifiers[I + 1]);
+    SourceLocation Loc =
+        SourceLocation::getFromRawEncoding(MappedUndeclaredIdentifiers[I + 2]);
+    AsmLabelAttr *Attr = AsmLabelAttr::CreateImplicit(
+        getContext(), MappedId->getName(),
+        AttributeCommonInfo(NameId, SourceRange(Loc),
+                            AttributeCommonInfo::Form::Pragma()));
+    MappedIDs.push_back(std::make_pair(NameId, Attr));
+  }
+  MappedUndeclaredIdentifiers.clear();
+}
+
 void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) {
   for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) {
     ExternalVTableUse VT;
diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp
index 3ae7ac0e8c9fa0..31694b67570120 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -951,6 +951,7 @@ void ASTWriter::WriteBlockInfoBlock() {
   RECORD(SEMA_DECL_REFS);
   RECORD(WEAK_UNDECLARED_IDENTIFIERS);
   RECORD(EXTNAME_UNDECLARED_IDENTIFIERS);
+  RECORD(MAPPED_UNDECLARED_IDENTIFIERS);
   RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
   RECORD(UPDATE_VISIBLE);
   RECORD(DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD);
@@ -6212,6 +6213,28 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema *SemaPtr, StringRef isysroot,
     }
   }
 
+  // Write the set of #pragma map'd identifiers that have not been resolved to a
+  // declaration yet. The map is keyed by pointer, so sort by name to keep the
+  // output deterministic.
+  RecordData MappedUndeclaredIdentifiers;
+  if (SemaPtr && !isWritingStdCXXNamedModules()) {
+    ASTContext &Context = SemaPtr->Context;
+    SmallVector<std::pair<IdentifierInfo *, Sema::PendingPragmaMapInfo>, 4>
+        Pending;
+    for (const auto &[II, Info] : SemaPtr->PendingMappedNames)
+      if (!Info.Used)
+        Pending.emplace_back(II, Info);
+    llvm::sort(Pending, [](const auto &L, const auto &R) {
+      return L.first->getName() < R.first->getName();
+    });
+    ASTRecordWriter MappedWriter(Context, *this, MappedUndeclaredIdentifiers);
+    for (const auto &[II, Info] : Pending) {
+      MappedWriter.AddIdentifierRef(II);
+      MappedWriter.AddIdentifierRef(&Context.Idents.get(Info.Attr->getLabel()));
+      MappedWriter.AddSourceLocation(Info.NameLoc);
+    }
+  }
+
   // Form the record of special types.
   RecordData SpecialTypes;
   if (SemaPtr) {
@@ -6365,6 +6388,11 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema *SemaPtr, StringRef isysroot,
     Stream.EmitRecord(EXTNAME_UNDECLARED_IDENTIFIERS,
                       ExtnameUndeclaredIdentifiers);
 
+  // Write the record containing #pragma map'd undeclared identifiers.
+  if (!MappedUndeclaredIdentifiers.empty())
+    Stream.EmitRecord(MAPPED_UNDECLARED_IDENTIFIERS,
+                      MappedUndeclaredIdentifiers);
+
   if (!WritingModule) {
     // Write the submodules that were imported, if any.
     struct ModuleInfo {
diff --git a/clang/test/PCH/pragma-map.c b/clang/test/PCH/pragma-map.c
new file mode 100644
index 00000000000000..dd751d868ebbd3
--- /dev/null
+++ b/clang/test/PCH/pragma-map.c
@@ -0,0 +1,50 @@
+// REQUIRES: systemz-registered-target
+
+/// Test this without pch.
+// RUN: %clang_cc1 -triple s390x-ibm-zos -include %S/pragma-map.h %s -emit-llvm -o - | FileCheck %s
+
+/// Test with pch.
+// RUN: %clang_cc1 -triple s390x-ibm-zos -x c-header -emit-pch -o %t %S/pragma-map.h
+// RUN: %clang_cc1 -triple s390x-ibm-zos -include-pch %t %s -emit-llvm -o - | FileCheck %s
+
+/// Compile it a few times to check that the PCH file is deterministic.
+// RUN: %clang_cc1 -triple s390x-ibm-zos -x c-header -emit-pch -o %t.cmp %S/pragma-map.h
+// RUN: diff %t %t.cmp >/dev/null
+// RUN: %clang_cc1 -triple s390x-ibm-zos -x c-header -emit-pch -o %t.cmp %S/pragma-map.h
+// RUN: diff %t %t.cmp >/dev/null
+// RUN: %clang_cc1 -triple s390x-ibm-zos -x c-header -emit-pch -o %t.cmp %S/pragma-map.h
+// RUN: diff %t %t.cmp >/dev/null
+
+/// Check that #pragma map is exported into PCHs even if the header contains no
+/// declaration of the symbol.
+void undecfunc1(void);
+void undecfunc2(void) {}
+void undecfunc3(void);
+void undecfunc4(void);
+void undecfunc5(void);
+void undecfunc6(void);
+extern int undecvar;
+void undecesc(void);
+
+void use(void) {
+  undecfunc1();
+  undecfunc2();
+  undecfunc3();
+  undecfunc4();
+  undecfunc5();
+  undecfunc6();
+  undecvar = 1;
+  undecesc();
+  declfunc();
+}
+
+// CHECK-DAG: @UNDECVAR = external global i32
+// CHECK-DAG: define void @UNDEC2()
+// CHECK-DAG: call void @UNDEC1()
+// CHECK-DAG: call void @UNDEC2()
+// CHECK-DAG: call void @UNDEC3()
+// CHECK-DAG: call void @UNDEC4()
+// CHECK-DAG: call void @UNDEC5()
+// CHECK-DAG: call void @UNDEC6()
+// CHECK-DAG: call void @"@ESC"()
+// CHECK-DAG: call void @DECL()
diff --git a/clang/test/PCH/pragma-map.h b/clang/test/PCH/pragma-map.h
new file mode 100644
index 00000000000000..2053cdf2a0a92e
--- /dev/null
+++ b/clang/test/PCH/pragma-map.h
@@ -0,0 +1,18 @@
+// Header for PCH test pragma-map.c
+
+// None of these symbols is declared in this header; the declarations come from
+// the file that includes it.
+#pragma map(undecfunc1, "UNDEC1")
+#pragma map(undecfunc2, "UNDEC2")
+#pragma map(undecfunc3, "UNDEC3")
+#pragma map(undecfunc4, "UNDEC4")
+#pragma map(undecfunc5, "UNDEC5")
+#pragma map(undecfunc6, "UNDEC6")
+#pragma map(undecvar, "UNDECVAR")
+
+// A numeric escape sequence is an IBM-1047 code point; '@' is 0x7C (\174).
+#pragma map(undecesc, "\174ESC")
+
+// The symbol is declared after the pragma, in this header.
+#pragma map(declfunc, "DECL")
+void declfunc(void);
diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ASTUtils.h b/lldb/source/Plugins/ExpressionParser/Clang/ASTUtils.h
index 3529bb03b5d896..6006e504169a21 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ASTUtils.h
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ASTUtils.h
@@ -601,6 +601,13 @@ class SemaSourceWithPriorities : public clang::ExternalSemaSource {
       Source->ReadExtnameUndeclaredIdentifiers(EI);
   }
 
+  void ReadMappedUndeclaredIdentifiers(
+      llvm::SmallVectorImpl<std::pair<clang::IdentifierInfo *,
+                                      clang::AsmLabelAttr *>> &MI) override {
+    for (auto &Source : Sources)
+      Source->ReadMappedUndeclaredIdentifiers(MI);
+  }
+
   void ReadUsedVTables(
       llvm::SmallVectorImpl<clang::ExternalVTableUse> &VTables) override {
     for (auto &Source : Sources)

>From 7db1881c92d089d419a7c6b71c32aae74700d955 Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Wed, 18 Nov 2026 10:21:08 -0500
Subject: [PATCH 09/13] address comments

---
 clang/include/clang/Parse/Parser.h |  3 ---
 clang/lib/Sema/SemaAttr.cpp        | 12 ++++++------
 2 files changed, 6 insertions(+), 9 deletions(-)

diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h
index 2dc8ea69feea63..8d79281abf4b02 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -7323,9 +7323,6 @@ class Parser : public CodeCompletionHandler {
 
   void zOSHandlePragmaHelper(tok::TokenKind);
 
-  bool zOSParseParameterList(StringRef PragmaName,
-                             std::optional<SmallVector<QualType, 4>> &TypeList);
-
   /// Handle the annotation token produced for
   /// #pragma export ...
   void HandlePragmaExport();
diff --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp
index 785be78f0d4943..13fd23275d8837 100644
--- a/clang/lib/Sema/SemaAttr.cpp
+++ b/clang/lib/Sema/SemaAttr.cpp
@@ -1467,7 +1467,8 @@ void Sema::ActOnPragmaMap(IdentifierInfo *IdentId, SourceLocation NameLoc,
   assert(!MappedName.empty());
 
   // All identifiers must be in UTF-8. On z/OS the mapped name was parsed with
-  // CA_ToLiteralEncodingForAsm, so it is in IBM-1047 and must be converted back.
+  // CA_ToLiteralEncodingForAsm, so it is in IBM-1047 and must be converted
+  // back.
   llvm::SmallString<16> MappedNameUTF;
   if (llvm::TextEncodingConverter *Conv =
           PP.getTextEncoding().getzOSFromAsmLiteralEncodingConverter()) {
@@ -1492,15 +1493,14 @@ void Sema::ActOnPragmaMap(IdentifierInfo *IdentId, SourceLocation NameLoc,
       lookupExternCFunctionOrVariable(IdentId, NameLoc, curScope);
   if (!PrevDecl) {
     LoadExternalMappedUndeclaredIdentifiers();
-    auto PendingName = PendingMappedNames.find(IdentId);
-    if (PendingName != PendingMappedNames.end()) {
-      auto &Info = PendingName->second;
-      if (!Info.Attr->isEquivalent(Attr)) {
+    auto [It, Inserted] = PendingMappedNames.try_emplace(IdentId, Info);
+    if (!Inserted) {
+      if (!It->second.Attr->isEquivalent(Attr)) {
         Diag(NameLoc, diag::warn_pragma_different_asm_label) << IdentId;
         return;
       }
+      It->second = Info;
     }
-    PendingMappedNames[IdentId] = Info;
     return;
   }
 

>From 1f14f49025a6893ac4dd8c1b2a127cc868c2f4f5 Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Wed, 18 Nov 2026 16:29:50 -0500
Subject: [PATCH 10/13] [SystemZ] Set source-date-epoch in pragma-map PCH test

---
 clang/test/PCH/pragma-map.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/test/PCH/pragma-map.c b/clang/test/PCH/pragma-map.c
index dd751d868ebbd3..45891c797e7cd7 100644
--- a/clang/test/PCH/pragma-map.c
+++ b/clang/test/PCH/pragma-map.c
@@ -4,15 +4,15 @@
 // RUN: %clang_cc1 -triple s390x-ibm-zos -include %S/pragma-map.h %s -emit-llvm -o - | FileCheck %s
 
 /// Test with pch.
-// RUN: %clang_cc1 -triple s390x-ibm-zos -x c-header -emit-pch -o %t %S/pragma-map.h
+// RUN: %clang_cc1 -source-date-epoch 1613676318 -triple s390x-ibm-zos -x c-header -emit-pch -o %t %S/pragma-map.h
 // RUN: %clang_cc1 -triple s390x-ibm-zos -include-pch %t %s -emit-llvm -o - | FileCheck %s
 
 /// Compile it a few times to check that the PCH file is deterministic.
-// RUN: %clang_cc1 -triple s390x-ibm-zos -x c-header -emit-pch -o %t.cmp %S/pragma-map.h
+// RUN: %clang_cc1 -source-date-epoch 1613676318 -triple s390x-ibm-zos -x c-header -emit-pch -o %t.cmp %S/pragma-map.h
 // RUN: diff %t %t.cmp >/dev/null
-// RUN: %clang_cc1 -triple s390x-ibm-zos -x c-header -emit-pch -o %t.cmp %S/pragma-map.h
+// RUN: %clang_cc1 -source-date-epoch 1613676318 -triple s390x-ibm-zos -x c-header -emit-pch -o %t.cmp %S/pragma-map.h
 // RUN: diff %t %t.cmp >/dev/null
-// RUN: %clang_cc1 -triple s390x-ibm-zos -x c-header -emit-pch -o %t.cmp %S/pragma-map.h
+// RUN: %clang_cc1 -source-date-epoch 1613676318 -triple s390x-ibm-zos -x c-header -emit-pch -o %t.cmp %S/pragma-map.h
 // RUN: diff %t %t.cmp >/dev/null
 
 /// Check that #pragma map is exported into PCHs even if the header contains no

>From 89741bf409d55c211f609a0f18e35b83047b65fc Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Tue, 24 Nov 2026 17:15:06 -0500
Subject: [PATCH 11/13] no need to add info again

---
 clang/lib/Sema/SemaAttr.cpp | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp
index 13fd23275d8837..7dfe575624a3c2 100644
--- a/clang/lib/Sema/SemaAttr.cpp
+++ b/clang/lib/Sema/SemaAttr.cpp
@@ -1494,13 +1494,8 @@ void Sema::ActOnPragmaMap(IdentifierInfo *IdentId, SourceLocation NameLoc,
   if (!PrevDecl) {
     LoadExternalMappedUndeclaredIdentifiers();
     auto [It, Inserted] = PendingMappedNames.try_emplace(IdentId, Info);
-    if (!Inserted) {
-      if (!It->second.Attr->isEquivalent(Attr)) {
-        Diag(NameLoc, diag::warn_pragma_different_asm_label) << IdentId;
-        return;
-      }
-      It->second = Info;
-    }
+    if (!Inserted && !It->second.Attr->isEquivalent(Attr))
+      Diag(NameLoc, diag::warn_pragma_different_asm_label) << IdentId;
     return;
   }
 

>From 5e961dc6f70bb91fcd7c7da6c2f3911720bcc8e2 Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Wed, 25 Nov 2026 11:13:20 -0500
Subject: [PATCH 12/13] Apply batched suggestions from code review

Co-authored-by: Aaron Ballman <aaron at aaronballman.com>
---
 clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 98c5a09a7a08e6..551ddeacdf19c1 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -1301,7 +1301,7 @@ def warn_failed_to_resolve_pragma : Warning<
   "failed to resolve '#pragma %0' to a declaration">,
   InGroup<IgnoredPragmas>;
 def warn_pragma_different_asm_label : Warning<
-  "#pragma map has conflicting asm label; not applied to %0">,
+  "'#pragma map' has conflicting asm label; not applied to %0">,
   InGroup<IgnoredPragmas>;
 def warn_pragma_not_applied : Warning<
   "#pragma %0 is applicable to symbols with external linkage only; "

>From f18e34db6cb1e74a413bb822f070ffc0f98cec93 Mon Sep 17 00:00:00 2001
From: Sean Perry <perry at ca.ibm.com>
Date: Thu, 26 Nov 2026 01:17:46 -0500
Subject: [PATCH 13/13] interact with overloadable and alias attributes

---
 clang/docs/LanguageExtensions.md              |  4 +
 clang/lib/Sema/SemaAttr.cpp                   | 15 +++-
 clang/lib/Sema/SemaDecl.cpp                   |  4 +
 .../CodeGen/SystemZ/pragma-map-alias-diag.c   | 16 ++++
 clang/test/CodeGen/SystemZ/pragma-map-alias.c | 74 ++++++++++++++++
 .../CodeGen/SystemZ/pragma-map-overloadable.c | 87 +++++++++++++++++++
 clang/test/Sema/pragma-map-alias.c            | 33 +++++++
 clang/test/Sema/pragma-map.c                  | 25 ++++++
 8 files changed, 255 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/CodeGen/SystemZ/pragma-map-alias-diag.c
 create mode 100644 clang/test/CodeGen/SystemZ/pragma-map-alias.c
 create mode 100644 clang/test/CodeGen/SystemZ/pragma-map-overloadable.c
 create mode 100644 clang/test/Sema/pragma-map-alias.c

diff --git a/clang/docs/LanguageExtensions.md b/clang/docs/LanguageExtensions.md
index 240527c4b174ca..c3982635c9d7f4 100644
--- a/clang/docs/LanguageExtensions.md
+++ b/clang/docs/LanguageExtensions.md
@@ -1149,6 +1149,10 @@ to have "C" linkage in C++.  A warning will be generated if the pragma is applie
 declaration that does not have external linkage or no declaration was found for the
 name by the end of the translation unit.
 
+In C, functions declared with `__attribute__((overloadable))` are mangled, so the
+pragma only applies to the one overload of that name, if any, that does not have
+the attribute.
+
 The pragma must appear before the first use of the declaration in the pragma.
 
 Pragma map and asm label can not be specified for the same declaration with different
diff --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp
index 7dfe575624a3c2..d9201cb5052a52 100644
--- a/clang/lib/Sema/SemaAttr.cpp
+++ b/clang/lib/Sema/SemaAttr.cpp
@@ -1407,8 +1407,15 @@ NamedDecl *Sema::lookupExternCFunctionOrVariable(IdentifierInfo *IdentId,
                                                  Scope *curScope) {
   LookupResult Result(*this, IdentId, NameLoc, LookupOrdinaryName);
   LookupName(Result, curScope);
-  if (!getLangOpts().CPlusPlus)
-    return Result.getAsSingle<NamedDecl>();
+  if (!getLangOpts().CPlusPlus) {
+    // In C, functions declared with __attribute__((overloadable)) are mangled;
+    // at most one function of a given name can omit the attribute, and that is
+    // the one with the plain C name.
+    for (NamedDecl *D : Result)
+      if (isa<VarDecl>(D) || !D->hasAttr<OverloadableAttr>())
+        return D;
+    return nullptr;
+  }
   for (NamedDecl *D : Result) {
     if (auto *FD = dyn_cast<FunctionDecl>(D))
       if (FD->isExternC())
@@ -1500,7 +1507,9 @@ void Sema::ActOnPragmaMap(IdentifierInfo *IdentId, SourceLocation NameLoc,
   }
 
   if (auto *FD = dyn_cast<FunctionDecl>(PrevDecl->getCanonicalDecl())) {
-    if (FD->hasBody()) {
+    // isDefined() also covers definitions that have no body, such as a
+    // function declared with the alias attribute.
+    if (FD->isDefined()) {
       Diag(NameLoc, diag::warn_pragma_not_applied_to_used_symbol)
           << "map" << PrevDecl;
       return;
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 9b8dc619cb5bfb..2dbd43db151304 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -7803,6 +7803,10 @@ void Sema::ProcessPragmaMap(DeclaratorDecl *NewD) {
         if (getLangOpts().CPlusPlus) {
           if (!FD->isExternC())
             return;
+        } else if (FD->hasAttr<OverloadableAttr>()) {
+          // In C, an overloadable function is mangled, so the pragma applies
+          // only to the (single) overload that lacks the attribute.
+          return;
         }
       }
       Info.Used = true;
diff --git a/clang/test/CodeGen/SystemZ/pragma-map-alias-diag.c b/clang/test/CodeGen/SystemZ/pragma-map-alias-diag.c
new file mode 100644
index 00000000000000..8ab398ab826777
--- /dev/null
+++ b/clang/test/CodeGen/SystemZ/pragma-map-alias-diag.c
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 %s -triple s390x-ibm-zos -emit-llvm-only -verify
+// REQUIRES: systemz-registered-target
+
+// Once the aliasee is mapped, its symbol name is the mapped name, so an alias
+// that names it by its source name no longer refers to a defined symbol.
+#pragma map(t1, "T1")
+void t1(void) {}
+void a1(void) __attribute__((alias("t1"))); // expected-error {{alias must point to a defined variable or function}} \
+                                            // expected-note {{the function or variable specified in an alias must refer to its mangled name}} \
+                                            // expected-note {{function by that name is mangled as "T1"}}
+
+#pragma map(v2, "V2")
+int v2 = 2;
+extern int va2 __attribute__((alias("v2"))); // expected-error {{alias must point to a defined variable or function}} \
+                                             // expected-note {{the function or variable specified in an alias must refer to its mangled name}} \
+                                             // expected-note {{function by that name is mangled as "V2"}}
diff --git a/clang/test/CodeGen/SystemZ/pragma-map-alias.c b/clang/test/CodeGen/SystemZ/pragma-map-alias.c
new file mode 100644
index 00000000000000..8ffbfb782b0cdf
--- /dev/null
+++ b/clang/test/CodeGen/SystemZ/pragma-map-alias.c
@@ -0,0 +1,74 @@
+// RUN: %clang_cc1 %s -triple s390x-ibm-zos -emit-llvm -o - | FileCheck %s
+// REQUIRES: systemz-registered-target
+
+// The aliasee of an alias attribute is a symbol name, so it must use the mapped
+// name when the aliasee is mapped. A mapped alias is emitted under its mapped
+// name.
+
+// Mapped aliasee; the alias itself is not mapped.
+#pragma map(t1, "T1")
+void t1(void) {}
+void a1(void) __attribute__((alias("T1")));
+
+// Mapped alias, pragma before the alias declaration.
+void t2(void) {}
+#pragma map(a2, "A2")
+void a2(void) __attribute__((alias("t2")));
+
+// A function declared with the alias attribute is a definition, so a pragma
+// after the alias declaration is not applied (the Sema test checks the
+// warning). The alias keeps its own name.
+void t3(void) {}
+void a3(void) __attribute__((alias("t3")));
+#pragma map(a3, "A3")
+
+// Mapped alias and mapped aliasee.
+#pragma map(t4, "T4")
+void t4(void) {}
+#pragma map(a4, "A4")
+void a4(void) __attribute__((alias("T4")));
+
+// Variables: mapped aliasee and mapped alias, with the pragma before the alias
+// declaration.
+#pragma map(v5, "V5")
+int v5 = 5;
+#pragma map(va5, "VA5")
+extern int va5 __attribute__((alias("V5")));
+
+// Variable alias; the pragma after the alias declaration is applied.
+int v6 = 6;
+extern int va6 __attribute__((alias("v6")));
+#pragma map(va6, "VA6")
+
+// Variable aliasee mapped; the alias itself is not mapped.
+#pragma map(v7, "V7")
+int v7 = 7;
+extern int va7 __attribute__((alias("V7")));
+
+int use(void) {
+  a1();
+  a2();
+  a3();
+  a4();
+  return va5 + va6 + va7;
+}
+
+// CHECK-DAG: @VA5 = {{.*}}alias i32, ptr @V5
+// CHECK-DAG: @VA6 = {{.*}}alias i32, ptr @v6
+// CHECK-DAG: @va7 = {{.*}}alias i32, ptr @V7
+// CHECK-DAG: @a1 = {{.*}}alias void (), ptr @T1
+// CHECK-DAG: @A2 = {{.*}}alias void (), ptr @t2
+// CHECK-DAG: @a3 = {{.*}}alias void (), ptr @t3
+// CHECK-DAG: @A4 = {{.*}}alias void (), ptr @T4
+// CHECK-DAG: define{{.*}} void @T1()
+// CHECK-DAG: define{{.*}} void @t2()
+// CHECK-DAG: define{{.*}} void @t3()
+// CHECK-DAG: define{{.*}} void @T4()
+// CHECK:     define{{.*}} i32 @use()
+// CHECK:       call void @a1()
+// CHECK-NEXT:  call void @A2()
+// CHECK-NEXT:  call void @a3()
+// CHECK-NEXT:  call void @A4()
+// CHECK:       load i32, ptr @VA5
+// CHECK:       load i32, ptr @VA6
+// CHECK:       load i32, ptr @va7
diff --git a/clang/test/CodeGen/SystemZ/pragma-map-overloadable.c b/clang/test/CodeGen/SystemZ/pragma-map-overloadable.c
new file mode 100644
index 00000000000000..709f4d10030123
--- /dev/null
+++ b/clang/test/CodeGen/SystemZ/pragma-map-overloadable.c
@@ -0,0 +1,87 @@
+// RUN: %clang_cc1 %s -triple s390x-ibm-zos -emit-llvm -o - | FileCheck %s
+// REQUIRES: systemz-registered-target
+
+// Overloadable functions are mangled, so #pragma map only applies to the
+// overload that lacks __attribute__((overloadable)).
+
+// Pragma after the declarations.
+void a(double) __attribute__((overloadable));
+void a(int);
+#pragma map(a, "A")
+
+// Pragma before the declarations, overloadable function first. The pragma must
+// not be attached to the overloadable declaration (ProcessPragmaMap); it is
+// applied when the unmarked overload is declared.
+#pragma map(b, "B")
+void b(double) __attribute__((overloadable));
+void b(int);
+
+// Unmarked overload declared first.
+void c(int);
+void c(double) __attribute__((overloadable));
+#pragma map(c, "C")
+
+// Every overload is marked; the pragma is not applied.
+void d(int) __attribute__((overloadable));
+void d(double) __attribute__((overloadable));
+#pragma map(d, "D")
+
+// Pragma before the unmarked overload, which is declared first.
+#pragma map(e, "E")
+void e(int);
+void e(double) __attribute__((overloadable));
+
+// Pragma between the declarations, marked overload first.
+void f(double) __attribute__((overloadable));
+#pragma map(f, "F")
+void f(int);
+
+// Pragma between the declarations, unmarked overload first.
+void g(int);
+#pragma map(g, "G")
+void g(double) __attribute__((overloadable));
+
+// Using the overloadable overload does not prevent mapping the unmarked one.
+void h(double) __attribute__((overloadable));
+void h(int);
+void use_h(void) {
+  h(1.0);
+}
+#pragma map(h, "H")
+
+void t(void) {
+  a(1);
+  a(1.0);
+  b(1);
+  b(1.0);
+  c(1);
+  c(1.0);
+  d(1);
+  d(1.0);
+  e(1);
+  e(1.0);
+  f(1);
+  f(1.0);
+  g(1);
+  g(1.0);
+  h(1);
+}
+
+// CHECK: define {{.*}}void @use_h()
+// CHECK: call void @_Z1hd(double {{.*}})
+// CHECK: define {{.*}}void @t()
+// CHECK: call void @A(i32 {{.*}})
+// CHECK: call void @_Z1ad(double {{.*}})
+// CHECK: call void @B(i32 {{.*}})
+// CHECK: call void @_Z1bd(double {{.*}})
+// CHECK: call void @C(i32 {{.*}})
+// CHECK: call void @_Z1cd(double {{.*}})
+// CHECK: call void @_Z1di(i32 {{.*}})
+// CHECK: call void @_Z1dd(double {{.*}})
+// CHECK: call void @E(i32 {{.*}})
+// CHECK: call void @_Z1ed(double {{.*}})
+// CHECK: call void @F(i32 {{.*}})
+// CHECK: call void @_Z1fd(double {{.*}})
+// CHECK: call void @G(i32 {{.*}})
+// CHECK: call void @_Z1gd(double {{.*}})
+// CHECK: call void @H(i32 {{.*}})
diff --git a/clang/test/Sema/pragma-map-alias.c b/clang/test/Sema/pragma-map-alias.c
new file mode 100644
index 00000000000000..c6be1fbf5efa07
--- /dev/null
+++ b/clang/test/Sema/pragma-map-alias.c
@@ -0,0 +1,33 @@
+// RUN: %clang_cc1 %s -triple s390x-ibm-zos -fsyntax-only -verify
+// REQUIRES: systemz-registered-target
+
+// Cases that are accepted are covered in
+// CodeGen/SystemZ/pragma-map-alias.c.
+
+// An alias attribute that names its aliasee by source name marks the aliasee
+// as used, so a later #pragma map on the aliasee is not applied.
+void t1(void);
+void a1(void) __attribute__((alias("t1")));
+#pragma map(t1, "T1") // expected-warning {{#pragma map can only applied to unused symbols}}
+
+// Same for variables.
+extern int v1;
+extern int va1 __attribute__((alias("v1")));
+#pragma map(v1, "V1") // expected-warning {{#pragma map can only applied to unused symbols}}
+
+// A function declared with the alias attribute is a definition, so the pragma
+// is not applied to it when it appears after the alias declaration.
+void t2(void) {}
+void a2(void) __attribute__((alias("t2")));
+#pragma map(a2, "A2") // expected-warning {{#pragma map can only applied to unused symbols}}
+
+// An alias that has already been used.
+void t3(void) {}
+void a3(void) __attribute__((alias("t3")));
+void use_a3(void) { a3(); }
+#pragma map(a3, "A3") // expected-warning {{#pragma map can only applied to unused symbols}}
+
+// Conflicting asm label on the alias.
+void t4(void) {}
+#pragma map(a4, "A4") // expected-warning {{#pragma map has conflicting asm label; not applied to 'a4'}}
+void a4(void) asm("A4X") __attribute__((alias("t4")));
diff --git a/clang/test/Sema/pragma-map.c b/clang/test/Sema/pragma-map.c
index 166883663469d4..9a0d3438ac888a 100644
--- a/clang/test/Sema/pragma-map.c
+++ b/clang/test/Sema/pragma-map.c
@@ -53,4 +53,29 @@ int func() {
 static int var_static;
 #pragma map(var_static,"VAR2") // expected-warning {{#pragma map is applicable to symbols with external linkage only; not applied to 'var_static'}}
 
+// Testing __attribute__((overloadable)). Overloadable functions are mangled, so
+// the pragma can only apply to the single overload that lacks the attribute.
+// Cases that are accepted are covered in
+// CodeGen/SystemZ/pragma-map-overloadable.c.
 
+// All overloads are marked, so there is nothing to map.
+void ov1(int) __attribute__((overloadable));
+void ov1(double) __attribute__((overloadable));
+#pragma map(ov1, "OV1") // expected-warning{{failed to resolve '#pragma map' to a declaration}}
+
+#pragma map(ov2, "OV2") // expected-warning{{failed to resolve '#pragma map' to a declaration}}
+void ov2(int) __attribute__((overloadable));
+void ov2(double) __attribute__((overloadable));
+
+// Conflicting labels are diagnosed against the unmarked overload only.
+void ov3(double) __attribute__((overloadable));
+void ov3(int) asm("OV3A");
+#pragma map(ov3, "OV3B") // expected-warning {{#pragma map has conflicting asm label; not applied to 'ov3'}}
+
+// Using the unmarked overload prevents mapping.
+void ov4(double) __attribute__((overloadable));
+void ov4(int);
+void use_ov4(void) {
+  ov4(1);
+}
+#pragma map(ov4, "OV4") // expected-warning {{#pragma map can only applied to unused symbols}}



More information about the cfe-commits mailing list