shallguard 0.1.2

ShallGuard requirement traceability, executable coverage, and review tooling
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! Scanning of Rust sources for requirement anchors.
//!
//! Every anchor is found syntactically via `syn` — comments are never
//! anchors and never participate in checking. Three concepts are kept
//! apart:
//!
//! - **references** — every requirement ID cited by some anchor. Used
//!   only for unknown/retired-ID checking.
//! - **enforcement anchors** — `#[shallguard::enforces(...)]` attributes on items,
//!   struct fields, and enum variants, and statement-position
//!   `shallguard::enforces_here!("REQ-...")` macro invocations inside function
//!   bodies. Commented-out anchors or anchor-like text in strings are
//!   invisible.
//! - **verification anchors** — `#[shallguard::verifies(...)]` attributes attached
//!   to functions that carry a recognized test attribute (`#[test]`,
//!   `#[tokio::test]`, or any attribute path ending in `test`) and are
//!   not `#[ignore]`d. There is no statement form: a Rust test is always
//!   an item and carries the attribute.

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use regex::Regex;
use serde::{Deserialize, Serialize};
use syn::spanned::Spanned as _;
use syn::visit::Visit;

/// An `#[shallguard::enforces]` attribute or `shallguard::enforces_here!` invocation.
pub struct EnforcementAnchor {
    /// Workspace-relative Rust source file.
    pub file: PathBuf,
    /// 1-based line.
    pub line: usize,
    /// Requirement IDs carried by this anchor.
    pub ids: Vec<String>,
    /// Syntactic class used to decide whether runtime coverage applies.
    pub scope_kind: EnforcementScopeKind,
    /// Complete syntactic enforcement scope, when one can be identified.
    pub scope: Option<SourceRange>,
}

/// Coverage-relevant class of an enforcement anchor's owning syntax.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EnforcementScopeKind {
    /// Body of an annotated free function or method.
    FunctionBody,
    /// Smallest enclosing block of a `shallguard::enforces_here!` invocation.
    Block,
    /// Annotated constant initializer; executable only if LLVM emits regions.
    ConstInitializer,
    /// Annotated static initializer; executable only if LLVM emits regions.
    StaticInitializer,
    /// Declaration-only syntax such as a field, variant, type, or trait.
    Structural,
    /// An anchor inside opaque generated syntax with no recoverable block.
    Unmapped,
}

impl EnforcementScopeKind {
    /// Whether this syntax can reasonably carry LLVM executable regions.
    pub fn is_potentially_executable(self) -> bool {
        matches!(
            self,
            Self::FunctionBody | Self::Block | Self::ConstInitializer | Self::StaticInitializer
        )
    }
}

/// One-based, half-open source range compatible with LLVM coverage coordinates.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct SourceRange {
    /// First line in the range.
    pub start_line: usize,
    /// First column in the range.
    pub start_column: usize,
    /// Last line in the range.
    pub end_line: usize,
    /// Column immediately after the range.
    pub end_column: usize,
}

/// A `#[shallguard::verifies]` attribute on a real, enabled test function.
pub struct VerificationAnchor {
    pub file: PathBuf,
    /// 1-based line.
    pub line: usize,
    /// The test function's name.
    pub test_fn: String,
    /// Inline module path inside the source file.
    pub inline_modules: Vec<String>,
    pub ids: Vec<String>,
}

/// A structurally invalid anchor (e.g. `#[shallguard::verifies]` off a test).
pub struct InvalidAnchor {
    pub file: PathBuf,
    pub line: usize,
    pub message: String,
}

/// Everything the scanner found.
#[shallguard::enforces("REQ-TRACE-007")]
pub struct Anchors {
    /// Every requirement ID cited by an anchor: id -> (file, line).
    pub references: HashMap<String, Vec<(PathBuf, usize)>>,
    pub enforcement: Vec<EnforcementAnchor>,
    pub verification: Vec<VerificationAnchor>,
    pub invalid: Vec<InvalidAnchor>,
}

impl Anchors {
    /// IDs claimed by at least one valid verification anchor.
    pub fn verified_ids(&self) -> impl Iterator<Item = &str> {
        self.verification
            .iter()
            .flat_map(|a| a.ids.iter().map(String::as_str))
    }
}

