1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
5 Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
6 Software License, Version 1.0. (See accompanying file
7 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
10 #if !defined(BOOST_WAVE_emit_custom_line_directives_HOOKS_INCLUDED)
11 #define BOOST_WAVE_emit_custom_line_directives_HOOKS_INCLUDED
18 #include <boost/assert.hpp>
19 #include <boost/config.hpp>
21 #include <boost/wave/token_ids.hpp>
22 #include <boost/wave/util/macro_helpers.hpp>
23 #include <boost/wave/preprocessing_hooks.hpp>
25 ///////////////////////////////////////////////////////////////////////////////
27 // The emit_custom_line_directives_hooks policy class is used to register some
28 // of the more advanced (and probably more rarely used hooks with the Wave
31 // This policy type is used as a template parameter to the boost::wave::context<>
34 ///////////////////////////////////////////////////////////////////////////////
35 class emit_custom_line_directives_hooks
36 : public boost::wave::context_policies::default_preprocessing_hooks
39 ///////////////////////////////////////////////////////////////////////////
41 // The function 'emit_line_directive' is called whenever a #line directive
42 // has to be emitted into the generated output.
44 // The parameter 'ctx' is a reference to the context object used for
45 // instantiating the preprocessing iterators by the user.
47 // The parameter 'pending' may be used to push tokens back into the input
48 // stream, which are to be used instead of the default output generated
49 // for the #line directive.
51 // The parameter 'act_token' contains the actual #pragma token, which may
52 // be used for error output. The line number stored in this token can be
53 // used as the line number emitted as part of the #line directive.
55 // If the return value is 'false', a default #line directive is emitted
56 // by the library. A return value of 'true' will inhibit any further
57 // actions, the tokens contained in 'pending' will be copied verbatim
60 ///////////////////////////////////////////////////////////////////////////
61 template <typename ContextT, typename ContainerT>
63 emit_line_directive(ContextT const& ctx, ContainerT &pending,
64 typename ContextT::token_type const& act_token)
66 // emit a #line directive showing the relative filename instead
67 typename ContextT::position_type pos = act_token.get_position();
68 unsigned int column = 1;
70 typedef typename ContextT::token_type result_type;
71 using namespace boost::wave;
73 pos.set_column(column);
74 pending.push_back(result_type(T_POUND, "#", pos));
76 pos.set_column(++column); // account for '#'
77 pending.push_back(result_type(T_SPACE, " ", pos));
79 std::string buffer = std::to_string(pos.get_line());
81 pos.set_column(++column); // account for ' '
82 pending.push_back(result_type(T_INTLIT, buffer.c_str(), pos));
83 pos.set_column(column += buffer.size()); // account for <number>
84 pending.push_back(result_type(T_SPACE, " ", pos));
85 pos.set_column(++column); // account for ' '
87 std::string file("\"");
88 boost::filesystem::path filename(
89 boost::wave::util::create_path(ctx.get_current_relative_filename().c_str()));
91 using boost::wave::util::impl::escape_lit;
92 file += escape_lit(boost::wave::util::native_file_string(filename)) + "\"";
94 pending.push_back(result_type(T_STRINGLIT, file.c_str(), pos));
95 pos.set_column(column += (unsigned int)file.size()); // account for filename
96 pending.push_back(result_type(T_GENERATEDNEWLINE, "\n", pos));
102 #endif // !defined(BOOST_WAVE_ADVANCED_PREPROCESSING_HOOKS_INCLUDED)