Skip to content

Commit 69dbfc7

Browse files
committed
Generate AnyHasDocComments node
1 parent 2fb6f5e commit 69dbfc7

File tree

6 files changed

+95
-21
lines changed

6 files changed

+95
-21
lines changed

crates/profile/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jemalloc-ctl = { version = "0.4.1", package = "tikv-jemalloc-ctl", optional = tr
2121
perf-event = "0.4"
2222

2323
[target.'cfg(windows)'.dependencies]
24-
winapi = { version = "0.3.8", features = ["psapi"] }
24+
winapi = { version = "0.3.8", features = ["processthreadsapi", "psapi"] }
2525

2626
[features]
2727
cpu_profiler = []

crates/stdx/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ always-assert = { version = "0.1.2", features = ["log"] }
1717

1818
[target.'cfg(windows)'.dependencies]
1919
miow = "0.4.0"
20-
winapi = "0.3.9"
20+
winapi = { version = "0.3.9", features = ["winerror"] }
2121

2222
[features]
2323
# Uncomment to enable for the whole crate graph

crates/syntax/src/ast/generated/nodes.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ pub struct SourceFile {
216216
}
217217
impl ast::HasAttrs for SourceFile {}
218218
impl ast::HasModuleItem for SourceFile {}
219+
impl ast::HasDocComments for SourceFile {}
219220
impl SourceFile {
220221
pub fn shebang_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![shebang]) }
221222
}
@@ -227,6 +228,7 @@ pub struct Const {
227228
impl ast::HasAttrs for Const {}
228229
impl ast::HasName for Const {}
229230
impl ast::HasVisibility for Const {}
231+
impl ast::HasDocComments for Const {}
230232
impl Const {
231233
pub fn default_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![default]) }
232234
pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) }
@@ -246,6 +248,7 @@ impl ast::HasAttrs for Enum {}
246248
impl ast::HasName for Enum {}
247249
impl ast::HasVisibility for Enum {}
248250
impl ast::HasGenericParams for Enum {}
251+
impl ast::HasDocComments for Enum {}
249252
impl Enum {
250253
pub fn enum_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![enum]) }
251254
pub fn variant_list(&self) -> Option<VariantList> { support::child(&self.syntax) }
@@ -283,6 +286,7 @@ impl ast::HasAttrs for Fn {}
283286
impl ast::HasName for Fn {}
284287
impl ast::HasVisibility for Fn {}
285288
impl ast::HasGenericParams for Fn {}
289+
impl ast::HasDocComments for Fn {}
286290
impl Fn {
287291
pub fn default_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![default]) }
288292
pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) }
@@ -303,6 +307,7 @@ pub struct Impl {
303307
impl ast::HasAttrs for Impl {}
304308
impl ast::HasVisibility for Impl {}
305309
impl ast::HasGenericParams for Impl {}
310+
impl ast::HasDocComments for Impl {}
306311
impl Impl {
307312
pub fn default_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![default]) }
308313
pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) }
@@ -320,6 +325,7 @@ pub struct MacroRules {
320325
impl ast::HasAttrs for MacroRules {}
321326
impl ast::HasName for MacroRules {}
322327
impl ast::HasVisibility for MacroRules {}
328+
impl ast::HasDocComments for MacroRules {}
323329
impl MacroRules {
324330
pub fn macro_rules_token(&self) -> Option<SyntaxToken> {
325331
support::token(&self.syntax, T![macro_rules])
@@ -335,6 +341,7 @@ pub struct MacroDef {
335341
impl ast::HasAttrs for MacroDef {}
336342
impl ast::HasName for MacroDef {}
337343
impl ast::HasVisibility for MacroDef {}
344+
impl ast::HasDocComments for MacroDef {}
338345
impl MacroDef {
339346
pub fn macro_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![macro]) }
340347
pub fn args(&self) -> Option<TokenTree> { support::child(&self.syntax) }
@@ -348,6 +355,7 @@ pub struct Module {
348355
impl ast::HasAttrs for Module {}
349356
impl ast::HasName for Module {}
350357
impl ast::HasVisibility for Module {}
358+
impl ast::HasDocComments for Module {}
351359
impl Module {
352360
pub fn mod_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mod]) }
353361
pub fn item_list(&self) -> Option<ItemList> { support::child(&self.syntax) }
@@ -361,6 +369,7 @@ pub struct Static {
361369
impl ast::HasAttrs for Static {}
362370
impl ast::HasName for Static {}
363371
impl ast::HasVisibility for Static {}
372+
impl ast::HasDocComments for Static {}
364373
impl Static {
365374
pub fn static_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![static]) }
366375
pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) }
@@ -379,6 +388,7 @@ impl ast::HasAttrs for Struct {}
379388
impl ast::HasName for Struct {}
380389
impl ast::HasVisibility for Struct {}
381390
impl ast::HasGenericParams for Struct {}
391+
impl ast::HasDocComments for Struct {}
382392
impl Struct {
383393
pub fn struct_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![struct]) }
384394
pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) }
@@ -394,6 +404,7 @@ impl ast::HasName for Trait {}
394404
impl ast::HasVisibility for Trait {}
395405
impl ast::HasGenericParams for Trait {}
396406
impl ast::HasTypeBounds for Trait {}
407+
impl ast::HasDocComments for Trait {}
397408
impl Trait {
398409
pub fn unsafe_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![unsafe]) }
399410
pub fn auto_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![auto]) }
@@ -410,6 +421,7 @@ impl ast::HasName for TypeAlias {}
410421
impl ast::HasVisibility for TypeAlias {}
411422
impl ast::HasGenericParams for TypeAlias {}
412423
impl ast::HasTypeBounds for TypeAlias {}
424+
impl ast::HasDocComments for TypeAlias {}
413425
impl TypeAlias {
414426
pub fn default_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![default]) }
415427
pub fn type_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![type]) }
@@ -426,6 +438,7 @@ impl ast::HasAttrs for Union {}
426438
impl ast::HasName for Union {}
427439
impl ast::HasVisibility for Union {}
428440
impl ast::HasGenericParams for Union {}
441+
impl ast::HasDocComments for Union {}
429442
impl Union {
430443
pub fn union_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![union]) }
431444
pub fn record_field_list(&self) -> Option<RecordFieldList> { support::child(&self.syntax) }
@@ -437,6 +450,7 @@ pub struct Use {
437450
}
438451
impl ast::HasAttrs for Use {}
439452
impl ast::HasVisibility for Use {}
453+
impl ast::HasDocComments for Use {}
440454
impl Use {
441455
pub fn use_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![use]) }
442456
pub fn use_tree(&self) -> Option<UseTree> { support::child(&self.syntax) }
@@ -582,6 +596,7 @@ pub struct RecordField {
582596
impl ast::HasAttrs for RecordField {}
583597
impl ast::HasName for RecordField {}
584598
impl ast::HasVisibility for RecordField {}
599+
impl ast::HasDocComments for RecordField {}
585600
impl RecordField {
586601
pub fn colon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![:]) }
587602
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
@@ -593,6 +608,7 @@ pub struct TupleField {
593608
}
594609
impl ast::HasAttrs for TupleField {}
595610
impl ast::HasVisibility for TupleField {}
611+
impl ast::HasDocComments for TupleField {}
596612
impl TupleField {
597613
pub fn ty(&self) -> Option<Type> { support::child(&self.syntax) }
598614
}
@@ -614,6 +630,7 @@ pub struct Variant {
614630
impl ast::HasAttrs for Variant {}
615631
impl ast::HasName for Variant {}
616632
impl ast::HasVisibility for Variant {}
633+
impl ast::HasDocComments for Variant {}
617634
impl Variant {
618635
pub fn field_list(&self) -> Option<FieldList> { support::child(&self.syntax) }
619636
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
@@ -1567,6 +1584,7 @@ pub enum Adt {
15671584
Union(Union),
15681585
}
15691586
impl ast::HasAttrs for Adt {}
1587+
impl ast::HasDocComments for Adt {}
15701588
impl ast::HasGenericParams for Adt {}
15711589
impl ast::HasName for Adt {}
15721590
impl ast::HasVisibility for Adt {}
@@ -1609,6 +1627,12 @@ pub struct AnyHasAttrs {
16091627
}
16101628
impl ast::HasAttrs for AnyHasAttrs {}
16111629

1630+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1631+
pub struct AnyHasDocComments {
1632+
pub(crate) syntax: SyntaxNode,
1633+
}
1634+
impl ast::HasDocComments for AnyHasDocComments {}
1635+
16121636
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
16131637
pub struct AnyHasGenericParams {
16141638
pub(crate) syntax: SyntaxNode,
@@ -3869,6 +3893,27 @@ impl AstNode for AnyHasAttrs {
38693893
}
38703894
fn syntax(&self) -> &SyntaxNode { &self.syntax }
38713895
}
3896+
impl AnyHasDocComments {
3897+
#[inline]
3898+
pub fn new<T: ast::HasDocComments>(node: T) -> AnyHasDocComments {
3899+
AnyHasDocComments { syntax: node.syntax().clone() }
3900+
}
3901+
}
3902+
impl AstNode for AnyHasDocComments {
3903+
fn can_cast(kind: SyntaxKind) -> bool {
3904+
match kind {
3905+
SOURCE_FILE | CONST | ENUM | FN | IMPL | MACRO_RULES | MACRO_DEF | MODULE | STATIC
3906+
| STRUCT | TRAIT | TYPE_ALIAS | UNION | USE | RECORD_FIELD | TUPLE_FIELD | VARIANT => {
3907+
true
3908+
}
3909+
_ => false,
3910+
}
3911+
}
3912+
fn cast(syntax: SyntaxNode) -> Option<Self> {
3913+
Self::can_cast(syntax.kind()).then(|| AnyHasDocComments { syntax })
3914+
}
3915+
fn syntax(&self) -> &SyntaxNode { &self.syntax }
3916+
}
38723917
impl AnyHasGenericParams {
38733918
#[inline]
38743919
pub fn new<T: ast::HasGenericParams>(node: T) -> AnyHasGenericParams {

crates/syntax/src/ast/node_ext.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -771,21 +771,4 @@ impl ast::HasLoopBody for ast::ForExpr {
771771
}
772772
}
773773

774-
impl ast::HasDocComments for ast::SourceFile {}
775-
impl ast::HasDocComments for ast::Fn {}
776-
impl ast::HasDocComments for ast::Struct {}
777-
impl ast::HasDocComments for ast::Union {}
778-
impl ast::HasDocComments for ast::RecordField {}
779-
impl ast::HasDocComments for ast::TupleField {}
780-
impl ast::HasDocComments for ast::Enum {}
781-
impl ast::HasDocComments for ast::Variant {}
782-
impl ast::HasDocComments for ast::Trait {}
783-
impl ast::HasDocComments for ast::Module {}
784-
impl ast::HasDocComments for ast::Static {}
785-
impl ast::HasDocComments for ast::Const {}
786-
impl ast::HasDocComments for ast::TypeAlias {}
787-
impl ast::HasDocComments for ast::Impl {}
788-
impl ast::HasDocComments for ast::MacroRules {}
789-
impl ast::HasDocComments for ast::MacroDef {}
790-
impl ast::HasDocComments for ast::Macro {}
791-
impl ast::HasDocComments for ast::Use {}
774+
impl ast::HasAttrs for ast::AnyHasDocComments {}

crates/syntax/src/ast/traits.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
//! Various traits that are implemented by ast nodes.
22
//!
33
//! The implementations are usually trivial, and live in generated.rs
4+
use itertools::Either;
5+
46
use crate::{
57
ast::{self, support, AstChildren, AstNode, AstToken},
68
syntax_node::SyntaxElementChildren,
7-
SyntaxToken, T,
9+
SyntaxElement, SyntaxToken, T,
810
};
911

1012
pub trait HasName: AstNode {
@@ -74,6 +76,9 @@ pub trait HasDocComments: HasAttrs {
7476
fn doc_comments(&self) -> CommentIter {
7577
CommentIter { iter: self.syntax().children_with_tokens() }
7678
}
79+
fn doc_comments_and_attrs(&self) -> AttrCommentIter {
80+
AttrCommentIter { iter: self.syntax().children_with_tokens() }
81+
}
7782
}
7883

7984
impl CommentIter {
@@ -105,3 +110,17 @@ impl Iterator for CommentIter {
105110
self.iter.by_ref().find_map(|el| el.into_token().and_then(ast::Comment::cast))
106111
}
107112
}
113+
114+
pub struct AttrCommentIter {
115+
iter: SyntaxElementChildren,
116+
}
117+
118+
impl Iterator for AttrCommentIter {
119+
type Item = Either<ast::Comment, ast::Attr>;
120+
fn next(&mut self) -> Option<Self::Item> {
121+
self.iter.by_ref().find_map(|el| match el {
122+
SyntaxElement::Node(node) => ast::Attr::cast(node).map(Either::Right),
123+
SyntaxElement::Token(tok) => ast::Comment::cast(tok).map(Either::Left),
124+
})
125+
}
126+
}

crates/syntax/src/tests/sourcegen_ast.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,33 @@ fn extract_struct_traits(ast: &mut AstSrc) {
777777
extract_struct_trait(node, name, methods);
778778
}
779779
}
780+
781+
let nodes_with_doc_comments = [
782+
"SourceFile",
783+
"Fn",
784+
"Struct",
785+
"Union",
786+
"RecordField",
787+
"TupleField",
788+
"Enum",
789+
"Variant",
790+
"Trait",
791+
"Module",
792+
"Static",
793+
"Const",
794+
"TypeAlias",
795+
"Impl",
796+
"MacroRules",
797+
"MacroDef",
798+
"Macro",
799+
"Use",
800+
];
801+
802+
for node in &mut ast.nodes {
803+
if nodes_with_doc_comments.contains(&&*node.name) {
804+
node.traits.push("HasDocComments".into());
805+
}
806+
}
780807
}
781808

782809
fn extract_struct_trait(node: &mut AstNodeSrc, trait_name: &str, methods: &[&str]) {

0 commit comments

Comments
 (0)