Skip to content

Commit

Permalink
Option to print summaries (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanventer authored Dec 10, 2024
1 parent 270643b commit 7afcd5f
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 34 deletions.
47 changes: 19 additions & 28 deletions Cargo.lock

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

Binary file modified binaries/summary_store.tar
Binary file not shown.
21 changes: 18 additions & 3 deletions checker/src/call_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use serde::{Deserialize, Serialize};
use mirai_annotations::*;
use rustc_hir::def_id::DefId;
use rustc_middle::ty::TyCtxt;
use rustc_span::Span;

// An unique identifier for a Rust type string.
type TypeId = u32;
Expand Down Expand Up @@ -116,6 +117,8 @@ pub struct CallGraphConfig {
included_crates: Vec<Box<str>>,
/// Datalog output configuration
datalog_config: Option<DatalogConfig>,
/// If true, collect all call sites.
pub include_calls_in_summaries: bool,
}

impl CallGraphConfig {
Expand All @@ -132,6 +135,7 @@ impl CallGraphConfig {
reductions,
included_crates,
datalog_config,
include_calls_in_summaries: false,
}
}

Expand Down Expand Up @@ -314,7 +318,7 @@ impl CallGraphEdge {
#[derive(Clone)]
pub struct CallGraph<'tcx> {
/// Configuration for the call graph
config: CallGraphConfig,
pub config: CallGraphConfig,
/// A context to use for getting the information about DefIds that is needed
/// to generate fully qualified names for them.
tcx: TyCtxt<'tcx>,
Expand Down Expand Up @@ -368,7 +372,7 @@ impl<'tcx> CallGraph<'tcx> {
self.config.dot_output_path.is_some() || self.config.datalog_config.is_some()
}

/// Produce an updated call graph structure that preserves all of the
/// Produce an updated call graph structure that preserves all the
/// fields except `graph`, which is replaced.
fn update(&self, graph: Graph<CallGraphNode, CallGraphEdge>) -> CallGraph<'tcx> {
CallGraph {
Expand Down Expand Up @@ -433,7 +437,10 @@ impl<'tcx> CallGraph<'tcx> {
callee: DefId,
external_callee: bool,
) {
if self.config.call_sites_output_path.is_some() && !self.non_local_defs.contains(&caller) {
if self.config.include_calls_in_summaries
|| (self.config.call_sites_output_path.is_some()
&& !self.non_local_defs.contains(&caller))
{
self.call_sites.insert(loc, (caller, callee));
if external_callee {
self.non_local_defs.insert(callee);
Expand Down Expand Up @@ -1082,6 +1089,14 @@ impl<'tcx> CallGraph<'tcx> {
call_graph.to_call_sites(Path::new(call_path.as_ref()));
}
}

pub fn get_calls_for_def_ids(&self) -> HashMap<DefId, Vec<(Span, DefId)>> {
let mut calls = HashMap::<DefId, Vec<(Span, DefId)>>::new();
for (span, (caller, callee)) in self.call_sites.iter() {
calls.entry(*caller).or_default().push((*span, *callee));
}
calls
}
}

/// Supported Datalog output formats
Expand Down
4 changes: 4 additions & 0 deletions checker/src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,11 @@ impl MiraiCallbacks {
type_cache: Rc::new(RefCell::new(TypeCache::new())),
call_graph: CallGraph::new(call_graph_config, tcx),
};
if crate_visitor.options.print_summaries {
crate_visitor.call_graph.config.include_calls_in_summaries = true;
}
crate_visitor.analyze_some_bodies();
crate_visitor.call_graph.output();
crate_visitor.print_summaries();
}
}
14 changes: 13 additions & 1 deletion checker/src/crate_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl<'compilation> CrateVisitor<'compilation, '_> {
DefId::local(DefIndex::from_u32(0))
};

// Analyze all functions that are white listed or public
// Analyze all functions that are whitelisted or public
let building_standard_summaries = std::env::var("MIRAI_START_FRESH").is_ok();
for local_def_id in self.tcx.hir().body_owners() {
let def_id = local_def_id.to_def_id();
Expand Down Expand Up @@ -185,6 +185,7 @@ impl<'compilation> CrateVisitor<'compilation, '_> {
let kind = self.tcx.def_kind(def_id);
if matches!(kind, rustc_hir::def::DefKind::Static { .. })
|| utils::is_foreign_contract(self.tcx, def_id)
|| self.options.print_summaries
{
self.summary_cache
.set_summary_for(def_id, self.tcx, summary);
Expand Down Expand Up @@ -290,4 +291,15 @@ impl<'compilation> CrateVisitor<'compilation, '_> {
}
}
}

pub fn print_summaries(&mut self) {
if !self.options.print_summaries {
return;
}
let calls_for_def_ids = self.call_graph.get_calls_for_def_ids();
let summaries_for_llm = self
.summary_cache
.get_summaries_for_llm(self.tcx, calls_for_def_ids);
print!("{}", summaries_for_llm.to_json());
}
}
2 changes: 1 addition & 1 deletion checker/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl Options {
matches.value_source("print_summaries"),
Some(ValueSource::DefaultValue)
) {
self.print_summaries = false;
self.print_summaries = true;
}
args[rustc_args_start..].to_vec()
}
Expand Down
Loading

0 comments on commit 7afcd5f

Please sign in to comment.