Skip to content

Commit e25160b

Browse files
committed
refactor: improve code comments
1 parent 40411f0 commit e25160b

27 files changed

+324
-280
lines changed

src/bin/cargo-pta.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
// This source code is licensed under the GNU license found in the
44
// LICENSE file in the root directory of this source tree.
55

6+
//! This provides an implementation for the "cargo pta" subcommand.
7+
//!
8+
//! The subcommand is the same as "cargo check" but with three differences:
9+
//! 1) It implicitly adds the options "-Z always_encode_mir" to the rustc invocation.
10+
//! 2) It calls `pta` rather than `rustc` for all the targets of the current package.
11+
//! 3) It runs `cargo test --no-run` for test targets.
12+
613
use cargo_metadata::Package;
714
use log::info;
815
use rustc_tools_util::VersionInfo;
@@ -15,11 +22,15 @@ use std::process::{Command, Stdio};
1522

1623
use rupta::util;
1724

25+
/// The help message for `cargo-pta`
1826
const CARGO_PTA_HELP: &str = r#"Pointer analysis tool for Rust programs
1927
Usage:
2028
cargo pta
2129
"#;
2230

31+
/// Set the environment variable `PTA_BUILD_STD` to enable the building of std library when running pta.
32+
const PTA_BUILD_STD: &str = "PTA_BUILD_STD";
33+
2334
pub fn main() {
2435
if std::env::args().take_while(|a| a != "--").any(|a| a == "--help" || a == "-h") {
2536
println!("{}", CARGO_PTA_HELP);
@@ -126,17 +137,12 @@ fn call_cargo_on_target(target: &String, kind: &str) {
126137
if arg == "--" {
127138
break;
128139
}
129-
if arg == "--no-std-build" {
130-
continue;
131-
}
132140
cmd.arg(arg);
133141
}
134142

135143
// Enable Cargo to compile the standard library from source code as part of a crate graph compilation.
136-
if !has_arg_flag("-Zbuild-std") && !has_arg_flag("build-std") {
137-
if !has_arg_flag("--no-std-build") {
138-
cmd.arg("-Zbuild-std");
139-
}
144+
if env::var(PTA_BUILD_STD).is_ok() {
145+
cmd.arg("-Zbuild-std");
140146

141147
if !has_arg_flag("--target") {
142148
let toolchain_target = toolchain_target().expect("could not get toolchain target");
@@ -244,15 +250,15 @@ fn call_rustc() {
244250
}
245251
}
246252

247-
// Determines whether a flag `name` is present before `--`.
248-
// For example, has_arg_flag("-v")
253+
/// Determines whether a flag `name` is present before `--`.
254+
/// For example, has_arg_flag("-v")
249255
fn has_arg_flag(name: &str) -> bool {
250256
let mut args = std::env::args().take_while(|val| val != "--");
251257
args.any(|val| val == name)
252258
}
253259

254-
// Gets the value of a `name`.
255-
// `--name value` or `--name=value`
260+
/// Gets the value of `name`.
261+
/// `--name value` or `--name=value`
256262
fn get_arg_flag_value(name: &str) -> Option<String> {
257263
let mut args = std::env::args().take_while(|val| val != "--");
258264
loop {
@@ -274,7 +280,7 @@ fn get_arg_flag_value(name: &str) -> Option<String> {
274280
}
275281
}
276282

277-
// Returns the target of the toolchain, e.g. "x86_64-unknown-linux-gnu".
283+
/// Returns the target of the toolchain, e.g. "x86_64-unknown-linux-gnu".
278284
fn toolchain_target() -> Option<String> {
279285
let sysroot = util::find_sysroot();
280286

src/bin/pta.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
// This source code is licensed under the GNU license found in the
44
// LICENSE file in the root directory of this source tree.
55

6+
//! The main routine of `rupta`.
7+
//!
8+
//! Implemented as a stub that invokes the rust compiler with a call back to execute
9+
//! pointer analysis during rust compilation.
10+
611
#![feature(rustc_private)]
712

813
extern crate rustc_driver;

src/builder/call_graph_builder.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,51 @@
33
// This source code is licensed under the GNU license found in the
44
// LICENSE file in the root directory of this source tree.
55

6+
//! This module provides essential functions for resolving call targets.
7+
68
use rustc_hir::def_id::DefId;
79
use rustc_middle::ty::{GenericArgsRef, TyCtxt, TyKind};
810

911
use crate::util;
1012

11-
/// Try to resolve the given FnDef, devirtualize the callee function if possible.
13+
/// Try to resolve the function with `def_id` and `gen_args`.
14+
///
15+
/// If the function is not a trait method, (`def_id`, `gen_args`) is returned
16+
/// directly. Otherwise, the function is devirtualized to a specific implementation.
1217
pub fn resolve_fn_def<'tcx>(
1318
tcx: TyCtxt<'tcx>,
14-
callee_def_id: DefId,
19+
def_id: DefId,
1520
gen_args: GenericArgsRef<'tcx>,
1621
) -> (DefId, GenericArgsRef<'tcx>) {
17-
if tcx.is_mir_available(callee_def_id) && !util::is_trait_method(tcx, callee_def_id) {
18-
(callee_def_id, gen_args)
19-
} else if let Some((callee_def_id, callee_substs)) =
20-
try_to_devirtualize(tcx, callee_def_id, gen_args)
22+
if tcx.is_mir_available(def_id) && !util::is_trait_method(tcx, def_id) {
23+
(def_id, gen_args)
24+
} else if let Some((resolved_def_id, resolved_substs)) =
25+
try_to_devirtualize(tcx, def_id, gen_args)
2126
{
22-
(callee_def_id, callee_substs)
27+
(resolved_def_id, resolved_substs)
2328
} else {
24-
// if the mir is unavailable or the callee cannot be resolved, return the callee_def_id directly
25-
(callee_def_id, gen_args)
29+
// if the function cannot be resolved,
30+
// return the original (def_id, gen_args) pair directly.
31+
(def_id, gen_args)
2632
}
2733
}
2834

35+
/// Try to devirtualize a trait method with `def_id` and `gen_args`.
36+
///
37+
/// Returns `None` if the given `def_id` does not correspond to a trait method or
38+
/// we cannot resolve the trait method to a specific instance. For example, the
39+
/// first gen_arg is a dynamic type.
2940
pub fn try_to_devirtualize<'tcx>(
3041
tcx: TyCtxt<'tcx>,
31-
callee_def_id: DefId,
42+
def_id: DefId,
3243
gen_args: GenericArgsRef<'tcx>,
3344
) -> Option<(DefId, GenericArgsRef<'tcx>)> {
34-
if !util::is_trait_method(tcx, callee_def_id) {
45+
if !util::is_trait_method(tcx, def_id) {
3546
return None;
3647
}
3748

49+
// A trait method cannot be devirtualized when the first gen_arg corresponds
50+
// to a dynamic type.
3851
let arg0_ty = gen_args
3952
.types()
4053
.next()
@@ -45,20 +58,20 @@ pub fn try_to_devirtualize<'tcx>(
4558

4659
let param_env = rustc_middle::ty::ParamEnv::reveal_all();
4760
let abi = tcx
48-
.type_of(callee_def_id)
61+
.type_of(def_id)
4962
.skip_binder()
5063
.fn_sig(tcx)
5164
.abi();
5265
let resolved_instance = if abi == rustc_target::spec::abi::Abi::Rust {
5366
// Instance::resolve panics if try_normalize_erasing_regions returns an error.
54-
// It is hard to figure out exactly when this will be the case.
67+
// It is difficult to determine exactly when this error will occur.
5568
if tcx.try_normalize_erasing_regions(param_env, gen_args).is_err() {
5669
None
5770
} else {
5871
Some(rustc_middle::ty::Instance::resolve(
5972
tcx,
6073
param_env,
61-
callee_def_id,
74+
def_id,
6275
gen_args,
6376
))
6477
}
@@ -67,10 +80,6 @@ pub fn try_to_devirtualize<'tcx>(
6780
};
6881
if let Some(Ok(Some(instance))) = resolved_instance {
6982
let resolved_def_id = instance.def.def_id();
70-
let has_mir = tcx.is_mir_available(resolved_def_id);
71-
if !has_mir {
72-
return None;
73-
}
7483
return Some((resolved_def_id, instance.args));
7584
}
7685
None

0 commit comments

Comments
 (0)