#[shallguard::enforces("REQ-TRACE-001")]
pub fn scan(root: &Path, roots: &[&str]) -> Result<Anchors> {
    let id_re = Regex::new(r"REQ-[A-Z]{2,}-\d{3}").expect("BUG: invalid ID regex");

    let mut files = Vec::new();
    for scan_root in roots {
        collect_rs_files(&root.join(scan_root), &mut files)
            .with_context(|| format!("scanning {scan_root}"))?;
    }
    files.sort();

    let mut anchors = Anchors {
        references: HashMap::new(),
        enforcement: Vec::new(),
        verification: Vec::new(),
        invalid: Vec::new(),
    };

    for file in files {
        let text = std::fs::read_to_string(&file)
            .with_context(|| format!("reading {}", file.display()))?;
        let rel = file
            .strip_prefix(root)
            .expect("BUG: scanned file outside workspace root")
            .to_path_buf();

        let ast = syn::parse_file(&text).with_context(|| format!("parsing {}", file.display()))?;
        walk_items(&ast.items, &rel, &[], &id_re, &mut anchors);

        let mut macro_visitor = MacroVisitor {
            file: &rel,
            id_re: &id_re,
            anchors: &mut anchors,
            blocks: Vec::new(),
        };
        macro_visitor.visit_file(&ast);
    }

    // References are anchor-derived: every ID an anchor cites, with the
    // anchor's location. Comments and other free text never count.
    let cited = anchors
        .enforcement
        .iter()
        .map(|a| (&a.ids, &a.file, a.line))
        .chain(
            anchors
                .verification
                .iter()
                .map(|a| (&a.ids, &a.file, a.line)),
        )
        .flat_map(|(ids, file, line)| ids.iter().map(move |id| (id.clone(), file.clone(), line)))
        .collect::<Vec<_>>();
    for (id, file, line) in cited {
        anchors.references.entry(id).or_default().push((file, line));
    }

    Ok(anchors)
}

/// Collects `shallguard::enforces_here!("REQ-...")` macro invocations anywhere in a
/// file — statement position in function bodies, or item position.
#[shallguard::enforces("REQ-TRACE-003")]
struct MacroVisitor<'a> {
    file: &'a Path,
    id_re: &'a Regex,
    anchors: &'a mut Anchors,
    blocks: Vec<SourceRange>,
}

impl<'ast> Visit<'ast> for MacroVisitor<'_> {
    fn visit_block(&mut self, block: &'ast syn::Block) {
        self.blocks.push(source_range(block.span()));
        syn::visit::visit_block(self, block);
        self.blocks.pop();
    }

    fn visit_macro(&mut self, mac: &'ast syn::Macro) {
        let is_anchor = mac
            .path
            .segments
            .last()
            .is_some_and(|segment| segment.ident == "enforces_here");
        if is_anchor {
            let line = mac
                .path
                .segments
                .first()
                .map_or(1, |s| s.ident.span().start().line);
            self.record(line, &mac.tokens, self.blocks.last().copied());
        } else {
            // Another macro's body (tokio::select!, etc.) is an opaque
            // token stream to syn — a `shallguard::enforces_here!` nested inside it
            // still expands at compile time, so find it at token level.
            self.scan_tokens(mac.tokens.clone(), self.blocks.last().copied());
        }
        syn::visit::visit_macro(self, mac);
    }
}

impl MacroVisitor<'_> {
    /// Records one `shallguard::enforces_here!` anchor whose argument tokens are
    /// `args`; an empty ID list is a structural error.
    fn record(&mut self, line: usize, args: &proc_macro2::TokenStream, scope: Option<SourceRange>) {
        let ids: Vec<String> = self
            .id_re
            .find_iter(&args.to_string())
            .map(|m| m.as_str().to_string())
            .collect();
        if ids.is_empty() {
            self.anchors.invalid.push(InvalidAnchor {
                file: self.file.to_path_buf(),
                line,
                message: "shallguard::enforces_here! without a requirement ID".to_string(),
            });
        } else {
            self.anchors.enforcement.push(EnforcementAnchor {
                file: self.file.to_path_buf(),
                line,
                ids,
                scope_kind: scope.map_or(EnforcementScopeKind::Unmapped, |_| {
                    EnforcementScopeKind::Block
                }),
                scope,
            });
        }
    }

    /// Walks a raw token stream looking for the `enforces_here ! ( ... )`
    /// sequence, recursing into every group so arbitrarily nested macro
    /// bodies are covered.
    fn scan_tokens(
        &mut self,
        tokens: proc_macro2::TokenStream,
        enclosing_block: Option<SourceRange>,
    ) {
        use proc_macro2::{Delimiter, TokenTree};
        let trees: Vec<TokenTree> = tokens.into_iter().collect();
        let mut i = 0;
        while i < trees.len() {
            match &trees[i] {
                TokenTree::Ident(ident) if *ident == "enforces_here" => {
                    if let (Some(TokenTree::Punct(p)), Some(TokenTree::Group(g))) =
                        (trees.get(i + 1), trees.get(i + 2))
                        && p.as_char() == '!'
                    {
                        self.record(ident.span().start().line, &g.stream(), enclosing_block);
                        i += 3;
                        continue;
                    }
                    i += 1;
                }
                TokenTree::Group(g) => {
                    let nested_block = if g.delimiter() == Delimiter::Brace {
                        Some(source_range(g.span()))
                    } else {
                        enclosing_block
                    };
                    self.scan_tokens(g.stream(), nested_block);
                    i += 1;
                }
                _ => i += 1,
            }
        }
    }
}

