]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Boost.Wave: A Standard compliant C++ preprocessor library | |
3 | http://www.boost.org/ | |
4 | ||
5 | Copyright (c) 2001-2010 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 | =============================================================================*/ | |
9 | ||
10 | #if !defined(BOOST_WAVE_ADVANCED_PREPROCESSING_HOOKS_INCLUDED) | |
11 | #define BOOST_WAVE_ADVANCED_PREPROCESSING_HOOKS_INCLUDED | |
12 | ||
13 | #include <cstdio> | |
14 | #include <ostream> | |
15 | #include <string> | |
16 | ||
17 | #include <boost/assert.hpp> | |
18 | #include <boost/config.hpp> | |
19 | ||
20 | #include <boost/wave/token_ids.hpp> | |
21 | #include <boost/wave/util/macro_helpers.hpp> | |
22 | #include <boost/wave/preprocessing_hooks.hpp> | |
23 | ||
24 | /////////////////////////////////////////////////////////////////////////////// | |
25 | // | |
26 | // The advanced_preprocessing_hooks policy class is used to register some | |
27 | // of the more advanced (and probably more rarely used hooks with the Wave | |
28 | // library. | |
29 | // | |
30 | // This policy type is used as a template parameter to the boost::wave::context<> | |
31 | // object. | |
32 | // | |
33 | /////////////////////////////////////////////////////////////////////////////// | |
34 | class advanced_preprocessing_hooks | |
35 | : public boost::wave::context_policies::default_preprocessing_hooks | |
36 | { | |
37 | public: | |
38 | advanced_preprocessing_hooks() : need_comment(true) {} | |
39 | ||
40 | /////////////////////////////////////////////////////////////////////////// | |
41 | // | |
42 | // The function 'found_directive' is called, whenever a preprocessor | |
43 | // directive was encountered, but before the corresponding action is | |
44 | // executed. | |
45 | // | |
46 | // The parameter 'ctx' is a reference to the context object used for | |
47 | // instantiating the preprocessing iterators by the user. | |
48 | // | |
49 | // The parameter 'directive' is a reference to the token holding the | |
50 | // preprocessing directive. | |
51 | // | |
52 | /////////////////////////////////////////////////////////////////////////// | |
7c673cae FG |
53 | template <typename ContextT, typename TokenT> |
54 | bool | |
55 | found_directive(ContextT const& ctx, TokenT const& directive) | |
7c673cae FG |
56 | { |
57 | // print the commented conditional directives | |
58 | using namespace boost::wave; | |
59 | token_id id = token_id(directive); | |
60 | switch (id) { | |
61 | case T_PP_IFDEF: | |
62 | case T_PP_IFNDEF: | |
63 | case T_PP_IF: | |
64 | case T_PP_ELIF: | |
65 | std::cout << "// " << directive.get_value() << " "; | |
66 | need_comment = false; | |
67 | break; | |
68 | ||
69 | case T_PP_ELSE: | |
70 | case T_PP_ENDIF: | |
71 | std::cout << "// " << directive.get_value() << std::endl; | |
72 | need_comment = true; | |
73 | break; | |
74 | ||
75 | default: | |
76 | break; | |
77 | } | |
78 | ||
7c673cae | 79 | return false; |
7c673cae FG |
80 | } |
81 | ||
82 | /////////////////////////////////////////////////////////////////////////// | |
83 | // | |
84 | // The function 'evaluated_conditional_expression' is called, whenever a | |
85 | // conditional preprocessing expression was evaluated (the expression | |
86 | // given to a #if, #elif, #ifdef or #ifndef directive) | |
87 | // | |
88 | // The parameter 'ctx' is a reference to the context object used for | |
89 | // instantiating the preprocessing iterators by the user. | |
90 | // | |
91 | // The parameter 'expression' holds the non-expanded token sequence | |
92 | // comprising the evaluated expression. | |
93 | // | |
94 | // The parameter expression_value contains the result of the evaluation of | |
95 | // the expression in the current preprocessing context. | |
96 | // | |
97 | // The return value defines, whether the given expression has to be | |
98 | // evaluated again, allowing to decide which of the conditional branches | |
99 | // should be expanded. You need to return 'true' from this hook function | |
100 | // to force the expression to be re-evaluated. | |
101 | // | |
102 | /////////////////////////////////////////////////////////////////////////// | |
7c673cae FG |
103 | template <typename ContextT, typename TokenT, typename ContainerT> |
104 | bool | |
105 | evaluated_conditional_expression(ContextT const &ctx, | |
106 | TokenT const& directive, ContainerT const& expression, | |
107 | bool expression_value) | |
7c673cae FG |
108 | { |
109 | // print the conditional expressions | |
110 | std::cout << boost::wave::util::impl::as_string(expression) << std::endl; | |
111 | need_comment = true; | |
112 | return false; // ok to continue, do not re-evaluate expression | |
113 | } | |
114 | ||
115 | /////////////////////////////////////////////////////////////////////////// | |
116 | // | |
117 | // The function 'skipped_token' is called, whenever a token is about to be | |
118 | // skipped due to a false preprocessor condition (code fragments to be | |
119 | // skipped inside the not evaluated conditional #if/#else/#endif branches). | |
120 | // | |
121 | // The parameter 'ctx' is a reference to the context object used for | |
122 | // instantiating the preprocessing iterators by the user. | |
123 | // | |
124 | // The parameter 'token' refers to the token to be skipped. | |
125 | // | |
126 | /////////////////////////////////////////////////////////////////////////// | |
7c673cae FG |
127 | template <typename ContextT, typename TokenT> |
128 | void | |
129 | skipped_token(ContextT const& ctx, TokenT const& token) | |
7c673cae FG |
130 | { |
131 | // prepend a comment at the beginning of all skipped lines | |
132 | using namespace boost::wave; | |
133 | if (need_comment && token_id(token) != T_SPACE) { | |
134 | std::cout << "// "; | |
135 | need_comment = false; | |
136 | } | |
137 | std::cout << token.get_value(); | |
138 | if (token_id(token) == T_NEWLINE || token_id(token) == T_CPPCOMMENT) | |
139 | need_comment = true; | |
140 | } | |
141 | ||
142 | private: | |
143 | bool need_comment; | |
144 | }; | |
145 | ||
146 | #endif // !defined(BOOST_WAVE_ADVANCED_PREPROCESSING_HOOKS_INCLUDED) |