]> granicus.if.org Git - clang/commitdiff
Formatter: Add tests for try/catch. Let 'throw' start an expression.
authorNico Weber <nicolasweber@gmx.de>
Mon, 7 Jan 2013 16:36:17 +0000 (16:36 +0000)
committerNico Weber <nicolasweber@gmx.de>
Mon, 7 Jan 2013 16:36:17 +0000 (16:36 +0000)
Before:
  throw a *b;

Now:
  throw a * b;

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@171754 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp
unittests/Format/FormatTest.cpp

index 8faa93cdc7c9f80df7dee42b237b63d3c3fd6133..7a6b6e2081b836ae4c43258c449307ad879bc2ef 100644 (file)
@@ -808,9 +808,8 @@ private:
       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)
index 17e98741fd9059bb6af42d85b186fffdae0fc97e..d4490dddfc316880ef8539dd80c94a9870366f9f 100644 (file)
@@ -377,6 +377,46 @@ TEST_F(FormatTest, FormatsNamespaces) {
                "}");
 }
 
+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' };");