Skip to content

Commit 505b2f1

Browse files
committed
Move DirectiveLine into its own submodule
1 parent 8d603ef commit 505b2f1

File tree

2 files changed

+69
-66
lines changed

2 files changed

+69
-66
lines changed

src/tools/compiletest/src/directives.rs

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::directives::auxiliary::{AuxProps, parse_and_update_aux};
1515
use crate::directives::directive_names::{
1616
KNOWN_DIRECTIVE_NAMES, KNOWN_HTMLDOCCK_DIRECTIVE_NAMES, KNOWN_JSONDOCCK_DIRECTIVE_NAMES,
1717
};
18+
use crate::directives::line::{DirectiveLine, line_directive};
1819
use crate::directives::needs::CachedNeedsConditions;
1920
use crate::errors::ErrorKind;
2021
use crate::executor::{CollectedTestDesc, ShouldPanic};
@@ -24,6 +25,7 @@ use crate::util::static_regex;
2425
pub(crate) mod auxiliary;
2526
mod cfg;
2627
mod directive_names;
28+
mod line;
2729
mod needs;
2830
#[cfg(test)]
2931
mod tests;
@@ -820,70 +822,6 @@ impl TestProps {
820822
}
821823
}
822824

823-
/// If the given line begins with the appropriate comment prefix for a directive,
824-
/// returns a struct containing various parts of the directive.
825-
fn line_directive<'line>(
826-
line_number: usize,
827-
original_line: &'line str,
828-
) -> Option<DirectiveLine<'line>> {
829-
// Ignore lines that don't start with the comment prefix.
830-
let after_comment =
831-
original_line.trim_start().strip_prefix(COMPILETEST_DIRECTIVE_PREFIX)?.trim_start();
832-
833-
let revision;
834-
let raw_directive;
835-
836-
if let Some(after_open_bracket) = after_comment.strip_prefix('[') {
837-
// A comment like `//@[foo]` only applies to revision `foo`.
838-
let Some((line_revision, after_close_bracket)) = after_open_bracket.split_once(']') else {
839-
panic!(
840-
"malformed condition directive: expected `{COMPILETEST_DIRECTIVE_PREFIX}[foo]`, found `{original_line}`"
841-
)
842-
};
843-
844-
revision = Some(line_revision);
845-
raw_directive = after_close_bracket.trim_start();
846-
} else {
847-
revision = None;
848-
raw_directive = after_comment;
849-
};
850-
851-
Some(DirectiveLine { line_number, revision, raw_directive })
852-
}
853-
854-
/// The (partly) broken-down contents of a line containing a test directive,
855-
/// which [`iter_directives`] passes to its callback function.
856-
///
857-
/// For example:
858-
///
859-
/// ```text
860-
/// //@ compile-flags: -O
861-
/// ^^^^^^^^^^^^^^^^^ raw_directive
862-
///
863-
/// //@ [foo] compile-flags: -O
864-
/// ^^^ revision
865-
/// ^^^^^^^^^^^^^^^^^ raw_directive
866-
/// ```
867-
struct DirectiveLine<'ln> {
868-
line_number: usize,
869-
/// Some test directives start with a revision name in square brackets
870-
/// (e.g. `[foo]`), and only apply to that revision of the test.
871-
/// If present, this field contains the revision name (e.g. `foo`).
872-
revision: Option<&'ln str>,
873-
/// The main part of the directive, after removing the comment prefix
874-
/// and the optional revision specifier.
875-
///
876-
/// This is "raw" because the directive's name and colon-separated value
877-
/// (if present) have not yet been extracted or checked.
878-
raw_directive: &'ln str,
879-
}
880-
881-
impl<'ln> DirectiveLine<'ln> {
882-
fn applies_to_test_revision(&self, test_revision: Option<&str>) -> bool {
883-
self.revision.is_none() || self.revision == test_revision
884-
}
885-
}
886-
887825
pub(crate) struct CheckDirectiveResult<'ln> {
888826
is_known_directive: bool,
889827
trailing_directive: Option<&'ln str>,
@@ -916,8 +854,6 @@ fn check_directive<'a>(
916854
CheckDirectiveResult { is_known_directive, trailing_directive }
917855
}
918856

919-
const COMPILETEST_DIRECTIVE_PREFIX: &str = "//@";
920-
921857
fn iter_directives(
922858
mode: TestMode,
923859
poisoned: &mut bool,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const COMPILETEST_DIRECTIVE_PREFIX: &str = "//@";
2+
3+
/// If the given line begins with the appropriate comment prefix for a directive,
4+
/// returns a struct containing various parts of the directive.
5+
pub(crate) fn line_directive<'line>(
6+
line_number: usize,
7+
original_line: &'line str,
8+
) -> Option<DirectiveLine<'line>> {
9+
// Ignore lines that don't start with the comment prefix.
10+
let after_comment =
11+
original_line.trim_start().strip_prefix(COMPILETEST_DIRECTIVE_PREFIX)?.trim_start();
12+
13+
let revision;
14+
let raw_directive;
15+
16+
if let Some(after_open_bracket) = after_comment.strip_prefix('[') {
17+
// A comment like `//@[foo]` only applies to revision `foo`.
18+
let Some((line_revision, after_close_bracket)) = after_open_bracket.split_once(']') else {
19+
panic!(
20+
"malformed condition directive: expected `{COMPILETEST_DIRECTIVE_PREFIX}[foo]`, found `{original_line}`"
21+
)
22+
};
23+
24+
revision = Some(line_revision);
25+
raw_directive = after_close_bracket.trim_start();
26+
} else {
27+
revision = None;
28+
raw_directive = after_comment;
29+
};
30+
31+
Some(DirectiveLine { line_number, revision, raw_directive })
32+
}
33+
34+
/// The (partly) broken-down contents of a line containing a test directive,
35+
/// which [`iter_directives`] passes to its callback function.
36+
///
37+
/// For example:
38+
///
39+
/// ```text
40+
/// //@ compile-flags: -O
41+
/// ^^^^^^^^^^^^^^^^^ raw_directive
42+
///
43+
/// //@ [foo] compile-flags: -O
44+
/// ^^^ revision
45+
/// ^^^^^^^^^^^^^^^^^ raw_directive
46+
/// ```
47+
pub(crate) struct DirectiveLine<'ln> {
48+
pub(crate) line_number: usize,
49+
50+
/// Some test directives start with a revision name in square brackets
51+
/// (e.g. `[foo]`), and only apply to that revision of the test.
52+
/// If present, this field contains the revision name (e.g. `foo`).
53+
pub(crate) revision: Option<&'ln str>,
54+
55+
/// The main part of the directive, after removing the comment prefix
56+
/// and the optional revision specifier.
57+
///
58+
/// This is "raw" because the directive's name and colon-separated value
59+
/// (if present) have not yet been extracted or checked.
60+
pub(crate) raw_directive: &'ln str,
61+
}
62+
63+
impl<'ln> DirectiveLine<'ln> {
64+
pub(crate) fn applies_to_test_revision(&self, test_revision: Option<&str>) -> bool {
65+
self.revision.is_none() || self.revision == test_revision
66+
}
67+
}

0 commit comments

Comments
 (0)