[PATCH] Formatter completely ignores try/catch blocks

Diego Alexander Rojas alexander.rojas at gmail.com
Thu Jan 9 10:41:08 PST 2014


The attached patch allows clang-formatter to properly format try catch blocks.

Before the patch there was no proper way of dealing with try/catch
blocks so the braces occurring after the keyword were considered as
compound-statements and a new line was always inserted.

Even though it works for the traditional try/catch blocks, It still
has issues when dealing with function-try-block's


--
Diego Alexander Rojas Páez

E-Mail:     alexander.rojas at gmail.com
Mobile:     +49 (151) 5249 5452
Address:  Stefan-George-Ring 46
               81929, Munich
               Germany

-------------- next part --------------
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp (revision 198773)
+++ lib/Format/UnwrappedLineParser.cpp (working copy)
@@ -621,6 +621,9 @@
   case tok::kw_private:
     parseAccessSpecifier();
     return;
+  case tok::kw_try:
+    parseTryCatch();
+    return;
   case tok::kw_if:
     parseIfThenElse();
     return;
@@ -1028,6 +1031,50 @@
   }
 }

+void UnwrappedLineParser::parseTryCatch() {
+  assert(FormatTok->Tok.is(tok::kw_try) && "'try' expected");
+  nextToken();
+  bool NeedsUnwrappedLine = false;
+  if (FormatTok->Tok.is(tok::l_brace)) {
+    CompoundStatementIndenter Indenter(this, Style, Line->Level);
+    parseBlock(/*MustBeDeclaration=*/false);
+    if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
+        Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
+      addUnwrappedLine();
+    } else {
+      NeedsUnwrappedLine = true;
+    }
+  } else if (!FormatTok->Tok.is(tok::kw_catch)) {
+    // The C++ standard requires a compound-statement after a try.
+    // If there's none, we try to assume there's a structuralElement
+    // and try to continue.
+    StructuralError = true;
+    addUnwrappedLine();
+    ++Line->Level;
+    parseStructuralElement();
+    --Line->Level;
+  }
+  while (FormatTok->Tok.is(tok::kw_catch)) {
+    nextToken();
+    if (FormatTok->Tok.is(tok::l_paren))
+      parseParens();
+    NeedsUnwrappedLine = false;
+    if (FormatTok->Tok.is(tok::l_brace)) {
+      CompoundStatementIndenter Indenter(this, Style, Line->Level);
+      parseBlock(/*MustBeDeclaration=*/false);
+      if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
+          Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
+        addUnwrappedLine();
+      } else {
+        NeedsUnwrappedLine = true;
+      }
+    }
+  }
+  if (NeedsUnwrappedLine) {
+    addUnwrappedLine();
+  }
+}
+
 void UnwrappedLineParser::parseNamespace() {
   assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
   nextToken();
Index: lib/Format/UnwrappedLineParser.h
===================================================================
--- lib/Format/UnwrappedLineParser.h (revision 198773)
+++ lib/Format/UnwrappedLineParser.h (working copy)
@@ -85,6 +85,7 @@
   void parseReturn();
   void parseParens();
   void parseSquare();
+  void parseTryCatch();
   void parseIfThenElse();
   void parseForOrWhileLoop();
   void parseDoWhile();




More information about the cfe-commits mailing list