diff options
author | Ben Hamilton <benhamilton@google.com> | 2018-03-06 17:21:42 +0000 |
---|---|---|
committer | Ben Hamilton <benhamilton@google.com> | 2018-03-06 17:21:42 +0000 |
commit | 1d6c6ee1cf98d260b486242b288a9d8fa0e4e7e1 (patch) | |
tree | ef113b17350d8baf20f8bcd8ac7cb22312786e5b /clang/unittests/Format/FormatTest.cpp | |
parent | 6cd91f1d4485b511017952592e3fab070942cb46 (diff) | |
download | bcm5719-llvm-1d6c6ee1cf98d260b486242b288a9d8fa0e4e7e1.tar.gz bcm5719-llvm-1d6c6ee1cf98d260b486242b288a9d8fa0e4e7e1.zip |
[clang-format] Improve detection of ObjC for-in statements
Summary:
Previously, clang-format would detect the following as an
Objective-C for-in statement:
for (int x = in.value(); ...) {}
because the logic only decided a for-loop was definitely *not*
an Objective-C for-in loop after it saw a semicolon or a colon.
To fix this, I delayed the decision of whether this was a for-in
statement until after we found the matching right-paren, at which
point we know if we've seen a semicolon or not.
Test Plan: New tests added. Ran tests with:
make -j12 FormatTests && ./tools/clang/unittests/Format/FormatTests
Reviewers: krasimir, jolesiak
Reviewed By: jolesiak
Subscribers: djasper, cfe-commits, klimek
Differential Revision: https://reviews.llvm.org/D43904
llvm-svn: 326815
Diffstat (limited to 'clang/unittests/Format/FormatTest.cpp')
-rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index c0117215e6a..75667d56268 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -742,6 +742,12 @@ TEST_F(FormatTest, FormatsForLoop) { " aaaaaaaaaaaaaaaaaaaaaaaaaaa != bbbbbbbbbbbbbbbbbbbbbbb;\n" " ++aaaaaaaaaaaaaaaaaaaaaaaaaaa) {"); + // These should not be formatted as Objective-C for-in loops. + verifyFormat("for (Foo *x = 0; x != in; x++) {\n}"); + verifyFormat("Foo *x;\nfor (x = 0; x != in; x++) {\n}"); + verifyFormat("Foo *x;\nfor (x in y) {\n}"); + verifyFormat("for (const Foo<Bar> &baz = in.value(); !baz.at_end(); ++baz) {\n}"); + FormatStyle NoBinPacking = getLLVMStyle(); NoBinPacking.BinPackParameters = false; verifyFormat("for (int aaaaaaaaaaa = 1;\n" @@ -12082,6 +12088,31 @@ TEST_F(FormatTest, FileAndCode) { EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo", "@interface Foo\n@end\n")); } +TEST_F(FormatTest, GuessLanguageWithForIn) { + EXPECT_EQ(FormatStyle::LK_Cpp, + guessLanguage("foo.h", "for (Foo *x = 0; x != in; x++) {}")); + EXPECT_EQ(FormatStyle::LK_ObjC, + guessLanguage("foo.h", "for (Foo *x in bar) {}")); + EXPECT_EQ(FormatStyle::LK_ObjC, + guessLanguage("foo.h", "for (Foo *x in [bar baz]) {}")); + EXPECT_EQ(FormatStyle::LK_ObjC, + guessLanguage("foo.h", "for (Foo *x in [bar baz:blech]) {}")); + EXPECT_EQ( + FormatStyle::LK_ObjC, + guessLanguage("foo.h", "for (Foo *x in [bar baz:blech, 1, 2, 3, 0]) {}")); + EXPECT_EQ(FormatStyle::LK_ObjC, + guessLanguage("foo.h", "for (Foo *x in [bar baz:^{[uh oh];}]) {}")); + EXPECT_EQ(FormatStyle::LK_Cpp, + guessLanguage("foo.h", "Foo *x; for (x = 0; x != in; x++) {}")); + EXPECT_EQ(FormatStyle::LK_ObjC, + guessLanguage("foo.h", "Foo *x; for (x in y) {}")); + EXPECT_EQ( + FormatStyle::LK_Cpp, + guessLanguage( + "foo.h", + "for (const Foo<Bar>& baz = in.value(); !baz.at_end(); ++baz) {}")); +} + } // end namespace } // end namespace format } // end namespace clang |