cpp_exceptions.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. http://www.boost.org/
  4. Copyright (c) 2001-2012 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. #if !defined(BOOST_CPP_EXCEPTIONS_HPP_5190E447_A781_4521_A275_5134FF9917D7_INCLUDED)
  9. #define BOOST_CPP_EXCEPTIONS_HPP_5190E447_A781_4521_A275_5134FF9917D7_INCLUDED
  10. #include <exception>
  11. #include <string>
  12. #include <limits>
  13. #include <boost/assert.hpp>
  14. #include <boost/config.hpp>
  15. #include <boost/throw_exception.hpp>
  16. #include <boost/wave/wave_config.hpp>
  17. #include <boost/wave/cpp_throw.hpp>
  18. // this must occur after all of the includes and before any code appears
  19. #ifdef BOOST_HAS_ABI_HEADERS
  20. #include BOOST_ABI_PREFIX
  21. #endif
  22. ///////////////////////////////////////////////////////////////////////////////
  23. namespace boost {
  24. namespace wave {
  25. ///////////////////////////////////////////////////////////////////////////////
  26. // exception severity
  27. namespace util {
  28. enum severity {
  29. severity_remark = 0,
  30. severity_warning,
  31. severity_error,
  32. severity_fatal,
  33. severity_commandline_error,
  34. last_severity_code = severity_commandline_error
  35. };
  36. inline char const *
  37. get_severity(int level)
  38. {
  39. static char const *severity_text[] =
  40. {
  41. "remark", // severity_remark
  42. "warning", // severity_warning
  43. "error", // severity_error
  44. "fatal error", // severity_fatal
  45. "command line error" // severity_commandline_error
  46. };
  47. BOOST_ASSERT(severity_remark <= level &&
  48. level <= last_severity_code);
  49. return severity_text[level];
  50. }
  51. }
  52. ///////////////////////////////////////////////////////////////////////////////
  53. // cpp_exception, the base class for all specific C preprocessor exceptions
  54. class BOOST_SYMBOL_VISIBLE cpp_exception
  55. : public std::exception
  56. {
  57. public:
  58. cpp_exception(std::size_t line_, std::size_t column_, char const *filename_) throw()
  59. : line(line_), column(column_)
  60. {
  61. unsigned int off = 0;
  62. while (off < sizeof(filename)-1 && *filename_)
  63. filename[off++] = *filename_++;
  64. filename[off] = 0;
  65. }
  66. ~cpp_exception() throw() {}
  67. char const *what() const throw() BOOST_OVERRIDE = 0; // to be overloaded
  68. virtual char const *description() const throw() = 0;
  69. virtual int get_errorcode() const throw() = 0;
  70. virtual int get_severity() const throw() = 0;
  71. virtual char const* get_related_name() const throw() = 0;
  72. virtual bool is_recoverable() const throw() = 0;
  73. std::size_t line_no() const throw() { return line; }
  74. std::size_t column_no() const throw() { return column; }
  75. char const *file_name() const throw() { return filename; }
  76. protected:
  77. char filename[512];
  78. std::size_t line;
  79. std::size_t column;
  80. };
  81. ///////////////////////////////////////////////////////////////////////////////
  82. // preprocessor error
  83. class BOOST_SYMBOL_VISIBLE preprocess_exception :
  84. public cpp_exception
  85. {
  86. public:
  87. enum error_code {
  88. no_error = 0,
  89. unexpected_error,
  90. macro_redefinition,
  91. macro_insertion_error,
  92. bad_include_file,
  93. bad_include_statement,
  94. bad_has_include_expression,
  95. ill_formed_directive,
  96. error_directive,
  97. warning_directive,
  98. ill_formed_expression,
  99. missing_matching_if,
  100. missing_matching_endif,
  101. ill_formed_operator,
  102. bad_define_statement,
  103. bad_define_statement_va_args,
  104. bad_define_statement_va_opt,
  105. bad_define_statement_va_opt_parens,
  106. bad_define_statement_va_opt_recurse,
  107. too_few_macroarguments,
  108. too_many_macroarguments,
  109. empty_macroarguments,
  110. improperly_terminated_macro,
  111. bad_line_statement,
  112. bad_line_number,
  113. bad_line_filename,
  114. bad_undefine_statement,
  115. bad_macro_definition,
  116. illegal_redefinition,
  117. duplicate_parameter_name,
  118. invalid_concat,
  119. last_line_not_terminated,
  120. ill_formed_pragma_option,
  121. include_nesting_too_deep,
  122. misplaced_operator,
  123. alreadydefined_name,
  124. undefined_macroname,
  125. invalid_macroname,
  126. unexpected_qualified_name,
  127. division_by_zero,
  128. integer_overflow,
  129. illegal_operator_redefinition,
  130. ill_formed_integer_literal,
  131. ill_formed_character_literal,
  132. unbalanced_if_endif,
  133. character_literal_out_of_range,
  134. could_not_open_output_file,
  135. incompatible_config,
  136. ill_formed_pragma_message,
  137. pragma_message_directive,
  138. last_error_number = pragma_message_directive
  139. };
  140. preprocess_exception(char const *what_, error_code code, std::size_t line_,
  141. std::size_t column_, char const *filename_) throw()
  142. : cpp_exception(line_, column_, filename_),
  143. code(code)
  144. {
  145. unsigned int off = 0;
  146. while (off < sizeof(buffer) - 1 && *what_)
  147. buffer[off++] = *what_++;
  148. buffer[off] = 0;
  149. }
  150. ~preprocess_exception() throw() {}
  151. char const *what() const throw() BOOST_OVERRIDE
  152. {
  153. return "boost::wave::preprocess_exception";
  154. }
  155. char const *description() const throw() BOOST_OVERRIDE
  156. {
  157. return buffer;
  158. }
  159. int get_severity() const throw() BOOST_OVERRIDE
  160. {
  161. return severity_level(code);
  162. }
  163. int get_errorcode() const throw() BOOST_OVERRIDE
  164. {
  165. return code;
  166. }
  167. char const* get_related_name() const throw() BOOST_OVERRIDE
  168. {
  169. return "<unknown>";
  170. }
  171. bool is_recoverable() const throw() BOOST_OVERRIDE
  172. {
  173. switch (get_errorcode()) {
  174. // these are the exceptions thrown during processing not supposed to
  175. // produce any tokens on the context::iterator level
  176. case preprocess_exception::no_error: // just a placeholder
  177. case preprocess_exception::macro_redefinition:
  178. case preprocess_exception::macro_insertion_error:
  179. case preprocess_exception::bad_macro_definition:
  180. case preprocess_exception::illegal_redefinition:
  181. case preprocess_exception::duplicate_parameter_name:
  182. case preprocess_exception::invalid_macroname:
  183. case preprocess_exception::bad_include_file:
  184. case preprocess_exception::bad_include_statement:
  185. case preprocess_exception::bad_has_include_expression:
  186. case preprocess_exception::ill_formed_directive:
  187. case preprocess_exception::error_directive:
  188. case preprocess_exception::warning_directive:
  189. case preprocess_exception::ill_formed_expression:
  190. case preprocess_exception::missing_matching_if:
  191. case preprocess_exception::missing_matching_endif:
  192. case preprocess_exception::unbalanced_if_endif:
  193. case preprocess_exception::bad_define_statement:
  194. case preprocess_exception::bad_define_statement_va_args:
  195. case preprocess_exception::bad_define_statement_va_opt:
  196. case preprocess_exception::bad_define_statement_va_opt_parens:
  197. case preprocess_exception::bad_define_statement_va_opt_recurse:
  198. case preprocess_exception::bad_line_statement:
  199. case preprocess_exception::bad_line_number:
  200. case preprocess_exception::bad_line_filename:
  201. case preprocess_exception::bad_undefine_statement:
  202. case preprocess_exception::division_by_zero:
  203. case preprocess_exception::integer_overflow:
  204. case preprocess_exception::ill_formed_integer_literal:
  205. case preprocess_exception::ill_formed_character_literal:
  206. case preprocess_exception::character_literal_out_of_range:
  207. case preprocess_exception::last_line_not_terminated:
  208. case preprocess_exception::include_nesting_too_deep:
  209. case preprocess_exception::illegal_operator_redefinition:
  210. case preprocess_exception::incompatible_config:
  211. case preprocess_exception::ill_formed_pragma_option:
  212. case preprocess_exception::ill_formed_pragma_message:
  213. case preprocess_exception::pragma_message_directive:
  214. return true;
  215. case preprocess_exception::unexpected_error:
  216. case preprocess_exception::ill_formed_operator:
  217. case preprocess_exception::too_few_macroarguments:
  218. case preprocess_exception::too_many_macroarguments:
  219. case preprocess_exception::empty_macroarguments:
  220. case preprocess_exception::improperly_terminated_macro:
  221. case preprocess_exception::invalid_concat:
  222. case preprocess_exception::could_not_open_output_file:
  223. break;
  224. }
  225. return false;
  226. }
  227. static char const *error_text(int code)
  228. {
  229. // error texts in this array must appear in the same order as the items in
  230. // the error enum above
  231. static char const *preprocess_exception_errors[] = {
  232. "no error", // no_error
  233. "unexpected error (should not happen)", // unexpected_error
  234. "illegal macro redefinition", // macro_redefinition
  235. "macro definition failed (out of memory?)", // macro_insertion_error
  236. "could not find include file", // bad_include_file
  237. "ill formed #include directive", // bad_include_statement
  238. "ill formed __has_include expression", // bad_has_include_expression
  239. "ill formed preprocessor directive", // ill_formed_directive
  240. "encountered #error directive or #pragma wave stop()", // error_directive
  241. "encountered #warning directive", // warning_directive
  242. "ill formed preprocessor expression", // ill_formed_expression
  243. "the #if for this directive is missing", // missing_matching_if
  244. "detected at least one missing #endif directive", // missing_matching_endif
  245. "ill formed preprocessing operator", // ill_formed_operator
  246. "ill formed #define directive", // bad_define_statement
  247. "__VA_ARGS__ can only appear in the "
  248. "expansion of a C99 variadic macro", // bad_define_statement_va_args
  249. "__VA_OPT__ can only appear in the "
  250. "expansion of a C++20 variadic macro", // bad_define_statement_va_opt
  251. "__VA_OPT__ must be followed by a left "
  252. "paren in a C++20 variadic macro", // bad_define_statement_va_opt_parens
  253. "__VA_OPT__() may not contain __VA_OPT__", // bad_define_statement_va_opt_recurse
  254. "too few macro arguments", // too_few_macroarguments
  255. "too many macro arguments", // too_many_macroarguments
  256. "empty macro arguments are not supported in pure C++ mode, "
  257. "use variadics mode to allow these", // empty_macroarguments
  258. "improperly terminated macro invocation "
  259. "or replacement-list terminates in partial "
  260. "macro expansion (not supported yet)", // improperly_terminated_macro
  261. "ill formed #line directive", // bad_line_statement
  262. "line number argument of #line directive "
  263. "should consist out of decimal digits "
  264. "only and must be in range of [1..INT_MAX]", // bad_line_number
  265. "filename argument of #line directive should "
  266. "be a narrow string literal", // bad_line_filename
  267. "#undef may not be used on this predefined name", // bad_undefine_statement
  268. "invalid macro definition", // bad_macro_definition
  269. "this predefined name may not be redefined", // illegal_redefinition
  270. "duplicate macro parameter name", // duplicate_parameter_name
  271. "pasting the following two tokens does not "
  272. "give a valid preprocessing token", // invalid_concat
  273. "last line of file ends without a newline", // last_line_not_terminated
  274. "unknown or illformed pragma option", // ill_formed_pragma_option
  275. "include files nested too deep", // include_nesting_too_deep
  276. "misplaced operator defined()", // misplaced_operator
  277. "the name is already used in this scope as "
  278. "a macro or scope name", // alreadydefined_name
  279. "undefined macro or scope name may not be imported", // undefined_macroname
  280. "ill formed macro name", // invalid_macroname
  281. "qualified names are supported in C++11 mode only", // unexpected_qualified_name
  282. "division by zero in preprocessor expression", // division_by_zero
  283. "integer overflow in preprocessor expression", // integer_overflow
  284. "this cannot be used as a macro name as it is "
  285. "an operator in C++", // illegal_operator_redefinition
  286. "ill formed integer literal or integer constant too large", // ill_formed_integer_literal
  287. "ill formed character literal", // ill_formed_character_literal
  288. "unbalanced #if/#endif in include file", // unbalanced_if_endif
  289. "expression contains out of range character literal", // character_literal_out_of_range
  290. "could not open output file", // could_not_open_output_file
  291. "incompatible state information", // incompatible_config
  292. "illformed pragma message", // ill_formed_pragma_message
  293. "encountered #pragma message directive" // pragma_message_directive
  294. };
  295. BOOST_ASSERT(no_error <= code && code <= last_error_number);
  296. return preprocess_exception_errors[code];
  297. }
  298. static util::severity severity_level(int code)
  299. {
  300. static util::severity preprocess_exception_severity[] = {
  301. util::severity_remark, // no_error
  302. util::severity_fatal, // unexpected_error
  303. util::severity_warning, // macro_redefinition
  304. util::severity_fatal, // macro_insertion_error
  305. util::severity_error, // bad_include_file
  306. util::severity_error, // bad_include_statement
  307. util::severity_error, // bad_has_include_expression
  308. util::severity_error, // ill_formed_directive
  309. util::severity_fatal, // error_directive
  310. util::severity_warning, // warning_directive
  311. util::severity_error, // ill_formed_expression
  312. util::severity_error, // missing_matching_if
  313. util::severity_error, // missing_matching_endif
  314. util::severity_error, // ill_formed_operator
  315. util::severity_error, // bad_define_statement
  316. util::severity_error, // bad_define_statement_va_args
  317. util::severity_error, // bad_define_statement_va_opt
  318. util::severity_error, // bad_define_statement_va_opt_parens
  319. util::severity_error, // bad_define_statement_va_opt_recurse
  320. util::severity_warning, // too_few_macroarguments
  321. util::severity_warning, // too_many_macroarguments
  322. util::severity_warning, // empty_macroarguments
  323. util::severity_error, // improperly_terminated_macro
  324. util::severity_warning, // bad_line_statement
  325. util::severity_warning, // bad_line_number
  326. util::severity_warning, // bad_line_filename
  327. util::severity_warning, // bad_undefine_statement
  328. util::severity_commandline_error, // bad_macro_definition
  329. util::severity_warning, // illegal_redefinition
  330. util::severity_error, // duplicate_parameter_name
  331. util::severity_error, // invalid_concat
  332. util::severity_warning, // last_line_not_terminated
  333. util::severity_warning, // ill_formed_pragma_option
  334. util::severity_fatal, // include_nesting_too_deep
  335. util::severity_error, // misplaced_operator
  336. util::severity_error, // alreadydefined_name
  337. util::severity_error, // undefined_macroname
  338. util::severity_error, // invalid_macroname
  339. util::severity_error, // unexpected_qualified_name
  340. util::severity_fatal, // division_by_zero
  341. util::severity_error, // integer_overflow
  342. util::severity_error, // illegal_operator_redefinition
  343. util::severity_error, // ill_formed_integer_literal
  344. util::severity_error, // ill_formed_character_literal
  345. util::severity_warning, // unbalanced_if_endif
  346. util::severity_warning, // character_literal_out_of_range
  347. util::severity_error, // could_not_open_output_file
  348. util::severity_remark, // incompatible_config
  349. util::severity_warning, // ill_formed_pragma_message
  350. util::severity_remark, // pragma_message_directive
  351. };
  352. BOOST_ASSERT(no_error <= code && code <= last_error_number);
  353. return preprocess_exception_severity[code];
  354. }
  355. static char const *severity_text(int code)
  356. {
  357. return util::get_severity(severity_level(code));
  358. }
  359. private:
  360. char buffer[512];
  361. error_code code;
  362. };
  363. ///////////////////////////////////////////////////////////////////////////////
  364. // Error during macro handling, this exception contains the related macro name
  365. class BOOST_SYMBOL_VISIBLE macro_handling_exception :
  366. public preprocess_exception
  367. {
  368. public:
  369. macro_handling_exception(char const *what_, error_code code, std::size_t line_,
  370. std::size_t column_, char const *filename_, char const *macroname) throw()
  371. : preprocess_exception(what_, code, line_, column_, filename_)
  372. {
  373. unsigned int off = 0;
  374. while (off < sizeof(name) && *macroname)
  375. name[off++] = *macroname++;
  376. name[off] = 0;
  377. }
  378. ~macro_handling_exception() throw() {}
  379. char const *what() const throw() BOOST_OVERRIDE
  380. {
  381. return "boost::wave::macro_handling_exception";
  382. }
  383. char const* get_related_name() const throw() BOOST_OVERRIDE
  384. {
  385. return name;
  386. }
  387. private:
  388. char name[512];
  389. };
  390. ///////////////////////////////////////////////////////////////////////////////
  391. //
  392. // The is_recoverable() function allows to decide, whether it is possible
  393. // simply to continue after a given exception was thrown by Wave.
  394. //
  395. // This is kind of a hack to allow to recover from certain errors as long as
  396. // Wave doesn't provide better means of error recovery.
  397. //
  398. ///////////////////////////////////////////////////////////////////////////////
  399. inline bool
  400. is_recoverable(cpp_exception const& e)
  401. {
  402. return e.is_recoverable();
  403. }
  404. ///////////////////////////////////////////////////////////////////////////////
  405. } // namespace wave
  406. } // namespace boost
  407. // the suffix header occurs after all of the code
  408. #ifdef BOOST_HAS_ABI_HEADERS
  409. #include BOOST_ABI_SUFFIX
  410. #endif
  411. #endif // !defined(BOOST_CPP_EXCEPTIONS_HPP_5190E447_A781_4521_A275_5134FF9917D7_INCLUDED)