Implemented parsing and AST support for the MS __leave exception statement. Also a minor fix to __except printing in StmtPrinter.cpp. Thanks to Aaron Ballman for review.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@163083 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/AST/RecursiveASTVisitor.h b/include/clang/AST/RecursiveASTVisitor.h
index ae4ff4b..46585d6 100644
--- a/include/clang/AST/RecursiveASTVisitor.h
+++ b/include/clang/AST/RecursiveASTVisitor.h
@@ -2202,6 +2202,7 @@
 })
 
 DEF_TRAVERSE_STMT(SEHTryStmt, {})
+DEF_TRAVERSE_STMT(SEHLeaveStmt, {})
 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
 DEF_TRAVERSE_STMT(SEHFinallyStmt,{})
 
diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h
index 042c513..e43a6f9 100644
--- a/include/clang/AST/Stmt.h
+++ b/include/clang/AST/Stmt.h
@@ -1889,6 +1889,34 @@
   static bool classof(SEHTryStmt *) { return true; }
 };
 
+class SEHLeaveStmt : public Stmt {
+  SourceLocation  LeaveLoc;
+
+  SEHLeaveStmt(SourceLocation LeaveLoc);
+
+  friend class ASTReader;
+  friend class ASTStmtReader;
+  explicit SEHLeaveStmt(EmptyShell E) : Stmt(SEHLeaveStmtClass, E) { }
+
+public:
+  static SEHLeaveStmt* Create(ASTContext &C,
+                              SourceLocation LeaveLoc);
+
+  SourceRange getSourceRange() const LLVM_READONLY {
+    return SourceRange(getLeaveLoc());
+  }
+
+  SourceLocation getLeaveLoc() const { return LeaveLoc; }
+
+  child_range children() { return child_range(); }
+
+  static bool classof(const Stmt *T) {
+    return T->getStmtClass() == SEHLeaveStmtClass;
+  }
+
+  static bool classof(SEHLeaveStmt *) { return true; }
+};
+
 }  // end namespace clang
 
 #endif
diff --git a/include/clang/Basic/DiagnosticParseKinds.td b/include/clang/Basic/DiagnosticParseKinds.td
index e4e339a..5608fa8 100644
--- a/include/clang/Basic/DiagnosticParseKinds.td
+++ b/include/clang/Basic/DiagnosticParseKinds.td
@@ -740,6 +740,9 @@
 
 def err_seh___finally_block : Error<
   "%0 only allowed in __finally block">;
+
+def err_seh___try_block : Error<
+  "%0 only allowed in __try block">;
   
 } // end of Parse Issue category.
 
diff --git a/include/clang/Basic/StmtNodes.td b/include/clang/Basic/StmtNodes.td
index 06da947..46ae07f 100644
--- a/include/clang/Basic/StmtNodes.td
+++ b/include/clang/Basic/StmtNodes.td
@@ -164,6 +164,7 @@
 // Microsoft Extensions.
 def CXXUuidofExpr : DStmt<Expr>; 
 def SEHTryStmt : Stmt;
+def SEHLeaveStmt : Stmt;
 def SEHExceptStmt : Stmt;
 def SEHFinallyStmt : Stmt;
 def MSDependentExistsStmt : Stmt;
diff --git a/include/clang/Sema/Scope.h b/include/clang/Sema/Scope.h
index b78556e..201b2e7 100644
--- a/include/clang/Sema/Scope.h
+++ b/include/clang/Sema/Scope.h
@@ -82,7 +82,10 @@
     SwitchScope = 0x800,
 
     /// TryScope - This is the scope of a C++ try statement.
-    TryScope = 0x1000
+    TryScope = 0x1000,
+
+    /// SEHTryScope - This is scope of a Microsoft SEH try statement.
+    SEHTryScope = 0x2000
   };
 private:
   /// The parent scope for this scope.  This is null for the translation-unit
@@ -292,6 +295,9 @@
   /// \brief Determine whether this scope is a C++ 'try' block.
   bool isTryScope() const { return getFlags() & Scope::TryScope; }
 
