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