From a3d0406d31bf7468cd1fe51683c73bd854e30a91 Mon Sep 17 00:00:00 2001
From: Erik Verbruggen <erik.verbruggen@nokia.com>
Date: Tue, 2 Feb 2010 15:38:21 +0100
Subject: [PATCH] Fixed throw expression parsing.

---
 src/shared/cplusplus/Parser.cpp      | 24 ++++++++++++++++++++----
 tests/auto/cplusplus/ast/tst_ast.cpp |  9 +++++++++
 2 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/src/shared/cplusplus/Parser.cpp b/src/shared/cplusplus/Parser.cpp
index 904f1a31892..61efa2baa82 100644
--- a/src/shared/cplusplus/Parser.cpp
+++ b/src/shared/cplusplus/Parser.cpp
@@ -182,7 +182,10 @@ inline bool isRightAssociative(int tokenKind)
 #endif
 
 #define PARSE_EXPRESSION_WITH_OPERATOR_PRECEDENCE(node, minPrecedence) { \
-    if (!parseCastExpression(node)) \
+    if (LA() == T_THROW) { \
+        if (!parseThrowExpression(node)) \
+            return false; \
+    } else if (!parseCastExpression(node)) \
         return false; \
     \
     parseExpressionWithOperatorPrecedence(node, minPrecedence); \
@@ -2322,14 +2325,27 @@ bool Parser::parseStringLiteral(ExpressionAST *&node)
 bool Parser::parseExpressionStatement(StatementAST *&node)
 {
     DEBUG_THIS_RULE();
-    ExpressionAST *expression = 0;
-    if (LA() == T_SEMICOLON || parseExpression(expression)) {
+    if (LA() == T_SEMICOLON) {
         ExpressionStatementAST *ast = new (_pool) ExpressionStatementAST;
-        ast->expression = expression;
         match(T_SEMICOLON, &ast->semicolon_token);
         node = ast;
         return true;
     }
+
+    ExpressionAST *expression = 0;
+    MemoryPool *oldPool = _pool;
+    MemoryPool tmp;
+    _pool = &tmp;
+    if (parseExpression(expression)) {
+        ExpressionStatementAST *ast = new (oldPool) ExpressionStatementAST;
+        ast->expression = expression->clone(oldPool);
+        match(T_SEMICOLON, &ast->semicolon_token);
+        node = ast;
+        _pool = oldPool;
+        return true;
+    }
+    _pool = oldPool;
+
     return false;
 }
 
diff --git a/tests/auto/cplusplus/ast/tst_ast.cpp b/tests/auto/cplusplus/ast/tst_ast.cpp
index c9c8b73f463..b23bd76eea7 100644
--- a/tests/auto/cplusplus/ast/tst_ast.cpp
+++ b/tests/auto/cplusplus/ast/tst_ast.cpp
@@ -51,6 +51,7 @@ private slots:
     void condition_1();
     void init_1();
     void conditional_1();
+    void throw_1();
 
     // statements
     void if_statement_1();
@@ -341,6 +342,14 @@ void tst_AST::conditional_1()
     QCOMPARE(unit->spell(one->literal_token), "1");
 }
 
+void tst_AST::throw_1()
+{
+    QSharedPointer<TranslationUnit> unit(parseStatement("throw 1;"));
+    AST *ast = unit->ast();
+    QVERIFY(ast != 0);
+    QVERIFY(ast->asExpressionStatement());
+}
+
 void tst_AST::function_call_1()
 {
     QSharedPointer<TranslationUnit> unit(parseStatement("retranslateUi(blah);"));
-- 
GitLab