From 2d09298bee743db5a10a9c92791e43b6135f897e Mon Sep 17 00:00:00 2001
From: Hans Wennborg <hans@hanshq.net>
Date: Fri, 27 Jan 2017 16:45:42 +0000
Subject: [PATCH] Merging r292590:
 ------------------------------------------------------------------------
 r292590 | abataev | 2017-01-20 00:57:28 -0800 (Fri, 20 Jan 2017) | 6 lines

[OPENMP] Fix for PR31643: Clang crashes when compiling code on Windows
with SEH and openmp

In some cituations (during codegen for Windows SEH constructs)
CodeGenFunction instance may have CurFn equal to nullptr. OpenMP related
code does not expect such situation during cleanup.
------------------------------------------------------------------------


git-svn-id: https://llvm.org/svn/llvm-project/cfe/branches/release_40@293302 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/CodeGen/CodeGenFunction.cpp |  3 +--
 test/OpenMP/openmp_seh.c        | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 2 deletions(-)
 create mode 100644 test/OpenMP/openmp_seh.c

diff --git a/lib/CodeGen/CodeGenFunction.cpp b/lib/CodeGen/CodeGenFunction.cpp
index 137c69420dd..e142a21b1e7 100644
--- a/lib/CodeGen/CodeGenFunction.cpp
+++ b/lib/CodeGen/CodeGenFunction.cpp
@@ -112,9 +112,8 @@ CodeGenFunction::~CodeGenFunction() {
   if (FirstBlockInfo)
     destroyBlockInfos(FirstBlockInfo);
 
-  if (getLangOpts().OpenMP) {
+  if (getLangOpts().OpenMP && CurFn)
     CGM.getOpenMPRuntime().functionFinished(*this);
-  }
 }
 
 CharUnits CodeGenFunction::getNaturalPointeeTypeAlignment(QualType T,
diff --git a/test/OpenMP/openmp_seh.c b/test/OpenMP/openmp_seh.c
new file mode 100644
index 00000000000..74dccec125e
--- /dev/null
+++ b/test/OpenMP/openmp_seh.c
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -verify -triple x86_64-pc-windows-msvc19.0.0 -fopenmp -fms-compatibility -x c++ -emit-llvm %s -o - | FileCheck %s
+// expected-no-diagnostics
+// REQUIRES: x86-registered-target
+extern "C" {
+void __cpuid(int[4], int);
+}
+
+// CHECK-LABEL: @main
+int main(void) {
+  __try {
+    int info[4];
+    __cpuid(info, 1);
+  } __except (1) {
+  }
+
+  return 0;
+}
+
-- 
GitLab