Skip to content

Commit 10d8cb9

Browse files
Merge #3958
3958: Add proc-macro related config and tests r=matklad a=edwin0cheng This PR do the following things: 1. Add cli argument `proc-macro` for running proc-macro server. 2. Added support for proc-macro in bench and analysis-stats 3. Added typescript config for proc-macros 4. Added an heavy test for proc-macros. To test it out: 1. run `cargo xtask install --proc-macro` 2. add `"rust-analyzer.cargo.loadOutDirsFromCheck": true"` and `"rust-analyzer.procMacro.enabled": true"` in vs code config. [Edit] Change to use `rust-analyzer proc-macro` for running proc-macro standalone process. Co-authored-by: Edwin Cheng <[email protected]>
2 parents a4cda3e + 16a74cf commit 10d8cb9

File tree

17 files changed

+208
-29
lines changed

17 files changed

+208
-29
lines changed

Cargo.lock

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

crates/ra_proc_macro/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod msg;
1212
use process::{ProcMacroProcessSrv, ProcMacroProcessThread};
1313
use ra_tt::{SmolStr, Subtree};
1414
use std::{
15+
ffi::OsStr,
1516
path::{Path, PathBuf},
1617
sync::Arc,
1718
};
@@ -56,8 +57,15 @@ pub struct ProcMacroClient {
5657
}
5758

