Skip to content

Commit daf2bee

Browse files
committed
Rust: attach empty locations to types in the crate graph
1 parent 9337f4f commit daf2bee

File tree

1 file changed

+52
-12
lines changed

1 file changed

+52
-12
lines changed

rust/extractor/src/main.rs

+52-12
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ struct Extractor<'a> {
4040
steps: Vec<ExtractionStep>,
4141
}
4242

43+
fn emit_empty_location<E: trap::TrapClass>(trap: &mut TrapFile, entity_label: trap::Label<E>) {
44+
let (file_label, fresh) = trap.writer.global_id("empty;sourcefile");
45+
if fresh {
46+
panic!("empty file not populated");
47+
}
48+
let (location_label, fresh) = trap
49+
.writer
50+
.location_label(codeql_extractor::trap::Location {
51+
file_label,
52+
start_line: 0,
53+
start_column: 0,
54+
end_line: 0,
55+
end_column: 0,
56+
});
57+
if fresh {
58+
panic!("empty location not populated");
59+
}
60+
trap.writer.add_tuple(
61+
"locatable_locations",
62+
vec![entity_label.into(), location_label.into()],
63+
);
64+
}
4365
fn emit_hir_path(
4466
trap: &mut TrapFile,
4567
path: &ra_ap_hir_def::path::Path,
@@ -49,7 +71,9 @@ fn emit_hir_path(
4971
id: trap::TrapId::Star,
5072
text: Some(segment.name.as_str().to_owned()),
5173
});
52-
trap.emit(generated::PathSegment {
74+
emit_empty_location(trap, name_ref);
75+
76+
let label = trap.emit(generated::PathSegment {
5377
id: trap::TrapId::Star,
5478
generic_arg_list: None,
5579
name_ref: Some(name_ref),
@@ -58,17 +82,21 @@ fn emit_hir_path(
5882
ret_type: None,
5983
return_type_syntax: None,
6084
type_repr: None,
61-
})
85+
});
86+
emit_empty_location(trap, label);
87+
label
6288
});
6389
let qualifier = path
6490
.mod_path()
6591
.filter(|p| !p.segments().is_empty())
6692
.and_then(|_| path.qualifier().as_ref().map(|p| emit_hir_path(trap, p)));
67-
trap.emit(generated::Path {
93+
let label = trap.emit(generated::Path {
6894
id: trap::TrapId::Star,
6995
qualifier,
7096
part,
71-
})
97+
});
98+
emit_empty_location(trap, label);
99+
label
72100
}
73101
fn emit_hir_fn(
74102
trap: &mut TrapFile,
@@ -85,46 +113,55 @@ fn emit_hir_fn(
85113
id: trap::TrapId::Star,
86114
type_repr: ret,
87115
});
116+
emit_empty_location(trap, ret);
117+
88118
let self_param = self_type.map(|ty| {
89119
let type_repr = emit_hir_typeref(trap, ty);
90-
trap.emit(generated::SelfParam {
120+
let label = trap.emit(generated::SelfParam {
91121
id: trap::TrapId::Star,
92122
attrs: vec![],
93123
type_repr,
94124
is_mut: false,
95125
lifetime: None,
96126
name: None,
97-
})
127+
});
128+
emit_empty_location(trap, label);
129+
label
98130
});
99131
let params = params
100132
.iter()
101133
.map(|t| {
102134
let type_repr = emit_hir_typeref(trap, t);
103-
trap.emit(generated::Param {
135+
let label = trap.emit(generated::Param {
104136
id: trap::TrapId::Star,
105137
attrs: vec![],
106138
type_repr,
107139
pat: None,
108-
})
140+
});
141+
emit_empty_location(trap, label);
142+
label
109143
})
110144
.collect();
111145
let params = trap.emit(generated::ParamList {
112146
id: trap::TrapId::Star,
113147
params,
114148
self_param,
115149
});
116-
trap.emit(generated::FnPtrTypeRepr {
150+
emit_empty_location(trap, params);
151+
let label = trap.emit(generated::FnPtrTypeRepr {
117152
id: trap::TrapId::Star,
118153
abi: None,
119154
is_async,
120155
is_const,
121156
is_unsafe,
122157
param_list: Some(params),
123158
ret_type: Some(ret),
124-
})
159+
});
160+
emit_empty_location(trap, label);
161+
label
125162
}
126163
fn emit_hir_typeref(trap: &mut TrapFile, ty: &TypeRef) -> Option<trap::Label<generated::TypeRepr>> {
127-
match ty {
164+
let maybe_label = match ty {
128165
TypeRef::Never => Some(
129166
trap.emit(generated::NeverTypeRepr {
130167
id: trap::TrapId::Star,
@@ -234,7 +271,10 @@ fn emit_hir_typeref(trap: &mut TrapFile, ty: &TypeRef) -> Option<trap::Label<gen
234271
TypeRef::DynTrait(_) => None, // TODO handle dyn
235272
TypeRef::Macro(_) => None,
236273
TypeRef::Error => None,
237-
}
274+
};
275+
maybe_label.inspect(|&label| {
276+
emit_empty_location(trap, label);
277+
})
238278
}
239279

240280
fn emit_variant_data(

0 commit comments

Comments
 (0)