From ba4a697d8cb577c03c84c0c91a25ecbeaa9c68e6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 8 Sep 2018 18:34:41 +0300 Subject: [PATCH] move fuzz-invariants to the library --- crates/libsyntax2/fuzz/fuzz_targets/parser.rs | 5 +-- crates/libsyntax2/src/lib.rs | 38 ++--------------- crates/libsyntax2/src/utils.rs | 42 ++++++++++++++++++- 3 files changed, 44 insertions(+), 41 deletions(-) diff --git a/crates/libsyntax2/fuzz/fuzz_targets/parser.rs b/crates/libsyntax2/fuzz/fuzz_targets/parser.rs index f941855e86..da87180bb4 100644 --- a/crates/libsyntax2/fuzz/fuzz_targets/parser.rs +++ b/crates/libsyntax2/fuzz/fuzz_targets/parser.rs @@ -4,9 +4,6 @@ extern crate libsyntax2; fuzz_target!(|data: &[u8]| { if let Ok(text) = std::str::from_utf8(data) { - let x = libsyntax2::File::parse(text); - let _ = x.ast(); - let _ = x.syntax(); - let _ = x.errors(); + libsyntax2::utils::check_fuzz_invariants(text) } }); diff --git a/crates/libsyntax2/src/lib.rs b/crates/libsyntax2/src/lib.rs index 7a30f5d387..d955c01e72 100644 --- a/crates/libsyntax2/src/lib.rs +++ b/crates/libsyntax2/src/lib.rs @@ -66,7 +66,9 @@ impl File { fn new(green: GreenNode, errors: Vec) -> File { let root = SyntaxRoot::new(green, errors); let root = SyntaxNode::new_owned(root); - validate_block_structure(root.borrowed()); + if cfg!(debug_assertions) { + utils::validate_block_structure(root.borrowed()); + } File { root } } pub fn parse(text: &str) -> File { @@ -112,40 +114,6 @@ impl File { } } -#[cfg(not(debug_assertions))] -fn validate_block_structure(_: SyntaxNodeRef) {} - -#[cfg(debug_assertions)] -fn validate_block_structure(root: SyntaxNodeRef) { - let mut stack = Vec::new(); - for node in algo::walk::preorder(root) { - match node.kind() { - SyntaxKind::L_CURLY => { - stack.push(node) - } - SyntaxKind::R_CURLY => { - if let Some(pair) = stack.pop() { - assert_eq!( - node.parent(), - pair.parent(), - "\nunpaired curleys:\n{}\n{}\n", - root.text(), - utils::dump_tree(root), - ); - assert!( - node.next_sibling().is_none() && pair.prev_sibling().is_none(), - "\nfloating curlys at {:?}\nfile:\n{}\nerror:\n{}\n", - node, - root.text(), - node.text(), - ); - } - } - _ => (), - } - } -} - #[derive(Debug, Clone)] pub struct AtomEdit { pub delete: TextRange, diff --git a/crates/libsyntax2/src/utils.rs b/crates/libsyntax2/src/utils.rs index fbe48dd71c..671dd7afa7 100644 --- a/crates/libsyntax2/src/utils.rs +++ b/crates/libsyntax2/src/utils.rs @@ -1,7 +1,7 @@ use std::fmt::Write; use { - algo::walk::{walk, WalkEvent}, - SyntaxNodeRef, TreeRoot, + algo::walk::{preorder, walk, WalkEvent}, + SyntaxKind, File, SyntaxNodeRef, TreeRoot, }; /// Parse a file and create a string representation of the resulting parse tree. @@ -45,3 +45,41 @@ pub fn dump_tree(syntax: SyntaxNodeRef) -> String { return buf; } + +pub fn check_fuzz_invariants(text: &str) { + let file = File::parse(text); + let root = file.syntax(); + validate_block_structure(root); + let _ = file.ast(); + let _ = file.errors(); +} + +pub(crate) fn validate_block_structure(root: SyntaxNodeRef) { + let mut stack = Vec::new(); + for node in preorder(root) { + match node.kind() { + SyntaxKind::L_CURLY => { + stack.push(node) + } + SyntaxKind::R_CURLY => { + if let Some(pair) = stack.pop() { + assert_eq!( + node.parent(), + pair.parent(), + "\nunpaired curleys:\n{}\n{}\n", + root.text(), + dump_tree(root), + ); + assert!( + node.next_sibling().is_none() && pair.prev_sibling().is_none(), + "\nfloating curlys at {:?}\nfile:\n{}\nerror:\n{}\n", + node, + root.text(), + node.text(), + ); + } + } + _ => (), + } + } +}