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