mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
Rust: run cargo fmt
This commit is contained in:
@@ -0,0 +1 @@
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::fs;
|
||||
use crate::path;
|
||||
use anyhow;
|
||||
use log::{debug, warn};
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub struct Archiver {
|
||||
pub root: PathBuf
|
||||
pub root: PathBuf,
|
||||
}
|
||||
|
||||
impl Archiver {
|
||||
@@ -20,7 +20,7 @@ impl Archiver {
|
||||
dest.push(path::key(source));
|
||||
let parent = dest.parent().unwrap();
|
||||
if fs::metadata(&dest).is_ok() {
|
||||
return Ok(())
|
||||
return Ok(());
|
||||
}
|
||||
fs::create_dir_all(parent)?;
|
||||
fs::copy(source, dest)?;
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
use std::path::PathBuf;
|
||||
use anyhow::Context;
|
||||
use serde::{Deserialize, Serialize, Serializer, Deserializer};
|
||||
use serde_with;
|
||||
use figment::{Figment, providers::{Env, Serialized}};
|
||||
use clap::{Parser, ArgAction, ValueEnum};
|
||||
use clap::builder::PossibleValue;
|
||||
use clap::{ArgAction, Parser, ValueEnum};
|
||||
use codeql_extractor::trap;
|
||||
use figment::{
|
||||
providers::{Env, Serialized},
|
||||
Figment,
|
||||
};
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
use serde_with;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Default, Serialize, Deserialize, Clone, Copy, ValueEnum)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
@@ -65,11 +68,14 @@ impl Config {
|
||||
let mut cli_args = CliArgs::parse();
|
||||
if let Some(inputs_file) = cli_args.inputs_file.take() {
|
||||
let inputs_list = std::fs::read_to_string(inputs_file).context("reading file list")?;
|
||||
cli_args.inputs.extend(inputs_list.split("\n").map(PathBuf::from));
|
||||
cli_args
|
||||
.inputs
|
||||
.extend(inputs_list.split("\n").map(PathBuf::from));
|
||||
}
|
||||
Ok(Figment::new()
|
||||
.merge(Env::prefixed("CODEQL_EXTRACTOR_RUST_"))
|
||||
.merge(Serialized::defaults(cli_args))
|
||||
.extract().context("loading configuration")?)
|
||||
.extract()
|
||||
.context("loading configuration")?)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
use std::fs;
|
||||
use std::path::{PathBuf, Path};
|
||||
use ra_ap_project_model::CargoConfig;
|
||||
use crate::trap::TrapId;
|
||||
use anyhow::Context;
|
||||
use log;
|
||||
use ra_ap_hir::db::DefDatabase;
|
||||
use ra_ap_hir::{Crate, ModuleDefId};
|
||||
use ra_ap_hir::AdtId::{EnumId, UnionId, StructId};
|
||||
use ra_ap_hir::sym::ge;
|
||||
use ra_ap_hir::AdtId::{EnumId, StructId, UnionId};
|
||||
use ra_ap_hir::{Crate, ModuleDefId};
|
||||
use ra_ap_load_cargo::{load_workspace_at, LoadCargoConfig, ProcMacroServerChoice};
|
||||
use crate::trap::{TrapId};
|
||||
use ra_ap_project_model::CargoConfig;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
mod config;
|
||||
pub mod trap;
|
||||
pub mod generated;
|
||||
mod translate;
|
||||
mod archive;
|
||||
mod config;
|
||||
pub mod generated;
|
||||
pub mod path;
|
||||
mod translate;
|
||||
pub mod trap;
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let cfg = config::Config::extract().context("failed to load configuration")?;
|
||||
@@ -25,9 +25,13 @@ fn main() -> anyhow::Result<()> {
|
||||
.init()?;
|
||||
log::info!("{cfg:?}");
|
||||
let traps = trap::TrapFileProvider::new(&cfg).context("failed to set up trap files")?;
|
||||
let archiver = archive::Archiver { root: cfg.source_archive_dir };
|
||||
let archiver = archive::Archiver {
|
||||
root: cfg.source_archive_dir,
|
||||
};
|
||||
|
||||
let config = CargoConfig { ..Default::default() };
|
||||
let config = CargoConfig {
|
||||
..Default::default()
|
||||
};
|
||||
let no_progress = |_| ();
|
||||
let load_config = LoadCargoConfig {
|
||||
load_out_dirs_from_check: true,
|
||||
@@ -35,20 +39,28 @@ fn main() -> anyhow::Result<()> {
|
||||
prefill_caches: false,
|
||||
};
|
||||
for input in cfg.inputs {
|
||||
let (db, vfs, _macro_server) = load_workspace_at(&input, &config, &load_config, &no_progress).context("loading inputs")?;
|
||||
let crates = <dyn DefDatabase>::crate_graph(&db);
|
||||
let (db, vfs, _macro_server) =
|
||||
load_workspace_at(&input, &config, &load_config, &no_progress)
|
||||
.context("loading inputs")?;
|
||||
let crates = <dyn DefDatabase>::crate_graph(&db);
|
||||
for crate_id in crates.iter() {
|
||||
let krate = Crate::from(crate_id);
|
||||
let name = krate.display_name(&db);
|
||||
let crate_name = name.as_ref().map(|n| n.canonical_name().as_str()).unwrap_or("");
|
||||
let trap = traps.create("crates", &PathBuf::from(format!("/{}_{}", crate_name, crate_id.into_raw().into_u32())));
|
||||
translate::CrateTranslator::new(
|
||||
&db,
|
||||
trap,
|
||||
&krate,
|
||||
&vfs,
|
||||
&archiver,
|
||||
).emit_crate().context("writing trap file")?;
|
||||
let crate_name = name
|
||||
.as_ref()
|
||||
.map(|n| n.canonical_name().as_str())
|
||||
.unwrap_or("");
|
||||
let trap = traps.create(
|
||||
"crates",
|
||||
&PathBuf::from(format!(
|
||||
"/{}_{}",
|
||||
crate_name,
|
||||
crate_id.into_raw().into_u32()
|
||||
)),
|
||||
);
|
||||
translate::CrateTranslator::new(&db, trap, &krate, &vfs, &archiver)
|
||||
.emit_crate()
|
||||
.context("writing trap file")?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
use std::path::{absolute, Path, PathBuf};
|
||||
use std::fs::canonicalize;
|
||||
use std::path::{absolute, Path, PathBuf};
|
||||
|
||||
pub fn key(p: &Path) -> PathBuf {
|
||||
let normalized = canonicalize(p).or_else(|_| absolute(p)).unwrap_or_else(|_| p.into());
|
||||
let root = normalized.ancestors().last().unwrap(); // ancestors always yields at least one
|
||||
normalized.strip_prefix(root).unwrap().into() // stripping an ancestor always works
|
||||
let normalized = canonicalize(p)
|
||||
.or_else(|_| absolute(p))
|
||||
.unwrap_or_else(|_| p.into());
|
||||
let root = normalized.ancestors().last().unwrap(); // ancestors always yields at least one
|
||||
normalized.strip_prefix(root).unwrap().into() // stripping an ancestor always works
|
||||
}
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
use crate::archive::Archiver;
|
||||
use crate::trap::{AsTrapKeyPart, TrapFile, TrapId};
|
||||
use crate::{generated, trap_key};
|
||||
use anyhow;
|
||||
use codeql_extractor::trap;
|
||||
use ra_ap_hir::HasSource;
|
||||
use ra_ap_hir::{Crate, Module, ModuleDef};
|
||||
use ra_ap_ide_db::line_index::LineIndex;
|
||||
use ra_ap_ide_db::{LineIndexDatabase, RootDatabase};
|
||||
use ra_ap_syntax::ast::HasName;
|
||||
use ra_ap_syntax::AstNode;
|
||||
use ra_ap_vfs::{AbsPath, FileId, Vfs};
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::path::{PathBuf};
|
||||
use crate::trap::{TrapFile, TrapId, AsTrapKeyPart};
|
||||
use crate::{generated, trap_key};
|
||||
use ra_ap_hir::{Crate, Module, ModuleDef};
|
||||
use anyhow;
|
||||
use ra_ap_hir::{HasSource};
|
||||
use ra_ap_vfs::{AbsPath, FileId, Vfs};
|
||||
use ra_ap_syntax::ast::HasName;
|
||||
use crate::archive::Archiver;
|
||||
use std::io::Result;
|
||||
use std::path::PathBuf;
|
||||
use triomphe::Arc;
|
||||
use ra_ap_ide_db::{LineIndexDatabase, RootDatabase};
|
||||
use ra_ap_ide_db::line_index::LineIndex;
|
||||
use ra_ap_syntax::AstNode;
|
||||
use codeql_extractor::trap;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct FileData {
|
||||
@@ -30,7 +30,6 @@ pub struct CrateTranslator<'a> {
|
||||
file_labels: HashMap<PathBuf, FileData>,
|
||||
}
|
||||
|
||||
|
||||
impl CrateTranslator<'_> {
|
||||
pub fn new<'a>(
|
||||
db: &'a RootDatabase,
|
||||
@@ -56,9 +55,13 @@ impl CrateTranslator<'_> {
|
||||
self.archiver.archive(&canonical);
|
||||
canonical = fs::canonicalize(&canonical).unwrap_or(canonical);
|
||||
let name = canonical.to_string_lossy();
|
||||
let label = self.trap.emit(generated::DbFile { id: trap_key!["file;", name.as_ref()], name: String::from(name) });
|
||||
let label = self.trap.emit(generated::DbFile {
|
||||
id: trap_key!["file;", name.as_ref()],
|
||||
name: String::from(name),
|
||||
});
|
||||
let line_index = <dyn LineIndexDatabase>::line_index(self.db, file_id);
|
||||
self.file_labels.insert(canonical.clone(), FileData { label, line_index });
|
||||
self.file_labels
|
||||
.insert(canonical.clone(), FileData { label, line_index });
|
||||
}
|
||||
self.file_labels.get(&canonical).cloned()
|
||||
})
|
||||
@@ -68,7 +71,8 @@ impl CrateTranslator<'_> {
|
||||
where
|
||||
T::Ast: AstNode,
|
||||
{
|
||||
entity.source(self.db)
|
||||
entity
|
||||
.source(self.db)
|
||||
.and_then(|source| source.file_id.file_id().map(|f| (f.file_id(), source)))
|
||||
.and_then(|(file_id, source)| self.emit_file(file_id).map(|data| (data, source)))
|
||||
.and_then(|(data, source)| {
|
||||
@@ -79,7 +83,12 @@ impl CrateTranslator<'_> {
|
||||
})
|
||||
}
|
||||
|
||||
fn emit_definition(&mut self, module_label: trap::Label, id: ModuleDef, labels: &mut Vec<trap::Label>) {
|
||||
fn emit_definition(
|
||||
&mut self,
|
||||
module_label: trap::Label,
|
||||
id: ModuleDef,
|
||||
labels: &mut Vec<trap::Label>,
|
||||
) {
|
||||
match id {
|
||||
ModuleDef::Module(_) => {}
|
||||
ModuleDef::Function(function) => {
|
||||
@@ -115,7 +124,7 @@ impl CrateTranslator<'_> {
|
||||
});
|
||||
}
|
||||
|
||||
pub fn emit_crate(&mut self) -> std::io::Result<()> {
|
||||
pub fn emit_crate(&mut self) -> std::io::Result<()> {
|
||||
self.emit_file(self.krate.root_file(self.db));
|
||||
let mut map = HashMap::<Module, trap::Label>::new();
|
||||
for module in self.krate.modules(self.db) {
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
use crate::config::Compression;
|
||||
use crate::generated;
|
||||
use crate::{config, path};
|
||||
use codeql_extractor::trap;
|
||||
use log::{debug, trace};
|
||||
use ra_ap_ide_db::line_index::LineCol;
|
||||
use std::ffi::OsString;
|
||||
use std::fmt::{Debug, Display, Formatter};
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
use log::{debug, trace};
|
||||
use crate::{config, path};
|
||||
use codeql_extractor::trap;
|
||||
use ra_ap_ide_db::line_index::LineCol;
|
||||
use crate::config::Compression;
|
||||
use crate::generated;
|
||||
|
||||
//TODO: typed labels
|
||||
pub trait AsTrapKeyPart {
|
||||
@@ -81,7 +81,12 @@ pub struct TrapFile {
|
||||
}
|
||||
|
||||
impl TrapFile {
|
||||
pub fn emit_location(&mut self, file_label: trap::Label, start: LineCol, end: LineCol) -> trap::Label {
|
||||
pub fn emit_location(
|
||||
&mut self,
|
||||
file_label: trap::Label,
|
||||
start: LineCol,
|
||||
end: LineCol,
|
||||
) -> trap::Label {
|
||||
let start_line = start.line as usize;
|
||||
let start_column = start.col as usize;
|
||||
let end_line = end.line as usize;
|
||||
@@ -119,7 +124,8 @@ impl TrapFile {
|
||||
|
||||
pub fn commit(&self) -> std::io::Result<()> {
|
||||
std::fs::create_dir_all(self.path.parent().unwrap())?;
|
||||
self.writer.write_to_file(&self.path, self.compression.into())
|
||||
self.writer
|
||||
.write_to_file(&self.path, self.compression.into())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,11 +147,15 @@ impl TrapFileProvider {
|
||||
pub fn create(&self, category: &str, key: &Path) -> TrapFile {
|
||||
let mut path = PathBuf::from(category);
|
||||
path.push(path::key(key));
|
||||
path.set_extension(path.extension().map(|e| {
|
||||
let mut o: OsString = e.to_owned();
|
||||
o.push(".trap");
|
||||
o
|
||||
}).unwrap_or("trap".into()));
|
||||
path.set_extension(
|
||||
path.extension()
|
||||
.map(|e| {
|
||||
let mut o: OsString = e.to_owned();
|
||||
o.push(".trap");
|
||||
o
|
||||
})
|
||||
.unwrap_or("trap".into()),
|
||||
);
|
||||
debug!("creating trap file {}", path.display());
|
||||
path = self.trap_dir.join(path);
|
||||
TrapFile {
|
||||
|
||||
Reference in New Issue
Block a user