From 72bba9882889b2e20fd91e3c6c3a97debbbe6543 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 18 Apr 2020 19:29:04 +0800 Subject: Merge empty delim subtree in proc-macro --- crates/ra_proc_macro_srv/src/rustc_server.rs | 11 ++++++++++- .../src/tests/fixtures/test_serialize_proc_macro.txt | 3 +-- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'crates/ra_proc_macro_srv/src') diff --git a/crates/ra_proc_macro_srv/src/rustc_server.rs b/crates/ra_proc_macro_srv/src/rustc_server.rs index ec0d35692..9fcfdc450 100644 --- a/crates/ra_proc_macro_srv/src/rustc_server.rs +++ b/crates/ra_proc_macro_srv/src/rustc_server.rs @@ -76,7 +76,16 @@ impl Extend for TokenStream { impl Extend for TokenStream { fn extend>(&mut self, streams: I) { for item in streams { - self.subtree.token_trees.extend(&mut item.into_iter()) + for tkn in item { + match tkn { + tt::TokenTree::Subtree(subtree) if subtree.delimiter.is_none() => { + self.subtree.token_trees.extend(subtree.token_trees); + } + _ => { + self.subtree.token_trees.push(tkn); + } + } + } } } } diff --git a/crates/ra_proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt b/crates/ra_proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt index 24507d98d..1f5d940fa 100644 --- a/crates/ra_proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt +++ b/crates/ra_proc_macro_srv/src/tests/fixtures/test_serialize_proc_macro.txt @@ -25,8 +25,7 @@ SUBTREE $ SUBTREE () 4294967295 IDENT feature 4294967295 PUNCH = [alone] 4294967295 - SUBTREE $ - LITERAL "cargo-clippy" 0 + LITERAL "cargo-clippy" 0 PUNCH , [alone] 4294967295 IDENT allow 4294967295 SUBTREE () 4294967295 -- cgit v1.2.3 From d3019164dcbb46f8c369ed4efff79de5a42a95a8 Mon Sep 17 00:00:00 2001 From: veetaha Date: Mon, 20 Apr 2020 21:26:10 +0300 Subject: ra_proc_macro: cleanups here and there --- crates/ra_proc_macro_srv/src/cli.rs | 58 ++++++--------- crates/ra_proc_macro_srv/src/dylib.rs | 107 ++++++++++++--------------- crates/ra_proc_macro_srv/src/lib.rs | 36 ++++----- crates/ra_proc_macro_srv/src/rustc_server.rs | 2 +- crates/ra_proc_macro_srv/src/tests/utils.rs | 2 +- 5 files changed, 86 insertions(+), 119 deletions(-) (limited to 'crates/ra_proc_macro_srv/src') diff --git a/crates/ra_proc_macro_srv/src/cli.rs b/crates/ra_proc_macro_srv/src/cli.rs index c771f2b38..5f1f3ba3c 100644 --- a/crates/ra_proc_macro_srv/src/cli.rs +++ b/crates/ra_proc_macro_srv/src/cli.rs @@ -2,55 +2,43 @@ use crate::{expand_task, list_macros}; use ra_proc_macro::msg::{self, Message}; - use std::io; -fn read_request() -> Result, io::Error> { - let stdin = io::stdin(); - let mut stdin = stdin.lock(); - msg::Request::read(&mut stdin) -} - -fn write_response(res: Result) -> Result<(), io::Error> { - let msg: msg::Response = match res { - Ok(res) => res, - Err(err) => msg::Response::Error(msg::ResponseError { - code: msg::ErrorCode::ExpansionError, - message: err, - }), - }; - - let stdout = io::stdout(); - let mut stdout = stdout.lock(); - msg.write(&mut stdout) -} - pub fn run() { loop { let req = match read_request() { Err(err) => { - eprintln!("Read message error on ra_proc_macro_srv: {}", err.to_string()); + eprintln!("Read message error on ra_proc_macro_srv: {}", err); continue; } Ok(None) => continue, Ok(Some(req)) => req, }; - match req { - msg::Request::ListMacro(task) => { - if let Err(err) = - write_response(list_macros(&task).map(|it| msg::Response::ListMacro(it))) - { - eprintln!("Write message error on list macro: {}", err); - } - } + let res = match req { + msg::Request::ListMacro(task) => Ok(msg::Response::ListMacro(list_macros(&task))), msg::Request::ExpansionMacro(task) => { - if let Err(err) = - write_response(expand_task(&task).map(|it| msg::Response::ExpansionMacro(it))) - { - eprintln!("Write message error on expansion macro: {}", err); - } + expand_task(&task).map(msg::Response::ExpansionMacro) } + }; + + let msg = res.unwrap_or_else(|err| { + msg::Response::Error(msg::ResponseError { + code: msg::ErrorCode::ExpansionError, + message: err, + }) + }); + + if let Err(err) = write_response(msg) { + eprintln!("Write message error: {}", err); } } } + +fn read_request() -> io::Result> { + msg::Request::read(&mut io::stdin().lock()) +} + +fn write_response(msg: msg::Response) -> io::Result<()> { + msg.write(&mut io::stdout().lock()) +} diff --git a/crates/ra_proc_macro_srv/src/dylib.rs b/crates/ra_proc_macro_srv/src/dylib.rs index 16bd7466e..f7a86a532 100644 --- a/crates/ra_proc_macro_srv/src/dylib.rs +++ b/crates/ra_proc_macro_srv/src/dylib.rs @@ -9,43 +9,37 @@ use libloading::Library; use memmap::Mmap; use ra_proc_macro::ProcMacroKind; -use std::io::Error as IoError; -use std::io::ErrorKind as IoErrorKind; +use std::io; const NEW_REGISTRAR_SYMBOL: &str = "_rustc_proc_macro_decls_"; -fn invalid_data_err(e: impl Into>) -> IoError { - IoError::new(IoErrorKind::InvalidData, e) +fn invalid_data_err(e: impl Into>) -> io::Error { + io::Error::new(io::ErrorKind::InvalidData, e) } -fn is_derive_registrar_symbol(symbol: &str) -> bool { +fn is_derive_registrar_symbol(symbol: &&str) -> bool { symbol.contains(NEW_REGISTRAR_SYMBOL) } -fn find_registrar_symbol(file: &Path) -> Result, IoError> { +fn find_registrar_symbol(file: &Path) -> io::Result> { let file = File::open(file)?; let buffer = unsafe { Mmap::map(&file)? }; let object = Object::parse(&buffer).map_err(invalid_data_err)?; - match object { + let name = match object { Object::Elf(elf) => { let symbols = elf.dynstrtab.to_vec().map_err(invalid_data_err)?; - let name = - symbols.iter().find(|s| is_derive_registrar_symbol(s)).map(|s| s.to_string()); - Ok(name) - } - Object::PE(pe) => { - let name = pe - .exports - .iter() - .flat_map(|s| s.name) - .find(|s| is_derive_registrar_symbol(s)) - .map(|s| s.to_string()); - Ok(name) + symbols.into_iter().find(is_derive_registrar_symbol).map(&str::to_owned) } + Object::PE(pe) => pe + .exports + .iter() + .flat_map(|s| s.name) + .find(is_derive_registrar_symbol) + .map(&str::to_owned), Object::Mach(Mach::Binary(binary)) => { let exports = binary.exports().map_err(invalid_data_err)?; - let name = exports + exports .iter() .map(|s| { // In macos doc: @@ -58,12 +52,12 @@ fn find_registrar_symbol(file: &Path) -> Result, IoError> { &s.name } }) - .find(|s| is_derive_registrar_symbol(s)) - .map(|s| s.to_string()); - Ok(name) + .find(is_derive_registrar_symbol) + .map(&str::to_owned) } - _ => Ok(None), - } + _ => return Ok(None), + }; + Ok(name) } /// Loads dynamic library in platform dependent manner. @@ -93,15 +87,16 @@ fn load_library(file: &Path) -> Result { } struct ProcMacroLibraryLibloading { - // Hold the dylib to prevent it for unloadeding + // Hold the dylib to prevent it from unloading _lib: Library, exported_macros: Vec, } impl ProcMacroLibraryLibloading { - fn open(file: &Path) -> Result { - let symbol_name = find_registrar_symbol(file)? - .ok_or(invalid_data_err(format!("Cannot find registrar symbol in file {:?}", file)))?; + fn open(file: &Path) -> io::Result { + let symbol_name = find_registrar_symbol(file)?.ok_or_else(|| { + invalid_data_err(format!("Cannot find registrar symbol in file {:?}", file)) + })?; let lib = load_library(file).map_err(invalid_data_err)?; let exported_macros = { @@ -121,18 +116,16 @@ pub struct Expander { } impl Expander { - pub fn new>(lib: &P) -> Result { - let mut libs = vec![]; - /* Some libraries for dynamic loading require canonicalized path (even when it is - already absolute - */ - let lib = - lib.as_ref().canonicalize().expect(&format!("Cannot canonicalize {:?}", lib.as_ref())); + pub fn new(lib: &Path) -> Result { + // Some libraries for dynamic loading require canonicalized path even when it is + // already absolute + let lib = lib + .canonicalize() + .unwrap_or_else(|err| panic!("Cannot canonicalize {:?}: {:?}", lib, err)); let library = ProcMacroLibraryImpl::open(&lib).map_err(|e| e.to_string())?; - libs.push(library); - Ok(Expander { libs }) + Ok(Expander { libs: vec![library] }) } pub fn expand( @@ -176,7 +169,6 @@ impl Expander { parsed_attributes, parsed_body, ); - return res.map(|it| it.subtree); } _ => continue, @@ -187,26 +179,21 @@ impl Expander { Err(bridge::PanicMessage::String("Nothing to expand".to_string())) } - pub fn list_macros(&self) -> Result, bridge::PanicMessage> { - let mut result = vec![]; - - for lib in &self.libs { - for proc_macro in &lib.exported_macros { - let res = match proc_macro { - bridge::client::ProcMacro::CustomDerive { trait_name, .. } => { - (trait_name.to_string(), ProcMacroKind::CustomDerive) - } - bridge::client::ProcMacro::Bang { name, .. } => { - (name.to_string(), ProcMacroKind::FuncLike) - } - bridge::client::ProcMacro::Attr { name, .. } => { - (name.to_string(), ProcMacroKind::Attr) - } - }; - result.push(res); - } - } - - Ok(result) + pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> { + self.libs + .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) + } + bridge::client::ProcMacro::Bang { name, .. } => { + (name.to_string(), ProcMacroKind::FuncLike) + } + bridge::client::ProcMacro::Attr { name, .. } => { + (name.to_string(), ProcMacroKind::Attr) + } + }) + .collect() } } diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs index c62b0ed89..7faf36834 100644 --- a/crates/ra_proc_macro_srv/src/lib.rs +++ b/crates/ra_proc_macro_srv/src/lib.rs @@ -3,10 +3,10 @@ //! This library is able to call compiled Rust custom derive dynamic libraries on arbitrary code. //! The general idea here is based on https://github.com/fedochet/rust-proc-macro-expander. //! -//! But we change some several design for fitting RA needs: +//! But we adapt it to better fit RA needs: //! -//! * We use `ra_tt` for proc-macro `TokenStream` server, it is easy to manipute and interact with -//! RA then proc-macro2 token stream. +//! * We use `ra_tt` for proc-macro `TokenStream` server, it is easy to manipulate and interact with +//! RA than `proc-macro2` token stream. //! * By **copying** the whole rustc `lib_proc_macro` code, we are able to build this with `stable` //! rustc rather than `unstable`. (Although in gerenal ABI compatibility is still an issue) @@ -21,36 +21,28 @@ 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 = dylib::Expander::new(&task.lib) - .expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib)); + let expander = create_expander(&task.lib); match expander.expand(&task.macro_name, &task.macro_body, task.attributes.as_ref()) { Ok(expansion) => Ok(ExpansionResult { expansion }), Err(msg) => { - let reason = format!( - "Cannot perform expansion for {}: error {:?}!", - &task.macro_name, - msg.as_str() - ); - Err(reason) + Err(format!("Cannot perform expansion for {}: error {:?}", &task.macro_name, msg)) } } } -pub(crate) fn list_macros(task: &ListMacrosTask) -> Result { - let expander = dylib::Expander::new(&task.lib) - .expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib)); +pub(crate) fn list_macros(task: &ListMacrosTask) -> ListMacrosResult { + let expander = create_expander(&task.lib); - match expander.list_macros() { - Ok(macros) => Ok(ListMacrosResult { macros }), - Err(msg) => { - let reason = - format!("Cannot perform expansion for {:?}: error {:?}!", &task.lib, msg.as_str()); - Err(reason) - } - } + 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, err)) } pub mod cli; diff --git a/crates/ra_proc_macro_srv/src/rustc_server.rs b/crates/ra_proc_macro_srv/src/rustc_server.rs index 9fcfdc450..f481d70b2 100644 --- a/crates/ra_proc_macro_srv/src/rustc_server.rs +++ b/crates/ra_proc_macro_srv/src/rustc_server.rs @@ -6,7 +6,7 @@ //! The original idea from fedochet is using proc-macro2 as backend, //! we use ra_tt instead for better intergation with RA. //! -//! FIXME: No span and source file informatin is implemented yet +//! FIXME: No span and source file information is implemented yet use crate::proc_macro::bridge::{self, server}; use ra_tt as tt; diff --git a/crates/ra_proc_macro_srv/src/tests/utils.rs b/crates/ra_proc_macro_srv/src/tests/utils.rs index 1ee409449..2139ec7a4 100644 --- a/crates/ra_proc_macro_srv/src/tests/utils.rs +++ b/crates/ra_proc_macro_srv/src/tests/utils.rs @@ -60,6 +60,6 @@ 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).unwrap(); + let res = list_macros(&task); res.macros.into_iter().map(|(name, kind)| format!("{} [{:?}]", name, kind)).collect() } -- cgit v1.2.3 From 982af2286a57667263f6cf0abb80b5e20b198047 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Mon, 20 Apr 2020 21:50:06 +0300 Subject: Display path not debug it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Laurențiu Nicola --- crates/ra_proc_macro_srv/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_proc_macro_srv/src') diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs index 7faf36834..178458703 100644 --- a/crates/ra_proc_macro_srv/src/lib.rs +++ b/crates/ra_proc_macro_srv/src/lib.rs @@ -42,7 +42,7 @@ pub(crate) fn list_macros(task: &ListMacrosTask) -> ListMacrosResult { fn create_expander(lib: &Path) -> dylib::Expander { dylib::Expander::new(lib) - .unwrap_or_else(|err| panic!("Cannot create expander for {:?}: {:?}", lib, err)) + .unwrap_or_else(|err| panic!("Cannot create expander for {}: {}", lib.display(), err)) } pub mod cli; -- cgit v1.2.3 From 0f5b1fef5ea0a34e2760383182e9683cdbd7d49e Mon Sep 17 00:00:00 2001 From: Veetaha Date: Mon, 20 Apr 2020 21:54:43 +0300 Subject: Display path not debug it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Laurențiu Nicola --- crates/ra_proc_macro_srv/src/dylib.rs | 14 +++++++------- crates/ra_proc_macro_srv/src/lib.rs | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) (limited to 'crates/ra_proc_macro_srv/src') diff --git a/crates/ra_proc_macro_srv/src/dylib.rs b/crates/ra_proc_macro_srv/src/dylib.rs index f7a86a532..d202eb0fd 100644 --- a/crates/ra_proc_macro_srv/src/dylib.rs +++ b/crates/ra_proc_macro_srv/src/dylib.rs @@ -17,7 +17,7 @@ fn invalid_data_err(e: impl Into>) -> i io::Error::new(io::ErrorKind::InvalidData, e) } -fn is_derive_registrar_symbol(symbol: &&str) -> bool { +fn is_derive_registrar_symbol(symbol: &str) -> bool { symbol.contains(NEW_REGISTRAR_SYMBOL) } @@ -29,13 +29,13 @@ fn find_registrar_symbol(file: &Path) -> io::Result> { let name = match object { Object::Elf(elf) => { let symbols = elf.dynstrtab.to_vec().map_err(invalid_data_err)?; - symbols.into_iter().find(is_derive_registrar_symbol).map(&str::to_owned) + symbols.into_iter().find(|s| is_derive_registrar_symbol(s)).map(&str::to_owned) } Object::PE(pe) => pe .exports .iter() .flat_map(|s| s.name) - .find(is_derive_registrar_symbol) + .find(|s| is_derive_registrar_symbol(s)) .map(&str::to_owned), Object::Mach(Mach::Binary(binary)) => { let exports = binary.exports().map_err(invalid_data_err)?; @@ -52,12 +52,12 @@ fn find_registrar_symbol(file: &Path) -> io::Result> { &s.name } }) - .find(is_derive_registrar_symbol) + .find(|s| is_derive_registrar_symbol(s)) .map(&str::to_owned) } _ => return Ok(None), }; - Ok(name) + return Ok(name); } /// Loads dynamic library in platform dependent manner. @@ -95,7 +95,7 @@ struct ProcMacroLibraryLibloading { impl ProcMacroLibraryLibloading { fn open(file: &Path) -> io::Result { let symbol_name = find_registrar_symbol(file)?.ok_or_else(|| { - invalid_data_err(format!("Cannot find registrar symbol in file {:?}", file)) + invalid_data_err(format!("Cannot find registrar symbol in file {}", file.display())) })?; let lib = load_library(file).map_err(invalid_data_err)?; @@ -121,7 +121,7 @@ impl Expander { // already absolute let lib = lib .canonicalize() - .unwrap_or_else(|err| panic!("Cannot canonicalize {:?}: {:?}", lib, err)); + .unwrap_or_else(|err| panic!("Cannot canonicalize {}: {:?}", lib.display(), err)); let library = ProcMacroLibraryImpl::open(&lib).map_err(|e| e.to_string())?; diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs index 178458703..f64e05a2d 100644 --- a/crates/ra_proc_macro_srv/src/lib.rs +++ b/crates/ra_proc_macro_srv/src/lib.rs @@ -42,7 +42,7 @@ pub(crate) fn list_macros(task: &ListMacrosTask) -> ListMacrosResult { fn create_expander(lib: &Path) -> dylib::Expander { dylib::Expander::new(lib) - .unwrap_or_else(|err| panic!("Cannot create expander for {}: {}", lib.display(), err)) + .unwrap_or_else(|err| panic!("Cannot create expander for {}: {:?}", lib.display(), err)) } pub mod cli; -- cgit v1.2.3 From 834960d841b667ad1c102ceeec8fbb20aaae099d Mon Sep 17 00:00:00 2001 From: Veetaha Date: Mon, 20 Apr 2020 22:07:47 +0300 Subject: Fix typo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Laurențiu Nicola --- crates/ra_proc_macro_srv/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_proc_macro_srv/src') diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs index f64e05a2d..3aca859db 100644 --- a/crates/ra_proc_macro_srv/src/lib.rs +++ b/crates/ra_proc_macro_srv/src/lib.rs @@ -5,7 +5,7 @@ //! //! But we adapt it to better fit RA needs: //! -//! * We use `ra_tt` for proc-macro `TokenStream` server, it is easy to manipulate and interact with +//! * We use `ra_tt` for proc-macro `TokenStream` server, it is easier to manipulate and interact with //! RA than `proc-macro2` token stream. //! * By **copying** the whole rustc `lib_proc_macro` code, we are able to build this with `stable` //! rustc rather than `unstable`. (Although in gerenal ABI compatibility is still an issue) -- cgit v1.2.3