+  /// \brief Determine whether this scope is a MS SEH 'try' block.
+  bool isSEHTryScope() const { return getFlags() & Scope::SEHTryScope; }
+
   /// containedInPrototypeScope - Return true if this or a parent scope
   /// is a FunctionPrototypeScope.
   bool containedInPrototypeScope() const;
diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h
index 5fdb09c..fd0cd9c 100644
--- a/include/clang/Sema/Sema.h
+++ b/include/clang/Sema/Sema.h
@@ -2622,6 +2622,8 @@
                               Stmt *TryBlock,
                               Stmt *Handler);
 
+  StmtResult ActOnSEHLeaveStmt(SourceLocation LeaveLoc);
+
   StmtResult ActOnSEHExceptBlock(SourceLocation Loc,
                                  Expr *FilterExpr,
                                  Stmt *Block);
diff --git a/include/clang/Serialization/ASTBitCodes.h b/include/clang/Serialization/ASTBitCodes.h
index f213413..25303ef 100644
--- a/include/clang/Serialization/ASTBitCodes.h
+++ b/include/clang/Serialization/ASTBitCodes.h
@@ -1204,6 +1204,7 @@
       STMT_SEH_EXCEPT,            // SEHExceptStmt
       STMT_SEH_FINALLY,           // SEHFinallyStmt
       STMT_SEH_TRY,               // SEHTryStmt
+      STMT_SEH_LEAVE,             // SEHLeaveStmt
       
       // ARC
       EXPR_OBJC_BRIDGED_CAST,     // ObjCBridgedCastExpr
diff --git a/lib/AST/Stmt.cpp b/lib/AST/Stmt.cpp
index ddd47bf..dfe61f2 100644
--- a/lib/AST/Stmt.cpp
+++ b/lib/AST/Stmt.cpp
@@ -1006,6 +1006,17 @@
   return dyn_cast<SEHFinallyStmt>(getHandler());
 }
 
+SEHLeaveStmt::SEHLeaveStmt(SourceLocation LeaveLoc)
+  : Stmt(SEHLeaveStmtClass),
+    LeaveLoc(LeaveLoc)
+{
+}
+
+SEHLeaveStmt* SEHLeaveStmt::Create(ASTContext &C,
+                                   SourceLocation LeaveLoc) {
+  return new(C) SEHLeaveStmt(LeaveLoc);
+}
+
 SEHExceptStmt::SEHExceptStmt(SourceLocation Loc,
                              Expr *FilterExpr,
                              Stmt *Block)
diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp
index 0cf3aaf..ea8878e 100644
--- a/lib/AST/StmtPrinter.cpp
+++ b/lib/AST/StmtPrinter.cpp
@@ -537,6 +537,11 @@
   OS << "\n";
 }
 
+void StmtPrinter::VisitSEHLeaveStmt(SEHLeaveStmt *Node) {
+  Indent() << "__leave;";
+  OS << "\n";
+}
+
 void StmtPrinter::PrintRawSEHFinallyStmt(SEHFinallyStmt *Node) {
   OS << "__finally ";
   PrintRawCompoundStmt(Node->getBlock());
@@ -546,7 +551,7 @@
 void StmtPrinter::PrintRawSEHExceptHandler(SEHExceptStmt *Node) {
   OS << "__except (";
   VisitExpr(Node->getFilterExpr());
-  OS << ")\n";
+  OS << ") ";
   PrintRawCompoundStmt(Node->getBlock());
   OS << "\n";
 }
diff --git a/lib/AST/StmtProfile.cpp b/lib/AST/StmtProfile.cpp
index 5d7f9f8..1ab789e 100644
--- a/lib/AST/StmtProfile.cpp
+++ b/lib/AST/StmtProfile.cpp
@@ -215,6 +215,10 @@
   VisitStmt(S);
 }
 
+void StmtProfiler::VisitSEHLeaveStmt(const SEHLeaveStmt *S) {
+  VisitStmt(S);
+}
+
 void StmtProfiler::VisitObjCForCollectionStmt(const ObjCForCollectionStmt *S) {
   VisitStmt(S);
 }
