Skip to content

Commit 5c5aac6

Browse files
committed
Rust: exclude all function, const, and static bodies for dependencies
1 parent 08c95ef commit 5c5aac6

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

rust/extractor/src/translate/base.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use ra_ap_ide_db::line_index::{LineCol, LineIndex};
1818
use ra_ap_ide_db::RootDatabase;
1919
use ra_ap_parser::SyntaxKind;
2020
use ra_ap_span::{EditionedFileId, TextSize};
21-
use ra_ap_syntax::ast::HasName;
21+
use ra_ap_syntax::ast::{Const, Fn, HasName, Static};
2222
use ra_ap_syntax::{
2323
ast, AstNode, NodeOrToken, SyntaxElementChildren, SyntaxError, SyntaxNode, SyntaxToken,
2424
TextRange,
@@ -563,7 +563,34 @@ impl<'a> Translator<'a> {
563563
})();
564564
}
565565

566-
pub(crate) fn should_be_excluded(&self, item: &impl ast::HasAttrs) -> bool {
566+
pub(crate) fn should_be_excluded(&self, item: &(impl ast::HasAttrs + ast::AstNode)) -> bool {
567+
if "true"
568+
== std::env::var("CODEQL_EXTRACTOR_RUST_OPTION_EXCLUDE_BODIES").unwrap_or_default()
569+
{
570+
let syntax = item.syntax();
571+
if let Some(body) = syntax.parent().and_then(Fn::cast).and_then(|x| x.body()) {
572+
if body.syntax() == item.syntax() {
573+
log::warn!("Skipping Fn body");
574+
return true;
575+
}
576+
}
577+
if let Some(body) = syntax.parent().and_then(Const::cast).and_then(|x| x.body()) {
578+
if body.syntax() == item.syntax() {
579+
log::warn!("Skipping Const body");
580+
return true;
581+
}
582+
}
583+
if let Some(body) = syntax
584+
.parent()
585+
.and_then(Static::cast)
586+
.and_then(|x| x.body())
587+
{
588+
if body.syntax() == item.syntax() {
589+
log::warn!("Skipping Static body");
590+
return true;
591+
}
592+
}
593+
}
567594
self.semantics.is_some_and(|sema| {
568595
item.attrs().any(|attr| {
569596
attr.as_simple_call().is_some_and(|(name, tokens)| {

rust/tools/autobuild.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,27 @@ def get_cargo_metadata():
2020
scratch_dir = os.environ.get("CODEQL_EXTRACTOR_RUST_SCRATCH_DIR")
2121
metadata = get_cargo_metadata()
2222
metadata_file = os.path.join(scratch_dir, "metadata_file.yaml")
23+
os.makedirs(scratch_dir, exist_ok=True)
2324
with open(metadata_file, "w") as f:
2425
f.write("---\n")
2526
f.write(json.dumps(metadata, indent=4))
2627

2728
subprocess.run(["codeql", "database", "index-files", database,
2829
"-lyaml", "--working-dir", scratch_dir, "--include", "metadata_file.yaml"])
30+
paths = set()
2931
for package in metadata['packages']:
3032
for target in package['targets']:
31-
if 'lib' in target['kind']:
33+
if 'lib' in target['kind'] or 'bin' in target['kind']:
3234
src_path = target['src_path']
33-
dir = os.path.dirname(src_path)
34-
autobuild = "{root}/tools/{platform}/autobuild".format(
35-
root=CODEQL_EXTRACTOR_RUST_ROOT, platform=CODEQL_PLATFORM)
36-
subprocess.run([autobuild], cwd=dir)
35+
paths.add(os.path.dirname(src_path))
36+
37+
autobuild = "{root}/tools/{platform}/autobuild".format(
38+
root=CODEQL_EXTRACTOR_RUST_ROOT, platform=CODEQL_PLATFORM)
39+
40+
for path in paths:
41+
if not path.startswith(os.getcwd()):
42+
env = os.environ.copy()
43+
env['CODEQL_EXTRACTOR_RUST_OPTION_EXCLUDE_BODIES'] = "true"
44+
else:
45+
env = None
46+
subprocess.run([autobuild], cwd=path, env=env)

0 commit comments

Comments
 (0)