]> granicus.if.org Git - clang/commitdiff
Improve error recovery when we see ':' and expect a ';'.
authorJohn McCall <rjmccall@apple.com>
Tue, 7 Sep 2010 18:31:03 +0000 (18:31 +0000)
committerJohn McCall <rjmccall@apple.com>
Tue, 7 Sep 2010 18:31:03 +0000 (18:31 +0000)
I, at least, make this typo all the time.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@113243 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Parse/Parser.cpp
test/Parser/objc-interfaces.m
test/Parser/recovery.c

index ca06ba72f64713948707365643292ad253c0f080..02802773a031635dfae25cbaceb24bb78a795978 100644 (file)
@@ -133,6 +133,13 @@ SourceLocation Parser::MatchRHSPunctuation(tok::TokenKind RHSTok,
   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 @@ bool Parser::ExpectAndConsume(tok::TokenKind ExpectedTok, unsigned DiagID,
     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() &&
index aac3faa4350a66d19d808a2e394f5a378a57177c..0ae17f15ee4d4d771fdedb8d87140ccd6b5ee03f 100644 (file)
@@ -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
 
index 6cd95da878d2e44ac119af4f37ecc4d0bd3c5852..1b33f0225bcea923e3ad6df5e0d74daf1e0082cb 100644 (file)
@@ -78,3 +78,9 @@ void foo() {
 // 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;
+}