1use std::{mem, slice};
2
3use rustc_ast::visit::{self, Visitor};
4use rustc_ast::{self as ast, HasNodeId, NodeId, attr};
5use rustc_ast_pretty::pprust;
6use rustc_attr_parsing::AttributeParser;
7use rustc_errors::DiagCtxtHandle;
8use rustc_expand::base::{ExtCtxt, ResolverExpand};
9use rustc_expand::expand::{AstFragment, ExpansionConfig};
10use rustc_feature::Features;
11use rustc_hir::attrs::AttributeKind;
12use rustc_session::Session;
13use rustc_span::hygiene::AstPass;
14use rustc_span::source_map::SourceMap;
15use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
16use smallvec::smallvec;
17use thin_vec::{ThinVec, thin_vec};
18
19use crate::errors;
20
21struct ProcMacroDerive {
22 id: NodeId,
23 trait_name: Symbol,
24 function_ident: Ident,
25 span: Span,
26 attrs: ThinVec<Symbol>,
27}
28
29struct ProcMacroDef {
30 id: NodeId,
31 function_ident: Ident,
32 span: Span,
33}
34
35enum ProcMacro {
36 Derive(ProcMacroDerive),
37 Attr(ProcMacroDef),
38 Bang(ProcMacroDef),
39}
40
41struct CollectProcMacros<'a> {
42 macros: Vec<ProcMacro>,
43 in_root: bool,
44 dcx: DiagCtxtHandle<'a>,
45 session: &'a Session,
46 source_map: &'a SourceMap,
47 is_proc_macro_crate: bool,
48 is_test_crate: bool,
49}
50
51pub fn inject(
52 krate: &mut ast::Crate,
53 sess: &Session,
54 features: &Features,
55 resolver: &mut dyn ResolverExpand,
56 is_proc_macro_crate: bool,
57 has_proc_macro_decls: bool,
58 is_test_crate: bool,
59 dcx: DiagCtxtHandle<'_>,
60) {
61 let ecfg = ExpansionConfig::default(sym::proc_macro, features);
62 let mut cx = ExtCtxt::new(sess, ecfg, resolver, None);
63
64 let mut collect = CollectProcMacros {
65 macros: Vec::new(),
66 in_root: true,
67 dcx,
68 session: sess,
69 source_map: sess.source_map(),
70 is_proc_macro_crate,
71 is_test_crate,
72 };
73
74 if has_proc_macro_decls || is_proc_macro_crate {
75 visit::walk_crate(&mut collect, krate);
76 }
77 let macros = collect.macros;
78
79 if !is_proc_macro_crate {
80 return;
81 }
82
83 if is_test_crate {
84 return;
85 }
86
87 let decls = mk_decls(&mut cx, ¯os);
88 krate.items.push(decls);
89}
90
91impl<'a> CollectProcMacros<'a> {
92 fn check_not_pub_in_root(&self, vis: &ast::Visibility, sp: Span) {
93 if self.is_proc_macro_crate && self.in_root && vis.kind.is_pub() {
94 self.dcx.emit_err(errors::ProcMacro { span: sp });
95 }
96 }
97
98 fn collect_custom_derive(
99 &mut self,
100 item: &'a ast::Item,
101 function_ident: Ident,
102 attr: &'a ast::Attribute,
103 ) {
104 let Some(rustc_hir::Attribute::Parsed(AttributeKind::ProcMacroDerive {
105 trait_name,
106 helper_attrs,
107 ..
108 })) = AttributeParser::parse_limited(
109 self.session,
110 slice::from_ref(attr),
111 sym::proc_macro_derive,
112 item.span,
113 item.node_id(),
114 None,
115 )
116 else {
117 return;
118 };
119
120 if self.in_root && item.vis.kind.is_pub() {
121 self.macros.push(ProcMacro::Derive(ProcMacroDerive {
122 id: item.id,
123 span: item.span,
124 trait_name,
125 function_ident,
126 attrs: helper_attrs,
127 }));
128 } else {
129 let msg = if !self.in_root {
130 "functions tagged with `#[proc_macro_derive]` must \
131 currently reside in the root of the crate"
132 } else {
133 "functions tagged with `#[proc_macro_derive]` must be `pub`"
134 };
135 self.dcx.span_err(self.source_map.guess_head_span(item.span), msg);
136 }
137 }
138
139 fn collect_attr_proc_macro(&mut self, item: &'a ast::Item, function_ident: Ident) {
140 if self.in_root && item.vis.kind.is_pub() {
141 self.macros.push(ProcMacro::Attr(ProcMacroDef {
142 id: item.id,
143 span: item.span,
144 function_ident,
145 }));
146 } else {
147 let msg = if !self.in_root {
148 "functions tagged with `#[proc_macro_attribute]` must \
149 currently reside in the root of the crate"
150 } else {
151 "functions tagged with `#[proc_macro_attribute]` must be `pub`"
152 };
153 self.dcx.span_err(self.source_map.guess_head_span(item.span), msg);
154 }
155 }
156
157 fn collect_bang_proc_macro(&mut self, item: &'a ast::Item, function_ident: Ident) {
158 if self.in_root && item.vis.kind.is_pub() {
159 self.macros.push(ProcMacro::Bang(ProcMacroDef {
160 id: item.id,
161 span: item.span,
162 function_ident,
163 }));
164 } else {
165 let msg = if !self.in_root {
166 "functions tagged with `#[proc_macro]` must \
167 currently reside in the root of the crate"
168 } else {
169 "functions tagged with `#[proc_macro]` must be `pub`"
170 };
171 self.dcx.span_err(self.source_map.guess_head_span(item.span), msg);
172 }
173 }
174}
175
176impl<'a> Visitor<'a> for CollectProcMacros<'a> {
177 fn visit_item(&mut self, item: &'a ast::Item) {
178 if let ast::ItemKind::MacroDef(..) = item.kind {
179 if self.is_proc_macro_crate && attr::contains_name(&item.attrs, sym::macro_export) {
180 self.dcx.emit_err(errors::ExportMacroRules {
181 span: self.source_map.guess_head_span(item.span),
182 });
183 }
184 }
185
186 let mut found_attr: Option<&'a ast::Attribute> = None;
187
188 for attr in &item.attrs {
189 if attr.is_proc_macro_attr() {
190 if let Some(prev_attr) = found_attr {
191 let prev_item = prev_attr.get_normal_item();
192 let item = attr.get_normal_item();
193 let path_str = pprust::path_to_string(&item.path);
194 let msg = if item.path.segments[0].ident.name
195 == prev_item.path.segments[0].ident.name
196 {
197 format!(
198 "only one `#[{path_str}]` attribute is allowed on any given function",
199 )
200 } else {
201 format!(
202 "`#[{}]` and `#[{}]` attributes cannot both be applied
203 to the same function",
204 path_str,
205 pprust::path_to_string(&prev_item.path),
206 )
207 };
208
209 self.dcx
210 .struct_span_err(attr.span, msg)
211 .with_span_label(prev_attr.span, "previous attribute here")
212 .emit();
213
214 return;
215 }
216
217 found_attr = Some(attr);
218 }
219 }
220
221 let Some(attr) = found_attr else {
222 self.check_not_pub_in_root(&item.vis, self.source_map.guess_head_span(item.span));
223 let prev_in_root = mem::replace(&mut self.in_root, false);
224 visit::walk_item(self, item);
225 self.in_root = prev_in_root;
226 return;
227 };
228
229 let fn_ident = if let ast::ItemKind::Fn(fn_) = &item.kind {
232 fn_.ident
233 } else {
234 return;
236 };
237
238 if self.is_test_crate {
239 return;
240 }
241
242 if !self.is_proc_macro_crate {
243 self.dcx
244 .create_err(errors::AttributeOnlyUsableWithCrateType {
245 span: attr.span,
246 path: &pprust::path_to_string(&attr.get_normal_item().path),
247 })
248 .emit();
249 return;
250 }
251
252 if attr.has_name(sym::proc_macro_derive) {
254 self.collect_custom_derive(item, fn_ident, attr);
255 } else if attr.has_name(sym::proc_macro_attribute) {
256 self.collect_attr_proc_macro(item, fn_ident);
257 } else if attr.has_name(sym::proc_macro) {
258 self.collect_bang_proc_macro(item, fn_ident);
259 };
260
261 let prev_in_root = mem::replace(&mut self.in_root, false);
262 visit::walk_item(self, item);
263 self.in_root = prev_in_root;
264 }
265}
266
267fn mk_decls(cx: &mut ExtCtxt<'_>, macros: &[ProcMacro]) -> Box<ast::Item> {
284 let expn_id = cx.resolver.expansion_for_ast_pass(
285 DUMMY_SP,
286 AstPass::ProcMacroHarness,
287 &[sym::rustc_attrs, sym::proc_macro_internals],
288 None,
289 );
290 let span = DUMMY_SP.with_def_site_ctxt(expn_id.to_expn_id());
291
292 let proc_macro = Ident::new(sym::proc_macro, span);
293 let krate = cx.item(span, ast::AttrVec::new(), ast::ItemKind::ExternCrate(None, proc_macro));
294
295 let bridge = Ident::new(sym::bridge, span);
296 let client = Ident::new(sym::client, span);
297 let proc_macro_ty = Ident::new(sym::ProcMacro, span);
298 let custom_derive = Ident::new(sym::custom_derive, span);
299 let attr = Ident::new(sym::attr, span);
300 let bang = Ident::new(sym::bang, span);
301
302 let decls = macros
307 .iter()
308 .map(|m| {
309 let harness_span = span;
310 let span = match m {
311 ProcMacro::Derive(m) => m.span,
312 ProcMacro::Attr(m) | ProcMacro::Bang(m) => m.span,
313 };
314 let local_path = |cx: &ExtCtxt<'_>, ident| cx.expr_path(cx.path(span, vec![ident]));
315 let proc_macro_ty_method_path = |cx: &ExtCtxt<'_>, method| {
316 cx.expr_path(cx.path(
317 span.with_ctxt(harness_span.ctxt()),
318 vec![proc_macro, bridge, client, proc_macro_ty, method],
319 ))
320 };
321 match m {
322 ProcMacro::Derive(cd) => {
323 cx.resolver.declare_proc_macro(cd.id);
324 cx.expr_call(
327 harness_span,
328 proc_macro_ty_method_path(cx, custom_derive),
329 thin_vec![
330 cx.expr_str(span, cd.trait_name),
331 cx.expr_array_ref(
332 span,
333 cd.attrs
334 .iter()
335 .map(|&s| cx.expr_str(span, s))
336 .collect::<ThinVec<_>>(),
337 ),
338 local_path(cx, cd.function_ident),
339 ],
340 )
341 }
342 ProcMacro::Attr(ca) | ProcMacro::Bang(ca) => {
343 cx.resolver.declare_proc_macro(ca.id);
344 let ident = match m {
345 ProcMacro::Attr(_) => attr,
346 ProcMacro::Bang(_) => bang,
347 ProcMacro::Derive(_) => unreachable!(),
348 };
349
350 cx.expr_call(
353 harness_span,
354 proc_macro_ty_method_path(cx, ident),
355 thin_vec![
356 cx.expr_str(span, ca.function_ident.name),
357 local_path(cx, ca.function_ident),
358 ],
359 )
360 }
361 }
362 })
363 .collect();
364
365 let mut decls_static = cx.item_static(
366 span,
367 Ident::new(sym::_DECLS, span),
368 cx.ty_ref(
369 span,
370 cx.ty(
371 span,
372 ast::TyKind::Slice(
373 cx.ty_path(cx.path(span, vec![proc_macro, bridge, client, proc_macro_ty])),
374 ),
375 ),
376 None,
377 ast::Mutability::Not,
378 ),
379 ast::Mutability::Not,
380 cx.expr_array_ref(span, decls),
381 );
382 decls_static.attrs.extend([
383 cx.attr_word(sym::rustc_proc_macro_decls, span),
384 cx.attr_word(sym::used, span),
385 cx.attr_nested_word(sym::allow, sym::deprecated, span),
386 ]);
387
388 let block = cx.expr_block(
389 cx.block(span, thin_vec![cx.stmt_item(span, krate), cx.stmt_item(span, decls_static)]),
390 );
391
392 let anon_constant = cx.item_const(
393 span,
394 Ident::new(kw::Underscore, span),
395 cx.ty(span, ast::TyKind::Tup(ThinVec::new())),
396 block,
397 );
398
399 let items = AstFragment::Items(smallvec![anon_constant]);
401 cx.monotonic_expander().fully_expand_fragment(items).make_items().pop().unwrap()
402}