/// Recursively walks items (modules, impls) collecting anchor attributes.
#[shallguard::enforces("REQ-TRACE-002")]
fn walk_items(
    items: &[syn::Item],
    file: &Path,
    inline_modules: &[String],
    id_re: &Regex,
    anchors: &mut Anchors,
) {
    for item in items {
        match item {
            syn::Item::Mod(m) => {
                collect_item_attrs(
                    &m.attrs,
                    EnforcementScopeKind::Structural,
                    source_range(m.span()),
                    file,
                    id_re,
                    anchors,
                );
                if let Some((_, items)) = &m.content {
                    let mut nested = Vec::with_capacity(inline_modules.len() + 1);
                    nested.extend_from_slice(inline_modules);
                    nested.push(m.ident.to_string());
                    walk_items(items, file, &nested, id_re, anchors);
                }
            }
            syn::Item::Impl(imp) => {
                collect_item_attrs(
                    &imp.attrs,
                    EnforcementScopeKind::Structural,
                    source_range(imp.span()),
                    file,
                    id_re,
                    anchors,
                );
                for ii in &imp.items {
                    if let syn::ImplItem::Fn(f) = ii {
                        collect_fn_attrs(
                            &f.attrs,
                            &f.sig.ident,
                            source_range(f.block.span()),
                            file,
                            inline_modules,
                            id_re,
                            anchors,
                        );
                    }
                }
            }
            syn::Item::Fn(f) => {
                collect_fn_attrs(
                    &f.attrs,
                    &f.sig.ident,
                    source_range(f.block.span()),
                    file,
                    inline_modules,
                    id_re,
                    anchors,
                );
            }
            syn::Item::Struct(s) => {
                collect_item_attrs(
                    &s.attrs,
                    EnforcementScopeKind::Structural,
                    source_range(s.span()),
                    file,
                    id_re,
                    anchors,
                );
                for field in &s.fields {
                    collect_item_attrs(
                        &field.attrs,
                        EnforcementScopeKind::Structural,
                        source_range(field.span()),
                        file,
                        id_re,
                        anchors,
                    );
                }
            }
            syn::Item::Enum(e) => {
                collect_item_attrs(
                    &e.attrs,
                    EnforcementScopeKind::Structural,
                    source_range(e.span()),
                    file,
                    id_re,
                    anchors,
                );
                for variant in &e.variants {
                    collect_item_attrs(
                        &variant.attrs,
                        EnforcementScopeKind::Structural,
                        source_range(variant.span()),
                        file,
                        id_re,
                        anchors,
                    );
                    for field in &variant.fields {
                        collect_item_attrs(
                            &field.attrs,
                            EnforcementScopeKind::Structural,
                            source_range(field.span()),
                            file,
                            id_re,
                            anchors,
                        );
                    }
                }
            }
            syn::Item::Const(c) => collect_item_attrs(
                &c.attrs,
                EnforcementScopeKind::ConstInitializer,
                source_range(c.expr.span()),
                file,
                id_re,
                anchors,
            ),
            syn::Item::Static(s) => collect_item_attrs(
                &s.attrs,
                EnforcementScopeKind::StaticInitializer,
                source_range(s.expr.span()),
                file,
                id_re,
                anchors,
            ),
            syn::Item::Trait(t) => collect_item_attrs(
                &t.attrs,
                EnforcementScopeKind::Structural,
                source_range(t.span()),
                file,
                id_re,
                anchors,
            ),
            syn::Item::Type(t) => collect_item_attrs(
                &t.attrs,
                EnforcementScopeKind::Structural,
                source_range(t.span()),
                file,
                id_re,
                anchors,
            ),
            syn::Item::Use(u) => collect_item_attrs(
                &u.attrs,
                EnforcementScopeKind::Structural,
                source_range(u.span()),
                file,
                id_re,
                anchors,
            ),
            _ => {}
        }
    }
}

/// Handles attributes on a non-function item: `#[shallguard::enforces]` is an
/// enforcement anchor, `#[shallguard::verifies]` is invalid here.
fn collect_item_attrs(
    attrs: &[syn::Attribute],
    scope_kind: EnforcementScopeKind,
    scope: SourceRange,
    file: &Path,
    id_re: &Regex,
    anchors: &mut Anchors,
) {
    for attr in attrs {
        match anchor_kind(attr) {
            Some(AnchorKind::Enforces) => {
                if let Some((line, ids)) = attr_ids(attr, id_re) {
                    anchors.enforcement.push(EnforcementAnchor {
                        file: file.to_path_buf(),
                        line,
                        ids,
                        scope_kind,
                        scope: Some(scope),
                    });
                }
            }
            Some(AnchorKind::Verifies) => anchors.invalid.push(InvalidAnchor {
                file: file.to_path_buf(),
                line: attr_line(attr),
                message: "#[shallguard::verifies] on a non-test item is not evidence".to_string(),
            }),
            None => {}
        }
    }
}

