Fix to PR19022 as well as some other problems of error recovery in switch statement.
Details
Diff Detail
Event Timeline
lib/Parse/ParseStmt.cpp | ||
---|---|---|
701–705 | This doesn't look right in the case where ColonLoc is invalid. AfterColonLoc will point to the *start* of the previous token in this case, and we'll provide a fix-it inserting a semicolon in a fairly random place. In the case where ColonLoc is invalid, we've already hit at least one error, so it seems better to suppress the follow-on diagnostic here. I think this will also do the wrong thing if we have a sequence of case statements, and the last one is missing a colon or is otherwise malformed; we'll use the last ColonLoc for any of the case statements we succeeded in handling. Maybe reset ColonLoc to SourceLocation() each time around the do loop to fix this? | |
lib/Sema/SemaStmt.cpp | ||
707–708 | This comment doesn't explain what's happening here. Something like: // This happens if the body was ill-formed. Synthesize a null statement at the end of the switch condition. ... would be better. But our normal approach here would be to simply return StmtError() in this case. Any reason not to do that? |
Thank you for review.
lib/Parse/ParseStmt.cpp | ||
---|---|---|
701–705 | Yes, this is complicated point, thank you for the detailed explanation. It looks like there is no reliable way to determine location for error message if ColonLoc is undefined due to previous error. Suppressing follow-on diagnostic gives better results. Resetting ColonLoc at the beginning of case label is also necessary. | |
lib/Sema/SemaStmt.cpp | ||
707–708 | It looks like preparing bogus statement body is not necessary, StmtError is enough. |
Thank you for review!
And special thanks for language corrections.
2014-05-21 8:07 GMT+07:00 Richard Smith <richard@metafoo.co.uk>:
Looks fine with some tiny fixes.
Comment at: lib/Parse/ParseStmt.cpp:706
@@ +705,3 @@
+ not valid. If ColonLoc doesn't point to valid text location,
there was
+ another parsing error, so don't produce extra diagnostic.+ if (ColonLoc.isValid()) {
"a valid text location"
"an extra diagnostic" or "extra diagnostics"?Comment at: lib/Sema/SemaStmt.cpp:705-706
@@ -704,2 +704,4 @@
- SS->setBody(BodyStmt, SwitchLoc);
+ Stmt *Body = BodyStmt;
+ if (!Body) return StmtError();+ SS->setBody(Body, SwitchLoc);
Why the extra variable?
This doesn't look right in the case where ColonLoc is invalid. AfterColonLoc will point to the *start* of the previous token in this case, and we'll provide a fix-it inserting a semicolon in a fairly random place.
In the case where ColonLoc is invalid, we've already hit at least one error, so it seems better to suppress the follow-on diagnostic here.
I think this will also do the wrong thing if we have a sequence of case statements, and the last one is missing a colon or is otherwise malformed; we'll use the last ColonLoc for any of the case statements we succeeded in handling. Maybe reset ColonLoc to SourceLocation() each time around the do loop to fix this?