This is an archive of the discontinued LLVM Phabricator instance.

Recovery of switch statement
ClosedPublic

Authored by sepavloff on Mar 20 2014, 10:58 AM.

Details

Summary

Fix to PR19022 as well as some other problems of error recovery in switch statement.

Diff Detail

Event Timeline

sepavloff updated this revision to Unknown Object (????).Apr 6 2014, 9:00 PM

Updated patch.

Added comments, tests, handle more cases.
Please review.

Thank you.

rsmith added inline comments.Apr 11 2014, 1:52 PM
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.

sepavloff updated this revision to Unknown Object (????).Apr 22 2014, 12:28 AM

Updated patch.

rsmith accepted this revision.May 20 2014, 6:07 PM
rsmith added a reviewer: rsmith.

Looks fine with some tiny fixes.

lib/Parse/ParseStmt.cpp
707

"a valid text location"
"an extra diagnostic" or "extra diagnostics"?

lib/Sema/SemaStmt.cpp
705–718

Why the extra variable?

This revision is now accepted and ready to land.May 20 2014, 6:07 PM
sepavloff closed this revision.May 21 2014, 7:56 AM
sepavloff updated this revision to Diff 9661.

Closed by commit rL209302 (authored by @sepavloff).

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?

http://reviews.llvm.org/D3137