2021-12-15 18:59:00 -08:00
|
|
|
//! Calculates information used for the --show-coverage flag.
|
2024-06-20 05:04:30 +10:00
|
|
|
|
2021-10-02 14:07:08 -07:00
|
|
|
use std::collections::BTreeMap;
|
2019-01-30 14:04:56 -06:00
|
|
|
use std::ops;
|
2024-07-29 08:13:50 +10:00
|
|
|
|
2021-09-06 16:26:49 +02:00
|
|
|
use rustc_hir as hir;
|
2020-10-08 18:05:01 +02:00
|
|
|
use rustc_lint::builtin::MISSING_DOCS;
|
2025-03-19 09:41:38 +00:00
|
|
|
use rustc_middle::lint::{LevelAndSource, LintLevelSource};
|
2020-10-08 18:05:01 +02:00
|
|
|
use rustc_session::lint;
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::FileName;
|
2020-02-17 13:53:27 +01:00
|
|
|
use serde::Serialize;
|
2024-08-30 13:16:08 +10:00
|
|
|
use tracing::debug;
|
2019-01-30 14:04:56 -06:00
|
|
|
|
|
|
|
|
use crate::clean;
|
2019-02-21 10:58:40 -06:00
|
|
|
use crate::core::DocContext;
|
|
|
|
|
use crate::html::markdown::{ErrorCodes, find_testable_code};
|
|
|
|
|
use crate::passes::Pass;
|
|
|
|
|
use crate::passes::check_doc_test_visibility::{Tests, should_have_doc_example};
|
2021-10-31 21:24:11 -07:00
|
|
|
use crate::visit::DocVisitor;
|
2019-02-20 15:26:35 -06:00
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) const CALCULATE_DOC_COVERAGE: Pass = Pass {
|
2019-01-30 14:04:56 -06:00
|
|
|
name: "calculate-doc-coverage",
|
2024-10-14 17:51:08 +02:00
|
|
|
run: Some(calculate_doc_coverage),
|
2019-01-30 14:04:56 -06:00
|
|
|
description: "counts the number of items with and without documentation",
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-12 01:11:32 -05:00
|
|
|
fn calculate_doc_coverage(krate: clean::Crate, ctx: &mut DocContext<'_>) -> clean::Crate {
|
|
|
|
|
let mut calc = CoverageCalculator { items: Default::default(), ctx };
|
2021-10-31 21:24:11 -07:00
|
|
|
calc.visit_crate(&krate);
|
2019-01-30 14:04:56 -06:00
|
|
|
|
2020-10-08 18:05:01 +02:00
|
|
|
calc.print_results();
|
2019-01-30 14:04:56 -06:00
|
|
|
|
|
|
|
|
krate
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 16:43:04 +02:00
|
|
|
#[derive(Default, Copy, Clone, Serialize, Debug)]
|
2019-02-20 15:26:35 -06:00
|
|
|
struct ItemCount {
|
|
|
|
|
total: u64,
|
|
|
|
|
with_docs: u64,
|
2020-08-19 22:44:21 +02:00
|
|
|
total_examples: u64,
|
2020-08-18 13:31:23 +02:00
|
|
|
with_examples: u64,
|
2019-02-20 15:26:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ItemCount {
|
2020-08-19 22:44:21 +02:00
|
|
|
fn count_item(
|
|
|
|
|
&mut self,
|
|
|
|
|
has_docs: bool,
|
|
|
|
|
has_doc_example: bool,
|
|
|
|
|
should_have_doc_examples: bool,
|
2020-09-01 14:19:24 +02:00
|
|
|
should_have_docs: bool,
|
2020-08-19 22:44:21 +02:00
|
|
|
) {
|
2020-09-01 14:19:24 +02:00
|
|
|
if has_docs || should_have_docs {
|
|
|
|
|
self.total += 1;
|
|
|
|
|
}
|
2019-02-20 15:26:35 -06:00
|
|
|
|
|
|
|
|
if has_docs {
|
|
|
|
|
self.with_docs += 1;
|
|
|
|
|
}
|
2020-08-20 21:15:02 +02:00
|
|
|
if should_have_doc_examples || has_doc_example {
|
|
|
|
|
self.total_examples += 1;
|
|
|
|
|
}
|
|
|
|
|
if has_doc_example {
|
2020-08-20 21:09:40 +02:00
|
|
|
self.with_examples += 1;
|
2020-08-18 13:31:23 +02:00
|
|
|
}
|
2019-02-20 15:26:35 -06: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 13:31:23 +02:00
|
|
|
|
|
|
|
|
fn examples_percentage(&self) -> Option<f64> {
|
2020-08-19 22:44:21 +02:00
|
|
|
if self.total_examples > 0 {
|
|
|
|
|
Some((self.with_examples as f64 * 100.0) / self.total_examples as f64)
|
2020-08-18 13:31:23 +02:00
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-20 15:26:35 -06:00
|
|
|
}
|
|
|
|
|
|
2019-02-21 10:58:40 -06:00
|
|
|
impl ops::Sub for ItemCount {
|
2019-02-20 15:26:35 -06:00
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
|
|
fn sub(self, rhs: Self) -> Self {
|
2020-08-18 13:31:23 +02:00
|
|
|
ItemCount {
|
|
|
|
|
total: self.total - rhs.total,
|
|
|
|
|
with_docs: self.with_docs - rhs.with_docs,
|
2020-08-19 22:44:21 +02:00
|
|
|
total_examples: self.total_examples - rhs.total_examples,
|
2020-08-18 13:31:23 +02:00
|
|
|
with_examples: self.with_examples - rhs.with_examples,
|
|
|
|
|
}
|
2019-02-20 15:26:35 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 10:58:40 -06: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 22:44:21 +02:00
|
|
|
self.total_examples += rhs.total_examples;
|
2020-08-18 13:31:23 +02:00
|
|
|
self.with_examples += rhs.with_examples;
|
2019-02-21 10:58:40 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-08 18:05:01 +02:00
|
|
|
struct CoverageCalculator<'a, 'b> {
|
2019-02-28 15:27:42 -06:00
|
|
|
items: BTreeMap<FileName, ItemCount>,
|
2021-02-12 01:11:32 -05:00
|
|
|
ctx: &'a mut DocContext<'b>,
|
2019-11-16 16:12:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn limit_filename_len(filename: String) -> String {
|
2020-02-17 13:53:27 +01: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 16:12:29 +01:00
|
|
|
} else {
|
|
|
|
|
filename
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-27 15:44:10 +01:00
|
|
|
impl CoverageCalculator<'_, '_> {
|
2020-02-17 13:53:27 +01:00
|
|
|
fn to_json(&self) -> String {
|
|
|
|
|
serde_json::to_string(
|
|
|
|
|
&self
|
|
|
|
|
.items
|
|
|
|
|
.iter()
|
2021-04-19 23:27:02 +01:00
|
|
|
.map(|(k, v)| (k.prefer_local().to_string(), v))
|
2020-02-17 13:53:27 +01:00
|
|
|
.collect::<BTreeMap<String, &ItemCount>>(),
|
|
|
|
|
)
|
|
|
|
|
.expect("failed to convert JSON data to string")
|
2019-11-16 16:12:29 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-08 18:05:01 +02:00
|
|
|
fn print_results(&self) {
|
2021-02-11 21:29:22 -05:00
|
|
|
let output_format = self.ctx.output_format;
|
2024-11-04 14:28:35 +01:00
|
|
|
// In this case we want to ensure that the `OutputFormat` is JSON and NOT the `DocContext`.
|
2021-01-28 18:00:07 -08:00
|
|
|
if output_format.is_json() {
|
2020-02-17 13:53:27 +01:00
|
|
|
println!("{}", self.to_json());
|
2019-11-16 16:12:29 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2019-02-21 10:58:40 -06:00
|
|
|
let mut total = ItemCount::default();
|
|
|
|
|
|
2019-02-28 15:27:42 -06:00
|
|
|
fn print_table_line() {
|
2020-08-20 21:09:40 +02:00
|
|
|
println!("+-{0:->35}-+-{0:->10}-+-{0:->10}-+-{0:->10}-+-{0:->10}-+", "");
|
2019-02-28 15:27:42 -06:00
|
|
|
}
|
2019-02-21 10:58:40 -06:00
|
|
|
|
2020-08-18 13:31:23 +02:00
|
|
|
fn print_table_record(
|
|
|
|
|
name: &str,
|
|
|
|
|
count: ItemCount,
|
|
|
|
|
percentage: f64,
|
|
|
|
|
examples_percentage: f64,
|
|
|
|
|
) {
|
2019-02-28 15:27:42 -06:00
|
|
|
println!(
|
2023-08-15 14:27:29 +02:00
|
|
|
"| {name:<35} | {with_docs:>10} | {percentage:>9.1}% | {with_examples:>10} | \
|
|
|
|
|
{examples_percentage:>9.1}% |",
|
|
|
|
|
with_docs = count.with_docs,
|
|
|
|
|
with_examples = count.with_examples,
|
2019-02-28 15:27:42 -06:00
|
|
|
);
|
2019-02-21 10:58:40 -06:00
|
|
|
}
|
|
|
|
|
|
2019-02-28 15:27:42 -06:00
|
|
|
print_table_line();
|
|
|
|
|
println!(
|
2020-08-20 21:09:40 +02:00
|
|
|
"| {:<35} | {:>10} | {:>10} | {:>10} | {:>10} |",
|
|
|
|
|
"File", "Documented", "Percentage", "Examples", "Percentage",
|
2019-02-28 15:27:42 -06:00
|
|
|
);
|
|
|
|
|
print_table_line();
|
2019-02-21 10:58:40 -06:00
|
|
|
|
2019-02-28 15:27:42 -06:00
|
|
|
for (file, &count) in &self.items {
|
2020-08-21 16:43:04 +02:00
|
|
|
if let Some(percentage) = count.percentage() {
|
2020-08-18 13:31:23 +02:00
|
|
|
print_table_record(
|
2021-04-19 23:27:02 +01:00
|
|
|
&limit_filename_len(file.prefer_local().to_string_lossy().into()),
|
2020-08-18 13:31:23 +02:00
|
|
|
count,
|
|
|
|
|
percentage,
|
2020-08-21 16:43:04 +02:00
|
|
|
count.examples_percentage().unwrap_or(0.),
|
2020-08-18 13:31:23 +02:00
|
|
|
);
|
2019-02-21 10:58:40 -06:00
|
|
|
|
2019-02-28 15:27:42 -06:00
|
|
|
total += count;
|
2019-02-21 10:58:40 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-28 15:27:42 -06:00
|
|
|
print_table_line();
|
2020-08-18 13:31:23 +02:00
|
|
|
print_table_record(
|
|
|
|
|
"Total",
|
|
|
|
|
total,
|
|
|
|
|
total.percentage().unwrap_or(0.0),
|
|
|
|
|
total.examples_percentage().unwrap_or(0.0),
|
|
|
|
|
);
|
2019-02-28 15:27:42 -06:00
|
|
|
print_table_line();
|
2019-02-21 10:58:40 -06:00
|
|
|
}
|
2019-01-30 14:04:56 -06:00
|
|
|
}
|
|
|
|
|
|
2024-11-27 15:44:10 +01:00
|
|
|
impl DocVisitor<'_> for CoverageCalculator<'_, '_> {
|
2021-10-31 21:24:11 -07:00
|
|
|
fn visit_item(&mut self, i: &clean::Item) {
|
2022-04-16 14:28:09 +02:00
|
|
|
if !i.item_id.is_local() {
|
2021-10-31 21:24:11 -07:00
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-30 10:52:45 -07:00
|
|
|
match i.kind {
|
2019-02-20 15:15:13 -06:00
|
|
|
clean::StrippedItem(..) => {
|
|
|
|
|
// don't count items in stripped modules
|
2021-10-31 21:24:11 -07:00
|
|
|
return;
|
2019-02-20 15:15:13 -06:00
|
|
|
}
|
2021-04-09 09:00:12 -04:00
|
|
|
// docs on `use` and `extern crate` statements are not displayed, so they're not
|
|
|
|
|
// worth counting
|
|
|
|
|
clean::ImportItem(..) | clean::ExternCrateItem { .. } => {}
|
|
|
|
|
// Don't count trait impls, the missing-docs lint doesn't so we shouldn't either.
|
|
|
|
|
// 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
|
|
|
|
|
clean::ImplItem(_) => {}
|
2019-02-21 10:58:40 -06:00
|
|
|
_ => {
|
2020-08-19 22:44:21 +02:00
|
|
|
let has_docs = !i.attrs.doc_strings.is_empty();
|
|
|
|
|
let mut tests = Tests { found_tests: 0 };
|
|
|
|
|
|
2025-02-15 11:18:29 -08:00
|
|
|
find_testable_code(&i.doc_value(), &mut tests, ErrorCodes::No, None);
|
2020-08-19 22:44:21 +02:00
|
|
|
|
|
|
|
|
let has_doc_example = tests.found_tests != 0;
|
2023-01-26 14:30:28 +04:00
|
|
|
let hir_id = DocContext::as_local_hir_id(self.ctx.tcx, i.item_id).unwrap();
|
2025-03-19 08:59:19 +00:00
|
|
|
let LevelAndSource { level, src, .. } =
|
2025-03-19 09:41:38 +00:00
|
|
|
self.ctx.tcx.lint_level_at_node(MISSING_DOCS, hir_id);
|
2021-09-06 16:26:49 +02:00
|
|
|
|
|
|
|
|
// In case we have:
|
|
|
|
|
//
|
|
|
|
|
// ```
|
|
|
|
|
// enum Foo { Bar(u32) }
|
|
|
|
|
// // or:
|
|
|
|
|
// struct Bar(u32);
|
|
|
|
|
// ```
|
|
|
|
|
//
|
|
|
|
|
// there is no need to require documentation on the fields of tuple variants and
|
|
|
|
|
// tuple structs.
|
|
|
|
|
let should_be_ignored = i
|
2022-04-16 14:28:09 +02:00
|
|
|
.item_id
|
2021-09-06 16:26:49 +02:00
|
|
|
.as_def_id()
|
2022-04-25 22:08:45 +03:00
|
|
|
.and_then(|def_id| self.ctx.tcx.opt_parent(def_id))
|
2025-02-03 10:45:49 +11:00
|
|
|
.and_then(|def_id| self.ctx.tcx.hir_get_if_local(def_id))
|
2021-09-06 16:26:49 +02:00
|
|
|
.map(|node| {
|
|
|
|
|
matches!(
|
|
|
|
|
node,
|
|
|
|
|
hir::Node::Variant(hir::Variant {
|
2022-11-06 19:46:55 +00:00
|
|
|
data: hir::VariantData::Tuple(_, _, _),
|
2021-09-06 16:26:49 +02:00
|
|
|
..
|
|
|
|
|
}) | hir::Node::Item(hir::Item {
|
2025-05-28 15:18:48 +10:00
|
|
|
kind: hir::ItemKind::Struct(_, _, hir::VariantData::Tuple(_, _, _)),
|
2021-09-06 16:26:49 +02:00
|
|
|
..
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or(false);
|
|
|
|
|
|
2020-10-12 13:48:45 +02:00
|
|
|
// `missing_docs` is allow-by-default, so don't treat this as ignoring the item
|
2021-09-06 16:26:49 +02:00
|
|
|
// unless the user had an explicit `allow`.
|
|
|
|
|
//
|
|
|
|
|
let should_have_docs = !should_be_ignored
|
2025-03-19 09:41:38 +00:00
|
|
|
&& (level != lint::Level::Allow || matches!(src, LintLevelSource::Default));
|
2021-09-06 16:26:49 +02:00
|
|
|
|
2022-08-09 02:48:01 +00:00
|
|
|
if let Some(span) = i.span(self.ctx.tcx) {
|
|
|
|
|
let filename = span.filename(self.ctx.sess());
|
2023-08-14 22:25:32 +02:00
|
|
|
debug!("counting {:?} {:?} in {filename:?}", i.type_(), i.name);
|
2022-08-09 02:48:01 +00:00
|
|
|
self.items.entry(filename).or_default().count_item(
|
|
|
|
|
has_docs,
|
|
|
|
|
has_doc_example,
|
|
|
|
|
should_have_doc_example(self.ctx, i),
|
|
|
|
|
should_have_docs,
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-01-30 14:04:56 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-31 21:24:11 -07:00
|
|
|
self.visit_item_recur(i)
|
2019-01-30 14:04:56 -06:00
|
|
|
}
|
|
|
|
|
}
|