8189796: Incorrect end position for missing statement

Recording end positions for error trees representing missing statements.

Co-authored-by: Dusan Balek <dusan.balek@oracle.com>
Reviewed-by: mcimadamore
This commit is contained in:
Jan Lahoda 2017-10-24 12:07:27 +02:00
parent ed0ea37394
commit a663434be7
2 changed files with 45 additions and 3 deletions

View File

@ -2396,9 +2396,8 @@ public class JavacParser implements Parser {
int pos = token.pos;
List<JCStatement> stats = blockStatement();
if (stats.isEmpty()) {
JCErroneous e = F.at(pos).Erroneous();
error(e, "illegal.start.of.stmt");
return F.at(pos).Exec(e);
JCErroneous e = syntaxError(pos, "illegal.start.of.stmt");
return toP(F.at(pos).Exec(e));
} else {
JCStatement first = stats.head;
String error = null;

View File

@ -38,6 +38,7 @@ import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.ErroneousTree;
import com.sun.source.tree.ExpressionStatementTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.IfTree;
import com.sun.source.tree.LambdaExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.MethodTree;
@ -284,6 +285,48 @@ public class JavacParserTest extends TestCase {
1, t.getSourcePositions().getStartPosition(cut, clazz));
}
@Test
void testPositionMissingStatement() throws IOException {
String code = "class C { void t() { if (true) } }";
DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<>();
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, dc, null,
null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next();
Trees trees = Trees.instance(ct);
SourcePositions positions = trees.getSourcePositions();
new TreeScanner<Void, Void>() {
@Override
public Void visitIf(IfTree it, Void v) {
StatementTree st = it.getThenStatement();
int startpos = (int) positions.getStartPosition(cut, st);
int endpos = (int) positions.getEndPosition(cut, st);
assertEquals("testPositionMissingStatement.execpos", startpos, endpos);
assertEquals("testPositionMissingStatement.execkind",
Kind.EXPRESSION_STATEMENT,
st.getKind());
Tree err = ((ExpressionStatementTree) st).getExpression();
startpos = (int) positions.getStartPosition(cut, err);
endpos = (int) positions.getEndPosition(cut, err);
assertEquals("testPositionMissingStatement.errpos", startpos, endpos);
assertEquals("testPositionMissingStatement.errkind",
Kind.ERRONEOUS,
err.getKind());
return super.visitIf(it, v);
}
}.scan(cut, null);
assertEquals("testPositionMissingStatement.diags", 1, dc.getDiagnostics().size());
Diagnostic<? extends JavaFileObject> d = dc.getDiagnostics().get(0);
int startpos = (int) d.getStartPosition();
int pos = (int) d.getPosition();
int endpos = (int) d.getEndPosition();
assertEquals("testPositionMissingStatement.diagspan", startpos, endpos);
assertEquals("testPositionMissingStatement.diagpref", startpos, pos);
}
@Test
void testPositionsSane1() throws IOException {
performPositionsSanityTest("package test; class Test { " +