/// Returns whether an attribute path names a test attribute.
///
/// The last path segment must be `test` or end in `test`. This accepts
/// `#[test]`, `#[tokio::test]`, and custom harness attributes such as
/// `#[my_harness::container_test]`. The `verifies` macro applies the same
/// rule at compile time.
#[shallguard::enforces("REQ-TRACE-004")]
fn is_test_attribute(path: &syn::Path) -> bool {
    path.segments
        .last()
        .is_some_and(|segment| segment.ident.to_string().ends_with("test"))
}

/// Handles attributes on a function (free or impl): `#[shallguard::verifies]` is a
/// verification anchor only when the function is a real, enabled test.
#[shallguard::enforces("REQ-TRACE-004")]
fn collect_fn_attrs(
    attrs: &[syn::Attribute],
    ident: &syn::Ident,
    scope: SourceRange,
    file: &Path,
    inline_modules: &[String],
    id_re: &Regex,
    anchors: &mut Anchors,
) {
    let is_test = attrs.iter().any(|a| is_test_attribute(a.path()));
    let is_ignored = attrs.iter().any(|a| a.path().is_ident("ignore"));

    for attr in attrs {
        match anchor_kind(attr) {
            Some(AnchorKind::Enforces) => {
                if let Some((line, ids)) = attr_ids(attr, id_re) {
                    anchors.enforcement.push(EnforcementAnchor {
                        file: file.to_path_buf(),
                        line,
                        ids,
                        scope_kind: EnforcementScopeKind::FunctionBody,
                        scope: Some(scope),
                    });
                }
            }
            Some(AnchorKind::Verifies) => {
                if !is_test {
                    anchors.invalid.push(InvalidAnchor {
                        file: file.to_path_buf(),
                        line: attr_line(attr),
                        message: format!(
                            "#[shallguard::verifies] on non-test function `{ident}` is not evidence"
                        ),
                    });
                } else if is_ignored {
                    anchors.invalid.push(InvalidAnchor {
                        file: file.to_path_buf(),
                        line: attr_line(attr),
                        message: format!(
                            "#[shallguard::verifies] on #[ignore]d test `{ident}` is not evidence"
                        ),
                    });
                } else if let Some((line, ids)) = attr_ids(attr, id_re) {
                    anchors.verification.push(VerificationAnchor {
                        file: file.to_path_buf(),
                        line,
                        test_fn: ident.to_string(),
                        inline_modules: inline_modules.to_vec(),
                        ids,
                    });
                }
            }
            None => {}
        }
    }
}

enum AnchorKind {
    Enforces,
    Verifies,
}

fn anchor_kind(attr: &syn::Attribute) -> Option<AnchorKind> {
    match attr.path().segments.last().map(|s| s.ident.to_string()) {
        Some(name) if name == "enforces" => Some(AnchorKind::Enforces),
        Some(name) if name == "verifies" => Some(AnchorKind::Verifies),
        _ => None,
    }
}

fn attr_line(attr: &syn::Attribute) -> usize {
    attr.path()
        .segments
        .first()
        .map_or(1, |s| s.ident.span().start().line)
}

fn source_range(span: proc_macro2::Span) -> SourceRange {
    let start = span.start();
    let end = span.end();
    SourceRange {
        start_line: start.line,
        start_column: start.column + 1,
        end_line: end.line,
        end_column: end.column + 1,
    }
}

fn attr_ids(attr: &syn::Attribute, id_re: &Regex) -> Option<(usize, Vec<String>)> {
    let syn::Meta::List(list) = &attr.meta else {
        return None;
    };
    let ids: Vec<String> = id_re
        .find_iter(&list.tokens.to_string())
        .map(|m| m.as_str().to_string())
        .collect();
    if ids.is_empty() {
        None
    } else {
        Some((attr_line(attr), ids))
    }
}

pub fn collect_rs_files(dir: &Path, out: &mut Vec<PathBuf>) -> Result<()> {
    if !dir.exists() {
        return Ok(());
    }
    for entry in std::fs::read_dir(dir).with_context(|| format!("listing {}", dir.display()))? {
        let entry = entry?;
        let path = entry.path();
        if path.is_dir() {
            if path.file_name().is_some_and(|n| n == "target") {
                continue;
            }
            collect_rs_files(&path, out)?;
        } else if path.extension().is_some_and(|e| e == "rs") {
            out.push(path);
        }
    }
    Ok(())
}

#[cfg(test)]
#[path = "scan_tests.rs"]
mod tests;