r288334 - [ObjC] Avoid a @try/@finally/@autoreleasepool fixit when parsing an expression

Alex Lorenz via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 1 04:14:39 PST 2016


Author: arphaman
Date: Thu Dec  1 06:14:38 2016
New Revision: 288334

URL: http://llvm.org/viewvc/llvm-project?rev=288334&view=rev
Log:
[ObjC] Avoid a @try/@finally/@autoreleasepool fixit when parsing an expression

This patch ensures that the typo fixit for the @try/@finally/@autoreleasepool {}
directive is shown only when we're parsing an actual statement where such
directives can actually be present.

rdar://19669565

Differential Revision: https://reviews.llvm.org/D26916

Added:
    cfe/trunk/test/Parser/objc-at-directive-fixit.m
Modified:
    cfe/trunk/lib/Parse/ParseObjc.cpp

Modified: cfe/trunk/lib/Parse/ParseObjc.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseObjc.cpp?rev=288334&r1=288333&r2=288334&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseObjc.cpp (original)
+++ cfe/trunk/lib/Parse/ParseObjc.cpp Thu Dec  1 06:14:38 2016
@@ -2773,6 +2773,7 @@ StmtResult Parser::ParseObjCAtStatement(
     return Actions.ActOnNullStmt(Tok.getLocation());
   }
 
+  ExprStatementTokLoc = AtLoc;
   ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
   if (Res.isInvalid()) {
     // If the expression is invalid, skip ahead to the next semicolon. Not
@@ -2869,7 +2870,11 @@ ExprResult Parser::ParseObjCAtExpression
       return ParseAvailabilityCheckExpr(AtLoc);
       default: {
         const char *str = nullptr;
-        if (GetLookAheadToken(1).is(tok::l_brace)) {
+        // Only provide the @try/@finally/@autoreleasepool fixit when we're sure
+        // that this is a proper statement where such directives could actually
+        // occur.
+        if (GetLookAheadToken(1).is(tok::l_brace) &&
+            ExprStatementTokLoc == AtLoc) {
           char ch = Tok.getIdentifierInfo()->getNameStart()[0];
           str =  
             ch == 't' ? "try" 

Added: cfe/trunk/test/Parser/objc-at-directive-fixit.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/objc-at-directive-fixit.m?rev=288334&view=auto
==============================================================================
--- cfe/trunk/test/Parser/objc-at-directive-fixit.m (added)
+++ cfe/trunk/test/Parser/objc-at-directive-fixit.m Thu Dec  1 06:14:38 2016
@@ -0,0 +1,28 @@
+// RUN: %clang_cc1 -fsyntax-only -triple x86_64-apple-macosx10.10.0 -verify -fobjc-exceptions %s
+// RUN: not %clang_cc1 -fsyntax-only -triple x86_64-apple-macosx10.10.0 -fdiagnostics-parseable-fixits -fobjc-exceptions %s 2>&1 | FileCheck %s
+
+// rdar://19669565
+
+void bar(int x);
+
+void f() {
+  @try { }
+  @finally { }
+  @autoreleasepool { }
+
+  // Provide a fixit when we are parsing a standalone statement
+  @tr { }; // expected-error {{unexpected '@' in program}}
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:4-[[@LINE-1]]:6}:"try"
+  @finaly { }; // expected-error {{unexpected '@' in program}}
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:4-[[@LINE-1]]:10}:"finally"
+  @autorelpool { }; // expected-error {{unexpected '@' in program}}
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:4-[[@LINE-1]]:15}:"autoreleasepool"
+
+  // Ensure that no fixit is given when parsing expressions
+  // CHECK-NOT: fix-it
+  id thing = @autoreleasepool { }; // expected-error {{unexpected '@' in program}}
+  (void)@tr { }; // expected-error {{unexpected '@' in program}}
+  bar(@final { }); // expected-error {{unexpected '@' in program}}
+  for(@auto;;) { } // expected-error {{unexpected '@' in program}}
+  [@try]; // expected-error {{unexpected '@' in program}}
+}




More information about the cfe-commits mailing list