2019-01-30 20:04:56 +00:00
|
|
|
use crate::clean;
|
|
|
|
use crate::core::DocContext;
|
|
|
|
use crate::fold::{self, DocFolder};
|
|
|
|
use crate::passes::Pass;
|
|
|
|
|
|
|
|
use syntax::attr;
|
2019-02-28 21:27:42 +00:00
|
|
|
use syntax_pos::FileName;
|
2019-05-08 03:21:18 +00:00
|
|
|
use syntax::symbol::sym;
|
2019-01-30 20:04:56 +00:00
|
|
|
|
2019-02-21 16:58:40 +00:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
use std::ops;
|
2019-02-20 21:26:35 +00:00
|
|
|
|
2019-01-30 20:04:56 +00:00
|
|
|
pub const CALCULATE_DOC_COVERAGE: Pass = Pass {
|
|
|
|
name: "calculate-doc-coverage",
|
|
|
|
pass: calculate_doc_coverage,
|
|
|
|
description: "counts the number of items with and without documentation",
|
|
|
|
};
|
|
|
|
|
2018-12-08 19:30:23 +00:00
|
|
|
fn calculate_doc_coverage(krate: clean::Crate, _: &DocContext<'_>) -> clean::Crate {
|
2019-01-30 20:04:56 +00:00
|
|
|
let mut calc = CoverageCalculator::default();
|
|
|
|
let krate = calc.fold_crate(krate);
|
|
|
|
|
2019-02-21 16:58:40 +00:00
|
|
|
calc.print_results();
|
2019-01-30 20:04:56 +00:00
|
|
|
|
|
|
|
krate
|
|
|
|
}
|
|
|
|
|
2019-02-20 21:26:35 +00:00
|
|
|
#[derive(Default, Copy, Clone)]
|
|
|
|
struct ItemCount {
|
|
|
|
total: u64,
|
|
|
|
with_docs: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ItemCount {
|
|
|
|
fn count_item(&mut self, has_docs: bool) {
|
|
|
|
self.total += 1;
|
|
|
|
|
|
|
|
if has_docs {
|
|
|
|
self.with_docs += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn percentage(&self) -> Option<f64> {
|
|
|
|
if self.total > 0 {
|
|
|
|
Some((self.with_docs as f64 * 100.0) / self.total as f64)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:40 +00:00
|
|
|
impl ops::Sub for ItemCount {
|
2019-02-20 21:26:35 +00:00
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn sub(self, rhs: Self) -> Self {
|
|
|
|
ItemCount {
|
|
|
|
total: self.total - rhs.total,
|
|
|
|
with_docs: self.with_docs - rhs.with_docs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:58:40 +00:00
|
|
|
impl ops::AddAssign for ItemCount {
|
|
|
|
fn add_assign(&mut self, rhs: Self) {
|
|
|
|
self.total += rhs.total;
|
|
|
|
self.with_docs += rhs.with_docs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 20:04:56 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
struct CoverageCalculator {
|
2019-02-28 21:27:42 +00:00
|
|
|
items: BTreeMap<FileName, ItemCount>,
|
2019-02-21 16:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CoverageCalculator {
|
|
|
|
fn print_results(&self) {
|
|
|
|
let mut total = ItemCount::default();
|
|
|
|
|
2019-02-28 21:27:42 +00:00
|
|
|
fn print_table_line() {
|
|
|
|
println!("+-{0:->35}-+-{0:->10}-+-{0:->10}-+-{0:->10}-+", "");
|
|
|
|
}
|
2019-02-21 16:58:40 +00:00
|
|
|
|
2019-02-28 21:27:42 +00:00
|
|
|
fn print_table_record(name: &str, count: ItemCount, percentage: f64) {
|
|
|
|
println!("| {:<35} | {:>10} | {:>10} | {:>9.1}% |",
|
|
|
|
name, count.with_docs, count.total, percentage);
|
2019-02-21 16:58:40 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 21:27:42 +00:00
|
|
|
print_table_line();
|
|
|
|
println!("| {:<35} | {:>10} | {:>10} | {:>10} |",
|
|
|
|
"File", "Documented", "Total", "Percentage");
|
|
|
|
print_table_line();
|
2019-02-21 16:58:40 +00:00
|
|
|
|
2019-02-28 21:27:42 +00:00
|
|
|
for (file, &count) in &self.items {
|
2019-02-21 16:58:40 +00:00
|
|
|
if let Some(percentage) = count.percentage() {
|
2019-02-28 21:27:42 +00:00
|
|
|
let mut name = file.to_string();
|
|
|
|
// if a filename is too long, shorten it so we don't blow out the table
|
|
|
|
// FIXME(misdreavus): this needs to count graphemes, and probably also track
|
|
|
|
// double-wide characters...
|
|
|
|
if name.len() > 35 {
|
|
|
|
name = "...".to_string() + &name[name.len()-32..];
|
2019-02-21 16:58:40 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 21:27:42 +00:00
|
|
|
print_table_record(&name, count, percentage);
|
2019-02-21 16:58:40 +00:00
|
|
|
|
2019-02-28 21:27:42 +00:00
|
|
|
total += count;
|
2019-02-21 16:58:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 21:27:42 +00:00
|
|
|
print_table_line();
|
|
|
|
print_table_record("Total", total, total.percentage().unwrap_or(0.0));
|
|
|
|
print_table_line();
|
2019-02-21 16:58:40 +00:00
|
|
|
}
|
2019-01-30 20:04:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fold::DocFolder for CoverageCalculator {
|
2019-02-28 21:27:42 +00:00
|
|
|
fn fold_item(&mut self, i: clean::Item) -> Option<clean::Item> {
|
2019-02-21 16:58:40 +00:00
|
|
|
let has_docs = !i.attrs.doc_strings.is_empty();
|
|
|
|
|
2019-01-30 20:04:56 +00:00
|
|
|
match i.inner {
|
2019-02-21 16:58:40 +00:00
|
|
|
_ if !i.def_id.is_local() => {
|
|
|
|
// non-local items are skipped because they can be out of the users control,
|
|
|
|
// especially in the case of trait impls, which rustdoc eagerly inlines
|
|
|
|
return Some(i);
|
|
|
|
}
|
2019-02-20 21:15:13 +00:00
|
|
|
clean::StrippedItem(..) => {
|
|
|
|
// don't count items in stripped modules
|
|
|
|
return Some(i);
|
|
|
|
}
|
2019-02-21 16:58:40 +00:00
|
|
|
clean::ImportItem(..) | clean::ExternCrateItem(..) => {
|
|
|
|
// docs on `use` and `extern crate` statements are not displayed, so they're not
|
|
|
|
// worth counting
|
|
|
|
return Some(i);
|
|
|
|
}
|
2019-01-30 20:04:56 +00:00
|
|
|
clean::ImplItem(ref impl_)
|
2019-05-08 03:21:18 +00:00
|
|
|
if attr::contains_name(&i.attrs.other_attrs, sym::automatically_derived)
|
2019-01-30 20:04:56 +00:00
|
|
|
|| impl_.synthetic || impl_.blanket_impl.is_some() =>
|
|
|
|
{
|
2019-02-21 16:58:40 +00:00
|
|
|
// built-in derives get the `#[automatically_derived]` attribute, and
|
|
|
|
// synthetic/blanket impls are made up by rustdoc and can't be documented
|
2019-01-30 20:04:56 +00:00
|
|
|
// FIXME(misdreavus): need to also find items that came out of a derive macro
|
|
|
|
return Some(i);
|
|
|
|
}
|
2019-02-21 16:58:40 +00:00
|
|
|
clean::ImplItem(ref impl_) => {
|
|
|
|
if let Some(ref tr) = impl_.trait_ {
|
2019-02-28 21:27:42 +00:00
|
|
|
debug!("impl {:#} for {:#} in {}", tr, impl_.for_, i.source.filename);
|
2019-01-30 20:04:56 +00:00
|
|
|
|
2019-02-28 21:27:42 +00:00
|
|
|
// don't count trait impls, the missing-docs lint doesn't so we shouldn't
|
|
|
|
// either
|
2019-02-21 16:58:40 +00:00
|
|
|
return Some(i);
|
|
|
|
} else {
|
|
|
|
// inherent impls *can* be documented, and those docs show up, but in most
|
|
|
|
// cases it doesn't make sense, as all methods on a type are in one single
|
|
|
|
// impl block
|
2019-02-28 21:27:42 +00:00
|
|
|
debug!("impl {:#} in {}", impl_.for_, i.source.filename);
|
2019-02-21 16:58:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2019-02-28 21:27:42 +00:00
|
|
|
debug!("counting {} {:?} in {}", i.type_(), i.name, i.source.filename);
|
|
|
|
self.items.entry(i.source.filename.clone())
|
|
|
|
.or_default()
|
|
|
|
.count_item(has_docs);
|
2019-01-30 20:04:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.fold_item_recur(i)
|
|
|
|
}
|
|
|
|
}
|