Skip to content

Commit

Permalink
source injector: add the crate
Browse files Browse the repository at this point in the history
This crate takes an absolute path to a rustc repo and adds path-dependencies
that point towards the respective rustc subcrates into the Cargo.tomls of
the clippy and clippy_lints crate.

This allows rustc-analyzer to show proper type annotations etc on rustc-internals inside the clippy repo.

Usage: ./souce_injector/target/debug/souce_injector /absolute/path/to/rust/

cc rust-lang/rust-analyzer#3517
cc #5514
  • Loading branch information
matthiaskrgr committed May 27, 2020
1 parent ee3088f commit 2ba355c
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ out
/clippy_workspace_tests/target
/clippy_dev/target
/rustc_tools_util/target
/souce_injector/target

# Generated by dogfood
/target_recur/
Expand Down
9 changes: 9 additions & 0 deletions souce_injector/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "souce_injector"
version = "0.1.0"
authors = ["Matthias Krüger <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
93 changes: 93 additions & 0 deletions souce_injector/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use std::env;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;

// This crate takes an absolute path to a rustc repo and alters the dependencies to point towards
// the respective rustc subcrates instead of using extern crate xyz.
// This allows rust analyzer to analyze rustc internals and show proper information inside clippy
// code. See https://github.com/rust-lang/rust-clippy/issues/5514 and https://github.com/rust-lang/rust-clippy/issues/5514 for details

fn main() -> std::io::Result<()> {
// first arg is the path to a rust repo
let rustc_path = env::args()
.nth(1)
.expect("First argument must be the path to a rustc repo root");

let rustc_path = PathBuf::from(rustc_path);
assert!(rustc_path.is_dir(), "path is not a directory");
let rustc_source_basedir = rustc_path.join("src");
assert!(
rustc_source_basedir.is_dir(),
"are you sure the path leads to a rustc repo?"
);

let clippy_root_manifest = fs::read_to_string("Cargo.toml").expect("failed to read ./Cargo.toml");
let clippy_root_lib_rs = fs::read_to_string("src/driver.rs").expect("failed to read ./src/driver.rs");
inject_deps_into_manifest(
&rustc_source_basedir,
"Cargo.toml",
clippy_root_manifest,
clippy_root_lib_rs,
)?;

let clippy_lints_manifest =
fs::read_to_string("clippy_lints/Cargo.toml").expect("failed to read ./clippy_lints/Cargo.toml");
let clippy_lints_lib_rs =
fs::read_to_string("clippy_lints/src/lib.rs").expect("failed to read ./clippy_lints/src/lib.rs");
inject_deps_into_manifest(
&rustc_source_basedir,
"clippy_lints/Cargo.toml",
clippy_lints_manifest,
clippy_lints_lib_rs,
)?;

Ok(())
}

fn inject_deps_into_manifest(
rustc_source_dir: &PathBuf,
manifest_path: &str,
cargo_toml: String,
lib_rs: String,
) -> std::io::Result<()> {
let extern_crates = lib_rs
.lines()
// get the deps
.filter(|line| line.starts_with("extern crate"))
// we have something like "extern crate foo;", we only care about the "foo"
// ↓ ↓
// extern crate rustc_middle;
.map(|s| &s[13..(s.len() - 1)]);

let new_deps = extern_crates.map(|dep| {
// format the dependencies that are going to be put inside the Cargo.toml
format!(
"{dep} = {{ path = \"{source_path}/lib{dep}\" }}\n",
dep = dep,
source_path = rustc_source_dir.display()
)
});

// format a new [dependencies]-block with the new deps we need to inject
let mut all_deps = String::from("[dependencies]\n");
new_deps.for_each(|dep_line| {
all_deps.push_str(&dep_line);
});

// replace "[dependencies]" with
// [dependencies]
// dep1 = { path = ... }
// dep2 = { path = ... }
// etc
let new_manifest = cargo_toml.replacen("[dependencies]\n", &all_deps, 1);

// println!("{}", new_manifest);
let mut file = File::create(manifest_path)?;
file.write_all(new_manifest.as_bytes())?;

println!("Dependency paths injected: {}", manifest_path);

Ok(())
}

0 comments on commit 2ba355c

Please sign in to comment.