diff --git a/qa/common_tools/CMakeLists.txt b/qa/common_tools/CMakeLists.txt index 445f1d217c..8d72dd1c39 100644 --- a/qa/common_tools/CMakeLists.txt +++ b/qa/common_tools/CMakeLists.txt @@ -36,6 +36,8 @@ add_executable( qa_common_tools tools/coroutines/coroutines.cpp tools/io_benchmark/io_benchmark.cpp + + tools/sexpr_parser/sexpr_parse.cpp ) include_directories( @@ -49,6 +51,7 @@ target_link_libraries( qa_common_tools legacy_gal gal qa_utils + sexpr ${wxWidgets_LIBRARIES} ) diff --git a/qa/common_tools/main.cpp b/qa/common_tools/main.cpp index f537fd826a..b2c9482aee 100644 --- a/qa/common_tools/main.cpp +++ b/qa/common_tools/main.cpp @@ -25,6 +25,7 @@ #include "tools/coroutines/coroutine_tools.h" #include "tools/io_benchmark/io_benchmark.h" +#include "tools/sexpr_parser/sexpr_parse.h" /** * List of registered tools. @@ -35,6 +36,7 @@ const static std::vector known_tools = { &coroutine_tool, &io_benchmark_tool, + &sexpr_parser_tool, }; diff --git a/qa/common_tools/tools/sexpr_parser/sexpr_parse.cpp b/qa/common_tools/tools/sexpr_parser/sexpr_parse.cpp new file mode 100644 index 0000000000..b5a23ec179 --- /dev/null +++ b/qa/common_tools/tools/sexpr_parser/sexpr_parse.cpp @@ -0,0 +1,161 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you may find one here: + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or you may search the http://www.gnu.org website for the version 2 license, + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +/** + * @file + * Utility tool for parsing S-Expression data with the SEXPR class + * for benchmarking, testing, etc. + */ + +#include "sexpr_parse.h" + +#include + +#include +#include + +#include + +#include +#include + + +class QA_SEXPR_PARSER +{ +public: + QA_SEXPR_PARSER( bool aVerbose ) : m_verbose( aVerbose ) + { + } + + bool Parse( std::istream& aStream ) + { + // Don't let the parser handle stream reading - we don't want to + // see how long the disk IO takes. Read to memory first (event the + // biggest files will fit in) + const std::string sexpr_str( std::istreambuf_iterator( aStream ), {} ); + + PROF_COUNTER timer; + // Perform the parse + std::unique_ptr sexpr( m_parser.Parse( sexpr_str ) ); + + if( m_verbose ) + timer.Show( "S-Expression Parsing" ); + + return sexpr != nullptr; + } + +private: + bool m_verbose; + SEXPR::PARSER m_parser; +}; + +static const wxCmdLineEntryDesc g_cmdLineDesc[] = { + { + wxCMD_LINE_SWITCH, + "h", + "help", + _( "displays help on the command line parameters" ).mb_str(), + wxCMD_LINE_VAL_NONE, + wxCMD_LINE_OPTION_HELP, + }, + { + wxCMD_LINE_SWITCH, + "v", + "verbose", + _( "print parsing information" ).mb_str(), + }, + { + wxCMD_LINE_PARAM, + nullptr, + nullptr, + _( "input file" ).mb_str(), + wxCMD_LINE_VAL_STRING, + wxCMD_LINE_PARAM_OPTIONAL | wxCMD_LINE_PARAM_MULTIPLE, + }, + { wxCMD_LINE_NONE } +}; + + +enum PARSER_RET_CODES +{ + PARSE_FAILED = KI_TEST::RET_CODES::TOOL_SPECIFIC, +}; + + +int sexpr_parser_func( int argc, char* argv[] ) +{ + wxCmdLineParser cl_parser( argc, argv ); + cl_parser.SetDesc( g_cmdLineDesc ); + cl_parser.AddUsageText( _( "Tests parsing of S-Expression files" ) ); + + int cmd_parsed_ok = cl_parser.Parse(); + if( cmd_parsed_ok != 0 ) + { + // Help and invalid input both stop here + return ( cmd_parsed_ok == -1 ) ? KI_TEST::RET_CODES::OK : KI_TEST::RET_CODES::BAD_CMDLINE; + } + + const auto file_count = cl_parser.GetParamCount(); + const bool verbose = cl_parser.Found( "verbose" ); + + QA_SEXPR_PARSER qa_parser( verbose ); + + bool ok = true; + + if( file_count == 0 ) + { + // Parse the file provided on stdin - used by AFL to drive the + // program + qa_parser.Parse( std::cin ); + } + else + { + // Parse 'n' files given on the command line + // (this is useful for input minimisation (e.g. afl-tmin) as + // well as manual testing + for( unsigned i = 0; i < file_count; i++ ) + { + const auto filename = cl_parser.GetParam( i ).ToStdString(); + + if( verbose ) + std::cout << "Parsing: " << filename << std::endl; + + std::ifstream fin; + fin.open( filename ); + + ok = ok && qa_parser.Parse( fin ); + } + } + + if( !ok ) + return PARSER_RET_CODES::PARSE_FAILED; + + return KI_TEST::RET_CODES::OK; +} + + +KI_TEST::UTILITY_PROGRAM sexpr_parser_tool = { + "sexpr_parser", + "Benchmark s-expression parsing", + sexpr_parser_func, +}; \ No newline at end of file diff --git a/qa/common_tools/tools/sexpr_parser/sexpr_parse.h b/qa/common_tools/tools/sexpr_parser/sexpr_parse.h new file mode 100644 index 0000000000..6ff57380be --- /dev/null +++ b/qa/common_tools/tools/sexpr_parser/sexpr_parse.h @@ -0,0 +1,31 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2019 KiCad Developers, see CHANGELOG.TXT for contributors. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you may find one here: + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or you may search the http://www.gnu.org website for the version 2 license, + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef QA_COMMON_TOOLS_SEXPR_PARSE__H +#define QA_COMMON_TOOLS_SEXPR_PARSE__H + +#include + +extern KI_TEST::UTILITY_PROGRAM sexpr_parser_tool; + +#endif // QA_COMMON_TOOLS_SEXPR_PARSE__H \ No newline at end of file