Use "JSON mode" as the name for running with `-j`.

This commit is contained in:
Nicholas Nethercote 2020-03-04 09:18:39 +11:00
Родитель d5591e4831
Коммит 8933ac5f23
2 изменённых файлов: 21 добавлений и 21 удалений

Просмотреть файл

@ -53,7 +53,7 @@ impl Interner {
}
}
enum JsonEscaping {
enum JsonMode {
No,
Yes,
}
@ -252,19 +252,19 @@ impl FileInfo {
struct Fixer {
re: Regex,
file_infos: FxHashMap<String, FileInfo>,
json_escaping: JsonEscaping,
json_mode: JsonMode,
}
/// Records address of functions from a symbol table.
type SymFuncAddrs = FxHashMap<String, u64>;
impl Fixer {
fn new(json_escaping: JsonEscaping) -> Fixer {
fn new(json_mode: JsonMode) -> Fixer {
Fixer {
// Matches lines produced by MozFormatCodeAddress().
re: Regex::new(r"^(.*#\d+: )(.+)\[(.+) \+0x([0-9A-Fa-f]+)\](.*)$").unwrap(),
file_infos: FxHashMap::default(),
json_escaping,
json_mode,
}
}
@ -567,13 +567,13 @@ impl Fixer {
},
};
// If JSON escaping is enabled, we need to escape any new strings we
// If JSON mode is enabled, we need to escape any new strings we
// produce. However, strings that came in from the text (i.e.
// `in_func_name` and `in_file_name`), will already be escaped, so if
// they become part of the output they shouldn't be escaped.
// they become part of the output they shouldn't be re-escaped.
if let Some(func_info) = file_info.func_info(address) {
let raw_func_name = func_info.demangled_name();
let out_func_name = if let JsonEscaping::Yes = self.json_escaping {
let out_func_name = if let JsonMode::Yes = self.json_mode {
Fixer::json_escape(&raw_func_name)
} else {
raw_func_name
@ -583,7 +583,7 @@ impl Fixer {
// We have the function name, filename, and line number from
// the debug info.
let raw_file_name = file_info.interner.get(line_info.path);
let out_file_name = if let JsonEscaping::Yes = self.json_escaping {
let out_file_name = if let JsonMode::Yes = self.json_mode {
Fixer::json_escape(&raw_file_name)
} else {
raw_file_name.to_string()
@ -622,19 +622,19 @@ r##"usage: fix-stacks [options] < input > output
Post-process the stack frames produced by MozFormatCodeAddress().
options:
-h, --help show this message and exit
-j, --json Use JSON escaping for printed function names and file names
-h, --help Show this message and exit
-j, --json Treat input and output as JSON fragments
"##;
fn main_inner() -> io::Result<()> {
// Process command line arguments.
let mut json_escaping = JsonEscaping::No;
let mut json_mode = JsonMode::No;
for arg in env::args().skip(1) {
if arg == "-h" || arg == "--help" {
println!("{}", USAGE_MSG);
return Ok(());
} else if arg == "-j" || arg == "--json" {
json_escaping = JsonEscaping::Yes;
json_mode = JsonMode::Yes;
} else {
let msg = format!(
"bad argument `{}`. Run `fix-stacks -h` for more information.",
@ -646,7 +646,7 @@ fn main_inner() -> io::Result<()> {
let reader = io::BufReader::new(io::stdin());
let mut fixer = Fixer::new(json_escaping);
let mut fixer = Fixer::new(json_mode);
for line in reader.lines() {
writeln!(io::stdout(), "{}", fixer.fix(line.unwrap()))?;
}

Просмотреть файл

@ -31,7 +31,7 @@ fn test_linux() {
// LINE 0x11cd line=13 file=/home/njn/moz/fix-stacks/tests/example.c
// LINE 0x11db line=14 file=/home/njn/moz/fix-stacks/tests/example.c
let mut fixer = Fixer::new(JsonEscaping::No);
let mut fixer = Fixer::new(JsonMode::No);
// Test various addresses.
let mut func = |name, addr, linenum| {
@ -62,7 +62,7 @@ fn test_linux() {
func("g", 0x11de, 14);
// Try a new Fixer.
fixer = Fixer::new(JsonEscaping::No);
fixer = Fixer::new(JsonMode::No);
// Test various addresses outside `main`, `f`, and `g`.
let mut outside = |addr| {
@ -110,7 +110,7 @@ fn test_windows() {
// outputs contains backwards slashes, though, because that is what is used
// within the debug info.
let mut fixer = Fixer::new(JsonEscaping::Yes);
let mut fixer = Fixer::new(JsonMode::Yes);
// Test various addresses using `example-windows`, which redirects to
// `example-windows.pdb`.
@ -142,8 +142,8 @@ fn test_windows() {
func("g", 0x6c49, 12);
func("g", 0x6c63, 14);
// Try a new Fixer, without JSON escaping.
fixer = Fixer::new(JsonEscaping::No);
// Try a new Fixer, without JSON mode.
fixer = Fixer::new(JsonMode::No);
// Test various addresses outside `main`, `f`, and `g`, using
// `example-windows.pdb` directly.
@ -229,7 +229,7 @@ fn test_mac() {
// LINE 0xf38 line=10 file=/Users/njn/moz/fix-stacks/tests/mac-lib2.c
// LINE 0xf49 line=11 file=/Users/njn/moz/fix-stacks/tests/mac-lib2.c
let mut fixer = Fixer::new(JsonEscaping::No);
let mut fixer = Fixer::new(JsonMode::No);
// Test addresses from all the object files that `mac-multi` references.
let mut func = |name, addr, full_path, locn| {
@ -266,7 +266,7 @@ fn test_mac() {
#[test]
fn test_regex() {
let mut fixer = Fixer::new(JsonEscaping::No);
let mut fixer = Fixer::new(JsonMode::No);
// Test various different unchanged line forms, that don't match the regex.
let mut unchanged = |line: &str| {
@ -301,7 +301,7 @@ fn test_regex() {
#[test]
fn test_files() {
let mut fixer = Fixer::new(JsonEscaping::Yes);
let mut fixer = Fixer::new(JsonMode::Yes);
// Test various different file errors. An error message is also printed to
// stderr for each one, but we don't test for that.