Only extract function bodies for local crates,

unless the -Oextract_dependencies=true flag is supplied
This commit is contained in:
Arthur Baars
2024-09-10 15:45:09 +02:00
parent 43e54fb0ae
commit f37b4aebc2
4 changed files with 49 additions and 10 deletions

View File

@@ -13,4 +13,24 @@ file_types:
- name: rust
display_name: Rust files
extensions:
- .rs
- .rs
options:
trap:
title: Options pertaining to TRAP.
type: object
properties:
compression:
title: Controls compression for the TRAP files written by the extractor.
description: >
This option is only intended for use in debugging the extractor. Accepted
values are 'brotli' (the default, to write brotli-compressed TRAP), 'gzip', and 'none'
(to write uncompressed TRAP).
type: string
pattern: "^(none|gzip|brotli)$"
extract_dependencies:
title: Whether to extract dependencies.
description: >
Extract the source code of dependencies and the standard libraries in addition to
normal source code.
type: string
pattern: "^(false|true)$"

View File

@@ -32,6 +32,7 @@ pub struct Config {
pub scratch_dir: PathBuf,
pub trap_dir: PathBuf,
pub source_archive_dir: PathBuf,
pub extract_dependencies: bool,
pub verbose: u8,
pub compression: Compression,
pub inputs: Vec<PathBuf>,
@@ -72,6 +73,7 @@ impl Config {
}
Figment::new()
.merge(Env::prefixed("CODEQL_EXTRACTOR_RUST_"))
.merge(Env::prefixed("CODEQL_EXTRACTOR_RUST_OPTION_"))
.merge(Serialized::defaults(cli_args))
.extract()
.context("loading configuration")

View File

@@ -78,9 +78,16 @@ fn main() -> anyhow::Result<()> {
crate_id.into_raw().into_u32()
)),
);
translate::CrateTranslator::new(&db, trap, &krate, &vfs, &archiver)
.emit_crate()
.context("writing trap file")?;
translate::CrateTranslator::new(
&db,
trap,
&krate,
&vfs,
&archiver,
cfg.extract_dependencies,
)
.emit_crate()
.context("writing trap file")?;
}
}
Ok(())

View File

@@ -32,6 +32,7 @@ pub struct CrateTranslator<'a> {
krate: &'a Crate,
vfs: &'a Vfs,
archiver: &'a Archiver,
extract_dependencies: bool,
file_labels: HashMap<PathBuf, FileData>,
}
@@ -42,6 +43,7 @@ impl CrateTranslator<'_> {
krate: &'a Crate,
vfs: &'a Vfs,
archiver: &'a Archiver,
extract_dependencies: bool,
) -> CrateTranslator<'a> {
CrateTranslator {
db,
@@ -49,6 +51,7 @@ impl CrateTranslator<'_> {
krate,
vfs,
archiver,
extract_dependencies,
file_labels: HashMap::new(),
}
}
@@ -928,20 +931,27 @@ impl CrateTranslator<'_> {
}
ModuleDef::Function(function) => {
let def: ra_ap_hir::DefWithBody = function.into();
let (body, source_map) = self.db.body_with_source_map(def.into());
let txt = body.pretty_print(self.db, def.into(), Edition::Edition2021);
println!("{}", &txt);
let name = function.name(self.db);
let location = self.emit_location(function);
let body = self.emit_expr(body.body_expr, &body, &source_map);
let body = if self.extract_dependencies || self.krate.origin(self.db).is_local() {
let (body, source_map) = self.db.body_with_source_map(def.into());
let txt = body.pretty_print(self.db, def.into(), Edition::Edition2021);
println!("{}", &txt);
self.emit_expr(body.body_expr, &body, &source_map)
} else {
self.trap.emit(generated::MissingExpr {
id: TrapId::Star,
location: None,
})
};
labels.push(self.trap.emit(generated::Function {
id: trap_key![module_label, name.as_str()],
location,
name: name.as_str().into(),
body,
}));
}))
}
ModuleDef::Adt(adt) => {
let location = self.emit_location(adt);