litcheck_filecheck/rules/
plain.rs

1use crate::common::*;
2
3use super::*;
4
5#[derive(Debug)]
6pub struct CheckPlain<M> {
7    pattern: M,
8}
9impl<M> CheckPlain<M>
10where
11    M: MatcherMut,
12{
13    pub fn new(pattern: M) -> Self {
14        Self { pattern }
15    }
16}
17impl<M> Spanned for CheckPlain<M>
18where
19    M: MatcherMut,
20{
21    fn span(&self) -> SourceSpan {
22        self.pattern.span()
23    }
24}
25impl<M> Rule for CheckPlain<M>
26where
27    M: MatcherMut,
28{
29    fn kind(&self) -> Check {
30        Check::Plain
31    }
32
33    fn apply<'input, 'context, C>(&self, context: &mut C) -> DiagResult<Matches<'input>>
34    where
35        C: Context<'input, 'context> + ?Sized,
36    {
37        let input = context.search_block();
38        let result = self.pattern.try_match_mut(input, context)?;
39
40        match &result {
41            MatchResult {
42                ty,
43                info: Some(info),
44            } if ty.is_ok() => {
45                context.cursor_mut().set_start(info.span.end());
46            }
47            _ => (),
48        }
49        Ok(result.into())
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56    use crate::pattern::matcher::*;
57
58    #[test]
59    fn check_plain_test() -> DiagResult<()> {
60        let mut context = TestContext::new();
61        context.with_checks("CHECK: @inc4").with_input(
62            "
63define void @sub1(i32* %p, i32 %v) {
64entry:
65        %0 = tail call i32 @llvm.atomic.load.sub.i32.p0i32(i32* %p, i32 %v)
66        ret void
67}
68
69define void @inc4(i64* %p) {
70entry:
71        %0 = tail call i64 @llvm.atomic.load.add.i64.p0i64(i64* %p, i64 1)
72        ret void
73}
74",
75        );
76        let mut mctx = context.match_context();
77        let pattern = SubstringMatcher::new(
78            Span::new(SourceSpan::from(8..12), Cow::Borrowed("@inc4")),
79            &mctx.config,
80        )
81        .expect("expected pattern to be valid");
82        let rule = CheckPlain::new(pattern);
83        let matches = rule
84            .apply(&mut mctx)
85            .expect("expected non-fatal application of rule");
86        let matched = TestResult::from_matches(matches, &mctx).into_result()?;
87        assert_eq!(matched[0].span.offset(), 153);
88        assert_eq!(matched[0].span.len(), 5);
89        let input = mctx.search();
90        assert_eq!(input.as_str(matched[0].matched_range()), "@inc4");
91        assert_eq!(input.buffer()[mctx.cursor().start()], b'(');
92
93        Ok(())
94    }
95}