Rust: fix locations

In QL locations are 1-based inclusive ranges. The locations is rust are 0-based
and the end position is exclusive.

To patch things up, subtract 1 from the end offset and add 1 to all line and column numbers.
This commit is contained in:
Arthur Baars
2024-09-09 13:54:42 +02:00
parent 86215b4f02
commit 61592a3256
2 changed files with 20 additions and 15 deletions

View File

@@ -11,7 +11,7 @@ use ra_ap_hir_def::hir::{CaptureBy, ExprId, LabelId, MatchArm, PatId, Statement}
use ra_ap_ide_db::line_index::LineIndex;
use ra_ap_ide_db::{label, LineIndexDatabase, RootDatabase};
use ra_ap_syntax::ast::RangeOp;
use ra_ap_syntax::{AstNode, Edition};
use ra_ap_syntax::{AstNode, Edition, TextRange, TextSize};
use ra_ap_vfs::{FileId, Vfs};
use std::collections::HashMap;
use std::fs;
@@ -81,11 +81,9 @@ impl CrateTranslator<'_> {
.file_id()
.map(|f| (f.file_id(), source))
.and_then(|(file_id, source)| self.emit_file(file_id).map(|data| (data, source)))
.and_then(|(data, source)| {
.map(|(data, source)| {
let range = source.value.text_range();
let start = data.line_index.line_col(range.start());
let end = data.line_index.line_col(range.end());
Some(self.trap.emit_location(data.label, start, end))
self.emit_location_textrange(data, range)
})
}
@@ -151,12 +149,19 @@ impl CrateTranslator<'_> {
.and_then(|(file_id, source)| self.emit_file(file_id).map(|data| (data, source)))
.map(|(data, source)| {
let range = source.value.syntax().text_range();
let start = data.line_index.line_col(range.start());
let end = data.line_index.line_col(range.end());
self.trap.emit_location(data.label, start, end)
self.emit_location_textrange(data, range)
})
}
fn emit_location_textrange(&mut self, data: FileData, range: TextRange) -> trap::Label {
let start = data.line_index.line_col(range.start());
let end = data.line_index.line_col(
range
.end()
.checked_sub(TextSize::new(1))
.unwrap_or(range.end()),
);
self.trap.emit_location(data.label, start, end)
}
fn emit_label(
&mut self,
label_id: LabelId,

View File

@@ -2,10 +2,10 @@ use crate::config::Compression;
use crate::generated;
use crate::{config, path};
use codeql_extractor::trap;
use log::{debug};
use log::debug;
use ra_ap_ide_db::line_index::LineCol;
use std::ffi::OsString;
use std::fmt::{Debug};
use std::fmt::Debug;
use std::path::{Path, PathBuf};
//TODO: typed labels
@@ -85,10 +85,10 @@ impl TrapFile {
start: LineCol,
end: LineCol,
) -> trap::Label {
let start_line = start.line as usize;
let start_column = start.col as usize;
let end_line = end.line as usize;
let end_column = end.col as usize;
let start_line = 1 + start.line as usize;
let start_column = 1 + start.col as usize;
let end_line = 1 + end.line as usize;
let end_column = 1 + end.col as usize;
let (ret, _) = self.writer.location_label(trap::Location {
start_line,
start_column,