1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
3 Sample demonstrating the usage of advanced preprocessor hooks.
7 Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
8 Software License, Version 1.0. (See accompanying file
9 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 =============================================================================*/
17 ///////////////////////////////////////////////////////////////////////////////
18 // Include Wave itself
19 #include <boost/wave.hpp>
21 ///////////////////////////////////////////////////////////////////////////////
22 // Include the lexer stuff
23 #include <boost/wave/cpplexer/cpp_lex_token.hpp> // token class
24 #include <boost/wave/cpplexer/cpp_lex_iterator.hpp> // lexer class
26 #include "emit_custom_line_directives.hpp"
28 ///////////////////////////////////////////////////////////////////////////////
31 // This sample shows how to use the emit_line_directive preprocessing hooks
32 // to customize the format of any generated #line directive. The sample will
33 // emit #line directives formatted compatible with those generated by gcc:
35 // # <lineno> <rel_file_name>
37 int main(int argc
, char *argv
[])
40 std::cerr
<< "Usage: emit_custom_line_directives infile" << std::endl
;
44 // current file position is saved for exception handling
45 boost::wave::util::file_position_type current_position
;
48 // Open and read in the specified input file.
49 std::ifstream
instream(argv
[1]);
52 if (!instream
.is_open()) {
53 std::cerr
<< "Could not open input file: " << argv
[1] << std::endl
;
56 instream
.unsetf(std::ios::skipws
);
57 instring
= std::string(std::istreambuf_iterator
<char>(instream
.rdbuf()),
58 std::istreambuf_iterator
<char>());
60 // The template boost::wave::cpplexer::lex_token<> is the token type to be
61 // used by the Wave library.
62 typedef boost::wave::cpplexer::lex_token
<> token_type
;
64 // The template boost::wave::cpplexer::lex_iterator<> is the lexer type to
65 // be used by the Wave library.
66 typedef boost::wave::cpplexer::lex_iterator
<token_type
> lex_iterator_type
;
68 // This is the resulting context type to use. The first template parameter
69 // should match the iterator type to be used during construction of the
70 // corresponding context object (see below).
71 typedef boost::wave::context
<std::string::iterator
, lex_iterator_type
,
72 boost::wave::iteration_context_policies::load_file_to_string
,
73 emit_custom_line_directives_hooks
76 // The preprocessor iterator shouldn't be constructed directly. It is
77 // to be generated through a wave::context<> object. This wave:context<>
78 // object additionally may be used to initialize and define different
79 // parameters of the actual preprocessing (not done here).
81 // The preprocessing of the input stream is done on the fly behind the
82 // scenes during iteration over the context_type::iterator_type stream.
83 context_type
ctx (instring
.begin(), instring
.end(), argv
[1]);
85 ctx
.set_language(boost::wave::enable_long_long(ctx
.get_language()));
86 ctx
.set_language(boost::wave::enable_preserve_comments(ctx
.get_language()));
87 ctx
.set_language(boost::wave::enable_prefer_pp_numbers(ctx
.get_language()));
89 // analyze the input file, print out the preprocessed tokens
90 context_type::iterator_type first
= ctx
.begin();
91 context_type::iterator_type last
= ctx
.end();
93 while (first
!= last
) {
94 current_position
= (*first
).get_position();
95 std::cout
<< (*first
).get_value();
99 catch (boost::wave::cpp_exception
const& e
) {
100 // some preprocessing error
102 << e
.file_name() << "(" << e
.line_no() << "): "
103 << e
.description() << std::endl
;
106 catch (std::exception
const& e
) {
107 // use last recognized token to retrieve the error position
109 << current_position
.get_file()
110 << "(" << current_position
.get_line() << "): "
111 << "exception caught: " << e
.what()
116 // use last recognized token to retrieve the error position
118 << current_position
.get_file()
119 << "(" << current_position
.get_line() << "): "
120 << "unexpected exception caught." << std::endl
;