Rust: extract syntax errors

This commit is contained in:
Arthur Baars
2024-09-23 16:41:34 +02:00
parent 4dbb15ddda
commit b04abc09f0
3 changed files with 58 additions and 11 deletions

View File

@@ -27,15 +27,14 @@ fn extract(
let parse = ra_ap_syntax::ast::SourceFile::parse(&input, Edition::CURRENT);
for err in parse.errors() {
let (start, _) = translator.location(err.range());
log::warn!("{}:{}:{}: {}", display_path, start.line, start.col, err);
translator.emit_parse_error(display_path.as_ref(), err);
}
if let Some(ast) = SourceFile::cast(parse.syntax_node()) {
translator.emit_source_file(ast);
translator.trap.commit()?
} else {
log::warn!("Skipped {}", display_path);
}
translator.trap.commit()?;
Ok(())
}
fn main() -> anyhow::Result<()> {

View File

@@ -1,10 +1,9 @@
use crate::trap::TrapFile;
use crate::trap::{DiagnosticSeverity, TrapFile};
use crate::trap::{Label, TrapClass};
use codeql_extractor::trap::{self};
use ra_ap_ide_db::line_index::{LineCol, LineIndex};
use ra_ap_syntax::ast::RangeItem;
use ra_ap_syntax::TextSize;
use ra_ap_syntax::{ast, TextRange};
use ra_ap_syntax::{ast, SyntaxError, TextRange, TextSize};
pub trait TextValue {
fn try_get_text(&self) -> Option<String>;
}
@@ -83,4 +82,17 @@ impl Translator {
let (start, end) = self.location(node.syntax().text_range());
self.trap.emit_location(self.label, label, start, end)
}
pub fn emit_parse_error(&mut self, path: &str, err: SyntaxError) {
let (start, end) = self.location(err.range());
log::warn!("{}:{}:{}: {}", path, start.line, start.col, err);
let message = err.to_string();
let location = self.trap.emit_location_label(self.label, start, end);
self.trap.emit_diagnostic(
DiagnosticSeverity::Warning,
"parse_error".to_owned(),
message.clone(),
message,
location,
);
}
}

View File

@@ -128,19 +128,25 @@ pub struct TrapFile {
compression: Compression,
}
#[derive(Copy, Clone)]
pub enum DiagnosticSeverity {
Debug = 10,
Info = 20,
Warning = 30,
Error = 40,
}
impl TrapFile {
pub fn emit_location<E: TrapClass>(
pub fn emit_location_label(
&mut self,
file_label: UntypedLabel,
entity_label: Label<E>,
start: LineCol,
end: LineCol,
) {
) -> UntypedLabel {
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 location_label = extractor::location_label(
extractor::location_label(
&mut self.writer,
trap::Location {
file_label,
@@ -149,13 +155,43 @@ impl TrapFile {
end_line,
end_column,
},
);
)
}
pub fn emit_location<E: TrapClass>(
&mut self,
file_label: UntypedLabel,
entity_label: Label<E>,
start: LineCol,
end: LineCol,
) {
let location_label = self.emit_location_label(file_label, start, end);
self.writer.add_tuple(
"locatable_locations",
vec![entity_label.into(), location_label.into()],
);
}
pub fn emit_diagnostic(
&mut self,
severity: DiagnosticSeverity,
error_tag: String,
error_message: String,
full_error_message: String,
location: UntypedLabel,
) {
let label = self.writer.fresh_id();
self.writer.add_tuple(
"diagnostics",
vec![
trap::Arg::Label(label),
trap::Arg::Int(severity as usize),
trap::Arg::String(error_tag),
trap::Arg::String(error_message),
trap::Arg::String(full_error_message),
trap::Arg::Label(location),
],
);
}
pub fn emit_file(&mut self, absolute_path: &Path) -> trap::Label {
extractor::populate_file(&mut self.writer, absolute_path)
}