Skip to content

Commit b04abc0

Browse files
committed
Rust: extract syntax errors
1 parent 4dbb15d commit b04abc0

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

rust/extractor/src/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@ fn extract(
2727

2828
let parse = ra_ap_syntax::ast::SourceFile::parse(&input, Edition::CURRENT);
2929
for err in parse.errors() {
30-
let (start, _) = translator.location(err.range());
31-
log::warn!("{}:{}:{}: {}", display_path, start.line, start.col, err);
30+
translator.emit_parse_error(display_path.as_ref(), err);
3231
}
3332
if let Some(ast) = SourceFile::cast(parse.syntax_node()) {
3433
translator.emit_source_file(ast);
35-
translator.trap.commit()?
3634
} else {
3735
log::warn!("Skipped {}", display_path);
3836
}
37+
translator.trap.commit()?;
3938
Ok(())
4039
}
4140
fn main() -> anyhow::Result<()> {

rust/extractor/src/translate/base.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use crate::trap::TrapFile;
1+
use crate::trap::{DiagnosticSeverity, TrapFile};
22
use crate::trap::{Label, TrapClass};
33
use codeql_extractor::trap::{self};
44
use ra_ap_ide_db::line_index::{LineCol, LineIndex};
55
use ra_ap_syntax::ast::RangeItem;
6-
use ra_ap_syntax::TextSize;
7-
use ra_ap_syntax::{ast, TextRange};
6+
use ra_ap_syntax::{ast, SyntaxError, TextRange, TextSize};
87
pub trait TextValue {
98
fn try_get_text(&self) -> Option<String>;
109
}
@@ -83,4 +82,17 @@ impl Translator {
8382
let (start, end) = self.location(node.syntax().text_range());
8483
self.trap.emit_location(self.label, label, start, end)
8584
}
85+
pub fn emit_parse_error(&mut self, path: &str, err: SyntaxError) {
86+
let (start, end) = self.location(err.range());
87+
log::warn!("{}:{}:{}: {}", path, start.line, start.col, err);
88+
let message = err.to_string();
89+
let location = self.trap.emit_location_label(self.label, start, end);
90+
self.trap.emit_diagnostic(
91+
DiagnosticSeverity::Warning,
92+
"parse_error".to_owned(),
93+
message.clone(),
94+
message,
95+
location,
96+
);
97+
}
8698
}

rust/extractor/src/trap.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,25 @@ pub struct TrapFile {
128128
compression: Compression,
129129
}
130130

131+
#[derive(Copy, Clone)]
132+
pub enum DiagnosticSeverity {
133+
Debug = 10,
134+
Info = 20,
135+
Warning = 30,
136+
Error = 40,
137+
}
131138
impl TrapFile {
132-
pub fn emit_location<E: TrapClass>(
139+
pub fn emit_location_label(
133140
&mut self,
134141
file_label: UntypedLabel,
135-
entity_label: Label<E>,
136142
start: LineCol,
137143
end: LineCol,
138-
) {
144+
) -> UntypedLabel {
139145
let start_line = 1 + start.line as usize;
140146
let start_column = 1 + start.col as usize;
141147
let end_line = 1 + end.line as usize;
142148
let end_column = 1 + end.col as usize;
143-
let location_label = extractor::location_label(
149+
extractor::location_label(
144150
&mut self.writer,
145151
trap::Location {
146152
file_label,
@@ -149,13 +155,43 @@ impl TrapFile {
149155
end_line,
150156
end_column,
151157
},
152-
);
158+
)
159+
}
160+
pub fn emit_location<E: TrapClass>(
161+
&mut self,
162+
file_label: UntypedLabel,
163+
entity_label: Label<E>,
164+
start: LineCol,
165+
end: LineCol,
166+
) {
167+
let location_label = self.emit_location_label(file_label, start, end);
153168
self.writer.add_tuple(
154169
"locatable_locations",
155170
vec![entity_label.into(), location_label.into()],
156171
);
157172
}
158173

174+
pub fn emit_diagnostic(
175+
&mut self,
176+
severity: DiagnosticSeverity,
177+
error_tag: String,
178+
error_message: String,
179+
full_error_message: String,
180+
location: UntypedLabel,
181+
) {
182+
let label = self.writer.fresh_id();
183+
self.writer.add_tuple(
184+
"diagnostics",
185+
vec![
186+
trap::Arg::Label(label),
187+
trap::Arg::Int(severity as usize),
188+
trap::Arg::String(error_tag),
189+
trap::Arg::String(error_message),
190+
trap::Arg::String(full_error_message),
191+
trap::Arg::Label(location),
192+
],
193+
);
194+
}
159195
pub fn emit_file(&mut self, absolute_path: &Path) -> trap::Label {
160196
extractor::populate_file(&mut self.writer, absolute_path)
161197
}

0 commit comments

Comments
 (0)