Re-land r228258 and make clang-cl's /EHs- disable -fexceptions again

After r228258, Clang started emitting C++ EH IR that LLVM wasn't ready
to deal with, even when exceptions were disabled with /EHs-. This time,
make /EHs- turn off -fexceptions while still emitting exceptional
constructs in functions using __try.  Since Sema rejects C++ exception
handling constructs before CodeGen, landingpads should only appear in
such functions as the result of a __try.

llvm-svn: 228329
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index d17cf22..4761c32 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -3303,11 +3303,12 @@
   if (getCurScope() && getCurScope()->isOpenMPSimdDirectiveScope())
     Diag(TryLoc, diag::err_omp_simd_region_cannot_use_stmt) << "try";
 
+  sema::FunctionScopeInfo *FSI = getCurFunction();
+
   // C++ try is incompatible with SEH __try.
-  if (!getLangOpts().Borland && getCurFunction()->FirstSEHTryLoc.isValid()) {
+  if (!getLangOpts().Borland && FSI->FirstSEHTryLoc.isValid()) {
     Diag(TryLoc, diag::err_mixing_cxx_try_seh_try);
-    Diag(getCurFunction()->FirstSEHTryLoc, diag::note_conflicting_try_here)
-        << "'__try'";
+    Diag(FSI->FirstSEHTryLoc, diag::note_conflicting_try_here) << "'__try'";
   }
 
   const unsigned NumHandlers = Handlers.size();
@@ -3352,7 +3353,7 @@
     }
   }
 
-  getCurFunction()->setHasCXXTry(TryLoc);
+  FSI->setHasCXXTry(TryLoc);
 
   // FIXME: We should detect handlers that cannot catch anything because an
   // earlier handler catches a superclass. Need to find a method that is not
@@ -3367,15 +3368,29 @@
                                   Stmt *TryBlock, Stmt *Handler) {
   assert(TryBlock && Handler);
 
+  sema::FunctionScopeInfo *FSI = getCurFunction();
+
   // SEH __try is incompatible with C++ try. Borland appears to support this,
   // however.
-  if (!getLangOpts().Borland && getCurFunction()->FirstCXXTryLoc.isValid()) {
-    Diag(TryLoc, diag::err_mixing_cxx_try_seh_try);
-    Diag(getCurFunction()->FirstCXXTryLoc, diag::note_conflicting_try_here)
-        << "'try'";
+  if (!getLangOpts().Borland) {
+    if (FSI->FirstCXXTryLoc.isValid()) {
+      Diag(TryLoc, diag::err_mixing_cxx_try_seh_try);
+      Diag(FSI->FirstCXXTryLoc, diag::note_conflicting_try_here) << "'try'";
+    }
   }
 
-  getCurFunction()->setHasSEHTry(TryLoc);
+  FSI->setHasSEHTry(TryLoc);
+
+  // Reject __try in Obj-C methods, blocks, and captured decls, since we don't
+  // track if they use SEH.
+  DeclContext *DC = CurContext;
+  while (DC && !DC->isFunctionOrMethod())
+    DC = DC->getParent();
+  FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(DC);
+  if (FD)
+    FD->setUsesSEHTry(true);
+  else
+    Diag(TryLoc, diag::err_seh_try_outside_functions);
 
   return SEHTryStmt::Create(Context, IsCXXTry, TryLoc, TryBlock, Handler);
 }