Skip to content

Commit 1964daf

Browse files
committed
Rust: exclude all function, const, and static bodies for dependencies
1 parent dc847e4 commit 1964daf

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
@@ -16,7 +16,7 @@ use ra_ap_ide_db::RootDatabase;
1616
use ra_ap_ide_db::line_index::{LineCol, LineIndex};
1717
use ra_ap_parser::SyntaxKind;
1818
use ra_ap_span::TextSize;
19-
use ra_ap_syntax::ast::HasName;
19+
use ra_ap_syntax::ast::{Const, Fn, HasName, Static};
2020
use ra_ap_syntax::{
2121
AstNode, NodeOrToken, SyntaxElementChildren, SyntaxError, SyntaxNode, SyntaxToken, TextRange,
2222
ast,
@@ -598,7 +598,34 @@ impl<'a> Translator<'a> {
598598
})();
599599
}
600600

601-
pub(crate) fn should_be_excluded(&self, item: &impl ast::HasAttrs) -> bool {
601+
pub(crate) fn should_be_excluded(&self, item: &(impl ast::HasAttrs + ast::AstNode)) -> bool {
602+
if "true"
603+
== std::env::var("CODEQL_EXTRACTOR_RUST_OPTION_EXCLUDE_BODIES").unwrap_or_default()
604+
{
605+
let syntax = item.syntax();
606+
if let Some(body) = syntax.parent().and_then(Fn::cast).and_then(|x| x.body()) {
607+
if body.syntax() == syntax {
608+
tracing::debug!("Skipping Fn body");
609+
return true;
610+
}
611+
}
612+
if let Some(body) = syntax.parent().and_then(Const::cast).and_then(|x| x.body()) {
613+
if body.syntax() == syntax {
614+
tracing::debug!("Skipping Const body");
615+
return true;
616+
}
617+
}
618+
if let Some(body) = syntax
619+
.parent()
620+
.and_then(Static::cast)
621+
.and_then(|x| x.body())
622+
{
623+
if body.syntax() == syntax {
624+
tracing::debug!("Skipping Static body");
625+
return true;
626+
}
627+
}
628+
}
602629
self.semantics.is_some_and(|sema| {
603630
item.attrs().any(|attr| {
604631
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)