1
Fork 0

strongly-typed passes

This commit is contained in:
QuietMisdreavus 2018-07-27 09:11:19 -05:00
parent 667fdc1ce4
commit aaec1014d8
8 changed files with 86 additions and 49 deletions

View file

@ -26,6 +26,7 @@
#![feature(entry_and_modify)]
#![feature(ptr_offset_from)]
#![feature(crate_visibility_modifier)]
#![feature(const_fn)]
#![recursion_limit="256"]
@ -367,8 +368,8 @@ fn main_args(args: &[String]) -> isize {
if matches.opt_strs("passes") == ["list"] {
println!("Available passes for running rustdoc:");
for &(name, _, description) in passes::PASSES {
println!("{:>20} - {}", name, description);
for pass in passes::PASSES {
println!("{:>20} - {}", pass.name(), pass.description());
}
println!("\nDefault passes for rustdoc:");
for &name in passes::DEFAULT_PASSES {
@ -751,8 +752,13 @@ where R: 'static + Send,
for pass in &passes {
// determine if we know about this pass
let pass = match passes::PASSES.iter().find(|(p, ..)| p == pass) {
Some(pass) => pass.1,
let pass = match passes::PASSES.iter().find(|p| p.name() == pass) {
Some(pass) => if let Some(pass) = pass.late_fn() {
pass
} else {
// not a late pass, but still valid so don't report the error
continue
}
None => {
error!("unknown pass {}, skipping", *pass);

View file

@ -11,8 +11,13 @@
use clean::{self, DocFragment, Item};
use fold;
use fold::DocFolder;
use passes::Pass;
use std::mem::replace;
pub const COLLAPSE_DOCS: Pass =
Pass::late("collapse-docs", collapse_docs,
"concatenates all document attributes into one document attribute");
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum DocFragmentKind {
Sugared,

View file

@ -18,61 +18,65 @@ use fold;
use fold::StripItem;
mod collapse_docs;
pub use self::collapse_docs::collapse_docs;
pub use self::collapse_docs::COLLAPSE_DOCS;
mod strip_hidden;
pub use self::strip_hidden::strip_hidden;
pub use self::strip_hidden::STRIP_HIDDEN;
mod strip_private;
pub use self::strip_private::strip_private;
pub use self::strip_private::STRIP_PRIVATE;
mod strip_priv_imports;
pub use self::strip_priv_imports::strip_priv_imports;
pub use self::strip_priv_imports::STRIP_PRIV_IMPORTS;
mod unindent_comments;
pub use self::unindent_comments::unindent_comments;
pub use self::unindent_comments::UNINDENT_COMMENTS;
mod propagate_doc_cfg;
pub use self::propagate_doc_cfg::propagate_doc_cfg;
pub use self::propagate_doc_cfg::PROPAGATE_DOC_CFG;
type Pass = (
&'static str, // name
fn(clean::Crate) -> clean::Crate, // fn
&'static str,
); // description
#[derive(Copy, Clone, Debug)]
pub enum Pass {
LatePass {
name: &'static str,
pass: fn(clean::Crate) -> clean::Crate,
description: &'static str,
}
}
impl Pass {
pub const fn late(name: &'static str,
pass: fn(clean::Crate) -> clean::Crate,
description: &'static str) -> Pass {
Pass::LatePass { name, pass, description }
}
pub fn name(self) -> &'static str {
match self {
Pass::LatePass { name, .. } => name,
}
}
pub fn description(self) -> &'static str {
match self {
Pass::LatePass { description, .. } => description,
}
}
pub fn late_fn(self) -> Option<fn(clean::Crate) -> clean::Crate> {
match self {
Pass::LatePass { pass, .. } => Some(pass),
}
}
}
pub const PASSES: &'static [Pass] = &[
(
"strip-hidden",
strip_hidden,
"strips all doc(hidden) items from the output",
),
(
"unindent-comments",
unindent_comments,
"removes excess indentation on comments in order for markdown to like it",
),
(
"collapse-docs",
collapse_docs,
"concatenates all document attributes into one document attribute",
),
(
"strip-private",
strip_private,
"strips all private items from a crate which cannot be seen externally, \
implies strip-priv-imports",
),
(
"strip-priv-imports",
strip_priv_imports,
"strips all private import statements (`use`, `extern crate`) from a crate",
),
(
"propagate-doc-cfg",
propagate_doc_cfg,
"propagates `#[doc(cfg(...))]` to child items",
),
STRIP_HIDDEN,
UNINDENT_COMMENTS,
COLLAPSE_DOCS,
STRIP_PRIVATE,
STRIP_PRIV_IMPORTS,
PROPAGATE_DOC_CFG,
];
pub const DEFAULT_PASSES: &'static [&'static str] = &[

View file

@ -13,6 +13,11 @@ use std::sync::Arc;
use clean::{Crate, Item};
use clean::cfg::Cfg;
use fold::DocFolder;
use passes::Pass;
pub const PROPAGATE_DOC_CFG: Pass =
Pass::late("propagate-doc-cfg", propagate_doc_cfg,
"propagates `#[doc(cfg(...))]` to child items");
pub fn propagate_doc_cfg(cr: Crate) -> Crate {
CfgPropagator { parent_cfg: None }.fold_crate(cr)

View file

@ -16,7 +16,11 @@ use clean::Item;
use fold;
use fold::DocFolder;
use fold::StripItem;
use passes::ImplStripper;
use passes::{ImplStripper, Pass};
pub const STRIP_HIDDEN: Pass =
Pass::late("strip-hidden", strip_hidden,
"strips all doc(hidden) items from the output");
/// Strip items marked `#[doc(hidden)]`
pub fn strip_hidden(krate: clean::Crate) -> clean::Crate {

View file

@ -10,7 +10,10 @@
use clean;
use fold::DocFolder;
use passes::ImportStripper;
use passes::{ImportStripper, Pass};
pub const STRIP_PRIV_IMPORTS: Pass = Pass::late("strip-priv-imports", strip_priv_imports,
"strips all private import statements (`use`, `extern crate`) from a crate");
pub fn strip_priv_imports(krate: clean::Crate) -> clean::Crate {
ImportStripper.fold_crate(krate)

View file

@ -12,7 +12,12 @@ use rustc::util::nodemap::DefIdSet;
use clean;
use fold::DocFolder;
use passes::{ImplStripper, ImportStripper, Stripper};
use passes::{ImplStripper, ImportStripper, Stripper, Pass};
pub const STRIP_PRIVATE: Pass =
Pass::late("strip-private", strip_private,
"strips all private items from a crate which cannot be seen externally, \
implies strip-priv-imports");
/// Strip private items from the point of view of a crate or externally from a
/// crate, specified by the `xcrate` flag.

View file

@ -14,6 +14,11 @@ use std::usize;
use clean::{self, DocFragment, Item};
use fold::{self, DocFolder};
use passes::Pass;
pub const UNINDENT_COMMENTS: Pass =
Pass::late("unindent-comments", unindent_comments,
"removes excess indentation on comments in order for markdown to like it");
pub fn unindent_comments(krate: clean::Crate) -> clean::Crate {
CommentCleaner.fold_crate(krate)