]> granicus.if.org Git - clang/commitdiff
clang-format: Fix braced list detection.
authorDaniel Jasper <djasper@google.com>
Thu, 22 May 2014 12:46:38 +0000 (12:46 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 22 May 2014 12:46:38 +0000 (12:46 +0000)
Before:
  static_assert(std::is_integral<int> {} + 0, "");
  int a = std::is_integral<int> {}
  + 0;

After:
  static_assert(std::is_integral<int>{} + 0, "");
  int a = std::is_integral<int>{} + 0;

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

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

index e0106d94cf8f4b64e79d5ef9ff36cf941bee536a..d0750af1b0fa4e11590d5bc86c8e10a24300ed6f 100644 (file)
@@ -338,6 +338,11 @@ void UnwrappedLineParser::calculateBraceTypes() {
           if (Style.Language == FormatStyle::LK_Proto) {
             ProbablyBracedList = NextTok->isOneOf(tok::comma, tok::r_square);
           } else {
+            // Using OriginalColumn to distinguish between ObjC methods and
+            // binary operators is a bit hacky.
+            bool NextIsObjCMethod = NextTok->isOneOf(tok::plus, tok::minus) &&
+                                    NextTok->OriginalColumn == 0;
+
             // If there is a comma, semicolon or right paren after the closing
             // brace, we assume this is a braced initializer list.  Note that
             // regardless how we mark inner braces here, we will overwrite the
@@ -350,8 +355,7 @@ void UnwrappedLineParser::calculateBraceTypes() {
                 NextTok->isOneOf(tok::comma, tok::semi, tok::period, tok::colon,
                                  tok::r_paren, tok::r_square, tok::l_brace,
                                  tok::l_paren) ||
-                (NextTok->isBinaryOperator() &&
-                 !NextTok->isOneOf(tok::plus, tok::minus));
+                (NextTok->isBinaryOperator() && !NextIsObjCMethod);
           }
           if (ProbablyBracedList) {
             Tok->BlockKind = BK_BracedInit;
index f9016f4bf1a9ed17b32bd87e0996e85c248c8739..729eeaf4399e17f204933439075357d4ed72cb22 100644 (file)
@@ -5262,6 +5262,8 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
                "  T member = {arg1, arg2};\n"
                "};");
   verifyFormat("vector<int> foo = {::SomeGlobalFunction()};");
+  verifyFormat("static_assert(std::is_integral<int>{} + 0, \"\");");
+  verifyFormat("int a = std::is_integral<int>{} + 0;");
 
   verifyFormat("int foo(int i) { return fo1{}(i); }");
   verifyFormat("int foo(int i) { return fo1{}(i); }");
@@ -6088,10 +6090,14 @@ TEST_F(FormatTest, FormatObjCImplementation) {
                "@implementation Bar\n"
                "@end");
 
-  verifyFormat("@implementation Foo : Bar\n"
-               "+ (id)init {\n}\n"
-               "- (void)foo {\n}\n"
-               "@end");
+  EXPECT_EQ("@implementation Foo : Bar\n"
+            "+ (id)init {\n}\n"
+            "- (void)foo {\n}\n"
+            "@end",
+            format("@implementation Foo : Bar\n"
+                   "+(id)init{}\n"
+                   "-(void)foo{}\n"
+                   "@end"));
 
   verifyFormat("@implementation Foo {\n"
                "  int _i;\n"