8028434: Line number nodes were off for while nodes and do while nodes - the line number of a loop node should be treated as the location of the test expression
authorlagergren
Mon, 18 Nov 2013 16:35:39 +0100
changeset 21868 f3cd4fa7f8b9
parent 21867 4e5ee0aeb468
child 21869 da99d0cbd413
8028434: Line number nodes were off for while nodes and do while nodes - the line number of a loop node should be treated as the location of the test expression Reviewed-by: jlaskey, sundar
nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java
nashorn/src/jdk/nashorn/internal/ir/debug/PrintVisitor.java
nashorn/src/jdk/nashorn/internal/parser/Parser.java
nashorn/test/script/basic/JDK-8028434.js
nashorn/test/script/basic/JDK-8028434.js.EXPECTED
--- a/nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java	Sat Nov 16 00:23:46 2013 +0100
+++ b/nashorn/src/jdk/nashorn/internal/codegen/CodeGenerator.java	Mon Nov 18 16:35:39 2013 +0100
@@ -2186,15 +2186,14 @@
 
     @Override
     public boolean enterWhileNode(final WhileNode whileNode) {
-        lineNumber(whileNode);
-
         final Expression test          = whileNode.getTest();
         final Block      body          = whileNode.getBody();
         final Label      breakLabel    = whileNode.getBreakLabel();
         final Label      continueLabel = whileNode.getContinueLabel();
+        final boolean    isDoWhile     = whileNode.isDoWhile();
         final Label      loopLabel     = new Label("loop");
 
-        if (!whileNode.isDoWhile()) {
+        if (!isDoWhile) {
             method._goto(continueLabel);
         }
 
@@ -2202,6 +2201,7 @@
         body.accept(this);
         if (!whileNode.isTerminal()) {
             method.label(continueLabel);
+            lineNumber(whileNode);
             new BranchOptimizer(this, method).execute(test, loopLabel, true);
             method.label(breakLabel);
         }
--- a/nashorn/src/jdk/nashorn/internal/ir/debug/PrintVisitor.java	Sat Nov 16 00:23:46 2013 +0100
+++ b/nashorn/src/jdk/nashorn/internal/ir/debug/PrintVisitor.java	Mon Nov 18 16:35:39 2013 +0100
@@ -28,6 +28,7 @@
 import java.util.List;
 import jdk.nashorn.internal.ir.BinaryNode;
 import jdk.nashorn.internal.ir.Block;
+import jdk.nashorn.internal.ir.BlockStatement;
 import jdk.nashorn.internal.ir.CaseNode;
 import jdk.nashorn.internal.ir.CatchNode;
 import jdk.nashorn.internal.ir.ExpressionStatement;
@@ -141,7 +142,6 @@
     @Override
     public boolean enterBlock(final Block block) {
         sb.append(' ');
-        //sb.append(Debug.id(block));
         sb.append('{');
 
         indent += TABWIDTH;
@@ -190,8 +190,13 @@
         sb.append(EOLN);
         indent();
         sb.append('}');
-       // sb.append(Debug.id(block));
+
+        return false;
+    }
 
+    @Override
+    public boolean enterBlockStatement(final BlockStatement statement) {
+        statement.getBlock().accept(this);
         return false;
     }
 
@@ -233,7 +238,6 @@
     public boolean enterFunctionNode(final FunctionNode functionNode) {
         functionNode.toString(sb);
         enterBlock(functionNode.getBody());
-        //sb.append(EOLN);
         return false;
     }
 
--- a/nashorn/src/jdk/nashorn/internal/parser/Parser.java	Sat Nov 16 00:23:46 2013 +0100
+++ b/nashorn/src/jdk/nashorn/internal/parser/Parser.java	Mon Nov 18 16:35:39 2013 +0100
@@ -1210,21 +1210,24 @@
      */
     private void whileStatement() {
         // Capture WHILE token.
-        final int  whileLine  = line;
         final long whileToken = token;
         // WHILE tested in caller.
         next();
 
         // Construct WHILE node.
-        WhileNode whileNode = new WhileNode(whileLine, whileToken, Token.descPosition(whileToken), false);
+        WhileNode whileNode = new WhileNode(line, whileToken, Token.descPosition(whileToken), false);
         lc.push(whileNode);
 
         try {
             expect(LPAREN);
-            whileNode = whileNode.setTest(lc, expression());
+            final int whileLine = line;
+            final Expression test = expression();
             expect(RPAREN);
-            whileNode = whileNode.setBody(lc, getStatement());
-            appendStatement(whileNode);
+            final Block body = getStatement();
+            appendStatement(whileNode =
+                new WhileNode(whileLine, whileToken, finish, false).
+                    setTest(lc, test).
+                    setBody(lc, body));
         } finally {
             lc.pop(whileNode);
         }
@@ -1242,28 +1245,33 @@
      */
     private void doStatement() {
         // Capture DO token.
-        final int  doLine  = line;
         final long doToken = token;
         // DO tested in the caller.
         next();
 
-        WhileNode doWhileNode = new WhileNode(doLine, doToken, Token.descPosition(doToken), true);
+        WhileNode doWhileNode = new WhileNode(-1, doToken, Token.descPosition(doToken), true);
         lc.push(doWhileNode);
 
         try {
            // Get DO body.
-            doWhileNode = doWhileNode.setBody(lc, getStatement());
+            final Block body = getStatement();
 
             expect(WHILE);
             expect(LPAREN);
-            doWhileNode = doWhileNode.setTest(lc, expression());
+            final int doLine = line;
+            final Expression test = expression();
             expect(RPAREN);
 
             if (type == SEMICOLON) {
                 endOfLine();
             }
             doWhileNode.setFinish(finish);
-            appendStatement(doWhileNode);
+
+            //line number is last
+            appendStatement(doWhileNode =
+                new WhileNode(doLine, doToken, finish, true).
+                    setBody(lc, body).
+                    setTest(lc, test));
         } finally {
             lc.pop(doWhileNode);
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8028434.js	Mon Nov 18 16:35:39 2013 +0100
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8028434: Check that the line number of the tests in while and do while loops
+ * is correct. It needs to correspond to the line with the test expression.
+ *
+ * @test
+ * @run
+ */
+
+try {
+    while (test.apa < 0) {
+	print("x");
+    }
+} catch (e) {
+    var st = e.getStackTrace();
+    if (st.length != 1) {
+	print("erroneous stacktrace length " + s.length);
+    }
+    if (st[0].lineNumber !== 32) {
+	print("erroneous stacktrace element, lineNumber=" + st[0].lineNumber + " elem=" + st);
+    }
+}
+
+try {
+    do {
+	print("x");
+    } while (test.apa < 0);
+} catch (e) {
+    var st = e.getStackTrace();
+    if (st.length != 1) {
+	print("erroneous stacktrace length " + s.length);
+    }
+    if (st[0].lineNumber !== 48) {
+	print("erroneous stacktrace element, lineNumber= " + st[0].lineNumber + " elem=" + st);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8028434.js.EXPECTED	Mon Nov 18 16:35:39 2013 +0100
@@ -0,0 +1,1 @@
+x