[cfe-commits] r171754 - in /cfe/trunk: lib/Format/Format.cpp unittests/Format/FormatTest.cpp

Nico Weber nicolasweber at gmx.de
Mon Jan 7 08:36:17 PST 2013


Author: nico
Date: Mon Jan  7 10:36:17 2013
New Revision: 171754

URL: http://llvm.org/viewvc/llvm-project?rev=171754&view=rev
Log:
Formatter: Add tests for try/catch. Let 'throw' start an expression.

Before:
  throw a *b;

Now:
  throw a * b;


Modified:
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=171754&r1=171753&r2=171754&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Mon Jan  7 10:36:17 2013
@@ -808,9 +808,8 @@
       TokenAnnotation &Annotation = Annotations[i];
       const FormatToken &Tok = Line.Tokens[i];
 
-      if (getPrecedence(Tok) == prec::Assignment)
-        IsRHS = true;
-      else if (Tok.Tok.is(tok::kw_return))
+      if (getPrecedence(Tok) == prec::Assignment ||
+          Tok.Tok.is(tok::kw_return) || Tok.Tok.is(tok::kw_throw))
         IsRHS = true;
 
       if (Annotation.Type != TT_Unknown)

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=171754&r1=171753&r2=171754&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Jan  7 10:36:17 2013
@@ -377,6 +377,46 @@
                "}");
 }
 
+TEST_F(FormatTest, FormatTryCatch) {
+  verifyFormat("try {\n"
+               "  throw a * b;\n"
+               "}\n"
+               "catch (int a) {\n"
+               "  // Do nothing.\n"
+               "}\n"
+               "catch (...) {\n"
+               "  exit(42);\n"
+               "}");
+
+  // Function-level try statements.
+  verifyFormat("int f() try {\n"
+               "  return 4;\n"
+               "}\n"
+               "catch (...) {\n"
+               "  return 5;\n"
+               "}");
+  verifyFormat("class A {\n"
+               "  int a;\n"
+               "  A() try : a(0) {\n"
+               "  }\n"
+               "  catch (...) {\n"
+               "    throw;\n"
+               "  }\n"
+               "};\n");
+}
+
+TEST_F(FormatTest, FormatObjCTryCatch) {
+  verifyFormat("@try {\n"
+               "  f();\n"
+               "}\n"
+               "@catch (NSException e) {\n"
+               "  @throw;\n"
+               "}\n"
+               "@finally {\n"
+               "  exit(42);\n"
+               "}");
+}
+
 TEST_F(FormatTest, StaticInitializers) {
   verifyFormat("static SomeClass SC = { 1, 'a' };");
 





More information about the cfe-commits mailing list