5859
impl ProcMacroClient {
59-
pub fn extern_process(process_path: &Path) -> Result<ProcMacroClient, std::io::Error> {
60-
let (thread, process) = ProcMacroProcessSrv::run(process_path)?;
60+
pub fn extern_process<I, S>(
61+
process_path: &Path,
62+
args: I,
63+
) -> Result<ProcMacroClient, std::io::Error>
64+
where
65+
I: IntoIterator<Item = S>,
66+
S: AsRef<OsStr>,
67+
{
68+
let (thread, process) = ProcMacroProcessSrv::run(process_path, args)?;
6169
Ok(ProcMacroClient {
6270
kind: ProcMacroClientKind::Process { process: Arc::new(process), thread },
6371
})

crates/ra_proc_macro/src/process.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTas
99
use io::{BufRead, BufReader};
1010
use std::{
1111
convert::{TryFrom, TryInto},
12+
ffi::OsStr,
1213
io::{self, Write},
1314
path::{Path, PathBuf},
1415
process::{Child, Command, Stdio},
@@ -44,8 +45,13 @@ impl Drop for Process {
4445
}
4546

4647
impl Process {
47-
fn run(process_path: &Path) -> Result<Process, io::Error> {
48+
fn run<I, S>(process_path: &Path, args: I) -> Result<Process, io::Error>
49+
where
50+
I: IntoIterator<Item = S>,
51+
S: AsRef<OsStr>,
52+
{
4853
let child = Command::new(process_path.clone())
54+
.args(args)
4955
.stdin(Stdio::piped())
5056
.stdout(Stdio::piped())
5157
.stderr(Stdio::null())
@@ -74,10 +80,15 @@ impl Process {
7480
}
7581

7682
impl ProcMacroProcessSrv {
77-
pub fn run(
83+
pub fn run<I, S>(
7884
process_path: &Path,
79-
) -> Result<(ProcMacroProcessThread, ProcMacroProcessSrv), io::Error> {
80-
let process = Process::run(process_path)?;
85+
args: I,
86+
) -> Result<(ProcMacroProcessThread, ProcMacroProcessSrv), io::Error>
87+
where
88+
I: IntoIterator<Item = S>,
89+
S: AsRef<OsStr>,
90+
{
91+
let process = Process::run(process_path, args)?;
8192

8293
let (task_tx, task_rx) = bounded(0);
8394
let handle = jod_thread::spawn(move || {

crates/ra_proc_macro_srv/src/main.rs renamed to crates/ra_proc_macro_srv/src/cli.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Driver for proc macro server
22
3+
use crate::{expand_task, list_macros};
34
use ra_proc_macro::msg::{self, Message};
4-
use ra_proc_macro_srv::{expand_task, list_macros};
55

66
use std::io;
77

@@ -24,7 +24,8 @@ fn write_response(res: Result<msg::Response, String>) -> Result<(), io::Error> {
2424
let mut stdout = stdout.lock();
2525
msg.write(&mut stdout)
2626
}
27-
fn main() {
27+
28+
pub fn run() {
2829
loop {
2930
let req = match read_request() {
3031
Err(err) => {

crates/ra_proc_macro_srv/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ mod dylib;
2222
use proc_macro::bridge::client::TokenStream;
2323
use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask};
2424

25-
pub fn expand_task(task: &ExpansionTask) -> Result<ExpansionResult, String> {
25+
pub(crate) fn expand_task(task: &ExpansionTask) -> Result<ExpansionResult, String> {
2626
let expander = dylib::Expander::new(&task.lib)
2727
.expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib));
2828

@@ -39,7 +39,7 @@ pub fn expand_task(task: &ExpansionTask) -> Result<ExpansionResult, String> {
3939
}
4040
}
4141

42-
pub fn list_macros(task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
42+
pub(crate) fn list_macros(task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
4343
let expander = dylib::Expander::new(&task.lib)
4444
.expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib));
4545

@@ -53,5 +53,7 @@ pub fn list_macros(task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
5353
}
5454
}
5555

56+
pub mod cli;
57+
5658
#[cfg(test)]
5759
mod tests;

crates/rust-analyzer/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ ra_db = { path = "../ra_db" }
4646
hir = { path = "../ra_hir", package = "ra_hir" }
4747
hir_def = { path = "../ra_hir_def", package = "ra_hir_def" }
4848
hir_ty = { path = "../ra_hir_ty", package = "ra_hir_ty" }
49-
49+
ra_proc_macro_srv = { path = "../ra_proc_macro_srv" }
5050

5151
[target.'cfg(windows)'.dependencies]
5252
winapi = "0.3.8"

crates/rust-analyzer/src/bin/args.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,23 @@ pub(crate) enum Command {
2929
with_deps: bool,
3030
path: PathBuf,
3131
load_output_dirs: bool,
32+
with_proc_macro: bool,
3233
},
3334
Bench {
3435
path: PathBuf,
3536
what: BenchWhat,
3637
load_output_dirs: bool,
38+
with_proc_macro: bool,
3739
},
3840
Diagnostics {
3941
path: PathBuf,
4042
load_output_dirs: bool,
43+
with_proc_macro: bool,
4144
/// Include files which are not modules. In rust-analyzer
4245
/// this would include the parser test files.
4346
all: bool,
4447
},
48+
ProcMacro,
4549
RunServer,
4650
Version,
4751
}
@@ -148,6 +152,7 @@ FLAGS:
148152
-h, --help Prints help information
149153
--memory-usage
150154
--load-output-dirs Load OUT_DIR values by running `cargo check` before analysis
155+
--with-proc-macro Use ra-proc-macro-srv for proc-macro expanding
151156
-v, --verbose
152157
-q, --quiet
153158
@@ -165,6 +170,7 @@ ARGS:
165170
let only: Option<String> = matches.opt_value_from_str(["-o", "--only"])?;
166171
let with_deps: bool = matches.contains("--with-deps");
167172
let load_output_dirs = matches.contains("--load-output-dirs");
173+
let with_proc_macro = matches.contains("--with-proc-macro");
168174
let path = {
169175
let mut trailing = matches.free()?;
170176
if trailing.len() != 1 {
@@ -173,7 +179,15 @@ ARGS:
173179
trailing.pop().unwrap().into()
174180
};
175181

176-
Command::Stats { randomize, memory_usage, only, with_deps, path, load_output_dirs }
182+
Command::Stats {
183+
randomize,
184+
memory_usage,
185+
only,
186+
with_deps,
187+
path,
188+
load_output_dirs,
189+
with_proc_macro,
190+
}
177191
}
178192
"analysis-bench" => {
179193
if matches.contains(["-h", "--help"]) {
@@ -187,6 +201,7 @@ USAGE:
187201
FLAGS:
188202
-h, --help Prints help information
189203
--load-output-dirs Load OUT_DIR values by running `cargo check` before analysis
204+
--with-proc-macro Use ra-proc-macro-srv for proc-macro expanding
190205
-v, --verbose
191206
192207
OPTIONS:
@@ -214,7 +229,8 @@ ARGS:
214229
),
215230
};
216231
let load_output_dirs = matches.contains("--load-output-dirs");
217-
Command::Bench { path, what, load_output_dirs }
232+
let with_proc_macro = matches.contains("--with-proc-macro");
233+
Command::Bench { path, what, load_output_dirs, with_proc_macro }
218234
}
219235
"diagnostics" => {
220236
if matches.contains(["-h", "--help"]) {
@@ -237,6 +253,7 @@ ARGS:
237253
}
238254

239255
let load_output_dirs = matches.contains("--load-output-dirs");
256+
let with_proc_macro = matches.contains("--with-proc-macro");
240257
let all = matches.contains("--all");
241258
let path = {
242259
let mut trailing = matches.free()?;
@@ -246,8 +263,9 @@ ARGS:
246263
trailing.pop().unwrap().into()
247264
};
248265

249-
Command::Diagnostics { path, load_output_dirs, all }
266+
Command::Diagnostics { path, load_output_dirs, with_proc_macro, all }
250267
}
268+
"proc-macro" => Command::ProcMacro,
251269
_ => {
252270
eprintln!(
253271
"\

crates/rust-analyzer/src/bin/main.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ fn main() -> Result<()> {
2525
with_deps,
2626
path,
2727
load_output_dirs,
28+
with_proc_macro,
2829
} => cli::analysis_stats(
2930
args.verbosity,
3031
memory_usage,
@@ -33,16 +34,24 @@ fn main() -> Result<()> {
3334
with_deps,
3435
randomize,
3536
load_output_dirs,
37+
with_proc_macro,
3638
)?,
3739

38-
args::Command::Bench { path, what, load_output_dirs } => {
39-
cli::analysis_bench(args.verbosity, path.as_ref(), what, load_output_dirs)?
40+
args::Command::Bench { path, what, load_output_dirs, with_proc_macro } => {
41+
cli::analysis_bench(
42+
args.verbosity,
43+
path.as_ref(),
44+
what,
45+
load_output_dirs,
46+
with_proc_macro,
47+
)?
4048
}
4149

42-
args::Command::Diagnostics { path, load_output_dirs, all } => {
43-
cli::diagnostics(path.as_ref(), load_output_dirs, all)?
50+
args::Command::Diagnostics { path, load_output_dirs, with_proc_macro, all } => {
51+
cli::diagnostics(path.as_ref(), load_output_dirs, with_proc_macro, all)?
4452
}
4553

54+
args::Command::ProcMacro => run_proc_macro_sv()?,
4655
args::Command::RunServer => run_server()?,
4756
args::Command::Version => println!("rust-analyzer {}", env!("REV")),
4857
}
@@ -56,6 +65,11 @@ fn setup_logging() -> Result<()> {
5665
Ok(())
5766
}
5867

68+
fn run_proc_macro_sv() -> Result<()> {
69+
ra_proc_macro_srv::cli::run();
70+
Ok(())
71+
}
72+
5973
fn run_server() -> Result<()> {
6074
log::info!("lifecycle: server started");
6175

crates/rust-analyzer/src/cli/analysis_bench.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ pub fn analysis_bench(
4747
path: &Path,
4848
what: BenchWhat,
4949
load_output_dirs: bool,
50+
with_proc_macro: bool,
5051
) -> Result<()> {
5152
ra_prof::init();
5253

5354
let start = Instant::now();
5455
eprint!("loading: ");
55-
let (mut host, roots) = load_cargo(path, load_output_dirs)?;
56+
let (mut host, roots) = load_cargo(path, load_output_dirs, with_proc_macro)?;
5657
let db = host.raw_database();
5758
eprintln!("{:?}\n", start.elapsed());
5859

crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ pub fn analysis_stats(
2525
with_deps: bool,
2626
randomize: bool,
2727
load_output_dirs: bool,
28+
with_proc_macro: bool,
2829
) -> Result<()> {
2930
let db_load_time = Instant::now();
30-
let (mut host, roots) = load_cargo(path, load_output_dirs)?;
31+
let (mut host, roots) = load_cargo(path, load_output_dirs, with_proc_macro)?;
3132
let db = host.raw_database();
3233
println!("Database loaded, {} roots, {:?}", roots.len(), db_load_time.elapsed());
3334
let analysis_time = Instant::now();

0 commit comments

Comments
 (0)