fix location off-by-ones with regexp parsing

This commit is contained in:
Erik Krogh Kristensen
2021-11-15 13:43:39 +01:00
parent 80919e39a2
commit 2163648b39
6 changed files with 1283 additions and 469 deletions

View File

@@ -589,7 +589,7 @@ public class ASTExtractor {
trapwriter.addTuple("literals", valueString, source, key);
Position start = nd.getLoc().getStart();
com.semmle.util.locations.Position startPos = new com.semmle.util.locations.Position(start.getLine(), start.getColumn(), start.getOffset());
com.semmle.util.locations.Position startPos = new com.semmle.util.locations.Position(start.getLine(), start.getColumn() + 1 /* Convert from 0-based to 1-based. */, start.getOffset());
if (nd.isRegExp()) {
OffsetTranslation offsets = new OffsetTranslation();
@@ -867,7 +867,7 @@ public class ASTExtractor {
}
OffsetTranslation offsets = concatResult.snd();
Position start = nd.getLoc().getStart();
com.semmle.util.locations.Position startPos = new com.semmle.util.locations.Position(start.getLine(), start.getColumn(), start.getOffset());
com.semmle.util.locations.Position startPos = new com.semmle.util.locations.Position(start.getLine(), start.getColumn() + 1 /* Convert from 0-based to 1-based. */, start.getOffset());
SourceMap sourceMap = SourceMap.legacyWithStartPos(SourceMap.fromString(nd.getLoc().getSource()).offsetBy(0, offsets), startPos);
regexpExtractor.extract(foldedString, sourceMap, nd, true);
return;

View File

@@ -121,10 +121,12 @@ public class RegExpExtractor {
}
public void emitLocation(SourceElement term, Label lbl) {
int sl = sourceMap.getStart(term.getLoc().getStart().getColumn()).getLine();
int sc = sourceMap.getStart(term.getLoc().getStart().getColumn()).getColumn() + 1; // convert to 1-based
int el = sourceMap.getEnd(term.getLoc().getEnd().getColumn()).getLine();
int ec = sourceMap.getEnd(term.getLoc().getEnd().getColumn()).getColumn() - 1; // convert to inclusive
int start = term.getLoc().getStart().getColumn();
int sl = sourceMap.getStart(start).getLine();
int sc = sourceMap.getStart(start).getColumn();
int end = term.getLoc().getEnd().getColumn();
int el = sourceMap.getStart(end).getLine();
int ec = sourceMap.getStart(end).getColumn() - 1; // convert to inclusive
locationManager.emitSnippetLocation(lbl, sl, sc, el, ec);
}