diff --git a/Cargo.lock b/Cargo.lock index a4037e283442..d11fbc94b96c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -437,7 +437,7 @@ dependencies = [ name = "codeql-autobuilder-rust" version = "0.1.0" dependencies = [ - "codeql-extractor", + "extractor-common", ] [[package]] @@ -455,12 +455,10 @@ dependencies = [ "serde", "serde_json", "tracing", - "tracing-subscriber", "tree-sitter", "tree-sitter-json 0.24.8", "tree-sitter-ql 0.23.1 (registry+https://github.com/rust-lang/crates.io-index)", "yeast", - "zstd", ] [[package]] @@ -469,6 +467,7 @@ version = "0.1.0" dependencies = [ "clap", "codeql-extractor", + "extractor-common", "rayon", "regex", "tracing", @@ -504,6 +503,7 @@ version = "0.1.0" dependencies = [ "clap", "codeql-extractor", + "extractor-common", "rayon", "regex", "serde", @@ -525,7 +525,6 @@ dependencies = [ "anyhow", "chrono", "clap", - "codeql-extractor", "dunce", "extractor-common", "figment", @@ -931,6 +930,11 @@ dependencies = [ [[package]] name = "extractor-common" version = "0.1.0" +dependencies = [ + "flate2", + "tracing-subscriber", + "zstd", +] [[package]] name = "fastrand" diff --git a/misc/bazel/3rdparty/rust_workspace_deps/crates.bzl b/misc/bazel/3rdparty/rust_workspace_deps/crates.bzl index 8216914f89b6..42f20636780a 100644 --- a/misc/bazel/3rdparty/rust_workspace_deps/crates.bzl +++ b/misc/bazel/3rdparty/rust_workspace_deps/crates.bzl @@ -456,11 +456,15 @@ _NORMAL_DEPENDENCIES = { }, }, "shared/extractor-common": { + _COMMON_CONDITION: { + "flate2": Label("@vendor_rs//flate2-1.1.9"), + "tracing-subscriber": Label("@vendor_rs//tracing-subscriber-0.3.23"), + "zstd": Label("@vendor_rs//zstd-0.14.2"), + }, }, "shared/tree-sitter-extractor": { _COMMON_CONDITION: { "chrono": Label("@vendor_rs//chrono-0.4.45"), - "flate2": Label("@vendor_rs//flate2-1.1.9"), "globset": Label("@vendor_rs//globset-0.4.18"), "num_cpus": Label("@vendor_rs//num_cpus-1.17.0"), "rayon": Label("@vendor_rs//rayon-1.12.0"), @@ -468,9 +472,7 @@ _NORMAL_DEPENDENCIES = { "serde": Label("@vendor_rs//serde-1.0.229"), "serde_json": Label("@vendor_rs//serde_json-1.0.154"), "tracing": Label("@vendor_rs//tracing-0.1.44"), - "tracing-subscriber": Label("@vendor_rs//tracing-subscriber-0.3.23"), "tree-sitter": Label("@vendor_rs//tree-sitter-0.26.9"), - "zstd": Label("@vendor_rs//zstd-0.14.2"), }, }, "shared/yeast": { @@ -552,6 +554,8 @@ _NORMAL_ALIASES = { }, }, "shared/extractor-common": { + _COMMON_CONDITION: { + }, }, "shared/tree-sitter-extractor": { _COMMON_CONDITION: { @@ -602,6 +606,7 @@ _NORMAL_DEV_DEPENDENCIES = { }, "shared/tree-sitter-extractor": { _COMMON_CONDITION: { + "flate2": Label("@vendor_rs//flate2-1.1.9"), "rand": Label("@vendor_rs//rand-0.10.3"), "tree-sitter-json": Label("@vendor_rs//tree-sitter-json-0.24.8"), "tree-sitter-ql": Label("@vendor_rs//tree-sitter-ql-0.23.1"), diff --git a/ql/extractor/Cargo.toml b/ql/extractor/Cargo.toml index 8f68f9331741..4bd05c7a1a29 100644 --- a/ql/extractor/Cargo.toml +++ b/ql/extractor/Cargo.toml @@ -21,3 +21,4 @@ tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } rayon = "1.9.0" regex = "1.10.4" codeql-extractor = { path = "../../shared/tree-sitter-extractor" } +extractor-common = { path = "../../shared/extractor-common" } diff --git a/ql/extractor/src/autobuilder.rs b/ql/extractor/src/autobuilder.rs index 77f171ce285b..4ac1ac3b5b9d 100644 --- a/ql/extractor/src/autobuilder.rs +++ b/ql/extractor/src/autobuilder.rs @@ -3,7 +3,7 @@ use std::path::PathBuf; use clap::Args; -use codeql_extractor::autobuilder; +use extractor_common::autobuilder; #[derive(Args)] // The autobuilder takes no command-line options, but this may change in the future. diff --git a/ql/extractor/src/extractor.rs b/ql/extractor/src/extractor.rs index a59e6fddcfc0..69c694f68671 100644 --- a/ql/extractor/src/extractor.rs +++ b/ql/extractor/src/extractor.rs @@ -2,7 +2,7 @@ use clap::Args; use std::path::PathBuf; use codeql_extractor::extractor::simple; -use codeql_extractor::trap; +use extractor_common::trap; #[derive(Args)] pub struct Options { @@ -20,7 +20,7 @@ pub struct Options { } pub fn run(options: Options) -> std::io::Result<()> { - codeql_extractor::extractor::set_tracing_level("ql"); + extractor_common::extractor::set_tracing_level("ql"); let extractor = simple::Extractor { prefix: "ql".to_string(), diff --git a/ql/extractor/src/generator.rs b/ql/extractor/src/generator.rs index 7f8ca718344c..3aa204bacfcb 100644 --- a/ql/extractor/src/generator.rs +++ b/ql/extractor/src/generator.rs @@ -15,7 +15,7 @@ pub struct Options { } pub fn run(options: Options) -> std::io::Result<()> { - codeql_extractor::extractor::set_tracing_level("ql"); + extractor_common::extractor::set_tracing_level("ql"); let languages = vec![ Language { diff --git a/ruby/extractor/src/autobuilder.rs b/ruby/extractor/src/autobuilder.rs index a4a09e6bada5..78d53b5f4dbf 100644 --- a/ruby/extractor/src/autobuilder.rs +++ b/ruby/extractor/src/autobuilder.rs @@ -3,7 +3,7 @@ use std::path::PathBuf; use clap::Args; -use codeql_extractor::autobuilder; +use extractor_common::autobuilder; #[derive(Args)] // The autobuilder takes no command-line options, but this may change in the future. diff --git a/ruby/extractor/src/extractor.rs b/ruby/extractor/src/extractor.rs index 88377c88d920..13ff7b5ccb2b 100644 --- a/ruby/extractor/src/extractor.rs +++ b/ruby/extractor/src/extractor.rs @@ -1,5 +1,6 @@ use clap::Args; use extractor_common::file_paths::{self, PathTransformer}; +use extractor_common::trap; use rayon::prelude::*; use std::borrow::Cow; use std::collections::HashSet; @@ -9,7 +10,7 @@ use std::path::{Path, PathBuf}; use std::sync::LazyLock; use tree_sitter::{Language, Parser, Range}; -use codeql_extractor::{diagnostics, extractor, node_types, trap}; +use codeql_extractor::{diagnostics, extractor, node_types}; #[derive(Args)] pub struct Options { @@ -28,7 +29,7 @@ pub struct Options { #[allow(clippy::too_many_lines)] pub fn run(options: &Options) -> std::io::Result<()> { - extractor::set_tracing_level("ruby"); + extractor_common::extractor::set_tracing_level("ruby"); tracing::info!("Extraction started"); let diagnostics = diagnostics::DiagnosticLoggers::new("ruby"); let mut main_thread_logger = diagnostics.logger(); @@ -229,7 +230,7 @@ pub fn run(options: &Options) -> std::io::Result<()> { let path = PathBuf::from("extras"); let mut trap_writer = trap::Writer::new(); - extractor::populate_empty_location(&mut trap_writer); + extractor_common::extractor::populate_empty_location(&mut trap_writer); let res = write_trap( &trap_dir, &path, diff --git a/ruby/extractor/src/generator.rs b/ruby/extractor/src/generator.rs index 0fc6f9d5cc74..ad8068e3c551 100644 --- a/ruby/extractor/src/generator.rs +++ b/ruby/extractor/src/generator.rs @@ -15,7 +15,7 @@ pub struct Options { } pub fn run(options: Options) -> std::io::Result<()> { - codeql_extractor::extractor::set_tracing_level("ruby"); + extractor_common::extractor::set_tracing_level("ruby"); let languages = vec![ Language { diff --git a/rust/autobuild/BUILD.bazel b/rust/autobuild/BUILD.bazel index 1aaaa9d9789c..a639a49ee7dd 100644 --- a/rust/autobuild/BUILD.bazel +++ b/rust/autobuild/BUILD.bazel @@ -14,6 +14,6 @@ codeql_rust_binary( deps = all_crate_deps( normal = True, ) + [ - "//shared/tree-sitter-extractor", + "//shared/extractor-common", ], ) diff --git a/rust/autobuild/Cargo.toml b/rust/autobuild/Cargo.toml index 41f0d8710042..ccff4d6a456d 100644 --- a/rust/autobuild/Cargo.toml +++ b/rust/autobuild/Cargo.toml @@ -7,4 +7,4 @@ edition.workspace = true workspace = true [dependencies] -codeql-extractor = { path = "../../shared/tree-sitter-extractor" } +extractor-common = { path = "../../shared/extractor-common" } diff --git a/rust/autobuild/src/main.rs b/rust/autobuild/src/main.rs index 9587b566b3b9..8b911f82d6b1 100644 --- a/rust/autobuild/src/main.rs +++ b/rust/autobuild/src/main.rs @@ -1,4 +1,4 @@ -use codeql_extractor::autobuilder; +use extractor_common::autobuilder; use std::path::PathBuf; fn main() -> std::io::Result<()> { diff --git a/rust/extractor/BUILD.bazel b/rust/extractor/BUILD.bazel index 0ed545a7f37e..3c3eafc88e50 100644 --- a/rust/extractor/BUILD.bazel +++ b/rust/extractor/BUILD.bazel @@ -19,6 +19,5 @@ codeql_rust_binary( normal = True, ) + [ "//shared/extractor-common", - "//shared/tree-sitter-extractor", ], ) diff --git a/rust/extractor/Cargo.toml b/rust/extractor/Cargo.toml index f013fb07ea76..f88ce58c3558 100644 --- a/rust/extractor/Cargo.toml +++ b/rust/extractor/Cargo.toml @@ -32,7 +32,6 @@ ra_ap_cfg = "0.0.358" ra_ap_intern = "0.0.358" serde = "1.0.229" triomphe = "0.1.17" -codeql-extractor = { path = "../../shared/tree-sitter-extractor" } extractor-common = { path = "../../shared/extractor-common" } itertools = "0.15.0" glob = "0.3.4" diff --git a/rust/extractor/src/config.rs b/rust/extractor/src/config.rs index bc884db75bb9..3a6c034749df 100644 --- a/rust/extractor/src/config.rs +++ b/rust/extractor/src/config.rs @@ -2,7 +2,7 @@ mod deserialize; use anyhow::Context; use clap::Parser; -use codeql_extractor::trap; +use extractor_common::trap; use figment::{ Figment, providers::{Env, Format, Serialized, Yaml}, diff --git a/rust/extractor/src/main.rs b/rust/extractor/src/main.rs index 92f119b68777..4b7381489642 100644 --- a/rust/extractor/src/main.rs +++ b/rust/extractor/src/main.rs @@ -265,7 +265,7 @@ fn main() -> anyhow::Result<()> { }; tracing_subscriber::registry() - .with(codeql_extractor::extractor::default_subscriber_with_level( + .with(extractor_common::extractor::default_subscriber_with_level( "single_arch", &config.logging_verbosity, )) diff --git a/rust/extractor/src/trap.rs b/rust/extractor/src/trap.rs index 0dd665eea393..8bcb8dfbad29 100644 --- a/rust/extractor/src/trap.rs +++ b/rust/extractor/src/trap.rs @@ -1,6 +1,5 @@ use crate::{config, generated}; -use codeql_extractor::{extractor, trap}; -use extractor_common::file_paths; +use extractor_common::{extractor, file_paths, trap}; use ra_ap_ide_db::line_index::LineCol; use std::fmt::Debug; use std::hash::Hash; diff --git a/shared/extractor-common/BUILD.bazel b/shared/extractor-common/BUILD.bazel index ab2ba3756976..7814b738f236 100644 --- a/shared/extractor-common/BUILD.bazel +++ b/shared/extractor-common/BUILD.bazel @@ -1,10 +1,13 @@ load("@rules_rust//rust:defs.bzl", "rust_library") +load("//misc/bazel/3rdparty/rust_workspace_deps:defs.bzl", "aliases", "all_crate_deps") exports_files(["Cargo.toml"]) rust_library( name = "extractor-common", srcs = glob(["src/**/*.rs"]), + aliases = aliases(), crate_name = "extractor_common", visibility = ["//visibility:public"], + deps = all_crate_deps(), ) diff --git a/shared/extractor-common/Cargo.toml b/shared/extractor-common/Cargo.toml index 437a396936a4..4841853306c8 100644 --- a/shared/extractor-common/Cargo.toml +++ b/shared/extractor-common/Cargo.toml @@ -6,3 +6,9 @@ edition.workspace = true [lints] workspace = true + +# When updating these dependencies, run `rust/update_cargo_deps.sh` +[dependencies] +flate2 = "1.1" +tracing-subscriber = { version = "0.3.23", features = ["env-filter"] } +zstd = "0.14.2" diff --git a/shared/tree-sitter-extractor/src/autobuilder.rs b/shared/extractor-common/src/autobuilder.rs similarity index 100% rename from shared/tree-sitter-extractor/src/autobuilder.rs rename to shared/extractor-common/src/autobuilder.rs diff --git a/shared/extractor-common/src/extractor.rs b/shared/extractor-common/src/extractor.rs new file mode 100644 index 000000000000..9d107b4d01ba --- /dev/null +++ b/shared/extractor-common/src/extractor.rs @@ -0,0 +1,197 @@ +use crate::{file_paths, trap}; +use std::env; +use std::path::Path; + +use tracing_subscriber::EnvFilter; +use tracing_subscriber::Layer; +use tracing_subscriber::filter::Filtered; +use tracing_subscriber::fmt::format::DefaultFields; +use tracing_subscriber::fmt::format::Format; +use tracing_subscriber::layer::SubscriberExt; +use tracing_subscriber::util::SubscriberInitExt; + +/// Sets the tracing level based on the environment variables +/// `RUST_LOG` and `CODEQL_VERBOSITY` (prioritized in that order), +/// falling back to `warn` if neither is set. +pub fn set_tracing_level(language: &str) { + let verbosity = env::var("CODEQL_VERBOSITY").ok(); + tracing_subscriber::registry() + .with(default_subscriber_with_level(language, &verbosity)) + .init(); +} + +/// Create a `Subscriber` configured with the tracing level based on the environment variables +/// `RUST_LOG` and `verbosity` (prioritized in that order), falling back to `warn` if neither is set. +pub fn default_subscriber_with_level( + language: &str, + verbosity: &Option, +) -> Filtered< + tracing_subscriber::fmt::Layer< + tracing_subscriber::Registry, + DefaultFields, + Format, + >, + EnvFilter, + tracing_subscriber::Registry, +> { + tracing_subscriber::fmt::layer() + .with_target(false) + .without_time() + .with_level(true) + .with_filter( + tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else( + |_| -> tracing_subscriber::EnvFilter { + let verbosity = + verbosity + .as_ref() + .map_or("warn", |v| match v.to_lowercase().as_str() { + "off" | "errors" => "error", + "warnings" => "warn", + "info" | "progress" => "info", + "debug" | "progress+" => "debug", + "trace" | "progress++" | "progress+++" => "trace", + _ => "warn", + }); + tracing_subscriber::EnvFilter::new(format!( + "{language}_extractor={verbosity},codeql_extractor={verbosity},extractor_common={verbosity}" + )) + }, + ), + ) +} + +pub fn populate_file( + writer: &mut trap::Writer, + absolute_path: &Path, + transformer: Option<&file_paths::PathTransformer>, +) -> trap::Label { + let (file_label, fresh) = writer.global_id(&trap::full_id_for_file( + &file_paths::normalize_and_transform_path(absolute_path, transformer), + )); + if fresh { + writer.add_tuple( + "files", + vec![ + trap::Arg::Label(file_label), + trap::Arg::String(file_paths::normalize_and_transform_path( + absolute_path, + transformer, + )), + ], + ); + populate_parent_folders(writer, file_label, absolute_path.parent(), transformer); + } + file_label +} + +fn populate_empty_file(writer: &mut trap::Writer) -> trap::Label { + let (file_label, fresh) = writer.global_id("empty;sourcefile"); + if fresh { + writer.add_tuple( + "files", + vec![ + trap::Arg::Label(file_label), + trap::Arg::String(String::new()), + ], + ); + } + file_label +} + +pub fn populate_empty_location(writer: &mut trap::Writer) { + let file_label = populate_empty_file(writer); + let loc_label = global_location( + writer, + trap::Location { + file_label, + start_line: 0, + start_column: 0, + end_line: 0, + end_column: 0, + }, + ); + writer.add_tuple("empty_location", vec![trap::Arg::Label(loc_label)]); +} + +pub fn populate_parent_folders( + writer: &mut trap::Writer, + child_label: trap::Label, + path: Option<&Path>, + transformer: Option<&file_paths::PathTransformer>, +) { + let mut path = path; + let mut child_label = child_label; + loop { + match path { + None => break, + Some(folder) => { + let parent = folder.parent(); + let folder = file_paths::normalize_and_transform_path(folder, transformer); + let (folder_label, fresh) = writer.global_id(&trap::full_id_for_folder(&folder)); + writer.add_tuple( + "containerparent", + vec![ + trap::Arg::Label(folder_label), + trap::Arg::Label(child_label), + ], + ); + if fresh { + writer.add_tuple( + "folders", + vec![trap::Arg::Label(folder_label), trap::Arg::String(folder)], + ); + path = parent; + child_label = folder_label; + } else { + break; + } + } + } + } +} + +/** Get the label for the given location, defining it a global ID if it doesn't exist yet. */ +fn global_location(writer: &mut trap::Writer, location: trap::Location) -> trap::Label { + let (loc_label, fresh) = writer.global_id(&format!( + "loc,{{{}}},{},{},{},{}", + location.file_label, + location.start_line, + location.start_column, + location.end_line, + location.end_column + )); + if fresh { + writer.add_tuple( + "locations_default", + vec![ + trap::Arg::Label(loc_label), + trap::Arg::Label(location.file_label), + trap::Arg::Int(location.start_line), + trap::Arg::Int(location.start_column), + trap::Arg::Int(location.end_line), + trap::Arg::Int(location.end_column), + ], + ); + } + loc_label +} + +/** Get the label for the given location, creating it as a fresh ID if we haven't seen the location + * yet for this file. */ +pub fn location_label(writer: &mut trap::Writer, location: trap::Location) -> trap::Label { + let (loc_label, fresh) = writer.location_label(location); + if fresh { + writer.add_tuple( + "locations_default", + vec![ + trap::Arg::Label(loc_label), + trap::Arg::Label(location.file_label), + trap::Arg::Int(location.start_line), + trap::Arg::Int(location.start_column), + trap::Arg::Int(location.end_line), + trap::Arg::Int(location.end_column), + ], + ); + } + loc_label +} diff --git a/shared/extractor-common/src/lib.rs b/shared/extractor-common/src/lib.rs index 8ecc127546ef..c6e713a435eb 100644 --- a/shared/extractor-common/src/lib.rs +++ b/shared/extractor-common/src/lib.rs @@ -1,3 +1,6 @@ //! Parser-independent utilities shared by CodeQL extractors. +pub mod autobuilder; +pub mod extractor; pub mod file_paths; +pub mod trap; diff --git a/shared/tree-sitter-extractor/src/trap.rs b/shared/extractor-common/src/trap.rs similarity index 100% rename from shared/tree-sitter-extractor/src/trap.rs rename to shared/extractor-common/src/trap.rs diff --git a/shared/tree-sitter-extractor/Cargo.toml b/shared/tree-sitter-extractor/Cargo.toml index e1a1790cee95..d31d0e01ee7a 100644 --- a/shared/tree-sitter-extractor/Cargo.toml +++ b/shared/tree-sitter-extractor/Cargo.toml @@ -10,21 +10,19 @@ workspace = true # When updating these dependencies, run `rust/update_cargo_deps.sh` [dependencies] extractor-common = { path = "../extractor-common" } -flate2 = "1.1" globset = "0.4" tree-sitter = ">= 0.23.0" tracing = "0.1" -tracing-subscriber = { version = "0.3.23", features = ["env-filter"] } rayon = "1.12.0" regex = "1.13.1" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" chrono = { version = "0.4.45", features = ["serde"] } num_cpus = "1.17.0" -zstd = "0.14.2" yeast = { path = "../yeast" } [dev-dependencies] +flate2 = "1.1" tree-sitter-ql = "0.23.1" tree-sitter-json = "0.24.8" rand = "0.10.3" diff --git a/shared/tree-sitter-extractor/src/extractor/desugaring.rs b/shared/tree-sitter-extractor/src/extractor/desugaring.rs index 7f2d7666f6c5..4042e4079914 100644 --- a/shared/tree-sitter-extractor/src/extractor/desugaring.rs +++ b/shared/tree-sitter-extractor/src/extractor/desugaring.rs @@ -8,7 +8,7 @@ //! [`crate::extractor::tree_sitter_parser`]) and fully custom parsers plug in //! the same way. -use crate::trap; +use extractor_common::trap; use std::collections::BTreeMap; use std::path::PathBuf; diff --git a/shared/tree-sitter-extractor/src/extractor/driver.rs b/shared/tree-sitter-extractor/src/extractor/driver.rs index 93aac9e379db..deec2667254c 100644 --- a/shared/tree-sitter-extractor/src/extractor/driver.rs +++ b/shared/tree-sitter-extractor/src/extractor/driver.rs @@ -6,7 +6,7 @@ //! by glob, writing TRAP, and copying into the source archive — is identical //! and lives here, parameterised over the [`LanguageExtractor`] trait. -use extractor_common::file_paths; +use extractor_common::{file_paths, trap}; use globset::{GlobBuilder, GlobSetBuilder}; use rayon::prelude::*; use std::fs::File; @@ -15,7 +15,6 @@ use std::path::{Path, PathBuf}; use crate::diagnostics; use crate::node_types::NodeTypeMap; -use crate::trap; /// A language that [`run_extractor`] can process: it knows its file globs, its /// TRAP schema, and how to extract a single file. Implemented by @@ -204,7 +203,7 @@ pub(crate) fn run_extractor( let path = PathBuf::from("extras"); let mut trap_writer = trap::Writer::new(); - crate::extractor::populate_empty_location(&mut trap_writer); + extractor_common::extractor::populate_empty_location(&mut trap_writer); let res = write_trap(trap_dir, &path, &trap_writer, trap_compression); tracing::info!("Extraction complete"); diff --git a/shared/tree-sitter-extractor/src/extractor/mod.rs b/shared/tree-sitter-extractor/src/extractor/mod.rs index 07663e4481a6..02cc9be2aed6 100644 --- a/shared/tree-sitter-extractor/src/extractor/mod.rs +++ b/shared/tree-sitter-extractor/src/extractor/mod.rs @@ -1,19 +1,11 @@ use crate::diagnostics; use crate::node_types::{self, EntryKind, Field, NodeTypeMap, Storage, TypeName}; -use crate::trap; -use extractor_common::file_paths; +use extractor_common::extractor::{location_label, populate_file}; +use extractor_common::{file_paths, trap}; use std::collections::BTreeMap; use std::collections::BTreeSet as Set; -use std::env; use std::path::Path; -use tracing_subscriber::EnvFilter; -use tracing_subscriber::Layer; -use tracing_subscriber::filter::Filtered; -use tracing_subscriber::fmt::format::DefaultFields; -use tracing_subscriber::fmt::format::Format; -use tracing_subscriber::layer::SubscriberExt; -use tracing_subscriber::util::SubscriberInitExt; use tree_sitter::{Language, Node, Parser, Range, Tree}; pub mod desugaring; @@ -104,191 +96,6 @@ impl AstNode for yeast::Node { } } -/// Sets the tracing level based on the environment variables -/// `RUST_LOG` and `CODEQL_VERBOSITY` (prioritized in that order), -/// falling back to `warn` if neither is set. -pub fn set_tracing_level(language: &str) { - let verbosity = env::var("CODEQL_VERBOSITY").ok(); - tracing_subscriber::registry() - .with(default_subscriber_with_level(language, &verbosity)) - .init(); -} - -/// Create a `Subscriber` configured with the tracing level based on the environment variables -/// `RUST_LOG` and `verbosity` (prioritized in that order), falling back to `warn` if neither is set. -pub fn default_subscriber_with_level( - language: &str, - verbosity: &Option, -) -> Filtered< - tracing_subscriber::fmt::Layer< - tracing_subscriber::Registry, - DefaultFields, - Format, - >, - EnvFilter, - tracing_subscriber::Registry, -> { - tracing_subscriber::fmt::layer() - .with_target(false) - .without_time() - .with_level(true) - .with_filter( - tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else( - |_| -> tracing_subscriber::EnvFilter { - let verbosity = - verbosity - .as_ref() - .map_or("warn", |v| match v.to_lowercase().as_str() { - "off" | "errors" => "error", - "warnings" => "warn", - "info" | "progress" => "info", - "debug" | "progress+" => "debug", - "trace" | "progress++" | "progress+++" => "trace", - _ => "warn", - }); - tracing_subscriber::EnvFilter::new(format!( - "{language}_extractor={verbosity},codeql_extractor={verbosity}" - )) - }, - ), - ) -} -pub fn populate_file( - writer: &mut trap::Writer, - absolute_path: &Path, - transformer: Option<&file_paths::PathTransformer>, -) -> trap::Label { - let (file_label, fresh) = writer.global_id(&trap::full_id_for_file( - &file_paths::normalize_and_transform_path(absolute_path, transformer), - )); - if fresh { - writer.add_tuple( - "files", - vec![ - trap::Arg::Label(file_label), - trap::Arg::String(file_paths::normalize_and_transform_path( - absolute_path, - transformer, - )), - ], - ); - populate_parent_folders(writer, file_label, absolute_path.parent(), transformer); - } - file_label -} - -fn populate_empty_file(writer: &mut trap::Writer) -> trap::Label { - let (file_label, fresh) = writer.global_id("empty;sourcefile"); - if fresh { - writer.add_tuple( - "files", - vec![ - trap::Arg::Label(file_label), - trap::Arg::String(String::new()), - ], - ); - } - file_label -} - -pub fn populate_empty_location(writer: &mut trap::Writer) { - let file_label = populate_empty_file(writer); - let loc_label = global_location( - writer, - trap::Location { - file_label, - start_line: 0, - start_column: 0, - end_line: 0, - end_column: 0, - }, - ); - writer.add_tuple("empty_location", vec![trap::Arg::Label(loc_label)]); -} - -pub fn populate_parent_folders( - writer: &mut trap::Writer, - child_label: trap::Label, - path: Option<&Path>, - transformer: Option<&file_paths::PathTransformer>, -) { - let mut path = path; - let mut child_label = child_label; - loop { - match path { - None => break, - Some(folder) => { - let parent = folder.parent(); - let folder = file_paths::normalize_and_transform_path(folder, transformer); - let (folder_label, fresh) = writer.global_id(&trap::full_id_for_folder(&folder)); - writer.add_tuple( - "containerparent", - vec![ - trap::Arg::Label(folder_label), - trap::Arg::Label(child_label), - ], - ); - if fresh { - writer.add_tuple( - "folders", - vec![trap::Arg::Label(folder_label), trap::Arg::String(folder)], - ); - path = parent; - child_label = folder_label; - } else { - break; - } - } - } - } -} - -/** Get the label for the given location, defining it a global ID if it doesn't exist yet. */ -fn global_location(writer: &mut trap::Writer, location: trap::Location) -> trap::Label { - let (loc_label, fresh) = writer.global_id(&format!( - "loc,{{{}}},{},{},{},{}", - location.file_label, - location.start_line, - location.start_column, - location.end_line, - location.end_column - )); - if fresh { - writer.add_tuple( - "locations_default", - vec![ - trap::Arg::Label(loc_label), - trap::Arg::Label(location.file_label), - trap::Arg::Int(location.start_line), - trap::Arg::Int(location.start_column), - trap::Arg::Int(location.end_line), - trap::Arg::Int(location.end_column), - ], - ); - } - loc_label -} - -/** Get the label for the given location, creating it as a fresh ID if we haven't seen the location - * yet for this file. */ -pub fn location_label(writer: &mut trap::Writer, location: trap::Location) -> trap::Label { - let (loc_label, fresh) = writer.location_label(location); - if fresh { - writer.add_tuple( - "locations_default", - vec![ - trap::Arg::Label(loc_label), - trap::Arg::Label(location.file_label), - trap::Arg::Int(location.start_line), - trap::Arg::Int(location.start_column), - trap::Arg::Int(location.end_line), - trap::Arg::Int(location.end_column), - ], - ); - } - loc_label -} - /// Extract a file with a tree-sitter grammar, walking the parse tree directly. /// /// `path` is assumed to be canonicalized. diff --git a/shared/tree-sitter-extractor/src/extractor/simple.rs b/shared/tree-sitter-extractor/src/extractor/simple.rs index 271f72c610b3..690eec43254f 100644 --- a/shared/tree-sitter-extractor/src/extractor/simple.rs +++ b/shared/tree-sitter-extractor/src/extractor/simple.rs @@ -1,4 +1,4 @@ -use crate::trap; +use extractor_common::trap; use std::path::PathBuf; use crate::diagnostics; diff --git a/shared/tree-sitter-extractor/src/lib.rs b/shared/tree-sitter-extractor/src/lib.rs index dfc5446f657e..0ba366390cdd 100644 --- a/shared/tree-sitter-extractor/src/lib.rs +++ b/shared/tree-sitter-extractor/src/lib.rs @@ -1,7 +1,5 @@ -pub mod autobuilder; pub mod diagnostics; pub mod extractor; pub mod generator; pub mod node_types; pub mod options; -pub mod trap; diff --git a/shared/tree-sitter-extractor/tests/integration_test.rs b/shared/tree-sitter-extractor/tests/integration_test.rs index 2b243ff7945b..ec249eeedcb2 100644 --- a/shared/tree-sitter-extractor/tests/integration_test.rs +++ b/shared/tree-sitter-extractor/tests/integration_test.rs @@ -1,5 +1,5 @@ use codeql_extractor::extractor::simple; -use codeql_extractor::trap; +use extractor_common::trap; mod common; use common::{SourceArchive, create_source_dir, expect_trap_file}; diff --git a/shared/tree-sitter-extractor/tests/multiple_languages.rs b/shared/tree-sitter-extractor/tests/multiple_languages.rs index 2e45e56754a3..670f5b20c311 100644 --- a/shared/tree-sitter-extractor/tests/multiple_languages.rs +++ b/shared/tree-sitter-extractor/tests/multiple_languages.rs @@ -1,5 +1,5 @@ use codeql_extractor::extractor::simple; -use codeql_extractor::trap; +use extractor_common::trap; mod common; use common::{SourceArchive, create_source_dir, expect_trap_file}; diff --git a/unified/extractor/BUILD.bazel b/unified/extractor/BUILD.bazel index 73ce3ca0e810..0d1158f165ab 100644 --- a/unified/extractor/BUILD.bazel +++ b/unified/extractor/BUILD.bazel @@ -23,6 +23,7 @@ codeql_rust_binary( deps = all_crate_deps( normal = True, ) + [ + "//shared/extractor-common", "//shared/tree-sitter-extractor", "//shared/yeast", "//unified/swift-syntax-rs:swift_syntax_rs", @@ -69,6 +70,7 @@ _TESTS = { deps = all_crate_deps( normal = True, ) + [ + "//shared/extractor-common", "//shared/tree-sitter-extractor", "//shared/yeast", "//unified/swift-syntax-rs:swift_syntax_rs", diff --git a/unified/extractor/Cargo.toml b/unified/extractor/Cargo.toml index 6843d3e6d7bd..9eedd39a838c 100644 --- a/unified/extractor/Cargo.toml +++ b/unified/extractor/Cargo.toml @@ -19,5 +19,6 @@ serde = "1.0.229" serde_json = { version = "1.0.154", features = ["unbounded_depth"] } codeql-extractor = { path = "../../shared/tree-sitter-extractor" } +extractor-common = { path = "../../shared/extractor-common" } yeast = { path = "../../shared/yeast" } swift-syntax-rs = { path = "../swift-syntax-rs" } diff --git a/unified/extractor/src/autobuilder.rs b/unified/extractor/src/autobuilder.rs index e18aabc2a529..f775283746c0 100644 --- a/unified/extractor/src/autobuilder.rs +++ b/unified/extractor/src/autobuilder.rs @@ -3,7 +3,7 @@ use std::path::PathBuf; use clap::Args; -use codeql_extractor::autobuilder; +use extractor_common::autobuilder; #[derive(Args)] // The autobuilder takes no command-line options, but this may change in the future. diff --git a/unified/extractor/src/extractor.rs b/unified/extractor/src/extractor.rs index 9693451858d4..15f60be222f6 100644 --- a/unified/extractor/src/extractor.rs +++ b/unified/extractor/src/extractor.rs @@ -1,7 +1,7 @@ use crate::languages; use clap::Args; use codeql_extractor::extractor::desugaring; -use codeql_extractor::trap; +use extractor_common::trap; use std::path::Path; use std::path::PathBuf; use std::{env, fs}; @@ -21,7 +21,7 @@ pub struct Options { } pub fn run(options: Options) -> std::io::Result<()> { - codeql_extractor::extractor::set_tracing_level("unified"); + extractor_common::extractor::set_tracing_level("unified"); // The generated dbscheme/QL library uses the unified_* relation namespace. // Keep per-language specs for parser/rules/file globs, but normalize the diff --git a/unified/extractor/src/generator.rs b/unified/extractor/src/generator.rs index d2243b22c19c..68dee356e2ba 100644 --- a/unified/extractor/src/generator.rs +++ b/unified/extractor/src/generator.rs @@ -18,7 +18,7 @@ pub struct Options { } pub fn run(options: Options) -> std::io::Result<()> { - codeql_extractor::extractor::set_tracing_level("unified"); + extractor_common::extractor::set_tracing_level("unified"); let Options { dbscheme, library } = options; // The QL-visible schema is the unified output AST, not the per-language diff --git a/unified/extractor/src/languages/swift/swift.rs b/unified/extractor/src/languages/swift/swift.rs index 2582955ab8d7..9f615e15af62 100644 --- a/unified/extractor/src/languages/swift/swift.rs +++ b/unified/extractor/src/languages/swift/swift.rs @@ -2091,7 +2091,7 @@ pub fn language_spec(desugared_ast_schema: &'static str) -> desugaring::Language post_extract: Some(Box::new( |context: &SwiftContext, labels: &desugaring::OutputLabels, - trap_writer: &mut codeql_extractor::trap::Writer| { + trap_writer: &mut extractor_common::trap::Writer| { for (node, value) in context.interpreted_strings.borrow().iter() { let node_labels = labels.get(node).ok_or_else(|| { format!(