Skip to content

Commit 097a090

Browse files
committed
Move DirectiveLine into its own submodule
1 parent 2cb4e7d commit 097a090

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::edition::{Edition, parse_edition};
2021
use crate::errors::ErrorKind;
@@ -25,6 +26,7 @@ use crate::{fatal, help};
2526
pub(crate) mod auxiliary;
2627
mod cfg;
2728
mod directive_names;
29+
mod line;
2830
mod needs;
2931
#[cfg(test)]
3032
mod tests;
@@ -824,70 +826,6 @@ impl TestProps {
824826
}
825827
}
826828

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

923-
const COMPILETEST_DIRECTIVE_PREFIX: &str = "//@";
924-
925861
fn iter_directives(
926862
mode: TestMode,
927863
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)