diff options
Diffstat (limited to 'unittests/Format/FormatTestObjC.cpp')
-rw-r--r-- | unittests/Format/FormatTestObjC.cpp | 74 |
1 files changed, 70 insertions, 4 deletions
diff --git a/unittests/Format/FormatTestObjC.cpp b/unittests/Format/FormatTestObjC.cpp index a417b6710d..b859d92a89 100644 --- a/unittests/Format/FormatTestObjC.cpp +++ b/unittests/Format/FormatTestObjC.cpp @@ -1,9 +1,8 @@ //===- unittest/Format/FormatTestObjC.cpp - Formatting unit tests----------===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// @@ -166,6 +165,20 @@ TEST(FormatTestObjCStyle, DetectsObjCInHeaders) { EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); } +TEST(FormatTestObjCStyle, AvoidDetectingDesignatedInitializersAsObjCInHeaders) { + auto Style = getStyle("LLVM", "a.h", "none", + "static const char *names[] = {[0] = \"foo\",\n" + "[kBar] = \"bar\"};"); + ASSERT_TRUE((bool)Style); + EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language); + + Style = getStyle("LLVM", "a.h", "none", + "static const char *names[] = {[0] EQ \"foo\",\n" + "[kBar] EQ \"bar\"};"); + ASSERT_TRUE((bool)Style); + EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language); +} + TEST_F(FormatTestObjC, FormatObjCTryCatch) { verifyFormat("@try {\n" " f();\n" @@ -598,6 +611,7 @@ TEST_F(FormatTestObjC, FormatObjCMethodDeclarations) { TEST_F(FormatTestObjC, FormatObjCMethodExpr) { verifyFormat("[foo bar:baz];"); + verifyFormat("[foo bar]->baz;"); verifyFormat("return [foo bar:baz];"); verifyFormat("return (a)[foo bar:baz];"); verifyFormat("f([foo bar:baz]);"); @@ -1315,6 +1329,58 @@ TEST_F(FormatTestObjC, AlwaysBreakBeforeMultilineStrings) { " @\"fffff\"];"); } +TEST_F(FormatTestObjC, DisambiguatesCallsFromCppLambdas) { + verifyFormat("x = ([a foo:bar] && b->c == 'd');"); + verifyFormat("x = ([a foo:bar] + b->c == 'd');"); + verifyFormat("x = ([a foo:bar] + !b->c == 'd');"); + verifyFormat("x = ([a foo:bar] + ~b->c == 'd');"); + verifyFormat("x = ([a foo:bar] - b->c == 'd');"); + verifyFormat("x = ([a foo:bar] / b->c == 'd');"); + verifyFormat("x = ([a foo:bar] % b->c == 'd');"); + verifyFormat("x = ([a foo:bar] | b->c == 'd');"); + verifyFormat("x = ([a foo:bar] || b->c == 'd');"); + verifyFormat("x = ([a foo:bar] && b->c == 'd');"); + verifyFormat("x = ([a foo:bar] == b->c == 'd');"); + verifyFormat("x = ([a foo:bar] != b->c == 'd');"); + verifyFormat("x = ([a foo:bar] <= b->c == 'd');"); + verifyFormat("x = ([a foo:bar] >= b->c == 'd');"); + verifyFormat("x = ([a foo:bar] << b->c == 'd');"); + verifyFormat("x = ([a foo:bar] ? b->c == 'd' : 'e');"); + // FIXME: The following are wrongly classified as C++ lambda expressions. + // For example this code: + // x = ([a foo:bar] & b->c == 'd'); + // is formatted as: + // x = ([a foo:bar] & b -> c == 'd'); + // verifyFormat("x = ([a foo:bar] & b->c == 'd');"); + // verifyFormat("x = ([a foo:bar] > b->c == 'd');"); + // verifyFormat("x = ([a foo:bar] < b->c == 'd');"); + // verifyFormat("x = ([a foo:bar] >> b->c == 'd');"); +} + +TEST_F(FormatTestObjC, DisambiguatesCallsFromStructuredBindings) { + verifyFormat("int f() {\n" + " if (a && [f arg])\n" + " return 0;\n" + "}"); + verifyFormat("int f() {\n" + " if (a & [f arg])\n" + " return 0;\n" + "}"); + verifyFormat("int f() {\n" + " for (auto &[elem] : list)\n" + " return 0;\n" + "}"); + verifyFormat("int f() {\n" + " for (auto &&[elem] : list)\n" + " return 0;\n" + "}"); + verifyFormat( + "int f() {\n" + " for (auto /**/ const /**/ volatile /**/ && /**/ [elem] : list)\n" + " return 0;\n" + "}"); +} + } // end namespace } // end namespace format } // end namespace clang |