Skip to content
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

Parsing a whole crate with particular cfgs defined #588

Closed
gnzlbg opened this issue Mar 3, 2019 · 4 comments
Closed

Parsing a whole crate with particular cfgs defined #588

gnzlbg opened this issue Mar 3, 2019 · 4 comments

Comments

@gnzlbg
Copy link
Contributor

gnzlbg commented Mar 3, 2019

I was thinking about porting the ctest crate (used by libc) to syn, mainly because it uses the latest release of the syntex_syntax crate which does not support repr(transparent), and that blocks adding APIs that could benefit from repr(transparent) to libc.

I have a couple of questions and am not sure where is the best place to ask them.

The syntex_syntax crate has a parse_crate function, that given a file, continues walking the directory tree parsing modules, it can do a certain level of macro expansion while doing this (e.g. cfg(unix), etc. where these macros are defined in the session).

I was wondering if syn has something similar, or what would be the best way of achieving the same thing.

I thought that maybe I could parse a single file, then do a fold on the file that expands some macros, then visit all the modules, resolve their paths in the file-system, and repeat. Something like this seems widely-useful enough that it might be worth it to implement it in syn directly instead.

E.g. the ctest crate defines, for each target-triple, which macros should be expanded, but that sounds manual and brittle. It might be better to somehow hook up rustc_target and obtain this information from there.

@gnzlbg gnzlbg changed the title Porting ctest from syntex_syntax to syn Parsing a whole crate with particular cfgs defined Mar 3, 2019
@dtolnay
Copy link
Owner

dtolnay commented Mar 3, 2019

Syn is only a parser, so other aspects of compilation like expanding macros, dealing with target-specific configuration properties, crawling the filesystem, etc are out of scope.

https://github.com/TedDriggs/syn-inline-mod looks like it handles some of what you need.

@gnzlbg
Copy link
Contributor Author

gnzlbg commented Mar 3, 2019

I see, I'll check that out, thanks!

@gnzlbg gnzlbg closed this as completed Mar 3, 2019
@gnzlbg
Copy link
Contributor Author

gnzlbg commented Mar 4, 2019

@dtolnay is there a crate for expanding macros ? Like in the project you mention:

#[cfg(foo)] 
mod a;

is expanded to

#[cfg(foo)]
mod a {
   ...a contents...
}

This gives me an AST for the whole crate, but now I'd like to define some macros, and expand them, e.g., removing all those cfgs, cfg_if!, etc.

@dtolnay
Copy link
Owner

dtolnay commented Mar 4, 2019

I don't know of such a crate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants