[PATCH] clang-format properly handles try/catch blocks

Alexander Rojas alexander.rojas at gmail.com
Wed Jan 22 05:31:40 PST 2014



================
Comment at: lib/Format/UnwrappedLineParser.cpp:1076
@@ +1075,3 @@
+  while (FormatTok->Tok.is(tok::kw_catch)) {
+    nextToken();
+    if (FormatTok->Tok.is(tok::l_paren))
----------------
Manuel Klimek wrote:
> Same here. The problem is if anybody ever finds a real use case to put anything in between (including macros) we might fail to parse the file...
I don't get what you mean here. If the token is not tok::kw_catch, it won't start the while and NeedsUnwrappedLine will be true. Is there any other case that needs consideration?

================
Comment at: lib/Format/UnwrappedLineParser.cpp:1042
@@ +1041,3 @@
+  bool NeedsUnwrappedLine = false;
+  if (FormatTok->Tok.is(tok::colon)) {
+      // We are in a function try block, what comes is an initializer list
----------------
Manuel Klimek wrote:
> I'm wondering whether we just want to eat everything up to the next '{'. In general, we try to parse as little as possible in the structural parser.
> 
> If we decide we really need to parse the structure here, I think we should pull out a parseInitializerList() method.
I've been thinking on how to parse the initializer list, and it might be quite cumbersome. I've been thinking on leaving out of this patch the ability of dealing with function-try blocks and leave it for latter (since I do this as a hobby, there's really a limit on how much time I can spend on this).

================
Comment at: lib/Format/UnwrappedLineParser.cpp:1075-1091
@@ +1074,19 @@
+  }
+  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 ||
+          Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
+        addUnwrappedLine();
+      } else {
+        NeedsUnwrappedLine = true;
+      }
+    }
+  }
+  if (NeedsUnwrappedLine) {
----------------
>From Daniel: This needs a test case with multiple catch blocks.

================
Comment at: lib/Format/UnwrappedLineParser.cpp:1043
@@ +1042,3 @@
+  if (FormatTok->Tok.is(tok::colon)) {
+      // We are in a function try block, what comes is an initializer list
+      nextToken();
----------------
Manuel Klimek wrote:
> Nit: add '.'.
Sorry, but what do you mean by “Nit”?


http://llvm-reviews.chandlerc.com/D2555



More information about the cfe-commits mailing list