Boost C++ Libraries: Ticket #8408: Boost is not compatible with Clang's -Wimplicit-fallthrough diagnostic https://svn.boost.org/trac10/ticket/8408 <p> Some of Boost source/header files trigger Clang's -Wimplicit-fallthrough warning, which finds unannotated fall-throughs between case labels in switch statements (more details on the diagnostic here: <a class="ext-link" href="http://clang.llvm.org/docs/LanguageExtensions.html#the-clang-fallthrough-attribute"><span class="icon">​</span>http://clang.llvm.org/docs/LanguageExtensions.html#the-clang-fallthrough-attribute</a>). </p> <p> I propose introducing BOOST_FALLTHROUGH macro to be used to annotate non-trivial fall-through between case labels. </p> <p> A patch with the macro and fixes to the code is attached. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8408 Trac 1.4.3 Alexander Kornienko <alexfh@…> Fri, 05 Apr 2013 23:53:03 GMT attachment set https://svn.boost.org/trac10/ticket/8408 https://svn.boost.org/trac10/ticket/8408 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost-fallthrough.diff</span> </li> </ul> <p> Patch with BOOST_FALLTHROUGH macro and fixes to Boost sources/headers </p> Ticket viboes Sun, 07 Apr 2013 18:35:15 GMT component changed; owner set https://svn.boost.org/trac10/ticket/8408#comment:1 https://svn.boost.org/trac10/ticket/8408#comment:1 <ul> <li><strong>owner</strong> set to <span class="trac-author">John Maddock</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">config</span> </li> </ul> <p> Adding something to Boost.Config needs tests and documentation. You can find on the documentation how this must be done. If you can provide a complete patch there are more chanes this can be included. Once Boost.Config is updated, you can create a ticket for each one of the concerned libraries. </p> Ticket gromer@… Mon, 08 Apr 2013 15:18:08 GMT cc set https://svn.boost.org/trac10/ticket/8408#comment:2 https://svn.boost.org/trac10/ticket/8408#comment:2 <ul> <li><strong>cc</strong> <span class="trac-author">gromer@…</span> added </li> </ul> Ticket anonymous Mon, 08 Apr 2013 15:20:48 GMT <link>https://svn.boost.org/trac10/ticket/8408#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8408#comment:3</guid> <description> <p> For the second part, are you saying this will require a separate ticket and patch for each library that's updated to use the annotation? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Mon, 08 Apr 2013 16:59:22 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8408#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8408#comment:4</guid> <description> <blockquote class="citation"> <p> For the second part, are you saying this will require a separate ticket and patch for each library that's updated to use the annotation? </p> </blockquote> <p> That would certainly increase the chance of acceptance. </p> <p> One thing about your patches though: some compilers (GCC?) will warn if there are empty statements - which is exactly what your warning patch will generate on non-clang compilers. </p> <p> So BOOST_FALLTHROUGH needs to be defined to be used without a following ";". </p> </description> <category>Ticket</category> </item> <item> <author>Alexander Kornienko <alexfh@…></author> <pubDate>Mon, 08 Apr 2013 17:38:53 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8408#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8408#comment:5</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/8408#comment:4" title="Comment 4">johnmaddock</a>: </p> <blockquote class="citation"> <blockquote class="citation"> <p> For the second part, are you saying this will require a separate ticket and patch for each library that's updated to use the annotation? </p> </blockquote> <p> That would certainly increase the chance of acceptance. </p> </blockquote> <p> Thanks for clarification. </p> <blockquote class="citation"> <p> One thing about your patches though: some compilers (GCC?) will warn if there are empty statements - which is exactly what your warning patch will generate on non-clang compilers. </p> <p> So BOOST_FALLTHROUGH needs to be defined to be used without a following ";". </p> </blockquote> <p> On unsupported compilers (or in non-C++11 mode in Clang) it expands to "do {} while(0)", which requires a semicolon as well. This construct was meant to look and behave consistently with 'break;' statement, which it can be an alternative to in many cases. So requiring a semicolon after it seems to be reasonable. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Mon, 08 Apr 2013 17:56:44 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8408#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8408#comment:6</guid> <description> <blockquote class="citation"> <p> On unsupported compilers (or in non-C++11 mode in Clang) it expands to "do {} while(0)", which requires a semicolon as well. This construct was meant to look and behave consistently with 'break;' statement, which it can be an alternative to in many cases. So requiring a semicolon after it seems to be reasonable. </p> </blockquote> <p> "do{}while(0)" generates a "Conditional expression is constant" warning on MSVC. </p> <p> Defining to "(void)" might do it, but it wouldn't surprise me if some compiler warned about that too in future :-( </p> </description> <category>Ticket</category> </item> <item> <author>Alexander Kornienko <alexfh@…></author> <pubDate>Mon, 08 Apr 2013 18:31:38 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8408#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8408#comment:7</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/8408#comment:6" title="Comment 6">johnmaddock</a>: </p> <blockquote class="citation"> <blockquote class="citation"> <p> On unsupported compilers (or in non-C++11 mode in Clang) it expands to "do {} while(0)", which requires a semicolon as well. This construct was meant to look and behave consistently with 'break;' statement, which it can be an alternative to in many cases. So requiring a semicolon after it seems to be reasonable. </p> </blockquote> <p> "do{}while(0)" generates a "Conditional expression is constant" warning on MSVC. </p> <p> Defining to "(void)" might do it, but it wouldn't surprise me if some compiler warned about that too in future :-( </p> </blockquote> <p> How about a separate define for Clang with C++11, and a separate one for MSVC? All others can use "do{}while(0)". </p> <p> As an idea for compilers that warn on "do{}while(0)", we could use a function call without trailing semicolon: </p> <pre class="wiki">void boost_fallthrough() {} #define BOOST_FALLTHROUGH boost_fallthrough() </pre><p> Then all implementations will require a trailing semicolon, which will help to avoid at least one type of errors in usages of this macro. </p> </description> <category>Ticket</category> </item> <item> <author>Alexander Kornienko <alexfh@…></author> <pubDate>Tue, 09 Apr 2013 18:39:39 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/8408 https://svn.boost.org/trac10/ticket/8408 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost-config-fallthrough.patch</span> </li> </ul> <p> Patch for Boost.Config: adds BOOST_FALLTHROUGH macro + tests + documentation. </p> Ticket Alexander Kornienko <alexfh@…> Tue, 09 Apr 2013 18:47:53 GMT <link>https://svn.boost.org/trac10/ticket/8408#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8408#comment:8</guid> <description> <p> I've added another patch (boost-config-fallthrough.patch) with changes only to Boost.Config. It contains BOOST_FALLTHROUGH macro definition for Clang and all other compilers, documentation in .qbk format, compiled .html documentation, tests and introduces one usage of BOOST_FALLTHROUGH macro in libs/config/test/boost_no_std_wstreambuf.ipp. I'm not sure if the last one should go to this patch or not. </p> <p> I've ran tests for this patch with a recent development version of Clang. I've also verified that ((void)0); construct doesn't warn on MSVC 2012 (v17.00.51025 for x86). </p> <p> Please review the patch. Thank you! </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Tue, 09 Apr 2013 19:02:19 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8408#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8408#comment:9</guid> <description> <p> I think John is right. It would be better if the macro contains the ';' so that there is no risk in introducing a new warning. </p> <pre class="wiki">#if __cplusplus &gt;= 201103L &amp;&amp; defined(__has_warning) # if __has_feature(cxx_attributes) &amp;&amp; __has_warning("-Wimplicit-fallthrough") # define BOOST_FALLTHROUGH [[clang::fallthrough]]; # endif #endif </pre><p> and </p> <pre class="wiki">#ifndef BOOST_FALLTHROUGH # define BOOST_FALLTHROUGH #endif </pre> </description> <category>Ticket</category> </item> <item> <author>Alexander Kornienko <alexfh@…></author> <pubDate>Wed, 10 Apr 2013 00:40:18 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8408#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8408#comment:10</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/8408#comment:9" title="Comment 9">viboes</a>: </p> <blockquote class="citation"> <p> I think John is right. It would be better if the macro contains the ';' so that there is no risk in introducing a new warning. </p> </blockquote> <p> I'm happy to change the macro to not require a semicolon, but first I'd like to share my arguments for the opposite solution: </p> <ol><li>Consistency with break/return/continue/throw/goto: <pre class="wiki">break; return xxx; BOOST_FALLTHROUGH; // but without semicolon it looks alien: BOOST_FALLTHROUGH </pre></li></ol><ol start="2"><li>More generally, C++ statements usually end with a semicolon or closing brace, and a standalone identifier doesn't look like a separate valid C++ statement; this makes code less readable. </li></ol><ol start="3"><li>For the same reason as 2. this can confuse various tools, such as code editors, code formatters, linters etc. E.g. vim treats the next line after a standalone identifier as a continuation, and adds extra indent to it. clang-format - the new C++ formatter - is not going to format this correctly too. </li></ol><ol start="4"><li>If the macro doesn't require a semicolon, but a developer inserts it by mistake, presence of this extra null-statement will lead to two warnings on Clang with -Wimplicit-fallthrough: fallthrough annotation is not immediately before a fallthrough and fallthrough is not annotated. On other compilers this will only warn if a compiler complains about null-statements. I consider this a worse situation than when a macro requires a semicolon, and all compilers will warn if a developer forgets it. </li></ol><p> That's it. Now it's up to you to decide what is better for Boost. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Wed, 10 Apr 2013 12:24:34 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8408#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8408#comment:11</guid> <description> <p> I have to be honest: I'm not particularly taken by any of the solutions so far :-( </p> <p> By way of keeping it simple, what's wrong with just disabling (with pushes and pops) this warning in the few effected source files - rather like we do with MSVC warnings? IMO it looks simpler and clearer to do it this way rather than introducing new macros etc... </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Wed, 10 Apr 2013 14:22:05 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8408#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8408#comment:12</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/8408#comment:11" title="Comment 11">johnmaddock</a>: </p> <blockquote class="citation"> <p> By way of keeping it simple, what's wrong with just disabling (with pushes and pops) this warning in the few effected source files - rather like we do with MSVC warnings? IMO it looks simpler and clearer to do it this way rather than introducing new macros etc... </p> </blockquote> <p> +1 </p> </description> <category>Ticket</category> </item> <item> <author>gromer@…</author> <pubDate>Wed, 10 Apr 2013 14:45:04 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8408#comment:13 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8408#comment:13</guid> <description> <p> FWIW I agree with alexfh that the macro should require a semicolon at the point of use. I'm kind of flabbergasted that "while(0)" warns in MSVC; "do{...}while(0)" is an idiomatic way of providing a macro that can be used wherever a statement is expected, and "while(true)" or the equivalent is an even more idiomatic way of writing a loop with no condition. This Stack Overflow answer seems to indicate a consensus that MSVC's warning is broken: <a class="ext-link" href="http://stackoverflow.com/a/1946485"><span class="icon">​</span>http://stackoverflow.com/a/1946485</a> </p> <p> As for the idea of disabling the warning with pragmas, I think that would be a missed opportunity: unintended fall-through is a common source of real bugs, and in places where fall-through is intended, I think explicitly annotating that fact makes the code more readable. If we're going to mark up the source files anyway, why not mark them up in a way that enhances readability and catches bugs? </p> </description> <category>Ticket</category> </item> <item> <author>Alexander Kornienko <alexfh@…></author> <pubDate>Wed, 10 Apr 2013 16:41:02 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8408#comment:14 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8408#comment:14</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/8408#comment:11" title="Comment 11">johnmaddock</a>: </p> <blockquote class="citation"> <p> I have to be honest: I'm not particularly taken by any of the solutions so far :-( </p> <p> By way of keeping it simple, what's wrong with just disabling (with pushes and pops) this warning in the few effected source files - rather like we do with MSVC warnings? IMO it looks simpler and clearer to do it this way rather than introducing new macros etc... </p> </blockquote> <p> This macro could be a better alternative to fall-through/fall through/FALLTHROUGH/fallthrough/drop through/the lack of break is intentional/... and dozens of other variations of comments annotating this for humans. Frequently, there are no comments at all, so it's sometimes difficult to understand whether a fall-through is intentional or not. Having compiler-verifiable annotations would improve code readability and prevent a common kind of bug when one just forgets to put a 'break;' before next case label. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Thu, 18 Apr 2013 17:48:02 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8408#comment:15 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8408#comment:15</guid> <description> <p> I still worry that this will simply generate more compiler warnings down the road... However, I do like the syntax. So I'll commit shortly. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Thu, 18 Apr 2013 17:50:22 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/8408#comment:16 https://svn.boost.org/trac10/ticket/8408#comment:16 <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> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/83958" title="Apply patch from #8408. Fixes #8408.">[83958]</a>) Apply patch from <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8408" title="#8408: Patches: Boost is not compatible with Clang's -Wimplicit-fallthrough diagnostic (closed: fixed)">#8408</a>. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8408" title="#8408: Patches: Boost is not compatible with Clang's -Wimplicit-fallthrough diagnostic (closed: fixed)">#8408</a>. </p> Ticket Alexander Kornienko <alexfh@…> Thu, 18 Apr 2013 22:25:38 GMT <link>https://svn.boost.org/trac10/ticket/8408#comment:17 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8408#comment:17</guid> <description> <p> Thank you! Nit: there are trailing spaces added almost in each changed line, I guess, this is not intentional. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Mon, 22 Apr 2013 17:52:14 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8408#comment:18 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8408#comment:18</guid> <description> <blockquote class="citation"> <p> Nit: there are trailing spaces added almost in each changed line, I guess, this is not intentional. </p> </blockquote> <p> My bad. Does it matter? </p> </description> <category>Ticket</category> </item> <item> <author>Alexander Kornienko <alexfh@…></author> <pubDate>Mon, 22 Apr 2013 18:50:03 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8408#comment:19 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8408#comment:19</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/8408#comment:18" title="Comment 18">johnmaddock</a>: </p> <blockquote class="citation"> <blockquote class="citation"> <p> Nit: there are trailing spaces added almost in each changed line, I guess, this is not intentional. </p> </blockquote> <p> My bad. Does it matter? </p> </blockquote> <p> As a minor style issue + a bit of extra work for us when merging. </p> </description> <category>Ticket</category> </item> <item> <author>gromer@…</author> <pubDate>Mon, 22 Apr 2013 19:15:46 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8408#comment:20 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8408#comment:20</guid> <description> <p> FWIW, speaking as the one who'll probably be responsible for the merging, I have no problem with the trailing whitespace, and the style issue seems like a moot point, since trailing whitespace is pretty endemic to the Boost codebase. </p> </description> <category>Ticket</category> </item> <item> <author>Alexander Kornienko <alexfh@…></author> <pubDate>Mon, 22 Apr 2013 20:07:46 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8408#comment:21 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8408#comment:21</guid> <description> <p> Then it doesn't matter. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>James E. King, III</dc:creator> <pubDate>Thu, 12 Oct 2017 17:44:03 GMT</pubDate> <title>version, milestone changed https://svn.boost.org/trac10/ticket/8408#comment:22 https://svn.boost.org/trac10/ticket/8408#comment:22 <ul> <li><strong>version</strong> <span class="trac-field-old">Boost Development Trunk</span> → <span class="trac-field-new">Boost 1.61.0</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.62.0</span> </li> </ul> Ticket