From bbd242ec866d3a7ac79aff61964a8af01f880c75 Mon Sep 17 00:00:00 2001
From: David Majnemer <david.majnemer@gmail.com>
Date: Thu, 14 Aug 2014 06:35:08 +0000
Subject: [PATCH] Parse: Don't attempt to act on #pragma init_seg when not
 targeting MSVC

It doesn't really make sense to try and do stuff with #pragma init_seg
when targeting non-Microsoft platforms; notions like library vs user
initializers don't exist for other targets.

This fixes PR20639.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@215618 91177308-0d34-0410-b5e6-96231b3b80d8
---
 include/clang/Basic/DiagnosticParseKinds.td | 5 +++++
 lib/Parse/ParsePragma.cpp                   | 6 ++++++
 test/SemaCXX/pragma-init_seg.cpp            | 8 +++++++-
 3 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/include/clang/Basic/DiagnosticParseKinds.td b/include/clang/Basic/DiagnosticParseKinds.td
index 8937449c264..80cdb3d26aa 100644
--- a/include/clang/Basic/DiagnosticParseKinds.td
+++ b/include/clang/Basic/DiagnosticParseKinds.td
@@ -867,6 +867,11 @@ def warn_pragma_pack_malformed : Warning<
 def warn_pragma_unused_expected_var : Warning<
   "expected '#pragma unused' argument to be a variable name">,
   InGroup<IgnoredPragmas>;
+// - #pragma init_seg
+def warn_pragma_init_seg_unsupported_target : Warning<
+  "'#pragma init_seg' is only supported when targeting a "
+  "Microsoft environment">,
+  InGroup<IgnoredPragmas>;
 // - #pragma fp_contract
 def err_pragma_fp_contract_scope : Error<
   "'#pragma fp_contract' can only appear at file scope or at the start of a "
diff --git a/lib/Parse/ParsePragma.cpp b/lib/Parse/ParsePragma.cpp
index ecdef48f0e2..64066c1ec29 100644
--- a/lib/Parse/ParsePragma.cpp
+++ b/lib/Parse/ParsePragma.cpp
@@ -12,6 +12,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "RAIIObjectsForParser.h"
+#include "clang/Basic/TargetInfo.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Parse/ParseDiagnostic.h"
 #include "clang/Parse/Parser.h"
@@ -661,6 +662,11 @@ bool Parser::HandlePragmaMSSegment(StringRef PragmaName,
 // #pragma init_seg({ compiler | lib | user | "section-name" [, func-name]} )
 bool Parser::HandlePragmaMSInitSeg(StringRef PragmaName,
                                    SourceLocation PragmaLocation) {
+  if (getTargetInfo().getTriple().getEnvironment() != llvm::Triple::MSVC) {
+    PP.Diag(PragmaLocation, diag::warn_pragma_init_seg_unsupported_target);
+    return false;
+  }
+
   if (ExpectAndConsume(tok::l_paren, diag::warn_pragma_expected_lparen,
                        PragmaName))
     return false;
diff --git a/test/SemaCXX/pragma-init_seg.cpp b/test/SemaCXX/pragma-init_seg.cpp
index 38520b0c2ef..e18d0e6814a 100644
--- a/test/SemaCXX/pragma-init_seg.cpp
+++ b/test/SemaCXX/pragma-init_seg.cpp
@@ -1,5 +1,7 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -fms-extensions %s -triple x86_64-pc-win32
+// RUN: %clang_cc1 -fsyntax-only -verify -fms-extensions %s -triple i386-apple-darwin13.3.0
 
+#ifndef __APPLE__
 #pragma init_seg(L".my_seg") // expected-warning {{expected 'compiler', 'lib', 'user', or a string literal}}
 #pragma init_seg( // expected-warning {{expected 'compiler', 'lib', 'user', or a string literal}}
 #pragma init_seg asdf // expected-warning {{missing '('}}
@@ -10,6 +12,10 @@
 #pragma init_seg("\x") // expected-error {{\x used with no following hex digits}}
 #pragma init_seg("a" L"b") // expected-warning {{expected non-wide string literal in '#pragma init_seg'}}
 
-int f();
 #pragma init_seg(compiler)
+#else
+#pragma init_seg(compiler) // expected-warning {{'#pragma init_seg' is only supported when targeting a Microsoft environment}}
+#endif
+
+int f();
 int __declspec(thread) x = f(); // expected-error {{initializer for thread-local variable must be a constant expression}}
-- 
GitLab