Here is an implementation of a feature 'if (init; condition)', proposed in p0305r0 and accepted at the last meeting in Oulu:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0305r0.html
Details
- Reviewers
mclow.lists rsmith
Diff Detail
Event Timeline
The proposal has changed since the pre-meeting mailing, and now covers switch as well as if. You also need to handle the expression-statement form of init-statement.
As it happens, I've also been working on this proposal, and the bit that you're getting wrong here (the parsing side) is the part that I've finished. I've committed that as r274169; if you rebase your Sema changes on top of that (and add support for switch), we can get this change committed to finish off the implementation.
We'll also need some tests before this can be committed.
Thank you!
lib/Parse/ParseStmt.cpp | ||
---|---|---|
1062–1075 ↗ | (On Diff #62197) | We should try to only walk over the tokens once to identify whether this is a condition declaration, a simple-declaration of an init-statement, or an expression. |
lib/Parse/ParseTentative.cpp | ||
77–79 ↗ | (On Diff #62197) | This is not correct; TryParseSimpleDeclaration usually stops long before it reaches the end of the simple-declaration, so this will give false negatives in lots of cases. |
This is not correct; TryParseSimpleDeclaration usually stops long before it reaches the end of the simple-declaration, so this will give false negatives in lots of cases.
Yes, I noticed that, that's why I changed use of TryParseSimpleDeclaration to something easier in last revision.
Thanks for reply, Richard. The revision you sent looks great, I should have updated my working copy before :)
Anyways, there are not too much of changes to Sema/AST stuff, I just applied them on top of yours, added the same for SwitchStmt. I'll update the test you put according to my changes tomorrow.
There are a few missing pieces here:
- Analysis/CFG.cpp needs to be taught to build a correct CFG for these. You can test this with an example like 'if (bool b; b)' which should give a -Wuninitialized warning.
- AST/ExprConstant.cpp needs to be taught to perform constant evaluation for these properly.
- More test coverage. In addition for test cases for the above two cases, please also add some tests to test/CodeGenCXX to test that we emit correct code for the expression and declaration form of these constructs, for both if and switch, and add a test that checks that the extra statement round-trips through an AST file properly (see test/PCH for some examples).
@rsmith,
Thanks for the comments, I've addressed them (except for tests, they are to be added asap). I'm especially not sure about changes in CFG.cpp, could you verify please?
@rsmith,
I've added some tests for c++1z init statement. Please let me know if there is anything else I should add or change.
This looks really good. Some minor comments then this is ready to commit.
include/clang/AST/Stmt.h | ||
---|---|---|
890 | * on the right, please (you can run clang-format over the patch to handle minor details like this). | |
lib/CodeGen/CGStmt.cpp | ||
565 | Too much indentation here. | |
lib/Sema/SemaStmt.cpp | ||
512 | Please also delete this diagnostic from include/clang/Basic/DiagnosticSemaKinds.td. | |
test/CodeGenCXX/cxx1z-init-statement.cpp | ||
2 | Probably best to run this with -std=c++1z, but it's not a big deal. | |
6 | Please use a placeholder for the SSA variables here: %[[A:.*]] = alloca i32 store i32 5, i32* %[[A]] ... so that this test doesn't depend on whether we generate names for these values or not (debug and release builds of Clang differ here in some cases, and it's not a salient part of the test). | |
test/PCH/cxx1z-init-statement.cpp | ||
9–10 | Maybe change this test to use constexpr and static_assert so that we can test that the behavior is correct, not just that it doesn't crash. | |
test/SemaCXX/cxx1z-init-statement-warn-unused.cpp | ||
7 | Maybe add a test like this: int a; if (a = 0; a) {} ... to make sure we actually visit the init-statement. |
* on the right, please (you can run clang-format over the patch to handle minor details like this).