Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust: extract sources of crates #18523

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions rust/extractor/src/translate/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)| {
Expand Down
2 changes: 1 addition & 1 deletion rust/tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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__"],
)
36 changes: 36 additions & 0 deletions rust/tools/autobuild.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 3 additions & 1 deletion rust/tools/autobuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading