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
#![cfg_attr(test, feature(assert_matches))]
pub mod ast;
pub mod check;
mod context;
mod cursor;
mod env;
pub mod errors;
pub mod expr;
mod input;
pub mod parse;
pub mod pattern;
pub mod rules;
mod test;

pub use self::errors::{CheckFailedError, RelatedCheckError, RelatedError, TestFailed};
#[cfg(test)]
pub use self::test::TestContext;
pub use self::test::{Test, TestResult};

use clap::{builder::ValueParser, ArgAction, Args, ColorChoice, ValueEnum};
use std::sync::Arc;

pub(crate) mod common {
    pub use std::{
        borrow::Cow,
        fmt,
        ops::{ControlFlow, RangeBounds},
        sync::Arc,
    };

    pub use either::Either::{self, Left, Right};
    pub use litcheck::{
        diagnostics::{
            ArcSource, Diag, DiagResult, Diagnostic, Label, NamedSourceFile, Report, Source,
            SourceFile, SourceSpan, Span, Spanned,
        },
        range::{self, Range},
        text::{self, Newline},
        StringInterner, Symbol,
    };
    pub use regex_automata::{meta::Regex, util::look::LookMatcher};
    pub use smallvec::{smallvec, SmallVec};

    pub use crate::ast::{Check, Constraint};
    pub use crate::context::{Context, ContextExt, ContextGuard, MatchContext};
    pub use crate::cursor::{Cursor, CursorGuard, CursorPosition};
    pub use crate::env::{Env, LexicalScope, LexicalScopeExtend, LexicalScopeMut, ScopeGuard};
    pub use crate::errors::{
        CheckFailedError, RelatedCheckError, RelatedError, RelatedLabel, TestFailed,
    };
    pub use crate::expr::{BinaryOp, Expr, Number, NumberFormat, Value, VariableName};
    pub use crate::input::Input;
    pub use crate::pattern::{
        AnyMatcher, CaptureInfo, MatchInfo, MatchResult, MatchType, Matcher, MatcherMut, Matches,
        Pattern, PatternIdentifier, PatternSearcher, Searcher,
    };
    pub use crate::rules::{DynRule, Rule};
    #[cfg(test)]
    pub(crate) use crate::test::TestContext;
    pub use crate::test::TestResult;
    pub use crate::Config;
}

pub const DEFAULT_CHECK_PREFIXES: &[&str] = &["CHECK"];
pub const DEFAULT_COMMENT_PREFIXES: &[&str] = &["COM", "RUN"];

