testwave_app.cpp 56 KB

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