From 62d79b80e1255323a64d024223a67536e7e0adea Mon Sep 17 00:00:00 2001 From: Andrew Hlynskyi Date: Wed, 8 Sep 2021 00:08:13 +0300 Subject: [PATCH] feat(cli): add a flag to compile a parser in debug mode with -O0 C/C++ compiler flag --- cli/loader/src/lib.rs | 33 +++++++++++++++++++++++++-------- cli/src/main.rs | 14 ++++++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/cli/loader/src/lib.rs b/cli/loader/src/lib.rs index d1a37df0..0002bf08 100644 --- a/cli/loader/src/lib.rs +++ b/cli/loader/src/lib.rs @@ -101,6 +101,7 @@ pub struct Loader { language_configuration_ids_by_file_type: HashMap>, highlight_names: Box>>, use_all_highlight_names: bool, + debug_build: bool, } unsafe impl Send for Loader {} @@ -122,6 +123,7 @@ impl Loader { language_configuration_ids_by_file_type: HashMap::new(), highlight_names: Box::new(Mutex::new(Vec::new())), use_all_highlight_names: true, + debug_build: false, } } @@ -347,7 +349,11 @@ impl Loader { parser_path: &Path, scanner_path: &Option, ) -> Result { - let mut library_path = self.parser_lib_path.join(name); + let mut lib_name = name.to_string(); + if self.debug_build { + lib_name.push_str(".debug._"); + } + let mut library_path = self.parser_lib_path.join(lib_name); library_path.set_extension(DYLIB_EXTENSION); let recompile = needs_recompile(&library_path, &parser_path, &scanner_path) @@ -369,11 +375,13 @@ impl Loader { } if cfg!(windows) { - command - .args(&["/nologo", "/LD", "/I"]) - .arg(header_path) - .arg("/Od") - .arg(parser_path); + command.args(&["/nologo", "/LD", "/I"]).arg(header_path); + if self.debug_build { + command.arg("/Od"); + } else { + command.arg("/O2"); + } + command.arg(parser_path); if let Some(scanner_path) = scanner_path.as_ref() { command.arg(scanner_path); } @@ -389,8 +397,13 @@ impl Loader { .arg("-I") .arg(header_path) .arg("-o") - .arg(&library_path) - .arg("-O2"); + .arg(&library_path); + + if self.debug_build { + command.arg("-O0"); + } else { + command.arg("-O2"); + } // For conditional compilation of external scanner code when // used internally by `tree-siteer parse` and other sub commands. @@ -644,6 +657,10 @@ impl Loader { Err(anyhow!("No language found")) } } + + pub fn use_debug_build(&mut self, flag: bool) { + self.debug_build = flag; + } } impl<'a> LanguageConfiguration<'a> { diff --git a/cli/src/main.rs b/cli/src/main.rs index f64e4973..6687373d 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -45,6 +45,11 @@ fn run() -> Result<()> { .long("debug-graph") .short("D"); + let debug_build_arg = Arg::with_name("debug-build") + .help("Compile a parser in debug mode") + .long("debug-build") + .short("0"); + let paths_file_arg = Arg::with_name("paths-file") .help("The path to a file with paths to source file(s)") .long("paths") @@ -103,6 +108,7 @@ fn run() -> Result<()> { .arg(&paths_arg) .arg(&scope_arg) .arg(&debug_arg) + .arg(&debug_build_arg) .arg(&debug_graph_arg) .arg(Arg::with_name("debug-xml").long("xml").short("x")) .arg( @@ -178,6 +184,7 @@ fn run() -> Result<()> { .help("Update all syntax trees in corpus files with current parser output"), ) .arg(&debug_arg) + .arg(&debug_build_arg) .arg(&debug_graph_arg), ) .subcommand( @@ -273,8 +280,12 @@ fn run() -> Result<()> { ("test", Some(matches)) => { let debug = matches.is_present("debug"); let debug_graph = matches.is_present("debug-graph"); + let debug_build = matches.is_present("debug-build"); let update = matches.is_present("update"); let filter = matches.value_of("filter"); + + loader.use_debug_build(debug_build); + let languages = loader.languages_at_path(¤t_dir)?; let language = languages .first() @@ -310,6 +321,7 @@ fn run() -> Result<()> { ("parse", Some(matches)) => { let debug = matches.is_present("debug"); let debug_graph = matches.is_present("debug-graph"); + let debug_build = matches.is_present("debug-build"); let debug_xml = matches.is_present("debug-xml"); let quiet = matches.is_present("quiet"); let time = matches.is_present("time"); @@ -323,6 +335,8 @@ fn run() -> Result<()> { env::set_var("TREE_SITTER_DEBUG", "1"); } + loader.use_debug_build(debug_build); + let timeout = matches .value_of("timeout") .map_or(0, |t| u64::from_str_radix(t, 10).unwrap());