📄 testwave_app.cpp
字号:
if (!extract_special_information(filename, instr, 'E', expected_error)) return false; if (!expected_error.empty() && !got_expected_result(filename, error, expected_error)) { // we expected an error but got none (or a different one) if (debuglevel > 2) { std::cerr << filename << ": failed" << std::endl << "result: " << std::endl << result << std::endl; if (!error.empty()) { std::cerr << "expected result: " << std::endl << expected << std::endl; } if (!expected_error.empty()) { std::cerr << "expected error: " << std::endl << expected_error << std::endl; } } else if (debuglevel > 1) { std::cerr << filename << ": failed" << std::endl; } retval = false; } else if (!got_expected_result(filename, result, expected)) { // no preprocessing error encountered if (debuglevel > 2) { std::cerr << filename << ": failed" << std::endl << "result: " << std::endl << result << std::endl << "expected: " << std::endl << expected << std::endl; } else if (debuglevel > 1) { std::cerr << filename << ": failed" << std::endl; } retval = false; } else { // preprocessing succeeded, check hook information, if appropriate if (test_hooks && !expected_hooks.empty() && !got_expected_result(filename, hooks, expected_hooks)) { if (debuglevel > 2) { std::cerr << filename << ": failed" << std::endl << "hooks result: " << std::endl << hooks << std::endl; std::cerr << "expected hooks result: " << std::endl << expected_hooks << std::endl; } else if (debuglevel > 1) { std::cerr << filename << ": failed" << std::endl; } retval = false; } } // print success message, if appropriate if (retval) { if (debuglevel > 5) { std::cerr << filename << ": succeeded" << std::endl << "result: " << std::endl << result << std::endl << "hooks result: " << std::endl << hooks << std::endl; } else if (debuglevel > 4) { std::cerr << filename << ": succeeded" << std::endl << "result: " << std::endl << result << std::endl; } else if (debuglevel > 3) { std::cerr << filename << ": succeeded" << std::endl; } printed_result = true; } } if (!pp_result) { // there was a preprocessing error, was it expected? std::string expected_error; if (!extract_special_information(filename, instr, 'E', expected_error)) return false; if (!got_expected_result(filename, error, expected_error)) { // the error was unexpected if (debuglevel > 2) { std::cerr << filename << ": failed" << std::endl; if (!expected_error.empty()) { std::cerr << "error result: " << std::endl << error << std::endl << "expected error: " << std::endl << expected_error << std::endl; } else { std::cerr << "unexpected error: " << error << std::endl; } } else if (debuglevel > 1) { std::cerr << filename << ": failed" << std::endl; } retval = false; } if (retval) { if (debuglevel > 5) { std::cerr << filename << ": succeeded (caught expected error)" << std::endl << "error result: " << std::endl << error << std::endl; if (!printed_result) { std::cerr << "hooks result: " << std::endl << hooks << std::endl; } } else if (debuglevel > 4) { std::cerr << filename << ": succeeded (caught expected error)" << std::endl << "error result: " << std::endl << error << std::endl; } else if (debuglevel > 3) { // caught the expected error message std::cerr << filename << ": succeeded" << std::endl; } } } return retval; } else { std::cerr << filename << ": no information about expected results found" << std::endl; } return false;}/////////////////////////////////////////////////////////////////////////////////// print the current version of this program/////////////////////////////////////////////////////////////////////////////////int testwave_app::print_version(){// get time of last compilation of this fileboost::wave::util::time_conversion_helper compilation_time(__DATE__ " " __TIME__);// calculate the number of days since Feb 12 2005 // (the day the testwave project was started)std::tm first_day; using namespace std; // some platforms have memset in namespace std memset (&first_day, 0, sizeof(std::tm)); first_day.tm_mon = 1; // Feb first_day.tm_mday = 12; // 12 first_day.tm_year = 105; // 2005long seconds = long(std::difftime(compilation_time.get_time(), std::mktime(&first_day))); std::cout << TESTWAVE_VERSION_MAJOR << '.' << TESTWAVE_VERSION_MINOR << '.' << TESTWAVE_VERSION_SUBMINOR << '.' << seconds/(3600*24) // get number of days from seconds << std::endl; return 0; // exit app}/////////////////////////////////////////////////////////////////////////////////// print the copyright statement/////////////////////////////////////////////////////////////////////////////////int testwave_app::print_copyright(){ char const *copyright[] = { "", "Testwave: A test driver for the Boost.Wave C++ preprocessor library", "http://www.boost.org/", "", "Copyright (c) 2001-2008 Hartmut Kaiser, Distributed under the Boost", "Software License, Version 1.0. (See accompanying file", "LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)", 0 }; for (int i = 0; 0 != copyright[i]; ++i) std::cout << copyright[i] << std::endl; return 0; // exit app}/////////////////////////////////////////////////////////////////////////////////// Read the given file into a string/////////////////////////////////////////////////////////////////////////////////bool testwave_app::read_file(std::string const& filename, std::string& instr){// open the given file and report error, if appropriate std::ifstream instream(filename.c_str()); if (!instream.is_open()) { std::cerr << "testwave: could not open input file: " << filename << std::endl; return false; } else if (9 == debuglevel) { std::cerr << "read_file: succeeded to open input file: " << filename << std::endl; } instream.unsetf(std::ios::skipws);// read the input file into a string #if defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)// this is known to be very slow for large files on some systems std::copy (std::istream_iterator<char>(instream), std::istream_iterator<char>(), std::inserter(instr, instr.end()));#else instr = std::string(std::istreambuf_iterator<char>(instream.rdbuf()), std::istreambuf_iterator<char>());#endif if (9 == debuglevel) { std::cerr << "read_file: succeeded to read input file: " << filename << std::endl; } return true;}///////////////////////////////////////////////////////////////////////////////namespace { std::string const& trim_whitespace(std::string& value) { std::string::size_type first = value.find_first_not_of(" \t"); if (std::string::npos == first) value.clear(); else { std::string::size_type last = value.find_last_not_of(" \t"); assert(std::string::npos != last); value = value.substr(first, last-first+1); } return value; }}/////////////////////////////////////////////////////////////////////////////////// Extract special information from comments marked with the given letter/////////////////////////////////////////////////////////////////////////////////bool testwave_app::extract_special_information(std::string const& filename, std::string const& instr, char flag, std::string& content){ if (9 == debuglevel) { std::cerr << "extract_special_information: extracting special information ('" << flag << "') from input file: " << filename << std::endl; }// tokenize the input data into C++ tokens using the C++ lexer typedef boost::wave::cpplexer::lex_token<> token_type; typedef boost::wave::cpplexer::lex_iterator<token_type> lexer_type; typedef token_type::position_type position_type; boost::wave::language_support const lang_opts = (boost::wave::language_support)( boost::wave::support_option_variadics | boost::wave::support_option_long_long | boost::wave::support_option_no_character_validation | boost::wave::support_option_convert_trigraphs | boost::wave::support_option_insert_whitespace); position_type pos(filename.c_str()); lexer_type it = lexer_type(instr.begin(), instr.end(), pos, lang_opts); lexer_type end = lexer_type(); try { // look for C or C++ comments starting with the special character for (/**/; it != end; ++it) { using namespace boost::wave; token_id id = token_id(*it); if (T_CCOMMENT == id) { std::string value = (*it).get_value().c_str(); if (flag == value[2]) { if (value.size() > 3 && '(' == value[3]) { std::size_t p = value.find_first_of(")"); if (std::string::npos == p) { std::cerr << "testwave: missing closing parenthesis in '" << flag << "()' directive" << std::endl; return false; } std::string source = value.substr(4, p-4); std::string result, error, hooks; bool pp_result = preprocess_file(filename, source, result, error, hooks, true); if (!pp_result) { std::cerr
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -