1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
|
/*=============================================================================
Boost.Wave: A Standard compliant C++ preprocessor library
http://www.boost.org/
Copyright (c) 2001-2006 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)
=============================================================================*/
// system headers
#include <string>
#include <iostream>
#include <vector>
// include boost
#include <boost/config.hpp>
#include <boost/throw_exception.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/detail/workaround.hpp>
// include Wave
#include <boost/wave.hpp>
// include the lexer related stuff
#include <boost/wave/cpplexer/cpp_lex_token.hpp> // token type
#include <boost/wave/cpplexer/cpp_lex_iterator.hpp> // lexer type
// test application related headers
#include "cmd_line_utils.hpp"
#include "testwave_app.hpp"
namespace po = boost::program_options;
namespace fs = boost::filesystem;
///////////////////////////////////////////////////////////////////////////////
// testwave version definitions
#define TESTWAVE_VERSION_MAJOR 0
#define TESTWAVE_VERSION_MINOR 4
#define TESTWAVE_VERSION_SUBMINOR 0
///////////////////////////////////////////////////////////////////////////////
// workaround for missing ostringstream
#ifdef BOOST_NO_STRINGSTREAM
#include <strstream>
#define BOOST_WAVETEST_OSSTREAM std::ostrstream
std::string BOOST_WAVETEST_GETSTRING(std::ostrstream& ss)
{
ss << ends;
std::string rval = ss.str();
ss.freeze(false);
return rval;
}
#else
#include <sstream>
#define BOOST_WAVETEST_GETSTRING(ss) ss.str()
#define BOOST_WAVETEST_OSSTREAM std::ostringstream
#endif
namespace {
///////////////////////////////////////////////////////////////////////////
template <typename Iterator>
inline bool
handle_next_token(Iterator &it, Iterator const& end,
std::string &result)
{
typedef typename Iterator::value_type token_type;
token_type tok = *it++;
result = result + tok.get_value().c_str();
return (it == end) ? false : true;
}
///////////////////////////////////////////////////////////////////////////
template <typename String>
String const& handle_quoted_filepath(String &name)
{
using boost::wave::util::impl::unescape_lit;
String unesc_name = unescape_lit(name.substr(1, name.size()-2));
fs::path p (unesc_name.c_str(), fs::native);
name = String("\"") + p.leaf().c_str() + String("\"");
return name;
}
///////////////////////////////////////////////////////////////////////////
template <typename String>
String const& handle_filepath(String &name)
{
using boost::wave::util::impl::unescape_lit;
String unesc_name = unescape_lit(name);
fs::path p (unesc_name.c_str(), fs::native);
name = p.leaf().c_str();
return name;
}
///////////////////////////////////////////////////////////////////////////
template <typename Iterator>
bool handle_line_directive(Iterator &it, Iterator const& end,
std::string &result)
{
typedef typename Iterator::value_type token_type;
typedef typename token_type::string_type string_type;
if (!handle_next_token(it, end, result) || // #line
!handle_next_token(it, end, result) || // whitespace
!handle_next_token(it, end, result) || // number
!handle_next_token(it, end, result)) // whitespace
{
return false;
}
using boost::wave::util::impl::unescape_lit;
token_type filename = *it;
string_type name = filename.get_value();
handle_quoted_filepath(name);
result = result + name.c_str();
return true;
}
}
///////////////////////////////////////////////////////////////////////////
//
// This function compares the real result and the expected one but first
// replaces all occurences in the expected result of
// $E: to the result of preprocessing the given expression
// $F: to the passed full filepath
// $P: to the full path
// $V: to the current Boost version number
//
///////////////////////////////////////////////////////////////////////////
bool
testwave_app::got_expected_result(std::string const& filename,
std::string const& result, std::string& expected)
{
using boost::wave::util::impl::escape_lit;
std::string full_result;
std::string::size_type pos = 0;
std::string::size_type pos1 = expected.find_first_of("$");
if (pos1 != std::string::npos) {
do {
switch(expected[pos1+1]) {
case 'E': // preprocess the given token sequence
{
if ('(' == expected[pos1+2]) {
std::size_t p = expected.find_first_of(")", pos1+1);
if (std::string::npos == p) {
std::cerr
<< "testwave: unmatched parenthesis in $E"
" directive" << std::endl;
return false;
}
std::string source = expected.substr(pos1+3, p-pos1-3);
std::string result, error;
bool pp_result = preprocess_file(filename, source,
result, error, true);
if (!pp_result) {
std::cerr
<< "testwave: preprocessing error in $E directive: "
<< error << std::endl;
return false;
}
full_result = full_result +
expected.substr(pos, pos1-pos) + result;
pos1 = expected.find_first_of ("$",
pos = pos1 + 4 + source.size());
}
}
break;
case 'F': // insert base file name
full_result = full_result +
expected.substr(pos, pos1-pos) + escape_lit(filename);
pos1 = expected.find_first_of ("$", pos = pos1 + 2);
break;
case 'P': // insert full path
{
fs::path fullpath = fs::complete(
fs::path(filename, fs::native),
fs::current_path());
if ('(' == expected[pos1+2]) {
// the $P(basename) syntax is used
std::size_t p = expected.find_first_of(")", pos1+1);
if (std::string::npos == p) {
std::cerr
<< "testwave: unmatched parenthesis in $P"
" directive" << std::endl;
return false;
}
std::string base = expected.substr(pos1+3, p-pos1-3);
fullpath = fullpath.branch_path() /
fs::path(base, fs::native);
full_result = full_result +
expected.substr(pos, pos1-pos) +
escape_lit(fullpath.normalize().native_file_string());
pos1 = expected.find_first_of ("$",
pos = pos1 + 4 + base.size());
}
else {
// the $P is used on its own
full_result = full_result +
expected.substr(pos, pos1-pos) +
escape_lit(fullpath.native_file_string());
pos1 = expected.find_first_of ("$", pos = pos1 + 2);
}
}
break;
case 'V': // insert Boost version
full_result = full_result +
expected.substr(pos, pos1-pos) + BOOST_LIB_VERSION;
pos1 = expected.find_first_of ("$", pos = pos1 + 2);
break;
default:
full_result = full_result +
expected.substr(pos, pos1-pos);
pos1 = expected.find_first_of ("$", (pos = pos1) + 1);
break;
}
} while(pos1 != std::string::npos);
full_result += expected.substr(pos);
}
else {
full_result = expected;
}
expected = full_result;
return full_result == result;
}
///////////////////////////////////////////////////////////////////////////////
testwave_app::testwave_app(po::variables_map const& vm)
: debuglevel(1), desc_options("Preprocessor configuration options"),
global_vm(vm)
{
desc_options.add_options()
("include,I", po::value<cmd_line_utils::include_paths>()->composing(),
"specify an additional include directory")
("sysinclude,S", po::value<std::vector<std::string> >()->composing(),
"specify an additional system include directory")
("define,D", po::value<std::vector<std::string> >()->composing(),
"specify a macro to define (as macro[=[value]])")
("predefine,P", po::value<std::vector<std::string> >()->composing(),
"specify a macro to predefine (as macro[=[value]])")
("undefine,U", po::value<std::vector<std::string> >()->composing(),
"specify a macro to undefine")
("nesting,n", po::value<int>(),
"specify a new maximal include nesting depth")
("long_long", "enable long long support in C++ mode")
("preserve", "preserve comments")
#if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
("variadics", "enable certain C99 extensions in C++ mode")
("c99", "enable C99 mode (implies --variadics)")
#endif
#if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0
("noguard,G", "disable include guard detection")
#endif
;
}
///////////////////////////////////////////////////////////////////////////////
//
// Test the given file (i.e. preprocess the file and compare the result
// against the embedded 'R' comments, if an error occurs compare the error
// message against the given 'E' comments).
//
///////////////////////////////////////////////////////////////////////////////
bool
testwave_app::test_a_file(std::string filename)
{
// read the input file into a string
std::string instr;
if (!read_file(filename, instr))
return false; // error was reported already
// extract expected output, preprocess the data and compare results
std::string expected;
if (extract_expected_output(filename, instr, expected)) {
bool retval = true; // assume success
std::string result, error;
bool pp_result = preprocess_file(filename, instr, result, error);
if (pp_result || !result.empty()) {
// did we expect an error?
std::string expected_error;
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 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;
}
}
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
<< "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;
}
else if (debuglevel > 4) {
std::cerr
<< filename << ": succeeded" << std::endl
<< "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 file
boost::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; // 2005
long 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-2006 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);
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;
bool pp_result = preprocess_file(filename, source,
result, error, true);
if (!pp_result) {
std::cerr
<< "testwave: preprocessing error in '" << flag
<< "()' directive: " << error << std::endl;
return false;
}
// include this text into the extracted information
// only if the result is not zero
using namespace std; // some system have atoi in namespace std
if (0 != atoi(result.c_str())) {
std::string thiscontent(value.substr(p+1));
if (9 == debuglevel) {
std::cerr << "extract_special_information: extracted: "
<< thiscontent << std::endl;
}
trim_whitespace(thiscontent);
content += thiscontent;
}
}
else {
std::string thiscontent(value.substr(3, value.size()-5));
if (9 == debuglevel) {
std::cerr << "extract_special_information: extracted: "
<< thiscontent << std::endl;
}
trim_whitespace(thiscontent);
content += thiscontent;
}
}
}
else if (T_CPPCOMMENT == 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;
bool pp_result = preprocess_file(filename, source,
result, error, true);
if (!pp_result) {
std::cerr
<< "testwave: preprocessing error in '" << flag
<< "()' directive: " << error << std::endl;
return false;
}
// include this text into the extracted information
// only if the result is not zero
using namespace std; // some system have atoi in namespace std
if (0 != atoi(result.c_str())) {
std::string thiscontent(value.substr((' ' == value[p+1]) ? p+2 : p+1));
if (9 == debuglevel) {
std::cerr << "extract_special_information: extracted: "
<< thiscontent << std::endl;
}
trim_whitespace(thiscontent);
content += thiscontent;
}
}
else {
std::string thiscontent(value.substr((' ' == value[3]) ? 4 : 3));
if (9 == debuglevel) {
std::cerr << "extract_special_information: extracted: "
<< thiscontent;
}
trim_whitespace(content);
content += thiscontent;
}
}
}
}
}
catch (boost::wave::cpplexer::lexing_exception const &e) {
// some lexing error
std::cerr
<< e.file_name() << "(" << e.line_no() << "): "
<< e.description() << std::endl;
return false;
}
if (9 == debuglevel) {
std::cerr << "extract_special_information: succeeded extracting special information ('"
<< flag << "')" << std::endl;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////
//
// Extract the expected output from the given input data
//
// The expected output has to be provided inside of special comments which
// start with a capital 'R'. All such comments are concatenated and returned
// through the parameter 'expected'.
//
///////////////////////////////////////////////////////////////////////////////
inline bool
testwave_app::extract_expected_output(std::string const& filename,
std::string const& instr, std::string& expected)
{
return extract_special_information(filename, instr, 'R', expected);
}
///////////////////////////////////////////////////////////////////////////////
//
// Extracts the required preprocessing options from the given input data and
// initialises the given Wave context object accordingly.
// We allow the same (applicable) options to be used as are valid for the wave
// driver executable.
//
///////////////////////////////////////////////////////////////////////////////
template <typename Context>
bool
testwave_app::extract_options(std::string const& filename,
std::string const& instr, Context& ctx, bool single_line)
{
if (9 == debuglevel) {
std::cerr << "extract_options: extracting options" << std::endl;
}
// extract the required information from the comments flagged by a
// capital 'O'
std::string options;
if (!extract_special_information(filename, instr, 'O', options))
return false;
try {
// parse the configuration information into a program_options_description
// object
po::variables_map local_vm;
cmd_line_utils::read_config_options(debuglevel, options, desc_options, local_vm);
initialise_options(ctx, local_vm, single_line);
}
catch (std::exception const &e) {
std::cerr << filename << ": exception caught: " << e.what()
<< std::endl;
return false;
}
if (9 == debuglevel) {
std::cerr << "extract_options: succeeded extracting options"
<< std::endl;
}
return true;
}
namespace {
template <typename T>
inline T const&
variables_map_as(po::variable_value const& v, T*)
{
#if (__GNUC__ == 3 && (__GNUC_MINOR__ == 2 || __GNUC_MINOR__ == 3)) || \
BOOST_WORKAROUND(__MWERKS__, < 0x3200)
// gcc 3.2.x and 3.3.x choke on vm[...].as<...>()
// CW 8.3 has problems with the v.as<T>() below
T const* r = boost::any_cast<T>(&v.value());
if (!r)
boost::throw_exception(boost::bad_any_cast());
return *r;
#else
return v.as<T>();
#endif
}
}
template <typename Context>
bool
testwave_app::initialise_options(Context& ctx, po::variables_map const& vm,
bool single_line)
{
if (9 == debuglevel) {
std::cerr << "initialise_options: initializing options" << std::endl;
}
// initialize the given context from the parsed options
#if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
// enable C99 mode, if appropriate (implies variadics)
if (vm.count("c99")) {
if (9 == debuglevel) {
std::cerr << "initialise_options: option: c99" << std::endl;
}
ctx.set_language(
boost::wave::language_support(
boost::wave::support_c99
| boost::wave::support_option_emit_line_directives
#if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0
| boost::wave::support_option_include_guard_detection
#endif
));
}
else if (vm.count("variadics")) {
// enable variadics and placemarkers, if appropriate
if (9 == debuglevel) {
std::cerr << "initialise_options: option: variadics" << std::endl;
}
ctx.set_language(boost::wave::enable_variadics(ctx.get_language()));
}
#endif // BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
// enable long_long mode, if appropriate
if (vm.count("long_long")) {
if (9 == debuglevel) {
std::cerr << "initialise_options: option: long_long" << std::endl;
}
ctx.set_language(boost::wave::enable_long_long(ctx.get_language()));
}
// enable preserving comments mode, if appropriate
if (vm.count("preserve")) {
if (9 == debuglevel) {
std::cerr << "initialise_options: option: preserve" << std::endl;
}
ctx.set_language(
boost::wave::enable_preserve_comments(ctx.get_language()));
}
// disable automatic include guard detection
if (vm.count("noguard")) {
if (9 == debuglevel) {
std::cerr << "initialise_options: option: guard" << std::endl;
}
ctx.set_language(
boost::wave::enable_include_guard_detection(ctx.get_language(), false));
}
// enable trigraph conversion
if (9 == debuglevel) {
std::cerr << "initialise_options: option: convert_trigraphs" << std::endl;
}
ctx.set_language(boost::wave::enable_convert_trigraphs(ctx.get_language()));
// enable single_line mode
if (single_line) {
if (9 == debuglevel) {
std::cerr << "initialise_options: option: single_line" << std::endl;
}
ctx.set_language(boost::wave::enable_single_line(ctx.get_language()));
}
// add include directories to the system include search paths
if (vm.count("sysinclude")) {
std::vector<std::string> const& syspaths =
variables_map_as(vm["sysinclude"], (std::vector<std::string> *)NULL);
std::vector<std::string>::const_iterator end = syspaths.end();
for (std::vector<std::string>::const_iterator cit = syspaths.begin();
cit != end; ++cit)
{
if (9 == debuglevel) {
std::cerr << "initialise_options: option: -S" << *cit
<< std::endl;
}
ctx.add_sysinclude_path((*cit).c_str());
}
}
// add include directories to the user include search paths
if (vm.count("include")) {
cmd_line_utils::include_paths const &ip =
variables_map_as(vm["include"], (cmd_line_utils::include_paths*)NULL);
std::vector<std::string>::const_iterator end = ip.paths.end();
for (std::vector<std::string>::const_iterator cit = ip.paths.begin();
cit != end; ++cit)
{
if (9 == debuglevel) {
std::cerr << "initialise_options: option: -I" << *cit
<< std::endl;
}
ctx.add_include_path((*cit).c_str());
}
// if on the command line was given -I- , this has to be propagated
if (ip.seen_separator) {
if (9 == debuglevel) {
std::cerr << "initialise_options: option: -I-" << std::endl;
}
ctx.set_sysinclude_delimiter();
}
// add system include directories to the include path
std::vector<std::string>::const_iterator sysend = ip.syspaths.end();
for (std::vector<std::string>::const_iterator syscit = ip.syspaths.begin();
syscit != sysend; ++syscit)
{
if (9 == debuglevel) {
std::cerr << "initialise_options: option: -S" << *syscit
<< std::endl;
}
ctx.add_sysinclude_path((*syscit).c_str());
}
}
// add additional defined macros
if (vm.count("define")) {
std::vector<std::string> const ¯os =
variables_map_as(vm["define"], (std::vector<std::string>*)NULL);
std::vector<std::string>::const_iterator end = macros.end();
for (std::vector<std::string>::const_iterator cit = macros.begin();
cit != end; ++cit)
{
if (9 == debuglevel) {
std::cerr << "initialise_options: option: -D" << *cit
<< std::endl;
}
ctx.add_macro_definition(*cit);
}
}
// add additional predefined macros
if (vm.count("predefine")) {
std::vector<std::string> const &predefmacros =
variables_map_as(vm["predefine"], (std::vector<std::string>*)NULL);
std::vector<std::string>::const_iterator end = predefmacros.end();
for (std::vector<std::string>::const_iterator cit = predefmacros.begin();
cit != end; ++cit)
{
if (9 == debuglevel) {
std::cerr << "initialise_options: option: -P" << *cit
<< std::endl;
}
ctx.add_macro_definition(*cit);
}
}
// undefine specified macros
if (vm.count("undefine")) {
std::vector<std::string> const &undefmacros =
variables_map_as(vm["undefine"], (std::vector<std::string>*)NULL);
std::vector<std::string>::const_iterator end = undefmacros.end();
for (std::vector<std::string>::const_iterator cit = undefmacros.begin();
cit != end; ++cit)
{
if (9 == debuglevel) {
std::cerr << "initialise_options: option: -U" << *cit
<< std::endl;
}
ctx.remove_macro_definition((*cit).c_str());
}
}
// maximal include nesting depth
if (vm.count("nesting")) {
int max_depth = variables_map_as(vm["nesting"], (int*)NULL);
if (max_depth < 1 || max_depth > 100000) {
std::cerr << "testwave: bogus maximal include nesting depth: "
<< max_depth << std::endl;
return false;
}
else if (9 == debuglevel) {
std::cerr << "initialise_options: option: -n" << max_depth
<< std::endl;
}
ctx.set_max_include_nesting_depth(max_depth);
}
if (9 == debuglevel) {
std::cerr << "initialise_options: succeeded to initialize options"
<< std::endl;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////
// construct a SIZEOF macro definition string and predefine this macro
template <typename Context>
inline bool
testwave_app::add_sizeof_definition(Context& ctx, char const *name, int value)
{
BOOST_WAVETEST_OSSTREAM strm;
strm << "__TESTWAVE_SIZEOF_" << name << "__=" << value;
std::string macro(BOOST_WAVETEST_GETSTRING(strm));
if (!ctx.add_macro_definition(macro)) {
std::cerr << "testwave: failed to predefine macro: " << macro
<< std::endl;
return false;
}
else if (9 == debuglevel) {
std::cerr << "add_sizeof_definition: predefined macro: " << macro
<< std::endl;
}
return true;
}
// construct a MIN macro definition string and predefine this macro
template <typename T, typename Context>
inline bool
testwave_app::add_min_definition(Context& ctx, char const *name)
{
BOOST_WAVETEST_OSSTREAM strm;
if (!std::numeric_limits<T>::is_signed) {
strm << "__TESTWAVE_" << name << "_MIN__="
<< "0x" << std::hex
<< (std::numeric_limits<T>::min)() << "U";
}
else {
strm << "__TESTWAVE_" << name << "_MIN__=( "
<< (std::numeric_limits<T>::min)()+1 << "-1)";
}
std::string macro(BOOST_WAVETEST_GETSTRING(strm));
if (!ctx.add_macro_definition(macro)) {
std::cerr << "testwave: failed to predefine macro: " << macro
<< std::endl;
return false;
}
else if (9 == debuglevel) {
std::cerr << "add_min_definition: predefined macro: " << macro
<< std::endl;
}
return true;
}
// construct a MAX macro definition string and predefine this macro
template <typename T, typename Context>
inline bool
testwave_app::add_max_definition(Context& ctx, char const *name)
{
BOOST_WAVETEST_OSSTREAM strm;
if (!std::numeric_limits<T>::is_signed) {
strm << "__TESTWAVE_" << name << "_MAX__="
<< "0x" << std::hex
<< (std::numeric_limits<T>::max)() << "U";
}
else {
strm << "__TESTWAVE_" << name << "_MAX__="
<< (std::numeric_limits<T>::max)();
}
std::string macro(BOOST_WAVETEST_GETSTRING(strm));
if (!ctx.add_macro_definition(macro)) {
std::cerr << "testwave: failed to predefine macro: " << macro
<< std::endl;
return false;
}
else if (9 == debuglevel) {
std::cerr << "add_max_definition: predefined macro: " << macro
<< std::endl;
}
return true;
}
// Predefine __TESTWAVE_HAS_STRICT_LEXER__
template <typename Context>
inline bool
testwave_app::add_strict_lexer_definition(Context& ctx)
{
std::string macro("__TESTWAVE_HAS_STRICT_LEXER__=1");
if (!ctx.add_macro_definition(macro)) {
std::cerr << "testwave: failed to predefine macro: " << macro
<< std::endl;
return false;
}
else if (9 == debuglevel) {
std::cerr << "add_strict_lexer_definition: predefined macro: " << macro
<< std::endl;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////
//
// Add special predefined macros to the context object.
//
// This adds a lot of macros to the test environment, which allows to adjust
// the test cases for different platforms.
//
///////////////////////////////////////////////////////////////////////////////
template <typename Context>
bool
testwave_app::add_predefined_macros(Context& ctx)
{
// add the __TESTWAVE_SIZEOF_<type>__ macros
if (!add_sizeof_definition(ctx, "CHAR", sizeof(char)) ||
!add_sizeof_definition(ctx, "SHORT", sizeof(short)) ||
!add_sizeof_definition(ctx, "INT", sizeof(int)) ||
#if defined(BOOST_HAS_LONG_LONG)
!add_sizeof_definition(ctx, "LONGLONG", sizeof(boost::long_long_type)) ||
#endif
!add_sizeof_definition(ctx, "LONG", sizeof(long)))
{
std::cerr << "testwave: failed to add a predefined macro (SIZEOF)."
<< std::endl;
return false;
}
// add the __TESTWAVE_<type>_MIN__ macros
if (/*!add_min_definition<char>(ctx, "CHAR") ||*/
/*!add_min_definition<unsigned char>(ctx, "UCHAR") ||*/
!add_min_definition<short>(ctx, "SHORT") ||
!add_min_definition<unsigned short>(ctx, "USHORT") ||
!add_min_definition<int>(ctx, "INT") ||
!add_min_definition<unsigned int>(ctx, "UINT") ||
#if defined(BOOST_HAS_LONG_LONG)
!add_min_definition<boost::long_long_type>(ctx, "LONGLONG") ||
!add_min_definition<boost::ulong_long_type>(ctx, "ULONGLONG") ||
#endif
!add_min_definition<long>(ctx, "LONG") ||
!add_min_definition<unsigned long>(ctx, "ULONG"))
{
std::cerr << "testwave: failed to add a predefined macro (MIN)."
<< std::endl;
}
// add the __TESTWAVE_<type>_MAX__ macros
if (/*!add_max_definition<char>(ctx, "CHAR") ||*/
/*!add_max_definition<unsigned char>(ctx, "UCHAR") ||*/
!add_max_definition<short>(ctx, "SHORT") ||
!add_max_definition<unsigned short>(ctx, "USHORT") ||
!add_max_definition<int>(ctx, "INT") ||
!add_max_definition<unsigned int>(ctx, "UINT") ||
#if defined(BOOST_HAS_LONG_LONG)
!add_max_definition<boost::long_long_type>(ctx, "LONGLONG") ||
!add_max_definition<boost::ulong_long_type>(ctx, "ULONGLONG") ||
#endif
!add_max_definition<long>(ctx, "LONG") ||
!add_max_definition<unsigned long>(ctx, "ULONG"))
{
std::cerr << "testwave: failed to add a predefined macro (MAX)."
<< std::endl;
}
#if BOOST_WAVE_USE_STRICT_LEXER != 0
return add_strict_lexer_definition(ctx);
#else
return true;
#endif
}
///////////////////////////////////////////////////////////////////////////////
//
// Preprocess the given input data and return the generated output through
// the parameter 'result'.
//
///////////////////////////////////////////////////////////////////////////////
bool
testwave_app::preprocess_file(std::string filename, std::string const& instr,
std::string& result, std::string& error, bool single_line)
{
// create the wave::context object and initialize it from the file to
// preprocess (may contain options inside of special comments)
typedef boost::wave::cpplexer::lex_token<> token_type;
typedef boost::wave::cpplexer::lex_iterator<token_type> lexer_type;
typedef boost::wave::context<std::string::const_iterator, lexer_type>
context_type;
if (9 == debuglevel) {
std::cerr << "preprocess_file: preprocessing input file: " << filename
<< std::endl;
}
try {
// create preprocessing context
context_type ctx(instr.begin(), instr.end(), filename.c_str());
// initialize the context from the options given on the command line
if (!initialise_options(ctx, global_vm, single_line))
return false;
// extract the options from the input data and initialize the context
if (!extract_options(filename, instr, ctx, single_line))
return false;
// add special predefined macros
if (!add_predefined_macros(ctx))
return false;
// preprocess the input, loop over all generated tokens collecting the
// generated text
context_type::iterator_type end = ctx.end();
for (context_type::iterator_type it = ctx.begin(); it != end; ++it)
{
using namespace boost::wave;
if (T_PP_LINE == token_id(*it)) {
// special handling of the whole #line directive is required to
// allow correct file name matching
if (!handle_line_directive(it, end, result))
return false; // unexpected eof
}
else {
// add the value of the current token
result = result + (*it).get_value().c_str();
}
}
error.clear();
}
catch (boost::wave::cpplexer::lexing_exception const& e) {
// some lexer error
BOOST_WAVETEST_OSSTREAM strm;
std::string filename = e.file_name();
strm
<< handle_filepath(filename) << "(" << e.line_no() << "): "
<< e.description() << std::endl;
error = BOOST_WAVETEST_GETSTRING(strm);
return false;
}
catch (boost::wave::cpp_exception const& e) {
// some preprocessing error
BOOST_WAVETEST_OSSTREAM strm;
std::string filename = e.file_name();
strm
<< handle_filepath(filename) << "(" << e.line_no() << "): "
<< e.description() << std::endl;
error = BOOST_WAVETEST_GETSTRING(strm);
return false;
}
if (9 == debuglevel) {
std::cerr << "preprocess_file: succeeded to preprocess input file: "
<< filename << std::endl;
}
return true;
}
|