]> granicus.if.org Git - clang/commitdiff
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;

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

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

index d354078231cb936ed183eadfaa96dd547c6ed10d..c08bcf4f5c109a66619c2aac555af846731188ef 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 58712ef38fbd4358b9ff77240204fa2a251e59b5..22da93ebe3c1907b90ea822c20f1f191e6a625af 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) {