📄 cpp_iterator.hpp
字号:
new iteration_context_type(native_path.native_file_string().c_str(),
act_pos, ctx.get_language()));
// call the include policy trace function
ctx.get_trace_policy().opened_include_file(dir_path, file_path,
ctx.get_iteration_depth(), is_system);
// store current file position
iter_ctx->filename = act_pos.get_file();
iter_ctx->line = act_pos.get_line();
iter_ctx->if_block_depth = ctx.get_if_block_depth();
// push the old iteration context onto the stack and continue with the new
ctx.push_iteration_context(act_pos, iter_ctx);
iter_ctx = new_iter_ctx;
seen_newline = true; // fake a newline to trigger pp_directive
must_emit_line_directive = true;
act_pos.set_file(iter_ctx->filename); // initialize file position
#if BOOST_WAVE_SUPPORT_PRAGMA_ONCE != 0
ctx.set_current_filename(iter_ctx->real_filename.c_str());
#endif
// last_line = iter_ctx->line;
act_pos.set_line(iter_ctx->line);
act_pos.set_column(0);
}
}
///////////////////////////////////////////////////////////////////////////////
//
// on_include(): handle #include ... directives
//
///////////////////////////////////////////////////////////////////////////////
namespace {
// trim all whitespace from the beginning and the end of the given string
template <typename StringT>
inline StringT
trim_whitespace(StringT const &s)
{
typedef typename StringT::size_type size_type;
size_type first = s.find_first_not_of(" \t\v\f");
if (StringT::npos == first)
return StringT();
size_type last = s.find_last_not_of(" \t\v\f");
return s.substr(first, last-first+1);
}
}
template <typename ContextT>
inline void
pp_iterator_functor<ContextT>::on_include(
typename parse_tree_type::const_iterator const &begin,
typename parse_tree_type::const_iterator const &end, bool include_next)
{
BOOST_ASSERT(ctx.get_if_block_status());
// preprocess the given token sequence (the body of the #include directive)
get_token_value<result_type, parse_node_type> get_value;
token_sequence_type expanded;
token_sequence_type toexpand;
std::copy(make_ref_transform_iterator(begin, get_value),
make_ref_transform_iterator(end, get_value),
std::inserter(toexpand, toexpand.end()));
typename token_sequence_type::iterator begin2 = toexpand.begin();
ctx.expand_whole_tokensequence(begin2, toexpand.end(), expanded,
false);
// now, include the file
string_type s (trim_whitespace(boost::wave::util::impl::as_string(expanded)));
bool is_system = '<' == s[0] && '>' == s[s.size()-1];
if (!is_system && !('\"' == s[0] && '\"' == s[s.size()-1])) {
// should resolve into something like <...> or "..."
BOOST_WAVE_THROW(preprocess_exception, bad_include_statement,
s.c_str(), act_pos);
}
on_include(s, is_system, include_next);
}
///////////////////////////////////////////////////////////////////////////////
//
// on_define(): handle #define directives
//
///////////////////////////////////////////////////////////////////////////////
template <typename ContextT>
inline void
pp_iterator_functor<ContextT>::on_define (parse_node_type const &node)
{
BOOST_ASSERT(ctx.get_if_block_status());
// retrieve the macro definition from the parse tree
result_type macroname;
std::vector<result_type> macroparameters;
token_sequence_type macrodefinition;
bool has_parameters = false;
boost::wave::util::retrieve_macroname(node,
cpp_grammar_type::rule_ids.plain_define_id, macroname,
act_token.get_position());
has_parameters = boost::wave::util::retrieve_macrodefinition(node,
cpp_grammar_type::rule_ids.macro_parameters_id, macroparameters, act_token);
boost::wave::util::retrieve_macrodefinition(node,
cpp_grammar_type::rule_ids.macro_definition_id, macrodefinition, act_token);
if (has_parameters) {
#if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
if (boost::wave::need_variadics(ctx.get_language())) {
// test whether ellipsis are given, and if yes, if these are placed as the
// last argument
using namespace cpplexer;
typedef typename std::vector<result_type>::iterator
parameter_iterator_t;
bool seen_ellipses = false;
parameter_iterator_t end = macroparameters.end();
for (parameter_iterator_t pit = macroparameters.begin();
pit != end; ++pit)
{
if (seen_ellipses) {
// ellipses are not the last given formal argument
BOOST_WAVE_THROW(preprocess_exception, bad_define_statement,
macroname.get_value().c_str(), (*pit).get_position());
}
if (T_ELLIPSIS == token_id(*pit))
seen_ellipses = true;
}
// if there wasn't an ellipsis, then there shouldn't be a __VA_ARGS__
// placeholder in the definition too [C99 Standard 6.10.3.5]
if (!seen_ellipses) {
typedef typename token_sequence_type::iterator definition_iterator_t;
bool seen_va_args = false;
definition_iterator_t pend = macrodefinition.end();
for (definition_iterator_t dit = macrodefinition.begin();
dit != pend; ++dit)
{
if (T_IDENTIFIER == token_id(*dit) &&
"__VA_ARGS__" == (*dit).get_value())
{
seen_va_args = true;
}
}
if (seen_va_args) {
// must not have seen __VA_ARGS__ placeholder
BOOST_WAVE_THROW(preprocess_exception, bad_define_statement,
macroname.get_value().c_str(), act_token.get_position());
}
}
}
else
#endif // BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
{
// test, that there is no T_ELLIPSES given
using namespace cpplexer;
typedef typename std::vector<result_type>::iterator
parameter_iterator_t;
parameter_iterator_t end = macroparameters.end();
for (parameter_iterator_t pit = macroparameters.begin();
pit != end; ++pit)
{
if (T_ELLIPSIS == token_id(*pit)) {
// if variadics are disabled, no ellipses should be given
BOOST_WAVE_THROW(preprocess_exception, bad_define_statement,
macroname.get_value().c_str(), (*pit).get_position());
}
}
}
}
// add the new macro to the macromap
ctx.add_macro_definition(macroname, has_parameters, macroparameters,
macrodefinition);
}
///////////////////////////////////////////////////////////////////////////////
//
// on_undefine(): handle #undef directives
//
///////////////////////////////////////////////////////////////////////////////
template <typename ContextT>
inline void
pp_iterator_functor<ContextT>::on_undefine (result_type const &token)
{
BOOST_ASSERT(ctx.get_if_block_status());
// retrieve the macro name to undefine from the parse tree
ctx.remove_macro_definition(token.get_value()); // throws for predefined macros
}
///////////////////////////////////////////////////////////////////////////////
//
// on_ifdef(): handle #ifdef directives
//
///////////////////////////////////////////////////////////////////////////////
template <typename ContextT>
inline void
pp_iterator_functor<ContextT>::on_ifdef(
typename parse_tree_type::const_iterator const &begin,
typename parse_tree_type::const_iterator const &end)
{
get_token_value<result_type, parse_node_type> get_value;
bool is_defined = ctx.is_defined_macro(
make_ref_transform_iterator((*begin).children.begin(), get_value),
make_ref_transform_iterator((*begin).children.end(), get_value));
ctx.enter_if_block(is_defined);
}
///////////////////////////////////////////////////////////////////////////////
//
// on_ifndef(): handle #ifndef directives
//
///////////////////////////////////////////////////////////////////////////////
template <typename ContextT>
inline void
pp_iterator_functor<ContextT>::on_ifndef(
typename parse_tree_type::const_iterator const &begin,
typename parse_tree_type::const_iterator const &end)
{
get_token_value<result_type, parse_node_type> get_value;
bool is_defined = ctx.is_defined_macro(
make_ref_transform_iterator((*begin).children.begin(), get_value),
make_ref_transform_iterator((*begin).children.end(), get_value));
ctx.enter_if_block(!is_defined);
}
///////////////////////////////////////////////////////////////////////////////
//
// on_else(): handle #else directives
//
///////////////////////////////////////////////////////////////////////////////
template <typename ContextT>
inline void
pp_iterator_functor<ContextT>::on_else()
{
if (!ctx.enter_else_block()) {
// #else without matching #if
BOOST_WAVE_THROW(preprocess_exception, missing_matching_if, "#else",
act_pos);
}
}
///////////////////////////////////////////////////////////////////////////////
//
// on_endif(): handle #endif directives
//
///////////////////////////////////////////////////////////////////////////////
template <typename ContextT>
inline void
pp_iterator_functor<ContextT>::on_endif()
{
if (!ctx.exit_if_block()) {
// #endif without matching #if
BOOST_WAVE_THROW(preprocess_exception, missing_matching_if, "#endif",
act_pos);
}
}
///////////////////////////////////////////////////////////////////////////////
//
// on_if(): handle #if directives
//
///////////////////////////////////////////////////////////////////////////////
template <typename ContextT>
inline void
pp_iterator_functor<ContextT>::on_if(
typename parse_tree_type::const_iterator const &begin,
typename parse_tree_type::const_iterator const &end)
{
// preprocess the given sequence into the provided list
get_token_value<result_type, parse_node_type> get_value;
token_sequence_type expanded;
token_sequence_type toexpand;
std::copy(make_ref_transform_iterator(begin, get_value),
make_ref_transform_iterator(end, get_value),
std::inserter(toexpand, toexpand.end()));
typename token_sequence_type::iterator begin2 = toexpand.begin();
ctx.expand_whole_tokensequence(begin2, toexpand.end(), expanded);
// replace all remaining (== undefined) identifiers with an integer literal '0'
typename token_sequence_type::iterator exp_end = expanded.end();
for (typename token_sequence_type::iterator exp_it = expanded.begin();
exp_it != exp_end; ++exp_it)
{
using namespace boost::wave;
token_id id = token_id(*exp_it);
if (IS_CATEGORY(id, IdentifierTokenType) ||
IS_CATEGORY(id, KeywordTokenType))
{
(*exp_it).set_token_id(T_INTLIT);
(*exp_it).set_value("0");
}
}
#if BOOST_WAVE_DUMP_CONDITIONAL_EXPRESSIONS != 0
{
string_type outstr(boost::wave::util::impl::as_string(toexpand));
outstr += "(" + boost::wave::util::impl::as_string(expanded) + ")";
BOOST_WAVE_DUMP_CONDITIONAL_EXPRESSIONS_OUT << "#if " << outstr
<< std::endl;
}
#endif
// parse the expression and enter the #if block
ctx.enter_if_block(grammars::expression_grammar_gen<result_type>::
evaluate(expanded.begin(), expanded.end(), act_pos,
ctx.get_if_block_status()));
}
///////////////////////////////////////////////////////////////////////////////
//
// on_elif(): handle #elif directives
//
///////////////////////////////////////////////////////////////////////////////
template <typename ContextT>
inline void
pp_iterator_functor<ContextT>::on_elif(
typename parse_tree_type::const_iterator const &begin,
typename parse_tree_type::const_iterator const &end)
{
if (ctx.get_if_block_status()) {
if (!ctx.enter_elif_block(false)) {
// #else without matching #if
BOOST_WAVE_THROW(preprocess_exception, missing_matching_if, "#elif",
act_pos);
}
return; // #if/previous #elif was true, so don't enter this #elif
}
// preprocess the given sequence into the provided list
get_token_value<result_type, parse_node_type> get_value;
token_sequence_type expanded;
token_sequence_type toexpand;
std::copy(make_ref_transform_iterator(begin, get_value),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -