Skip to content

Commit 3e24444

Browse files
committed
Cacheproc-macro dlls
1 parent ef67e0a commit 3e24444

File tree

4 files changed

+70
-58
lines changed

4 files changed

+70
-58
lines changed

crates/ra_proc_macro_srv/src/cli.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
//! Driver for proc macro server
22
3-
use crate::{expand_task, list_macros};
3+
use crate::ProcMacroSrv;
44
use ra_proc_macro::msg::{self, Message};
55
use std::io;
66

77
pub fn run() -> io::Result<()> {
8+
let mut srv = ProcMacroSrv::default();
9+
810
while let Some(req) = read_request()? {
911
let res = match req {
10-
msg::Request::ListMacro(task) => Ok(msg::Response::ListMacro(list_macros(&task))),
12+
msg::Request::ListMacro(task) => srv.list_macros(&task).map(msg::Response::ListMacro),
1113
msg::Request::ExpansionMacro(task) => {
12-
expand_task(&task).map(msg::Response::ExpansionMacro)
14+
srv.expand(&task).map(msg::Response::ExpansionMacro)
1315
}
1416
};
1517

crates/ra_proc_macro_srv/src/dylib.rs

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl ProcMacroLibraryLibloading {
112112
type ProcMacroLibraryImpl = ProcMacroLibraryLibloading;
113113

114114
pub struct Expander {
115-
libs: Vec<ProcMacroLibraryImpl>,
115+
inner: ProcMacroLibraryImpl,
116116
}
117117

118118
impl Expander {
@@ -125,7 +125,7 @@ impl Expander {
125125

126126
let library = ProcMacroLibraryImpl::open(&lib).map_err(|e| e.to_string())?;
127127

128-
Ok(Expander { libs: vec![library] })
128+
Ok(Expander { inner: library })
129129
}
130130

131131
pub fn expand(
@@ -141,48 +141,46 @@ impl Expander {
141141
TokenStream::with_subtree(attr.clone())
142142
});
143143

144-
for lib in &self.libs {
145-
for proc_macro in &lib.exported_macros {
146-
match proc_macro {
147-
bridge::client::ProcMacro::CustomDerive { trait_name, client, .. }
148-
if *trait_name == macro_name =>
149-
{
150-
let res = client.run(
151-
&crate::proc_macro::bridge::server::SameThread,
152-
crate::rustc_server::Rustc::default(),
153-
parsed_body,
154-
);
155-
return res.map(|it| it.subtree);
156-
}
157-
bridge::client::ProcMacro::Bang { name, client } if *name == macro_name => {
158-
let res = client.run(
159-
&crate::proc_macro::bridge::server::SameThread,
160-
crate::rustc_server::Rustc::default(),
161-
parsed_body,
162-
);
163-
return res.map(|it| it.subtree);
164-
}
165-
bridge::client::ProcMacro::Attr { name, client } if *name == macro_name => {
166-
let res = client.run(
167-
&crate::proc_macro::bridge::server::SameThread,
168-
crate::rustc_server::Rustc::default(),
169-
parsed_attributes,
170-
parsed_body,
171-
);
172-
return res.map(|it| it.subtree);
173-
}
174-
_ => continue,
144+
for proc_macro in &self.inner.exported_macros {
145+
match proc_macro {
146+
bridge::client::ProcMacro::CustomDerive { trait_name, client, .. }
147+
if *trait_name == macro_name =>
148+
{
149+
let res = client.run(
150+
&crate::proc_macro::bridge::server::SameThread,
151+
crate::rustc_server::Rustc::default(),
152+
parsed_body,
153+
);
154+
return res.map(|it| it.subtree);
155+
}
156+
bridge::client::ProcMacro::Bang { name, client } if *name == macro_name => {
157+
let res = client.run(
158+
&crate::proc_macro::bridge::server::SameThread,
159+
crate::rustc_server::Rustc::default(),
160+
parsed_body,
161+
);
162+
return res.map(|it| it.subtree);
163+
}
164+
bridge::client::ProcMacro::Attr { name, client } if *name == macro_name => {
165+
let res = client.run(
166+
&crate::proc_macro::bridge::server::SameThread,
167+
crate::rustc_server::Rustc::default(),
168+
parsed_attributes,
169+
parsed_body,
170+
);
171+
return res.map(|it| it.subtree);
175172
}
173+
_ => continue,
176174
}
177175
}
178176

179177
Err(bridge::PanicMessage::String("Nothing to expand".to_string()))
180178
}
181179

182180
pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> {
183-
self.libs
181+
self.inner
182+
.exported_macros
184183
.iter()
185-
.flat_map(|it| &it.exported_macros)
186184
.map(|proc_macro| match proc_macro {
187185
bridge::client::ProcMacro::CustomDerive { trait_name, .. } => {
188186
(trait_name.to_string(), ProcMacroKind::CustomDerive)

crates/ra_proc_macro_srv/src/lib.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,40 @@ mod dylib;
2121

2222
use proc_macro::bridge::client::TokenStream;
2323
use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask};
24-
use std::path::Path;
25-
26-
pub(crate) fn expand_task(task: &ExpansionTask) -> Result<ExpansionResult, String> {
27-
let expander = create_expander(&task.lib);
24+
use std::{
25+
collections::{hash_map::Entry, HashMap},
26+
path::{Path, PathBuf},
27+
};
28+
29+
#[derive(Default)]
30+
pub(crate) struct ProcMacroSrv {
31+
expanders: HashMap<PathBuf, dylib::Expander>,
32+
}
2833

29-
match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) {
30-
Ok(expansion) => Ok(ExpansionResult { expansion }),
31-
Err(msg) => {
32-
Err(format!("Cannot perform expansion for {}: error {:?}", &task.macro_name, msg))
34+
impl ProcMacroSrv {
35+
pub fn expand(&mut self, task: &ExpansionTask) -> Result<ExpansionResult, String> {
36+
let expander = self.expander(&task.lib)?;
37+
match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) {
38+
Ok(expansion) => Ok(ExpansionResult { expansion }),
39+
Err(msg) => {
40+
Err(format!("Cannot perform expansion for {}: error {:?}", &task.macro_name, msg))
41+
}
3342
}
3443
}
35-
}
36-
37-
pub(crate) fn list_macros(task: &ListMacrosTask) -> ListMacrosResult {
38-
let expander = create_expander(&task.lib);
3944

40-
ListMacrosResult { macros: expander.list_macros() }
41-
}
45+
pub fn list_macros(&mut self, task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
46+
let expander = self.expander(&task.lib)?;
47+
Ok(ListMacrosResult { macros: expander.list_macros() })
48+
}
4249

43-
fn create_expander(lib: &Path) -> dylib::Expander {
44-
dylib::Expander::new(lib)
45-
.unwrap_or_else(|err| panic!("Cannot create expander for {}: {:?}", lib.display(), err))
50+
fn expander(&mut self, path: &Path) -> Result<&dylib::Expander, String> {
51+
Ok(match self.expanders.entry(path.to_path_buf()) {
52+
Entry::Vacant(v) => v.insert(dylib::Expander::new(path).map_err(|err| {
53+
format!("Cannot create expander for {}: {:?}", path.display(), err)
54+
})?),
55+
Entry::Occupied(e) => e.into_mut(),
56+
})
57+
}
4658
}
4759

4860
pub mod cli;

crates/ra_proc_macro_srv/src/tests/utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! utils used in proc-macro tests
22
33
use crate::dylib;
4-
use crate::list_macros;
4+
use crate::ProcMacroSrv;
55
pub use difference::Changeset as __Changeset;
66
use ra_proc_macro::ListMacrosTask;
77
use std::str::FromStr;
@@ -59,7 +59,7 @@ pub fn assert_expand(
5959
pub fn list(crate_name: &str, version: &str) -> Vec<String> {
6060
let path = fixtures::dylib_path(crate_name, version);
6161
let task = ListMacrosTask { lib: path };
62-
63-
let res = list_macros(&task);
62+
let mut srv = ProcMacroSrv::default();
63+
let res = srv.list_macros(&task).unwrap();
6464
res.macros.into_iter().map(|(name, kind)| format!("{} [{:?}]", name, kind)).collect()
6565
}

0 commit comments

Comments
 (0)