Skip to content

Commit f37b4ae

Browse files
committed
Only extract function bodies for local crates,
unless the -Oextract_dependencies=true flag is supplied
1 parent 43e54fb commit f37b4ae

File tree

4 files changed

+49
-10
lines changed

4 files changed

+49
-10
lines changed

rust/codeql-extractor.yml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,24 @@ file_types:
1313
- name: rust
1414
display_name: Rust files
1515
extensions:
16-
- .rs
16+
- .rs
17+
options:
18+
trap:
19+
title: Options pertaining to TRAP.
20+
type: object
21+
properties:
22+
compression:
23+
title: Controls compression for the TRAP files written by the extractor.
24+
description: >
25+
This option is only intended for use in debugging the extractor. Accepted
26+
values are 'brotli' (the default, to write brotli-compressed TRAP), 'gzip', and 'none'
27+
(to write uncompressed TRAP).
28+
type: string
29+
pattern: "^(none|gzip|brotli)$"
30+
extract_dependencies:
31+
title: Whether to extract dependencies.
32+
description: >
33+
Extract the source code of dependencies and the standard libraries in addition to
34+
normal source code.
35+
type: string
36+
pattern: "^(false|true)$"

rust/extractor/src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub struct Config {
3232
pub scratch_dir: PathBuf,
3333
pub trap_dir: PathBuf,
3434
pub source_archive_dir: PathBuf,
35+
pub extract_dependencies: bool,
3536
pub verbose: u8,
3637
pub compression: Compression,
3738
pub inputs: Vec<PathBuf>,
@@ -72,6 +73,7 @@ impl Config {
7273
}
7374
Figment::new()
7475
.merge(Env::prefixed("CODEQL_EXTRACTOR_RUST_"))
76+
.merge(Env::prefixed("CODEQL_EXTRACTOR_RUST_OPTION_"))
7577
.merge(Serialized::defaults(cli_args))
7678
.extract()
7779
.context("loading configuration")

rust/extractor/src/main.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,16 @@ fn main() -> anyhow::Result<()> {
7878
crate_id.into_raw().into_u32()
7979
)),
8080
);
81-
translate::CrateTranslator::new(&db, trap, &krate, &vfs, &archiver)
82-
.emit_crate()
83-
.context("writing trap file")?;
81+
translate::CrateTranslator::new(
82+
&db,
83+
trap,
84+
&krate,
85+
&vfs,
86+
&archiver,
87+
cfg.extract_dependencies,
88+
)
89+
.emit_crate()
90+
.context("writing trap file")?;
8491
}
8592
}
8693
Ok(())

rust/extractor/src/translate.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub struct CrateTranslator<'a> {
3232
krate: &'a Crate,
3333
vfs: &'a Vfs,
3434
archiver: &'a Archiver,
35+
extract_dependencies: bool,
3536
file_labels: HashMap<PathBuf, FileData>,
3637
}
3738

@@ -42,13 +43,15 @@ impl CrateTranslator<'_> {
4243
krate: &'a Crate,
4344
vfs: &'a Vfs,
4445
archiver: &'a Archiver,
46+
extract_dependencies: bool,
4547
) -> CrateTranslator<'a> {
4648
CrateTranslator {
4749
db,
4850
trap,
4951
krate,
5052
vfs,
5153
archiver,
54+
extract_dependencies,
5255
file_labels: HashMap::new(),
5356
}
5457
}
@@ -928,20 +931,27 @@ impl CrateTranslator<'_> {
928931
}
929932
ModuleDef::Function(function) => {
930933
let def: ra_ap_hir::DefWithBody = function.into();
931-
let (body, source_map) = self.db.body_with_source_map(def.into());
932-
let txt = body.pretty_print(self.db, def.into(), Edition::Edition2021);
933-
println!("{}", &txt);
934934

935935
let name = function.name(self.db);
936-
937936
let location = self.emit_location(function);
938-
let body = self.emit_expr(body.body_expr, &body, &source_map);
937+
938+
let body = if self.extract_dependencies || self.krate.origin(self.db).is_local() {
939+
let (body, source_map) = self.db.body_with_source_map(def.into());
940+
let txt = body.pretty_print(self.db, def.into(), Edition::Edition2021);
941+
println!("{}", &txt);
942+
self.emit_expr(body.body_expr, &body, &source_map)
943+
} else {
944+
self.trap.emit(generated::MissingExpr {
945+
id: TrapId::Star,
946+
location: None,
947+
})
948+
};
939949
labels.push(self.trap.emit(generated::Function {
940950
id: trap_key![module_label, name.as_str()],
941951
location,
942952
name: name.as_str().into(),
943953
body,
944-
}));
954+
}))
945955
}
946956
ModuleDef::Adt(adt) => {
947957
let location = self.emit_location(adt);

0 commit comments

Comments
 (0)