Skip to content

Commit ce18573

Browse files
Move DebuggerCommands to its own file
1 parent bc8ad24 commit ce18573

File tree

2 files changed

+71
-48
lines changed

2 files changed

+71
-48
lines changed

src/tools/compiletest/src/runtest.rs

Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ use tracing::*;
3838
use crate::extract_gdb_version;
3939
use crate::is_android_gdb_target;
4040

41+
mod debugger;
42+
use debugger::DebuggerCommands;
43+
4144
#[cfg(test)]
4245
mod tests;
4346

@@ -200,12 +203,6 @@ struct TestCx<'test> {
200203
revision: Option<&'test str>,
201204
}
202205

203-
struct DebuggerCommands {
204-
commands: Vec<String>,
205-
check_lines: Vec<String>,
206-
breakpoint_lines: Vec<usize>,
207-
}
208-
209206
enum ReadFrom {
210207
Path,
211208
Stdin(String),
@@ -674,7 +671,10 @@ impl<'test> TestCx<'test> {
674671

675672
// Parse debugger commands etc from test files
676673
let DebuggerCommands { commands, check_lines, breakpoint_lines, .. } =
677-
self.parse_debugger_commands(prefixes);
674+
match DebuggerCommands::parse_from(&self.testpaths.file, self.config, prefixes) {
675+
Ok(cmds) => cmds,
676+
Err(e) => self.fatal(&e),
677+
};
678678

679679
// https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/debugger-commands
680680
let mut script_str = String::with_capacity(2048);
@@ -757,7 +757,10 @@ impl<'test> TestCx<'test> {
757757
};
758758

759759
let DebuggerCommands { commands, check_lines, breakpoint_lines } =
760-
self.parse_debugger_commands(prefixes);
760+
match DebuggerCommands::parse_from(&self.testpaths.file, self.config, prefixes) {
761+
Ok(cmds) => cmds,
762+
Err(e) => self.fatal(&e),
763+
};
761764
let mut cmds = commands.join("\n");
762765

763766
// compile test file (it should have 'compile-flags:-g' in the header)
@@ -1018,7 +1021,10 @@ impl<'test> TestCx<'test> {
10181021

10191022
// Parse debugger commands etc from test files
10201023
let DebuggerCommands { commands, check_lines, breakpoint_lines, .. } =
1021-
self.parse_debugger_commands(prefixes);
1024+
match DebuggerCommands::parse_from(&self.testpaths.file, self.config, prefixes) {
1025+
Ok(cmds) => cmds,
1026+
Err(e) => self.fatal(&e),
1027+
};
10221028

10231029
// Write debugger script:
10241030
// We don't want to hang when calling `quit` while the process is still running
@@ -1131,45 +1137,6 @@ impl<'test> TestCx<'test> {
11311137
ProcRes { status, stdout: out, stderr: err, cmdline: format!("{:?}", cmd) }
11321138
}
11331139

1134-
fn parse_debugger_commands(&self, debugger_prefixes: &[&str]) -> DebuggerCommands {
1135-
let directives = debugger_prefixes
1136-
.iter()
1137-
.map(|prefix| (format!("{}-command", prefix), format!("{}-check", prefix)))
1138-
.collect::<Vec<_>>();
1139-
1140-
let mut breakpoint_lines = vec![];
1141-
let mut commands = vec![];
1142-
let mut check_lines = vec![];
1143-
let mut counter = 1;
1144-
let reader = BufReader::new(File::open(&self.testpaths.file).unwrap());
1145-
for line in reader.lines() {
1146-
match line {
1147-
Ok(line) => {
1148-
let line =
1149-
if line.starts_with("//") { line[2..].trim_start() } else { line.as_str() };
1150-
1151-
if line.contains("#break") {
1152-
breakpoint_lines.push(counter);
1153-
}
1154-
1155-
for &(ref command_directive, ref check_directive) in &directives {
1156-
self.config
1157-
.parse_name_value_directive(&line, command_directive)
1158-
.map(|cmd| commands.push(cmd));
1159-
1160-
self.config
1161-
.parse_name_value_directive(&line, check_directive)
1162-
.map(|cmd| check_lines.push(cmd));
1163-
}
1164-
}
1165-
Err(e) => self.fatal(&format!("Error while parsing debugger commands: {}", e)),
1166-
}
1167-
counter += 1;
1168-
}
1169-
1170-
DebuggerCommands { commands, check_lines, breakpoint_lines }
1171-
}
1172-
11731140
fn cleanup_debug_info_options(&self, options: &Option<String>) -> Option<String> {
11741141
if options.is_none() {
11751142
return None;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use crate::common::Config;
2+
3+
use std::fs::File;
4+
use std::io::{BufRead, BufReader};
5+
use std::path::Path;
6+
7+
pub(super) struct DebuggerCommands {
8+
pub commands: Vec<String>,
9+
pub check_lines: Vec<String>,
10+
pub breakpoint_lines: Vec<usize>,
11+
}
12+
13+
impl DebuggerCommands {
14+
pub(super) fn parse_from(
15+
file: &Path,
16+
config: &Config,
17+
debugger_prefixes: &[&str],
18+
) -> Result<Self, String> {
19+
let directives = debugger_prefixes
20+
.iter()
21+
.map(|prefix| (format!("{}-command", prefix), format!("{}-check", prefix)))
22+
.collect::<Vec<_>>();
23+
24+
let mut breakpoint_lines = vec![];
25+
let mut commands = vec![];
26+
let mut check_lines = vec![];
27+
let mut counter = 1;
28+
let reader = BufReader::new(File::open(file).unwrap());
29+
for line in reader.lines() {
30+
match line {
31+
Ok(line) => {
32+
let line =
33+
if line.starts_with("//") { line[2..].trim_start() } else { line.as_str() };
34+
35+
if line.contains("#break") {
36+
breakpoint_lines.push(counter);
37+
}
38+
39+
for &(ref command_directive, ref check_directive) in &directives {
40+
config
41+
.parse_name_value_directive(&line, command_directive)
42+
.map(|cmd| commands.push(cmd));
43+
44+
config
45+
.parse_name_value_directive(&line, check_directive)
46+
.map(|cmd| check_lines.push(cmd));
47+
}
48+
}
49+
Err(e) => return Err(format!("Error while parsing debugger commands: {}", e)),
50+
}
51+
counter += 1;
52+
}
53+
54+
Ok(Self { commands, check_lines, breakpoint_lines })
55+
}
56+
}

0 commit comments

Comments
 (0)