From 3e24444aee9db36d8de530dee9c0283ce793c6fd Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 24 Apr 2020 10:23:01 +0800 Subject: Cacheproc-macro dlls --- crates/ra_proc_macro_srv/src/cli.rs | 8 ++-- crates/ra_proc_macro_srv/src/dylib.rs | 68 ++++++++++++++--------------- crates/ra_proc_macro_srv/src/lib.rs | 46 +++++++++++-------- crates/ra_proc_macro_srv/src/tests/utils.rs | 6 +-- 4 files changed, 70 insertions(+), 58 deletions(-) diff --git a/crates/ra_proc_macro_srv/src/cli.rs b/crates/ra_proc_macro_srv/src/cli.rs index 7282e5b9b..1437794c9 100644 --- a/crates/ra_proc_macro_srv/src/cli.rs +++ b/crates/ra_proc_macro_srv/src/cli.rs @@ -1,15 +1,17 @@ //! Driver for proc macro server -use crate::{expand_task, list_macros}; +use crate::ProcMacroSrv; use ra_proc_macro::msg::{self, Message}; use std::io; pub fn run() -> io::Result<()> { + let mut srv = ProcMacroSrv::default(); + while let Some(req) = read_request()? { let res = match req { - msg::Request::ListMacro(task) => Ok(msg::Response::ListMacro(list_macros(&task))), + msg::Request::ListMacro(task) => srv.list_macros(&task).map(msg::Response::ListMacro), msg::Request::ExpansionMacro(task) => { - expand_task(&task).map(msg::Response::ExpansionMacro) + srv.expand(&task).map(msg::Response::ExpansionMacro) } }; diff --git a/crates/ra_proc_macro_srv/src/dylib.rs b/crates/ra_proc_macro_srv/src/dylib.rs index d202eb0fd..476bc5c01 100644 --- a/crates/ra_proc_macro_srv/src/dylib.rs +++ b/crates/ra_proc_macro_srv/src/dylib.rs @@ -112,7 +112,7 @@ impl ProcMacroLibraryLibloading { type ProcMacroLibraryImpl = ProcMacroLibraryLibloading; pub struct Expander { - libs: Vec, + inner: ProcMacroLibraryImpl, } impl Expander { @@ -125,7 +125,7 @@ impl Expander { let library = ProcMacroLibraryImpl::open(&lib).map_err(|e| e.to_string())?; - Ok(Expander { libs: vec![library] }) + Ok(Expander { inner: library }) } pub fn expand( @@ -141,38 +141,36 @@ impl Expander { TokenStream::with_subtree(attr.clone()) }); - for lib in &self.libs { - for proc_macro in &lib.exported_macros { - match proc_macro { - bridge::client::ProcMacro::CustomDerive { trait_name, client, .. } - if *trait_name == macro_name => - { - let res = client.run( - &crate::proc_macro::bridge::server::SameThread, - crate::rustc_server::Rustc::default(), - parsed_body, - ); - return res.map(|it| it.subtree); - } - bridge::client::ProcMacro::Bang { name, client } if *name == macro_name => { - let res = client.run( - &crate::proc_macro::bridge::server::SameThread, - crate::rustc_server::Rustc::default(), - parsed_body, - ); - return res.map(|it| it.subtree); - } - bridge::client::ProcMacro::Attr { name, client } if *name == macro_name => { - let res = client.run( - &crate::proc_macro::bridge::server::SameThread, - crate::rustc_server::Rustc::default(), - parsed_attributes, - parsed_body, - ); - return res.map(|it| it.subtree); - } - _ => continue, + for proc_macro in &self.inner.exported_macros { + match proc_macro { + bridge::client::ProcMacro::CustomDerive { trait_name, client, .. } + if *trait_name == macro_name => + { + let res = client.run( + &crate::proc_macro::bridge::server::SameThread, + crate::rustc_server::Rustc::default(), + parsed_body, + ); + return res.map(|it| it.subtree); + } + bridge::client::ProcMacro::Bang { name, client } if *name == macro_name => { + let res = client.run( + &crate::proc_macro::bridge::server::SameThread, + crate::rustc_server::Rustc::default(), + parsed_body, + ); + return res.map(|it| it.subtree); + } + bridge::client::ProcMacro::Attr { name, client } if *name == macro_name => { + let res = client.run( + &crate::proc_macro::bridge::server::SameThread, + crate::rustc_server::Rustc::default(), + parsed_attributes, + parsed_body, + ); + return res.map(|it| it.subtree); } + _ => continue, } } @@ -180,9 +178,9 @@ impl Expander { } pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> { - self.libs + self.inner + .exported_macros .iter() - .flat_map(|it| &it.exported_macros) .map(|proc_macro| match proc_macro { bridge::client::ProcMacro::CustomDerive { trait_name, .. } => { (trait_name.to_string(), ProcMacroKind::CustomDerive) diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs index 3aca859db..0954bd6b5 100644 --- a/crates/ra_proc_macro_srv/src/lib.rs +++ b/crates/ra_proc_macro_srv/src/lib.rs @@ -21,28 +21,40 @@ mod dylib; use proc_macro::bridge::client::TokenStream; use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask}; -use std::path::Path; - -pub(crate) fn expand_task(task: &ExpansionTask) -> Result { - let expander = create_expander(&task.lib); +use std::{ + collections::{hash_map::Entry, HashMap}, + path::{Path, PathBuf}, +}; + +#[derive(Default)] +pub(crate) struct ProcMacroSrv { + expanders: HashMap, +} - match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) { - Ok(expansion) => Ok(ExpansionResult { expansion }), - Err(msg) => { - Err(format!("Cannot perform expansion for {}: error {:?}", &task.macro_name, msg)) +impl ProcMacroSrv { + pub fn expand(&mut self, task: &ExpansionTask) -> Result { + let expander = self.expander(&task.lib)?; + match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) { + Ok(expansion) => Ok(ExpansionResult { expansion }), + Err(msg) => { + Err(format!("Cannot perform expansion for {}: error {:?}", &task.macro_name, msg)) + } } } -} - -pub(crate) fn list_macros(task: &ListMacrosTask) -> ListMacrosResult { - let expander = create_expander(&task.lib); - ListMacrosResult { macros: expander.list_macros() } -} + pub fn list_macros(&mut self, task: &ListMacrosTask) -> Result { + let expander = self.expander(&task.lib)?; + Ok(ListMacrosResult { macros: expander.list_macros() }) + } -fn create_expander(lib: &Path) -> dylib::Expander { - dylib::Expander::new(lib) - .unwrap_or_else(|err| panic!("Cannot create expander for {}: {:?}", lib.display(), err)) + fn expander(&mut self, path: &Path) -> Result<&dylib::Expander, String> { + Ok(match self.expanders.entry(path.to_path_buf()) { + Entry::Vacant(v) => v.insert(dylib::Expander::new(path).map_err(|err| { + format!("Cannot create expander for {}: {:?}", path.display(), err) + })?), + Entry::Occupied(e) => e.into_mut(), + }) + } } pub mod cli; diff --git a/crates/ra_proc_macro_srv/src/tests/utils.rs b/crates/ra_proc_macro_srv/src/tests/utils.rs index 2139ec7a4..646a427c5 100644 --- a/crates/ra_proc_macro_srv/src/tests/utils.rs +++ b/crates/ra_proc_macro_srv/src/tests/utils.rs @@ -1,7 +1,7 @@ //! utils used in proc-macro tests use crate::dylib; -use crate::list_macros; +use crate::ProcMacroSrv; pub use difference::Changeset as __Changeset; use ra_proc_macro::ListMacrosTask; use std::str::FromStr; @@ -59,7 +59,7 @@ pub fn assert_expand( pub fn list(crate_name: &str, version: &str) -> Vec { let path = fixtures::dylib_path(crate_name, version); let task = ListMacrosTask { lib: path }; - - let res = list_macros(&task); + let mut srv = ProcMacroSrv::default(); + let res = srv.list_macros(&task).unwrap(); res.macros.into_iter().map(|(name, kind)| format!("{} [{:?}]", name, kind)).collect() } -- cgit v1.2.3