Skip to content

Fix columns being off by one / implement line cache #1174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
38 changes: 38 additions & 0 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,44 @@ export class Source extends Node {
var kind = this.sourceKind;
return kind == SourceKind.LIBRARY || kind == SourceKind.LIBRARY_ENTRY;
}

/** Cached line starts. */
private lineCache: i32[] | null = null;

/** Rememberd column number. */
private lineColumn: i32 = 0;

/** Determines the line number at the specified position. */
lineAt(pos: i32): i32 {
assert(pos >= 0 && pos < 0x7fffffff);
var lineCache = this.lineCache;
if (!lineCache) {
this.lineCache = lineCache = [0];
let text = this.text;
for (let i = 0, k = text.length; i < k;) {
if (text.charCodeAt(i++) == CharCode.LINEFEED) lineCache.push(i);
}
lineCache.push(0x7fffffff);
}
var l = 0;
var r = lineCache.length - 1;
while (l < r) {
let m = l + i32((r - l) / 2);
let s = unchecked(lineCache[m]);
if (pos < s) r = m;
else if (pos < unchecked(lineCache[m + 1])) {
this.lineColumn = pos - s + 1;
return m + 1;
}
else l = m + 1;
}
return assert(0);
}

/** Gets the column number at the last position queried with `lineAt`. */
columnAt(): i32 {
return this.lineColumn;
}
}

/** Base class of all declaration statements. */
Expand Down
6 changes: 4 additions & 2 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10091,13 +10091,15 @@ export class Compiler extends DiagnosticEmitter {
}

