Merge pull request #1383 from tree-sitter/feat/debug-build

feat(cli): add a flag to compile a parser in debug mode with -O0 C/C++ compiler flag
This commit is contained in:
Max Brunsfeld 2021-09-13 11:39:13 -07:00 committed by GitHub
commit 6c66b5ee29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 8 deletions

View file

@ -101,6 +101,7 @@ pub struct Loader {
language_configuration_ids_by_file_type: HashMap<String, Vec<usize>>,
highlight_names: Box<Mutex<Vec<String>>>,
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<PathBuf>,
) -> Result<Language> {
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> {

View file

@ -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(&current_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());