Skip to content

Commit 4b25abd

Browse files
committed
Add a ui_test header check in tidy
1 parent 84d4d2e commit 4b25abd

File tree

5 files changed

+489
-31
lines changed

5 files changed

+489
-31
lines changed

src/tools/compiletest/src/header.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ fn iter_header_extra(
606606
// Process any extra directives supplied by the caller (e.g. because they
607607
// are implied by the test mode), with a dummy line number of 0.
608608
for directive in extra_directives {
609-
it(TestComment::new(None, test_common::CommentKind::Compiletest(directive), 0));
609+
it(TestComment::new(directive, None, test_common::CommentKind::Compiletest(directive), 0));
610610
}
611611

612612
test_common::iter_header(testfile, rdr, it);

src/tools/test_common/src/directives.rs

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,26 @@ pub trait NameDirective {
3939
fn parse_name_negative(&self, comment: &TestComment<'_>) -> bool;
4040
}
4141

42+
pub trait TestDirective {
43+
fn compiletest_name(&self) -> &'static str;
44+
fn ui_test_name(&self) -> Option<&'static str>;
45+
}
46+
4247
macro_rules! name_value_directive {
4348
($item:ident, $compiletest_name:literal) => {
4449
#[derive(Debug, Clone, Copy)]
4550
pub struct $item;
51+
52+
impl TestDirective for $item {
53+
fn compiletest_name(&self) -> &'static str {
54+
$compiletest_name
55+
}
56+
57+
fn ui_test_name(&self) -> Option<&'static str> {
58+
None
59+
}
60+
}
61+
4662
impl NameValueDirective for $item {
4763
fn parse_name_value<'line>(
4864
&self,
@@ -68,6 +84,17 @@ macro_rules! name_value_directive {
6884
($item:ident, $compiletest_name:literal, $ui_test_name:literal) => {
6985
#[derive(Debug, Clone, Copy)]
7086
pub struct $item;
87+
88+
impl TestDirective for $item {
89+
fn compiletest_name(&self) -> &'static str {
90+
$compiletest_name
91+
}
92+
93+
fn ui_test_name(&self) -> Option<&'static str> {
94+
Some($ui_test_name)
95+
}
96+
}
97+
7198
impl NameValueDirective for $item {
7299
fn parse_name_value<'line>(
73100
&self,
@@ -106,6 +133,17 @@ macro_rules! name_directive {
106133
($item:ident, $compiletest_name:literal) => {
107134
#[derive(Debug, Clone, Copy)]
108135
pub struct $item;
136+
137+
impl TestDirective for $item {
138+
fn compiletest_name(&self) -> &'static str {
139+
$compiletest_name
140+
}
141+
142+
fn ui_test_name(&self) -> Option<&'static str> {
143+
None
144+
}
145+
}
146+
109147
impl NameDirective for $item {
110148
fn parse_name(&self, comment: &TestComment<'_>) -> bool {
111149
match comment.comment() {
@@ -134,6 +172,17 @@ macro_rules! name_directive {
134172
($item:ident, $compiletest_name:literal, $ui_test_name:literal) => {
135173
#[derive(Debug, Clone, Copy)]
136174
pub struct $item;
175+
176+
impl TestDirective for $item {
177+
fn compiletest_name(&self) -> &'static str {
178+
$compiletest_name
179+
}
180+
181+
fn ui_test_name(&self) -> Option<&'static str> {
182+
Some($ui_test_name)
183+
}
184+
}
185+
137186
impl NameDirective for $item {
138187
fn parse_name(&self, comment: &TestComment<'_>) -> bool {
139188
match comment.comment() {
@@ -173,6 +222,17 @@ macro_rules! name_val_or_name_directive {
173222
($item:ident, $compiletest_name:literal) => {
174223
#[derive(Debug, Clone, Copy)]
175224
pub struct $item;
225+
226+
impl TestDirective for $item {
227+
fn compiletest_name(&self) -> &'static str {
228+
$compiletest_name
229+
}
230+
231+
fn ui_test_name(&self) -> Option<&'static str> {
232+
None
233+
}
234+
}
235+
176236
impl NameValueDirective for $item {
177237
fn parse_name_value<'line>(
178238
&self,
@@ -222,6 +282,17 @@ macro_rules! name_val_or_name_directive {
222282
($item:ident, $compiletest_name:literal, $ui_test_name:literal) => {
223283
#[derive(Debug, Clone, Copy)]
224284
pub struct $item;
285+
286+
impl TestDirective for $item {
287+
fn compiletest_name(&self) -> &'static str {
288+
$compiletest_name
289+
}
290+
291+
fn ui_test_name(&self) -> Option<&'static str> {
292+
Some($ui_test_name)
293+
}
294+
}
295+
225296
impl NameValueDirective for $item {
226297
fn parse_name_value<'line>(
227298
&self,
@@ -292,7 +363,7 @@ macro_rules! name_val_or_name_directive {
292363
// Macros are in the form (name, compiletest_name, ui_test_name).
293364
// If ui_test_name does not exist, ui_test does not support that directive.
294365
// ========================================================================
295-
name_value_directive!(ErrorPatternDirective, "error-pattern", "error-in-other-file");
366+
name_value_directive!(ErrorPatternDirective, "error-in-other-file", "error-pattern");
296367
name_value_directive!(CompileFlagsDirective, "compile-flags", "compile-flags");
297368
name_value_directive!(RunFlagsDirective, "run-flags"); // UNUSED IN UI TESTS
298369
name_value_directive!(PrettyModeDirective, "pretty-mode"); // UNUSED IN UI TESTS

src/tools/test_common/src/lib.rs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,39 @@ pub struct TestComment<'line> {
99
revision: Option<&'line str>,
1010
comment: CommentKind<'line>,
1111
line_num: usize,
12+
full_line: &'line str,
1213
}
1314

1415
impl<'line> TestComment<'line> {
15-
pub fn new(revision: Option<&'line str>, comment: CommentKind<'line>, line_num: usize) -> Self {
16-
Self { revision, comment, line_num }
16+
pub const fn new(
17+
line: &'line str,
18+
revision: Option<&'line str>,
19+
comment: CommentKind<'line>,
20+
line_num: usize,
21+
) -> Self {
22+
Self { revision, comment, line_num, full_line: line }
1723
}
1824

19-
pub fn revision(&self) -> Option<&str> {
25+
pub const fn revision(&self) -> Option<&str> {
2026
self.revision
2127
}
2228

23-
pub fn comment(&self) -> CommentKind<'_> {
29+
pub const fn comment(&self) -> CommentKind<'_> {
2430
self.comment
2531
}
2632

27-
pub fn line_num(&self) -> usize {
33+
pub const fn line_num(&self) -> usize {
2834
self.line_num
2935
}
3036

31-
pub fn comment_str(&self) -> &str {
37+
pub const fn comment_str(&self) -> &str {
3238
self.comment.line()
3339
}
40+
41+
/// The full line that contains the comment. You almost never want this.
42+
pub const fn full_line(&self) -> &str {
43+
self.full_line
44+
}
3445
}
3546

3647
#[derive(Debug, Clone, Copy)]
@@ -44,7 +55,7 @@ pub enum CommentKind<'line> {
4455
}
4556

4657
impl CommentKind<'_> {
47-
pub fn line(&self) -> &str {
58+
pub const fn line(&self) -> &str {
4859
match self {
4960
CommentKind::Compiletest(line) | CommentKind::UiTest(line) => line,
5061
}
@@ -61,13 +72,13 @@ pub fn iter_header<R: Read>(testfile: &Path, rdr: R, it: &mut dyn FnMut(TestComm
6172
let is_rust_file = testfile.extension().is_some_and(|e| e == "rs");
6273

6374
let mut rdr = BufReader::new(rdr);
64-
let mut ln = String::new();
75+
let mut full_ln = String::new();
6576
let mut line_num = 0;
6677

6778
loop {
6879
line_num += 1;
69-
ln.clear();
70-
if rdr.read_line(&mut ln).unwrap() == 0 {
80+
full_ln.clear();
81+
if rdr.read_line(&mut full_ln).unwrap() == 0 {
7182
break;
7283
}
7384

@@ -79,22 +90,33 @@ pub fn iter_header<R: Read>(testfile: &Path, rdr: R, it: &mut dyn FnMut(TestComm
7990
// Assume that any directives will be found before the first
8091
// module or function. This doesn't seem to be an optimization
8192
// with a warm page cache. Maybe with a cold one.
82-
let ln = ln.trim();
93+
let ln = full_ln.trim();
8394
if ln.starts_with("fn") || ln.starts_with("mod") {
8495
return;
8596
} else if is_rust_file {
8697
// first try to parse as a ui test comment, then as a compiletest comment
8798
if let Some((lncfg, ln)) = line_directive(RUST_UI_TEST_COMMENT, ln) {
88-
let directive = TestComment::new(lncfg, CommentKind::UiTest(ln), line_num);
99+
let directive =
100+
TestComment::new(full_ln.as_str(), lncfg, CommentKind::UiTest(ln), line_num);
89101
it(directive);
90102
} else if let Some((lncfg, ln)) = line_directive(RUST_COMPILETEST_COMMENT, ln) {
91-
let directive = TestComment::new(lncfg, CommentKind::Compiletest(ln), line_num);
103+
let directive = TestComment::new(
104+
full_ln.as_str(),
105+
lncfg,
106+
CommentKind::Compiletest(ln),
107+
line_num,
108+
);
92109
it(directive);
93110
}
94111
} else {
95112
// parse a non-rust file that compiletest knows about
96113
if let Some((lncfg, ln)) = line_directive(NON_RUST_COMMENT, ln) {
97-
let directive = TestComment::new(lncfg, CommentKind::Compiletest(ln), line_num);
114+
let directive = TestComment::new(
115+
full_ln.as_str(),
116+
lncfg,
117+
CommentKind::Compiletest(ln),
118+
line_num,
119+
);
98120
it(directive);
99121
}
100122
}

0 commit comments

Comments
 (0)