diff --git a/rust/extractor/src/translate/base.rs b/rust/extractor/src/translate/base.rs index cfdde2053d07..0ad1fabb203e 100644 --- a/rust/extractor/src/translate/base.rs +++ b/rust/extractor/src/translate/base.rs @@ -18,7 +18,7 @@ use ra_ap_ide_db::line_index::{LineCol, LineIndex}; use ra_ap_ide_db::RootDatabase; use ra_ap_parser::SyntaxKind; use ra_ap_span::{EditionedFileId, TextSize}; -use ra_ap_syntax::ast::HasName; +use ra_ap_syntax::ast::{Const, Fn, HasName, Static}; use ra_ap_syntax::{ ast, AstNode, NodeOrToken, SyntaxElementChildren, SyntaxError, SyntaxNode, SyntaxToken, TextRange, @@ -563,7 +563,31 @@ impl<'a> Translator<'a> { })(); } - pub(crate) fn should_be_excluded(&self, item: &impl ast::HasAttrs) -> bool { + pub(crate) fn should_be_excluded(&self, item: &(impl ast::HasAttrs + ast::AstNode)) -> bool { + let syntax = item.syntax(); + if let Some(body) = syntax.parent().and_then(Fn::cast).and_then(|x| x.body()) { + if body.syntax() == item.syntax() { + log::warn!("Skipping Fn body"); + return true; + } + } + if let Some(body) = syntax.parent().and_then(Const::cast).and_then(|x| x.body()) { + if body.syntax() == item.syntax() { + log::warn!("Skipping Const body"); + return true; + } + } + if let Some(body) = syntax + .parent() + .and_then(Static::cast) + .and_then(|x| x.body()) + { + if body.syntax() == item.syntax() { + log::warn!("Skipping Static body"); + return true; + } + } + self.semantics.is_some_and(|sema| { item.attrs().any(|attr| { attr.as_simple_call().is_some_and(|(name, tokens)| { diff --git a/rust/tools/BUILD.bazel b/rust/tools/BUILD.bazel index 92a97109edee..6b1c42d4a13b 100644 --- a/rust/tools/BUILD.bazel +++ b/rust/tools/BUILD.bazel @@ -2,7 +2,7 @@ load("//misc/bazel:pkg.bzl", "codeql_pkg_files") codeql_pkg_files( name = "tools", - srcs = glob(["*.cmd"]), + srcs = glob(["*.cmd", "autobuild.py"]), exes = glob(["*.sh"]), visibility = ["//rust:__pkg__"], ) diff --git a/rust/tools/autobuild.py b/rust/tools/autobuild.py new file mode 100644 index 000000000000..91a3d162067f --- /dev/null +++ b/rust/tools/autobuild.py @@ -0,0 +1,36 @@ + +import os +import json +import subprocess + +""" +run "cargo metadata --format-version=1" +""" + + +def get_cargo_metadata(): + metadata = json.loads(subprocess.check_output( + ["cargo", "metadata", "--format-version=1"])) + return metadata + + +CODEQL_EXTRACTOR_RUST_ROOT = os.environ.get("CODEQL_EXTRACTOR_RUST_ROOT") +CODEQL_PLATFORM = os.environ.get("CODEQL_PLATFORM") +database = os.environ.get("CODEQL_EXTRACTOR_RUST_WIP_DATABASE") +scratch_dir = os.environ.get("CODEQL_EXTRACTOR_RUST_SCRATCH_DIR") +metadata = get_cargo_metadata() +metadata_file = os.path.join(scratch_dir, "metadata_file.yaml") +with open(metadata_file, "w") as f: + f.write("---\n") + f.write(json.dumps(metadata, indent=4)) + +subprocess.run(["codeql", "database", "index-files", database, + "-lyaml", "--working-dir", scratch_dir, "--include", "metadata_file.yaml"]) +for package in metadata['packages']: + for target in package['targets']: + if 'lib' in target['kind']: + src_path = target['src_path'] + dir = os.path.dirname(src_path) + autobuild = "{root}/tools/{platform}/autobuild".format( + root=CODEQL_EXTRACTOR_RUST_ROOT, platform=CODEQL_PLATFORM) + subprocess.run([autobuild], cwd=dir) diff --git a/rust/tools/autobuild.sh b/rust/tools/autobuild.sh index 6d78ecf6d1f1..483f44c8ca67 100755 --- a/rust/tools/autobuild.sh +++ b/rust/tools/autobuild.sh @@ -3,4 +3,6 @@ set -eu export RUST_BACKTRACE=1 -exec "$CODEQL_EXTRACTOR_RUST_ROOT/tools/$CODEQL_PLATFORM/autobuild" +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +exec /usr/bin/env python "${SCRIPT_DIR}/autobuild.py" \ No newline at end of file