Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ private static boolean isIntentionalThreadAbortDivByZero(ImOperatorCall call) {
private static ImFunctionCall callWithStacktrace(de.peeeq.wurstscript.ast.Element trace, ImFunction f, ImExprs args) {
int stacktraceIndex = stacktraceParamIndex(f);
if (stacktraceIndex >= 0) {
args.add(stacktraceIndex, JassIm.ImStringVal("when calling " + f.getName()
args.add(stacktraceIndex, JassIm.ImStringVal(f.getName()
+ StackTraceInjector2.getCallPos(trace.attrErrorPos())));
}
return JassIm.ImFunctionCall(trace, f, JassIm.ImTypeArguments(), args, false, CallType.NORMAL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private void replaceVars(Element e, Map<ImVar, GetSetPair> oldToNewVar) {
args.add(set.getRight().copy());

if (generateStacktraces) {
args.add(JassIm.ImStringVal("when writing array " + va.getVar().getName() + StackTraceInjector2.getCallPos(va.getTrace().attrSource())));
args.add(JassIm.ImStringVal("write " + va.getVar().getName() + StackTraceInjector2.getCallPos(va.getTrace().attrSource())));
}

replacer.replace(set,
Expand All @@ -154,7 +154,7 @@ private void replaceVars(Element e, Map<ImVar, GetSetPair> oldToNewVar) {
args.add(val.copy());
}
if (generateStacktraces) {
args.add(JassIm.ImStringVal("when reading array " + am.getVar().getName() + " in " + StackTraceInjector2.getCallPos(am.getTrace().attrSource())));
args.add(JassIm.ImStringVal("read " + am.getVar().getName() + StackTraceInjector2.getCallPos(am.getTrace().attrSource())));
}
if (getSetMap.containsKey(am.getVar())) {
replacer.replace(am,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public void visit(ImMethodCall call) {
super.visit(call);
if (affectedFuncs.contains(call.getMethod().getImplementation())) {
String callPos = getCallPos(call.attrTrace().attrErrorPos());
call.getArguments().add(getStacktraceIndex(call), str("when calling " + name(call.getMethod()) + callPos));
call.getArguments().add(getStacktraceIndex(call), str(name(call.getMethod()) + callPos));
}
}
});
Expand Down Expand Up @@ -400,7 +400,7 @@ private void passStacktraceParams(final Multimap<ImFunction, ImFunctionCall> cal
// pass the stacktrace parameter at all calls
for (ImFunctionCall call : callsForF) {
String callPos = getCallPos(call.attrTrace().attrErrorPos());
call.getArguments().add(getStacktraceIndex(call), str("when calling " + name(f) + callPos));
call.getArguments().add(getStacktraceIndex(call), str(name(f) + callPos));
}
}
}
Expand Down Expand Up @@ -464,13 +464,17 @@ private String name(JassImElementWithName f) {
}

public static String getCallPos(WPos source) {
String callPos;
if (source.getFile().startsWith("<")) {
callPos = "";
} else {
callPos = " in " + source.printShort();
return "";
}
// Source paths can use either separator, independently of the compiler's host OS.
String file = source.getFile().replace('\\', '/');
file = file.substring(file.lastIndexOf('/') + 1);
int extension = file.lastIndexOf('.');
if (extension > 0) {
file = file.substring(0, extension);
}
return callPos;
return " (" + file + ":L" + source.getLine() + ")";
}

private ImStmt increment(de.peeeq.wurstscript.ast.Element trace, ImVar v) {
Expand Down Expand Up @@ -511,7 +515,7 @@ private void rewriteFuncRefs(final List<ImFuncRefOrCall> funcRefs, Set<ImFunctio
msg += " in function " + frEnclosingFunc.getName();
}
} else {
msg += " " +frTrace.attrSource().printShort();
msg += getCallPos(frTrace.attrSource());
}
ImExpr str = str(msg);
ImExprs args = JassIm.ImExprs(str);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,8 @@ public void stacktraceStringsNotInjectedIntoNumericComparisons() {
" let b = new BuildScanIterator",
" b.tryRect(new MyRect(0., 0., 256., 256.), 1, 1)"
);
assertContainsRegex(compiled, "\"when calling contains");
assertDoesNotContainRegex(compiled, "\"when calling contains[^\"]*\"\\s*[<>]=?");
assertContainsRegex(compiled, "\"contains");
assertDoesNotContainRegex(compiled, "\"contains[^\"]*\"\\s*[<>]=?");
assertDoesNotContainRegex(compiled, "(?s)if not\\((\\w+)\\) then.*?stepsAlongY\\([^\\n]*,\\s*\\1\\s*,");
}

Expand Down Expand Up @@ -2005,10 +2005,10 @@ public void stacktracesAreNotInjectedIntoConfigSeededLuaFunctions() {
assertDoesNotContainRegex(compiled, "function\\s+config\\([^\\)]*__wurst_stackPos");
assertDoesNotContainRegex(compiled, "function\\s+config1\\([^\\)]*__wurst_stackPos");
assertDoesNotContainRegex(compiled, "function\\s+configHelper\\([^\\)]*__wurst_stackPos");
assertDoesNotContainRegex(compiled, "configHelper\\(\"when calling configHelper");
assertDoesNotContainRegex(compiled, "configHelper\\(\"configHelper");

assertContainsRegex(compiled, "function\\s+runtimeHelper\\([^\\)]*__wurst_stackPos");
assertContainsRegex(compiled, "runtimeHelper\\(\"when calling runtimeHelper");
assertContainsRegex(compiled, "runtimeHelper\\(\"runtimeHelper");
}

@Test
Expand All @@ -2031,12 +2031,12 @@ public void stacktracesStayInjectedForLuaHelpersSharedByConfigAndRuntime() {
);

assertDoesNotContainRegex(compiled, "function\\s+configOnlyHelper\\([^\\)]*__wurst_stackPos");
assertDoesNotContainRegex(compiled, "configOnlyHelper\\(\"when calling configOnlyHelper");
assertDoesNotContainRegex(compiled, "configOnlyHelper\\(\"configOnlyHelper");

assertContainsRegex(compiled, "function\\s+sharedHelper\\([^\\)]*__wurst_stackPos");
assertContainsRegex(compiled, "function\\s+sharedLeaf\\([^\\)]*__wurst_stackPos");
assertContainsRegex(compiled, "sharedHelper\\(\"when calling sharedHelper");
assertContainsRegex(compiled, "sharedLeaf\\(\"when calling sharedLeaf");
assertContainsRegex(compiled, "sharedHelper\\(\"sharedHelper");
assertContainsRegex(compiled, "sharedLeaf\\(\"sharedLeaf");
}

@Test
Expand Down Expand Up @@ -2723,8 +2723,8 @@ public void aLoweredConcatenationKeepsItsPositionInAStackTrace() throws IOExcept
Charsets.UTF_8);

assertTrue("the concatenation should go through the helper",
compiled.contains("\"when calling __wurst_stringConcat in aLoweredConcatenationKeepsItsPositionInAStackTrace, line 3\""));
assertFalse("no helper call may lose its position", compiled.contains("\"when calling __wurst_stringConcat\""));
compiled.contains("\"__wurst_stringConcat (aLoweredConcatenationKeepsItsPositionInAStackTrace:L3)\""));
assertFalse("no helper call may lose its position", compiled.contains("\"__wurst_stringConcat\""));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tests.wurstscript.tests;

import de.peeeq.wurstscript.parser.WPos;
import de.peeeq.wurstscript.translation.imtranslation.StackTraceInjector2;
import org.testng.annotations.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

public class StackTraceTests extends WurstScriptTest {
@Test
public void compactSourcePositions() {
for (String file : new String[]{"UnitIndexer.wurst", "/project/UnitIndexer.wurst",
"C:\\project\\UnitIndexer.wurst"}) {
assertEquals(StackTraceInjector2.getCallPos(new WPos(file, null, 0, 0)),
" (UnitIndexer:L0)");
}
assertEquals(StackTraceInjector2.getCallPos(new WPos("<generated>", null, 0, 0)), "");
}

@Test
public void compactFramesOnBothBackends() throws IOException {
test().testLua(true).luaOnly(false).stacktraces().compilationUnits(
compilationUnit("Trace.wurst",
"package Trace",
"native println(string s)",
"function getStackTraceString() returns string",
" return \"\"",
"class Probe",
" @noinline function snapshot() returns string",
" return getStackTraceString()",
"@noinline function caller(Probe p)",
" println(p.snapshot())",
"init",
" caller(new Probe)"));
for (String file : new String[]{"StackTraceTests_compactFramesOnBothBackends_stacktraceinlopt.j",
"lua/StackTraceTests_compactFramesOnBothBackends.lua"}) {
String output = Files.readString(Path.of(TEST_OUTPUT_PATH, file), StandardCharsets.UTF_8);
assertTrue(output.contains("snapshot (Trace:L9)"), file);
assertTrue(output.contains("caller (Trace:L11)"), file);
assertFalse(output.contains("\"when calling "), file);
}
}
}
Loading