[cfe-commits] r113243 - in /cfe/trunk: lib/Parse/Parser.cpp test/Parser/objc-interfaces.m test/Parser/recovery.c

John McCall rjmccall at apple.com
Tue Sep 7 11:31:03 PDT 2010


Author: rjmccall
Date: Tue Sep  7 13:31:03 2010
New Revision: 113243

URL: http://llvm.org/viewvc/llvm-project?rev=113243&view=rev
Log:
Improve error recovery when we see ':' and expect a ';'.
I, at least, make this typo all the time.


Modified:
    cfe/trunk/lib/Parse/Parser.cpp
    cfe/trunk/test/Parser/objc-interfaces.m
    cfe/trunk/test/Parser/recovery.c

Modified: cfe/trunk/lib/Parse/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/Parser.cpp?rev=113243&r1=113242&r2=113243&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/Parser.cpp (original)
+++ cfe/trunk/lib/Parse/Parser.cpp Tue Sep  7 13:31:03 2010
@@ -133,6 +133,13 @@
   return R;
 }
 
+static bool IsCommonTypo(tok::TokenKind ExpectedTok, const Token &Tok) {
+  switch (ExpectedTok) {
+  case tok::semi: return Tok.is(tok::colon); // : for ;
+  default: return false;
+  }
+}
+
 /// ExpectAndConsume - The parser expects that 'ExpectedTok' is next in the
 /// input.  If so, it is consumed and false is returned.
 ///
@@ -146,6 +153,19 @@
     return false;
   }
 
+  // Detect common single-character typos and resume.
+  if (IsCommonTypo(ExpectedTok, Tok)) {
+    SourceLocation Loc = Tok.getLocation();
+    Diag(Loc, DiagID)
+      << Msg
+      << FixItHint::CreateReplacement(SourceRange(Loc),
+                                      getTokenSimpleSpelling(ExpectedTok));
+    ConsumeAnyToken();
+
+    // Pretend there wasn't a problem.
+    return false;
+  }
+
   const char *Spelling = 0;
   SourceLocation EndLoc = PP.getLocForEndOfToken(PrevTokLocation);
   if (EndLoc.isValid() &&

Modified: cfe/trunk/test/Parser/objc-interfaces.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/objc-interfaces.m?rev=113243&r1=113242&r2=113243&view=diff
==============================================================================
--- cfe/trunk/test/Parser/objc-interfaces.m (original)
+++ cfe/trunk/test/Parser/objc-interfaces.m Tue Sep  7 13:31:03 2010
@@ -3,6 +3,6 @@
 // Test features and error recovery for objc interfaces.
 
 @interface INTF
-- (int*) foo2  __attribute__((deprecated)) : (int) x1 __attribute__((deprecated)); // expected-error {{expected ';' after method prototype}}
+- (int*) foo2  __attribute__((deprecated)) : (int) x1 __attribute__((deprecated)); // expected-error {{expected ';' after method prototype}} expected-error {{method type specifier must start with '-' or '+'}}
 @end
 

Modified: cfe/trunk/test/Parser/recovery.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/recovery.c?rev=113243&r1=113242&r2=113243&view=diff
==============================================================================
--- cfe/trunk/test/Parser/recovery.c (original)
+++ cfe/trunk/test/Parser/recovery.c Tue Sep  7 13:31:03 2010
@@ -78,3 +78,9 @@
 // rdar://7980651
 typedef int intptr_t;  // expected-note {{'intptr_t' declared here}}
 void bar(intptr y);     // expected-error {{unknown type name 'intptr'; did you mean 'intptr_t'?}}
+
+void test1(void) {
+  int x = 2: // expected-error {{expected ';' at end of declaration}}
+  int y = x;
+  int z = y;
+}





More information about the cfe-commits mailing list