File: default_hooks.cpp

package info (click to toggle)
boost1.90 1.90.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 593,168 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,162; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (137 lines) | stat: -rw-r--r-- 3,975 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

// Copyright 2018 Peter Dimov.
// Copyrigth 2022 Jeff Trull.
//
// Distributed under the Boost Software License, Version 1.0.
//
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt

// Most test coverage of Wave happens with the "eat_whitespace" hooks in effect
// This adds some coverage for Wave with the (minimal) default hooks

#include <boost/wave.hpp>
#include <boost/wave/token_ids.hpp>
#include <boost/wave/cpplexer/cpp_lex_token.hpp>
#include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
#include <iostream>
#include <vector>
#include <utility>
#include <string>

int main()
{
    std::string input(
        // check LINE directives after conditional (false) sections 
        "#if 0\n"
        "int aa;\n"
        "#endif\n"
        "char c1;\n"     // #LINE 4

        "#ifdef FOO\n"
        "int bb;\n"
        "#endif\n"
        "char c2;\n"     // #LINE 8

        "#ifndef __FILE__\n"
        "int cc;\n"
        "#endif\n"
        "char c3;\n"     // #LINE 12
    );

    using namespace boost::wave;

    typedef std::pair<token_id, char const*> tokdata_type;
    typedef std::vector<tokdata_type> tokdata_list_type;
    tokdata_list_type expected{
        {T_PP_LINE, "#line"},
        {T_SPACE, " "},
        {T_INTLIT, "4"},
        {T_SPACE, " "},
        {T_STRINGLIT, nullptr},  // exact filepath is not interesting
        {T_NEWLINE, "\n"},
        {T_CHAR, "char"},
        {T_SPACE, " "},
        {T_IDENTIFIER, "c1"},
        {T_SEMICOLON, ";"},
        {T_NEWLINE, "\n"},

        {T_PP_LINE, "#line"},
        {T_SPACE, " "},
        {T_INTLIT, "8"},
        {T_SPACE, " "},
        {T_STRINGLIT, nullptr},
        {T_NEWLINE, "\n"},
        {T_CHAR, "char"},
        {T_SPACE, " "},
        {T_IDENTIFIER, "c2"},
        {T_SEMICOLON, ";"},
        {T_NEWLINE, "\n"},

        {T_PP_LINE, "#line"},
        {T_SPACE, " "},
        {T_INTLIT, "12"},
        {T_SPACE, " "},
        {T_STRINGLIT, nullptr},
        {T_NEWLINE, "\n"},
        {T_CHAR, "char"},
        {T_SPACE, " "},
        {T_IDENTIFIER, "c3"},
        {T_SEMICOLON, ";"},
        {T_NEWLINE, "\n"},

        {T_EOF, nullptr}
};

    try
    {
        typedef cpplexer::lex_token<> token_type;
        typedef cpplexer::lex_iterator<token_type> lex_iterator_type;
        typedef context<std::string::iterator, lex_iterator_type> context_type;

        context_type ctx( input.begin(), input.end(), "input.cpp" );

        tokdata_list_type::const_iterator first_expect = expected.begin();
        tokdata_list_type::const_iterator last_expect = expected.end();
        for( context_type::iterator_type first = ctx.begin(), last = ctx.end(); first != last; ++first, ++first_expect )
        {
            if (first_expect == last_expect)
            {
                std::cerr << "more tokens were produced than expected\n";
                return 3;
            }

            if (token_id(*first) != first_expect->first)
            {
                std::cerr << "expected " << get_token_name(first_expect->first) << ", got " << get_token_name(token_id(*first)) << "\n";
                return 3;
            }

            if ((first_expect->second != nullptr) && (first_expect->second != first->get_value()))
            {
                std::cerr << "expected token value " << first_expect->second << ", got " << first->get_value() << "\n";
                return 3;
            }
                
            std::cout << first->get_value();
        }

        if (first_expect != last_expect)
        {
            std::cerr << "fewer tokens were produced than expected\n";
            return 3;
        }

        return 0;
    }
    catch( cpp_exception const & x )
    {
        std::cerr << x.file_name() << "(" << x.line_no() << "): " << x.description() << std::endl;
        return 1;
    }
    catch( std::exception const & x )
    {
        std::cerr << "Exception: " << x.what() << std::endl;
        return 2;
    }
}