-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rustdoc: add option to calculate "documentation coverage" #58626
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
009c91a
add option to calculate documentation coverage
QuietMisdreavus 9e98a25
tabs -> spaces
QuietMisdreavus fc94593
count fewer items in calculate-doc-coverage
QuietMisdreavus 95500c0
refactor: combine item count numbers into a new struct
QuietMisdreavus 5eb1ab5
print doc coverage as a table of individual item types
QuietMisdreavus a3a2559
add a coverage mode for private items
QuietMisdreavus 3ce19b4
tweak wording of extern types
QuietMisdreavus 63bdd29
add tests for doc coverage
QuietMisdreavus 80b4919
update docs for doc coverage
QuietMisdreavus 1b63543
track items per-file instead of per-type
QuietMisdreavus 74cf1ad
tweak docs for rustdoc's `--show-coverage`
QuietMisdreavus 515dbe7
update rustdoc coverage tests with new table layout
QuietMisdreavus e28cf74
remove unused Display impl
QuietMisdreavus 3df0b89
only print coverage pass lists if running on nightly
QuietMisdreavus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
use crate::clean; | ||
use crate::core::DocContext; | ||
use crate::fold::{self, DocFolder}; | ||
use crate::passes::Pass; | ||
|
||
use syntax::attr; | ||
use syntax_pos::FileName; | ||
|
||
use std::collections::BTreeMap; | ||
use std::ops; | ||
|
||
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", | ||
}; | ||
|
||
fn calculate_doc_coverage(krate: clean::Crate, _: &DocContext<'_, '_, '_>) -> clean::Crate { | ||
let mut calc = CoverageCalculator::default(); | ||
let krate = calc.fold_crate(krate); | ||
|
||
calc.print_results(); | ||
|
||
krate | ||
} | ||
|
||
#[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 | ||
} | ||
} | ||
} | ||
|
||
impl ops::Sub for ItemCount { | ||
type Output = Self; | ||
|
||
fn sub(self, rhs: Self) -> Self { | ||
ItemCount { | ||
total: self.total - rhs.total, | ||
with_docs: self.with_docs - rhs.with_docs, | ||
} | ||
} | ||
} | ||
|
||
impl ops::AddAssign for ItemCount { | ||
fn add_assign(&mut self, rhs: Self) { | ||
self.total += rhs.total; | ||
self.with_docs += rhs.with_docs; | ||
} | ||
} | ||
|
||
#[derive(Default)] | ||
struct CoverageCalculator { | ||
items: BTreeMap<FileName, ItemCount>, | ||
} | ||
|
||
impl CoverageCalculator { | ||
fn print_results(&self) { | ||
let mut total = ItemCount::default(); | ||
|
||
fn print_table_line() { | ||
println!("+-{0:->35}-+-{0:->10}-+-{0:->10}-+-{0:->10}-+", ""); | ||
} | ||
|
||
fn print_table_record(name: &str, count: ItemCount, percentage: f64) { | ||
println!("| {:<35} | {:>10} | {:>10} | {:>9.1}% |", | ||
name, count.with_docs, count.total, percentage); | ||
} | ||
|
||
print_table_line(); | ||
println!("| {:<35} | {:>10} | {:>10} | {:>10} |", | ||
"File", "Documented", "Total", "Percentage"); | ||
print_table_line(); | ||
|
||
for (file, &count) in &self.items { | ||
if let Some(percentage) = count.percentage() { | ||
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..]; | ||
} | ||
|
||
print_table_record(&name, count, percentage); | ||
|
||
total += count; | ||
} | ||
} | ||
|
||
print_table_line(); | ||
print_table_record("Total", total, total.percentage().unwrap_or(0.0)); | ||
print_table_line(); | ||
} | ||
} | ||
|
||
impl fold::DocFolder for CoverageCalculator { | ||
fn fold_item(&mut self, i: clean::Item) -> Option<clean::Item> { | ||
let has_docs = !i.attrs.doc_strings.is_empty(); | ||
|
||
match i.inner { | ||
_ 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); | ||
} | ||
clean::StrippedItem(..) => { | ||
// don't count items in stripped modules | ||
return Some(i); | ||
} | ||
clean::ImportItem(..) | clean::ExternCrateItem(..) => { | ||
// docs on `use` and `extern crate` statements are not displayed, so they're not | ||
// worth counting | ||
return Some(i); | ||
} | ||
clean::ImplItem(ref impl_) | ||
if attr::contains_name(&i.attrs.other_attrs, "automatically_derived") | ||
|| impl_.synthetic || impl_.blanket_impl.is_some() => | ||
{ | ||
// built-in derives get the `#[automatically_derived]` attribute, and | ||
// synthetic/blanket impls are made up by rustdoc and can't be documented | ||
// FIXME(misdreavus): need to also find items that came out of a derive macro | ||
return Some(i); | ||
} | ||
clean::ImplItem(ref impl_) => { | ||
if let Some(ref tr) = impl_.trait_ { | ||
debug!("impl {:#} for {:#} in {}", tr, impl_.for_, i.source.filename); | ||
|
||
// don't count trait impls, the missing-docs lint doesn't so we shouldn't | ||
// either | ||
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 | ||
debug!("impl {:#} in {}", impl_.for_, i.source.filename); | ||
} | ||
} | ||
_ => { | ||
debug!("counting {} {:?} in {}", i.type_(), i.name, i.source.filename); | ||
self.items.entry(i.source.filename.clone()) | ||
.or_default() | ||
.count_item(has_docs); | ||
} | ||
} | ||
|
||
self.fold_item_recur(i) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to call
rust_input
inshow_coverage
case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rust_input
is what actually calls thecore::run
documentation loading code, so i wanted to go through that to make sure that it used the same cleaning/filtering process that regular docs loading did.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems a bit overkill but let's go along for now...