compiletest/directives/line.rs
1use std::fmt;
2
3use camino::Utf8Path;
4
5use crate::directives::LineNumber;
6
7const COMPILETEST_DIRECTIVE_PREFIX: &str = "//@";
8
9/// If the given line begins with the appropriate comment prefix for a directive,
10/// returns a struct containing various parts of the directive.
11pub(crate) fn line_directive<'a>(
12 file_path: &'a Utf8Path,
13 line_number: LineNumber,
14 original_line: &'a str,
15) -> Option<DirectiveLine<'a>> {
16 // Ignore lines that don't start with the comment prefix.
17 let after_comment =
18 original_line.trim_start().strip_prefix(COMPILETEST_DIRECTIVE_PREFIX)?.trim_start();
19
20 let revision;
21 let raw_directive;
22
23 if let Some(after_open_bracket) = after_comment.strip_prefix('[') {
24 // A comment like `//@[foo]` only applies to revision `foo`.
25 let Some((line_revision, after_close_bracket)) = after_open_bracket.split_once(']') else {
26 panic!(
27 "malformed condition directive: expected `{COMPILETEST_DIRECTIVE_PREFIX}[foo]`, found `{original_line}`"
28 )
29 };
30
31 revision = Some(line_revision);
32 raw_directive = after_close_bracket.trim_start();
33 } else {
34 revision = None;
35 raw_directive = after_comment;
36 };
37
38 // The directive name ends at the first occurrence of colon, space, or end-of-string.
39 let name = raw_directive.split([':', ' ']).next().expect("split is never empty");
40
41 Some(DirectiveLine { file_path, line_number, revision, raw_directive, name })
42}
43
44/// The (partly) broken-down contents of a line containing a test directive,
45/// which `iter_directives` passes to its callback function.
46///
47/// For example:
48///
49/// ```text
50/// //@ compile-flags: -O
51/// ^^^^^^^^^^^^^^^^^ raw_directive
52/// ^^^^^^^^^^^^^ name
53///
54/// //@ [foo] compile-flags: -O
55/// ^^^ revision
56/// ^^^^^^^^^^^^^^^^^ raw_directive
57/// ^^^^^^^^^^^^^ name
58/// ```
59pub(crate) struct DirectiveLine<'a> {
60 /// Path of the file containing this line.
61 ///
62 /// Mostly used for diagnostics, but some directives (e.g. `//@ pp-exact`)
63 /// also use it to compute a value based on the filename.
64 pub(crate) file_path: &'a Utf8Path,
65 pub(crate) line_number: LineNumber,
66
67 /// Some test directives start with a revision name in square brackets
68 /// (e.g. `[foo]`), and only apply to that revision of the test.
69 /// If present, this field contains the revision name (e.g. `foo`).
70 pub(crate) revision: Option<&'a str>,
71
72 /// The main part of the directive, after removing the comment prefix
73 /// and the optional revision specifier.
74 ///
75 /// This is "raw" because the directive's name and colon-separated value
76 /// (if present) have not yet been extracted or checked.
77 raw_directive: &'a str,
78
79 /// Name of the directive.
80 ///
81 /// Invariant: `self.raw_directive.starts_with(self.name)`
82 pub(crate) name: &'a str,
83}
84
85impl<'ln> DirectiveLine<'ln> {
86 pub(crate) fn applies_to_test_revision(&self, test_revision: Option<&str>) -> bool {
87 self.revision.is_none() || self.revision == test_revision
88 }
89
90 /// Helper method used by `value_after_colon` and `remark_after_space`.
91 /// Don't call this directly.
92 fn rest_after_separator(&self, separator: u8) -> Option<&'ln str> {
93 let n = self.name.len();
94 if self.raw_directive.as_bytes().get(n) != Some(&separator) {
95 return None;
96 }
97
98 Some(&self.raw_directive[n + 1..])
99 }
100
101 /// If this directive uses `name: value` syntax, returns the part after
102 /// the colon character.
103 pub(crate) fn value_after_colon(&self) -> Option<&'ln str> {
104 self.rest_after_separator(b':')
105 }
106
107 /// If this directive uses `name remark` syntax, returns the part after
108 /// the separating space.
109 pub(crate) fn remark_after_space(&self) -> Option<&'ln str> {
110 self.rest_after_separator(b' ')
111 }
112
113 /// Allows callers to print `raw_directive` if necessary,
114 /// without accessing the field directly.
115 pub(crate) fn display(&self) -> impl fmt::Display {
116 self.raw_directive
117 }
118}