Skip to content

Commit 41d0085

Browse files
author
Paolo Tranquilli
committed
Rust: address review
1 parent c79f818 commit 41d0085

File tree

9 files changed

+52
-121
lines changed

9 files changed

+52
-121
lines changed

Cargo.lock

Lines changed: 0 additions & 70 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/extractor/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,3 @@ codeql-extractor = { path = "../../shared/tree-sitter-extractor" }
3030
rust-extractor-macros = { path = "macros" }
3131
itertools = "0.13.0"
3232
glob = "0.3.1"
33-
gag = "1.0.0"

rust/extractor/macros/src/lib.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use quote::{format_ident, quote};
66
pub fn extractor_cli_config(_attr: TokenStream, item: TokenStream) -> TokenStream {
77
let ast = syn::parse_macro_input!(item as syn::ItemStruct);
88
let name = &ast.ident;
9-
let new_name = format_ident!("Cli{}", name);
10-
let fields: Vec<_> = ast
9+
let cli_name = format_ident!("Cli{}", name);
10+
let cli_fields = ast
1111
.fields
1212
.iter()
1313
.map(|f| {
@@ -39,17 +39,42 @@ pub fn extractor_cli_config(_attr: TokenStream, item: TokenStream) -> TokenStrea
3939
}
4040
}
4141
})
42-
.collect();
42+
.collect::<Vec<_>>();
43+
let debug_fields = ast
44+
.fields
45+
.iter()
46+
.map(|f| {
47+
let id = f.ident.as_ref().unwrap();
48+
if id == &format_ident!("inputs") {
49+
quote! {
50+
.field("number of inputs", &self.#id.len())
51+
}
52+
} else {
53+
quote! {
54+
.field(stringify!(#id), &self.#id)
55+
}
56+
}
57+
})
58+
.collect::<Vec<_>>();
59+
4360
let gen = quote! {
4461
#[serde_with::apply(_ => #[serde(default)])]
45-
#[derive(Debug, Deserialize, Default)]
62+
#[derive(Deserialize, Default)]
4663
#ast
4764

65+
impl Debug for #name {
66+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67+
f.debug_struct("configuration:")
68+
#(#debug_fields)*
69+
.finish()
70+
}
71+
}
72+
4873
#[serde_with::skip_serializing_none]
4974
#[derive(clap::Parser, Serialize)]
5075
#[command(about, long_about = None)]
51-
struct #new_name {
52-
#(#fields)*
76+
struct #cli_name {
77+
#(#cli_fields)*
5378
}
5479
};
5580
gen.into()

rust/extractor/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use itertools::Itertools;
1010
use num_traits::Zero;
1111
use rust_extractor_macros::extractor_cli_config;
1212
use serde::{Deserialize, Serialize};
13+
use std::fmt::Debug;
1314
use std::ops::Not;
1415
use std::path::PathBuf;
1516

