Skip to content

Commit

Permalink
chore: format all code ready for adding cargo format check point in G… (
Browse files Browse the repository at this point in the history
#119)

chore: format all code ready for adding cargo format check point in Github actions.
  • Loading branch information
Peefy authored Jul 27, 2022
1 parent 844dd15 commit 7f91ecd
Show file tree
Hide file tree
Showing 19 changed files with 125 additions and 97 deletions.
3 changes: 0 additions & 3 deletions kclvm/3rdparty/rustc_data_structures/src/base_n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
/// Bases up to and including 36 can be used for case-insensitive things.
use std::str;

#[cfg(test)]
mod tests;

pub const MAX_BASE: usize = 64;
pub const ALPHANUMERIC_ONLY: usize = 62;
pub const CASE_INSENSITIVE: usize = 36;
Expand Down
4 changes: 3 additions & 1 deletion kclvm/3rdparty/rustc_data_structures/src/stable_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ where
K: Eq + Hash,
{
pub fn new() -> StableMap<K, V> {
StableMap { base: FxHashMap::default() }
StableMap {
base: FxHashMap::default(),
}
}

pub fn into_sorted_vector(self) -> Vec<(K, V)>
Expand Down
4 changes: 3 additions & 1 deletion kclvm/3rdparty/rustc_data_structures/src/stable_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ impl<T> Eq for StableSet<T> where T: Eq + Hash {}

impl<T: Hash + Eq> StableSet<T> {
pub fn new() -> StableSet<T> {
StableSet { base: FxHashSet::default() }
StableSet {
base: FxHashSet::default(),
}
}

pub fn into_sorted_vector(self) -> Vec<T>
Expand Down
76 changes: 37 additions & 39 deletions kclvm/3rdparty/rustc_data_structures/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ use std::hash::{BuildHasher, Hash};
pub use std::sync::atomic::Ordering;
pub use std::sync::atomic::Ordering::SeqCst;

pub use std::marker::Send as Send;
pub use std::marker::Sync as Sync;
pub use std::marker::Send;
pub use std::marker::Sync;

pub use parking_lot::RwLockReadGuard as ReadGuard;
pub use parking_lot::MappedRwLockReadGuard as MappedReadGuard;
pub use parking_lot::RwLockWriteGuard as WriteGuard;
pub use parking_lot::MappedRwLockWriteGuard as MappedWriteGuard;
pub use parking_lot::MappedRwLockReadGuard as MappedReadGuard;
pub use parking_lot::MappedRwLockWriteGuard as MappedWriteGuard;
pub use parking_lot::RwLockReadGuard as ReadGuard;
pub use parking_lot::RwLockWriteGuard as WriteGuard;

pub use parking_lot::MutexGuard as LockGuard;
pub use parking_lot::MappedMutexGuard as MappedLockGuard;
pub use parking_lot::MappedMutexGuard as MappedLockGuard;
pub use parking_lot::MutexGuard as LockGuard;

pub use std::sync::atomic::{AtomicBool, AtomicUsize, AtomicU32, AtomicU64};
pub use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, AtomicUsize};

pub use std::sync::Arc as Lrc;
pub use std::sync::Weak as Weak;
pub use std::sync::Arc as Lrc;
pub use std::sync::Weak;

pub type MTRef<'a, T> = &'a T;
pub type MTRef<'a, T> = &'a T;

pub use rayon::{join, scope};
pub use rayon::{join, scope};

/// Runs a list of blocks in parallel. The first block is executed immediately on
/// the current thread. Use that for the longest running block.
#[macro_export]
macro_rules! parallel {
/// Runs a list of blocks in parallel. The first block is executed immediately on
/// the current thread. Use that for the longest running block.
#[macro_export]
macro_rules! parallel {
(impl $fblock:tt [$($c:tt,)*] [$block:tt $(, $rest:tt)*]) => {
parallel!(impl $fblock [$block, $($c,)*] [$($rest),*])
};
Expand All @@ -66,31 +66,27 @@ pub use std::sync::atomic::Ordering::SeqCst;
};
}

pub use rayon_core::WorkerLocal;
pub use rayon_core::WorkerLocal;

pub use rayon::iter::ParallelIterator;
use rayon::iter::IntoParallelIterator;
use rayon::iter::IntoParallelIterator;
pub use rayon::iter::ParallelIterator;

pub fn par_iter<T: IntoParallelIterator>(t: T) -> T::Iter {
t.into_par_iter()
}

pub fn par_for_each_in<T: IntoParallelIterator>(
t: T,
for_each: impl Fn(T::Item) + Sync + Send,
) {
t.into_par_iter().for_each(for_each)
}
pub fn par_iter<T: IntoParallelIterator>(t: T) -> T::Iter {
t.into_par_iter()
}

#[macro_export]
macro_rules! rustc_erase_owner {
($v:expr) => {{
let v = $v;
::rustc_data_structures::sync::assert_send_val(&v);
v.erase_send_sync_owner()
}}
}
pub fn par_for_each_in<T: IntoParallelIterator>(t: T, for_each: impl Fn(T::Item) + Sync + Send) {
t.into_par_iter().for_each(for_each)
}

#[macro_export]
macro_rules! rustc_erase_owner {
($v:expr) => {{
let v = $v;
::rustc_data_structures::sync::assert_send_val(&v);
v.erase_send_sync_owner()
}};
}

pub fn assert_sync<T: ?Sized + Sync>() {}
pub fn assert_send<T: ?Sized + Send>() {}
Expand All @@ -105,6 +101,8 @@ pub trait HashMapExt<K, V> {

impl<K: Eq + Hash, V: Eq, S: BuildHasher> HashMapExt<K, V> for HashMap<K, V, S> {
fn insert_same(&mut self, key: K, value: V) {
self.entry(key).and_modify(|old| assert!(*old == value)).or_insert(value);
self.entry(key)
.and_modify(|old| assert!(*old == value))
.or_insert(value);
}
}
5 changes: 4 additions & 1 deletion kclvm/3rdparty/rustc_data_structures/src/temp_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ impl AsRef<Path> for MaybeTempDir {

impl MaybeTempDir {
pub fn new(dir: TempDir, keep_on_drop: bool) -> MaybeTempDir {
MaybeTempDir { dir: ManuallyDrop::new(dir), keep: keep_on_drop }
MaybeTempDir {
dir: ManuallyDrop::new(dir),
keep: keep_on_drop,
}
}
}
4 changes: 2 additions & 2 deletions kclvm/ast/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::ast;
/// Try get a config expr mut ref from a expr if the expr is a schema or a config.
/// If not, return [None].
/// TODO: use [TryInto]?
///
///
/// # Examples
///
/// ```
Expand All @@ -29,6 +29,6 @@ pub fn try_get_config_expr_mut(expr: &mut ast::Expr) -> Option<&mut ast::ConfigE
}
}
ast::Expr::Config(config_expr) => Some(config_expr),
_ => None
_ => None,
}
}
3 changes: 2 additions & 1 deletion kclvm/config/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ pub fn write_info_cache(
let mut cache = read_info_cache(root, cache_name);
cache.insert(relative_path, cache_info);
let mut file = File::create(&tmp_filename).unwrap();
file.write_all(&ron::ser::to_string(&cache).unwrap().as_bytes()).unwrap();
file.write_all(&ron::ser::to_string(&cache).unwrap().as_bytes())
.unwrap();
std::fs::rename(&tmp_filename, &dst_filename).unwrap();
lock_file.unlock().unwrap();
Ok(())
Expand Down
17 changes: 10 additions & 7 deletions kclvm/parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,16 @@ impl<'a> Lexer<'a> {
kclvm_lexer::TokenKind::Minus => {
let head = start + BytePos::from_u32(1);
let tail = start + BytePos::from_u32(2);
if self.has_next_token(head, tail){
let next_tkn =
self.str_from_to(head, tail);
if self.has_next_token(head, tail) {
let next_tkn = self.str_from_to(head, tail);
if next_tkn == ">" {
// waste '>' token
self.pos = self.pos + BytePos::from_usize(1);
token::RArrow
} else {
token::BinOp(token::Minus)
token::BinOp(token::Minus)
}
}else{
} else {
token::BinOp(token::Minus)
}
}
Expand Down Expand Up @@ -546,8 +545,12 @@ impl<'a> Lexer<'a> {
&self.src[self.src_index(start)..self.src_index(end)]
}

fn has_next_token(&self, start: BytePos, end: BytePos) -> bool{
if self.src_index(start) > self.src_index(end) || self.src_index(end) > self.src.len(){ false }else{ true }
fn has_next_token(&self, start: BytePos, end: BytePos) -> bool {
if self.src_index(start) > self.src_index(end) || self.src_index(end) > self.src.len() {
false
} else {
true
}
}

fn symbol_from_to(&self, start: BytePos, end: BytePos) -> Symbol {
Expand Down
4 changes: 2 additions & 2 deletions kclvm/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn parse_file(filename: &str, code: Option<String>) -> Result<ast::Module, S
}

/// Parse a source string to a expression. When input empty string, it will return [None].
///
///
/// # Examples
/// ```
/// use kclvm_ast::ast;
Expand All @@ -108,7 +108,7 @@ pub fn parse_expr(src: &str) -> Option<ast::NodeRef<ast::Expr>> {
let sm = SourceMap::new(FilePathMapping::empty());
sm.new_source_file(PathBuf::from("").into(), src.to_string());
let sess = &ParseSession::with_source_map(Arc::new(sm));

Some(create_session_globals_then(|| {
let stream = parse_token_streams(sess, src, BytePos::from_u32(0));
let mut parser = Parser::new(sess, stream);
Expand Down
6 changes: 4 additions & 2 deletions kclvm/parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1992,7 +1992,8 @@ impl<'a> Parser<'a> {
let value = match result {
Some(value) => value,
None => {
self.sess.struct_token_error(&[token::LitKind::Integer.into()], token);
self.sess
.struct_token_error(&[token::LitKind::Integer.into()], token);
}
};
match lk.suffix {
Expand All @@ -2005,7 +2006,8 @@ impl<'a> Parser<'a> {
let value = match result {
Ok(value) => value,
_ => {
self.sess.struct_token_error(&[token::LitKind::Float.into()], token);
self.sess
.struct_token_error(&[token::LitKind::Float.into()], token);
}
};
(None, NumberLitValue::Float(value))
Expand Down
2 changes: 1 addition & 1 deletion kclvm/parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl<'a> Parser<'a> {
node: Comment {
text: x.as_str().to_string(),
},
filename: filename,
filename,
line: lo.line as u64,
column: lo.col.to_usize() as u64,
end_line: hi.line as u64,
Expand Down
22 changes: 6 additions & 16 deletions kclvm/parser/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::panic::{catch_unwind, set_hook};

use crate::*;

use expect_test::{expect, Expect};
use core::any::Any;
use expect_test::{expect, Expect};

fn check_parsing_file_ast_json(filename: &str, src: &str, expect: Expect) {
let m = parse_file(filename, Some(src.into())).unwrap();
Expand All @@ -12,13 +12,6 @@ fn check_parsing_file_ast_json(filename: &str, src: &str, expect: Expect) {
expect.assert_eq(&actual)
}

fn check_load_program_ast_json(files: &[&str], opts: Option<LoadProgramOptions>, expect: Expect) {
let prog = load_program(&files, opts).unwrap();
let actual = serde_json::ser::to_string(&prog).unwrap();
let actual = format!("{}\n", actual);
expect.assert_eq(&actual)
}

#[test]
fn test_parse_file() {
check_parsing_file_ast_json(
Expand Down Expand Up @@ -76,7 +69,7 @@ c = 3 # comment4444
);
}

pub fn check_result_panic_info(result: Result<(), Box<dyn Any + Send>>){
pub fn check_result_panic_info(result: Result<(), Box<dyn Any + Send>>) {
match result {
Err(e) => match e.downcast::<String>() {
Ok(_v) => {
Expand All @@ -89,19 +82,16 @@ pub fn check_result_panic_info(result: Result<(), Box<dyn Any + Send>>){
};
}

const PARSE_EXPR_INVALID_TEST_CASES: &[&'static str; 3] = &[
"fs1_i1re1~s",
"fh==-h==-",
"8_________i"
];
const PARSE_EXPR_INVALID_TEST_CASES: &[&'static str; 3] =
&["fs1_i1re1~s", "fh==-h==-", "8_________i"];

#[test]
pub fn test_parse_expr_invalid() {
for case in PARSE_EXPR_INVALID_TEST_CASES{
for case in PARSE_EXPR_INVALID_TEST_CASES {
set_hook(Box::new(|_| {}));
let result = catch_unwind(|| {
parse_expr(&case);
});
check_result_panic_info(result);
}
}
}
26 changes: 16 additions & 10 deletions kclvm/runner/benches/bench_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ use kclvm_runner::{execute, runner::ExecProgramArgs};
const EXEC_DATA_PATH: &str = "./src/exec_data/";

pub fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("refactor kclvm-runner", |b| b.iter(|| {
let prev_hook = std::panic::take_hook();
// disable print panic info
std::panic::set_hook(Box::new(|_| {}));
let result =
std::panic::catch_unwind(|| {
c.bench_function("refactor kclvm-runner", |b| {
b.iter(|| {
let prev_hook = std::panic::take_hook();
// disable print panic info
std::panic::set_hook(Box::new(|_| {}));
let result = std::panic::catch_unwind(|| {
for file in get_files(EXEC_DATA_PATH, false, true, ".k") {
exec(&file).unwrap();
}
});
assert!(result.is_ok());
std::panic::set_hook(prev_hook);
}));
assert!(result.is_ok());
std::panic::set_hook(prev_hook);
})
});
}

criterion_group!(benches, criterion_benchmark);
Expand All @@ -39,7 +40,12 @@ fn exec(file: &str) -> Result<String, String> {
}

/// Get kcl files from path.
fn get_files<P: AsRef<Path>>(path: P, recursively: bool, sorted: bool, suffix: &str) -> Vec<String> {
fn get_files<P: AsRef<Path>>(
path: P,
recursively: bool,
sorted: bool,
suffix: &str,
) -> Vec<String> {
let mut files = vec![];
for entry in WalkDir::new(path).into_iter().filter_map(|e| e.ok()) {
let path = entry.path();
Expand Down
Loading

0 comments on commit 7f91ecd

Please sign in to comment.