diff --git a/lib/CodeGen/CGStmt.cpp b/lib/CodeGen/CGStmt.cpp
index 465fb96..c535ec4 100644
--- a/lib/CodeGen/CGStmt.cpp
+++ b/lib/CodeGen/CGStmt.cpp
@@ -163,6 +163,7 @@
   case Stmt::CXXForRangeStmtClass:
     EmitCXXForRangeStmt(cast<CXXForRangeStmt>(*S));
   case Stmt::SEHTryStmtClass:
+  case Stmt::SEHLeaveStmtClass:
     // FIXME Not yet implemented
     break;
   }
diff --git a/lib/Parse/ParseStmt.cpp b/lib/Parse/ParseStmt.cpp
index f58f90d..f46ccb2 100644
--- a/lib/Parse/ParseStmt.cpp
+++ b/lib/Parse/ParseStmt.cpp
@@ -183,6 +183,19 @@
 
     return ParseExprStatement();
   }
+  
+  case tok::kw___leave: {
+    Token LeaveTok = Tok;
+    ConsumeToken();
+    if (getCurScope()->isSEHTryScope()) {
+      Res = Actions.ActOnSEHLeaveStmt(LeaveTok.getLocation());
+    } else {
+      Diag(LeaveTok, diag::err_seh___try_block)
+         << LeaveTok.getIdentifierInfo()->getName();
+      Res = StmtError();
+    }
+    break;
+  }
 
   case tok::kw_case:                // C99 6.8.1: labeled-statement
     return ParseCaseStatement();
@@ -322,7 +335,9 @@
   if(Tok.isNot(tok::l_brace))
     return StmtError(Diag(Tok,diag::err_expected_lbrace));
 
-  StmtResult TryBlock(ParseCompoundStatement());
+  // Use the SEHTryScope to handle __leave as a statement.
+  unsigned ScopeFlags = Scope::DeclScope | Scope::SEHTryScope;
+  StmtResult TryBlock(ParseCompoundStatement(false /*isStmtExpr*/, ScopeFlags));
   if(TryBlock.isInvalid())
     return TryBlock;
 
diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp
index 76410e2..e9866ff 100644
--- a/lib/Sema/SemaStmt.cpp
+++ b/lib/Sema/SemaStmt.cpp
@@ -2746,6 +2746,12 @@
 }
 
 StmtResult
+Sema::ActOnSEHLeaveStmt(SourceLocation LeaveLoc)
+{
+  return Owned(SEHLeaveStmt::Create(Context, LeaveLoc));
+}
+
+StmtResult
 Sema::ActOnSEHExceptBlock(SourceLocation Loc,
                           Expr *FilterExpr,
                           Stmt *Block) {
diff --git a/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h
index 19636f4..10eb52a 100644
--- a/lib/Sema/TreeTransform.h
+++ b/lib/Sema/TreeTransform.h
@@ -1361,6 +1361,10 @@
     return getSema().ActOnSEHTryBlock(IsCXXTry,TryLoc,TryBlock,Handler);
   }
 
