Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty printing of source code locations #131

Merged
merged 21 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Cargo.lock

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

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
[workspace]
members = ["crates/*"]
exclude = ["props", "crates/paralegal-flow/tests", "crates/paralegal-policy/tests", "crates/paralegal-explore"]
exclude = [
"props",
"crates/paralegal-flow/tests",
"crates/paralegal-policy/tests",
"crates/paralegal-explore",
]
resolver = "2"

[workspace.dependencies]
indexical = "0.3.1"
serde = "1.0.188"
petgraph = { version = "0.6", features = ["serde-1"] }
strum = { version = "0.25", features = ["derive"] }

[profile.release]
debug = true
Expand Down
2 changes: 1 addition & 1 deletion crates/paralegal-flow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ num-derive = "0.4"
num-traits = "0.2"
petgraph = { workspace = true }
humantime = "2"
strum = { version = "0.25", features = ["derive"] }
strum = { workspace = true }


#dot = "0.1"
Expand Down
105 changes: 87 additions & 18 deletions crates/paralegal-flow/src/ana/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use flowistry::pdg::{
};
use itertools::Itertools;
use petgraph::visit::{GraphBase, IntoNodeReferences, NodeIndexable, NodeRef};
use rustc_span::{FileNameDisplayPreference, Span as RustSpan};

mod inline_judge;

Expand Down Expand Up @@ -119,6 +120,7 @@ impl<'tcx> SPDGGenerator<'tcx> {
.iter()
.map(|id| (*id, def_info_for_item(*id, tcx)))
.collect();

let type_info = self.collect_type_info();
type_info_sanity_check(&controllers, &type_info);
ProgramDescription {
Expand Down Expand Up @@ -147,25 +149,49 @@ impl<'tcx> SPDGGenerator<'tcx> {
all_instructions
.into_iter()
.map(|i| {
let body = self.tcx.body_for_def_id(i.function).unwrap();
let info = match i.location {
RichLocation::End => InstructionInfo::Return,
RichLocation::Start => InstructionInfo::Start,
RichLocation::Location(loc) => match body.body.stmt_at(loc) {
crate::Either::Right(term) => {
if let Ok((id, ..)) = term.as_fn_and_args(self.tcx) {
InstructionInfo::FunctionCall(FunctionCallInfo {
id,
is_inlined: id.is_local(),
})
} else {
InstructionInfo::Terminator
let body = &self.tcx.body_for_def_id(i.function).unwrap().body;

let kind = match i.location {
RichLocation::End => InstructionKind::Return,
RichLocation::Start => InstructionKind::Start,
RichLocation::Location(loc) => {
let kind = match body.stmt_at(loc) {
crate::Either::Right(term) => {
if let Ok((id, ..)) = term.as_fn_and_args(self.tcx) {
InstructionKind::FunctionCall(FunctionCallInfo {
id,
is_inlined: id.is_local(),
})
} else {
InstructionKind::Terminator
}
}
}
_ => InstructionInfo::Statement,
},
crate::Either::Left(_) => InstructionKind::Statement,
};

kind
}
};
(i, info)
let rust_span = match i.location {
RichLocation::Location(loc) => {
let expanded_span = match body.stmt_at(loc) {
crate::Either::Right(term) => term.source_info.span,
crate::Either::Left(stmt) => stmt.source_info.span,
};
self.tcx
.sess
.source_map()
.stmt_span(expanded_span, body.span)
}
RichLocation::Start | RichLocation::End => self.tcx.def_span(i.function),
};
(
i,
InstructionInfo {
kind,
span: src_loc_for_span(rust_span, self.tcx),
},
)
})
.collect()
}
Expand Down Expand Up @@ -202,6 +228,38 @@ impl<'tcx> SPDGGenerator<'tcx> {
}
}

fn src_loc_for_span(span: RustSpan, tcx: TyCtxt) -> Span {
let (source_file, start_line, start_col, end_line, end_col) =
tcx.sess.source_map().span_to_location_info(span);
let file_path = source_file
.expect("could not find source file")
.name
.display(FileNameDisplayPreference::Local)
.to_string();
let abs_file_path = if !file_path.starts_with('/') {
std::env::current_dir()
.expect("failed to obtain current working directory")
.join(&file_path)
} else {
std::path::PathBuf::from(&file_path)
};
let src_info = SourceFileInfo {
file_path,
abs_file_path,
};
Span {
source_file: src_info.intern(),
start: SpanCoord {
line: start_line as u32,
col: start_col as u32,
},
end: SpanCoord {
line: end_line as u32,
col: end_col as u32,
},
}
}

fn default_index() -> <SPDGImpl as GraphBase>::NodeId {
<SPDGImpl as GraphBase>::NodeId::end()
}
Expand Down Expand Up @@ -545,16 +603,22 @@ impl<'a, 'tcx, C: Extend<DefId>> GraphConverter<'tcx, 'a, C> {
use petgraph::prelude::*;
let g_ref = self.dep_graph.clone();
let input = &g_ref.graph;
let tcx = self.tcx();
let mut markers: HashMap<NodeIndex, Vec<Identifier>> = HashMap::new();

for (i, weight) in input.node_references() {
let (kind, is_external_call_source, node_markers) = self.determine_node_kind(weight);
let at = weight.at.leaf();
let body = &tcx.body_for_def_id(at.function).unwrap().body;

let node_span = body.local_decls[weight.place.local].source_info.span;
let new_idx = self.register_node(
i,
NodeInfo {
at: weight.at,
description: format!("{:?}", weight.place),
kind,
span: src_loc_for_span(node_span, tcx),
},
);

Expand Down Expand Up @@ -729,7 +793,12 @@ fn def_info_for_item(id: DefId, tcx: TyCtxt) -> DefInfo {
}
}))
.collect();
DefInfo { name, path, kind }
DefInfo {
name,
path,
kind,
src_info: src_loc_for_span(tcx.def_span(id), tcx),
}
}

/// A higher order function that increases the logging level if the `target`
Expand Down
1 change: 1 addition & 0 deletions crates/paralegal-flow/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ impl<'g> CtrlRef<'g> {
.chain(self.ctrl.graph.node_weights().map(|info| info.at))
.filter(|m| {
instruction_info[&m.leaf()]
.kind
.as_function_call()
.map_or(false, |i| i.id == fun.ident)
})
Expand Down
2 changes: 2 additions & 0 deletions crates/paralegal-policy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ simple_logger = "2"
lazy_static = "1"
bitvec = "1"
petgraph = { workspace = true }
colored = "1"
strum = { workspace = true }

[dev-dependencies]
paralegal-flow = { path = "../paralegal-flow", features = ["test"] }
Loading
Loading