Skip to content

Commit

Permalink
Add EXPORT_ALL option for exporting more symbols in MIR JSON
Browse files Browse the repository at this point in the history
This will be needed for a `cargo saw-export` command (#39), which works over
more symbols than just the ones with a `crux_test` configuration.
  • Loading branch information
RyanGlScott committed May 1, 2023
1 parent f7f142e commit e207be6
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 7 deletions.
3 changes: 3 additions & 0 deletions doc/rustc.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ variables:
dependencies.
* `CRUX_USE_OVERRIDE_CRATES`: The list of crates for which `crucible-mir`
overrides should be used.
* `EXPORT_ALL`: If this environment variable is set, then the MIR JSON file
will export all top-level functions. Otherwise, it will only export those
functions with a `#[crux_test]` attribute.

## Other binaries

Expand Down
12 changes: 9 additions & 3 deletions src/analyz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ fn init_instances_from_mono_items(ms: &mut MirState) -> io::Result<()> {
fn init_instances_from_tests(ms: &mut MirState, out: &mut impl JsonOutput) -> io::Result<()> {
let tcx = ms.state.tcx;
for &def_id in tcx.mir_keys(def_id::LOCAL_CRATE) {
if !has_test_attr(tcx, def_id) {
if ms.export_style == ExportStyle::ExportCruxTests && !has_test_attr(tcx, def_id) {
continue;
}

Expand Down Expand Up @@ -884,6 +884,7 @@ fn emit_fn<'tcx>(
state: ms.state,
tys: ms.tys,
match_span_map: ms.match_span_map,
export_style: ms.export_style,
};
let ms = &mut ms;

Expand Down Expand Up @@ -945,6 +946,7 @@ pub struct AnalysisData<O> {
fn analyze_inner<'tcx, O: JsonOutput, F: FnOnce(&Path) -> io::Result<O>>(
sess: &Session,
queries: &'tcx Queries<'tcx>,
export_style: ExportStyle,
mk_output: F,
) -> Result<Option<AnalysisData<O>>, serde_cbor::Error> {
let mut mir_path = None;
Expand Down Expand Up @@ -993,6 +995,7 @@ fn analyze_inner<'tcx, O: JsonOutput, F: FnOnce(&Path) -> io::Result<O>>(
state: &state,
tys: &mut tys,
match_span_map: &get_match_spans(),
export_style: export_style,
};

// Traits and top-level statics can be enumerated directly.
Expand Down Expand Up @@ -1037,8 +1040,9 @@ fn analyze_inner<'tcx, O: JsonOutput, F: FnOnce(&Path) -> io::Result<O>>(
pub fn analyze_nonstreaming<'tcx>(
sess: &Session,
queries: &'tcx Queries<'tcx>,
export_style: ExportStyle,
) -> Result<Option<AnalysisData<()>>, serde_cbor::Error> {
let opt_ad = analyze_inner(sess, queries, |_| { Ok(lib_util::Output::default()) })?;
let opt_ad = analyze_inner(sess, queries, export_style, |_| { Ok(lib_util::Output::default()) })?;
let AnalysisData { mir_path, extern_mir_paths, output: out } = match opt_ad {
Some(x) => x,
None => return Ok(None),
Expand Down Expand Up @@ -1067,8 +1071,9 @@ pub fn analyze_nonstreaming<'tcx>(
pub fn analyze_streaming<'tcx>(
sess: &Session,
queries: &'tcx Queries<'tcx>,
export_style: ExportStyle,
) -> Result<Option<AnalysisData<()>>, serde_cbor::Error> {
let opt_ad = analyze_inner(sess, queries, lib_util::start_streaming)?;
let opt_ad = analyze_inner(sess, queries, export_style, lib_util::start_streaming)?;
let AnalysisData { mir_path, extern_mir_paths, output } = match opt_ad {
Some(x) => x,
None => return Ok(None),
Expand All @@ -1078,6 +1083,7 @@ pub fn analyze_streaming<'tcx>(
}

pub use self::analyze_streaming as analyze;
pub use analyz::to_json::ExportStyle;

fn make_attr(key: &str, value: &str) -> ast::Attribute {
ast::Attribute {
Expand Down
16 changes: 16 additions & 0 deletions src/analyz/to_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,21 @@ impl<'tcx> TyIntern<'tcx> {
}
}

/// How many functions should be exported in the JSON output?
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum ExportStyle {
/// Only export functions annotated with the `#[crux_test]` attribute.
ExportCruxTests,
/// Export all functions.
ExportAll
}

impl Default for ExportStyle {
fn default() -> Self {
ExportStyle::ExportCruxTests
}
}

pub struct MirState<'a, 'tcx : 'a> {
pub mir: Option<&'tcx Body<'tcx>>,
pub used: &'a mut Used<'tcx>,
Expand All @@ -263,6 +278,7 @@ pub struct MirState<'a, 'tcx : 'a> {
/// rewritten. This seems okay for now since the user is mostly interested in coverage in
/// their own top-level crate anyway.
pub match_span_map: &'a HashMap<Span, Span>,
pub export_style: ExportStyle,
}

/// Trait for converting MIR elements to JSON.
Expand Down
11 changes: 10 additions & 1 deletion src/bin/mir-json-rustc-wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ fn scrub_externs(externs: &mut Externs, use_override_crates: &HashSet<String>) {
struct MirJsonCallbacks {
analysis_data: Option<analyz::AnalysisData<()>>,
use_override_crates: HashSet<String>,
export_style: analyz::ExportStyle,
}

impl rustc_driver::Callbacks for MirJsonCallbacks {
Expand Down Expand Up @@ -137,7 +138,7 @@ impl rustc_driver::Callbacks for MirJsonCallbacks {
compiler: &Compiler,
queries: &'tcx Queries<'tcx>,
) -> Compilation {
self.analysis_data = analyz::analyze(compiler.session(), queries).unwrap();
self.analysis_data = analyz::analyze(compiler.session(), queries, self.export_style).unwrap();
Compilation::Continue
}
}
Expand Down Expand Up @@ -196,6 +197,12 @@ fn go() {
}
}

let export_style = if env::var("EXPORT_ALL").is_ok() {
analyz::ExportStyle::ExportAll
} else {
analyz::ExportStyle::ExportCruxTests
};


let test_idx = match args.iter().position(|s| s == "--test") {
None => {
Expand All @@ -207,6 +214,7 @@ fn go() {
&mut MirJsonCallbacks {
analysis_data: None,
use_override_crates: use_override_crates.clone(),
export_style,
},
None,
None,
Expand Down Expand Up @@ -242,6 +250,7 @@ fn go() {
let mut callbacks = MirJsonCallbacks {
analysis_data: None,
use_override_crates: use_override_crates.clone(),
export_style,
};
rustc_driver::run_compiler(
&args,
Expand Down
15 changes: 12 additions & 3 deletions src/bin/mir-json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ use mir_json::analyz;
use rustc_driver::Compilation;
use rustc_interface::interface::Compiler;
use rustc_interface::Queries;
use std::env;

struct MirJsonCallbacks;
struct MirJsonCallbacks {
export_style: analyz::ExportStyle,
}

impl rustc_driver::Callbacks for MirJsonCallbacks {
fn after_parsing<'tcx>(
Expand Down Expand Up @@ -46,17 +49,23 @@ impl rustc_driver::Callbacks for MirJsonCallbacks {
compiler: &Compiler,
queries: &'tcx Queries<'tcx>,
) -> Compilation {
analyz::analyze(compiler.session(), queries).unwrap();
analyz::analyze(compiler.session(), queries, self.export_style).unwrap();
Compilation::Continue
}
}

fn go() {
let args: Vec<String> = std::env::args().collect();

let export_style = if env::var("EXPORT_ALL").is_ok() {
analyz::ExportStyle::ExportAll
} else {
analyz::ExportStyle::ExportCruxTests
};

rustc_driver::run_compiler(
&args, // args: &[String]
&mut MirJsonCallbacks,
&mut MirJsonCallbacks { export_style },
None,
None,
).unwrap();
Expand Down

0 comments on commit e207be6

Please sign in to comment.