Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
Add option to just print out function signatures (#1185)
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanventer authored Sep 29, 2022
1 parent 59eea5e commit 4459d55
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 30 deletions.
48 changes: 20 additions & 28 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ string which can contain any of the following flags:
- `--single_func <name>`: the name of a specific function you want to analyze.
- `--body_analysis_timeout <seconds>`: the maximum number of seconds to spend analyzing a function body.
- `--call_graph_config <path_to_config>`: path to configuration file for call graph generator (see [Call Graph Generator documentation](documentation/CallGraph.md)). No call graph will be generated if this is not specified.
- `--print_function_names`: just print the source location and fully qualified function signature of every function.
- `--`: any arguments after this marker are passed on to rustc.

You can get some insight into the inner workings of MIRAI by setting the verbosity level of log output to one of
Expand Down
2 changes: 1 addition & 1 deletion checker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ doctest = false # and no doc tests
[dependencies]
bincode = { version = "*", features = ["i128"] }
cargo_metadata = "*"
clap = "*"
clap = "3.2"
env_logger = "*"
fs2 = "*"
itertools = "*"
Expand Down
11 changes: 11 additions & 0 deletions checker/src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::options::Options;
use crate::summaries::PersistentSummaryCache;

use crate::type_visitor::TypeCache;
use crate::utils;
use log::info;
use log_derive::*;
use rustc_driver::Compilation;
Expand Down Expand Up @@ -128,6 +129,16 @@ impl MiraiCallbacks {
/// Analyze the crate currently being compiled, using the information given in compiler and tcx.
#[logfn(TRACE)]
fn analyze_with_mirai<'tcx>(&mut self, compiler: &interface::Compiler, tcx: TyCtxt<'tcx>) {
if self.options.print_function_names {
for local_def_id in tcx.hir().body_owners() {
let def_id = local_def_id.to_def_id();
let span = tcx.def_span(def_id);
eprint!("{:?}: ", span);
let name = utils::def_id_as_qualified_name_str(tcx, def_id);
eprintln!("{}", name);
}
return;
}
let output_dir = String::from(self.output_directory.to_str().expect("valid string"));
let summary_store_path = if std::env::var("MIRAI_SHARE_PERSISTENT_STORE").is_ok() {
output_dir
Expand Down
10 changes: 9 additions & 1 deletion checker/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ fn make_options_parser<'help>(running_test_harness: bool) -> Command<'help> {
.long("call_graph_config")
.takes_value(true)
.help("Path call graph config.")
.long_help(r#"Path to a JSON file that configures call graph output. Please see the documentation for details (https://github.com/facebookexperimental/MIRAI/blob/main/documentation/CallGraph.md)."#));
.long_help(r#"Path to a JSON file that configures call graph output. Please see the documentation for details (https://github.com/facebookexperimental/MIRAI/blob/main/documentation/CallGraph.md)."#))
.arg(Arg::new("print_function_names")
.long("print_function_names")
.takes_value(false)
.help("Just print out the signatures of functions in the crate"));
if running_test_harness {
parser = parser.arg(Arg::new("test_only")
.long("test_only")
Expand All @@ -76,6 +80,7 @@ pub struct Options {
pub max_analysis_time_for_crate: u64,
pub statistics: bool,
pub call_graph_config: Option<String>,
pub print_function_names: bool,
}

/// Represents diag level.
Expand Down Expand Up @@ -213,6 +218,9 @@ impl Options {
if matches.is_present("call_graph_config") {
self.call_graph_config = matches.value_of("call_graph_config").map(|s| s.to_string());
}
if matches.is_present("print_function_names") {
self.print_function_names = true;
}
args[rustc_args_start..].to_vec()
}
}
26 changes: 26 additions & 0 deletions checker/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,32 @@ fn push_component_name(component_data: DefPathData, target: &mut String) {
};
}

/// Returns a human readable string representation of the item defined by def_id.
/// This is different from summary_key_str, in that it does not mangle type argument
/// values into the item name and furthermore includes parameter types and the
/// return types of functions. This is intended for use in call graphs.
pub fn def_id_as_qualified_name_str(tcx: TyCtxt<'_>, def_id: DefId) -> Rc<str> {
let mut name = format!("/{}/", crate_name(tcx, def_id));
name.push_str(&tcx.def_path_str(def_id));
let fn_ty = tcx.type_of(def_id);
if fn_ty.is_fn() {
name.push('(');
let fn_sig = fn_ty.fn_sig(tcx).skip_binder();
let mut first = true;
for param_ty in fn_sig.inputs() {
if first {
first = false;
} else {
name.push(',')
}
name.push_str(&format!("{:?}", param_ty));
}
name.push_str(")->");
name.push_str(&format!("{:?}", fn_sig.output()));
}
Rc::from(name.as_str())
}

/// Returns a readable display name for a DefId. This name may not be unique.
pub fn def_id_display_name(tcx: TyCtxt<'_>, def_id: DefId) -> String {
struct PrettyDefId<'tcx>(DefId, TyCtxt<'tcx>);
Expand Down

0 comments on commit 4459d55

Please sign in to comment.