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
283
284
285
use session::Session;
use middle::ty;
use std::env;
use std::fs::{self, File};
use std::path::{Path, PathBuf};
use syntax::{attr};
use syntax::ast::{self, NodeId, DefId};
use syntax::ast_util;
use syntax::codemap::*;
use syntax::parse::token::{self, get_ident, keywords};
use syntax::visit::{self, Visitor};
use syntax::print::pprust::ty_to_string;
use self::span_utils::SpanUtils;
mod span_utils;
mod recorder;
mod dump_csv;
pub struct SaveContext<'l, 'tcx: 'l> {
sess: &'l Session,
analysis: &'l ty::CrateAnalysis<'tcx>,
span_utils: SpanUtils<'l>,
}
pub struct CrateData {
pub name: String,
pub number: u32,
}
pub enum Data {
FunctionData(FunctionData),
VariableData(VariableData),
}
pub struct FunctionData {
pub id: NodeId,
pub name: String,
pub qualname: String,
pub declaration: Option<DefId>,
pub span: Span,
pub scope: NodeId,
}
pub struct VariableData {
pub id: NodeId,
pub name: String,
pub qualname: String,
pub span: Span,
pub scope: NodeId,
pub value: String,
pub type_value: String,
}
impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
pub fn new(sess: &'l Session,
analysis: &'l ty::CrateAnalysis<'tcx>,
span_utils: SpanUtils<'l>)
-> SaveContext<'l, 'tcx> {
SaveContext {
sess: sess,
analysis: analysis,
span_utils: span_utils,
}
}
pub fn get_external_crates(&self) -> Vec<CrateData> {
let mut result = Vec::new();
self.sess.cstore.iter_crate_data(|n, cmd| {
result.push(CrateData { name: cmd.name.clone(), number: n });
});
result
}
pub fn get_item_data(&self, item: &ast::Item) -> Data {
match item.node {
ast::Item_::ItemFn(..) => {
let name = self.analysis.ty_cx.map.path_to_string(item.id);
let qualname = format!("::{}", name);
let sub_span = self.span_utils.sub_span_after_keyword(item.span, keywords::Fn);
Data::FunctionData(FunctionData {
id: item.id,
name: name,
qualname: qualname,
declaration: None,
span: sub_span.unwrap(),
scope: self.analysis.ty_cx.map.get_parent(item.id),
})
}
ast::ItemStatic(ref typ, mt, ref expr) => {
let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
let (value, keyword) = match mt {
ast::MutMutable => (String::from_str("<mutable>"), keywords::Mut),
ast::MutImmutable => (self.span_utils.snippet(expr.span), keywords::Static),
};
let sub_span = self.span_utils.sub_span_after_keyword(item.span, keyword);
Data::VariableData(VariableData {
id: item.id,
name: get_ident(item.ident).to_string(),
qualname: qualname,
span: sub_span.unwrap(),
scope: self.analysis.ty_cx.map.get_parent(item.id),
value: value,
type_value: ty_to_string(&typ),
})
}
ast::ItemConst(ref typ, ref expr) => {
let qualname = format!("::{}", self.analysis.ty_cx.map.path_to_string(item.id));
let sub_span = self.span_utils.sub_span_after_keyword(item.span, keywords::Const);
Data::VariableData(VariableData {
id: item.id,
name: get_ident(item.ident).to_string(),
qualname: qualname,
span: sub_span.unwrap(),
scope: self.analysis.ty_cx.map.get_parent(item.id),
value: self.span_utils.snippet(expr.span),
type_value: ty_to_string(&typ),
})
}
_ => {
unimplemented!();
}
}
}
pub fn get_data_for_id(&self, _id: &NodeId) -> Data {
unimplemented!();
}
}
struct PathCollector {
collected_paths: Vec<(NodeId, ast::Path, ast::Mutability, recorder::Row)>,
}
impl PathCollector {
fn new() -> PathCollector {
PathCollector {
collected_paths: vec![],
}
}
}
impl<'v> Visitor<'v> for PathCollector {
fn visit_pat(&mut self, p: &ast::Pat) {
if generated_code(p.span) {
return;
}
match p.node {
ast::PatStruct(ref path, _, _) => {
self.collected_paths.push((p.id,
path.clone(),
ast::MutMutable,
recorder::StructRef));
}
ast::PatEnum(ref path, _) |
ast::PatQPath(_, ref path) => {
self.collected_paths.push((p.id, path.clone(), ast::MutMutable, recorder::VarRef));
}
ast::PatIdent(bm, ref path1, _) => {
debug!("PathCollector, visit ident in pat {}: {:?} {:?}",
token::get_ident(path1.node),
p.span,
path1.span);
let immut = match bm {
ast::BindByRef(_) => ast::MutImmutable,
ast::BindByValue(mt) => mt,
};
let path = ast_util::ident_to_path(path1.span, path1.node);
self.collected_paths.push((p.id, path, immut, recorder::VarRef));
}
_ => {}
}
visit::walk_pat(self, p);
}
}
#[allow(deprecated)]
pub fn process_crate(sess: &Session,
krate: &ast::Crate,
analysis: &ty::CrateAnalysis,
odir: Option<&Path>) {
if generated_code(krate.span) {
return;
}
assert!(analysis.glob_map.is_some());
let cratename = match attr::find_crate_name(&krate.attrs) {
Some(name) => name.to_string(),
None => {
info!("Could not find crate name, using 'unknown_crate'");
String::from_str("unknown_crate")
},
};
info!("Dumping crate {}", cratename);
let mut root_path = match env::var_os("DXR_RUST_TEMP_FOLDER") {
Some(val) => PathBuf::from(val),
None => match odir {
Some(val) => val.join("dxr"),
None => PathBuf::from("dxr-temp"),
},
};
match fs::create_dir_all(&root_path) {
Err(e) => sess.err(&format!("Could not create directory {}: {}",
root_path.display(), e)),
_ => (),
}
{
let disp = root_path.display();
info!("Writing output to {}", disp);
}
let mut out_name = cratename.clone();
out_name.push_str(".csv");
root_path.push(&out_name);
let output_file = match File::create(&root_path) {
Ok(f) => box f,
Err(e) => {
let disp = root_path.display();
sess.fatal(&format!("Could not open {}: {}", disp, e));
}
};
root_path.pop();
let mut visitor = dump_csv::DumpCsvVisitor::new(sess, analysis, output_file);
visitor.dump_crate_info(&cratename[..], krate);
visit::walk_crate(&mut visitor, krate);
}
fn escape(s: String) -> String {
s.replace("\"", "\"\"")
}
fn generated_code(span: Span) -> bool {
span.expn_id != NO_EXPANSION || span == DUMMY_SP
}