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