var filenameArg = this.ensureStaticString(codeLocation.range.source.normalizedPath);
var range = codeLocation.range;
var source = range.source;
return module.block(null, [
module.call(
abortInstance.internalName, [
messageArg,
filenameArg,
module.i32(codeLocation.range.line),
module.i32(codeLocation.range.column)
module.i32(source.lineAt(range.start)),
module.i32(source.columnAt())
],
NativeType.None
),
Expand Down
21 changes: 12 additions & 9 deletions src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,19 @@ export class DiagnosticMessage {
toString(): string {
var range = this.range;
if (range) {
let source = range.source;
return (
diagnosticCategoryToString(this.category) +
" " +
this.code.toString() +
": \"" +
this.message +
"\" in " +
range.source.normalizedPath +
source.normalizedPath +
":" +
range.line.toString() +
source.lineAt(range.start).toString() +
":" +
range.column.toString()
source.columnAt().toString()
);
}
return (
Expand Down Expand Up @@ -172,6 +173,7 @@ export function formatDiagnosticMessage(
// include range information if available
var range = message.range;
if (range) {
let source = range.source;

// include context information if requested
if (showContext) {
Expand All @@ -180,26 +182,27 @@ export function formatDiagnosticMessage(
}
sb.push("\n");
sb.push(" in ");
sb.push(range.source.normalizedPath);
sb.push(source.normalizedPath);
sb.push("(");
sb.push(range.line.toString());
sb.push(source.lineAt(range.start).toString());
sb.push(",");
sb.push(range.column.toString());
sb.push(source.columnAt().toString());
sb.push(")");

let relatedRange = message.relatedRange;
if (relatedRange) {
let relatedSource = relatedRange.source;
if (showContext) {
sb.push("\n");
sb.push(formatDiagnosticContext(relatedRange, useColors));
}
sb.push("\n");
sb.push(" in ");
sb.push(relatedRange.source.normalizedPath);
sb.push(relatedSource.normalizedPath);
sb.push("(");
sb.push(relatedRange.line.toString());
sb.push(relatedSource.lineAt(relatedRange.start).toString());
sb.push(",");
sb.push(relatedRange.column.toString());
sb.push(relatedSource.columnAt().toString());
sb.push(")");
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3106,13 +3106,14 @@ export class Function extends TypedElement {
if (this.program.options.sourceMap) {
let debugLocations = this.debugLocations;
for (let i = 0, k = debugLocations.length; i < k; ++i) {
let debugLocation = debugLocations[i];
let range = debugLocations[i];
let source = range.source;
module.setDebugLocation(
ref,
debugLocation.debugInfoRef,
debugLocation.source.debugInfoIndex,
debugLocation.line,
debugLocation.column
range.debugInfoRef,
source.debugInfoIndex,
source.lineAt(range.start),
source.columnAt()
);
}
}
Expand Down
26 changes: 1 addition & 25 deletions src/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,7 @@ export class Range {
source: Source;
start: i32;
end: i32;

// TODO: set these while tokenizing
// line: i32;
// column: i32;
debugInfoRef: usize = 0;

constructor(source: Source, start: i32, end: i32) {
this.source = source;
Expand All @@ -429,30 +426,9 @@ export class Range {
return new Range(this.source, this.end, this.end);
}

get line(): i32 {
var text = this.source.text;
var line = 1;
for (let pos = this.start; pos >= 0; --pos) {
if (text.charCodeAt(pos) == CharCode.LINEFEED) line++;
}
return line;
}

get column(): i32 {
var text = this.source.text;
var column = 0;
for (let pos = this.start - 1; pos >= 0; --pos) {
if (text.charCodeAt(pos) == CharCode.LINEFEED) break;
++column;
}
return column;
}

toString(): string {
return this.source.text.substring(this.start, this.end);
}

debugInfoRef: usize = 0;
}

/** Handler for intercepting comments while tokenizing. */
Expand Down
16 changes: 8 additions & 8 deletions tests/compiler/abi.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
i32.const 0
i32.const 32
i32.const 32
i32.const 2
i32.const 3
call $~lib/builtins/abort
unreachable
end
Expand Down Expand Up @@ -74,7 +74,7 @@
i32.const 0
i32.const 32
i32.const 45
i32.const 2
i32.const 3
call $~lib/builtins/abort
unreachable
end
Expand Down Expand Up @@ -103,7 +103,7 @@
i32.const 0
i32.const 32
i32.const 58
i32.const 2
i32.const 3
call $~lib/builtins/abort
unreachable
end
Expand All @@ -120,7 +120,7 @@
i32.const 0
i32.const 32
i32.const 65
i32.const 2
i32.const 3
call $~lib/builtins/abort
unreachable
end
Expand All @@ -135,7 +135,7 @@
i32.const 0
i32.const 32
i32.const 72
i32.const 2
i32.const 3
call $~lib/builtins/abort
unreachable
end
Expand All @@ -150,7 +150,7 @@
i32.const 0
i32.const 32
i32.const 74
i32.const 2
i32.const 3
call $~lib/builtins/abort
unreachable
end
Expand All @@ -163,7 +163,7 @@
i32.const 0
i32.const 32
i32.const 77
i32.const 2
i32.const 3
call $~lib/builtins/abort
unreachable
end
Expand All @@ -176,7 +176,7 @@
i32.const 0
i32.const 32
i32.const 79
i32.const 2
i32.const 3
call $~lib/builtins/abort
unreachable
end
Expand Down
26 changes: 13 additions & 13 deletions tests/compiler/assert-nonnull.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
i32.const 0
i32.const 1040
i32.const 2
i32.const 9
i32.const 10
call $~lib/builtins/abort
unreachable
end
Expand All @@ -45,7 +45,7 @@
i32.const 0
i32.const 1040
i32.const 11
i32.const 9
i32.const 10
call $~lib/builtins/abort
unreachable
end
Expand All @@ -61,7 +61,7 @@
i32.const 0
i32.const 1040
i32.const 15
i32.const 9
i32.const 10
call $~lib/builtins/abort
unreachable
end
Expand All @@ -74,7 +74,7 @@
i32.const 0
i32.const 1040
i32.const 19
i32.const 9
i32.const 10
call $~lib/builtins/abort
unreachable
end
Expand All @@ -86,7 +86,7 @@
i32.const 1104
i32.const 1168
i32.const 104
i32.const 41
i32.const 42
call $~lib/builtins/abort
unreachable
end
Expand All @@ -99,7 +99,7 @@
i32.const 1216
i32.const 1168
i32.const 108
i32.const 39
i32.const 40
call $~lib/builtins/abort
unreachable
end
Expand All @@ -114,7 +114,7 @@
i32.const 1104
i32.const 1168
i32.const 104
i32.const 41
i32.const 42
call $~lib/builtins/abort
unreachable
end
Expand All @@ -131,7 +131,7 @@
i32.const 0
i32.const 1040
i32.const 23
i32.const 9
i32.const 10
call $~lib/builtins/abort
unreachable
end
Expand Down Expand Up @@ -159,7 +159,7 @@
i32.const 0
i32.const 1040
i32.const 27
i32.const 9
i32.const 10
call $~lib/builtins/abort
unreachable
)
Expand All @@ -185,7 +185,7 @@
i32.const 0
i32.const 1040
i32.const 31
i32.const 9
i32.const 10
call $~lib/builtins/abort
unreachable
)
Expand All @@ -206,7 +206,7 @@
i32.const 0
i32.const 1040
i32.const 39
i32.const 12
i32.const 13
call $~lib/builtins/abort
unreachable
end
Expand All @@ -228,7 +228,7 @@
i32.const 0
i32.const 1040
i32.const 44
i32.const 9
i32.const 10
call $~lib/builtins/abort
unreachable
end
Expand All @@ -254,7 +254,7 @@
i32.const 0
i32.const 1040
i32.const 52
i32.const 9
i32.const 10
call $~lib/builtins/abort
unreachable
end
Expand Down
Loading