2019-01-30 20:04:56 +00:00
|
|
|
use crate::clean;
|
2019-11-16 15:12:29 +00:00
|
|
|
use crate::config::OutputFormat;
|
2019-01-30 20:04:56 +00:00
|
|
|
use crate::core::DocContext;
|
|
|
|
use crate::fold::{self, DocFolder};
|
2020-08-18 11:31:23 +00:00
|
|
|
use crate::html::markdown::{find_testable_code, ErrorCodes};
|
|
|
|
use crate::passes::doc_test_lints::Tests;
|
2019-01-30 20:04:56 +00:00
|
|
|
use crate::passes::Pass;
|
2020-01-01 18:30:57 +00:00
|
|
|
use rustc_span::symbol::sym;
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::FileName;
|
2020-02-17 12:53:27 +00:00
|
|
|
use serde::Serialize;
|
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",
|
2020-01-04 18:58:32 +00:00
|
|
|
run: calculate_doc_coverage,
|
2019-01-30 20:04:56 +00:00
|
|
|
description: "counts the number of items with and without documentation",
|
|
|
|
};
|
|
|
|
|
2020-02-17 12:53:27 +00:00
|
|
|
fn calculate_doc_coverage(krate: clean::Crate, ctx: &DocContext<'_>) -> clean::Crate {
|
|
|
|
let mut calc = CoverageCalculator::new();
|
2019-01-30 20:04:56 +00:00
|
|
|
let krate = calc.fold_crate(krate);
|
|
|
|
|
2020-02-17 12:53:27 +00:00
|
|
|
calc.print_results(ctx.renderinfo.borrow().output_format);
|
2019-01-30 20:04:56 +00:00
|
|
|
|
|
|
|
krate
|
|
|
|
}
|
|
|
|
|
2020-08-21 14:43:04 +00:00
|
|
|
#[derive(Default, Copy, Clone, Serialize, Debug)]
|
2019-02-20 21:26:35 +00:00
|
|
|
struct ItemCount {
|
|
|
|
total: u64,
|
|
|
|
with_docs: u64,
|
2020-08-19 20:44:21 +00:00
|
|
|
total_examples: u64,
|
2020-08-18 11:31:23 +00:00
|
|
|
with_examples: u64,
|
2019-02-20 21:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ItemCount {
|
2020-08-19 20:44:21 +00:00
|
|
|
fn count_item(
|
|
|
|
&mut self,
|
|
|
|
has_docs: bool,
|
|
|
|
has_doc_example: bool,
|
|
|
|
should_have_doc_examples: bool,
|
|
|
|
) {
|
2019-02-20 21:26:35 +00:00
|
|
|
self.total += 1;
|
|
|
|
|
|
|
|
if has_docs {
|
|
|
|
self.with_docs += 1;
|
|
|
|
}
|
2020-08-20 19:15:02 +00:00
|
|
|
if should_have_doc_examples || has_doc_example {
|
|
|
|
self.total_examples += 1;
|
|
|
|
}
|
|
|
|
if has_doc_example {
|
2020-08-20 19:09:40 +00:00
|
|
|
self.with_examples += 1;
|
2020-08-18 11:31:23 +00:00
|
|
|
}
|
2019-02-20 21:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn percentage(&self) -> Option<f64> {
|
|
|
|
if self.total > 0 {
|
|
|
|
Some((self.with_docs as f64 * 100.0) / self.total as f64)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2020-08-18 11:31:23 +00:00
|
|
|
|
|
|
|
fn examples_percentage(&self) -> Option<f64> {
|
2020-08-19 20:44:21 +00:00
|
|
|
if self.total_examples > 0 {
|
|
|
|
Some((self.with_examples as f64 * 100.0) / self.total_examples as f64)
|
2020-08-18 11:31:23 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2019-02-20 21:26:35 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2020-08-18 11:31:23 +00:00
|
|
|
ItemCount {
|
|
|
|
total: self.total - rhs.total,
|
|
|
|
with_docs: self.with_docs - rhs.with_docs,
|
2020-08-19 20:44:21 +00:00
|
|
|
total_examples: self.total_examples - rhs.total_examples,
|
2020-08-18 11:31:23 +00:00
|
|
|
with_examples: self.with_examples - rhs.with_examples,
|
|
|
|
}
|
2019-02-20 21:26:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2020-08-19 20:44:21 +00:00
|
|
|
self.total_examples += rhs.total_examples;
|
2020-08-18 11:31:23 +00:00
|
|
|
self.with_examples += rhs.with_examples;
|
2019-02-21 16:58:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 20:04:56 +00:00
|
|
|
struct CoverageCalculator {
|
2019-02-28 21:27:42 +00:00
|
|
|
items: BTreeMap<FileName, ItemCount>,
|
2019-11-16 15:12:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn limit_filename_len(filename: String) -> String {
|
2020-02-17 12:53:27 +00:00
|
|
|
let nb_chars = filename.chars().count();
|
|
|
|
if nb_chars > 35 {
|
|
|
|
"...".to_string()
|
|
|
|
+ &filename[filename.char_indices().nth(nb_chars - 32).map(|x| x.0).unwrap_or(0)..]
|
2019-11-16 15:12:29 +00:00
|
|
|
} else {
|
|
|
|
filename
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 12:53:27 +00:00
|
|
|
impl CoverageCalculator {
|
|
|
|
fn new() -> CoverageCalculator {
|
|
|
|
CoverageCalculator { items: Default::default() }
|
2019-11-16 15:12:29 +00:00
|
|
|
}
|
2019-02-21 16:58:40 +00:00
|
|
|
|
2020-02-17 12:53:27 +00:00
|
|
|
fn to_json(&self) -> String {
|
|
|
|
serde_json::to_string(
|
|
|
|
&self
|
|
|
|
.items
|
|
|
|
.iter()
|
|
|
|
.map(|(k, v)| (k.to_string(), v))
|
|
|
|
.collect::<BTreeMap<String, &ItemCount>>(),
|
|
|
|
)
|
|
|
|
.expect("failed to convert JSON data to string")
|
2019-11-16 15:12:29 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 12:53:27 +00:00
|
|
|
fn print_results(&self, output_format: Option<OutputFormat>) {
|
|
|
|
if output_format.map(|o| o.is_json()).unwrap_or_else(|| false) {
|
|
|
|
println!("{}", self.to_json());
|
2019-11-16 15:12:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-02-21 16:58:40 +00:00
|
|
|
let mut total = ItemCount::default();
|
|
|
|
|
2019-02-28 21:27:42 +00:00
|
|
|
fn print_table_line() {
|
2020-08-20 19:09:40 +00:00
|
|
|
println!("+-{0:->35}-+-{0:->10}-+-{0:->10}-+-{0:->10}-+-{0:->10}-+", "");
|
2019-02-28 21:27:42 +00:00
|
|
|
}
|
2019-02-21 16:58:40 +00:00
|
|
|
|
2020-08-18 11:31:23 +00:00
|
|
|
fn print_table_record(
|
|
|
|
name: &str,
|
|
|
|
count: ItemCount,
|
|
|
|
percentage: f64,
|
|
|
|
examples_percentage: f64,
|
|
|
|
) {
|
2019-12-22 22:42:04 +00:00
|
|
|
println!(
|
2020-08-20 19:09:40 +00:00
|
|
|
"| {:<35} | {:>10} | {:>9.1}% | {:>10} | {:>9.1}% |",
|
2020-08-20 19:28:06 +00:00
|
|
|
name, count.with_docs, percentage, count.with_examples, examples_percentage,
|
2019-12-22 22:42:04 +00:00
|
|
|
);
|
2019-02-21 16:58:40 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 21:27:42 +00:00
|
|
|
print_table_line();
|
2019-12-22 22:42:04 +00:00
|
|
|
println!(
|
2020-08-20 19:09:40 +00:00
|
|
|
"| {:<35} | {:>10} | {:>10} | {:>10} | {:>10} |",
|
|
|
|
"File", "Documented", "Percentage", "Examples", "Percentage",
|
2019-12-22 22:42:04 +00:00
|
|
|
);
|
2019-02-28 21:27:42 +00:00
|
|
|
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 {
|
2020-08-21 14:43:04 +00:00
|
|
|
if let Some(percentage) = count.percentage() {
|
2020-08-18 11:31:23 +00:00
|
|
|
print_table_record(
|
|
|
|
&limit_filename_len(file.to_string()),
|
|
|
|
count,
|
|
|
|
percentage,
|
2020-08-21 14:43:04 +00:00
|
|
|
count.examples_percentage().unwrap_or(0.),
|
2020-08-18 11:31:23 +00:00
|
|
|
);
|
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();
|
2020-08-18 11:31:23 +00:00
|
|
|
print_table_record(
|
|
|
|
"Total",
|
|
|
|
total,
|
|
|
|
total.percentage().unwrap_or(0.0),
|
|
|
|
total.examples_percentage().unwrap_or(0.0),
|
|
|
|
);
|
2019-02-28 21:27:42 +00:00
|
|
|
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-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_)
|
2020-07-30 01:27:50 +00:00
|
|
|
if i.attrs
|
|
|
|
.other_attrs
|
|
|
|
.iter()
|
|
|
|
.any(|item| item.has_name(sym::automatically_derived))
|
2019-12-22 22:42:04 +00:00
|
|
|
|| impl_.synthetic
|
|
|
|
|| impl_.blanket_impl.is_some() =>
|
2019-01-30 20:04:56 +00:00
|
|
|
{
|
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-12-22 22:42:04 +00:00
|
|
|
debug!(
|
|
|
|
"impl {:#} for {:#} in {}",
|
|
|
|
tr.print(),
|
|
|
|
impl_.for_.print(),
|
|
|
|
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-09-12 23:59:14 +00:00
|
|
|
debug!("impl {:#} in {}", impl_.for_.print(), i.source.filename);
|
2019-02-21 16:58:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2020-08-19 20:44:21 +00:00
|
|
|
let has_docs = !i.attrs.doc_strings.is_empty();
|
|
|
|
let mut tests = Tests { found_tests: 0 };
|
|
|
|
|
|
|
|
let should_have_doc_examples = !matches!(i.inner,
|
|
|
|
clean::StructFieldItem(_)
|
|
|
|
| clean::VariantItem(_)
|
|
|
|
| clean::AssocConstItem(_, _)
|
|
|
|
| clean::AssocTypeItem(_, _)
|
|
|
|
| clean::TypedefItem(_, _)
|
|
|
|
| clean::StaticItem(_)
|
|
|
|
| clean::ConstantItem(_)
|
2020-08-21 11:51:03 +00:00
|
|
|
| clean::ExternCrateItem(_, _)
|
|
|
|
| clean::ImportItem(_)
|
|
|
|
| clean::PrimitiveItem(_)
|
|
|
|
| clean::KeywordItem(_)
|
2020-08-19 20:44:21 +00:00
|
|
|
);
|
2020-08-20 19:15:02 +00:00
|
|
|
find_testable_code(
|
2020-08-20 19:28:06 +00:00
|
|
|
&i.attrs.doc_strings.iter().map(|d| d.as_str()).collect::<Vec<_>>().join("\n"),
|
2020-08-20 19:15:02 +00:00
|
|
|
&mut tests,
|
|
|
|
ErrorCodes::No,
|
|
|
|
false,
|
|
|
|
None,
|
|
|
|
);
|
2020-08-19 20:44:21 +00:00
|
|
|
|
|
|
|
let has_doc_example = tests.found_tests != 0;
|
2019-09-12 23:59:14 +00:00
|
|
|
debug!("counting {:?} {:?} in {}", i.type_(), i.name, i.source.filename);
|
2020-08-19 20:44:58 +00:00
|
|
|
self.items.entry(i.source.filename.clone()).or_default().count_item(
|
|
|
|
has_docs,
|
|
|
|
has_doc_example,
|
|
|
|
should_have_doc_examples,
|
|
|
|
);
|
2019-01-30 20:04:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.fold_item_recur(i)
|
|
|
|
}
|
|
|
|
}
|