Boost C++ Libraries: Ticket #4421: fix for #4400 https://svn.boost.org/trac10/ticket/4421 <p> The attached patch file fixes the bug described in ticket <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/4400" title="#4400: Bugs: BOOST_PP_SEQ_REPLACE fails in corner cases (closed: fixed)">#4400</a> </p> <p> Yet to be done: has somebody access to a compiler based on Edison Design Group C++ front end version 3.0.8 or earlier? I would like the code being compiled by such a compiler and see, whether the code fork for that compiler type runs properly. </p> <p> The preprocessor code has not been touched in the past 7 years, and seemingly there is no active maintainer around, so I chose to keep the fixes impact on existing software is minimal as possible. I can tell from the sources that the author of the preprocessor code meant BOOST_PP_SEQ_REST_N to fulfil a certain undocumented condition, which it violates in a single case, so a good fix would care about said condition. But BOOST_PP_SEQ_REST_N is much more at the core of the preprocessor, and a change affects more code. So, instead, I made the only client BOOST_PP_SEQ_REPLACE relying on that condition when BOOST_PP_SEQ_REST_N fails to fulfil it, independent of BOOST_PP_SEQ_REST_N. </p> <p> The code contains a compiler fork, that treats preprocessors with rescanning and/or argument substitution problems differently. I used the same technics I found elsewhere in boost code to work around that problems. I think I it is ok, and my (compliant) compiler does not complain, but, of course, there is a slight chance I got it wrong. </p> <p> To verify the fix is correct I used a self written test suite, that checks the validity of a BOOST_PP_SEQ_REPLACE implementation. It uses a brute force method, i.e. sequences of all possible lengths are tested, each element is replaced, and each replacement is individually checked. The test suite allows for varying the input sequence, so sequences containing characters with special meaning to the preprocessor has been tested as well. The test suite is a UNIX bash shell script, which I attached here as well. </p> <p> The test results show without exception so far, </p> <ul><li>that the fixed code produces exactly the same results than the old code, whereever the old code succeeds. This includes the distribution of white space characters in between sequence elements; </li><li>that the fixed code produces the correct result, where the old code fails; </li><li>that the same holds if the BOOST_PP_CONFIG_EDG flag is forced. However, the GNU preprocessor I used is standard compliant, so an old EDG based compiler might still fail. </li></ul><p> As I changed the code in seq_replace.hpp significantly, and in a not trivial manner, I added my copyright notice. </p> <p> Cheers </p> <p> Wolf Lammen </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4421 Trac 1.4.3 Wolf Lammen <ookami1@…> Sun, 11 Jul 2010 09:38:24 GMT attachment set https://svn.boost.org/trac10/ticket/4421 https://svn.boost.org/trac10/ticket/4421 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">seq_replace.patch</span> </li> </ul> Ticket Wolf Lammen <ookami1@…> Sun, 11 Jul 2010 09:39:03 GMT attachment set https://svn.boost.org/trac10/ticket/4421 https://svn.boost.org/trac10/ticket/4421 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">testseq.sh</span> </li> </ul> <p> test suite </p> Ticket Wolf Lammen <ookami1@…> Thu, 15 Jul 2010 21:28:43 GMT <link>https://svn.boost.org/trac10/ticket/4421#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4421#comment:1</guid> <description> <p> The more I look into the preprocessor source code, the more I am convinced, Mr. Mensonides wanted his macros to be able to handle empty sequences. This is in contrast to how I read the preprocessor documentation: </p> <p> <em>"Sequences are data structures that merge the properties of both lists and tuples with the exception that a seq cannot be empty. Therefore, an 'empty' seq is considered a special case scenario that must be handled separately in C++."</em> </p> <p> If my assumption above is right, BOOST_PP_SEQ_REST_N works incorrect for 256 as its first argument. </p> <p> So here is an alternative patch seq_split.patch, that fixes _both_ BOOST_PP_SEQ_REST_N and BOOST_PP_SEQ_REPLACE, and, to boot, simplifies code in seq/rest_n.cpp and seq/first_n.cpp. </p> <p> IMHO, it should be preferred over my previous patch seq_replace.patch, that fixes BOOST_PP_SEQ_REPLACE only. </p> </description> <category>Ticket</category> </item> <item> <author>Wolf Lammen <ookami1@…></author> <pubDate>Thu, 15 Jul 2010 21:29:23 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/4421 https://svn.boost.org/trac10/ticket/4421 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">seq_split.patch</span> </li> </ul> Ticket Steven Watanabe Mon, 30 Aug 2010 03:16:14 GMT <link>https://svn.boost.org/trac10/ticket/4421#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4421#comment:2</guid> <description> <p> Your patch prevents BOOST_PP_SEQ_REST_N from handling empty sequences. In the current C++ standard, empty sequences don't work because empty macro arguments are illegal, but IIRC, C99 and C++0x both allow empty macro arguments. </p> <p> See <a class="ext-link" href="http://chaos-pp.cvs.sourceforge.net/viewvc/chaos-pp/chaos-pp/chaos/preprocessor/seq/drop.h?hideattic=0&amp;revision=1.3&amp;view=markup"><span class="icon">​</span>http://chaos-pp.cvs.sourceforge.net/viewvc/chaos-pp/chaos-pp/chaos/preprocessor/seq/drop.h?hideattic=0&amp;revision=1.3&amp;view=markup</a> for another implementation. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Mon, 30 Aug 2010 03:18:27 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4421#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4421#comment:3</guid> <description> <p> Never mind. I missed the changes to split.hpp. However, I'm not sure that n ## seq is legal if seq is empty. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Mon, 30 Aug 2010 03:22:35 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4421#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4421#comment:4</guid> <description> <p> Unfortunately, your patch adds an extraneous BOOST_PP_EMPTY in BOOST_PP_SEQ_SPLIT. Are you sure that this doesn't break anything else? </p> </description> <category>Ticket</category> </item> <item> <author>ookami1@…</author> <pubDate>Mon, 30 Aug 2010 14:56:27 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4421#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4421#comment:5</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/4421#comment:4" title="Comment 4">steven_watanabe</a>: </p> <blockquote class="citation"> <p> Unfortunately, your patch adds an extraneous BOOST_PP_EMPTY in BOOST_PP_SEQ_SPLIT. Are you sure that this doesn't break anything else? </p> </blockquote> <p> Hi Steven, </p> <p> Grepping shows, that the only clients of BOOST_PP_SEQ_SPLIT within boost are BOOST_PP_SEQ_FIRST_N and BOOST_PP_SEQ_REST_N, and outside of boost SHOULD not exist a user, because BOOST_PP_SEQ_SPLIT, hidden in a detail directory, is not part of the preprocessor interface. And both clients, of course, have been adapted to the semantic change of BOOST_PP_SEQ_SPLIT. </p> <p> The usage of BOOST_PP_EMPTY is an idiom required, because C90 does not guarantee proper handling of empty arguments to macros. So MACRO(seq BOOST_PP_EMPTY) is a conformant way to pass an empty sequence (or anything that might expand to nothing) as a non-empty argument to a macro. During expansion the padded BOOST_PP_EMPTY is easily popped off by appending () to the passed argument. This technique is found all over the preprocessor code. </p> <p> BOOST_PP_SEQ_SPLIT has been tailored to follow this idiom suit: It expects a sequence padded with BOOST_PP_EMPTY, and it will return both the head and the tail of a split sequence padded again. This safe handling of empty heads or tails allowed the fixing BOOST_PP_SEQ_REPLACE (and, to boot, BOOST_PP_REST_N, BOOST_PP_FIRST_N) </p> <p> cheers Wolf Lammen </p> </description> <category>Ticket</category> </item> <item> <author>ookami1@…</author> <pubDate>Mon, 30 Aug 2010 16:05:26 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4421#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4421#comment:6</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/4421#comment:3" title="Comment 3">steven_watanabe</a>: </p> <blockquote class="citation"> <p> Never mind. I missed the changes to split.hpp. However, I'm not sure that n ## seq is legal if seq is empty. </p> </blockquote> <p> You are right here, this will not work, when an empty sequence (actually it will not be exactly empty, but BOOST_PP_EMPTY instead) is submitted. Fortunately, you will never call BOOST_PP_SEQ_REPLACE with an empty sequence. And calls to BOOST_PP_REST_N or BOOST_PP_FIRST_N with empty sequences are considered illegal as well, according to the documentation. So the problem reduces to a possibly internal usage of the latter macros, exploiting the fact, that they internally appended a (nil) to sequences. If I remember right, I already checked on that condition, but I am a bit unsure right now, so I will do it again. </p> <p> If it turns out to be a problem, the solution seems straightforward: For Microwerks (only): </p> <p> #define BOOST_PP_SEQ_SPLIT_0_BOOST_PP_EMPTY BOOST_PP_EMPTY, BOOST_PP_EMPTY </p> <p> cheers Wolf Lammen </p> </description> <category>Ticket</category> </item> <item> <author>ookami1@…</author> <pubDate>Tue, 31 Aug 2010 12:56:49 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4421#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4421#comment:7</guid> <description> <p> The only indirect callers of BOOST_PP_SEQ_SPLIT within boost, but outside of preprocessor/seq, I could find, are macros in: </p> <p> mpl/aux_/preprocessor/range.hpp (and their users) </p> <p> parameter/preprocessor.hpp (and their users) </p> <p> Only for some calls to BOOST_PP_SEQ_FIND_N in parameter/preprocessor.hpp I was unable to rule out they pass empty sequences, mostly, because I got lost in the hierarchies of layers found there. If (that is not for sure!) they really pass empty sequences to the preprocessor, then </p> <ol><li>this violates the documented limitations of the preprocessor code; </li><li>this is likely to fail on exotic compilers, because some do not support empty macro parameters at all. </li></ol><p> So, what line is to follow here? Fortify BOOST_PP_SEQ_FIRST_N such that it tolerates abuses to the extent possible? Stay within the asserted limitations and let bugs surface in dependent code? Notify the maintainers of boost/parameter of possible conflicts? Start a discussion on the mailing list? Give up on supporting sequences of size 256 altogether and decrement BOOST_PP_LIMIT_SEQ (currently set to 256, but I know many macros (if not the majority) in preprocessor/seq do not support this limit)? </p> </description> <category>Ticket</category> </item> <item> <author>ookami1@…</author> <pubDate>Sat, 18 Sep 2010 14:35:24 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/4421 https://svn.boost.org/trac10/ticket/4421 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">seq_first_rest_n.patch</span> </li> </ul> Ticket anonymous Sat, 18 Sep 2010 14:36:46 GMT <link>https://svn.boost.org/trac10/ticket/4421#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4421#comment:8</guid> <description> <p> seq_first_rest_n.patch fixes BOOST_PP_SEQ_FIRST_N and BOOST_PP_SEQ_REST_N, so they both accept sequences of length 256 now. As a bonus feature, for preprocessors sufficiantly handling empty arguments (or those expanding to nothing), both macros process empty sequences as well. Usage of empty sequences is outside of the specs of the preprocessor code, and is not generally supported by the preprocessor library. </p> <p> The previous patches did not handle empty sequences properly for DMC based preprocessors. This is corrected here, no other difference is introduced. </p> <p> This patch supersedes all previous ones. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Edward Diener</dc:creator> <pubDate>Mon, 18 May 2015 20:50:40 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4421#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4421#comment:9</guid> <description> <p> Fixed in the latest preprocessor code in the 'develop' branch. </p> <p> While I appreciate the patches I did not use them. It is an undefined result to call BOOST_PP_SEQ_REST_N with an 'n' which is equal to the size of the 'seq'. The fact that this returns nothing is an implementation detail, since a seq cannot be empty and BOOST_PP_SEQ_REST_N clearly states that it expands to a seq. I will update the documentation accordingly. I am not willing to change BOOST_PP_SEQ_REST_N to accomodate a situation which should be undefined. </p> </description> <category>Ticket</category> </item> <item> <author>ookami1@…</author> <pubDate>Tue, 19 May 2015 10:09:37 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4421#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4421#comment:10</guid> <description> <p> I do see your point, but let me comment on it nevertheless. This is not meant to talk you into applying this patches. It is fine with me, and actually I was quite surprised and pleased to see somebody care about this report at all after such a long time. </p> <p> Usually, you would do parameter checking in order to enforce an API. This cannot really be done in preprocessor code, as this is very expensive and requires lots of macro replacements, unduely slowing down compiling. </p> <p> So it is up to users to carefully use the preprocessor code. Apart from sloppy mindsets found all over the place, correct handling is not made easy. Who really sees at a glance that BOOST_PP_SEQ_REST_N must not be called with an index pointing to the last element. And are there other places with obscure restrictions? Then we have the exceptions (see the documentation for BOOST_PP_SEQ_NIL) which may fool people into thinking, they can do likewise somewhere else. </p> <p> On top, it does not help that BOOST_PP_SEQ_REST_N in reality behaves as expected in the vast majority of (and practically all relevant) cases. So code appears to be correct, but it is not. </p> <p> I proposed my patches because I felt it is more appropriate to be fault tolerant in such a situation. </p> <p> cheers Wolf </p> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/4421#comment:9" title="Comment 9">eldiener</a>: </p> <blockquote class="citation"> <p> Fixed in the latest preprocessor code in the 'develop' branch. </p> <p> While I appreciate the patches I did not use them. It is an undefined result to call BOOST_PP_SEQ_REST_N with an 'n' which is equal to the size of the 'seq'. The fact that this returns nothing is an implementation detail, since a seq cannot be empty and BOOST_PP_SEQ_REST_N clearly states that it expands to a seq. I will update the documentation accordingly. I am not willing to change BOOST_PP_SEQ_REST_N to accomodate a situation which should be undefined. </p> </blockquote> </description> <category>Ticket</category> </item> <item> <dc:creator>Edward Diener</dc:creator> <pubDate>Tue, 19 May 2015 13:52:24 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4421#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4421#comment:11</guid> <description> <p> It is usually possible to do a certain amount of parameter checking in Boost PP but there is a compile time expense to doing it. Adding more checking at a lower level slows down all macro code that uses that lower level macro, while doing more checking at a higher level only slows down that macro code calling the higher level macro. That was one decision on why I chose to only change BOOST_PP_SEQ_REPLACE. </p> <p> Another decision is to minimize possible changes to already existing usage while correcting functionality. The less change to have something work according to documentation is often more preferred than having more changes. </p> <p> I did update the documentation to BOOST_PP_SEQ_REST_N regarding the issue of using passing it an amount equal to or greater than the seq size. </p> <p> My Boost VMD library deals with emptiness as a viable concept in the preprocessor, because the test for emptiness, which can never work perfectly, is much better using variadic macros than when not using variadic macros. But in Boost PP, whose supports both non-variadic and some variadic macro use, emptiness should never be depended on. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Edward Diener</dc:creator> <pubDate>Fri, 21 Aug 2015 13:40:36 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4421#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4421#comment:12</guid> <description> <p> This general problem has been fixed in the Boost 1.59 release. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Edward Diener</dc:creator> <pubDate>Fri, 21 Aug 2015 13:40:54 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/4421#comment:13 https://svn.boost.org/trac10/ticket/4421#comment:13 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> Ticket