macra 0.1.3

Core library for macro-related analysis and serialization used by cargo-macra.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
use std::io::{BufRead, BufReader, Read};

/// The kind of macro invocation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MacroExpansionKind {
    /// Function-like macro: `name!(...)` / `name![...]` / `name!{...}`
    Bang,
    /// Attribute macro: `#[name]` or `#[name(...)]`
    Attribute,
    /// Derive macro: `#[derive(Name)]`
    Derive,
}

/// A single macro expansion pair: the "expanding" text and the "to" text.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MacroExpansion {
    pub expanding: String,
    pub arguments: String,
    pub to: String,
    /// Macro name (e.g. `"println"`, `"derive"`, `"test"`).
    pub name: String,
    /// Kind of macro invocation.
    pub kind: MacroExpansionKind,
    /// Raw input token stream that the macro receives.
    pub input: String,
}

/// A group of macro expansions from a single `note: trace_macro` block.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TraceGroup {
    pub expansions: Vec<MacroExpansion>,
}

/// Iterator over trace groups parsed from macro tracing output.
pub struct TraceParser<R: Read> {
    reader: BufReader<R>,
    current_line: String,
    peeked_line: Option<String>,
}

impl<R: Read> TraceParser<R> {
    pub fn new(reader: R) -> Self {
        Self {
            reader: BufReader::new(reader),
            current_line: String::new(),
            peeked_line: None,
        }
    }

    fn read_line(&mut self) -> Option<String> {
        if let Some(line) = self.peeked_line.take() {
            return Some(line);
        }
        self.current_line.clear();
        match self.reader.read_line(&mut self.current_line) {
            Ok(0) => None,
            Ok(_) => Some(self.current_line.trim_end_matches('\n').to_string()),
            Err(_) => None,
        }
    }

    fn peek_line(&mut self) -> Option<&str> {
        if self.peeked_line.is_none() {
            self.peeked_line = self.read_line();
        }
        self.peeked_line.as_deref()
    }

    /// Extract content between backticks, handling multi-line content.
    /// Returns the content and consumes lines as needed.
    ///
    /// The rustc trace-macros output wraps expanding/to content in backticks:
    ///   `= note: to \`CONTENT\``
    /// Content may itself contain backticks (e.g. doc comments with markdown
    /// links like `` [`something`] ``).  For single-line content the closing
    /// backtick is the *last* backtick on the line.  For multi-line content
    /// the closing backtick is the last character on the final line.
    fn extract_backtick_content(&mut self, first_line: &str) -> Option<String> {
        // Find the opening backtick
        let start_idx = first_line.find('`')?;
        let after_backtick = &first_line[start_idx + 1..];

        // Check if content ends on the same line (use rfind to skip inner backticks)
        if let Some(end_idx) = after_backtick.rfind('`') {
            return Some(after_backtick[..end_idx].to_string());
        }

        // Multi-line content: collect until closing backtick at end of line
        let mut content = after_backtick.to_string();

        loop {
            let line = self.read_line()?;
            if line.ends_with('`') {
                content.push('\n');
                content.push_str(&line[..line.len() - 1]);
                break;
            } else {
                content.push('\n');
                content.push_str(&line);
            }
        }

        Some(content)
    }

    /// Extract macro name from an `expanding` string.
    ///
    /// The expanding string has the form `name! { args }` (or with `()` / `[]`).
    /// Returns the name part before `!`.
    fn extract_macro_name(expanding: &str) -> String {
        if let Some(bang_pos) = expanding.find('!') {
            expanding[..bang_pos].trim().to_string()
        } else {
            expanding.to_string()
        }
    }