@@ -36,7 +37,6 @@ pub struct Config {
3637
pub scratch_dir: PathBuf,
3738
pub trap_dir: PathBuf,
3839
pub source_archive_dir: PathBuf,
39-
pub log_dir: PathBuf,
4040
pub extract_dependencies: bool,
4141
pub verbose: u8,
4242
pub compression: Compression,
@@ -55,7 +55,7 @@ impl Config {
5555
.merge(Env::prefixed("CODEQL_EXTRACTOR_RUST_"))
5656
.merge(Env::prefixed("CODEQL_EXTRACTOR_RUST_OPTION_"))
5757
.merge(Serialized::defaults(cli_args));
58-
if matches!(figment.find_value("qltest"), Ok(Value::Bool(_, true))) {
58+
if let Ok(Value::Bool(_, true)) = figment.find_value("qltest") {
5959
let cwd = std::env::current_dir()?;
6060
let mut option_files = cwd
6161
.ancestors()

rust/extractor/src/main.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ use log::info;
44
use ra_ap_ide_db::line_index::{LineCol, LineIndex};
55
use ra_ap_project_model::ProjectManifest;
66
use rust_analyzer::{ParseResult, RustAnalyzer};
7-
use std::fs::File;
8-
use std::io::{BufRead, BufReader};
97
use std::{
108
collections::HashMap,
119
path::{Path, PathBuf},
@@ -69,15 +67,16 @@ fn extract(
6967
});
7068
}
7169

72-
fn run_extractor(mut cfg: config::Config) -> anyhow::Result<()> {
70+
fn main() -> anyhow::Result<()> {
71+
let mut cfg = config::Config::extract().context("failed to load configuration")?;
7372
stderrlog::new()
7473
.module(module_path!())
75-
.verbosity(cfg.verbose as usize)
74+
.verbosity(2 + cfg.verbose as usize)
7675
.init()?;
7776
if cfg.qltest {
7877
qltest::prepare(&mut cfg)?;
7978
}
80-
info!("configuration: {cfg:#?}\n");
79+
info!("{cfg:#?}\n");
8180

8281
let traps = trap::TrapFileProvider::new(&cfg).context("failed to set up trap files")?;
8382
let archiver = archive::Archiver {
@@ -125,19 +124,3 @@ fn run_extractor(mut cfg: config::Config) -> anyhow::Result<()> {
125124

126125
Ok(())
127126
}
128-
129-
fn main() -> anyhow::Result<()> {
130-
let cfg = config::Config::extract().context("failed to load configuration")?;
131-
let qltest = cfg.qltest;
132-
let qltest_log = cfg.log_dir.join("qltest.log");
133-
let result = std::panic::catch_unwind(|| run_extractor(cfg));
134-
if qltest && matches!(result, Err(_) | Ok(Err(_))) {
135-
// in case of failure, print out the full log
136-
let log = File::open(qltest_log).context("opening qltest.log")?;
137-
let reader = BufReader::new(log);
138-
for line in reader.lines() {
139-
println!("{}", line.context("reading qltest.log")?);
140-
}
141-
}
142-
result.unwrap()
143-
}

rust/extractor/src/qltest.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use anyhow::Context;
33
use glob::glob;
44
use itertools::Itertools;
55
use log::info;
6+
use std::ffi::OsStr;
67
use std::fs;
78
use std::process::Command;
89

@@ -13,8 +14,9 @@ fn dump_lib() -> anyhow::Result<()> {
1314
.context("fetching test sources")?;
1415
let lib = paths
1516
.iter()
16-
.filter(|p| !["lib.rs", "main.rs"].contains(&p.file_name().unwrap().to_str().unwrap()))
17-
.map(|p| format!("mod {};", p.file_stem().unwrap().to_str().unwrap()))
17+
.map(|p| p.file_stem().expect("results of glob must have a name"))
18+
.filter(|&p| !["main", "lib"].map(OsStr::new).contains(&p))
19+
.map(|p| format!("mod {};", p.to_string_lossy()))
1820
.join("\n");
1921
fs::write("lib.rs", lib).context("writing lib.rs")
2022
}
@@ -49,21 +51,7 @@ fn set_sources(config: &mut Config) -> anyhow::Result<()> {
4951
Ok(())
5052
}
5153

52-
fn redirect_output(config: &Config) -> anyhow::Result<()> {
53-
let log_path = config.log_dir.join("qltest.log");
54-
let log = fs::OpenOptions::new()
55-
.append(true)
56-
.create(true)
57-
.open(&log_path)
58-
.context("opening qltest.log")?;
59-
Box::leak(Box::new(
60-
gag::Redirect::stderr(log).context("redirecting stderr")?,
61-
));
62-
Ok(())
63-
}
64-
6554
pub(crate) fn prepare(config: &mut Config) -> anyhow::Result<()> {
66-
redirect_output(config)?;
6755
dump_lib()?;
6856
set_sources(config)?;
6957
dump_cargo_manifest()?;

rust/ql/test/library-tests/variables/options

Lines changed: 0 additions & 1 deletion
This file was deleted.

rust/tools/qltest.cmd

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
@echo off
22

33
set "RUST_BACKTRACE=full"
4+
set "QLTEST_LOG=%CODEQL_EXTRACTOR_RUST_LOG_DIR%/qltest.log"
45

5-
type NUL && "%CODEQL_EXTRACTOR_RUST_ROOT%/tools/%CODEQL_PLATFORM%/extractor" --qltest
6+
type NUL && "%CODEQL_EXTRACTOR_RUST_ROOT%/tools/%CODEQL_PLATFORM%/extractor" --qltest >"%QLTEST_LOG%"
67

7-
exit /b %ERRORLEVEL%
8+
if %ERRORLEVEL% neq 0 (
9+
type "%QLTEST_LOG%"
10+
exit /b 1
11+
)

rust/tools/qltest.sh

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
set -eu
44

55
export RUST_BACKTRACE=full
6-
7-
"$CODEQL_EXTRACTOR_RUST_ROOT/tools/$CODEQL_PLATFORM/extractor" --qltest
6+
QLTEST_LOG="$CODEQL_EXTRACTOR_RUST_LOG_DIR"/qltest.log
7+
if ! "$CODEQL_EXTRACTOR_RUST_ROOT/tools/$CODEQL_PLATFORM/extractor" --qltest &>> "$QLTEST_LOG"; then
8+
cat "$QLTEST_LOG"
9+
exit 1
10+
fi

0 commit comments

Comments
 (0)