This is an archive of the discontinued LLVM Phabricator instance.

[C++17] Allow an empty expression in an if init statement
ClosedPublic

Authored by Rakete1111 on Nov 24 2017, 9:40 AM.

Details

Summary

This fixes PR35381 and an additional bug where clang didn't warn about the C++17 extension when having an expression in the init statement.

Thanks Nicolas Lesser for contributing the patch.

Diff Detail

Repository
rC Clang

Event Timeline

Rakete1111 created this revision.Nov 24 2017, 9:40 AM

Hi, thanks for working on this!
Can you add tests to make sure that this also works with switch statements (which also have this bug), and not with while? Also, it makes it a lot easier to review these patches if you add context lines to the diff.
Thanks,
Erik

lib/Parse/ParseExprCXX.cpp
1750

We should only be doing this if InitStmt != nullptr, right? I think this would lead us to be too permissive with while statements, which don't have this feature. Also, it would be nice to emit a c++14-compat warning here, like below.

Added a test for the switch statement and added full context to the diff.

Addressed final review comments by adding the appropriate warnings that were missing previously.

Rakete1111 marked an inline comment as done.

Moved the while test outside of the #ifdef region for better coverage :)

Rakete1111 edited the summary of this revision. (Show Details)

Added more test coverage for compatibility warnings, and fixed a bug at the same time :).

Rakete1111 updated this revision to Diff 128659.Jan 4 2018, 3:13 PM

Rebased + friendly 2018 ping :)

rsmith added inline comments.Jan 4 2018, 3:32 PM
lib/Parse/ParseExprCXX.cpp
1762

Please create a NullStmt here (Actions.ActOnNullStmt), rather than producing an AST that's indistinguishable from one where the init statement was omitted.

1783–1784

Use WarnOnInit() here.

test/CXX/stmt.stmt/stmt.select/p3.cpp
70–71

I think error recovery thought that a ) was omitted before the ;, so it imagined we had:

while (Var + Var); Var--) {}

Then it thought that a ; was omitted before the ), so it imagined we had:

while (Var + Var); Var--; ) {}

Then it saw a statement beginning with a ), and decided to skip to the next statement, which would skip past the ), the {, the }, and whatever statement you put next in the function.

Maybe we should parse as if an init-statement were permitted on a while loop (and produce an error after the fact if there's one present). But that should be done in a separate patch if you're interested in working on it.

Rakete1111 updated this revision to Diff 128729.Jan 5 2018, 3:53 AM

Addressed review comments. I'll write up a patch in the next coming days then :)

Rakete1111 marked 2 inline comments as done.Jan 5 2018, 3:54 AM

Rebase and ping :)

rsmith accepted this revision.Mar 14 2018, 6:53 AM

Thanks, LGTM

This revision is now accepted and ready to land.Mar 14 2018, 6:53 AM

@rsmith Can you commit please?

lichray edited the summary of this revision. (Show Details)Mar 17 2018, 2:41 PM
This revision was automatically updated to reflect the committed changes.