Skip to content

Commit

Permalink
Fix empty stringify under C++20 (#221)
Browse files Browse the repository at this point in the history
* fix segment fault of empty stringify under C++20
* Add tests for optional comma in variadic macro call

---------

Co-authored-by: Jeff Trull <edaskel@att.net>
  • Loading branch information
jwnhy and jefftrull authored Sep 15, 2024
1 parent 7ce8b53 commit ef14767
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
43 changes: 27 additions & 16 deletions include/boost/wave/util/cpp_macromap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1262,25 +1262,36 @@ macromap<ContextT>::expand_replacement_list(
else if (adjacent_stringize &&
!IS_CATEGORY(*cit, WhiteSpaceTokenType))
{
// stringize the current argument
BOOST_ASSERT(!arguments[i].empty());

// safe a copy of the first tokens position (not a reference!)
position_type pos((*arguments[i].begin()).get_position());

#if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
if (is_ellipsis && boost::wave::need_variadics(ctx.get_language())) {
impl::trim_sequence_left(arguments[i]);
impl::trim_sequence_right(arguments.back());
expanded.push_back(token_type(T_STRINGLIT,
impl::as_stringlit(arguments, i, pos), pos));
}
#if BOOST_WAVE_SUPPORT_CPP2A != 0
if (i >= arguments.size()) {
// no argument supplied; do nothing (only c20 should reach here)
BOOST_ASSERT(boost::wave::need_cpp2a(ctx.get_language()));
position_type last_valid(arguments.back().back().get_position());
// insert a empty string
expanded.push_back(token_type(T_STRINGLIT, "\"\"", last_valid));
}
else
#endif
{
impl::trim_sequence(arguments[i]);
expanded.push_back(token_type(T_STRINGLIT,
impl::as_stringlit(arguments[i], pos), pos));
// shouldn't be oob (w.o. cpp20)
BOOST_ASSERT(i < arguments.size() && !arguments[i].empty());
// safe a copy of the first tokens position (not a reference!)
position_type pos((*arguments[i].begin()).get_position());

#if BOOST_WAVE_SUPPORT_VARIADICS_PLACEMARKERS != 0
if (is_ellipsis && boost::wave::need_variadics(ctx.get_language())) {
impl::trim_sequence_left(arguments[i]);
impl::trim_sequence_right(arguments.back());
expanded.push_back(token_type(T_STRINGLIT,
impl::as_stringlit(arguments, i, pos), pos));
}
else
#endif
{
impl::trim_sequence(arguments[i]);
expanded.push_back(token_type(T_STRINGLIT,
impl::as_stringlit(arguments[i], pos), pos));
}
}
adjacent_stringize = false;
}
Expand Down
15 changes: 15 additions & 0 deletions test/testwave/testfiles/t_8_004.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,18 @@ sizedvec(int, foo, 12);
//R #line 24 "t_8_004.cpp"
//R std::vector<double> bar( 3 , 42.0);
sizedvec(double, bar, 3, 42.0);

// test omitting trailing comma when expanding a macro with both variadic and
// non-variadic parameters where no variadic parameters are supplied
// (allowed starting in C++20)
#define MACRO1(x, ...) x -> __VA_ARGS__
#define MACRO2(x, ...) #x#__VA_ARGS__
#define MACRO3(x, ...) x##__VA_ARGS__

//R #line 34 "t_8_004.cpp"
MACRO1(1,) //R 1 ->
MACRO2(1,) //R "1"""
MACRO3(1,) //R 1
MACRO1(1) //R 1 ->
MACRO2(1) //R "1"""
MACRO3(1) //R 1

0 comments on commit ef14767

Please sign in to comment.