diff --git a/rust/ast-generator/templates/extractor.mustache b/rust/ast-generator/templates/extractor.mustache index 0ce5b863f267..b94f8a6043a7 100644 --- a/rust/ast-generator/templates/extractor.mustache +++ b/rust/ast-generator/templates/extractor.mustache @@ -2,7 +2,7 @@ use super::base::Translator; use super::mappings::TextValue; -use crate::emit_detached; +use crate::{pre_emit,post_emit}; use crate::generated; use crate::trap::{Label, TrapId}; use ra_ap_syntax::ast::{ @@ -22,18 +22,20 @@ impl Translator<'_> { {{#enums}} pub(crate) fn emit_{{snake_case_name}}(&mut self, node: &ast::{{ast_name}}) -> Option> { + pre_emit!({{name}}, self, node); let label = match node { {{#variants}} ast::{{ast_name}}::{{variant_ast_name}}(inner) => self.emit_{{snake_case_name}}(inner).map(Into::into), {{/variants}} }?; - emit_detached!({{name}}, self, node, label); + post_emit!({{name}}, self, node, label); Some(label) } {{/enums}} {{#nodes}} pub(crate) fn emit_{{snake_case_name}}(&mut self, node: &ast::{{ast_name}}) -> Option> { + pre_emit!({{name}}, self, node); if self.should_be_excluded(node) { return None; } {{#has_attrs}} if self.should_be_excluded_attrs(node) { return None; } @@ -59,7 +61,7 @@ impl Translator<'_> { {{/fields}} }); self.emit_location(label, node); - emit_detached!({{name}}, self, node, label); + post_emit!({{name}}, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } diff --git a/rust/extractor/src/translate/base.rs b/rust/extractor/src/translate/base.rs index ece10f2f0f29..daf80fa45173 100644 --- a/rust/extractor/src/translate/base.rs +++ b/rust/extractor/src/translate/base.rs @@ -11,7 +11,7 @@ use ra_ap_hir::{ }; use ra_ap_hir_def::ModuleId; use ra_ap_hir_def::type_ref::Mutability; -use ra_ap_hir_expand::{ExpandResult, ExpandTo}; +use ra_ap_hir_expand::{ExpandResult, ExpandTo, InFile}; use ra_ap_ide_db::RootDatabase; use ra_ap_ide_db::line_index::{LineCol, LineIndex}; use ra_ap_parser::SyntaxKind; @@ -23,7 +23,15 @@ use ra_ap_syntax::{ }; #[macro_export] -macro_rules! emit_detached { +macro_rules! pre_emit { + (Item, $self:ident, $node:ident) => { + $self.setup_item_expansion($node); + }; + ($($_:tt)*) => {}; +} + +#[macro_export] +macro_rules! post_emit { (MacroCall, $self:ident, $node:ident, $label:ident) => { $self.extract_macro_call_expanded($node, $label); }; @@ -106,8 +114,9 @@ pub struct Translator<'a> { line_index: LineIndex, file_id: Option, pub semantics: Option<&'a Semantics<'a, RootDatabase>>, - resolve_paths: ResolvePaths, + resolve_paths: bool, source_kind: SourceKind, + macro_context_depth: usize, } const UNKNOWN_LOCATION: (LineCol, LineCol) = @@ -130,8 +139,9 @@ impl<'a> Translator<'a> { line_index, file_id: semantic_info.map(|i| i.file_id), semantics: semantic_info.map(|i| i.semantics), - resolve_paths, + resolve_paths: resolve_paths == ResolvePaths::Yes, source_kind, + macro_context_depth: 0, } } fn location(&self, range: TextRange) -> Option<(LineCol, LineCol)> { @@ -337,6 +347,11 @@ impl<'a> Translator<'a> { mcall: &ast::MacroCall, label: Label, ) { + if self.macro_context_depth > 0 { + // we are in an attribute macro, don't emit anything: we would be failing to expand any + // way as from version 0.0.274 rust-analyser only expands in the context of an expansion + return; + } if let Some(expanded) = self .semantics .as_ref() @@ -537,7 +552,7 @@ impl<'a> Translator<'a> { item: &T, label: Label, ) { - if self.resolve_paths == ResolvePaths::No { + if !self.resolve_paths { return; } (|| { @@ -560,7 +575,7 @@ impl<'a> Translator<'a> { item: &ast::Variant, label: Label, ) { - if self.resolve_paths == ResolvePaths::No { + if !self.resolve_paths { return; } (|| { @@ -583,7 +598,7 @@ impl<'a> Translator<'a> { item: &impl PathAst, label: Label, ) { - if self.resolve_paths == ResolvePaths::No { + if !self.resolve_paths { return; } (|| { @@ -606,7 +621,7 @@ impl<'a> Translator<'a> { item: &ast::MethodCallExpr, label: Label, ) { - if self.resolve_paths == ResolvePaths::No { + if !self.resolve_paths { return; } (|| { @@ -708,6 +723,16 @@ impl<'a> Translator<'a> { } } + pub(crate) fn setup_item_expansion(&mut self, node: &ast::Item) { + if self.semantics.is_some_and(|s| { + let file = s.hir_file_for(node.syntax()); + let node = InFile::new(file, node); + s.is_attr_macro_call(node) + }) { + self.macro_context_depth += 1; + } + } + pub(crate) fn emit_item_expansion(&mut self, node: &ast::Item, label: Label) { // TODO: remove this after fixing exponential expansion on libraries like funty-2.0.0 if self.source_kind == SourceKind::Library { @@ -715,6 +740,16 @@ impl<'a> Translator<'a> { } (|| { let semantics = self.semantics?; + let file = semantics.hir_file_for(node.syntax()); + let infile_node = InFile::new(file, node); + if !semantics.is_attr_macro_call(infile_node) { + return None; + } + self.macro_context_depth -= 1; + if self.macro_context_depth > 0 { + // only expand the outermost attribute macro + return None; + } let ExpandResult { value: expanded, .. } = semantics.expand_attr_macro(node)?; diff --git a/rust/extractor/src/translate/generated.rs b/rust/extractor/src/translate/generated.rs index 84159f970c9a..cd0c2f9a9d96 100644 --- a/rust/extractor/src/translate/generated.rs +++ b/rust/extractor/src/translate/generated.rs @@ -1,9 +1,9 @@ //! Generated by `ast-generator`, do not edit by hand. use super::base::Translator; use super::mappings::TextValue; -use crate::emit_detached; use crate::generated; use crate::trap::{Label, TrapId}; +use crate::{post_emit, pre_emit}; use ra_ap_syntax::ast::{ HasArgList, HasAttrs, HasGenericArgs, HasGenericParams, HasLoopBody, HasModuleItem, HasName, HasTypeBounds, HasVisibility, RangeItem, @@ -21,6 +21,7 @@ impl Translator<'_> { &mut self, node: &ast::AsmOperand, ) -> Option> { + pre_emit!(AsmOperand, self, node); let label = match node { ast::AsmOperand::AsmConst(inner) => self.emit_asm_const(inner).map(Into::into), ast::AsmOperand::AsmLabel(inner) => self.emit_asm_label(inner).map(Into::into), @@ -29,13 +30,14 @@ impl Translator<'_> { } ast::AsmOperand::AsmSym(inner) => self.emit_asm_sym(inner).map(Into::into), }?; - emit_detached!(AsmOperand, self, node, label); + post_emit!(AsmOperand, self, node, label); Some(label) } pub(crate) fn emit_asm_piece( &mut self, node: &ast::AsmPiece, ) -> Option> { + pre_emit!(AsmPiece, self, node); let label = match node { ast::AsmPiece::AsmClobberAbi(inner) => self.emit_asm_clobber_abi(inner).map(Into::into), ast::AsmPiece::AsmOperandNamed(inner) => { @@ -43,23 +45,25 @@ impl Translator<'_> { } ast::AsmPiece::AsmOptions(inner) => self.emit_asm_options(inner).map(Into::into), }?; - emit_detached!(AsmPiece, self, node, label); + post_emit!(AsmPiece, self, node, label); Some(label) } pub(crate) fn emit_assoc_item( &mut self, node: &ast::AssocItem, ) -> Option> { + pre_emit!(AssocItem, self, node); let label = match node { ast::AssocItem::Const(inner) => self.emit_const(inner).map(Into::into), ast::AssocItem::Fn(inner) => self.emit_fn(inner).map(Into::into), ast::AssocItem::MacroCall(inner) => self.emit_macro_call(inner).map(Into::into), ast::AssocItem::TypeAlias(inner) => self.emit_type_alias(inner).map(Into::into), }?; - emit_detached!(AssocItem, self, node, label); + post_emit!(AssocItem, self, node, label); Some(label) } pub(crate) fn emit_expr(&mut self, node: &ast::Expr) -> Option> { + pre_emit!(Expr, self, node); let label = match node { ast::Expr::ArrayExpr(inner) => self.emit_array_expr(inner).map(Into::into), ast::Expr::AsmExpr(inner) => self.emit_asm_expr(inner).map(Into::into), @@ -98,26 +102,28 @@ impl Translator<'_> { ast::Expr::YeetExpr(inner) => self.emit_yeet_expr(inner).map(Into::into), ast::Expr::YieldExpr(inner) => self.emit_yield_expr(inner).map(Into::into), }?; - emit_detached!(Expr, self, node, label); + post_emit!(Expr, self, node, label); Some(label) } pub(crate) fn emit_extern_item( &mut self, node: &ast::ExternItem, ) -> Option> { + pre_emit!(ExternItem, self, node); let label = match node { ast::ExternItem::Fn(inner) => self.emit_fn(inner).map(Into::into), ast::ExternItem::MacroCall(inner) => self.emit_macro_call(inner).map(Into::into), ast::ExternItem::Static(inner) => self.emit_static(inner).map(Into::into), ast::ExternItem::TypeAlias(inner) => self.emit_type_alias(inner).map(Into::into), }?; - emit_detached!(ExternItem, self, node, label); + post_emit!(ExternItem, self, node, label); Some(label) } pub(crate) fn emit_field_list( &mut self, node: &ast::FieldList, ) -> Option> { + pre_emit!(FieldList, self, node); let label = match node { ast::FieldList::RecordFieldList(inner) => { self.emit_record_field_list(inner).map(Into::into) @@ -126,26 +132,28 @@ impl Translator<'_> { self.emit_tuple_field_list(inner).map(Into::into) } }?; - emit_detached!(FieldList, self, node, label); + post_emit!(FieldList, self, node, label); Some(label) } pub(crate) fn emit_generic_arg( &mut self, node: &ast::GenericArg, ) -> Option> { + pre_emit!(GenericArg, self, node); let label = match node { ast::GenericArg::AssocTypeArg(inner) => self.emit_assoc_type_arg(inner).map(Into::into), ast::GenericArg::ConstArg(inner) => self.emit_const_arg(inner).map(Into::into), ast::GenericArg::LifetimeArg(inner) => self.emit_lifetime_arg(inner).map(Into::into), ast::GenericArg::TypeArg(inner) => self.emit_type_arg(inner).map(Into::into), }?; - emit_detached!(GenericArg, self, node, label); + post_emit!(GenericArg, self, node, label); Some(label) } pub(crate) fn emit_generic_param( &mut self, node: &ast::GenericParam, ) -> Option> { + pre_emit!(GenericParam, self, node); let label = match node { ast::GenericParam::ConstParam(inner) => self.emit_const_param(inner).map(Into::into), ast::GenericParam::LifetimeParam(inner) => { @@ -153,10 +161,11 @@ impl Translator<'_> { } ast::GenericParam::TypeParam(inner) => self.emit_type_param(inner).map(Into::into), }?; - emit_detached!(GenericParam, self, node, label); + post_emit!(GenericParam, self, node, label); Some(label) } pub(crate) fn emit_pat(&mut self, node: &ast::Pat) -> Option> { + pre_emit!(Pat, self, node); let label = match node { ast::Pat::BoxPat(inner) => self.emit_box_pat(inner).map(Into::into), ast::Pat::ConstBlockPat(inner) => self.emit_const_block_pat(inner).map(Into::into), @@ -175,19 +184,21 @@ impl Translator<'_> { ast::Pat::TupleStructPat(inner) => self.emit_tuple_struct_pat(inner).map(Into::into), ast::Pat::WildcardPat(inner) => self.emit_wildcard_pat(inner).map(Into::into), }?; - emit_detached!(Pat, self, node, label); + post_emit!(Pat, self, node, label); Some(label) } pub(crate) fn emit_stmt(&mut self, node: &ast::Stmt) -> Option> { + pre_emit!(Stmt, self, node); let label = match node { ast::Stmt::ExprStmt(inner) => self.emit_expr_stmt(inner).map(Into::into), ast::Stmt::Item(inner) => self.emit_item(inner).map(Into::into), ast::Stmt::LetStmt(inner) => self.emit_let_stmt(inner).map(Into::into), }?; - emit_detached!(Stmt, self, node, label); + post_emit!(Stmt, self, node, label); Some(label) } pub(crate) fn emit_type(&mut self, node: &ast::Type) -> Option> { + pre_emit!(TypeRepr, self, node); let label = match node { ast::Type::ArrayType(inner) => self.emit_array_type(inner).map(Into::into), ast::Type::DynTraitType(inner) => self.emit_dyn_trait_type(inner).map(Into::into), @@ -204,21 +215,23 @@ impl Translator<'_> { ast::Type::SliceType(inner) => self.emit_slice_type(inner).map(Into::into), ast::Type::TupleType(inner) => self.emit_tuple_type(inner).map(Into::into), }?; - emit_detached!(TypeRepr, self, node, label); + post_emit!(TypeRepr, self, node, label); Some(label) } pub(crate) fn emit_use_bound_generic_arg( &mut self, node: &ast::UseBoundGenericArg, ) -> Option> { + pre_emit!(UseBoundGenericArg, self, node); let label = match node { ast::UseBoundGenericArg::Lifetime(inner) => self.emit_lifetime(inner).map(Into::into), ast::UseBoundGenericArg::NameRef(inner) => self.emit_name_ref(inner).map(Into::into), }?; - emit_detached!(UseBoundGenericArg, self, node, label); + post_emit!(UseBoundGenericArg, self, node, label); Some(label) } pub(crate) fn emit_item(&mut self, node: &ast::Item) -> Option> { + pre_emit!(Item, self, node); let label = match node { ast::Item::Const(inner) => self.emit_const(inner).map(Into::into), ast::Item::Enum(inner) => self.emit_enum(inner).map(Into::into), @@ -238,10 +251,11 @@ impl Translator<'_> { ast::Item::Union(inner) => self.emit_union(inner).map(Into::into), ast::Item::Use(inner) => self.emit_use(inner).map(Into::into), }?; - emit_detached!(Item, self, node, label); + post_emit!(Item, self, node, label); Some(label) } pub(crate) fn emit_abi(&mut self, node: &ast::Abi) -> Option> { + pre_emit!(Abi, self, node); if self.should_be_excluded(node) { return None; } @@ -251,7 +265,7 @@ impl Translator<'_> { abi_string, }); self.emit_location(label, node); - emit_detached!(Abi, self, node, label); + post_emit!(Abi, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -259,6 +273,7 @@ impl Translator<'_> { &mut self, node: &ast::ArgList, ) -> Option> { + pre_emit!(ArgList, self, node); if self.should_be_excluded(node) { return None; } @@ -268,7 +283,7 @@ impl Translator<'_> { args, }); self.emit_location(label, node); - emit_detached!(ArgList, self, node, label); + post_emit!(ArgList, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -276,6 +291,7 @@ impl Translator<'_> { &mut self, node: &ast::ArrayExpr, ) -> Option> { + pre_emit!(ArrayExprInternal, self, node); if self.should_be_excluded(node) { return None; } @@ -292,7 +308,7 @@ impl Translator<'_> { is_semicolon, }); self.emit_location(label, node); - emit_detached!(ArrayExprInternal, self, node, label); + post_emit!(ArrayExprInternal, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -300,6 +316,7 @@ impl Translator<'_> { &mut self, node: &ast::ArrayType, ) -> Option> { + pre_emit!(ArrayTypeRepr, self, node); if self.should_be_excluded(node) { return None; } @@ -311,7 +328,7 @@ impl Translator<'_> { element_type_repr, }); self.emit_location(label, node); - emit_detached!(ArrayTypeRepr, self, node, label); + post_emit!(ArrayTypeRepr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -319,6 +336,7 @@ impl Translator<'_> { &mut self, node: &ast::AsmClobberAbi, ) -> Option> { + pre_emit!(AsmClobberAbi, self, node); if self.should_be_excluded(node) { return None; } @@ -326,7 +344,7 @@ impl Translator<'_> { .trap .emit(generated::AsmClobberAbi { id: TrapId::Star }); self.emit_location(label, node); - emit_detached!(AsmClobberAbi, self, node, label); + post_emit!(AsmClobberAbi, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -334,6 +352,7 @@ impl Translator<'_> { &mut self, node: &ast::AsmConst, ) -> Option> { + pre_emit!(AsmConst, self, node); if self.should_be_excluded(node) { return None; } @@ -345,7 +364,7 @@ impl Translator<'_> { is_const, }); self.emit_location(label, node); - emit_detached!(AsmConst, self, node, label); + post_emit!(AsmConst, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -353,12 +372,13 @@ impl Translator<'_> { &mut self, node: &ast::AsmDirSpec, ) -> Option> { + pre_emit!(AsmDirSpec, self, node); if self.should_be_excluded(node) { return None; } let label = self.trap.emit(generated::AsmDirSpec { id: TrapId::Star }); self.emit_location(label, node); - emit_detached!(AsmDirSpec, self, node, label); + post_emit!(AsmDirSpec, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -366,6 +386,7 @@ impl Translator<'_> { &mut self, node: &ast::AsmExpr, ) -> Option> { + pre_emit!(AsmExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -385,7 +406,7 @@ impl Translator<'_> { template, }); self.emit_location(label, node); - emit_detached!(AsmExpr, self, node, label); + post_emit!(AsmExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -393,6 +414,7 @@ impl Translator<'_> { &mut self, node: &ast::AsmLabel, ) -> Option> { + pre_emit!(AsmLabel, self, node); if self.should_be_excluded(node) { return None; } @@ -402,7 +424,7 @@ impl Translator<'_> { block_expr, }); self.emit_location(label, node); - emit_detached!(AsmLabel, self, node, label); + post_emit!(AsmLabel, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -410,6 +432,7 @@ impl Translator<'_> { &mut self, node: &ast::AsmOperandExpr, ) -> Option> { + pre_emit!(AsmOperandExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -421,7 +444,7 @@ impl Translator<'_> { out_expr, }); self.emit_location(label, node); - emit_detached!(AsmOperandExpr, self, node, label); + post_emit!(AsmOperandExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -429,6 +452,7 @@ impl Translator<'_> { &mut self, node: &ast::AsmOperandNamed, ) -> Option> { + pre_emit!(AsmOperandNamed, self, node); if self.should_be_excluded(node) { return None; } @@ -440,7 +464,7 @@ impl Translator<'_> { name, }); self.emit_location(label, node); - emit_detached!(AsmOperandNamed, self, node, label); + post_emit!(AsmOperandNamed, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -448,6 +472,7 @@ impl Translator<'_> { &mut self, node: &ast::AsmOption, ) -> Option> { + pre_emit!(AsmOption, self, node); if self.should_be_excluded(node) { return None; } @@ -457,7 +482,7 @@ impl Translator<'_> { is_raw, }); self.emit_location(label, node); - emit_detached!(AsmOption, self, node, label); + post_emit!(AsmOption, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -465,6 +490,7 @@ impl Translator<'_> { &mut self, node: &ast::AsmOptions, ) -> Option> { + pre_emit!(AsmOptionsList, self, node); if self.should_be_excluded(node) { return None; } @@ -477,7 +503,7 @@ impl Translator<'_> { asm_options, }); self.emit_location(label, node); - emit_detached!(AsmOptionsList, self, node, label); + post_emit!(AsmOptionsList, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -485,6 +511,7 @@ impl Translator<'_> { &mut self, node: &ast::AsmRegOperand, ) -> Option> { + pre_emit!(AsmRegOperand, self, node); if self.should_be_excluded(node) { return None; } @@ -500,7 +527,7 @@ impl Translator<'_> { asm_reg_spec, }); self.emit_location(label, node); - emit_detached!(AsmRegOperand, self, node, label); + post_emit!(AsmRegOperand, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -508,6 +535,7 @@ impl Translator<'_> { &mut self, node: &ast::AsmRegSpec, ) -> Option> { + pre_emit!(AsmRegSpec, self, node); if self.should_be_excluded(node) { return None; } @@ -517,11 +545,12 @@ impl Translator<'_> { identifier, }); self.emit_location(label, node); - emit_detached!(AsmRegSpec, self, node, label); + post_emit!(AsmRegSpec, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_asm_sym(&mut self, node: &ast::AsmSym) -> Option> { + pre_emit!(AsmSym, self, node); if self.should_be_excluded(node) { return None; } @@ -531,7 +560,7 @@ impl Translator<'_> { path, }); self.emit_location(label, node); - emit_detached!(AsmSym, self, node, label); + post_emit!(AsmSym, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -539,6 +568,7 @@ impl Translator<'_> { &mut self, node: &ast::AssocItemList, ) -> Option> { + pre_emit!(AssocItemList, self, node); if self.should_be_excluded(node) { return None; } @@ -556,7 +586,7 @@ impl Translator<'_> { attrs, }); self.emit_location(label, node); - emit_detached!(AssocItemList, self, node, label); + post_emit!(AssocItemList, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -564,6 +594,7 @@ impl Translator<'_> { &mut self, node: &ast::AssocTypeArg, ) -> Option> { + pre_emit!(AssocTypeArg, self, node); if self.should_be_excluded(node) { return None; } @@ -593,11 +624,12 @@ impl Translator<'_> { type_bound_list, }); self.emit_location(label, node); - emit_detached!(AssocTypeArg, self, node, label); + post_emit!(AssocTypeArg, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_attr(&mut self, node: &ast::Attr) -> Option> { + pre_emit!(Attr, self, node); if self.should_be_excluded(node) { return None; } @@ -607,7 +639,7 @@ impl Translator<'_> { meta, }); self.emit_location(label, node); - emit_detached!(Attr, self, node, label); + post_emit!(Attr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -615,6 +647,7 @@ impl Translator<'_> { &mut self, node: &ast::AwaitExpr, ) -> Option> { + pre_emit!(AwaitExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -629,7 +662,7 @@ impl Translator<'_> { expr, }); self.emit_location(label, node); - emit_detached!(AwaitExpr, self, node, label); + post_emit!(AwaitExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -637,6 +670,7 @@ impl Translator<'_> { &mut self, node: &ast::BecomeExpr, ) -> Option> { + pre_emit!(BecomeExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -651,7 +685,7 @@ impl Translator<'_> { expr, }); self.emit_location(label, node); - emit_detached!(BecomeExpr, self, node, label); + post_emit!(BecomeExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -659,6 +693,7 @@ impl Translator<'_> { &mut self, node: &ast::BinExpr, ) -> Option> { + pre_emit!(BinaryExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -677,7 +712,7 @@ impl Translator<'_> { rhs, }); self.emit_location(label, node); - emit_detached!(BinaryExpr, self, node, label); + post_emit!(BinaryExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -685,6 +720,7 @@ impl Translator<'_> { &mut self, node: &ast::BlockExpr, ) -> Option> { + pre_emit!(BlockExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -713,11 +749,12 @@ impl Translator<'_> { stmt_list, }); self.emit_location(label, node); - emit_detached!(BlockExpr, self, node, label); + post_emit!(BlockExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_box_pat(&mut self, node: &ast::BoxPat) -> Option> { + pre_emit!(BoxPat, self, node); if self.should_be_excluded(node) { return None; } @@ -727,7 +764,7 @@ impl Translator<'_> { pat, }); self.emit_location(label, node); - emit_detached!(BoxPat, self, node, label); + post_emit!(BoxPat, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -735,6 +772,7 @@ impl Translator<'_> { &mut self, node: &ast::BreakExpr, ) -> Option> { + pre_emit!(BreakExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -751,7 +789,7 @@ impl Translator<'_> { lifetime, }); self.emit_location(label, node); - emit_detached!(BreakExpr, self, node, label); + post_emit!(BreakExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -759,6 +797,7 @@ impl Translator<'_> { &mut self, node: &ast::CallExpr, ) -> Option> { + pre_emit!(CallExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -775,7 +814,7 @@ impl Translator<'_> { function, }); self.emit_location(label, node); - emit_detached!(CallExpr, self, node, label); + post_emit!(CallExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -783,6 +822,7 @@ impl Translator<'_> { &mut self, node: &ast::CastExpr, ) -> Option> { + pre_emit!(CastExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -799,7 +839,7 @@ impl Translator<'_> { type_repr, }); self.emit_location(label, node); - emit_detached!(CastExpr, self, node, label); + post_emit!(CastExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -807,6 +847,7 @@ impl Translator<'_> { &mut self, node: &ast::ClosureBinder, ) -> Option> { + pre_emit!(ClosureBinder, self, node); if self.should_be_excluded(node) { return None; } @@ -818,7 +859,7 @@ impl Translator<'_> { generic_param_list, }); self.emit_location(label, node); - emit_detached!(ClosureBinder, self, node, label); + post_emit!(ClosureBinder, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -826,6 +867,7 @@ impl Translator<'_> { &mut self, node: &ast::ClosureExpr, ) -> Option> { + pre_emit!(ClosureExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -858,11 +900,12 @@ impl Translator<'_> { ret_type, }); self.emit_location(label, node); - emit_detached!(ClosureExpr, self, node, label); + post_emit!(ClosureExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_const(&mut self, node: &ast::Const) -> Option> { + pre_emit!(Const, self, node); if self.should_be_excluded(node) { return None; } @@ -887,7 +930,7 @@ impl Translator<'_> { visibility, }); self.emit_location(label, node); - emit_detached!(Const, self, node, label); + post_emit!(Const, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -895,6 +938,7 @@ impl Translator<'_> { &mut self, node: &ast::ConstArg, ) -> Option> { + pre_emit!(ConstArg, self, node); if self.should_be_excluded(node) { return None; } @@ -904,7 +948,7 @@ impl Translator<'_> { expr, }); self.emit_location(label, node); - emit_detached!(ConstArg, self, node, label); + post_emit!(ConstArg, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -912,6 +956,7 @@ impl Translator<'_> { &mut self, node: &ast::ConstBlockPat, ) -> Option> { + pre_emit!(ConstBlockPat, self, node); if self.should_be_excluded(node) { return None; } @@ -923,7 +968,7 @@ impl Translator<'_> { is_const, }); self.emit_location(label, node); - emit_detached!(ConstBlockPat, self, node, label); + post_emit!(ConstBlockPat, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -931,6 +976,7 @@ impl Translator<'_> { &mut self, node: &ast::ConstParam, ) -> Option> { + pre_emit!(ConstParam, self, node); if self.should_be_excluded(node) { return None; } @@ -951,7 +997,7 @@ impl Translator<'_> { type_repr, }); self.emit_location(label, node); - emit_detached!(ConstParam, self, node, label); + post_emit!(ConstParam, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -959,6 +1005,7 @@ impl Translator<'_> { &mut self, node: &ast::ContinueExpr, ) -> Option> { + pre_emit!(ContinueExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -973,7 +1020,7 @@ impl Translator<'_> { lifetime, }); self.emit_location(label, node); - emit_detached!(ContinueExpr, self, node, label); + post_emit!(ContinueExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -981,6 +1028,7 @@ impl Translator<'_> { &mut self, node: &ast::DynTraitType, ) -> Option> { + pre_emit!(DynTraitTypeRepr, self, node); if self.should_be_excluded(node) { return None; } @@ -992,11 +1040,12 @@ impl Translator<'_> { type_bound_list, }); self.emit_location(label, node); - emit_detached!(DynTraitTypeRepr, self, node, label); + post_emit!(DynTraitTypeRepr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_enum(&mut self, node: &ast::Enum) -> Option> { + pre_emit!(Enum, self, node); if self.should_be_excluded(node) { return None; } @@ -1021,7 +1070,7 @@ impl Translator<'_> { where_clause, }); self.emit_location(label, node); - emit_detached!(Enum, self, node, label); + post_emit!(Enum, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1029,6 +1078,7 @@ impl Translator<'_> { &mut self, node: &ast::ExprStmt, ) -> Option> { + pre_emit!(ExprStmt, self, node); if self.should_be_excluded(node) { return None; } @@ -1038,7 +1088,7 @@ impl Translator<'_> { expr, }); self.emit_location(label, node); - emit_detached!(ExprStmt, self, node, label); + post_emit!(ExprStmt, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1046,6 +1096,7 @@ impl Translator<'_> { &mut self, node: &ast::ExternBlock, ) -> Option> { + pre_emit!(ExternBlock, self, node); if self.should_be_excluded(node) { return None; } @@ -1066,7 +1117,7 @@ impl Translator<'_> { is_unsafe, }); self.emit_location(label, node); - emit_detached!(ExternBlock, self, node, label); + post_emit!(ExternBlock, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1074,6 +1125,7 @@ impl Translator<'_> { &mut self, node: &ast::ExternCrate, ) -> Option> { + pre_emit!(ExternCrate, self, node); if self.should_be_excluded(node) { return None; } @@ -1092,7 +1144,7 @@ impl Translator<'_> { visibility, }); self.emit_location(label, node); - emit_detached!(ExternCrate, self, node, label); + post_emit!(ExternCrate, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1100,6 +1152,7 @@ impl Translator<'_> { &mut self, node: &ast::ExternItemList, ) -> Option> { + pre_emit!(ExternItemList, self, node); if self.should_be_excluded(node) { return None; } @@ -1117,7 +1170,7 @@ impl Translator<'_> { extern_items, }); self.emit_location(label, node); - emit_detached!(ExternItemList, self, node, label); + post_emit!(ExternItemList, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1125,6 +1178,7 @@ impl Translator<'_> { &mut self, node: &ast::FieldExpr, ) -> Option> { + pre_emit!(FieldExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -1141,11 +1195,12 @@ impl Translator<'_> { identifier, }); self.emit_location(label, node); - emit_detached!(FieldExpr, self, node, label); + post_emit!(FieldExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_fn(&mut self, node: &ast::Fn) -> Option> { + pre_emit!(Function, self, node); if self.should_be_excluded(node) { return None; } @@ -1186,7 +1241,7 @@ impl Translator<'_> { where_clause, }); self.emit_location(label, node); - emit_detached!(Function, self, node, label); + post_emit!(Function, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1194,6 +1249,7 @@ impl Translator<'_> { &mut self, node: &ast::FnPtrType, ) -> Option> { + pre_emit!(FnPtrTypeRepr, self, node); if self.should_be_excluded(node) { return None; } @@ -1213,7 +1269,7 @@ impl Translator<'_> { ret_type, }); self.emit_location(label, node); - emit_detached!(FnPtrTypeRepr, self, node, label); + post_emit!(FnPtrTypeRepr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1221,6 +1277,7 @@ impl Translator<'_> { &mut self, node: &ast::ForExpr, ) -> Option> { + pre_emit!(ForExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -1241,7 +1298,7 @@ impl Translator<'_> { pat, }); self.emit_location(label, node); - emit_detached!(ForExpr, self, node, label); + post_emit!(ForExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1249,6 +1306,7 @@ impl Translator<'_> { &mut self, node: &ast::ForType, ) -> Option> { + pre_emit!(ForTypeRepr, self, node); if self.should_be_excluded(node) { return None; } @@ -1262,7 +1320,7 @@ impl Translator<'_> { type_repr, }); self.emit_location(label, node); - emit_detached!(ForTypeRepr, self, node, label); + post_emit!(ForTypeRepr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1270,6 +1328,7 @@ impl Translator<'_> { &mut self, node: &ast::FormatArgsArg, ) -> Option> { + pre_emit!(FormatArgsArg, self, node); if self.should_be_excluded(node) { return None; } @@ -1281,7 +1340,7 @@ impl Translator<'_> { name, }); self.emit_location(label, node); - emit_detached!(FormatArgsArg, self, node, label); + post_emit!(FormatArgsArg, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1289,6 +1348,7 @@ impl Translator<'_> { &mut self, node: &ast::FormatArgsExpr, ) -> Option> { + pre_emit!(FormatArgsExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -1308,7 +1368,7 @@ impl Translator<'_> { template, }); self.emit_location(label, node); - emit_detached!(FormatArgsExpr, self, node, label); + post_emit!(FormatArgsExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1316,6 +1376,7 @@ impl Translator<'_> { &mut self, node: &ast::GenericArgList, ) -> Option> { + pre_emit!(GenericArgList, self, node); if self.should_be_excluded(node) { return None; } @@ -1328,7 +1389,7 @@ impl Translator<'_> { generic_args, }); self.emit_location(label, node); - emit_detached!(GenericArgList, self, node, label); + post_emit!(GenericArgList, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1336,6 +1397,7 @@ impl Translator<'_> { &mut self, node: &ast::GenericParamList, ) -> Option> { + pre_emit!(GenericParamList, self, node); if self.should_be_excluded(node) { return None; } @@ -1348,7 +1410,7 @@ impl Translator<'_> { generic_params, }); self.emit_location(label, node); - emit_detached!(GenericParamList, self, node, label); + post_emit!(GenericParamList, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1356,6 +1418,7 @@ impl Translator<'_> { &mut self, node: &ast::IdentPat, ) -> Option> { + pre_emit!(IdentPat, self, node); if self.should_be_excluded(node) { return None; } @@ -1376,11 +1439,12 @@ impl Translator<'_> { pat, }); self.emit_location(label, node); - emit_detached!(IdentPat, self, node, label); + post_emit!(IdentPat, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_if_expr(&mut self, node: &ast::IfExpr) -> Option> { + pre_emit!(IfExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -1399,11 +1463,12 @@ impl Translator<'_> { then, }); self.emit_location(label, node); - emit_detached!(IfExpr, self, node, label); + post_emit!(IfExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_impl(&mut self, node: &ast::Impl) -> Option> { + pre_emit!(Impl, self, node); if self.should_be_excluded(node) { return None; } @@ -1438,7 +1503,7 @@ impl Translator<'_> { where_clause, }); self.emit_location(label, node); - emit_detached!(Impl, self, node, label); + post_emit!(Impl, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1446,6 +1511,7 @@ impl Translator<'_> { &mut self, node: &ast::ImplTraitType, ) -> Option> { + pre_emit!(ImplTraitTypeRepr, self, node); if self.should_be_excluded(node) { return None; } @@ -1457,7 +1523,7 @@ impl Translator<'_> { type_bound_list, }); self.emit_location(label, node); - emit_detached!(ImplTraitTypeRepr, self, node, label); + post_emit!(ImplTraitTypeRepr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1465,6 +1531,7 @@ impl Translator<'_> { &mut self, node: &ast::IndexExpr, ) -> Option> { + pre_emit!(IndexExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -1481,7 +1548,7 @@ impl Translator<'_> { index, }); self.emit_location(label, node); - emit_detached!(IndexExpr, self, node, label); + post_emit!(IndexExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1489,6 +1556,7 @@ impl Translator<'_> { &mut self, node: &ast::InferType, ) -> Option> { + pre_emit!(InferTypeRepr, self, node); if self.should_be_excluded(node) { return None; } @@ -1496,7 +1564,7 @@ impl Translator<'_> { .trap .emit(generated::InferTypeRepr { id: TrapId::Star }); self.emit_location(label, node); - emit_detached!(InferTypeRepr, self, node, label); + post_emit!(InferTypeRepr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1504,6 +1572,7 @@ impl Translator<'_> { &mut self, node: &ast::ItemList, ) -> Option> { + pre_emit!(ItemList, self, node); if self.should_be_excluded(node) { return None; } @@ -1518,11 +1587,12 @@ impl Translator<'_> { items, }); self.emit_location(label, node); - emit_detached!(ItemList, self, node, label); + post_emit!(ItemList, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_label(&mut self, node: &ast::Label) -> Option> { + pre_emit!(Label, self, node); if self.should_be_excluded(node) { return None; } @@ -1532,7 +1602,7 @@ impl Translator<'_> { lifetime, }); self.emit_location(label, node); - emit_detached!(Label, self, node, label); + post_emit!(Label, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1540,6 +1610,7 @@ impl Translator<'_> { &mut self, node: &ast::LetElse, ) -> Option> { + pre_emit!(LetElse, self, node); if self.should_be_excluded(node) { return None; } @@ -1549,7 +1620,7 @@ impl Translator<'_> { block_expr, }); self.emit_location(label, node); - emit_detached!(LetElse, self, node, label); + post_emit!(LetElse, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1557,6 +1628,7 @@ impl Translator<'_> { &mut self, node: &ast::LetExpr, ) -> Option> { + pre_emit!(LetExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -1573,7 +1645,7 @@ impl Translator<'_> { pat, }); self.emit_location(label, node); - emit_detached!(LetExpr, self, node, label); + post_emit!(LetExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1581,6 +1653,7 @@ impl Translator<'_> { &mut self, node: &ast::LetStmt, ) -> Option> { + pre_emit!(LetStmt, self, node); if self.should_be_excluded(node) { return None; } @@ -1601,7 +1674,7 @@ impl Translator<'_> { type_repr, }); self.emit_location(label, node); - emit_detached!(LetStmt, self, node, label); + post_emit!(LetStmt, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1609,6 +1682,7 @@ impl Translator<'_> { &mut self, node: &ast::Lifetime, ) -> Option> { + pre_emit!(Lifetime, self, node); if self.should_be_excluded(node) { return None; } @@ -1618,7 +1692,7 @@ impl Translator<'_> { text, }); self.emit_location(label, node); - emit_detached!(Lifetime, self, node, label); + post_emit!(Lifetime, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1626,6 +1700,7 @@ impl Translator<'_> { &mut self, node: &ast::LifetimeArg, ) -> Option> { + pre_emit!(LifetimeArg, self, node); if self.should_be_excluded(node) { return None; } @@ -1635,7 +1710,7 @@ impl Translator<'_> { lifetime, }); self.emit_location(label, node); - emit_detached!(LifetimeArg, self, node, label); + post_emit!(LifetimeArg, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1643,6 +1718,7 @@ impl Translator<'_> { &mut self, node: &ast::LifetimeParam, ) -> Option> { + pre_emit!(LifetimeParam, self, node); if self.should_be_excluded(node) { return None; } @@ -1661,7 +1737,7 @@ impl Translator<'_> { type_bound_list, }); self.emit_location(label, node); - emit_detached!(LifetimeParam, self, node, label); + post_emit!(LifetimeParam, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1669,6 +1745,7 @@ impl Translator<'_> { &mut self, node: &ast::Literal, ) -> Option> { + pre_emit!(LiteralExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -1683,7 +1760,7 @@ impl Translator<'_> { text_value, }); self.emit_location(label, node); - emit_detached!(LiteralExpr, self, node, label); + post_emit!(LiteralExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1691,6 +1768,7 @@ impl Translator<'_> { &mut self, node: &ast::LiteralPat, ) -> Option> { + pre_emit!(LiteralPat, self, node); if self.should_be_excluded(node) { return None; } @@ -1700,7 +1778,7 @@ impl Translator<'_> { literal, }); self.emit_location(label, node); - emit_detached!(LiteralPat, self, node, label); + post_emit!(LiteralPat, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1708,6 +1786,7 @@ impl Translator<'_> { &mut self, node: &ast::LoopExpr, ) -> Option> { + pre_emit!(LoopExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -1724,7 +1803,7 @@ impl Translator<'_> { loop_body, }); self.emit_location(label, node); - emit_detached!(LoopExpr, self, node, label); + post_emit!(LoopExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1732,6 +1811,7 @@ impl Translator<'_> { &mut self, node: &ast::MacroCall, ) -> Option> { + pre_emit!(MacroCall, self, node); if self.should_be_excluded(node) { return None; } @@ -1748,7 +1828,7 @@ impl Translator<'_> { token_tree, }); self.emit_location(label, node); - emit_detached!(MacroCall, self, node, label); + post_emit!(MacroCall, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1756,6 +1836,7 @@ impl Translator<'_> { &mut self, node: &ast::MacroDef, ) -> Option> { + pre_emit!(MacroDef, self, node); if self.should_be_excluded(node) { return None; } @@ -1776,7 +1857,7 @@ impl Translator<'_> { visibility, }); self.emit_location(label, node); - emit_detached!(MacroDef, self, node, label); + post_emit!(MacroDef, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1784,6 +1865,7 @@ impl Translator<'_> { &mut self, node: &ast::MacroExpr, ) -> Option> { + pre_emit!(MacroExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -1793,7 +1875,7 @@ impl Translator<'_> { macro_call, }); self.emit_location(label, node); - emit_detached!(MacroExpr, self, node, label); + post_emit!(MacroExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1801,6 +1883,7 @@ impl Translator<'_> { &mut self, node: &ast::MacroItems, ) -> Option> { + pre_emit!(MacroItems, self, node); if self.should_be_excluded(node) { return None; } @@ -1810,7 +1893,7 @@ impl Translator<'_> { items, }); self.emit_location(label, node); - emit_detached!(MacroItems, self, node, label); + post_emit!(MacroItems, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1818,6 +1901,7 @@ impl Translator<'_> { &mut self, node: &ast::MacroPat, ) -> Option> { + pre_emit!(MacroPat, self, node); if self.should_be_excluded(node) { return None; } @@ -1827,7 +1911,7 @@ impl Translator<'_> { macro_call, }); self.emit_location(label, node); - emit_detached!(MacroPat, self, node, label); + post_emit!(MacroPat, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1835,6 +1919,7 @@ impl Translator<'_> { &mut self, node: &ast::MacroRules, ) -> Option> { + pre_emit!(MacroRules, self, node); if self.should_be_excluded(node) { return None; } @@ -1853,7 +1938,7 @@ impl Translator<'_> { visibility, }); self.emit_location(label, node); - emit_detached!(MacroRules, self, node, label); + post_emit!(MacroRules, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1861,6 +1946,7 @@ impl Translator<'_> { &mut self, node: &ast::MacroStmts, ) -> Option> { + pre_emit!(MacroBlockExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -1875,7 +1961,7 @@ impl Translator<'_> { statements, }); self.emit_location(label, node); - emit_detached!(MacroBlockExpr, self, node, label); + post_emit!(MacroBlockExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1883,6 +1969,7 @@ impl Translator<'_> { &mut self, node: &ast::MacroType, ) -> Option> { + pre_emit!(MacroTypeRepr, self, node); if self.should_be_excluded(node) { return None; } @@ -1892,7 +1979,7 @@ impl Translator<'_> { macro_call, }); self.emit_location(label, node); - emit_detached!(MacroTypeRepr, self, node, label); + post_emit!(MacroTypeRepr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1900,6 +1987,7 @@ impl Translator<'_> { &mut self, node: &ast::MatchArm, ) -> Option> { + pre_emit!(MatchArm, self, node); if self.should_be_excluded(node) { return None; } @@ -1918,7 +2006,7 @@ impl Translator<'_> { pat, }); self.emit_location(label, node); - emit_detached!(MatchArm, self, node, label); + post_emit!(MatchArm, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1926,6 +2014,7 @@ impl Translator<'_> { &mut self, node: &ast::MatchArmList, ) -> Option> { + pre_emit!(MatchArmList, self, node); if self.should_be_excluded(node) { return None; } @@ -1943,7 +2032,7 @@ impl Translator<'_> { attrs, }); self.emit_location(label, node); - emit_detached!(MatchArmList, self, node, label); + post_emit!(MatchArmList, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1951,6 +2040,7 @@ impl Translator<'_> { &mut self, node: &ast::MatchExpr, ) -> Option> { + pre_emit!(MatchExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -1969,7 +2059,7 @@ impl Translator<'_> { match_arm_list, }); self.emit_location(label, node); - emit_detached!(MatchExpr, self, node, label); + post_emit!(MatchExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -1977,6 +2067,7 @@ impl Translator<'_> { &mut self, node: &ast::MatchGuard, ) -> Option> { + pre_emit!(MatchGuard, self, node); if self.should_be_excluded(node) { return None; } @@ -1986,11 +2077,12 @@ impl Translator<'_> { condition, }); self.emit_location(label, node); - emit_detached!(MatchGuard, self, node, label); + post_emit!(MatchGuard, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_meta(&mut self, node: &ast::Meta) -> Option> { + pre_emit!(Meta, self, node); if self.should_be_excluded(node) { return None; } @@ -2006,7 +2098,7 @@ impl Translator<'_> { token_tree, }); self.emit_location(label, node); - emit_detached!(Meta, self, node, label); + post_emit!(Meta, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2014,6 +2106,7 @@ impl Translator<'_> { &mut self, node: &ast::MethodCallExpr, ) -> Option> { + pre_emit!(MethodCallExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -2036,11 +2129,12 @@ impl Translator<'_> { receiver, }); self.emit_location(label, node); - emit_detached!(MethodCallExpr, self, node, label); + post_emit!(MethodCallExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_module(&mut self, node: &ast::Module) -> Option> { + pre_emit!(Module, self, node); if self.should_be_excluded(node) { return None; } @@ -2059,11 +2153,12 @@ impl Translator<'_> { visibility, }); self.emit_location(label, node); - emit_detached!(Module, self, node, label); + post_emit!(Module, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_name(&mut self, node: &ast::Name) -> Option> { + pre_emit!(Name, self, node); if self.should_be_excluded(node) { return None; } @@ -2073,7 +2168,7 @@ impl Translator<'_> { text, }); self.emit_location(label, node); - emit_detached!(Name, self, node, label); + post_emit!(Name, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2081,6 +2176,7 @@ impl Translator<'_> { &mut self, node: &ast::NameRef, ) -> Option> { + pre_emit!(NameRef, self, node); if self.should_be_excluded(node) { return None; } @@ -2090,7 +2186,7 @@ impl Translator<'_> { text, }); self.emit_location(label, node); - emit_detached!(NameRef, self, node, label); + post_emit!(NameRef, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2098,6 +2194,7 @@ impl Translator<'_> { &mut self, node: &ast::NeverType, ) -> Option> { + pre_emit!(NeverTypeRepr, self, node); if self.should_be_excluded(node) { return None; } @@ -2105,7 +2202,7 @@ impl Translator<'_> { .trap .emit(generated::NeverTypeRepr { id: TrapId::Star }); self.emit_location(label, node); - emit_detached!(NeverTypeRepr, self, node, label); + post_emit!(NeverTypeRepr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2113,6 +2210,7 @@ impl Translator<'_> { &mut self, node: &ast::OffsetOfExpr, ) -> Option> { + pre_emit!(OffsetOfExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -2132,11 +2230,12 @@ impl Translator<'_> { type_repr, }); self.emit_location(label, node); - emit_detached!(OffsetOfExpr, self, node, label); + post_emit!(OffsetOfExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_or_pat(&mut self, node: &ast::OrPat) -> Option> { + pre_emit!(OrPat, self, node); if self.should_be_excluded(node) { return None; } @@ -2146,11 +2245,12 @@ impl Translator<'_> { pats, }); self.emit_location(label, node); - emit_detached!(OrPat, self, node, label); + post_emit!(OrPat, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_param(&mut self, node: &ast::Param) -> Option> { + pre_emit!(Param, self, node); if self.should_be_excluded(node) { return None; } @@ -2167,7 +2267,7 @@ impl Translator<'_> { type_repr, }); self.emit_location(label, node); - emit_detached!(Param, self, node, label); + post_emit!(Param, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2175,6 +2275,7 @@ impl Translator<'_> { &mut self, node: &ast::ParamList, ) -> Option> { + pre_emit!(ParamList, self, node); if self.should_be_excluded(node) { return None; } @@ -2186,7 +2287,7 @@ impl Translator<'_> { self_param, }); self.emit_location(label, node); - emit_detached!(ParamList, self, node, label); + post_emit!(ParamList, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2194,6 +2295,7 @@ impl Translator<'_> { &mut self, node: &ast::ParenExpr, ) -> Option> { + pre_emit!(ParenExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -2208,7 +2310,7 @@ impl Translator<'_> { expr, }); self.emit_location(label, node); - emit_detached!(ParenExpr, self, node, label); + post_emit!(ParenExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2216,6 +2318,7 @@ impl Translator<'_> { &mut self, node: &ast::ParenPat, ) -> Option> { + pre_emit!(ParenPat, self, node); if self.should_be_excluded(node) { return None; } @@ -2225,7 +2328,7 @@ impl Translator<'_> { pat, }); self.emit_location(label, node); - emit_detached!(ParenPat, self, node, label); + post_emit!(ParenPat, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2233,6 +2336,7 @@ impl Translator<'_> { &mut self, node: &ast::ParenType, ) -> Option> { + pre_emit!(ParenTypeRepr, self, node); if self.should_be_excluded(node) { return None; } @@ -2242,7 +2346,7 @@ impl Translator<'_> { type_repr, }); self.emit_location(label, node); - emit_detached!(ParenTypeRepr, self, node, label); + post_emit!(ParenTypeRepr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2250,6 +2354,7 @@ impl Translator<'_> { &mut self, node: &ast::ParenthesizedArgList, ) -> Option> { + pre_emit!(ParenthesizedArgList, self, node); if self.should_be_excluded(node) { return None; } @@ -2262,11 +2367,12 @@ impl Translator<'_> { type_args, }); self.emit_location(label, node); - emit_detached!(ParenthesizedArgList, self, node, label); + post_emit!(ParenthesizedArgList, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_path(&mut self, node: &ast::Path) -> Option> { + pre_emit!(Path, self, node); if self.should_be_excluded(node) { return None; } @@ -2278,7 +2384,7 @@ impl Translator<'_> { segment, }); self.emit_location(label, node); - emit_detached!(Path, self, node, label); + post_emit!(Path, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2286,6 +2392,7 @@ impl Translator<'_> { &mut self, node: &ast::PathExpr, ) -> Option> { + pre_emit!(PathExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -2300,7 +2407,7 @@ impl Translator<'_> { path, }); self.emit_location(label, node); - emit_detached!(PathExpr, self, node, label); + post_emit!(PathExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2308,6 +2415,7 @@ impl Translator<'_> { &mut self, node: &ast::PathPat, ) -> Option> { + pre_emit!(PathPat, self, node); if self.should_be_excluded(node) { return None; } @@ -2317,7 +2425,7 @@ impl Translator<'_> { path, }); self.emit_location(label, node); - emit_detached!(PathPat, self, node, label); + post_emit!(PathPat, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2325,6 +2433,7 @@ impl Translator<'_> { &mut self, node: &ast::PathSegment, ) -> Option> { + pre_emit!(PathSegment, self, node); if self.should_be_excluded(node) { return None; } @@ -2348,7 +2457,7 @@ impl Translator<'_> { return_type_syntax, }); self.emit_location(label, node); - emit_detached!(PathSegment, self, node, label); + post_emit!(PathSegment, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2356,6 +2465,7 @@ impl Translator<'_> { &mut self, node: &ast::PathType, ) -> Option> { + pre_emit!(PathTypeRepr, self, node); if self.should_be_excluded(node) { return None; } @@ -2365,7 +2475,7 @@ impl Translator<'_> { path, }); self.emit_location(label, node); - emit_detached!(PathTypeRepr, self, node, label); + post_emit!(PathTypeRepr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2373,6 +2483,7 @@ impl Translator<'_> { &mut self, node: &ast::PrefixExpr, ) -> Option> { + pre_emit!(PrefixExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -2389,7 +2500,7 @@ impl Translator<'_> { operator_name, }); self.emit_location(label, node); - emit_detached!(PrefixExpr, self, node, label); + post_emit!(PrefixExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2397,6 +2508,7 @@ impl Translator<'_> { &mut self, node: &ast::PtrType, ) -> Option> { + pre_emit!(PtrTypeRepr, self, node); if self.should_be_excluded(node) { return None; } @@ -2410,7 +2522,7 @@ impl Translator<'_> { type_repr, }); self.emit_location(label, node); - emit_detached!(PtrTypeRepr, self, node, label); + post_emit!(PtrTypeRepr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2418,6 +2530,7 @@ impl Translator<'_> { &mut self, node: &ast::RangeExpr, ) -> Option> { + pre_emit!(RangeExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -2436,7 +2549,7 @@ impl Translator<'_> { start, }); self.emit_location(label, node); - emit_detached!(RangeExpr, self, node, label); + post_emit!(RangeExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2444,6 +2557,7 @@ impl Translator<'_> { &mut self, node: &ast::RangePat, ) -> Option> { + pre_emit!(RangePat, self, node); if self.should_be_excluded(node) { return None; } @@ -2457,7 +2571,7 @@ impl Translator<'_> { start, }); self.emit_location(label, node); - emit_detached!(RangePat, self, node, label); + post_emit!(RangePat, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2465,6 +2579,7 @@ impl Translator<'_> { &mut self, node: &ast::RecordExpr, ) -> Option> { + pre_emit!(StructExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -2478,7 +2593,7 @@ impl Translator<'_> { struct_expr_field_list, }); self.emit_location(label, node); - emit_detached!(StructExpr, self, node, label); + post_emit!(StructExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2486,6 +2601,7 @@ impl Translator<'_> { &mut self, node: &ast::RecordExprField, ) -> Option> { + pre_emit!(StructExprField, self, node); if self.should_be_excluded(node) { return None; } @@ -2502,7 +2618,7 @@ impl Translator<'_> { identifier, }); self.emit_location(label, node); - emit_detached!(StructExprField, self, node, label); + post_emit!(StructExprField, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2510,6 +2626,7 @@ impl Translator<'_> { &mut self, node: &ast::RecordExprFieldList, ) -> Option> { + pre_emit!(StructExprFieldList, self, node); if self.should_be_excluded(node) { return None; } @@ -2529,7 +2646,7 @@ impl Translator<'_> { spread, }); self.emit_location(label, node); - emit_detached!(StructExprFieldList, self, node, label); + post_emit!(StructExprFieldList, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2537,6 +2654,7 @@ impl Translator<'_> { &mut self, node: &ast::RecordField, ) -> Option> { + pre_emit!(StructField, self, node); if self.should_be_excluded(node) { return None; } @@ -2559,7 +2677,7 @@ impl Translator<'_> { visibility, }); self.emit_location(label, node); - emit_detached!(StructField, self, node, label); + post_emit!(StructField, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2567,6 +2685,7 @@ impl Translator<'_> { &mut self, node: &ast::RecordFieldList, ) -> Option> { + pre_emit!(StructFieldList, self, node); if self.should_be_excluded(node) { return None; } @@ -2579,7 +2698,7 @@ impl Translator<'_> { fields, }); self.emit_location(label, node); - emit_detached!(StructFieldList, self, node, label); + post_emit!(StructFieldList, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2587,6 +2706,7 @@ impl Translator<'_> { &mut self, node: &ast::RecordPat, ) -> Option> { + pre_emit!(StructPat, self, node); if self.should_be_excluded(node) { return None; } @@ -2600,7 +2720,7 @@ impl Translator<'_> { struct_pat_field_list, }); self.emit_location(label, node); - emit_detached!(StructPat, self, node, label); + post_emit!(StructPat, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2608,6 +2728,7 @@ impl Translator<'_> { &mut self, node: &ast::RecordPatField, ) -> Option> { + pre_emit!(StructPatField, self, node); if self.should_be_excluded(node) { return None; } @@ -2624,7 +2745,7 @@ impl Translator<'_> { pat, }); self.emit_location(label, node); - emit_detached!(StructPatField, self, node, label); + post_emit!(StructPatField, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2632,6 +2753,7 @@ impl Translator<'_> { &mut self, node: &ast::RecordPatFieldList, ) -> Option> { + pre_emit!(StructPatFieldList, self, node); if self.should_be_excluded(node) { return None; } @@ -2646,7 +2768,7 @@ impl Translator<'_> { rest_pat, }); self.emit_location(label, node); - emit_detached!(StructPatFieldList, self, node, label); + post_emit!(StructPatFieldList, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2654,6 +2776,7 @@ impl Translator<'_> { &mut self, node: &ast::RefExpr, ) -> Option> { + pre_emit!(RefExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -2674,11 +2797,12 @@ impl Translator<'_> { is_raw, }); self.emit_location(label, node); - emit_detached!(RefExpr, self, node, label); + post_emit!(RefExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_ref_pat(&mut self, node: &ast::RefPat) -> Option> { + pre_emit!(RefPat, self, node); if self.should_be_excluded(node) { return None; } @@ -2690,7 +2814,7 @@ impl Translator<'_> { pat, }); self.emit_location(label, node); - emit_detached!(RefPat, self, node, label); + post_emit!(RefPat, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2698,6 +2822,7 @@ impl Translator<'_> { &mut self, node: &ast::RefType, ) -> Option> { + pre_emit!(RefTypeRepr, self, node); if self.should_be_excluded(node) { return None; } @@ -2711,11 +2836,12 @@ impl Translator<'_> { type_repr, }); self.emit_location(label, node); - emit_detached!(RefTypeRepr, self, node, label); + post_emit!(RefTypeRepr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_rename(&mut self, node: &ast::Rename) -> Option> { + pre_emit!(Rename, self, node); if self.should_be_excluded(node) { return None; } @@ -2725,7 +2851,7 @@ impl Translator<'_> { name, }); self.emit_location(label, node); - emit_detached!(Rename, self, node, label); + post_emit!(Rename, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2733,6 +2859,7 @@ impl Translator<'_> { &mut self, node: &ast::RestPat, ) -> Option> { + pre_emit!(RestPat, self, node); if self.should_be_excluded(node) { return None; } @@ -2745,7 +2872,7 @@ impl Translator<'_> { attrs, }); self.emit_location(label, node); - emit_detached!(RestPat, self, node, label); + post_emit!(RestPat, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2753,6 +2880,7 @@ impl Translator<'_> { &mut self, node: &ast::RetType, ) -> Option> { + pre_emit!(RetTypeRepr, self, node); if self.should_be_excluded(node) { return None; } @@ -2762,7 +2890,7 @@ impl Translator<'_> { type_repr, }); self.emit_location(label, node); - emit_detached!(RetTypeRepr, self, node, label); + post_emit!(RetTypeRepr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2770,6 +2898,7 @@ impl Translator<'_> { &mut self, node: &ast::ReturnExpr, ) -> Option> { + pre_emit!(ReturnExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -2784,7 +2913,7 @@ impl Translator<'_> { expr, }); self.emit_location(label, node); - emit_detached!(ReturnExpr, self, node, label); + post_emit!(ReturnExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2792,6 +2921,7 @@ impl Translator<'_> { &mut self, node: &ast::ReturnTypeSyntax, ) -> Option> { + pre_emit!(ReturnTypeSyntax, self, node); if self.should_be_excluded(node) { return None; } @@ -2799,7 +2929,7 @@ impl Translator<'_> { .trap .emit(generated::ReturnTypeSyntax { id: TrapId::Star }); self.emit_location(label, node); - emit_detached!(ReturnTypeSyntax, self, node, label); + post_emit!(ReturnTypeSyntax, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2807,6 +2937,7 @@ impl Translator<'_> { &mut self, node: &ast::SelfParam, ) -> Option> { + pre_emit!(SelfParam, self, node); if self.should_be_excluded(node) { return None; } @@ -2829,7 +2960,7 @@ impl Translator<'_> { type_repr, }); self.emit_location(label, node); - emit_detached!(SelfParam, self, node, label); + post_emit!(SelfParam, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2837,6 +2968,7 @@ impl Translator<'_> { &mut self, node: &ast::SlicePat, ) -> Option> { + pre_emit!(SlicePat, self, node); if self.should_be_excluded(node) { return None; } @@ -2846,7 +2978,7 @@ impl Translator<'_> { pats, }); self.emit_location(label, node); - emit_detached!(SlicePat, self, node, label); + post_emit!(SlicePat, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2854,6 +2986,7 @@ impl Translator<'_> { &mut self, node: &ast::SliceType, ) -> Option> { + pre_emit!(SliceTypeRepr, self, node); if self.should_be_excluded(node) { return None; } @@ -2863,7 +2996,7 @@ impl Translator<'_> { type_repr, }); self.emit_location(label, node); - emit_detached!(SliceTypeRepr, self, node, label); + post_emit!(SliceTypeRepr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2871,6 +3004,7 @@ impl Translator<'_> { &mut self, node: &ast::SourceFile, ) -> Option> { + pre_emit!(SourceFile, self, node); if self.should_be_excluded(node) { return None; } @@ -2885,11 +3019,12 @@ impl Translator<'_> { items, }); self.emit_location(label, node); - emit_detached!(SourceFile, self, node, label); + post_emit!(SourceFile, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_static(&mut self, node: &ast::Static) -> Option> { + pre_emit!(Static, self, node); if self.should_be_excluded(node) { return None; } @@ -2916,7 +3051,7 @@ impl Translator<'_> { visibility, }); self.emit_location(label, node); - emit_detached!(Static, self, node, label); + post_emit!(Static, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2924,6 +3059,7 @@ impl Translator<'_> { &mut self, node: &ast::StmtList, ) -> Option> { + pre_emit!(StmtList, self, node); if self.should_be_excluded(node) { return None; } @@ -2943,11 +3079,12 @@ impl Translator<'_> { tail_expr, }); self.emit_location(label, node); - emit_detached!(StmtList, self, node, label); + post_emit!(StmtList, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_struct(&mut self, node: &ast::Struct) -> Option> { + pre_emit!(Struct, self, node); if self.should_be_excluded(node) { return None; } @@ -2972,7 +3109,7 @@ impl Translator<'_> { where_clause, }); self.emit_location(label, node); - emit_detached!(Struct, self, node, label); + post_emit!(Struct, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -2980,16 +3117,18 @@ impl Translator<'_> { &mut self, node: &ast::TokenTree, ) -> Option> { + pre_emit!(TokenTree, self, node); if self.should_be_excluded(node) { return None; } let label = self.trap.emit(generated::TokenTree { id: TrapId::Star }); self.emit_location(label, node); - emit_detached!(TokenTree, self, node, label); + post_emit!(TokenTree, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_trait(&mut self, node: &ast::Trait) -> Option> { + pre_emit!(Trait, self, node); if self.should_be_excluded(node) { return None; } @@ -3024,7 +3163,7 @@ impl Translator<'_> { where_clause, }); self.emit_location(label, node); - emit_detached!(Trait, self, node, label); + post_emit!(Trait, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3032,6 +3171,7 @@ impl Translator<'_> { &mut self, node: &ast::TraitAlias, ) -> Option> { + pre_emit!(TraitAlias, self, node); if self.should_be_excluded(node) { return None; } @@ -3058,7 +3198,7 @@ impl Translator<'_> { where_clause, }); self.emit_location(label, node); - emit_detached!(TraitAlias, self, node, label); + post_emit!(TraitAlias, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3066,6 +3206,7 @@ impl Translator<'_> { &mut self, node: &ast::TryExpr, ) -> Option> { + pre_emit!(TryExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -3080,7 +3221,7 @@ impl Translator<'_> { expr, }); self.emit_location(label, node); - emit_detached!(TryExpr, self, node, label); + post_emit!(TryExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3088,6 +3229,7 @@ impl Translator<'_> { &mut self, node: &ast::TupleExpr, ) -> Option> { + pre_emit!(TupleExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -3102,7 +3244,7 @@ impl Translator<'_> { fields, }); self.emit_location(label, node); - emit_detached!(TupleExpr, self, node, label); + post_emit!(TupleExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3110,6 +3252,7 @@ impl Translator<'_> { &mut self, node: &ast::TupleField, ) -> Option> { + pre_emit!(TupleField, self, node); if self.should_be_excluded(node) { return None; } @@ -3126,7 +3269,7 @@ impl Translator<'_> { visibility, }); self.emit_location(label, node); - emit_detached!(TupleField, self, node, label); + post_emit!(TupleField, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3134,6 +3277,7 @@ impl Translator<'_> { &mut self, node: &ast::TupleFieldList, ) -> Option> { + pre_emit!(TupleFieldList, self, node); if self.should_be_excluded(node) { return None; } @@ -3146,7 +3290,7 @@ impl Translator<'_> { fields, }); self.emit_location(label, node); - emit_detached!(TupleFieldList, self, node, label); + post_emit!(TupleFieldList, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3154,6 +3298,7 @@ impl Translator<'_> { &mut self, node: &ast::TuplePat, ) -> Option> { + pre_emit!(TuplePat, self, node); if self.should_be_excluded(node) { return None; } @@ -3163,7 +3308,7 @@ impl Translator<'_> { fields, }); self.emit_location(label, node); - emit_detached!(TuplePat, self, node, label); + post_emit!(TuplePat, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3171,6 +3316,7 @@ impl Translator<'_> { &mut self, node: &ast::TupleStructPat, ) -> Option> { + pre_emit!(TupleStructPat, self, node); if self.should_be_excluded(node) { return None; } @@ -3182,7 +3328,7 @@ impl Translator<'_> { path, }); self.emit_location(label, node); - emit_detached!(TupleStructPat, self, node, label); + post_emit!(TupleStructPat, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3190,6 +3336,7 @@ impl Translator<'_> { &mut self, node: &ast::TupleType, ) -> Option> { + pre_emit!(TupleTypeRepr, self, node); if self.should_be_excluded(node) { return None; } @@ -3199,7 +3346,7 @@ impl Translator<'_> { fields, }); self.emit_location(label, node); - emit_detached!(TupleTypeRepr, self, node, label); + post_emit!(TupleTypeRepr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3207,6 +3354,7 @@ impl Translator<'_> { &mut self, node: &ast::TypeAlias, ) -> Option> { + pre_emit!(TypeAlias, self, node); if self.should_be_excluded(node) { return None; } @@ -3237,7 +3385,7 @@ impl Translator<'_> { where_clause, }); self.emit_location(label, node); - emit_detached!(TypeAlias, self, node, label); + post_emit!(TypeAlias, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3245,6 +3393,7 @@ impl Translator<'_> { &mut self, node: &ast::TypeArg, ) -> Option> { + pre_emit!(TypeArg, self, node); if self.should_be_excluded(node) { return None; } @@ -3254,7 +3403,7 @@ impl Translator<'_> { type_repr, }); self.emit_location(label, node); - emit_detached!(TypeArg, self, node, label); + post_emit!(TypeArg, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3262,6 +3411,7 @@ impl Translator<'_> { &mut self, node: &ast::TypeBound, ) -> Option> { + pre_emit!(TypeBound, self, node); if self.should_be_excluded(node) { return None; } @@ -3281,7 +3431,7 @@ impl Translator<'_> { use_bound_generic_args, }); self.emit_location(label, node); - emit_detached!(TypeBound, self, node, label); + post_emit!(TypeBound, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3289,6 +3439,7 @@ impl Translator<'_> { &mut self, node: &ast::TypeBoundList, ) -> Option> { + pre_emit!(TypeBoundList, self, node); if self.should_be_excluded(node) { return None; } @@ -3301,7 +3452,7 @@ impl Translator<'_> { bounds, }); self.emit_location(label, node); - emit_detached!(TypeBoundList, self, node, label); + post_emit!(TypeBoundList, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3309,6 +3460,7 @@ impl Translator<'_> { &mut self, node: &ast::TypeParam, ) -> Option> { + pre_emit!(TypeParam, self, node); if self.should_be_excluded(node) { return None; } @@ -3329,7 +3481,7 @@ impl Translator<'_> { type_bound_list, }); self.emit_location(label, node); - emit_detached!(TypeParam, self, node, label); + post_emit!(TypeParam, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3337,6 +3489,7 @@ impl Translator<'_> { &mut self, node: &ast::UnderscoreExpr, ) -> Option> { + pre_emit!(UnderscoreExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -3349,11 +3502,12 @@ impl Translator<'_> { attrs, }); self.emit_location(label, node); - emit_detached!(UnderscoreExpr, self, node, label); + post_emit!(UnderscoreExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_union(&mut self, node: &ast::Union) -> Option> { + pre_emit!(Union, self, node); if self.should_be_excluded(node) { return None; } @@ -3380,11 +3534,12 @@ impl Translator<'_> { where_clause, }); self.emit_location(label, node); - emit_detached!(Union, self, node, label); + post_emit!(Union, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } pub(crate) fn emit_use(&mut self, node: &ast::Use) -> Option> { + pre_emit!(Use, self, node); if self.should_be_excluded(node) { return None; } @@ -3401,7 +3556,7 @@ impl Translator<'_> { visibility, }); self.emit_location(label, node); - emit_detached!(Use, self, node, label); + post_emit!(Use, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3409,6 +3564,7 @@ impl Translator<'_> { &mut self, node: &ast::UseBoundGenericArgs, ) -> Option> { + pre_emit!(UseBoundGenericArgs, self, node); if self.should_be_excluded(node) { return None; } @@ -3421,7 +3577,7 @@ impl Translator<'_> { use_bound_generic_args, }); self.emit_location(label, node); - emit_detached!(UseBoundGenericArgs, self, node, label); + post_emit!(UseBoundGenericArgs, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3429,6 +3585,7 @@ impl Translator<'_> { &mut self, node: &ast::UseTree, ) -> Option> { + pre_emit!(UseTree, self, node); if self.should_be_excluded(node) { return None; } @@ -3446,7 +3603,7 @@ impl Translator<'_> { use_tree_list, }); self.emit_location(label, node); - emit_detached!(UseTree, self, node, label); + post_emit!(UseTree, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3454,6 +3611,7 @@ impl Translator<'_> { &mut self, node: &ast::UseTreeList, ) -> Option> { + pre_emit!(UseTreeList, self, node); if self.should_be_excluded(node) { return None; } @@ -3466,7 +3624,7 @@ impl Translator<'_> { use_trees, }); self.emit_location(label, node); - emit_detached!(UseTreeList, self, node, label); + post_emit!(UseTreeList, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3474,6 +3632,7 @@ impl Translator<'_> { &mut self, node: &ast::Variant, ) -> Option> { + pre_emit!(Variant, self, node); if self.should_be_excluded(node) { return None; } @@ -3494,7 +3653,7 @@ impl Translator<'_> { visibility, }); self.emit_location(label, node); - emit_detached!(Variant, self, node, label); + post_emit!(Variant, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3502,6 +3661,7 @@ impl Translator<'_> { &mut self, node: &ast::VariantList, ) -> Option> { + pre_emit!(VariantList, self, node); if self.should_be_excluded(node) { return None; } @@ -3514,7 +3674,7 @@ impl Translator<'_> { variants, }); self.emit_location(label, node); - emit_detached!(VariantList, self, node, label); + post_emit!(VariantList, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3522,6 +3682,7 @@ impl Translator<'_> { &mut self, node: &ast::Visibility, ) -> Option> { + pre_emit!(Visibility, self, node); if self.should_be_excluded(node) { return None; } @@ -3531,7 +3692,7 @@ impl Translator<'_> { path, }); self.emit_location(label, node); - emit_detached!(Visibility, self, node, label); + post_emit!(Visibility, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3539,6 +3700,7 @@ impl Translator<'_> { &mut self, node: &ast::WhereClause, ) -> Option> { + pre_emit!(WhereClause, self, node); if self.should_be_excluded(node) { return None; } @@ -3551,7 +3713,7 @@ impl Translator<'_> { predicates, }); self.emit_location(label, node); - emit_detached!(WhereClause, self, node, label); + post_emit!(WhereClause, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3559,6 +3721,7 @@ impl Translator<'_> { &mut self, node: &ast::WherePred, ) -> Option> { + pre_emit!(WherePred, self, node); if self.should_be_excluded(node) { return None; } @@ -3578,7 +3741,7 @@ impl Translator<'_> { type_bound_list, }); self.emit_location(label, node); - emit_detached!(WherePred, self, node, label); + post_emit!(WherePred, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3586,6 +3749,7 @@ impl Translator<'_> { &mut self, node: &ast::WhileExpr, ) -> Option> { + pre_emit!(WhileExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -3604,7 +3768,7 @@ impl Translator<'_> { loop_body, }); self.emit_location(label, node); - emit_detached!(WhileExpr, self, node, label); + post_emit!(WhileExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3612,12 +3776,13 @@ impl Translator<'_> { &mut self, node: &ast::WildcardPat, ) -> Option> { + pre_emit!(WildcardPat, self, node); if self.should_be_excluded(node) { return None; } let label = self.trap.emit(generated::WildcardPat { id: TrapId::Star }); self.emit_location(label, node); - emit_detached!(WildcardPat, self, node, label); + post_emit!(WildcardPat, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3625,6 +3790,7 @@ impl Translator<'_> { &mut self, node: &ast::YeetExpr, ) -> Option> { + pre_emit!(YeetExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -3639,7 +3805,7 @@ impl Translator<'_> { expr, }); self.emit_location(label, node); - emit_detached!(YeetExpr, self, node, label); + post_emit!(YeetExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } @@ -3647,6 +3813,7 @@ impl Translator<'_> { &mut self, node: &ast::YieldExpr, ) -> Option> { + pre_emit!(YieldExpr, self, node); if self.should_be_excluded(node) { return None; } @@ -3661,7 +3828,7 @@ impl Translator<'_> { expr, }); self.emit_location(label, node); - emit_detached!(YieldExpr, self, node, label); + post_emit!(YieldExpr, self, node, label); self.emit_tokens(node, label.into(), node.syntax().children_with_tokens()); Some(label) } diff --git a/rust/ql/integration-tests/macro-expansion/src/lib.rs b/rust/ql/integration-tests/macro-expansion/src/lib.rs index 6d2d6037e5d2..2007d3b111aa 100644 --- a/rust/ql/integration-tests/macro-expansion/src/lib.rs +++ b/rust/ql/integration-tests/macro-expansion/src/lib.rs @@ -1,7 +1,12 @@ use macros::repeat; #[repeat(3)] -fn foo() {} +fn foo() { + println!("Hello, world!"); + + #[repeat(2)] + fn inner() {} +} #[repeat(2)] #[repeat(3)] diff --git a/rust/ql/integration-tests/macro-expansion/summary.expected b/rust/ql/integration-tests/macro-expansion/summary.expected new file mode 100644 index 000000000000..6917d67b1cf0 --- /dev/null +++ b/rust/ql/integration-tests/macro-expansion/summary.expected @@ -0,0 +1,16 @@ +| Extraction errors | 0 | +| Extraction warnings | 0 | +| Files extracted - total | 2 | +| Files extracted - with errors | 0 | +| Files extracted - without errors | 2 | +| Files extracted - without errors % | 100 | +| Inconsistencies - AST | 0 | +| Inconsistencies - CFG | 0 | +| Inconsistencies - Path resolution | 0 | +| Inconsistencies - SSA | 0 | +| Inconsistencies - data flow | 0 | +| Lines of code extracted | 29 | +| Lines of user code extracted | 29 | +| Macro calls - resolved | 52 | +| Macro calls - total | 53 | +| Macro calls - unresolved | 1 | diff --git a/rust/ql/integration-tests/macro-expansion/summary.qlref b/rust/ql/integration-tests/macro-expansion/summary.qlref new file mode 100644 index 000000000000..926fc7903911 --- /dev/null +++ b/rust/ql/integration-tests/macro-expansion/summary.qlref @@ -0,0 +1 @@ +queries/summary/SummaryStatsReduced.ql diff --git a/rust/ql/integration-tests/macro-expansion/test.expected b/rust/ql/integration-tests/macro-expansion/test.expected index 1247930bd226..83edecf5d5df 100644 --- a/rust/ql/integration-tests/macro-expansion/test.expected +++ b/rust/ql/integration-tests/macro-expansion/test.expected @@ -1,11 +1,17 @@ -| src/lib.rs:3:1:4:11 | fn foo | 0 | src/lib.rs:4:1:4:10 | fn foo_0 | -| src/lib.rs:3:1:4:11 | fn foo | 1 | src/lib.rs:4:1:4:10 | fn foo_1 | -| src/lib.rs:3:1:4:11 | fn foo | 2 | src/lib.rs:4:1:4:10 | fn foo_2 | -| src/lib.rs:6:1:8:11 | fn bar | 0 | src/lib.rs:7:1:8:10 | fn bar_0 | -| src/lib.rs:6:1:8:11 | fn bar | 1 | src/lib.rs:7:1:8:10 | fn bar_1 | -| src/lib.rs:7:1:8:10 | fn bar_0 | 0 | src/lib.rs:8:1:8:10 | fn bar_0_0 | -| src/lib.rs:7:1:8:10 | fn bar_0 | 1 | src/lib.rs:8:1:8:10 | fn bar_0_1 | -| src/lib.rs:7:1:8:10 | fn bar_0 | 2 | src/lib.rs:8:1:8:10 | fn bar_0_2 | -| src/lib.rs:7:1:8:10 | fn bar_1 | 0 | src/lib.rs:8:1:8:10 | fn bar_1_0 | -| src/lib.rs:7:1:8:10 | fn bar_1 | 1 | src/lib.rs:8:1:8:10 | fn bar_1_1 | -| src/lib.rs:7:1:8:10 | fn bar_1 | 2 | src/lib.rs:8:1:8:10 | fn bar_1_2 | +| src/lib.rs:3:1:9:1 | fn foo | 0 | src/lib.rs:4:1:8:16 | fn foo_0 | +| src/lib.rs:3:1:9:1 | fn foo | 1 | src/lib.rs:4:1:8:16 | fn foo_1 | +| src/lib.rs:3:1:9:1 | fn foo | 2 | src/lib.rs:4:1:8:16 | fn foo_2 | +| src/lib.rs:7:5:8:16 | fn inner | 0 | src/lib.rs:8:5:8:16 | fn inner_0 | +| src/lib.rs:7:5:8:16 | fn inner | 0 | src/lib.rs:8:5:8:16 | fn inner_0 | +| src/lib.rs:7:5:8:16 | fn inner | 0 | src/lib.rs:8:5:8:16 | fn inner_0 | +| src/lib.rs:7:5:8:16 | fn inner | 1 | src/lib.rs:8:5:8:16 | fn inner_1 | +| src/lib.rs:7:5:8:16 | fn inner | 1 | src/lib.rs:8:5:8:16 | fn inner_1 | +| src/lib.rs:7:5:8:16 | fn inner | 1 | src/lib.rs:8:5:8:16 | fn inner_1 | +| src/lib.rs:11:1:13:11 | fn bar | 0 | src/lib.rs:12:1:13:10 | fn bar_0 | +| src/lib.rs:11:1:13:11 | fn bar | 1 | src/lib.rs:12:1:13:10 | fn bar_1 | +| src/lib.rs:12:1:13:10 | fn bar_0 | 0 | src/lib.rs:13:1:13:10 | fn bar_0_0 | +| src/lib.rs:12:1:13:10 | fn bar_0 | 1 | src/lib.rs:13:1:13:10 | fn bar_0_1 | +| src/lib.rs:12:1:13:10 | fn bar_0 | 2 | src/lib.rs:13:1:13:10 | fn bar_0_2 | +| src/lib.rs:12:1:13:10 | fn bar_1 | 0 | src/lib.rs:13:1:13:10 | fn bar_1_0 | +| src/lib.rs:12:1:13:10 | fn bar_1 | 1 | src/lib.rs:13:1:13:10 | fn bar_1_1 | +| src/lib.rs:12:1:13:10 | fn bar_1 | 2 | src/lib.rs:13:1:13:10 | fn bar_1_2 | diff --git a/rust/ql/src/queries/security/CWE-696/BadCtorInitialization.ql b/rust/ql/src/queries/security/CWE-696/BadCtorInitialization.ql index 80364a9de06a..80e1043a979c 100644 --- a/rust/ql/src/queries/security/CWE-696/BadCtorInitialization.ql +++ b/rust/ql/src/queries/security/CWE-696/BadCtorInitialization.ql @@ -44,8 +44,16 @@ class PathElement = AstNode; * reachable from a source. */ predicate edgesFwd(PathElement pred, PathElement succ) { - // attribute (source) -> callable - pred.(CtorAttr) = succ.(Callable).getAnAttr() + // attribute (source) -> function in macro expansion + exists(Function f | + pred.(CtorAttr) = f.getAnAttr() and + ( + f.getAttributeMacroExpansion().getAnItem() = succ.(Callable) + or + // if for some reason the ctor/dtor macro expansion failed, fall back to looking into the unexpanded item + not f.hasAttributeMacroExpansion() and f = succ.(Callable) + ) + ) or // [forwards reachable] callable -> enclosed call edgesFwd(_, pred) and @@ -73,4 +81,5 @@ query predicate edges(PathElement pred, PathElement succ) { from CtorAttr source, StdCall sink where edges+(source, sink) select sink, source, sink, - "Call to " + sink.toString() + " in a function with the " + source.getWhichAttr() + " attribute." + "Call to " + sink.toString() + " from the standard library in a function with the " + + source.getWhichAttr() + " attribute." diff --git a/rust/ql/test/query-tests/security/CWE-696/BadCTorInitialization.expected b/rust/ql/test/query-tests/security/CWE-696/BadCTorInitialization.expected index 9d8fc2524718..3ac74a3cb13b 100644 --- a/rust/ql/test/query-tests/security/CWE-696/BadCTorInitialization.expected +++ b/rust/ql/test/query-tests/security/CWE-696/BadCTorInitialization.expected @@ -1,49 +1,71 @@ #select -| test.rs:30:9:30:25 | ...::stdout(...) | test.rs:28:1:28:13 | Attr | test.rs:30:9:30:25 | ...::stdout(...) | Call to ...::stdout(...) in a function with the ctor attribute. | -| test.rs:35:9:35:25 | ...::stdout(...) | test.rs:33:1:33:13 | Attr | test.rs:35:9:35:25 | ...::stdout(...) | Call to ...::stdout(...) in a function with the dtor attribute. | -| test.rs:42:9:42:25 | ...::stdout(...) | test.rs:39:1:39:13 | Attr | test.rs:42:9:42:25 | ...::stdout(...) | Call to ...::stdout(...) in a function with the dtor attribute. | -| test.rs:52:9:52:16 | stdout(...) | test.rs:50:1:50:7 | Attr | test.rs:52:9:52:16 | stdout(...) | Call to stdout(...) in a function with the ctor attribute. | -| test.rs:57:9:57:16 | stderr(...) | test.rs:55:1:55:7 | Attr | test.rs:57:9:57:16 | stderr(...) | Call to stderr(...) in a function with the ctor attribute. | -| test.rs:62:14:62:28 | ...::_print(...) | test.rs:60:1:60:7 | Attr | test.rs:62:14:62:28 | ...::_print(...) | Call to ...::_print(...) in a function with the ctor attribute. | -| test.rs:68:9:68:24 | ...::stdin(...) | test.rs:65:1:65:7 | Attr | test.rs:68:9:68:24 | ...::stdin(...) | Call to ...::stdin(...) in a function with the ctor attribute. | -| test.rs:89:5:89:35 | ...::sleep(...) | test.rs:87:1:87:7 | Attr | test.rs:89:5:89:35 | ...::sleep(...) | Call to ...::sleep(...) in a function with the ctor attribute. | -| test.rs:96:5:96:23 | ...::exit(...) | test.rs:94:1:94:7 | Attr | test.rs:96:5:96:23 | ...::exit(...) | Call to ...::exit(...) in a function with the ctor attribute. | -| test.rs:125:9:125:16 | stderr(...) | test.rs:128:1:128:7 | Attr | test.rs:125:9:125:16 | stderr(...) | Call to stderr(...) in a function with the ctor attribute. | -| test.rs:125:9:125:16 | stderr(...) | test.rs:144:1:144:7 | Attr | test.rs:125:9:125:16 | stderr(...) | Call to stderr(...) in a function with the ctor attribute. | -| test.rs:125:9:125:16 | stderr(...) | test.rs:150:1:150:7 | Attr | test.rs:125:9:125:16 | stderr(...) | Call to stderr(...) in a function with the ctor attribute. | -| test.rs:125:9:125:44 | ... .write_all(...) | test.rs:128:1:128:7 | Attr | test.rs:125:9:125:44 | ... .write_all(...) | Call to ... .write_all(...) in a function with the ctor attribute. | -| test.rs:125:9:125:44 | ... .write_all(...) | test.rs:144:1:144:7 | Attr | test.rs:125:9:125:44 | ... .write_all(...) | Call to ... .write_all(...) in a function with the ctor attribute. | -| test.rs:125:9:125:44 | ... .write_all(...) | test.rs:150:1:150:7 | Attr | test.rs:125:9:125:44 | ... .write_all(...) | Call to ... .write_all(...) in a function with the ctor attribute. | -| test.rs:170:5:170:15 | ...::stdout(...) | test.rs:168:1:168:7 | Attr | test.rs:170:5:170:15 | ...::stdout(...) | Call to ...::stdout(...) in a function with the ctor attribute. | +| test.rs:30:9:30:24 | ...::stdout(...) | test.rs:28:1:28:13 | Attr | test.rs:30:9:30:24 | ...::stdout(...) | Call to ...::stdout(...) from the standard library in a function with the ctor attribute. | +| test.rs:30:9:30:48 | ... .write(...) | test.rs:28:1:28:13 | Attr | test.rs:30:9:30:48 | ... .write(...) | Call to ... .write(...) from the standard library in a function with the ctor attribute. | +| test.rs:35:9:35:24 | ...::stdout(...) | test.rs:33:1:33:13 | Attr | test.rs:35:9:35:24 | ...::stdout(...) | Call to ...::stdout(...) from the standard library in a function with the dtor attribute. | +| test.rs:35:9:35:48 | ... .write(...) | test.rs:33:1:33:13 | Attr | test.rs:35:9:35:48 | ... .write(...) | Call to ... .write(...) from the standard library in a function with the dtor attribute. | +| test.rs:42:9:42:24 | ...::stdout(...) | test.rs:39:1:39:13 | Attr | test.rs:42:9:42:24 | ...::stdout(...) | Call to ...::stdout(...) from the standard library in a function with the dtor attribute. | +| test.rs:42:9:42:48 | ... .write(...) | test.rs:39:1:39:13 | Attr | test.rs:42:9:42:48 | ... .write(...) | Call to ... .write(...) from the standard library in a function with the dtor attribute. | +| test.rs:52:9:52:15 | stdout(...) | test.rs:50:1:50:7 | Attr | test.rs:52:9:52:15 | stdout(...) | Call to stdout(...) from the standard library in a function with the ctor attribute. | +| test.rs:52:9:52:39 | ... .write(...) | test.rs:50:1:50:7 | Attr | test.rs:52:9:52:39 | ... .write(...) | Call to ... .write(...) from the standard library in a function with the ctor attribute. | +| test.rs:57:9:57:15 | stderr(...) | test.rs:55:1:55:7 | Attr | test.rs:57:9:57:15 | stderr(...) | Call to stderr(...) from the standard library in a function with the ctor attribute. | +| test.rs:57:9:57:43 | ... .write_all(...) | test.rs:55:1:55:7 | Attr | test.rs:57:9:57:43 | ... .write_all(...) | Call to ... .write_all(...) from the standard library in a function with the ctor attribute. | +| test.rs:62:14:62:28 | ...::_print(...) | test.rs:60:1:60:7 | Attr | test.rs:62:14:62:28 | ...::_print(...) | Call to ...::_print(...) from the standard library in a function with the ctor attribute. | +| test.rs:68:9:68:23 | ...::stdin(...) | test.rs:65:1:65:7 | Attr | test.rs:68:9:68:23 | ...::stdin(...) | Call to ...::stdin(...) from the standard library in a function with the ctor attribute. | +| test.rs:68:9:68:44 | ... .read_line(...) | test.rs:65:1:65:7 | Attr | test.rs:68:9:68:44 | ... .read_line(...) | Call to ... .read_line(...) from the standard library in a function with the ctor attribute. | +| test.rs:75:17:75:44 | ...::create(...) | test.rs:73:1:73:7 | Attr | test.rs:75:17:75:44 | ...::create(...) | Call to ...::create(...) from the standard library in a function with the ctor attribute. | +| test.rs:80:14:80:37 | ...::now(...) | test.rs:78:1:78:7 | Attr | test.rs:80:14:80:37 | ...::now(...) | Call to ...::now(...) from the standard library in a function with the ctor attribute. | +| test.rs:89:5:89:34 | ...::sleep(...) | test.rs:87:1:87:7 | Attr | test.rs:89:5:89:34 | ...::sleep(...) | Call to ...::sleep(...) from the standard library in a function with the ctor attribute. | +| test.rs:96:5:96:22 | ...::exit(...) | test.rs:94:1:94:7 | Attr | test.rs:96:5:96:22 | ...::exit(...) | Call to ...::exit(...) from the standard library in a function with the ctor attribute. | +| test.rs:125:9:125:16 | stderr(...) | test.rs:128:1:128:7 | Attr | test.rs:125:9:125:16 | stderr(...) | Call to stderr(...) from the standard library in a function with the ctor attribute. | +| test.rs:125:9:125:16 | stderr(...) | test.rs:144:1:144:7 | Attr | test.rs:125:9:125:16 | stderr(...) | Call to stderr(...) from the standard library in a function with the ctor attribute. | +| test.rs:125:9:125:16 | stderr(...) | test.rs:150:1:150:7 | Attr | test.rs:125:9:125:16 | stderr(...) | Call to stderr(...) from the standard library in a function with the ctor attribute. | +| test.rs:125:9:125:44 | ... .write_all(...) | test.rs:128:1:128:7 | Attr | test.rs:125:9:125:44 | ... .write_all(...) | Call to ... .write_all(...) from the standard library in a function with the ctor attribute. | +| test.rs:125:9:125:44 | ... .write_all(...) | test.rs:144:1:144:7 | Attr | test.rs:125:9:125:44 | ... .write_all(...) | Call to ... .write_all(...) from the standard library in a function with the ctor attribute. | +| test.rs:125:9:125:44 | ... .write_all(...) | test.rs:150:1:150:7 | Attr | test.rs:125:9:125:44 | ... .write_all(...) | Call to ... .write_all(...) from the standard library in a function with the ctor attribute. | +| test.rs:168:1:168:7 | ... .write(...) | test.rs:168:1:168:7 | Attr | test.rs:168:1:168:7 | ... .write(...) | Call to ... .write(...) from the standard library in a function with the ctor attribute. | +| test.rs:168:1:168:7 | ...::stdout(...) | test.rs:168:1:168:7 | Attr | test.rs:168:1:168:7 | ...::stdout(...) | Call to ...::stdout(...) from the standard library in a function with the ctor attribute. | edges -| test.rs:28:1:28:13 | Attr | test.rs:28:1:31:1 | fn bad1_1 | -| test.rs:28:1:31:1 | fn bad1_1 | test.rs:30:9:30:25 | ...::stdout(...) | -| test.rs:33:1:33:13 | Attr | test.rs:33:1:36:1 | fn bad1_2 | -| test.rs:33:1:36:1 | fn bad1_2 | test.rs:35:9:35:25 | ...::stdout(...) | -| test.rs:38:1:43:1 | fn bad1_3 | test.rs:42:9:42:25 | ...::stdout(...) | -| test.rs:39:1:39:13 | Attr | test.rs:38:1:43:1 | fn bad1_3 | -| test.rs:50:1:50:7 | Attr | test.rs:50:1:53:1 | fn bad2_1 | -| test.rs:50:1:53:1 | fn bad2_1 | test.rs:52:9:52:16 | stdout(...) | -| test.rs:55:1:55:7 | Attr | test.rs:55:1:58:1 | fn bad2_2 | -| test.rs:55:1:58:1 | fn bad2_2 | test.rs:57:9:57:16 | stderr(...) | -| test.rs:60:1:60:7 | Attr | test.rs:60:1:63:1 | fn bad2_3 | -| test.rs:60:1:63:1 | fn bad2_3 | test.rs:62:14:62:28 | ...::_print(...) | -| test.rs:65:1:65:7 | Attr | test.rs:65:1:69:1 | fn bad2_4 | -| test.rs:65:1:69:1 | fn bad2_4 | test.rs:68:9:68:24 | ...::stdin(...) | -| test.rs:87:1:87:7 | Attr | test.rs:87:1:90:1 | fn bad2_7 | -| test.rs:87:1:90:1 | fn bad2_7 | test.rs:89:5:89:35 | ...::sleep(...) | -| test.rs:94:1:94:7 | Attr | test.rs:94:1:97:1 | fn bad2_8 | -| test.rs:94:1:97:1 | fn bad2_8 | test.rs:96:5:96:23 | ...::exit(...) | +| test.rs:28:1:28:13 | Attr | test.rs:29:4:30:50 | fn bad1_1 | +| test.rs:29:4:30:50 | fn bad1_1 | test.rs:30:9:30:24 | ...::stdout(...) | +| test.rs:29:4:30:50 | fn bad1_1 | test.rs:30:9:30:48 | ... .write(...) | +| test.rs:33:1:33:13 | Attr | test.rs:34:4:35:50 | fn bad1_2 | +| test.rs:34:4:35:50 | fn bad1_2 | test.rs:35:9:35:24 | ...::stdout(...) | +| test.rs:34:4:35:50 | fn bad1_2 | test.rs:35:9:35:48 | ... .write(...) | +| test.rs:38:1:42:50 | fn bad1_3 | test.rs:42:9:42:24 | ...::stdout(...) | +| test.rs:38:1:42:50 | fn bad1_3 | test.rs:42:9:42:48 | ... .write(...) | +| test.rs:39:1:39:13 | Attr | test.rs:38:1:42:50 | fn bad1_3 | +| test.rs:50:1:50:7 | Attr | test.rs:51:4:52:41 | fn bad2_1 | +| test.rs:51:4:52:41 | fn bad2_1 | test.rs:52:9:52:15 | stdout(...) | +| test.rs:51:4:52:41 | fn bad2_1 | test.rs:52:9:52:39 | ... .write(...) | +| test.rs:55:1:55:7 | Attr | test.rs:56:4:57:45 | fn bad2_2 | +| test.rs:56:4:57:45 | fn bad2_2 | test.rs:57:9:57:15 | stderr(...) | +| test.rs:56:4:57:45 | fn bad2_2 | test.rs:57:9:57:43 | ... .write_all(...) | +| test.rs:60:1:60:7 | Attr | test.rs:61:4:62:30 | fn bad2_3 | +| test.rs:61:4:62:30 | fn bad2_3 | test.rs:62:14:62:28 | ...::_print(...) | +| test.rs:65:1:65:7 | Attr | test.rs:66:4:68:46 | fn bad2_4 | +| test.rs:66:4:68:46 | fn bad2_4 | test.rs:68:9:68:23 | ...::stdin(...) | +| test.rs:66:4:68:46 | fn bad2_4 | test.rs:68:9:68:44 | ... .read_line(...) | +| test.rs:73:1:73:7 | Attr | test.rs:74:4:75:55 | fn bad2_5 | +| test.rs:74:4:75:55 | fn bad2_5 | test.rs:75:17:75:44 | ...::create(...) | +| test.rs:78:1:78:7 | Attr | test.rs:79:4:80:39 | fn bad2_6 | +| test.rs:79:4:80:39 | fn bad2_6 | test.rs:80:14:80:37 | ...::now(...) | +| test.rs:87:1:87:7 | Attr | test.rs:88:4:89:36 | fn bad2_7 | +| test.rs:88:4:89:36 | fn bad2_7 | test.rs:89:5:89:34 | ...::sleep(...) | +| test.rs:94:1:94:7 | Attr | test.rs:95:4:96:24 | fn bad2_8 | +| test.rs:95:4:96:24 | fn bad2_8 | test.rs:96:5:96:22 | ...::exit(...) | | test.rs:124:1:126:1 | fn call_target3_1 | test.rs:125:9:125:16 | stderr(...) | | test.rs:124:1:126:1 | fn call_target3_1 | test.rs:125:9:125:44 | ... .write_all(...) | -| test.rs:128:1:128:7 | Attr | test.rs:128:1:131:1 | fn bad3_1 | -| test.rs:128:1:131:1 | fn bad3_1 | test.rs:130:5:130:20 | call_target3_1(...) | -| test.rs:130:5:130:20 | call_target3_1(...) | test.rs:124:1:126:1 | fn call_target3_1 | -| test.rs:144:1:144:7 | Attr | test.rs:144:1:148:1 | fn bad3_3 | +| test.rs:128:1:128:7 | Attr | test.rs:129:4:130:21 | fn bad3_1 | +| test.rs:129:4:130:21 | fn bad3_1 | test.rs:130:5:130:19 | call_target3_1(...) | +| test.rs:130:5:130:19 | call_target3_1(...) | test.rs:124:1:126:1 | fn call_target3_1 | +| test.rs:144:1:144:7 | Attr | test.rs:145:4:147:21 | fn bad3_3 | | test.rs:144:1:148:1 | fn bad3_3 | test.rs:146:5:146:20 | call_target3_1(...) | +| test.rs:145:4:147:21 | fn bad3_3 | test.rs:146:5:146:19 | call_target3_1(...) | +| test.rs:146:5:146:19 | call_target3_1(...) | test.rs:124:1:126:1 | fn call_target3_1 | | test.rs:146:5:146:20 | call_target3_1(...) | test.rs:124:1:126:1 | fn call_target3_1 | -| test.rs:150:1:150:7 | Attr | test.rs:150:1:153:1 | fn bad3_4 | -| test.rs:150:1:153:1 | fn bad3_4 | test.rs:152:5:152:12 | bad3_3(...) | -| test.rs:152:5:152:12 | bad3_3(...) | test.rs:144:1:148:1 | fn bad3_3 | -| test.rs:168:1:168:7 | Attr | test.rs:168:1:171:1 | fn bad4_1 | -| test.rs:168:1:171:1 | fn bad4_1 | test.rs:170:5:170:15 | ...::stdout(...) | +| test.rs:150:1:150:7 | Attr | test.rs:151:4:152:13 | fn bad3_4 | +| test.rs:151:4:152:13 | fn bad3_4 | test.rs:152:5:152:11 | bad3_3(...) | +| test.rs:152:5:152:11 | bad3_3(...) | test.rs:144:1:148:1 | fn bad3_3 | +| test.rs:168:1:168:7 | Attr | test.rs:169:4:170:16 | fn bad4_1 | +| test.rs:169:4:170:16 | fn bad4_1 | test.rs:168:1:168:7 | ... .write(...) | +| test.rs:169:4:170:16 | fn bad4_1 | test.rs:168:1:168:7 | ...::stdout(...) | diff --git a/rust/ql/test/query-tests/security/CWE-696/test.rs b/rust/ql/test/query-tests/security/CWE-696/test.rs index b23c06aa6a69..f589994d5d1d 100644 --- a/rust/ql/test/query-tests/security/CWE-696/test.rs +++ b/rust/ql/test/query-tests/security/CWE-696/test.rs @@ -70,14 +70,14 @@ fn bad2_4() { use std::fs; -#[ctor] // $ MISSING: Source=source2_5 +#[ctor] // $ Source=source2_5 fn bad2_5() { - let _buff = fs::File::create("hello.txt").unwrap(); // $ MISSING: Alert[rust/ctor-initialization]=source2_5 + let _buff = fs::File::create("hello.txt").unwrap(); // $ Alert[rust/ctor-initialization]=source2_5 } -#[ctor] // $ MISSING: Source=source2_6 +#[ctor] // $ Source=source2_6 fn bad2_6() { - let _t = std::time::Instant::now(); // $ MISSING: Alert[rust/ctor-initialization]=source2_6 + let _t = std::time::Instant::now(); // $ Alert[rust/ctor-initialization]=source2_6 } use std::time::Duration; @@ -165,7 +165,7 @@ macro_rules! macro4_1 { }; } -#[ctor] // $ Source=source4_1 +#[ctor] // $ Alert[rust/ctor-initialization] fn bad4_1() { - macro4_1!(); // $ Alert[rust/ctor-initialization]=source4_1 + macro4_1!(); }