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