Skip to content

Commit 1c4e799

Browse files
authored
Rollup merge of rust-lang#36132 - nrc:save-std, r=@eddyb
Add --Zsave-analysis-api This is a save-analysis variation which can be used with libraries distributed without their source (e.g., libstd). It will allow IDEs and other tools to get info about types and create URLs to docs and source, without the unnecessary clutter of internal-only save-analysis info. I'm sure we'll iterate somewhat on the design, but this is a first draft.
2 parents 2e1669f + 377be7a commit 1c4e799

File tree

9 files changed

+608
-83
lines changed

9 files changed

+608
-83
lines changed

src/librustc/session/config.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -849,9 +849,13 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
849849
ls: bool = (false, parse_bool, [UNTRACKED],
850850
"list the symbols defined by a library crate"),
851851
save_analysis: bool = (false, parse_bool, [UNTRACKED],
852-
"write syntax and type analysis (in JSON format) information in addition to normal output"),
852+
"write syntax and type analysis (in JSON format) information, in \
853+
addition to normal output"),
853854
save_analysis_csv: bool = (false, parse_bool, [UNTRACKED],
854-
"write syntax and type analysis (in CSV format) information in addition to normal output"),
855+
"write syntax and type analysis (in CSV format) information, in addition to normal output"),
856+
save_analysis_api: bool = (false, parse_bool, [UNTRACKED],
857+
"write syntax and type analysis information for opaque libraries (in JSON format), \
858+
in addition to normal output"),
855859
print_move_fragments: bool = (false, parse_bool, [UNTRACKED],
856860
"print out move-fragment data for every fn"),
857861
flowgraph_print_loans: bool = (false, parse_bool, [UNTRACKED],
@@ -2365,6 +2369,8 @@ mod tests {
23652369
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
23662370
opts.debugging_opts.save_analysis_csv = true;
23672371
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
2372+
opts.debugging_opts.save_analysis_api = true;
2373+
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
23682374
opts.debugging_opts.print_move_fragments = true;
23692375
assert_eq!(reference.dep_tracking_hash(), opts.dep_tracking_hash());
23702376
opts.debugging_opts.flowgraph_print_loans = true;

src/librustc_driver/driver.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ fn keep_hygiene_data(sess: &Session) -> bool {
250250
fn keep_ast(sess: &Session) -> bool {
251251
sess.opts.debugging_opts.keep_ast ||
252252
sess.opts.debugging_opts.save_analysis ||
253-
sess.opts.debugging_opts.save_analysis_csv
253+
sess.opts.debugging_opts.save_analysis_csv ||
254+
sess.opts.debugging_opts.save_analysis_api
254255
}
255256

256257
/// The name used for source code that doesn't originate in a file

src/librustc_driver/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -555,14 +555,17 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
555555

556556
fn save_analysis(sess: &Session) -> bool {
557557
sess.opts.debugging_opts.save_analysis ||
558-
sess.opts.debugging_opts.save_analysis_csv
558+
sess.opts.debugging_opts.save_analysis_csv ||
559+
sess.opts.debugging_opts.save_analysis_api
559560
}
560561

561562
fn save_analysis_format(sess: &Session) -> save::Format {
562563
if sess.opts.debugging_opts.save_analysis {
563564
save::Format::Json
564565
} else if sess.opts.debugging_opts.save_analysis_csv {
565566
save::Format::Csv
567+
} else if sess.opts.debugging_opts.save_analysis_api {
568+
save::Format::JsonApi
566569
} else {
567570
unreachable!();
568571
}

src/librustc_save_analysis/data.rs

+50-6
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
//! The `Dump` trait can be used together with `DumpVisitor` in order to
1414
//! retrieve the data from a crate.
1515
16+
use rustc::hir;
1617
use rustc::hir::def_id::DefId;
17-
use syntax::ast::{CrateNum, NodeId};
18+
use syntax::ast::{self, CrateNum, NodeId};
1819
use syntax_pos::Span;
1920

2021
pub struct CrateData {
@@ -76,6 +77,35 @@ pub enum Data {
7677
VariableRefData(VariableRefData),
7778
}
7879

80+
#[derive(Eq, PartialEq, Clone, Copy, Debug, RustcEncodable)]
81+
pub enum Visibility {
82+
Public,
83+
Restricted,
84+
Inherited,
85+
}
86+
87+
impl<'a> From<&'a ast::Visibility> for Visibility {
88+
fn from(v: &'a ast::Visibility) -> Visibility {
89+
match *v {
90+
ast::Visibility::Public => Visibility::Public,
91+
ast::Visibility::Crate(_) => Visibility::Restricted,
92+
ast::Visibility::Restricted { .. } => Visibility::Restricted,
93+
ast::Visibility::Inherited => Visibility::Inherited,
94+
}
95+
}
96+
}
97+
98+
impl<'a> From<&'a hir::Visibility> for Visibility {
99+
fn from(v: &'a hir::Visibility) -> Visibility {
100+
match *v {
101+
hir::Visibility::Public => Visibility::Public,
102+
hir::Visibility::Crate => Visibility::Restricted,
103+
hir::Visibility::Restricted { .. } => Visibility::Restricted,
104+
hir::Visibility::Inherited => Visibility::Inherited,
105+
}
106+
}
107+
}
108+
79109
/// Data for the prelude of a crate.
80110
#[derive(Debug, RustcEncodable)]
81111
pub struct CratePreludeData {
@@ -103,7 +133,7 @@ pub struct EnumData {
103133
pub span: Span,
104134
pub scope: NodeId,
105135
pub variants: Vec<NodeId>,
106-
136+
pub visibility: Visibility,
107137
}
108138

109139
/// Data for extern crates.
@@ -135,6 +165,8 @@ pub struct FunctionData {
135165
pub span: Span,
136166
pub scope: NodeId,
137167
pub value: String,
168+
pub visibility: Visibility,
169+
pub parent: Option<NodeId>,
138170
}
139171

140172
/// Data about a function call.
@@ -215,6 +247,7 @@ pub struct MethodData {
215247
pub scope: NodeId,
216248
pub value: String,
217249
pub decl_id: Option<DefId>,
250+
pub visibility: Visibility,
218251
}
219252

220253
/// Data for modules.
@@ -227,6 +260,7 @@ pub struct ModData {
227260
pub scope: NodeId,
228261
pub filename: String,
229262
pub items: Vec<NodeId>,
263+
pub visibility: Visibility,
230264
}
231265

232266
/// Data for a reference to a module.
@@ -248,6 +282,7 @@ pub struct StructData {
248282
pub scope: NodeId,
249283
pub value: String,
250284
pub fields: Vec<NodeId>,
285+
pub visibility: Visibility,
251286
}
252287

253288
#[derive(Debug, RustcEncodable)]
@@ -258,7 +293,8 @@ pub struct StructVariantData {
258293
pub qualname: String,
259294
pub type_value: String,
260295
pub value: String,
261-
pub scope: NodeId
296+
pub scope: NodeId,
297+
pub parent: Option<NodeId>,
262298
}
263299

264300
#[derive(Debug, RustcEncodable)]
@@ -270,6 +306,7 @@ pub struct TraitData {
270306
pub scope: NodeId,
271307
pub value: String,
272308
pub items: Vec<NodeId>,
309+
pub visibility: Visibility,
273310
}
274311

275312
#[derive(Debug, RustcEncodable)]
@@ -280,7 +317,8 @@ pub struct TupleVariantData {
280317
pub qualname: String,
281318
pub type_value: String,
282319
pub value: String,
283-
pub scope: NodeId
320+
pub scope: NodeId,
321+
pub parent: Option<NodeId>,
284322
}
285323

286324
/// Data for a typedef.
@@ -291,6 +329,8 @@ pub struct TypeDefData {
291329
pub span: Span,
292330
pub qualname: String,
293331
pub value: String,
332+
pub visibility: Visibility,
333+
pub parent: Option<NodeId>,
294334
}
295335

296336
/// Data for a reference to a type or trait.
@@ -308,15 +348,17 @@ pub struct UseData {
308348
pub span: Span,
309349
pub name: String,
310350
pub mod_id: Option<DefId>,
311-
pub scope: NodeId
351+
pub scope: NodeId,
352+
pub visibility: Visibility,
312353
}
313354

314355
#[derive(Debug, RustcEncodable)]
315356
pub struct UseGlobData {
316357
pub id: NodeId,
317358
pub span: Span,
318359
pub names: Vec<String>,
319-
pub scope: NodeId
360+
pub scope: NodeId,
361+
pub visibility: Visibility,
320362
}
321363

322364
/// Data for local and global variables (consts and statics).
@@ -328,8 +370,10 @@ pub struct VariableData {
328370
pub qualname: String,
329371
pub span: Span,
330372
pub scope: NodeId,
373+
pub parent: Option<NodeId>,
331374
pub value: String,
332375
pub type_value: String,
376+
pub visibility: Visibility,
333377
}
334378

335379
#[derive(Debug, RustcEncodable)]

0 commit comments

Comments
 (0)