    /// Extract macro arguments from an `expanding` string.
    ///
    /// The expanding string has the form `name! { args }` (or with `()` / `[]`).
    /// Returns the content between the outermost delimiters, trimmed.
    fn extract_arguments(expanding: &str) -> String {
        // Find the `!` that separates macro name from arguments
        let bang_pos = match expanding.find('!') {
            Some(p) => p,
            None => return String::new(),
        };
        let after_bang = expanding[bang_pos + 1..].trim_start();
        let first_char = match after_bang.chars().next() {
            Some(c) => c,
            None => return String::new(),
        };
        let (open, close) = match first_char {
            '{' => ('{', '}'),
            '(' => ('(', ')'),
            '[' => ('[', ']'),
            _ => return String::new(),
        };
        let mut depth = 0i32;
        let mut start = None;
        let mut end = None;
        for (i, ch) in after_bang.char_indices() {
            if ch == open {
                depth += 1;
                if start.is_none() {
                    start = Some(i + ch.len_utf8());
                }
            } else if ch == close {
                depth -= 1;
                if depth == 0 {
                    end = Some(i);
                    break;
                }
            }
        }
        match (start, end) {
            (Some(s), Some(e)) => after_bang[s..e].trim().to_string(),
            _ => String::new(),
        }
    }

    fn parse_trace_group(&mut self) -> Option<TraceGroup> {
        let mut expansions = Vec::new();

        loop {
            let line = match self.peek_line() {
                Some(l) => l.to_string(),
                None => break,
            };

            if line.starts_with("note: trace_macro") {
                // Next trace group starts
                if !expansions.is_empty() {
                    break;
                }
                // Consume the "note: trace_macro" line
                self.read_line();
                continue;
            }

            if line.contains("= note: expanding `") {
                self.read_line(); // consume the line
                let expanding = self.extract_backtick_content(&line)?;

                // Now look for the corresponding "to" line
                loop {
                    let to_line = match self.peek_line() {
                        Some(l) => l.to_string(),
                        None => return None,
                    };

                    if to_line.contains("= note: to `") {
                        self.read_line(); // consume the line
                        let to = self.extract_backtick_content(&to_line)?;
                        let input = Self::extract_arguments(&expanding);
                        let name = Self::extract_macro_name(&expanding);
                        expansions.push(MacroExpansion {
                            expanding,
                            arguments: String::new(),
                            to,
                            name,
                            kind: MacroExpansionKind::Bang,
                            input,
                        });
                        break;
                    } else if to_line.starts_with("note: trace_macro")
                        || to_line.contains("= note: expanding `")
                    {
                        // Unexpected: got another expanding before to
                        break;
                    } else {
                        // Skip other lines (location info, source code, etc.)
                        self.read_line();
                    }
                }
            } else if line.trim().is_empty()
                || line.starts_with("   -->")
                || line.starts_with("    |")
                || line.starts_with("   |")
                || line.starts_with("  -->")
                || line.starts_with("...")
                || line.contains("= note: this note originates")
            {
                // Skip location/formatting lines
                self.read_line();
            } else if !expansions.is_empty() {
                // Non-trace content after we have some expansions means end of group
                break;
            } else {
                // Skip unrelated lines before finding any expansions
                self.read_line();
            }
        }

        if expansions.is_empty() {
            None
        } else {
            Some(TraceGroup { expansions })
        }
    }
}

impl<R: Read> Iterator for TraceParser<R> {
    type Item = TraceGroup;

    fn next(&mut self) -> Option<Self::Item> {
        // Skip lines until we find "note: trace_macro"
        loop {
            match self.peek_line() {
                Some(line) if line.starts_with("note: trace_macro") => {
                    return self.parse_trace_group();
                }
                Some(_) => {
                    self.read_line();
                }
                None => return None,
            }
        }
    }
}

