Generate AnyHasDocComments node

This commit is contained in:
Lukas Wirth 2022-01-07 12:37:58 +01:00
parent 2fb6f5e46a
commit 69dbfc7754
6 changed files with 95 additions and 21 deletions

View file

@ -21,7 +21,7 @@ jemalloc-ctl = { version = "0.4.1", package = "tikv-jemalloc-ctl", optional = tr
perf-event = "0.4"
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.8", features = ["psapi"] }
winapi = { version = "0.3.8", features = ["processthreadsapi", "psapi"] }
[features]
cpu_profiler = []

View file

@ -17,7 +17,7 @@ always-assert = { version = "0.1.2", features = ["log"] }
[target.'cfg(windows)'.dependencies]
miow = "0.4.0"
winapi = "0.3.9"
winapi = { version = "0.3.9", features = ["winerror"] }
[features]
# Uncomment to enable for the whole crate graph

View file

@ -216,6 +216,7 @@ pub struct SourceFile {
}
impl ast::HasAttrs for SourceFile {}
impl ast::HasModuleItem for SourceFile {}
impl ast::HasDocComments for SourceFile {}
impl SourceFile {
pub fn shebang_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![shebang]) }
}
@ -227,6 +228,7 @@ pub struct Const {
impl ast::HasAttrs for Const {}
impl ast::HasName for Const {}
impl ast::HasVisibility for Const {}
impl ast::HasDocComments for Const {}
impl Const {
pub fn default_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![default]) }
pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) }
@ -246,6 +248,7 @@ impl ast::HasAttrs for Enum {}
impl ast::HasName for Enum {}
impl ast::HasVisibility for Enum {}
impl ast::HasGenericParams for Enum {}
impl ast::HasDocComments for Enum {}
impl Enum {
pub fn enum_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![enum]) }
pub fn variant_list(&self) -> Option<VariantList> { support::child(&self.syntax) }
@ -283,6 +286,7 @@ impl ast::HasAttrs for Fn {}
impl ast::HasName for Fn {}
impl ast::HasVisibility for Fn {}
impl ast::HasGenericParams for Fn {}
impl ast::HasDocComments for Fn {}
impl Fn {
pub fn default_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![default]) }
pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) }
@ -303,6 +307,7 @@ pub struct Impl {
impl ast::HasAttrs for Impl {}
impl ast::HasVisibility for Impl {}
impl ast::HasGenericParams for Impl {}
impl ast::HasDocComments for Impl {}
impl Impl {
pub fn default_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![default]) }
pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) }
@ -320,6 +325,7 @@ pub struct MacroRules {
impl ast::HasAttrs for MacroRules {}
impl ast::HasName for MacroRules {}
impl ast::HasVisibility for MacroRules {}
impl ast::HasDocComments for MacroRules {}
impl MacroRules {
pub fn macro_rules_token(&self) -> Option<SyntaxToken> {
support::token(&self.syntax, T![macro_rules])
@ -335,6 +341,7 @@ pub struct MacroDef {
impl ast::HasAttrs for MacroDef {}
impl ast::HasName for MacroDef {}
impl ast::HasVisibility for MacroDef {}
impl ast::HasDocComments for MacroDef {}
impl MacroDef {
pub fn macro_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![macro]) }
pub fn args(&self) -> Option<TokenTree> { support::child(&self.syntax) }
@ -348,6 +355,7 @@ pub struct Module {
impl ast::HasAttrs for Module {}
impl ast::HasName for Module {}
impl ast::HasVisibility for Module {}
impl ast::HasDocComments for Module {}
impl Module {
pub fn mod_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mod]) }
pub fn item_list(&self) -> Option<ItemList> { support::child(&self.syntax) }
@ -361,6 +369,7 @@ pub struct Static {
impl ast::HasAttrs for Static {}
impl ast::HasName for Static {}
impl ast::HasVisibility for Static {}
impl ast::HasDocComments for Static {}
impl Static {
pub fn static_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![static]) }
pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) }
@ -379,6 +388,7 @@ impl ast::HasAttrs for Struct {}
impl ast::HasName for Struct {}
impl ast::HasVisibility for Struct {}
impl ast::HasGenericParams for Struct {}
impl ast::HasDocComments for Struct {}
impl Struct {
pub fn struct_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![struct]) }
pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) }
@ -394,6 +404,7 @@ impl ast::HasName for Trait {}
impl ast::HasVisibility for Trait {}
impl ast::HasGenericParams for Trait {}
impl ast::HasTypeBounds for Trait {}
impl ast::HasDocComments for Trait {}
impl Trait {
pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) }
pub fn auto_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![auto]) }
@ -410,6 +421,7 @@ impl ast::HasName for TypeAlias {}
impl ast::HasVisibility for TypeAlias {}
impl ast::HasGenericParams for TypeAlias {}
impl ast::HasTypeBounds for TypeAlias {}
impl ast::HasDocComments for TypeAlias {}
impl TypeAlias {
pub fn default_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![default]) }
pub fn type_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![type]) }
@ -426,6 +438,7 @@ impl ast::HasAttrs for Union {}
impl ast::HasName for Union {}
impl ast::HasVisibility for Union {}
impl ast::HasGenericParams for Union {}
impl ast::HasDocComments for Union {}
impl Union {
pub fn union_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![union]) }
pub fn record_field_list(&self) -> Option<RecordFieldList> { support::child(&self.syntax) }
@ -437,6 +450,7 @@ pub struct Use {
}
impl ast::HasAttrs for Use {}
impl ast::HasVisibility for Use {}
impl ast::HasDocComments for Use {}
impl Use {
pub fn use_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![use]) }
pub fn use_tree(&self) -> Option<UseTree> { support::child(&self.syntax) }
@ -582,6 +596,7 @@ pub struct RecordField {
impl ast::HasAttrs for RecordField {}
impl ast::HasName for RecordField {}
impl ast::HasVisibility for RecordField {}
impl ast::HasDocComments for RecordField {}
impl RecordField {
pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) }
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
@ -593,6 +608,7 @@ pub struct TupleField {
}
impl ast::HasAttrs for TupleField {}
impl ast::HasVisibility for TupleField {}
impl ast::HasDocComments for TupleField {}
impl TupleField {
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
}
@ -614,6 +630,7 @@ pub struct Variant {
impl ast::HasAttrs for Variant {}
impl ast::HasName for Variant {}
impl ast::HasVisibility for Variant {}
impl ast::HasDocComments for Variant {}
impl Variant {
pub fn field_list(&self) -> Option<FieldList> { support::child(&self.syntax) }
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
@ -1567,6 +1584,7 @@ pub enum Adt {
Union(Union),
}
impl ast::HasAttrs for Adt {}
impl ast::HasDocComments for Adt {}
impl ast::HasGenericParams for Adt {}
impl ast::HasName for Adt {}
impl ast::HasVisibility for Adt {}
@ -1609,6 +1627,12 @@ pub struct AnyHasAttrs {
}
impl ast::HasAttrs for AnyHasAttrs {}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AnyHasDocComments {
pub(crate) syntax: SyntaxNode,
}
impl ast::HasDocComments for AnyHasDocComments {}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct AnyHasGenericParams {
pub(crate) syntax: SyntaxNode,
@ -3869,6 +3893,27 @@ impl AstNode for AnyHasAttrs {
}
fn syntax(&self) -> &SyntaxNode { &self.syntax }
}
impl AnyHasDocComments {
#[inline]
pub fn new<T: ast::HasDocComments>(node: T) -> AnyHasDocComments {
AnyHasDocComments { syntax: node.syntax().clone() }
}
}
impl AstNode for AnyHasDocComments {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
SOURCE_FILE | CONST | ENUM | FN | IMPL | MACRO_RULES | MACRO_DEF | MODULE | STATIC
| STRUCT | TRAIT | TYPE_ALIAS | UNION | USE | RECORD_FIELD | TUPLE_FIELD | VARIANT => {
true
}
_ => false,
}
}
fn cast(syntax: SyntaxNode) -> Option<Self> {
Self::can_cast(syntax.kind()).then(|| AnyHasDocComments { syntax })
}
fn syntax(&self) -> &SyntaxNode { &self.syntax }
}
impl AnyHasGenericParams {
#[inline]
pub fn new<T: ast::HasGenericParams>(node: T) -> AnyHasGenericParams {

View file

@ -771,21 +771,4 @@ impl ast::HasLoopBody for ast::ForExpr {
}
}
impl ast::HasDocComments for ast::SourceFile {}
impl ast::HasDocComments for ast::Fn {}
impl ast::HasDocComments for ast::Struct {}
impl ast::HasDocComments for ast::Union {}
impl ast::HasDocComments for ast::RecordField {}
impl ast::HasDocComments for ast::TupleField {}
impl ast::HasDocComments for ast::Enum {}
impl ast::HasDocComments for ast::Variant {}
impl ast::HasDocComments for ast::Trait {}
impl ast::HasDocComments for ast::Module {}
impl ast::HasDocComments for ast::Static {}
impl ast::HasDocComments for ast::Const {}
impl ast::HasDocComments for ast::TypeAlias {}
impl ast::HasDocComments for ast::Impl {}
impl ast::HasDocComments for ast::MacroRules {}
impl ast::HasDocComments for ast::MacroDef {}
impl ast::HasDocComments for ast::Macro {}
impl ast::HasDocComments for ast::Use {}
impl ast::HasAttrs for ast::AnyHasDocComments {}

View file

@ -1,10 +1,12 @@
//! Various traits that are implemented by ast nodes.
//!
//! The implementations are usually trivial, and live in generated.rs
use itertools::Either;
use crate::{
ast::{self, support, AstChildren, AstNode, AstToken},
syntax_node::SyntaxElementChildren,
SyntaxToken, T,
SyntaxElement, SyntaxToken, T,
};
pub trait HasName: AstNode {
@ -74,6 +76,9 @@ pub trait HasDocComments: HasAttrs {
fn doc_comments(&self) -> CommentIter {
CommentIter { iter: self.syntax().children_with_tokens() }
}
fn doc_comments_and_attrs(&self) -> AttrCommentIter {
AttrCommentIter { iter: self.syntax().children_with_tokens() }
}
}
impl CommentIter {
@ -105,3 +110,17 @@ impl Iterator for CommentIter {
self.iter.by_ref().find_map(|el| el.into_token().and_then(ast::Comment::cast))
}
}
pub struct AttrCommentIter {
iter: SyntaxElementChildren,
}
impl Iterator for AttrCommentIter {
type Item = Either<ast::Comment, ast::Attr>;
fn next(&mut self) -> Option<Self::Item> {
self.iter.by_ref().find_map(|el| match el {
SyntaxElement::Node(node) => ast::Attr::cast(node).map(Either::Right),
SyntaxElement::Token(tok) => ast::Comment::cast(tok).map(Either::Left),
})
}
}

View file

@ -777,6 +777,33 @@ fn extract_struct_traits(ast: &mut AstSrc) {
extract_struct_trait(node, name, methods);
}
}
let nodes_with_doc_comments = [
"SourceFile",
"Fn",
"Struct",
"Union",
"RecordField",
"TupleField",
"Enum",
"Variant",
"Trait",
"Module",
"Static",
"Const",
"TypeAlias",
"Impl",
"MacroRules",
"MacroDef",
"Macro",
"Use",
];
for node in &mut ast.nodes {
if nodes_with_doc_comments.contains(&&*node.name) {
node.traits.push("HasDocComments".into());
}
}
}
fn extract_struct_trait(node: &mut AstNodeSrc, trait_name: &str, methods: &[&str]) {