source: NonGTP/Boost/boost/wave/cpp_exceptions.hpp @ 857

Revision 857, 14.0 KB checked in by igarcia, 19 years ago (diff)
RevLine 
[857]1/*=============================================================================
2    Boost.Wave: A Standard compliant C++ preprocessor library
3
4    http://www.boost.org/
5
6    Copyright (c) 2001-2005 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
17#include <boost/assert.hpp>
18#include <boost/config.hpp>
19
20#include <boost/wave/wave_config.hpp>
21
22///////////////////////////////////////////////////////////////////////////////
23// helper macro for throwing exceptions
24#if !defined(BOOST_WAVE_THROW)
25#ifdef BOOST_NO_STRINGSTREAM
26#include <strstream>
27#define BOOST_WAVE_THROW(cls, code, msg, act_pos)                             \
28    {                                                                         \
29    using namespace boost::wave;                                              \
30    std::strstream stream;                                                    \
31        stream << cls::severity_text(cls::code) << ": "                       \
32        << cls::error_text(cls::code);                                        \
33    if ((msg)[0] != 0) stream << ": " << (msg);                               \
34    stream << std::ends;                                                      \
35    std::string throwmsg = stream.str(); stream.freeze(false);                \
36    throw cls(throwmsg.c_str(), cls::code, (act_pos).get_line(),              \
37        (act_pos).get_column(), (act_pos).get_file().c_str());                \
38    }                                                                         \
39    /**/
40#else
41#include <sstream>
42#define BOOST_WAVE_THROW(cls, code, msg, act_pos)                             \
43    {                                                                         \
44    using namespace boost::wave;                                              \
45    std::stringstream stream;                                                 \
46        stream << cls::severity_text(cls::code) << ": "                       \
47        << cls::error_text(cls::code);                                        \
48    if ((msg)[0] != 0) stream << ": " << (msg);                               \
49    stream << std::ends;                                                      \
50    throw cls(stream.str().c_str(), cls::code, (act_pos).get_line(),          \
51        (act_pos).get_column(), (act_pos).get_file().c_str());                \
52    }                                                                         \
53    /**/
54#endif // BOOST_NO_STRINGSTREAM
55#endif // BOOST_WAVE_THROW
56
57///////////////////////////////////////////////////////////////////////////////
58namespace boost {
59namespace wave {
60
61///////////////////////////////////////////////////////////////////////////////
62// exception severity
63namespace util {
64
65    enum severity {
66        severity_remark = 0,
67        severity_warning,
68        severity_error,
69        severity_fatal,
70        severity_commandline_error
71    };
72   
73    inline char const *
74    get_severity(severity level)
75    {
76        static char const *severity_text[] =
77        {
78            "remark",           // severity_remark
79            "warning",          // severity_warning
80            "error",            // severity_error
81            "fatal error",      // severity_fatal
82            "command line error"    // severity_commandline_error
83        };
84        BOOST_ASSERT(severity_remark <= level &&
85            level <= severity_commandline_error);
86        return severity_text[level];
87    }
88}
89
90///////////////////////////////////////////////////////////////////////////////
91//  cpp_exception, the base class for all specific C preprocessor exceptions
92class cpp_exception
93:   public std::exception
94{
95public:
96    cpp_exception(int line_, int column_, char const *filename_) throw()
97    :   line(line_), column(column_)
98    {
99        unsigned int off = 0;
100        while (off < sizeof(filename) && *filename_)
101            filename[off++] = *filename_++;
102        filename[off] = 0;
103    }
104    ~cpp_exception() throw() {}
105   
106    virtual char const *what() const throw() = 0;   // to be overloaded
107    virtual char const *description() const throw() = 0;
108   
109    int line_no() const throw() { return line; }
110    int column_no() const throw() { return column; }
111    char const *file_name() const throw() { return filename; }
112   
113protected:
114    char filename[512];
115    int line;
116    int column;
117};
118
119///////////////////////////////////////////////////////////////////////////////
120// preprocessor error
121class preprocess_exception :
122    public cpp_exception
123{
124public:
125    enum error_code {
126        unexpected_error = 0,
127        macro_redefinition,
128        macro_insertion_error,
129        bad_include_file,
130        bad_include_statement,
131        ill_formed_directive,
132        error_directive,
133        warning_directive,
134        ill_formed_expression,
135        missing_matching_if,
136        missing_matching_endif,
137        ill_formed_operator,
138        bad_define_statement,
139        too_few_macroarguments,
140        too_many_macroarguments,
141        empty_macroarguments,
142        improperly_terminated_macro,
143        bad_line_statement,
144        bad_undefine_statement,
145        bad_macro_definition,
146        illegal_redefinition,
147        duplicate_parameter_name,
148        invalid_concat,
149        last_line_not_terminated,
150        ill_formed_pragma_option,
151        include_nesting_too_deep,
152        misplaced_operator,
153        alreadydefined_name,
154        undefined_macroname,
155        invalid_macroname,
156        unexpected_qualified_name,
157        division_by_zero,
158        integer_overflow,
159        illegal_operator_redefinition,
160        ill_formed_integer_literal,
161        ill_formed_character_literal,
162        unbalanced_if_endif,
163        character_literal_out_of_range
164    };
165
166    preprocess_exception(char const *what_, error_code code, int line_,
167        int column_, char const *filename_) throw()
168    :   cpp_exception(line_, column_, filename_), level(severity_level(code))
169    {
170        unsigned int off = 0;
171        while (off < sizeof(buffer) && *what_)
172            buffer[off++] = *what_++;
173        buffer[off] = 0;
174    }
175    ~preprocess_exception() throw() {}
176   
177    virtual char const *what() const throw()
178    {
179        return "boost::wave::preprocess_exception";
180    }
181    virtual char const *description() const throw()
182    {
183        return buffer;
184    }
185    util::severity get_severity()
186    {
187        return level;
188    }
189
190    static char const *error_text(int code)
191    {
192    // error texts in this array must appear in the same order as the items in
193    // the error enum above
194        static char const *preprocess_exception_errors[] = {
195            "unexpected error (should not happen)",     // unexpected_error
196            "illegal macro redefinition",               // macro_redefinition
197            "macro definition failed (out of memory?)", // macro_insertion_error
198            "could not find include file",              // bad_include_file
199            "ill formed #include directive",            // bad_include_statement
200            "ill formed preprocessor directive",        // ill_formed_directive
201            "encountered #error directive or #pragma wave stop()", // error_directive
202            "encountered #warning directive",           // warning_directive
203            "ill formed preprocessor expression",       // ill_formed_expression
204            "the #if for this directive is missing",    // missing_matching_if
205            "detected at least one missing #endif directive",   // missing_matching_endif
206            "ill formed preprocessing operator",        // ill_formed_operator
207            "ill formed #define directive",             // bad_define_statement
208            "too few macro arguments",                  // too_few_macroarguments
209            "too many macro arguments",                 // too_many_macroarguments
210            "empty macro arguments are not supported in pure C++ mode, "
211            "use variadics mode to allow these",        // empty_macroarguments
212            "improperly terminated macro invocation "
213            "or replacement-list terminates in partial "
214            "macro expansion (not supported yet)",      // improperly_terminated_macro
215            "ill formed #line directive",               // bad_line_statement
216            "#undef may not be used on this predefined name",   // bad_undefine_statement
217            "invalid macro definition",                 // bad_macro_definition
218            "this predefined name may not be redefined",    // illegal_redefinition
219            "duplicate macro parameter name",           // duplicate_parameter_name
220            "pasting the following two tokens does not "
221            "give a valid preprocessing token",         // invalid_concat
222            "last line of file ends without a newline", // last_line_not_terminated
223            "unknown or illformed pragma option",       // ill_formed_pragma_option
224            "include files nested too deep",            // include_nesting_too_deep
225            "misplaced operator defined()",             // misplaced_operator
226            "the name is already used in this scope as "
227            "a macro or scope name",                    // alreadydefined_name
228            "undefined macro or scope name may not be imported", // undefined_macroname
229            "ill formed macro name",                    // invalid_macroname
230            "qualified names are supported in C++0x mode only",  // unexpected_qualified_name
231            "division by zero in preprocessor expression",       // division_by_zero
232            "integer overflow in preprocessor expression",       // integer_overflow
233            "this macro name cannot be used as a as it is an operator in C++",  // illegal_operator_redefinition
234            "ill formed integer literal or integer constant too large",   // ill_formed_integer_literal
235            "ill formed character literal",             // ill_formed_character_literal
236            "unbalanced #if/#endif in include file",    // unbalanced_if_endif
237            "character literal out of range"            // character_literal_out_of_range
238        };
239        BOOST_ASSERT(unexpected_error <= code &&
240            code <= character_literal_out_of_range);
241        return preprocess_exception_errors[code];
242    }
243
244    static util::severity severity_level(int code)
245    {
246        static util::severity preprocess_exception_severity[] = {
247            util::severity_fatal,              // unexpected_error
248            util::severity_warning,            // macro_redefinition
249            util::severity_fatal,              // macro_insertion_error
250            util::severity_error,              // bad_include_file
251            util::severity_error,              // bad_include_statement
252            util::severity_error,              // ill_formed_directive
253            util::severity_fatal,              // error_directive
254            util::severity_warning,            // warning_directive
255            util::severity_error,              // ill_formed_expression
256            util::severity_error,              // missing_matching_if
257            util::severity_error,              // missing_matching_endif
258            util::severity_error,              // ill_formed_operator
259            util::severity_error,              // bad_define_statement
260            util::severity_warning,            // too_few_macroarguments
261            util::severity_warning,            // too_many_macroarguments
262            util::severity_warning,            // empty_macroarguments
263            util::severity_error,              // improperly_terminated_macro
264            util::severity_warning,            // bad_line_statement
265            util::severity_warning,            // bad_undefine_statement
266            util::severity_commandline_error,  // bad_macro_definition
267            util::severity_warning,            // illegal_redefinition
268            util::severity_error,              // duplicate_parameter_name
269            util::severity_error,              // invalid_concat
270            util::severity_warning,            // last_line_not_terminated
271            util::severity_warning,            // ill_formed_pragma_option
272            util::severity_fatal,              // include_nesting_too_deep
273            util::severity_error,              // misplaced_operator
274            util::severity_error,              // alreadydefined_name
275            util::severity_error,              // undefined_macroname
276            util::severity_error,              // invalid_macroname
277            util::severity_error,              // unexpected_qualified_name
278            util::severity_fatal,              // division_by_zero
279            util::severity_error,              // integer_overflow
280            util::severity_error,              // illegal_operator_redefinition
281            util::severity_error,              // ill_formed_integer_literal
282            util::severity_error,              // ill_formed_character_literal
283            util::severity_warning,            // unbalanced_if_endif
284            util::severity_warning             // character_literal_out_of_range
285        };
286        BOOST_ASSERT(unexpected_error <= code &&
287            code <= character_literal_out_of_range);
288        return preprocess_exception_severity[code];
289    }
290    static char const *severity_text(int code)
291    {
292        return util::get_severity(severity_level(code));
293    }
294
295private:
296    char buffer[512];
297    util::severity level;
298};
299
300///////////////////////////////////////////////////////////////////////////////
301}   // namespace wave
302}   // namespace boost
303
304#endif // !defined(CPP_EXCEPTIONS_HPP_5190E447_A781_4521_A275_5134FF9917D7_INCLUDED)
Note: See TracBrowser for help on using the repository browser.