+  StmtResult RebuildSEHLeaveStmt(SourceLocation LeaveLoc) {
+    return getSema().ActOnSEHLeaveStmt(LeaveLoc);
+  }
+
   StmtResult RebuildSEHExceptStmt(SourceLocation Loc,
                                   Expr *FilterExpr,
                                   Stmt *Block) {
@@ -6001,6 +6005,12 @@
 
 template<typename Derived>
 StmtResult
+TreeTransform<Derived>::TransformSEHLeaveStmt(SEHLeaveStmt *S) {
+  return getDerived().RebuildSEHLeaveStmt(S->getLeaveLoc());
+}
+
+template<typename Derived>
+StmtResult
 TreeTransform<Derived>::TransformSEHFinallyStmt(SEHFinallyStmt *S) {
   StmtResult Block; //  = getDerived().TransformCompoundStatement(S->getBlock());
   if(Block.isInvalid()) return StmtError();
diff --git a/lib/Serialization/ASTReaderStmt.cpp b/lib/Serialization/ASTReaderStmt.cpp
index 6ca450b..3252c7d 100644
--- a/lib/Serialization/ASTReaderStmt.cpp
+++ b/lib/Serialization/ASTReaderStmt.cpp
@@ -1516,6 +1516,11 @@
   S->Children[SEHTryStmt::HANDLER] = Reader.ReadSubStmt();
 }
 
+void ASTStmtReader::VisitSEHLeaveStmt(SEHLeaveStmt *S) {
+  VisitStmt(S);
+  S->LeaveLoc = ReadSourceLocation(Record, Idx);
+}
+
 //===----------------------------------------------------------------------===//
 // CUDA Expressions and Statements
 //===----------------------------------------------------------------------===//
@@ -1986,6 +1991,9 @@
     case STMT_SEH_TRY:
       S = new (Context) SEHTryStmt(Empty);
       break;
+    case STMT_SEH_LEAVE:
+      S = new (Context) SEHLeaveStmt(Empty);
+      break;
     case STMT_CXX_CATCH:
       S = new (Context) CXXCatchStmt(Empty);
       break;
diff --git a/lib/Serialization/ASTWriterStmt.cpp b/lib/Serialization/ASTWriterStmt.cpp
index 5fa08d2..9d626d1 100644
--- a/lib/Serialization/ASTWriterStmt.cpp
+++ b/lib/Serialization/ASTWriterStmt.cpp
@@ -1554,6 +1554,12 @@
   Code = serialization::STMT_SEH_TRY;
 }
 
+void ASTStmtWriter::VisitSEHLeaveStmt(SEHLeaveStmt *S) {
+  VisitStmt(S);
+  Writer.AddSourceLocation(S->getLeaveLoc(), Record);
+  Code = serialization::STMT_SEH_LEAVE;
+}
+
 //===----------------------------------------------------------------------===//
 // ASTWriter Implementation
 //===----------------------------------------------------------------------===//
diff --git a/lib/StaticAnalyzer/Core/ExprEngine.cpp b/lib/StaticAnalyzer/Core/ExprEngine.cpp
index 4225c67..5c4492a 100644
--- a/lib/StaticAnalyzer/Core/ExprEngine.cpp
+++ b/lib/StaticAnalyzer/Core/ExprEngine.cpp
@@ -527,6 +527,7 @@
     case Stmt::PackExpansionExprClass:
     case Stmt::SubstNonTypeTemplateParmPackExprClass:
     case Stmt::SEHTryStmtClass:
+    case Stmt::SEHLeaveStmtClass:
     case Stmt::SEHExceptStmtClass:
     case Stmt::LambdaExprClass:
     case Stmt::SEHFinallyStmtClass: {
diff --git a/test/Parser/MicrosoftExtensions.c b/test/Parser/MicrosoftExtensions.c
index 7703999..57eee04 100644
--- a/test/Parser/MicrosoftExtensions.c
+++ b/test/Parser/MicrosoftExtensions.c
@@ -102,3 +102,9 @@
   struct S7 s;
   int i = s.t;	/* expected-warning {{'t' is deprecated}} */
 }
+
+void SEH() {
+  __try { __leave; } __except (0) {}
+  __try { } __except (0) { __leave; } // expected-error {{__leave only allowed in __try block}}
+  __try { } __finally { __leave; } // expected-error {{__leave only allowed in __try block}}
+}
\ No newline at end of file
diff --git a/tools/libclang/RecursiveASTVisitor.h b/tools/libclang/RecursiveASTVisitor.h
index ab96f36..6642d81 100644
--- a/tools/libclang/RecursiveASTVisitor.h
+++ b/tools/libclang/RecursiveASTVisitor.h
@@ -2122,6 +2122,7 @@
 })
 
 DEF_TRAVERSE_STMT(SEHTryStmt, {})
+DEF_TRAVERSE_STMT(SEHLeaveStmt, {})
 DEF_TRAVERSE_STMT(SEHExceptStmt, {})
 DEF_TRAVERSE_STMT(SEHFinallyStmt,{})