From c9ba172447f2e2fe938ae03c3b16b91bda5b4dc3 Mon Sep 17 00:00:00 2001 From: Steve Naroff Date: Wed, 16 Jul 2008 15:31:30 +0000 Subject: [PATCH] RewriteObjC::RewriteObjCTryStmt():Don't synthesize a catch begin if there are 0 catch clauses. This fixes clang ObjC rewriter: @try / @finally block produces unbalanced output. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@53679 91177308-0d34-0410-b5e6-96231b3b80d8 --- Driver/RewriteObjC.cpp | 26 +++++++++++++------------- test/Rewriter/rewrite-try-catch.m | 18 +++++++++++------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/Driver/RewriteObjC.cpp b/Driver/RewriteObjC.cpp index 0154aaebd9..73277d163a 100644 --- a/Driver/RewriteObjC.cpp +++ b/Driver/RewriteObjC.cpp @@ -1323,22 +1323,22 @@ Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { startBuf = SM->getCharacterData(startLoc); assert((*startBuf == '}') && "bogus @try block"); - - SourceLocation lastCurlyLoc = startLoc; - - startLoc = startLoc.getFileLocWithOffset(1); - buf = " /* @catch begin */ else {\n"; - buf += " id _caught = objc_exception_extract(&_stack);\n"; - buf += " objc_exception_try_enter (&_stack);\n"; - buf += " if (_setjmp(_stack.buf))\n"; - buf += " _rethrow = objc_exception_extract(&_stack);\n"; - buf += " else { /* @catch continue */"; - - InsertText(startLoc, buf.c_str(), buf.size()); + SourceLocation lastCurlyLoc = startLoc; + ObjCAtCatchStmt *catchList = S->getCatchStmts(); + if (catchList) { + startLoc = startLoc.getFileLocWithOffset(1); + buf = " /* @catch begin */ else {\n"; + buf += " id _caught = objc_exception_extract(&_stack);\n"; + buf += " objc_exception_try_enter (&_stack);\n"; + buf += " if (_setjmp(_stack.buf))\n"; + buf += " _rethrow = objc_exception_extract(&_stack);\n"; + buf += " else { /* @catch continue */"; + + InsertText(startLoc, buf.c_str(), buf.size()); + } bool sawIdTypedCatch = false; Stmt *lastCatchBody = 0; - ObjCAtCatchStmt *catchList = S->getCatchStmts(); while (catchList) { Stmt *catchStmt = catchList->getCatchParamStmt(); diff --git a/test/Rewriter/rewrite-try-catch.m b/test/Rewriter/rewrite-try-catch.m index 14966dccb3..be0bf09158 100644 --- a/test/Rewriter/rewrite-try-catch.m +++ b/test/Rewriter/rewrite-try-catch.m @@ -11,13 +11,17 @@ void foo() { int main() { -@try { - MYTRY(); -} + @try { + MYTRY(); + } -@catch (Foo* localException) { - MYCATCH(); - @throw; -} + @catch (Foo* localException) { + MYCATCH(); + @throw; + } + + // no catch clause + @try { } + @finally { } } -- 2.40.0