From 0ccb910922218f7ee5f6ce25b16b870f762e27f6 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 21 Mar 2019 11:26:05 -0700 Subject: [PATCH 1/2] Use a size_t instead of a uint32_t for cancellation flag --- cli/src/parse.rs | 4 ++-- cli/src/tests/parser_test.rs | 4 ++-- highlight/include/tree_sitter/highlight.h | 2 +- highlight/src/c_lib.rs | 6 +++--- highlight/src/lib.rs | 6 +++--- lib/binding/bindings.rs | 5 +++-- lib/binding/lib.rs | 10 +++++----- lib/include/tree_sitter/api.h | 4 ++-- lib/src/atomic.h | 4 ++-- lib/src/parser.c | 10 +++++----- 10 files changed, 28 insertions(+), 27 deletions(-) diff --git a/cli/src/parse.rs b/cli/src/parse.rs index b5230e77..1a0a73cc 100644 --- a/cli/src/parse.rs +++ b/cli/src/parse.rs @@ -2,7 +2,7 @@ use super::error::{Error, Result}; use super::util; use std::io::{self, Write}; use std::path::Path; -use std::sync::atomic::{AtomicU32, Ordering}; +use std::sync::atomic::{AtomicUsize, Ordering}; use std::time::Instant; use std::{fs, thread}; use tree_sitter::{Language, LogType, Parser}; @@ -27,7 +27,7 @@ pub fn parse_file_at_path( // If the `--cancel` flag was passed, then cancel the parse // when the user types a newline. if allow_cancellation { - let flag = Box::new(AtomicU32::new(0)); + let flag = Box::new(AtomicUsize::new(0)); unsafe { parser.set_cancellation_flag(Some(&flag)) }; thread::spawn(move || { let mut line = String::new(); diff --git a/cli/src/tests/parser_test.rs b/cli/src/tests/parser_test.rs index 63a4f3e8..51b44fde 100644 --- a/cli/src/tests/parser_test.rs +++ b/cli/src/tests/parser_test.rs @@ -1,7 +1,7 @@ use super::helpers::edits::{perform_edit, Edit, ReadRecorder}; use super::helpers::fixtures::{get_language, get_test_language}; use crate::generate::generate_parser_for_grammar; -use std::sync::atomic::{AtomicU32, Ordering}; +use std::sync::atomic::{AtomicUsize, Ordering}; use std::{thread, time}; use tree_sitter::{InputEdit, LogType, Parser, Point, Range}; @@ -303,7 +303,7 @@ fn test_parsing_on_multiple_threads() { #[test] fn test_parsing_cancelled_by_another_thread() { - let cancellation_flag = Box::new(AtomicU32::new(0)); + let cancellation_flag = Box::new(AtomicUsize::new(0)); let mut parser = Parser::new(); parser.set_language(get_language("javascript")).unwrap(); diff --git a/highlight/include/tree_sitter/highlight.h b/highlight/include/tree_sitter/highlight.h index 19f09fbd..347999d2 100644 --- a/highlight/include/tree_sitter/highlight.h +++ b/highlight/include/tree_sitter/highlight.h @@ -81,7 +81,7 @@ int ts_highlighter_highlight( const char *source_code, uint32_t source_code_len, TSHighlightBuffer *output, - const uint32_t *cancellation_flag + const size_t *cancellation_flag ); // TSHighlightBuffer: This struct stores the HTML output of syntax diff --git a/highlight/src/c_lib.rs b/highlight/src/c_lib.rs index 38596007..b8098980 100644 --- a/highlight/src/c_lib.rs +++ b/highlight/src/c_lib.rs @@ -5,7 +5,7 @@ use std::ffi::CStr; use std::io::Write; use std::os::raw::c_char; use std::process::abort; -use std::sync::atomic::AtomicU32; +use std::sync::atomic::AtomicUsize; use std::{fmt, slice}; use tree_sitter::{Language, PropertySheet}; @@ -137,7 +137,7 @@ pub extern "C" fn ts_highlighter_highlight( source_code: *const c_char, source_code_len: u32, output: *mut TSHighlightBuffer, - cancellation_flag: *const AtomicU32, + cancellation_flag: *const AtomicUsize, ) -> ErrorCode { let this = unwrap_ptr(this); let output = unwrap_mut_ptr(output); @@ -154,7 +154,7 @@ impl TSHighlighter { source_code: &[u8], scope_name: &str, output: &mut TSHighlightBuffer, - cancellation_flag: Option<&AtomicU32>, + cancellation_flag: Option<&AtomicUsize>, ) -> ErrorCode { let configuration = self.languages.get(scope_name); if configuration.is_none() { diff --git a/highlight/src/lib.rs b/highlight/src/lib.rs index 66c52c86..967b6466 100644 --- a/highlight/src/lib.rs +++ b/highlight/src/lib.rs @@ -6,7 +6,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; use serde_derive::*; use std::fmt::{self, Write}; use std::mem::transmute; -use std::sync::atomic::{AtomicU32, Ordering}; +use std::sync::atomic::{AtomicUsize, Ordering}; use std::{cmp, str, usize}; use tree_sitter::{Language, Node, Parser, Point, PropertySheet, Range, Tree, TreePropertyCursor}; @@ -95,7 +95,7 @@ where layers: Vec>, utf8_error_len: Option, operation_count: usize, - cancellation_flag: Option<&'a AtomicU32>, + cancellation_flag: Option<&'a AtomicUsize>, } #[derive(Copy, Clone, Debug)] @@ -382,7 +382,7 @@ where language: Language, property_sheet: &'a PropertySheet, injection_callback: F, - cancellation_flag: Option<&'a AtomicU32>, + cancellation_flag: Option<&'a AtomicUsize>, ) -> Result { let mut parser = Parser::new(); unsafe { parser.set_cancellation_flag(cancellation_flag.clone()) }; diff --git a/lib/binding/bindings.rs b/lib/binding/bindings.rs index b828c9e6..6dcbc09d 100644 --- a/lib/binding/bindings.rs +++ b/lib/binding/bindings.rs @@ -1,5 +1,6 @@ /* automatically generated by rust-bindgen */ +pub type __darwin_size_t = ::std::os::raw::c_ulong; pub type FILE = [u64; 19usize]; pub type TSSymbol = u16; #[repr(C)] @@ -136,10 +137,10 @@ extern "C" { ) -> *mut TSTree; } extern "C" { - pub fn ts_parser_cancellation_flag(arg1: *const TSParser) -> *const u32; + pub fn ts_parser_cancellation_flag(arg1: *const TSParser) -> *const usize; } extern "C" { - pub fn ts_parser_set_cancellation_flag(arg1: *mut TSParser, arg2: *const u32); + pub fn ts_parser_set_cancellation_flag(arg1: *mut TSParser, arg2: *const usize); } extern "C" { pub fn ts_parser_timeout_micros(arg1: *const TSParser) -> u64; diff --git a/lib/binding/lib.rs b/lib/binding/lib.rs index cc514c22..088a50c2 100644 --- a/lib/binding/lib.rs +++ b/lib/binding/lib.rs @@ -15,7 +15,7 @@ use std::collections::HashMap; use std::ffi::CStr; use std::marker::PhantomData; use std::os::raw::{c_char, c_void}; -use std::sync::atomic::AtomicU32; +use std::sync::atomic::AtomicUsize; use std::{fmt, ptr, slice, str, u16}; pub const PARSER_HEADER: &'static str = include_str!("../include/tree_sitter/parser.h"); @@ -336,13 +336,13 @@ impl Parser { }; } - pub unsafe fn cancellation_flag(&self) -> Option<&AtomicU32> { - (ffi::ts_parser_cancellation_flag(self.0) as *const AtomicU32).as_ref() + pub unsafe fn cancellation_flag(&self) -> Option<&AtomicUsize> { + (ffi::ts_parser_cancellation_flag(self.0) as *const AtomicUsize).as_ref() } - pub unsafe fn set_cancellation_flag(&self, flag: Option<&AtomicU32>) { + pub unsafe fn set_cancellation_flag(&self, flag: Option<&AtomicUsize>) { if let Some(flag) = flag { - ffi::ts_parser_set_cancellation_flag(self.0, flag as *const AtomicU32 as *const u32); + ffi::ts_parser_set_cancellation_flag(self.0, flag as *const AtomicUsize as *const usize); } else { ffi::ts_parser_set_cancellation_flag(self.0, ptr::null()); } diff --git a/lib/include/tree_sitter/api.h b/lib/include/tree_sitter/api.h index cfc5393d..380a1d50 100644 --- a/lib/include/tree_sitter/api.h +++ b/lib/include/tree_sitter/api.h @@ -88,8 +88,8 @@ void ts_parser_halt_on_error(TSParser *, bool); TSTree *ts_parser_parse(TSParser *, const TSTree *, TSInput); TSTree *ts_parser_parse_string(TSParser *, const TSTree *, const char *, uint32_t); TSTree *ts_parser_parse_string_encoding(TSParser *, const TSTree *, const char *, uint32_t, TSInputEncoding); -const uint32_t *ts_parser_cancellation_flag(const TSParser *); -void ts_parser_set_cancellation_flag(TSParser *, const uint32_t *); +const size_t *ts_parser_cancellation_flag(const TSParser *); +void ts_parser_set_cancellation_flag(TSParser *, const size_t *); uint64_t ts_parser_timeout_micros(const TSParser *); void ts_parser_set_timeout_micros(TSParser *, uint64_t); void ts_parser_reset(TSParser *); diff --git a/lib/src/atomic.h b/lib/src/atomic.h index 89f40e48..1df4ce08 100644 --- a/lib/src/atomic.h +++ b/lib/src/atomic.h @@ -7,7 +7,7 @@ #include -static inline uint32_t atomic_load(const volatile uint32_t *p) { +static inline size_t atomic_load(const volatile size_t *p) { return *p; } @@ -21,7 +21,7 @@ static inline uint32_t atomic_dec(volatile uint32_t *p) { #else -static inline uint32_t atomic_load(const volatile uint32_t *p) { +static inline size_t atomic_load(const volatile size_t *p) { return __atomic_load_n(p, __ATOMIC_RELAXED); } diff --git a/lib/src/parser.c b/lib/src/parser.c index 39699866..f2993a51 100644 --- a/lib/src/parser.c +++ b/lib/src/parser.c @@ -70,7 +70,7 @@ struct TSParser { uint64_t clock_limit; uint64_t start_clock; unsigned operation_count; - const volatile uint32_t *cancellation_flag; + const volatile size_t *cancellation_flag; bool halt_on_error; Subtree old_tree; TSRangeArray included_range_differences; @@ -1589,12 +1589,12 @@ void ts_parser_halt_on_error(TSParser *self, bool should_halt_on_error) { self->halt_on_error = should_halt_on_error; } -const uint32_t *ts_parser_cancellation_flag(const TSParser *self) { - return (const uint32_t *)self->cancellation_flag; +const size_t *ts_parser_cancellation_flag(const TSParser *self) { + return (const size_t *)self->cancellation_flag; } -void ts_parser_set_cancellation_flag(TSParser *self, const uint32_t *flag) { - self->cancellation_flag = (const volatile uint32_t *)flag; +void ts_parser_set_cancellation_flag(TSParser *self, const size_t *flag) { + self->cancellation_flag = (const volatile size_t *)flag; } uint64_t ts_parser_timeout_micros(const TSParser *self) { From 501708623294b369f86770892bc133f42f1f5f51 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Thu, 21 Mar 2019 11:26:13 -0700 Subject: [PATCH 2/2] Switch back to rust stable on CI --- .appveyor.yml | 4 ++-- .travis.yml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index a2787be7..610ac134 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -5,8 +5,8 @@ install: # Install rust - appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe - - IF "%PLATFORM%" == "x86" rustup-init -y --default-toolchain beta --default-host i686-pc-windows-msvc - - IF "%PLATFORM%" == "x64" rustup-init -y --default-toolchain beta --default-host x86_64-pc-windows-msvc + - IF "%PLATFORM%" == "x86" rustup-init -y --default-toolchain stable --default-host i686-pc-windows-msvc + - IF "%PLATFORM%" == "x64" rustup-init -y --default-toolchain stable --default-host x86_64-pc-windows-msvc - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin - rustc -vV - cargo -vV diff --git a/.travis.yml b/.travis.yml index b92cd1f2..06c71b34 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: rust rust: - - beta + - stable os: - linux