Attempt to make more accurate debug information

This commit is contained in:
konsoletyper 2014-10-01 12:08:13 +04:00
parent b7754ea1a5
commit f628a996ac
2 changed files with 71 additions and 40 deletions

View File

@ -136,6 +136,10 @@ class OptimizingVisitor implements StatementVisitor, ExprVisitor {
return; return;
} }
VariableExpr var = (VariableExpr)assignment.getLeftValue(); VariableExpr var = (VariableExpr)assignment.getLeftValue();
if (var.getLocation() != null && assignment.getLocation() != null &&
!assignment.getLocation().equals(var.getLocation())) {
return;
}
if (var.getIndex() == index) { if (var.getIndex() == index) {
resultSequence.remove(resultSequence.size() - 1); resultSequence.remove(resultSequence.size() - 1);
assignment.getRightValue().setLocation(assignment.getLocation()); assignment.getRightValue().setLocation(assignment.getLocation());

View File

@ -50,7 +50,7 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
private Properties properties = new Properties(); private Properties properties = new Properties();
private ServiceRepository services; private ServiceRepository services;
private DebugInformationEmitter debugEmitter = new DummyDebugInformationEmitter(); private DebugInformationEmitter debugEmitter = new DummyDebugInformationEmitter();
private Deque<NodeLocation> locationStack = new ArrayDeque<>(); private Deque<LocationStackEntry> locationStack = new ArrayDeque<>();
private DeferredCallSite lastCallSite; private DeferredCallSite lastCallSite;
private DeferredCallSite prevCallSite; private DeferredCallSite prevCallSite;
@ -62,6 +62,14 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
} }
private static class LocationStackEntry {
NodeLocation location;
public LocationStackEntry(NodeLocation location) {
this.location = location;
}
}
public void addInjector(MethodReference method, Injector injector) { public void addInjector(MethodReference method, Injector injector) {
injectorMap.put(method, new InjectorHolder(injector)); injectorMap.put(method, new InjectorHolder(injector));
} }
@ -581,19 +589,26 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
} }
private void pushLocation(NodeLocation location) { private void pushLocation(NodeLocation location) {
locationStack.push(location); LocationStackEntry prevEntry = locationStack.peek();
if (location != null) { if (location != null) {
if (prevEntry == null || !location.equals(prevEntry.location)) {
debugEmitter.emitLocation(location.getFileName(), location.getLine()); debugEmitter.emitLocation(location.getFileName(), location.getLine());
}
} else { } else {
if (prevEntry != null) {
debugEmitter.emitLocation(null, -1); debugEmitter.emitLocation(null, -1);
} }
} }
locationStack.push(new LocationStackEntry(location));
}
private void popLocation() { private void popLocation() {
locationStack.pop(); LocationStackEntry prevEntry = locationStack.pop();
NodeLocation location = locationStack.peek(); LocationStackEntry entry = locationStack.peek();
if (location != null) { if (entry != null) {
debugEmitter.emitLocation(location.getFileName(), location.getLine()); if (!entry.location.equals(prevEntry.location)) {
debugEmitter.emitLocation(entry.location.getFileName(), entry.location.getLine());
}
} else { } else {
debugEmitter.emitLocation(null, -1); debugEmitter.emitLocation(null, -1);
} }
@ -643,11 +658,11 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
prevCallSite = debugEmitter.emitCallSite(); prevCallSite = debugEmitter.emitCallSite();
writer.append("if").ws().append("("); writer.append("if").ws().append("(");
statement.getCondition().acceptVisitor(this); statement.getCondition().acceptVisitor(this);
debugEmitter.emitCallSite();
writer.append(")").ws().append("{").softNewLine().indent();
if (statement.getCondition().getLocation() != null) { if (statement.getCondition().getLocation() != null) {
popLocation(); popLocation();
} }
debugEmitter.emitCallSite();
writer.append(")").ws().append("{").softNewLine().indent();
for (Statement part : statement.getConsequent()) { for (Statement part : statement.getConsequent()) {
part.acceptVisitor(this); part.acceptVisitor(this);
} }
@ -675,12 +690,18 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
@Override @Override
public void visit(SwitchStatement statement) { public void visit(SwitchStatement statement) {
try { try {
if (statement.getValue().getLocation() != null) {
pushLocation(statement.getValue().getLocation());
}
if (statement.getId() != null) { if (statement.getId() != null) {
writer.append(statement.getId()).append(": "); writer.append(statement.getId()).append(": ");
} }
prevCallSite = debugEmitter.emitCallSite(); prevCallSite = debugEmitter.emitCallSite();
writer.append("switch").ws().append("("); writer.append("switch").ws().append("(");
statement.getValue().acceptVisitor(this); statement.getValue().acceptVisitor(this);
if (statement.getValue().getLocation() != null) {
popLocation();
}
debugEmitter.emitCallSite(); debugEmitter.emitCallSite();
writer.append(")").ws().append("{").softNewLine().indent(); writer.append(")").ws().append("{").softNewLine().indent();
for (SwitchClause clause : statement.getClauses()) { for (SwitchClause clause : statement.getClauses()) {
@ -709,6 +730,9 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
@Override @Override
public void visit(WhileStatement statement) { public void visit(WhileStatement statement) {
try { try {
if (statement.getCondition() != null && statement.getCondition().getLocation() != null) {
pushLocation(statement.getCondition().getLocation());
}
if (statement.getId() != null) { if (statement.getId() != null) {
writer.append(statement.getId()).append(":").ws(); writer.append(statement.getId()).append(":").ws();
} }
@ -717,6 +741,9 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
prevCallSite = debugEmitter.emitCallSite(); prevCallSite = debugEmitter.emitCallSite();
statement.getCondition().acceptVisitor(this); statement.getCondition().acceptVisitor(this);
debugEmitter.emitCallSite(); debugEmitter.emitCallSite();
if (statement.getCondition().getLocation() != null) {
popLocation();
}
} else { } else {
writer.append("true"); writer.append("true");
} }
@ -997,10 +1024,10 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
@Override @Override
public void visit(UnaryExpr expr) { public void visit(UnaryExpr expr) {
try {
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
pushLocation(expr.getLocation()); pushLocation(expr.getLocation());
} }
try {
switch (expr.getOperation()) { switch (expr.getOperation()) {
case NOT: case NOT:
writer.append("(!"); writer.append("(!");
@ -1061,12 +1088,12 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.append(')'); writer.append(')');
break; break;
} }
} catch (IOException e) {
throw new RenderingException("IO error occured", e);
}
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
popLocation(); popLocation();
} }
} catch (IOException e) {
throw new RenderingException("IO error occured", e);
}
} }
@Override @Override
@ -1395,10 +1422,10 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
@Override @Override
public void visit(NewArrayExpr expr) { public void visit(NewArrayExpr expr) {
try {
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
pushLocation(expr.getLocation()); pushLocation(expr.getLocation());
} }
try {
ValueType type = expr.getType(); ValueType type = expr.getType();
if (type instanceof ValueType.Primitive) { if (type instanceof ValueType.Primitive) {
switch (((ValueType.Primitive)type).getKind()) { switch (((ValueType.Primitive)type).getKind()) {
@ -1448,20 +1475,20 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
expr.getLength().acceptVisitor(this); expr.getLength().acceptVisitor(this);
writer.append(")"); writer.append(")");
} }
} catch (IOException e) {
throw new RenderingException("IO error occured", e);
}
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
popLocation(); popLocation();
} }
} catch (IOException e) {
throw new RenderingException("IO error occured", e);
}
} }
@Override @Override
public void visit(NewMultiArrayExpr expr) { public void visit(NewMultiArrayExpr expr) {
try {
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
pushLocation(expr.getLocation()); pushLocation(expr.getLocation());
} }
try {
ValueType type = expr.getType(); ValueType type = expr.getType();
for (int i = 0; i < expr.getDimensions().size(); ++i) { for (int i = 0; i < expr.getDimensions().size(); ++i) {
type = ((ValueType.Array)type).getItemType(); type = ((ValueType.Array)type).getItemType();
@ -1509,20 +1536,20 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
dimension.acceptVisitor(this); dimension.acceptVisitor(this);
} }
writer.append("])"); writer.append("])");
} catch (IOException e) {
throw new RenderingException("IO error occured", e);
}
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
popLocation(); popLocation();
} }
} catch (IOException e) {
throw new RenderingException("IO error occured", e);
}
} }
@Override @Override
public void visit(InstanceOfExpr expr) { public void visit(InstanceOfExpr expr) {
try {
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
pushLocation(expr.getLocation()); pushLocation(expr.getLocation());
} }
try {
if (expr.getType() instanceof ValueType.Object) { if (expr.getType() instanceof ValueType.Object) {
String clsName = ((ValueType.Object)expr.getType()).getClassName(); String clsName = ((ValueType.Object)expr.getType()).getClassName();
ClassHolder cls = classSource.get(clsName); ClassHolder cls = classSource.get(clsName);
@ -1539,27 +1566,27 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
writer.append("$rt_isInstance("); writer.append("$rt_isInstance(");
expr.getExpr().acceptVisitor(this); expr.getExpr().acceptVisitor(this);
writer.append(",").ws().append(typeToClsString(naming, expr.getType())).append(")"); writer.append(",").ws().append(typeToClsString(naming, expr.getType())).append(")");
} catch (IOException e) {
throw new RenderingException("IO error occured", e);
}
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
popLocation(); popLocation();
} }
} catch (IOException e) {
throw new RenderingException("IO error occured", e);
}
} }
@Override @Override
public void visit(StaticClassExpr expr) { public void visit(StaticClassExpr expr) {
try {
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
pushLocation(expr.getLocation()); pushLocation(expr.getLocation());
} }
try {
writer.append(typeToClsString(naming, expr.getType())); writer.append(typeToClsString(naming, expr.getType()));
} catch (IOException e) {
throw new RenderingException("IO error occured", e);
}
if (expr.getLocation() != null) { if (expr.getLocation() != null) {
popLocation(); popLocation();
} }
} catch (IOException e) {
throw new RenderingException("IO error occured", e);
}
} }
@Override @Override