-
Notifications
You must be signed in to change notification settings - Fork 930
Format modules defined inside cfg_if macro calls #3600
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b9bb32e
Format modules defined inside cfg_if macro calls
topecongiro 14c32b1
Rename methods and add doc comments
topecongiro 85ce271
Visit nested cfg_if!
topecongiro cf34155
Add a test for cfg_if
topecongiro 905ad0b
Fix a broken test on Windows
topecongiro 74f7eeb
Bit more refactorings
topecongiro b656bfd
Avoid cloning nodes from the AST
topecongiro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use syntax::ast; | ||
use syntax::parse::token::{DelimToken, Token}; | ||
use syntax::parse::{stream_to_parser_with_base_dir, Directory, ParseSess}; | ||
use syntax::symbol::kw; | ||
use syntax::visit::Visitor; | ||
use syntax_pos::Symbol; | ||
|
||
pub(crate) struct ModItem { | ||
pub(crate) item: ast::Item, | ||
} | ||
|
||
/// Traverse `cfg_if!` macro and fetch modules. | ||
pub(crate) struct CfgIfVisitor<'a> { | ||
parse_sess: &'a ParseSess, | ||
mods: Vec<ModItem>, | ||
base_dir: Directory<'a>, | ||
} | ||
|
||
impl<'a> CfgIfVisitor<'a> { | ||
pub(crate) fn new(parse_sess: &'a ParseSess, base_dir: Directory<'a>) -> CfgIfVisitor<'a> { | ||
CfgIfVisitor { | ||
mods: vec![], | ||
parse_sess, | ||
base_dir, | ||
} | ||
} | ||
|
||
pub(crate) fn mods(self) -> Vec<ModItem> { | ||
self.mods | ||
} | ||
} | ||
|
||
impl<'a, 'ast: 'a> Visitor<'ast> for CfgIfVisitor<'a> { | ||
fn visit_mac(&mut self, mac: &'ast ast::Mac) { | ||
match self.visit_mac_inner(mac) { | ||
Ok(()) => (), | ||
Err(e) => debug!("{}", e), | ||
scampi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
impl<'a, 'ast: 'a> CfgIfVisitor<'a> { | ||
fn visit_mac_inner(&mut self, mac: &'ast ast::Mac) -> Result<(), &'static str> { | ||
if mac.node.path != Symbol::intern("cfg_if") { | ||
return Err("Expected cfg_if"); | ||
} | ||
|
||
let mut parser = stream_to_parser_with_base_dir( | ||
self.parse_sess, | ||
mac.node.tts.clone(), | ||
self.base_dir.clone(), | ||
); | ||
parser.cfg_mods = false; | ||
let mut process_if_cfg = true; | ||
|
||
while parser.token != Token::Eof { | ||
if process_if_cfg { | ||
if !parser.eat_keyword(kw::If) { | ||
return Err("Expected `if`"); | ||
} | ||
parser | ||
.parse_attribute(false) | ||
.map_err(|_| "Failed to parse attributes")?; | ||
} | ||
|
||
if !parser.eat(&Token::OpenDelim(DelimToken::Brace)) { | ||
return Err("Expected an opening brace"); | ||
} | ||
|
||
while parser.token != Token::CloseDelim(DelimToken::Brace) && parser.token != Token::Eof | ||
{ | ||
let item = match parser.parse_item() { | ||
Ok(Some(item_ptr)) => item_ptr.into_inner(), | ||
Ok(None) => continue, | ||
Err(mut err) => { | ||
err.cancel(); | ||
parser.sess.span_diagnostic.reset_err_count(); | ||
return Err( | ||
"Expected item inside cfg_if block, but failed to parse it as an item", | ||
); | ||
} | ||
}; | ||
if let ast::ItemKind::Mod(..) = item.node { | ||
self.mods.push(ModItem { item }); | ||
} | ||
} | ||
|
||
if !parser.eat(&Token::CloseDelim(DelimToken::Brace)) { | ||
return Err("Expected a closing brace"); | ||
} | ||
|
||
if parser.eat(&Token::Eof) { | ||
break; | ||
} | ||
|
||
if !parser.eat_keyword(kw::Else) { | ||
return Err("Expected `else`"); | ||
} | ||
|
||
process_if_cfg = parser.token.is_keyword(kw::If); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.