cpp_exceptions.hpp 18 KB

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