1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//! Hide internal rules when documenting `macro_rules!` macros.
//!
//! When generating docs for `macro_rules!` macros, `rustdoc` will include every
//! rule, including internal rules that are only supposed to be called from within
//! your macro. The `clean_docs` attribute will hide your internal rules from
//! `rustdoc`.
//!
//! # Example:
//! ```
//! # use clean_macro_docs::clean_docs;
//! #[macro_export]
//! macro_rules! messy {
//!     (@impl $e:expr) => {
//!         format!("{}", $e)
//!     };
//!     ($e:expr) => {
//!         messy!(@impl $e)
//!     };
//! }
//!
//! #[clean_docs]
//! #[macro_export]
//! macro_rules! clean {
//!     (@impl $e:expr) => {
//!         format!("{}", $e)
//!     };
//!     ($e:expr) => {
//!         clean!(@impl $e)
//!     };
//! }
//! ```
//!
//! would be documented as
//! ```
//! macro_rules! mac {
//!     ($e:expr) => { ... };
//! }
//! ```
//! # How does it work?
//! The `clean!` macro above is transformed into
//! ```
//! #[macro_export]
//! macro_rules! clean {
//!     ($e:expr) => {
//!         $crate::__clean!(@impl $e)
//!     };
//! }
//!
//! #[macro_export]
//! macro_rules! __clean {
//!     (@impl $e:expr) => {
//!         format!("{}", $e)
//!     };
//! }
//!
//! macro_rules! clean {
//!     (@impl $e:expr) => {
//!         format!("{}", $e)
//!     };
//!     ($e:expr) => {
//!         clean!(@impl $e)
//!     };
//! }
//! ```
//!
//! The last, non-`macro_export`ed macro is there becuase Rust doesn't allow
//! macro-expanded macros to be invoked by absolute path (i.e. `$crate::__clean`).
//!
//! The solution is to shadow the `macro_export`ed macro with a local version
//! that doesn't use absolute paths.
//!
//! # Arguments
//! You can use these optional arguments to configure `clean_macro`.
//!
//! ```
//! # use clean_macro_docs::clean_docs;
//! #[clean_docs(impl = "#internal", internal = "__internal_mac")]
//! # macro_rules! mac { () => {} }
//! ```
//!
//! ## `impl`
//! A string representing the "flag" at the begining of an internal rule. Defaults to `"@"`.
//!
//! ```
//! # use clean_macro_docs::clean_docs;
//! #[clean_docs(impl = "#internal")]
//! #[macro_export]
//! macro_rules! mac {
//!     (#internal $e:expr) => {
//!         format!("{}", $e)
//!     };
//!     ($e:expr) => {
//!         mac!(#internal $e)
//!     };
//! }
//! ```
//!
//! ## `internal`
//! A string representing the identifier to use for the internal version of your macro.
//! By default `clean_docs` prepends `__` (two underscores) to the main macro's identifier.
//!
//! ```
//! # use clean_macro_docs::clean_docs;
//! #[clean_docs(internal = "__internal_mac")]
//! #[macro_export]
//! macro_rules! mac {
//!     (@impl $e:expr) => {
//!         format!("{}", $e)
//!     };
//!     ($e:expr) => {
//!         mac!(@impl $e)
//!     };
//! }
//! ```

extern crate proc_macro;
extern crate proc_macro2;

use proc_macro2::{Punct, Spacing, TokenStream, TokenTree};
use quote::{format_ident, quote, quote_spanned};
use std::str::FromStr;
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::{parse_macro_input, AttributeArgs, Ident, Lit, Meta, NestedMeta, Token};

mod macro_rules;
mod replace_macro_invocs;

use macro_rules::*;
use replace_macro_invocs::replace_macro_invocs;

#[proc_macro_attribute]
pub fn clean_docs(
    args: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    let args = parse_macro_input!(args as AttributeArgs);
    let mac_rules = parse_macro_input!(item as MacroRules);
    clean_docs_impl(args, mac_rules).into()
}

