Understand unary operators after "return" and "case".
authorDaniel Jasper <djasper@google.com>
Wed, 2 Jan 2013 15:26:16 +0000 (15:26 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 2 Jan 2013 15:26:16 +0000 (15:26 +0000)
This fixes llvm.org/PR14746.

Before: return - 1;
After:  return -1;
llvm-svn: 171389

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

index d354078..c08bcf4 100644 (file)
@@ -852,7 +852,8 @@ private:
     const Token &PreviousTok = Line.Tokens[Index - 1].Tok;
     if (PreviousTok.is(tok::equal) || PreviousTok.is(tok::l_paren) ||
         PreviousTok.is(tok::comma) || PreviousTok.is(tok::l_square) ||
-        PreviousTok.is(tok::question) || PreviousTok.is(tok::colon))
+        PreviousTok.is(tok::question) || PreviousTok.is(tok::colon) ||
+        PreviousTok.is(tok::kw_return) || PreviousTok.is(tok::kw_case))
       return TokenAnnotation::TT_UnaryOperator;
 
     // There can't be to consecutive binary operators.
index 58712ef..22da93e 100644 (file)
@@ -646,6 +646,12 @@ TEST_F(FormatTest, UnderstandsUnaryOperators) {
   verifyFormat("b ? -a : c;");
   verifyFormat("n * sizeof char16;");
   verifyFormat("sizeof(char);");
+
+  verifyFormat("return -1;");
+  verifyFormat("switch (a) {\n"
+               "case -1:\n"
+               "  break;\n"
+               "}");
 }
 
 TEST_F(FormatTest, UndestandsOverloadedOperators) {