-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathtrap.rs
More file actions
282 lines (254 loc) · 7.48 KB
/
trap.rs
File metadata and controls
282 lines (254 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use crate::{config, generated};
use codeql_extractor::{extractor, file_paths, trap};
use ra_ap_ide_db::line_index::LineCol;
use std::fmt::Debug;
use std::hash::Hash;
use std::marker::PhantomData;
use std::path::{Path, PathBuf};
use tracing::debug;
pub use trap::Label as UntypedLabel;
pub use trap::{Compression, Writer};
pub trait AsTrapKeyPart {
fn as_key_part(&self) -> String;
}
impl AsTrapKeyPart for UntypedLabel {
fn as_key_part(&self) -> String {
format!("{{{self}}}")
}
}
impl AsTrapKeyPart for String {
fn as_key_part(&self) -> String {
self.clone()
}
}
impl AsTrapKeyPart for &str {
fn as_key_part(&self) -> String {
String::from(*self)
}
}
pub trait TrapClass {
fn class_name() -> &'static str;
}
pub trait TrapEntry: Debug + Sized + TrapClass {
fn extract_id(&mut self) -> TrapId<Self>;
fn emit(self, id: Label<Self>, out: &mut Writer);
}
#[derive(Debug, Clone)]
pub enum TrapId<T: TrapEntry> {
Star,
Key(String),
Label(Label<T>),
}
impl<T: TrapEntry> From<String> for TrapId<T> {
fn from(value: String) -> Self {
TrapId::Key(value)
}
}
impl<T: TrapEntry> From<&str> for TrapId<T> {
fn from(value: &str) -> Self {
TrapId::Key(value.into())
}
}
impl<T: TrapEntry> From<Label<T>> for TrapId<T> {
fn from(value: Label<T>) -> Self {
Self::Label(value)
}
}
#[macro_export]
macro_rules! trap_key {
($($x:expr),+ $(,)?) => {{
let mut key = String::new();
$(
key.push_str(&$x.as_key_part());
)*
trap::TrapId::Key(key)
}};
}
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct Label<T: TrapClass> {
untyped: UntypedLabel,
phantom: PhantomData<T>, // otherwise Rust wants `T` to be used
}
// not deriving `Clone` and `Copy` because they require `T: Clone` and `T: Copy` respectively,
// even if `T` is not actually part of the fields.
// see https://github.com/rust-lang/rust/issues/108894
impl<T: TrapClass> Clone for Label<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T: TrapClass> Copy for Label<T> {}
impl<T: TrapClass> Label<T> {
pub fn as_untyped(&self) -> UntypedLabel {
self.untyped
}
/// # Safety
/// The user must make sure the label respects TRAP typing
pub unsafe fn from_untyped(untyped: UntypedLabel) -> Self {
Self {
untyped,
phantom: PhantomData,
}
}
}
impl<T: TrapClass> AsTrapKeyPart for Label<T> {
fn as_key_part(&self) -> String {
self.as_untyped().as_key_part()
}
}
impl<T: TrapClass> From<Label<T>> for trap::Arg {
fn from(value: Label<T>) -> Self {
trap::Arg::Label(value.as_untyped())
}
}
pub struct TrapFile {
pub path: PathBuf,
pub writer: Writer,
compression: Compression,
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum DiagnosticSeverity {
Debug = 10,
Info = 20,
Warning = 30,
Error = 40,
}
impl TrapFile {
pub fn emit_location_label(
&mut self,
file_label: Label<generated::File>,
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;
extractor::location_label(
&mut self.writer,
trap::Location {
file_label: file_label.as_untyped(),
start_line,
start_column,
end_line,
end_column,
},
)
}
pub fn emit_location<E: TrapClass>(
&mut self,
file_label: Label<generated::File>,
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_file_only_location<E: TrapClass>(
&mut self,
file_label: Label<generated::File>,
entity_label: Label<E>,
) {
let location_label = extractor::location_label(
&mut self.writer,
trap::Location {
file_label: file_label.as_untyped(),
start_line: 0,
start_column: 0,
end_line: 0,
end_column: 0,
},
);
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) -> Label<generated::File> {
let untyped = extractor::populate_file(&mut self.writer, absolute_path, None);
// SAFETY: populate_file emits `@file` typed labels
unsafe { Label::from_untyped(untyped) }
}
pub fn label<T: TrapEntry>(&mut self, id: TrapId<T>) -> Label<T> {
match id {
TrapId::Star => {
let untyped = self.writer.fresh_id();
// SAFETY: a `*` trap id is always safe for typing
unsafe { Label::from_untyped(untyped) }
}
TrapId::Key(s) => {
let untyped = self
.writer
.global_id(&format!("{},{}", T::class_name(), s))
.0;
// SAFETY: using type names as prefixes avoids labels having a conflicting type
unsafe { Label::from_untyped(untyped) }
}
TrapId::Label(l) => l,
}
}
pub fn emit<T: TrapEntry>(&mut self, mut e: T) -> Label<T> {
let label = self.label(e.extract_id());
e.emit(label, &mut self.writer);
label
}
pub fn commit(&self) -> std::io::Result<()> {
std::fs::create_dir_all(self.path.parent().unwrap())?;
self.writer.write_to_file(&self.path, self.compression)
}
}
pub struct TrapFileProvider {
trap_dir: PathBuf,
compression: Compression,
}
impl TrapFileProvider {
pub fn new(cfg: &config::Config) -> std::io::Result<TrapFileProvider> {
let trap_dir = cfg.trap_dir.clone();
std::fs::create_dir_all(&trap_dir)?;
Ok(TrapFileProvider {
trap_dir,
compression: cfg.trap_compression.into(),
})
}
pub fn create(&self, category: &str, key: impl AsRef<Path>) -> TrapFile {
let path = file_paths::path_for(
&self.trap_dir.join(category),
key.as_ref(),
self.compression.extension(),
None,
);
debug!("creating trap file {}", path.display());
let mut writer = trap::Writer::new();
extractor::populate_empty_location(&mut writer);
TrapFile {
path,
writer,
compression: self.compression,
}
}
}