/// Parse macro tracing output from a reader.
///
/// Takes any `std::io::Read` and returns an iterator over `TraceGroup`s.
/// Each `TraceGroup` contains one or more `MacroExpansion` pairs from
/// a single `note: trace_macro` block.
pub fn parse_trace<R: Read>(reader: R) -> TraceParser<R> {
    TraceParser::new(reader)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_single_expansion() {
        let input = r#"note: trace_macro
  --> src/lib.rs:15:17
   |
15 |             if !matches!(segment.arguments, PathArguments::None) {
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: expanding `matches! { segment.arguments, PathArguments::None }`
   = note: to `match segment.arguments { PathArguments::None => true, _ => false }`
"#;

        let groups: Vec<_> = parse_trace(input.as_bytes()).collect();
        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].expansions.len(), 1);
        assert_eq!(
            groups[0].expansions[0].expanding,
            "matches! { segment.arguments, PathArguments::None }"
        );
        assert_eq!(
            groups[0].expansions[0].to,
            "match segment.arguments { PathArguments::None => true, _ => false }"
        );
    }

    #[test]
    fn test_multiple_expansions_in_group() {
        let input = r#"note: trace_macro
  --> macro/lib.rs:16:17
   |
16 |                 abort!(segment, "Path arguments are not allowed");
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: expanding `abort! { segment, "message" }`
   = note: to `diagnostic!(segment, Error, "message").abort()`
   = note: expanding `diagnostic! { segment, Error, "message" }`
   = note: to `Diagnostic::new(segment, Error, "message")`
"#;

        let groups: Vec<_> = parse_trace(input.as_bytes()).collect();
        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].expansions.len(), 2);
        assert_eq!(groups[0].expansions[0].expanding, "abort! { segment, \"message\" }");
        assert_eq!(
            groups[0].expansions[0].to,
            "diagnostic!(segment, Error, \"message\").abort()"
        );
        assert_eq!(
            groups[0].expansions[1].expanding,
            "diagnostic! { segment, Error, \"message\" }"
        );
        assert_eq!(
            groups[0].expansions[1].to,
            "Diagnostic::new(segment, Error, \"message\")"
        );
    }

    #[test]
    fn test_multiline_to_content() {
        let input = r#"note: trace_macro
  --> src/lib.rs:1:1
   |
   = note: expanding `vec! { 1, 2, 3 }`
   = note: to `{
       let mut v = Vec::new();
       v.push(1);
       v.push(2);
       v.push(3);
       v
   }`
"#;

        let groups: Vec<_> = parse_trace(input.as_bytes()).collect();
        assert_eq!(groups.len(), 1);
        assert_eq!(groups[0].expansions.len(), 1);
        assert_eq!(groups[0].expansions[0].expanding, "vec! { 1, 2, 3 }");
        assert_eq!(
            groups[0].expansions[0].to,
            r#"{
       let mut v = Vec::new();
       v.push(1);
       v.push(2);
       v.push(3);
       v
   }"#
        );
    }

    #[test]
    fn test_multiple_trace_groups() {
        let input = r#"note: trace_macro
  --> src/lib.rs:1:1
   |
   = note: expanding `println! { "hello" }`
   = note: to `print!("hello\n")`

note: trace_macro
  --> src/lib.rs:2:1
   |
   = note: expanding `dbg! { x }`
   = note: to `{ eprintln!("{}", x); x }`
"#;

        let groups: Vec<_> = parse_trace(input.as_bytes()).collect();
        assert_eq!(groups.len(), 2);
        assert_eq!(groups[0].expansions[0].expanding, "println! { \"hello\" }");
        assert_eq!(groups[1].expansions[0].expanding, "dbg! { x }");
    }

    #[test]
    fn test_empty_input() {
        let input = "";
        let groups: Vec<_> = parse_trace(input.as_bytes()).collect();
        assert!(groups.is_empty());
    }

    #[test]
    fn test_no_trace_macro() {
        let input = r#"   Compiling myproject v0.1.0
    Finished dev profile
"#;
        let groups: Vec<_> = parse_trace(input.as_bytes()).collect();
        assert!(groups.is_empty());
    }

    #[test]
    fn test_real_rustc_output() {
        // Real output from `RUSTFLAGS="-Z trace-macros" cargo +nightly check`
        let input = r#"note: trace_macro
  --> macro/lib.rs:15:17
   |
15 |             if !matches!(segment.arguments, PathArguments::None) {
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: expanding `matches! { segment.arguments, PathArguments::None }`
   = note: to `#[allow(non_exhaustive_omitted_patterns)] match segment.arguments
           { PathArguments::None => true, _ => false }`

note: trace_macro
  --> macro/lib.rs:16:17
   |
16 |                 abort!(segment, "Path arguments are not allowed");
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: expanding `abort! { segment, "Path arguments are not allowed" }`
   = note: to `$crate :: diagnostic!
           (segment, $crate :: Level :: Error, "Path arguments are not allowed").abort()`
   = note: expanding `diagnostic! { segment, $crate :: Level :: Error, "Path arguments are not allowed" }`
   = note: to `{
               #[allow(unused_imports)] use $crate :: __export ::
               {
                   ToTokensAsSpanRange, Span2AsSpanRange, SpanAsSpanRange,
                   SpanRangeAsSpanRange
               }; use $crate :: DiagnosticExt; let span_range =
               (&
               segment).FIRST_ARG_MUST_EITHER_BE_Span_OR_IMPLEMENT_ToTokens_OR_BE_SpanRange();
               $crate :: Diagnostic ::
               spanned_range(span_range, $crate :: Level :: Error,
               "Path arguments are not allowed".to_string())
           }`

note: trace_macro
  --> macro/lib.rs:43:41
   |
43 |     if input.peek(Ident) && input.peek2(Token![=]) {
   |                                         ^^^^^^^^^
   |
   = note: expanding `Token! { = }`
   = note: to `$crate :: token :: Eq`
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.37s
"#;

        let groups: Vec<_> = parse_trace(input.as_bytes()).collect();
        assert_eq!(groups.len(), 3);

        // First group: single expansion
        assert_eq!(groups[0].expansions.len(), 1);
        assert_eq!(
            groups[0].expansions[0].expanding,
            "matches! { segment.arguments, PathArguments::None }"
        );
        assert_eq!(
            groups[0].expansions[0].to,
            r#"#[allow(non_exhaustive_omitted_patterns)] match segment.arguments
           { PathArguments::None => true, _ => false }"#
        );

        // Second group: multiple expansions (abort! -> diagnostic!)
        assert_eq!(groups[1].expansions.len(), 2);
        assert_eq!(
            groups[1].expansions[0].expanding,
            "abort! { segment, \"Path arguments are not allowed\" }"
        );
        assert_eq!(
            groups[1].expansions[0].to,
            r#"$crate :: diagnostic!
           (segment, $crate :: Level :: Error, "Path arguments are not allowed").abort()"#
        );
        assert_eq!(
            groups[1].expansions[1].expanding,
            "diagnostic! { segment, $crate :: Level :: Error, \"Path arguments are not allowed\" }"
        );
        assert_eq!(
            groups[1].expansions[1].to,
            r#"{
               #[allow(unused_imports)] use $crate :: __export ::
               {
                   ToTokensAsSpanRange, Span2AsSpanRange, SpanAsSpanRange,
                   SpanRangeAsSpanRange
               }; use $crate :: DiagnosticExt; let span_range =
               (&
               segment).FIRST_ARG_MUST_EITHER_BE_Span_OR_IMPLEMENT_ToTokens_OR_BE_SpanRange();
               $crate :: Diagnostic ::
               spanned_range(span_range, $crate :: Level :: Error,
               "Path arguments are not allowed".to_string())
           }"#
        );

        // Third group: Token! macro
        assert_eq!(groups[2].expansions.len(), 1);
        assert_eq!(groups[2].expansions[0].expanding, "Token! { = }");
        assert_eq!(groups[2].expansions[0].to, "$crate :: token :: Eq");
    }
}