/// FileCheck reads two files, one from standard input, and one specified on
/// the command line; and uses one to verify the other.
#[derive(Debug, Args)]
pub struct Config {
    /// Allow checking empty input. By default, empty input is rejected.
    #[arg(
        default_value_t = false,
        action(clap::ArgAction::SetTrue),
        help_heading = "Input"
    )]
    pub allow_empty: bool,
    /// Which prefixes to treat as directives.
    ///
    /// For example, in the directive `CHECK-SAME`, `CHECK` is the prefix.
    #[arg(
        long = "check-prefix",
        value_name = "PREFIX",
        default_value = "CHECK",
        action(clap::ArgAction::Append),
        value_parser(re_value_parser("^[A-Za-z][A-Za-z0-9_]*")),
        help_heading = "Syntax"
    )]
    pub check_prefixes: Vec<Arc<str>>,
    /// Which prefixes to treat as comments.
    ///
    /// All content on a line following a comment directive is ignored,
    /// up to the next newline.
    #[arg(
        long = "comment-prefix",
        value_name = "PREFIX",
        default_value = "COM,RUN",
        action(clap::ArgAction::Append),
        value_parser(re_value_parser("^[A-Za-z][A-Za-z0-9_]*")),
        help_heading = "Syntax"
    )]
    pub comment_prefixes: Vec<Arc<str>>,
    /// If specifying multiple check prefixes, this controls whether or not
    /// to raise an error if one of the prefixes is missing in the test file.
    #[arg(long, default_value_t = false, help_heading = "Syntax")]
    pub allow_unused_prefixes: bool,
    /// Disable default canonicalization of whitespace.
    ///
    /// By default, FileCheck canonicalizes horizontal whitespace (spaces and tabs)
    /// which causes it to ignore these differences (a space will match a tab).
    ///
    /// This flag disables horizontal whitespace canonicalization.
    ///
    /// Newlines are always canonicalized to LF regardless of this setting.
    #[arg(
        long = "strict-whitespace",
        default_value_t = false,
        help_heading = "Matching"
    )]
    pub strict_whitespace: bool,
    /// This flag changes the default matching behavior to require all positive
    /// matches to cover an entire line. Leading/trailing whitespace is ignored
    /// unless `--strict-whitespace` is also specified.
    ///
    /// By default, FileCheck allows matches of anywhere on a line, so by setting
    /// this, you effectively insert `{{^.*}}` or `{{^}}` before, and `{{[*$]}}`
    /// or `{{$}}` after every positive check pattern.
    ///
    /// NOTE Negative matches, i.e `CHECK-NOT` are not affected by this option.
    #[arg(long, default_value_t = false, help_heading = "Matching")]
    pub match_full_lines: bool,
    /// Disable case-sensitive matching
    #[arg(long, default_value_t = false, help_heading = "Matching")]
    pub ignore_case: bool,
    /// Adds implicit negative checks for the specified patterns between positive checks.
    ///
    /// This option allows writing stricter tests without polluting them with CHECK-NOTs.
    ///
    /// For example, `--implicit-check-not warning:` can be useful when testing diagnostic
    /// messages from tools that don’t have an option similar to `clang -verify`. With this
    /// option FileCheck will verify that input does not contain warnings not covered by any
    /// `CHECK:` patterns.
    #[arg(long, value_name = "CHECK", help_heading = "Matching")]
    pub implicit_check_not: Vec<String>,
    /// Dump input to stderr, adding annotations representing currently enabled diagnostics.
    #[arg(long, value_enum, value_name = "TYPE", default_value_t = Dump::Fail, help_heading = "Output")]
    pub dump_input: Dump,
    /// Specify the parts of the input to dump when `--dump-input` is set.
    ///
    /// When specified, print only input lines of KIND, plus any context specified by `--dump-input-context`.
    ///
    /// Defaults to `error` when `--dump-input=fail`, and `all` when `--dump-input=always`
    #[arg(
        long,
        value_enum,
        value_name = "KIND",
        default_value_t = DumpFilter::Error,
        default_value_ifs([("dump-input", "fail", Some("error")), ("dump-input", "always", Some("all"))]),
        help_heading = "Output"
    )]
    pub dump_input_filter: DumpFilter,
    /// Enables scope for regex variables
    ///
    /// Variables with names that start with `$` are considered global, and remain set throughout the file
    ///
    /// All other variables get undefined after each encountered `CHECK-LABEL`
    #[arg(long, default_value_t = false, help_heading = "Variables")]
    pub enable_var_scope: bool,
    /// Set a pattern variable VAR with value VALUE that can be used in `CHECK:` lines
    ///
    /// You must specify each one in `key=value` format
    #[arg(
        long = "define",
        short = 'D',
        value_name = "NAME=VALUE",
        help_heading = "Variables"
    )]
    pub variables: Vec<expr::CliVariable>,
    /// Set the verbosity level.
    ///
    /// If specified a single time, it causes filecheck to print good directive pattern matches
    ///
    /// If specified multiple times, filecheck will emit internal diagnostics to aid in troubleshooting.
    ///
    /// If `--dump-input=fail` or `--dump-input=always`, add information as input annotations instead.
    #[arg(long, short = 'v', action = ArgAction::Count, help_heading = "Output")]
    pub verbose: u8,
    /// Whether, and how, to color terminal output
    #[arg(
        global(true),
        value_enum,
        long,
        default_value_t = ColorChoice::Auto,
        default_missing_value = "auto",
        help_heading = "Output"
    )]
    pub color: ColorChoice,
}
impl Default for Config {
    fn default() -> Self {
        Self {
            allow_empty: false,
            check_prefixes: vec![Arc::from("CHECK".to_string().into_boxed_str())],
            comment_prefixes: vec![
                Arc::from("COM".to_string().into_boxed_str()),
                Arc::from("RUN".to_string().into_boxed_str()),
            ],
            allow_unused_prefixes: false,
            strict_whitespace: false,
            match_full_lines: false,
            ignore_case: false,
            implicit_check_not: vec![],
            dump_input: Default::default(),
            dump_input_filter: Default::default(),
            enable_var_scope: false,
            variables: vec![],
            verbose: 0,
            color: Default::default(),
        }
    }
}

#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, ValueEnum)]
pub enum Dump {
    /// Explain input dump and quit
    Help,
    /// Always dump input
    Always,
    /// Dump input on failure
    #[default]
    Fail,
    /// Never dump input
    Never,
}

#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, ValueEnum)]
pub enum DumpFilter {
    /// All input lines
    All,
    /// Input lines with annotations
    AnnotationFull,
    /// Input lines with starting points of annotations
    Annotation,
    /// Input lines with starting points of error annotations
    #[default]
    Error,
}

fn re_value_parser(r: &'static str) -> ValueParser {
    use clap::{error::ErrorKind, Error};

    ValueParser::from(move |s: &str| -> Result<Arc<str>, clap::Error> {
        let re = regex::Regex::new(r).unwrap();
        if re.is_match(s) {
            Ok(Arc::from(s.to_owned().into_boxed_str()))
        } else {
            Err(Error::raw(
                ErrorKind::ValueValidation,
                "'{s}' does not match expected pattern `{r}`",
            ))
        }
    })
}