[PATCH] D71239: [clang][Format] Fix ObjC keywords following try/catch getting split.

Michael Spencer via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 9 17:54:28 PST 2019


Bigcheese created this revision.
Bigcheese added reviewers: thakis, djasper.
Bigcheese added a project: clang-format.
Herald added a subscriber: dexonsmith.
Herald added a project: clang.

Previously things like:

  int main() {
    @try {
    } @catch (NSException *e) {
    }
  
    @try {
    } @catch (NSException *e) {
    }
  }

Would be formatted like:

  int main() {
    @try {
    } @catch (NSException *e) {
    }
  
    @
    try {
    } @catch (NSException *e) {
    }
  }

because `UnwrappedLineParser::parseTryCatch()` would consume the `@` as
part of checking for another `catch` or `finally`. This patch fixes that
by doing a lookahead.

I'm not super happy about the way the lookhead works, but this is the only thing that worked.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D71239

Files:
  clang/lib/Format/UnwrappedLineParser.cpp
  clang/unittests/Format/FormatTestObjC.cpp


Index: clang/unittests/Format/FormatTestObjC.cpp
===================================================================
--- clang/unittests/Format/FormatTestObjC.cpp
+++ clang/unittests/Format/FormatTestObjC.cpp
@@ -191,7 +191,13 @@
                "  @throw;\n"
                "} @finally {\n"
                "  exit(42);\n"
-               "}");
+               "}\n"
+               "@try {\n"
+               "} @catch (NSException *e) {\n"
+               "}\n"
+               "@ /* adena */ try {\n"
+               "} @catch (NSException *e) {\n"
+               "}\n");
   verifyFormat("DEBUG({\n"
                "  @try {\n"
                "  } @finally {\n"
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -1840,16 +1840,27 @@
     --Line->Level;
   }
   while (1) {
-    if (FormatTok->is(tok::at))
-      nextToken();
+    unsigned StoredPosition = Tokens->getPosition();
+    if (FormatTok->is(tok::at)) {
+      // Get next non-comment token.
+      do {
+        FormatTok = Tokens->getNextToken();
+      } while (FormatTok->is(tok::comment));
+    }
     if (!(FormatTok->isOneOf(tok::kw_catch, Keywords.kw___except,
                              tok::kw___finally) ||
           ((Style.Language == FormatStyle::LK_Java ||
             Style.Language == FormatStyle::LK_JavaScript) &&
            FormatTok->is(Keywords.kw_finally)) ||
           (FormatTok->Tok.isObjCAtKeyword(tok::objc_catch) ||
-           FormatTok->Tok.isObjCAtKeyword(tok::objc_finally))))
+           FormatTok->Tok.isObjCAtKeyword(tok::objc_finally)))) {
+      // Go back to before the (optional) @.
+      FormatTok = Tokens->setPosition(StoredPosition);
       break;
+    }
+    FormatTok = Tokens->setPosition(StoredPosition);
+    if (FormatTok->is(tok::at))
+      nextToken();
     nextToken();
     while (FormatTok->isNot(tok::l_brace)) {
       if (FormatTok->is(tok::l_paren)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71239.232981.patch
Type: text/x-patch
Size: 2048 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191210/460bc657/attachment.bin>


More information about the cfe-commits mailing list