Skip to content

Commit

Permalink
Merge pull request #106 from qryxip/support-proconio
Browse files Browse the repository at this point in the history
Support proconio
  • Loading branch information
qryxip authored May 2, 2021
2 parents b01ecfc + 71868b7 commit 9837693
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 10 deletions.
59 changes: 55 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,60 @@
# Changelog

## [Unreleased]

### Fixed

- Recognizes `#[macro_export(local_inner_macros)]`.

- Fixed a problem where `$crate`s are not replaced with `$crate::lib_name` in a certain case.

- Fixed a problem where `--remove` option could not be used for CRLF code.

With the above 3 fixtures, you can bundle [proconio](https://docs.rs/crate/proconio).

```console
❯ cat <<EOF >./src/main.rs
heredoc> #[macro_use]
heredoc> extern crate proconio as _;
heredoc>
heredoc> #[fastout]
heredoc> fn main() {
heredoc> input!(abs: [(u64, u64)]);
heredoc> for (a, b) in abs {
heredoc> println!("{}", a + b);
heredoc> }
heredoc> }
heredoc> EOF
❯ cargo equip \
> --resolve-cfgs \
> --remove docs \
> --minify libs \
> --rustfmt \
> --check \
> -o ./bundled.rs
❯ ./run-cargo-equip.bash -o ./bundled.rs
Running `/home/ryo/.cargo/bin/rustup run nightly cargo udeps --output json -p bundle-proconio --bin bundle-proconio`
Checking bundle-proconio v0.1.0 (/home/ryo/src/local/bundle-proconio)
Finished dev [unoptimized + debuginfo] target(s) in 0.45s
info: Loading save analysis from "/home/ryo/src/local/bundle-proconio/target/debug/deps/save-analysis/bundle_proconio-31a013a4acd96cad.json"
Running `/home/ryo/.cargo/bin/rustup run stable-x86_64-unknown-linux-gnu cargo check --message-format json -p 'bundle-proconio:0.1.0' --bin bundle-proconio`
Checking bundle-proconio v0.1.0 (/home/ryo/src/local/bundle-proconio)
Finished dev [unoptimized + debuginfo] target(s) in 0.44s
Spawning `/home/ryo/.cache/cargo-equip/rust-analyzer-2021-03-29 proc-macro`
Readied `#[derive_readable]`
Readied `#[fastout]`
Bundling the code
warning: found `crate` paths. replacing them with `crate::proconio`
Reading the license file of `lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)`
Reading the license file of `proconio 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)`
Checking cargo-equip-check-output-fs0en4z4r1d3gd3e v0.1.0 (/tmp/cargo-equip-check-output-fs0en4z4r1d3gd3e)
Finished dev [unoptimized + debuginfo] target(s) in 0.19s
❯ stat -c %s ./bundled.rs
18024
```

[Submission Info #46571 - Library Checker](https://judge.yosupo.jp/submission/46571)

## [0.12.0] - 2021-05-01Z

### Added
Expand Down Expand Up @@ -48,10 +103,6 @@

- cargo-equip won't error for unresolved `extern crate` items.

### Fixed

- Recognizes `#[macro_export(local_inner_macros)]`.

## [0.11.1] - 2021-03-30Z

### Added
Expand Down
19 changes: 13 additions & 6 deletions src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use maplit::{btreemap, btreeset};
use proc_macro2::{LineColumn, Spacing, Span, TokenStream, TokenTree};
use quote::{quote, ToTokens};
use std::{
borrow::Cow,
collections::{BTreeMap, BTreeSet, VecDeque},
env, fs, mem,
ops::Range,
Expand Down Expand Up @@ -753,13 +754,13 @@ pub(crate) fn process_extern_crates_in_lib(

pub(crate) fn modify_macros(code: &str, pseudo_extern_crate_name: &str) -> anyhow::Result<String> {
fn find_dollar_crates(token_stream: TokenStream, acc: &mut BTreeSet<LineColumn>) {
for (i, (tt1, tt2)) in token_stream.into_iter().tuple_windows().enumerate() {
if i == 0 {
if let proc_macro2::TokenTree::Group(group) = &tt1 {
find_dollar_crates(group.stream(), acc);
}
}
let mut token_stream = token_stream.into_iter().peekable();

if let Some(proc_macro2::TokenTree::Group(group)) = token_stream.peek() {
find_dollar_crates(group.stream(), acc);
}

for (tt1, tt2) in token_stream.tuple_windows() {
if let proc_macro2::TokenTree::Group(group) = &tt2 {
find_dollar_crates(group.stream(), acc);
}
Expand Down Expand Up @@ -1248,6 +1249,12 @@ fn erase(
code: &str,
visit_file: fn(&mut [FixedBitSet], TokenStream) -> syn::Result<()>,
) -> anyhow::Result<String> {
let code = &if code.contains("\r\n") {
Cow::from(code.replace("\r\n", "\n"))
} else {
Cow::from(code)
};

let code = if code.starts_with("#!") {
let (_, code) = code.split_at(code.find('\n').unwrap_or_else(|| code.len()));
code
Expand Down

0 comments on commit 9837693

Please sign in to comment.