fn clean_docs_impl(args: AttributeArgs, mut mac_rules: MacroRules) -> TokenStream {
    let mut priv_marker: Option<TokenStream> = None;
    let mut priv_ident: Option<Ident> = None;

    for arg in args {
        if let NestedMeta::Meta(Meta::NameValue(arg)) = arg {
            match arg
                .path
                .get_ident()
                .map(Ident::to_string)
                .as_ref()
                .map(String::as_str)
            {
                Some("impl") => {
                    if let Lit::Str(val) = &arg.lit {
                        priv_marker = Some({
                            if let Ok(priv_marker) = TokenStream::from_str(&val.value()) {
                                priv_marker
                            } else {
                                return quote_spanned! {
                                    arg.lit.span()=> compile_error!("invalid tokens");
                                };
                            }
                        })
                    } else {
                        return quote_spanned! {
                            arg.lit.span()=> compile_error!("expected string");
                        };
                    }
                }
                Some("internal") => {
                    if let Lit::Str(val) = &arg.lit {
                        priv_ident = Some({
                            if let Ok(priv_ident) = val.parse() {
                                priv_ident
                            } else {
                                return quote_spanned! {
                                    arg.lit.span()=> compile_error!("expected identifier");
                                };
                            }
                        })
                    } else {
                        return quote_spanned! {
                            arg.lit.span()=> compile_error!("expected string");
                        };
                    }
                }
                _ => {
                    let arg_path = &arg.path;
                    let arg_str = quote!(#arg_path).to_string();
                    return quote_spanned! {
                        arg.span()=> compile_error!(concat!("invalid argument: ", #arg_str));
                    };
                }
            };
        } else {
            let arg_str = quote!(#arg).to_string();
            return quote_spanned! {
                arg.span()=> compile_error!(concat!("invalid argument: ", #arg_str));
            };
        }
    }

    // Clone item, to be reimitted unmodified without #[macro_export]
    let mut original = mac_rules.clone();

    let pub_ident = &mac_rules.ident;

    // Default values
    let priv_marker = priv_marker
        .unwrap_or_else(|| TokenStream::from(TokenTree::Punct(Punct::new('@', Spacing::Joint))));
    let priv_ident = priv_ident.unwrap_or_else(|| format_ident!("__{}", pub_ident));

    let mut pub_rules = Punctuated::<MacroRulesRule, Token![;]>::new();
    let mut priv_rules = Punctuated::<MacroRulesRule, Token![;]>::new();

    for mut rule in mac_rules.rules {
        rule.body = replace_macro_invocs(rule.body, pub_ident, &priv_ident, &priv_marker);
        if rule.rule.to_string().starts_with(&priv_marker.to_string()) {
            priv_rules.push(rule);
        } else {
            pub_rules.push(rule);
        }
    }

    if pub_rules.is_empty() {
        return quote! {
            compile_error!("no public rules");
        };
    }

    if priv_rules.is_empty() {
        return quote! {
            #original
        };
    }

    if original.rules.trailing_punct() {
        priv_rules.push_punct(<Token![;]>::default());
        pub_rules.push_punct(<Token![;]>::default());
    }

    mac_rules.rules = pub_rules;

    let mut priv_mac_rules = MacroRules {
        ident: priv_ident,
        rules: priv_rules,
        ..mac_rules.clone()
    };

    // Remove doc comments (and other doc attrs) from private version
    priv_mac_rules.attrs.retain(|attr| {
        if let Some(ident) = attr.path.get_ident() {
            ident.to_string() != "doc"
        } else {
            true
        }
    });

    // Remove #[macro_export] and doc comments (and other doc attrs) from crate-internal version
    original.attrs.retain(|attr| {
        if let Some(ident) = attr.path.get_ident() {
            ident.to_string() != "macro_export" && ident.to_string() != "doc"
        } else {
            true
        }
    });

    let gen = quote! {
        #mac_rules
        #[doc(hidden)]
        #priv_mac_rules

        #[allow(unused_macros)]
        #original
    };
    gen.into()
}

#[cfg(test)]
mod tests;