[PATCH] D21834: Implementing 'If statement with Initializer'

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 29 14:27:31 PDT 2016


rsmith requested changes to this revision.
rsmith added a comment.
This revision now requires changes to proceed.

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!


================
Comment at: lib/Parse/ParseStmt.cpp:1062-1075
@@ -1060,1 +1061,16 @@
 
+  if (CK == Sema::ConditionKind::IfWithInit &&
+      isForInitDeclarationWithSemi()) {  // if (int X = 4;
+    // Parse declaration, which eats the ';'.
+    ParsedAttributesWithRange attrs(AttrFactory);
+    MaybeParseCXX11Attributes(attrs);
+
+    SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
+    DeclGroupPtrTy DG = ParseSimpleDeclaration(
+      Declarator::ForContext, DeclEnd, attrs, false, nullptr);
+    StmtResult InitStmt = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
+    if (Init) *Init = InitStmt.get();
+
+    ConsumeToken(); // Consume semi
+  }
+
----------------
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//.

================
Comment at: lib/Parse/ParseTentative.cpp:77-79
@@ +76,5 @@
+  TentativeParsingAction PA(*this);
+  TPResult TPR = TryParseSimpleDeclaration(false);
+  if (Tok.isNot(tok::semi))
+    TPR = TPResult::False;
+  PA.Revert();
----------------
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.


http://reviews.llvm.org/D21834





More information about the cfe-commits mailing list