Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1 | //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Chandler Carruth | 1050e8b | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 10 | #include "clang/Format/Format.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 11 | #include "../Tooling/RewriterTestContext.h" |
| 12 | #include "clang/Lex/Lexer.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace format { |
| 17 | |
| 18 | class FormatTest : public ::testing::Test { |
| 19 | protected: |
| 20 | std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, |
| 21 | const FormatStyle &Style) { |
| 22 | RewriterTestContext Context; |
| 23 | FileID ID = Context.createInMemoryFile("input.cc", Code); |
| 24 | SourceLocation Start = |
| 25 | Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset); |
| 26 | std::vector<CharSourceRange> Ranges( |
| 27 | 1, |
| 28 | CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length))); |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 29 | Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, |
| 30 | getFormattingLangOpts()); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 31 | tooling::Replacements Replace = reformat(Style, Lex, Context.Sources, |
| 32 | Ranges); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 33 | EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); |
| 34 | return Context.getRewrittenText(ID); |
| 35 | } |
| 36 | |
| 37 | std::string format(llvm::StringRef Code, |
| 38 | const FormatStyle &Style = getLLVMStyle()) { |
| 39 | return format(Code, 0, Code.size(), Style); |
| 40 | } |
| 41 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 42 | std::string messUp(llvm::StringRef Code) { |
| 43 | std::string MessedUp(Code.str()); |
| 44 | bool InComment = false; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 45 | bool InPreprocessorDirective = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 46 | bool JustReplacedNewline = false; |
| 47 | for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { |
| 48 | if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { |
| 49 | if (JustReplacedNewline) |
| 50 | MessedUp[i - 1] = '\n'; |
| 51 | InComment = true; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 52 | } else if (MessedUp[i] == '#' && JustReplacedNewline) { |
| 53 | MessedUp[i - 1] = '\n'; |
| 54 | InPreprocessorDirective = true; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 55 | } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') { |
| 56 | MessedUp[i] = ' '; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 57 | MessedUp[i + 1] = ' '; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 58 | } else if (MessedUp[i] == '\n') { |
| 59 | if (InComment) { |
| 60 | InComment = false; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 61 | } else if (InPreprocessorDirective) { |
| 62 | InPreprocessorDirective = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 63 | } else { |
| 64 | JustReplacedNewline = true; |
| 65 | MessedUp[i] = ' '; |
| 66 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 67 | } else if (MessedUp[i] != ' ') { |
| 68 | JustReplacedNewline = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 69 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 70 | } |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 71 | return MessedUp; |
| 72 | } |
| 73 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 74 | FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { |
| 75 | FormatStyle Style = getLLVMStyle(); |
| 76 | Style.ColumnLimit = ColumnLimit; |
| 77 | return Style; |
| 78 | } |
| 79 | |
| 80 | void verifyFormat(llvm::StringRef Code, |
| 81 | const FormatStyle &Style = getLLVMStyle()) { |
| 82 | EXPECT_EQ(Code.str(), format(messUp(Code), Style)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void verifyGoogleFormat(llvm::StringRef Code) { |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 86 | verifyFormat(Code, getGoogleStyle()); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 87 | } |
| 88 | }; |
| 89 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 90 | TEST_F(FormatTest, MessUp) { |
| 91 | EXPECT_EQ("1 2 3", messUp("1 2 3")); |
| 92 | EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n")); |
| 93 | EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc")); |
| 94 | EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc")); |
| 95 | EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne")); |
| 96 | } |
| 97 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 98 | //===----------------------------------------------------------------------===// |
| 99 | // Basic function tests. |
| 100 | //===----------------------------------------------------------------------===// |
| 101 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 102 | TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { |
| 103 | EXPECT_EQ(";", format(";")); |
| 104 | } |
| 105 | |
| 106 | TEST_F(FormatTest, FormatsGlobalStatementsAt0) { |
| 107 | EXPECT_EQ("int i;", format(" int i;")); |
| 108 | EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); |
| 109 | EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); |
| 110 | EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); |
| 111 | } |
| 112 | |
| 113 | TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { |
| 114 | EXPECT_EQ("int i;", format("int\ni;")); |
| 115 | } |
| 116 | |
| 117 | TEST_F(FormatTest, FormatsNestedBlockStatements) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 118 | EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 121 | TEST_F(FormatTest, FormatsNestedCall) { |
| 122 | verifyFormat("Method(f1, f2(f3));"); |
| 123 | verifyFormat("Method(f1(f2, f3()));"); |
| 124 | } |
| 125 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 126 | //===----------------------------------------------------------------------===// |
| 127 | // Tests for control statements. |
| 128 | //===----------------------------------------------------------------------===// |
| 129 | |
| 130 | TEST_F(FormatTest, FormatIfWithoutCompountStatement) { |
| 131 | verifyFormat("if (true)\n f();\ng();"); |
| 132 | verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); |
| 133 | verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 134 | verifyFormat("if (a)\n" |
| 135 | " // comment\n" |
| 136 | " f();"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | TEST_F(FormatTest, ParseIfElse) { |
| 140 | verifyFormat("if (true)\n" |
| 141 | " if (true)\n" |
| 142 | " if (true)\n" |
| 143 | " f();\n" |
| 144 | " else\n" |
| 145 | " g();\n" |
| 146 | " else\n" |
| 147 | " h();\n" |
| 148 | "else\n" |
| 149 | " i();"); |
| 150 | verifyFormat("if (true)\n" |
| 151 | " if (true)\n" |
| 152 | " if (true) {\n" |
| 153 | " if (true)\n" |
| 154 | " f();\n" |
| 155 | " } else {\n" |
| 156 | " g();\n" |
| 157 | " }\n" |
| 158 | " else\n" |
| 159 | " h();\n" |
| 160 | "else {\n" |
| 161 | " i();\n" |
| 162 | "}"); |
| 163 | } |
| 164 | |
| 165 | TEST_F(FormatTest, ElseIf) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 166 | verifyFormat("if (a) {} else if (b) {}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 167 | verifyFormat("if (a)\n" |
| 168 | " f();\n" |
| 169 | "else if (b)\n" |
| 170 | " g();\n" |
| 171 | "else\n" |
| 172 | " h();"); |
| 173 | } |
| 174 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 175 | TEST_F(FormatTest, FormatsForLoop) { |
| 176 | verifyFormat( |
| 177 | "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 178 | " ++VeryVeryLongLoopVariable)\n" |
| 179 | " ;"); |
| 180 | verifyFormat("for (;;)\n" |
| 181 | " f();"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 182 | verifyFormat("for (;;) {}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 183 | verifyFormat("for (;;) {\n" |
| 184 | " f();\n" |
| 185 | "}"); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 186 | |
| 187 | verifyFormat( |
| 188 | "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" |
| 189 | " E = UnwrappedLines.end();\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 190 | " I != E; ++I) {}"); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 191 | |
| 192 | verifyFormat( |
| 193 | "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 194 | " ++IIIII) {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | TEST_F(FormatTest, FormatsWhileLoop) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 198 | verifyFormat("while (true) {}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 199 | verifyFormat("while (true)\n" |
| 200 | " f();"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 201 | verifyFormat("while () {}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 202 | verifyFormat("while () {\n" |
| 203 | " f();\n" |
| 204 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 207 | TEST_F(FormatTest, FormatsDoWhile) { |
| 208 | verifyFormat("do {\n" |
| 209 | " do_something();\n" |
| 210 | "} while (something());"); |
| 211 | verifyFormat("do\n" |
| 212 | " do_something();\n" |
| 213 | "while (something());"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 216 | TEST_F(FormatTest, FormatsSwitchStatement) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 217 | verifyFormat("switch (x) {\n" |
| 218 | "case 1:\n" |
| 219 | " f();\n" |
| 220 | " break;\n" |
| 221 | "case kFoo:\n" |
| 222 | "case ns::kBar:\n" |
| 223 | "case kBaz:\n" |
| 224 | " break;\n" |
| 225 | "default:\n" |
| 226 | " g();\n" |
| 227 | " break;\n" |
| 228 | "}"); |
| 229 | verifyFormat("switch (x) {\n" |
| 230 | "case 1: {\n" |
| 231 | " f();\n" |
| 232 | " break;\n" |
| 233 | "}\n" |
| 234 | "}"); |
| 235 | verifyFormat("switch (test)\n" |
| 236 | " ;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 237 | verifyGoogleFormat("switch (x) {\n" |
| 238 | " case 1:\n" |
| 239 | " f();\n" |
| 240 | " break;\n" |
| 241 | " case kFoo:\n" |
| 242 | " case ns::kBar:\n" |
| 243 | " case kBaz:\n" |
| 244 | " break;\n" |
| 245 | " default:\n" |
| 246 | " g();\n" |
| 247 | " break;\n" |
| 248 | "}"); |
| 249 | verifyGoogleFormat("switch (x) {\n" |
| 250 | " case 1: {\n" |
| 251 | " f();\n" |
| 252 | " break;\n" |
| 253 | " }\n" |
| 254 | "}"); |
| 255 | verifyGoogleFormat("switch (test)\n" |
| 256 | " ;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 259 | TEST_F(FormatTest, FormatsLabels) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 260 | verifyFormat("void f() {\n" |
| 261 | " some_code();\n" |
| 262 | "test_label:\n" |
| 263 | " some_other_code();\n" |
| 264 | " {\n" |
| 265 | " some_more_code();\n" |
| 266 | " another_label:\n" |
| 267 | " some_more_code();\n" |
| 268 | " }\n" |
| 269 | "}"); |
| 270 | verifyFormat("some_code();\n" |
| 271 | "test_label:\n" |
| 272 | "some_other_code();"); |
| 273 | } |
| 274 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 275 | //===----------------------------------------------------------------------===// |
| 276 | // Tests for comments. |
| 277 | //===----------------------------------------------------------------------===// |
| 278 | |
| 279 | TEST_F(FormatTest, UnderstandsSingleLineComments) { |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 280 | verifyFormat("// line 1\n" |
| 281 | "// line 2\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 282 | "void f() {}\n"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 283 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 284 | verifyFormat("void f() {\n" |
| 285 | " // Doesn't do anything\n" |
| 286 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 287 | |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 288 | verifyFormat("int i // This is a fancy variable\n" |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 289 | " = 5;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 290 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 291 | verifyFormat("enum E {\n" |
| 292 | " // comment\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 293 | " VAL_A, // comment\n" |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 294 | " VAL_B\n" |
| 295 | "};"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 296 | |
| 297 | verifyFormat( |
| 298 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 299 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); |
Daniel Jasper | 043835a | 2013-01-09 09:33:39 +0000 | [diff] [blame] | 300 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 301 | " // Comment inside a statement.\n" |
| 302 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 303 | |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 304 | EXPECT_EQ("int i; // single line trailing comment", |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 305 | format("int i;\\\n// single line trailing comment")); |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 306 | |
| 307 | verifyGoogleFormat("int a; // Trailing comment."); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | TEST_F(FormatTest, UnderstandsMultiLineComments) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 311 | verifyFormat("f(/*test=*/ true);"); |
| 312 | } |
| 313 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 314 | //===----------------------------------------------------------------------===// |
| 315 | // Tests for classes, namespaces, etc. |
| 316 | //===----------------------------------------------------------------------===// |
| 317 | |
| 318 | TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 319 | verifyFormat("class A {};"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | TEST_F(FormatTest, UnderstandsAccessSpecifiers) { |
| 323 | verifyFormat("class A {\n" |
| 324 | "public:\n" |
| 325 | "protected:\n" |
| 326 | "private:\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 327 | " void f() {}\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 328 | "};"); |
| 329 | verifyGoogleFormat("class A {\n" |
| 330 | " public:\n" |
| 331 | " protected:\n" |
| 332 | " private:\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 333 | " void f() {}\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 334 | "};"); |
| 335 | } |
| 336 | |
| 337 | TEST_F(FormatTest, FormatsDerivedClass) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 338 | verifyFormat("class A : public B {};"); |
| 339 | verifyFormat("class A : public ::B {};"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 342 | TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 343 | verifyFormat("class A {} a, b;"); |
| 344 | verifyFormat("struct A {} a, b;"); |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 347 | TEST_F(FormatTest, FormatsEnum) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 348 | verifyFormat("enum {\n" |
| 349 | " Zero,\n" |
| 350 | " One = 1,\n" |
| 351 | " Two = One + 1,\n" |
| 352 | " Three = (One + Two),\n" |
| 353 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
| 354 | " Five = (One, Two, Three, Four, 5)\n" |
| 355 | "};"); |
| 356 | verifyFormat("enum Enum {\n" |
| 357 | "};"); |
| 358 | verifyFormat("enum {\n" |
| 359 | "};"); |
| 360 | } |
| 361 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 362 | TEST_F(FormatTest, FormatsNamespaces) { |
| 363 | verifyFormat("namespace some_namespace {\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 364 | "class A {};\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 365 | "void f() {\n" |
| 366 | " f();\n" |
| 367 | "}\n" |
| 368 | "}"); |
| 369 | verifyFormat("namespace {\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 370 | "class A {};\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 371 | "void f() {\n" |
| 372 | " f();\n" |
| 373 | "}\n" |
| 374 | "}"); |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 375 | verifyFormat("inline namespace X {\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 376 | "class A {};\n" |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 377 | "void f() {\n" |
| 378 | " f();\n" |
| 379 | "}\n" |
| 380 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 381 | verifyFormat("using namespace some_namespace;\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 382 | "class A {};\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 383 | "void f() {\n" |
| 384 | " f();\n" |
| 385 | "}"); |
| 386 | } |
| 387 | |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 388 | TEST_F(FormatTest, FormatTryCatch) { |
| 389 | verifyFormat("try {\n" |
| 390 | " throw a * b;\n" |
| 391 | "}\n" |
| 392 | "catch (int a) {\n" |
| 393 | " // Do nothing.\n" |
| 394 | "}\n" |
| 395 | "catch (...) {\n" |
| 396 | " exit(42);\n" |
| 397 | "}"); |
| 398 | |
| 399 | // Function-level try statements. |
| 400 | verifyFormat("int f() try {\n" |
| 401 | " return 4;\n" |
| 402 | "}\n" |
| 403 | "catch (...) {\n" |
| 404 | " return 5;\n" |
| 405 | "}"); |
| 406 | verifyFormat("class A {\n" |
| 407 | " int a;\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 408 | " A() try : a(0) {}\n" |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 409 | " catch (...) {\n" |
| 410 | " throw;\n" |
| 411 | " }\n" |
| 412 | "};\n"); |
| 413 | } |
| 414 | |
| 415 | TEST_F(FormatTest, FormatObjCTryCatch) { |
| 416 | verifyFormat("@try {\n" |
| 417 | " f();\n" |
| 418 | "}\n" |
| 419 | "@catch (NSException e) {\n" |
| 420 | " @throw;\n" |
| 421 | "}\n" |
| 422 | "@finally {\n" |
| 423 | " exit(42);\n" |
| 424 | "}"); |
| 425 | } |
| 426 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 427 | TEST_F(FormatTest, StaticInitializers) { |
| 428 | verifyFormat("static SomeClass SC = { 1, 'a' };"); |
| 429 | |
| 430 | // FIXME: Format like enums if the static initializer does not fit on a line. |
| 431 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 432 | "static SomeClass WithALoooooooooooooooooooongName = {\n" |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 433 | " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 434 | "};"); |
| 435 | |
| 436 | verifyFormat( |
| 437 | "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" |
| 438 | " looooooooooooooooooooooooooooooooooongname,\n" |
| 439 | " looooooooooooooooooooooooooooooong };"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 440 | } |
| 441 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 442 | TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { |
| 443 | verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 444 | " \\\n" |
| 445 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); |
| 446 | } |
| 447 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 448 | TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 449 | verifyFormat("virtual void write(ELFWriter *writerrr,\n" |
| 450 | " OwningPtr<FileOutputBuffer> &buffer) = 0;"); |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 453 | TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) { |
| 454 | EXPECT_EQ("#\n;", format("#;")); |
Manuel Klimek | dd5b101 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 455 | verifyFormat("#\n;\n;\n;"); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { |
| 459 | EXPECT_EQ("#line 42 \"test\"\n", |
| 460 | format("# \\\n line \\\n 42 \\\n \"test\"\n")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 461 | EXPECT_EQ("#define A \\\n B\n", |
| 462 | format("# \\\n define \\\n A \\\n B\n", |
| 463 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | TEST_F(FormatTest, EndOfFileEndsPPDirective) { |
| 467 | EXPECT_EQ("#line 42 \"test\"", |
| 468 | format("# \\\n line \\\n 42 \\\n \"test\"")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 469 | EXPECT_EQ("#define A \\\n B", |
| 470 | format("# \\\n define \\\n A \\\n B", |
| 471 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 474 | TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 475 | // If the macro fits in one line, we still do not get the full |
| 476 | // line, as only the next line decides whether we need an escaped newline and |
| 477 | // thus use the last column. |
| 478 | verifyFormat("#define A(B)", getLLVMStyleWithColumns(13)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 479 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 480 | verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12)); |
| 481 | verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 482 | verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); |
| 483 | } |
| 484 | |
| 485 | TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 486 | EXPECT_EQ("// some comment\n" |
| 487 | "#include \"a.h\"\n" |
| 488 | "#define A(A,\\\n" |
| 489 | " B)\n" |
| 490 | "#include \"b.h\"\n" |
| 491 | "// some comment\n", |
| 492 | format(" // some comment\n" |
| 493 | " #include \"a.h\"\n" |
| 494 | "#define A(A,\\\n" |
| 495 | " B)\n" |
| 496 | " #include \"b.h\"\n" |
| 497 | " // some comment\n", getLLVMStyleWithColumns(13))); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 500 | TEST_F(FormatTest, LayoutSingleHash) { |
| 501 | EXPECT_EQ("#\na;", format("#\na;")); |
| 502 | } |
| 503 | |
| 504 | TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { |
| 505 | EXPECT_EQ("#define A \\\n" |
| 506 | " c; \\\n" |
| 507 | " e;\n" |
| 508 | "f;", format("#define A c; e;\n" |
| 509 | "f;", getLLVMStyleWithColumns(14))); |
| 510 | } |
| 511 | |
| 512 | TEST_F(FormatTest, LayoutRemainingTokens) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 513 | EXPECT_EQ("{}", format("{}")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 517 | EXPECT_EQ("# define A\\\n b;", |
| 518 | format("# define A b;", 11, 2, getLLVMStyleWithColumns(11))); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | TEST_F(FormatTest, MacroDefinitionInsideStatement) { |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 522 | EXPECT_EQ("int x,\n" |
| 523 | "#define A\n" |
| 524 | " y;", format("int x,\n#define A\ny;")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 527 | TEST_F(FormatTest, HashInMacroDefinition) { |
| 528 | verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); |
| 529 | verifyFormat("#define A \\\n" |
| 530 | " { \\\n" |
| 531 | " f(#c);\\\n" |
| 532 | " }", getLLVMStyleWithColumns(11)); |
Daniel Jasper | 765561f | 2013-01-08 16:17:54 +0000 | [diff] [blame] | 533 | |
| 534 | verifyFormat("#define A(X) \\\n" |
| 535 | " void function##X()", getLLVMStyleWithColumns(22)); |
| 536 | |
| 537 | verifyFormat("#define A(a, b, c) \\\n" |
| 538 | " void a##b##c()", getLLVMStyleWithColumns(22)); |
| 539 | |
| 540 | verifyFormat("#define A \\\n" |
| 541 | " void # ## #", getLLVMStyleWithColumns(22)); |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 544 | TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { |
| 545 | EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); |
| 546 | } |
| 547 | |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 548 | TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { |
| 549 | verifyFormat("{\n {\n a #c;\n }\n}"); |
| 550 | } |
| 551 | |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 552 | TEST_F(FormatTest, FormatUnbalancedStructuralElements) { |
| 553 | EXPECT_EQ("#define A \\\n { \\\n {\nint i;", |
| 554 | format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); |
| 555 | EXPECT_EQ("#define A \\\n } \\\n }\nint i;", |
| 556 | format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); |
| 557 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 558 | |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 559 | TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 560 | EXPECT_EQ( |
| 561 | "#define A \\\n int i; \\\n int j;", |
| 562 | format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 565 | TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { |
| 566 | verifyFormat("#define A \\\n" |
| 567 | " int v( \\\n" |
| 568 | " a); \\\n" |
| 569 | " int i;", getLLVMStyleWithColumns(11)); |
| 570 | } |
| 571 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 572 | TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 573 | EXPECT_EQ( |
| 574 | "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 575 | " \\\n" |
| 576 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 577 | "\n" |
| 578 | "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 579 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", |
| 580 | format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 581 | "\\\n" |
| 582 | "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 583 | " \n" |
| 584 | " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 585 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 588 | TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { |
| 589 | EXPECT_EQ("int\n" |
| 590 | "#define A\n" |
| 591 | " a;", |
| 592 | format("int\n#define A\na;")); |
| 593 | verifyFormat( |
| 594 | "functionCallTo(someOtherFunction(\n" |
| 595 | " withSomeParameters, whichInSequence,\n" |
| 596 | " areLongerThanALine(andAnotherCall,\n" |
| 597 | "#define A \\\n" |
| 598 | " B\n" |
| 599 | " withMoreParamters,\n" |
| 600 | " whichStronglyInfluenceTheLayout),\n" |
| 601 | " andMoreParameters),\n" |
| 602 | " trailing);", getLLVMStyleWithColumns(69)); |
| 603 | } |
| 604 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 605 | TEST_F(FormatTest, LayoutBlockInsideParens) { |
| 606 | EXPECT_EQ("functionCall({\n" |
| 607 | " int i;\n" |
| 608 | "});", format(" functionCall ( {int i;} );")); |
| 609 | } |
| 610 | |
| 611 | TEST_F(FormatTest, LayoutBlockInsideStatement) { |
| 612 | EXPECT_EQ("SOME_MACRO {\n" |
| 613 | " int i;\n" |
| 614 | "}\n" |
| 615 | "int i;", format(" SOME_MACRO {int i;} int i;")); |
| 616 | } |
| 617 | |
| 618 | TEST_F(FormatTest, LayoutNestedBlocks) { |
| 619 | verifyFormat("void AddOsStrings(unsigned bitmask) {\n" |
| 620 | " struct s {\n" |
| 621 | " int i;\n" |
| 622 | " };\n" |
| 623 | " s kBitsToOs[] = { { 10 } };\n" |
| 624 | " for (int i = 0; i < 10; ++i)\n" |
| 625 | " return;\n" |
| 626 | "}"); |
| 627 | } |
| 628 | |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 629 | TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { |
| 630 | EXPECT_EQ("{}", format("{}")); |
| 631 | } |
| 632 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 633 | //===----------------------------------------------------------------------===// |
| 634 | // Line break tests. |
| 635 | //===----------------------------------------------------------------------===// |
| 636 | |
| 637 | TEST_F(FormatTest, FormatsFunctionDefinition) { |
| 638 | verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," |
| 639 | " int h, int j, int f,\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 640 | " int c, int ddddddddddddd) {}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | TEST_F(FormatTest, FormatsAwesomeMethodCall) { |
| 644 | verifyFormat( |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 645 | "SomeLongMethodName(SomeReallyLongMethod(\n" |
| 646 | " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n" |
| 647 | " SecondLongCall(parameter));"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 650 | TEST_F(FormatTest, ConstructorInitializers) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 651 | verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 652 | |
| 653 | verifyFormat( |
| 654 | "SomeClass::Constructor()\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 655 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 656 | |
| 657 | verifyFormat( |
| 658 | "SomeClass::Constructor()\n" |
| 659 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 660 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 661 | |
| 662 | verifyFormat("Constructor()\n" |
| 663 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 664 | " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 665 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 666 | " aaaaaaaaaaaaaaaaaaaaaaa() {}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 667 | |
| 668 | // Here a line could be saved by splitting the second initializer onto two |
| 669 | // lines, but that is not desireable. |
| 670 | verifyFormat("Constructor()\n" |
| 671 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 672 | " aaaaaaaaaaa(aaaaaaaaaaa),\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 673 | " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 674 | |
| 675 | verifyGoogleFormat("MyClass::MyClass(int var)\n" |
| 676 | " : some_var_(var), // 4 space indent\n" |
| 677 | " some_other_var_(var + 1) { // lined up\n" |
| 678 | "}"); |
| 679 | } |
| 680 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 681 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
| 682 | verifyFormat( |
| 683 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
| 684 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
| 685 | " f();"); |
| 686 | } |
| 687 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 688 | TEST_F(FormatTest, BreaksDesireably) { |
| 689 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 690 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 691 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 692 | |
| 693 | verifyFormat( |
| 694 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 695 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 696 | |
| 697 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 698 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 699 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 700 | |
| 701 | verifyFormat( |
| 702 | "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 703 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 704 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 705 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 706 | |
| 707 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 708 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 709 | |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 710 | verifyFormat( |
| 711 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" |
| 712 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 713 | |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 714 | // This test case breaks on an incorrect memoization, i.e. an optimization not |
| 715 | // taking into account the StopAt value. |
| 716 | verifyFormat( |
| 717 | "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 718 | " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 719 | " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 720 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 721 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 722 | verifyFormat("{\n {\n {\n" |
| 723 | " Annotation.SpaceRequiredBefore =\n" |
| 724 | " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" |
| 725 | " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" |
| 726 | " }\n }\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 729 | TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { |
| 730 | verifyFormat( |
| 731 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 732 | " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {}"); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 733 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 734 | " ccccccccccccccccccccccccc) {}"); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 735 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 736 | " ccccccccccccccccccccccccc) {}"); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 737 | verifyFormat( |
| 738 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 739 | " ccccccccccccccccccccccccc) {}"); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 742 | TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) { |
| 743 | verifyFormat( |
| 744 | "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n" |
| 745 | " SI->getAlignment(),\n" |
| 746 | " SI->getPointerAddressSpaceee());\n"); |
| 747 | verifyFormat( |
| 748 | "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" |
| 749 | " Line.Tokens.front().Tok.getLocation(),\n" |
| 750 | " Line.Tokens.back().Tok.getLocation());"); |
| 751 | } |
| 752 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 753 | TEST_F(FormatTest, AlignsAfterAssignments) { |
| 754 | verifyFormat( |
| 755 | "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 756 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 757 | verifyFormat( |
| 758 | "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 759 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 760 | verifyFormat( |
| 761 | "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 762 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 763 | verifyFormat( |
| 764 | "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 765 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 766 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 767 | "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 768 | " aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 769 | " aaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | TEST_F(FormatTest, AlignsAfterReturn) { |
| 773 | verifyFormat( |
| 774 | "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 775 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 776 | verifyFormat( |
| 777 | "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 778 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 779 | } |
| 780 | |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 781 | TEST_F(FormatTest, BreaksConditionalExpressions) { |
| 782 | verifyFormat( |
| 783 | "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" |
| 784 | " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" |
| 785 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 786 | verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" |
| 787 | " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);"); |
| 788 | } |
| 789 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 790 | TEST_F(FormatTest, AlignsStringLiterals) { |
| 791 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
| 792 | " \"short literal\");"); |
| 793 | verifyFormat( |
| 794 | "looooooooooooooooooooooooongFunction(\n" |
| 795 | " \"short literal\"\n" |
| 796 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
| 797 | } |
| 798 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 799 | TEST_F(FormatTest, AlignsPipes) { |
| 800 | verifyFormat( |
| 801 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 802 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 803 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 804 | verifyFormat( |
| 805 | "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" |
| 806 | " << aaaaaaaaaaaaaaaaaaaa;"); |
| 807 | verifyFormat( |
| 808 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 809 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 810 | verifyFormat( |
| 811 | "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 812 | " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" |
| 813 | " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); |
| 814 | verifyFormat( |
| 815 | "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 816 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 817 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 818 | } |
| 819 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 820 | TEST_F(FormatTest, UnderstandsEquals) { |
| 821 | verifyFormat( |
| 822 | "aaaaaaaaaaaaaaaaa =\n" |
| 823 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 824 | verifyFormat( |
| 825 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 826 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 827 | verifyFormat( |
| 828 | "if (a) {\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 829 | " f();\n" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 830 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 831 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 832 | |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 833 | verifyFormat( |
Daniel Jasper | 3fc0bb7 | 2013-01-09 10:40:23 +0000 | [diff] [blame] | 834 | // FIXME: Does an expression like this ever make sense? If yes, fix. |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 835 | "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 836 | " 10000000) {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 839 | TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 840 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 841 | " .looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 842 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 843 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 844 | " ->looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 845 | |
| 846 | verifyFormat( |
| 847 | "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" |
| 848 | " Parameter2);"); |
| 849 | |
| 850 | verifyFormat( |
| 851 | "ShortObject->shortFunction(\n" |
| 852 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" |
| 853 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); |
| 854 | |
| 855 | verifyFormat("loooooooooooooongFunction(\n" |
| 856 | " LoooooooooooooongObject->looooooooooooooooongFunction());"); |
| 857 | |
| 858 | verifyFormat( |
| 859 | "function(LoooooooooooooooooooooooooooooooooooongObject\n" |
| 860 | " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); |
| 861 | |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 862 | // Here, it is not necessary to wrap at "." or "->". |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 863 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 864 | " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 865 | verifyFormat( |
| 866 | "aaaaaaaaaaa->aaaaaaaaa(\n" |
| 867 | " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 868 | " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 871 | TEST_F(FormatTest, WrapsTemplateDeclarations) { |
| 872 | verifyFormat("template <typename T>\n" |
| 873 | "virtual void loooooooooooongFunction(int Param1, int Param2);"); |
| 874 | verifyFormat( |
| 875 | "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" |
| 876 | " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); |
| 877 | verifyFormat( |
| 878 | "template <typename T>\n" |
| 879 | "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" |
| 880 | " int Paaaaaaaaaaaaaaaaaaaaram2);"); |
Daniel Jasper | 5eda31e | 2013-01-02 18:30:06 +0000 | [diff] [blame] | 881 | verifyFormat( |
| 882 | "template <typename T>\n" |
| 883 | "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" |
| 884 | " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" |
| 885 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | d64f738 | 2013-01-09 09:50:48 +0000 | [diff] [blame] | 886 | verifyFormat("template <typename T>\n" |
| 887 | "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 888 | " int aaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 3fc0bb7 | 2013-01-09 10:40:23 +0000 | [diff] [blame] | 889 | verifyFormat( |
| 890 | "template <typename T1, typename T2 = char, typename T3 = char,\n" |
| 891 | " typename T4 = char>\n" |
| 892 | "void f();"); |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 893 | } |
| 894 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 895 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
| 896 | verifyFormat("A<int> a;"); |
| 897 | verifyFormat("A<A<A<int> > > a;"); |
| 898 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
| 899 | verifyFormat("bool x = a < 1 || 2 > a;"); |
| 900 | verifyFormat("bool x = 5 < f<int>();"); |
| 901 | verifyFormat("bool x = f<int>() > 5;"); |
| 902 | verifyFormat("bool x = 5 < a<int>::x;"); |
| 903 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
| 904 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
| 905 | |
| 906 | verifyGoogleFormat("A<A<int>> a;"); |
| 907 | verifyGoogleFormat("A<A<A<int>>> a;"); |
| 908 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
| 909 | |
| 910 | verifyFormat("test >> a >> b;"); |
| 911 | verifyFormat("test << a >> b;"); |
| 912 | |
| 913 | verifyFormat("f<int>();"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 914 | verifyFormat("template <typename T> void f() {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 917 | TEST_F(FormatTest, UnderstandsUnaryOperators) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 918 | verifyFormat("int a = -2;"); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 919 | verifyFormat("f(-1, -2, -3);"); |
| 920 | verifyFormat("a[-1] = 5;"); |
| 921 | verifyFormat("int a = 5 + -2;"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 922 | verifyFormat("if (i == -1) {}"); |
| 923 | verifyFormat("if (i != -1) {}"); |
| 924 | verifyFormat("if (i > -1) {}"); |
| 925 | verifyFormat("if (i < -1) {}"); |
Daniel Jasper | d56a737 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 926 | verifyFormat("++(a->f());"); |
| 927 | verifyFormat("--(a->f());"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 928 | verifyFormat("if (!(a->f())) {}"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 929 | |
| 930 | verifyFormat("a-- > b;"); |
| 931 | verifyFormat("b ? -a : c;"); |
| 932 | verifyFormat("n * sizeof char16;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 933 | verifyFormat("n * alignof char16;"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 934 | verifyFormat("sizeof(char);"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 935 | verifyFormat("alignof(char);"); |
Daniel Jasper | 1f0754b | 2013-01-02 15:26:16 +0000 | [diff] [blame] | 936 | |
| 937 | verifyFormat("return -1;"); |
| 938 | verifyFormat("switch (a) {\n" |
| 939 | "case -1:\n" |
| 940 | " break;\n" |
| 941 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | TEST_F(FormatTest, UndestandsOverloadedOperators) { |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 945 | verifyFormat("bool operator<();"); |
| 946 | verifyFormat("bool operator>();"); |
| 947 | verifyFormat("bool operator=();"); |
| 948 | verifyFormat("bool operator==();"); |
| 949 | verifyFormat("bool operator!=();"); |
| 950 | verifyFormat("int operator+();"); |
| 951 | verifyFormat("int operator++();"); |
| 952 | verifyFormat("bool operator();"); |
| 953 | verifyFormat("bool operator()();"); |
| 954 | verifyFormat("bool operator[]();"); |
| 955 | verifyFormat("operator bool();"); |
| 956 | verifyFormat("operator SomeType<int>();"); |
| 957 | verifyFormat("void *operator new(std::size_t size);"); |
| 958 | verifyFormat("void *operator new[](std::size_t size);"); |
| 959 | verifyFormat("void operator delete(void *ptr);"); |
| 960 | verifyFormat("void operator delete[](void *ptr);"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 963 | TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 964 | verifyFormat("int *f(int *a) {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 965 | verifyFormat("f(a, *a);"); |
| 966 | verifyFormat("f(*a);"); |
| 967 | verifyFormat("int a = b * 10;"); |
| 968 | verifyFormat("int a = 10 * b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 969 | verifyFormat("int a = b * c;"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 970 | verifyFormat("int a += b * c;"); |
| 971 | verifyFormat("int a -= b * c;"); |
| 972 | verifyFormat("int a *= b * c;"); |
| 973 | verifyFormat("int a /= b * c;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 974 | verifyFormat("int a = *b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 975 | verifyFormat("int a = *b * c;"); |
| 976 | verifyFormat("int a = b * *c;"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 977 | verifyFormat("int main(int argc, char **argv) {}"); |
Nico Weber | 00d5a04 | 2012-12-23 01:07:46 +0000 | [diff] [blame] | 978 | verifyFormat("return 10 * b;"); |
| 979 | verifyFormat("return *b * *c;"); |
| 980 | verifyFormat("return a & ~b;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 981 | verifyFormat("f(b ? *c : *d);"); |
| 982 | verifyFormat("int a = b ? *c : *d;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 983 | verifyFormat("*b = a;"); |
| 984 | verifyFormat("a * ~b;"); |
| 985 | verifyFormat("a * !b;"); |
| 986 | verifyFormat("a * +b;"); |
| 987 | verifyFormat("a * -b;"); |
| 988 | verifyFormat("a * ++b;"); |
| 989 | verifyFormat("a * --b;"); |
Daniel Jasper | 5cf7cf3 | 2013-01-10 11:14:08 +0000 | [diff] [blame] | 990 | verifyFormat("int *pa = (int *)&a;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 991 | |
Daniel Jasper | 9bb0d28 | 2013-01-04 20:46:38 +0000 | [diff] [blame] | 992 | verifyFormat("InvalidRegions[*R] = 0;"); |
| 993 | |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 994 | verifyFormat("A<int *> a;"); |
| 995 | verifyFormat("A<int **> a;"); |
| 996 | verifyFormat("A<int *, int *> a;"); |
| 997 | verifyFormat("A<int **, int **> a;"); |
Daniel Jasper | ef5b9c3 | 2013-01-02 15:46:59 +0000 | [diff] [blame] | 998 | verifyFormat("Type *A = static_cast<Type *>(P);"); |
Daniel Jasper | 5cf7cf3 | 2013-01-10 11:14:08 +0000 | [diff] [blame] | 999 | verifyFormat("Type *A = (Type *)P;"); |
| 1000 | verifyFormat("Type *A = (vector<Type *, int *>)P;"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 1001 | |
Daniel Jasper | 2db356d | 2013-01-08 20:03:18 +0000 | [diff] [blame] | 1002 | verifyFormat( |
| 1003 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1004 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1005 | |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1006 | verifyGoogleFormat("int main(int argc, char** argv) {}"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 1007 | verifyGoogleFormat("A<int*> a;"); |
| 1008 | verifyGoogleFormat("A<int**> a;"); |
| 1009 | verifyGoogleFormat("A<int*, int*> a;"); |
| 1010 | verifyGoogleFormat("A<int**, int**> a;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 1011 | verifyGoogleFormat("f(b ? *c : *d);"); |
| 1012 | verifyGoogleFormat("int a = b ? *c : *d;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1015 | TEST_F(FormatTest, FormatsFunctionTypes) { |
| 1016 | // FIXME: Determine the cases that need a space after the return type and fix. |
| 1017 | verifyFormat("A<bool()> a;"); |
| 1018 | verifyFormat("A<SomeType()> a;"); |
| 1019 | verifyFormat("A<void(*)(int, std::string)> a;"); |
| 1020 | |
| 1021 | verifyFormat("int(*func)(void *);"); |
| 1022 | } |
| 1023 | |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 1024 | TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1025 | verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1026 | " int LoooooooooooooooongParam2) {}"); |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 1027 | verifyFormat( |
| 1028 | "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" |
| 1029 | " SourceLocation L, IdentifierIn *II,\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1030 | " Type *T) {}"); |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 1033 | TEST_F(FormatTest, LineStartsWithSpecialCharacter) { |
| 1034 | verifyFormat("(a)->b();"); |
| 1035 | verifyFormat("--a;"); |
| 1036 | } |
| 1037 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 1038 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
| 1039 | EXPECT_EQ("#include <string>\n", format("#include <string>\n")); |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 1040 | EXPECT_EQ("#include <a/b/c.h>\n", format("#include <a/b/c.h>\n")); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 1041 | EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n")); |
| 1042 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
| 1043 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
Nico Weber | b23ae0c | 2012-12-21 18:21:56 +0000 | [diff] [blame] | 1044 | |
| 1045 | EXPECT_EQ("#import <string>\n", format("#import <string>\n")); |
| 1046 | EXPECT_EQ("#import <a/b/c.h>\n", format("#import <a/b/c.h>\n")); |
| 1047 | EXPECT_EQ("#import \"a/b/string\"\n", format("#import \"a/b/string\"\n")); |
| 1048 | EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); |
| 1049 | EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1052 | //===----------------------------------------------------------------------===// |
| 1053 | // Error recovery tests. |
| 1054 | //===----------------------------------------------------------------------===// |
| 1055 | |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 1056 | TEST_F(FormatTest, IncorrectCodeTrailingStuff) { |
| 1057 | verifyFormat("void f() { return } 42"); |
| 1058 | } |
| 1059 | |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 1060 | TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { |
| 1061 | verifyFormat("int aaaaaaaa =\n" |
| 1062 | " // Overly long comment\n" |
| 1063 | " b;", getLLVMStyleWithColumns(20)); |
| 1064 | verifyFormat("function(\n" |
| 1065 | " ShortArgument,\n" |
| 1066 | " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20)); |
| 1067 | } |
| 1068 | |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1069 | TEST_F(FormatTest, IncorrectAccessSpecifier) { |
| 1070 | verifyFormat("public:"); |
| 1071 | verifyFormat("class A {\n" |
| 1072 | "public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1073 | " void f() {}\n" |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1074 | "};"); |
| 1075 | verifyFormat("public\n" |
| 1076 | "int qwerty;"); |
| 1077 | verifyFormat("public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1078 | "B {}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1079 | verifyFormat("public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1080 | "{}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1081 | verifyFormat("public\n" |
| 1082 | "B {\n" |
| 1083 | " int x;\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1084 | "}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1085 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1086 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1087 | TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { |
| 1088 | verifyFormat("{"); |
| 1089 | } |
| 1090 | |
| 1091 | TEST_F(FormatTest, IncorrectCodeDoNoWhile) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1092 | verifyFormat("do {}"); |
| 1093 | verifyFormat("do {}\n" |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1094 | "f();"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1095 | verifyFormat("do {}\n" |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1096 | "wheeee(fun);"); |
| 1097 | verifyFormat("do {\n" |
| 1098 | " f();\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1099 | "}"); |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1102 | TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { |
| 1103 | verifyFormat("namespace {\n" |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1104 | "class Foo { Foo ( }; } // comment"); |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1107 | TEST_F(FormatTest, IncorrectCodeErrorDetection) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1108 | EXPECT_EQ("{\n{}\n", format("{\n{\n}\n")); |
| 1109 | EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); |
| 1110 | EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); |
| 1111 | EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n")); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1112 | |
| 1113 | FormatStyle Style = getLLVMStyle(); |
| 1114 | Style.ColumnLimit = 10; |
| 1115 | EXPECT_EQ("{\n" |
| 1116 | " {\n" |
| 1117 | " breakme(\n" |
| 1118 | " qwe);\n" |
| 1119 | "}\n", format("{\n" |
| 1120 | " {\n" |
| 1121 | " breakme(qwe);\n" |
| 1122 | "}\n", Style)); |
| 1123 | |
| 1124 | } |
| 1125 | |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 1126 | TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { |
| 1127 | verifyFormat( |
| 1128 | "int x = {\n" |
| 1129 | " avariable,\n" |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 1130 | " b(alongervariable)\n" |
| 1131 | "};", getLLVMStyleWithColumns(25)); |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) { |
| 1135 | verifyFormat( |
| 1136 | "Aaa({\n" |
| 1137 | " int i;\n" |
| 1138 | "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" |
| 1139 | " ccccccccccccccccc));"); |
| 1140 | } |
| 1141 | |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1142 | //===----------------------------------------------------------------------===// |
| 1143 | // Objective-C tests. |
| 1144 | //===----------------------------------------------------------------------===// |
| 1145 | |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1146 | TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { |
| 1147 | verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); |
| 1148 | EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", |
| 1149 | format("-(NSUInteger)indexOfObject:(id)anObject;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1150 | EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1151 | EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); |
| 1152 | EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", |
| 1153 | format("-(NSInteger)Method3:(id)anObject;")); |
| 1154 | EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", |
| 1155 | format("-(NSInteger)Method4:(id)anObject;")); |
| 1156 | EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", |
| 1157 | format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); |
| 1158 | EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", |
| 1159 | format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1160 | EXPECT_EQ( |
| 1161 | "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", |
| 1162 | format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); |
Fariborz Jahanian | 9b3f02c | 2012-12-21 22:51:18 +0000 | [diff] [blame] | 1163 | |
| 1164 | // Very long objectiveC method declaration. |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1165 | EXPECT_EQ( |
| 1166 | "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n " |
| 1167 | "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n " |
| 1168 | "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n " |
| 1169 | "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n " |
| 1170 | "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n " |
| 1171 | "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;", |
| 1172 | format( |
| 1173 | "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range " |
| 1174 | "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 " |
| 1175 | "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 " |
| 1176 | "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 " |
| 1177 | "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 " |
| 1178 | "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;")); |
Nico Weber | e0fd429 | 2013-01-10 20:18:33 +0000 | [diff] [blame] | 1179 | |
| 1180 | verifyFormat("- (int)sum:(vector<int>)numbers;"); |
| 1181 | verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); |
| 1182 | // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC |
| 1183 | // protocol lists (but not for template classes): |
| 1184 | //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 1187 | TEST_F(FormatTest, FormatObjCBlocks) { |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1188 | verifyFormat("int (^Block)(int, int);"); |
| 1189 | verifyFormat("int (^Block1)(int, int) = ^(int i, int j)"); |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1192 | TEST_F(FormatTest, FormatObjCInterface) { |
| 1193 | // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875 |
| 1194 | // FIXME: In google style, it's "+(id) init", not "+ (id)init". |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1195 | verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1196 | "@public\n" |
| 1197 | " int field1;\n" |
| 1198 | "@protected\n" |
| 1199 | " int field2;\n" |
| 1200 | "@private\n" |
| 1201 | " int field3;\n" |
| 1202 | "@package\n" |
| 1203 | " int field4;\n" |
| 1204 | "}\n" |
| 1205 | "+ (id)init;\n" |
| 1206 | "@end"); |
| 1207 | |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1208 | verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" |
| 1209 | " @public\n" |
| 1210 | " int field1;\n" |
| 1211 | " @protected\n" |
| 1212 | " int field2;\n" |
| 1213 | " @private\n" |
| 1214 | " int field3;\n" |
| 1215 | " @package\n" |
| 1216 | " int field4;\n" |
| 1217 | "}\n" |
| 1218 | "+ (id)init;\n" |
| 1219 | "@end"); |
| 1220 | |
| 1221 | verifyFormat("@interface Foo\n" |
| 1222 | "+ (id)init;\n" |
| 1223 | "// Look, a comment!\n" |
| 1224 | "- (int)answerWith:(int)i;\n" |
| 1225 | "@end"); |
| 1226 | |
| 1227 | verifyFormat("@interface Foo\n" |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 1228 | "@end\n" |
| 1229 | "@interface Bar\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1230 | "@end"); |
| 1231 | |
| 1232 | verifyFormat("@interface Foo : Bar\n" |
| 1233 | "+ (id)init;\n" |
| 1234 | "@end"); |
| 1235 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1236 | verifyFormat("@interface Foo : Bar <Baz, Quux>\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1237 | "+ (id)init;\n" |
| 1238 | "@end"); |
| 1239 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1240 | verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" |
| 1241 | "+ (id)init;\n" |
| 1242 | "@end"); |
| 1243 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 1244 | verifyFormat("@interface Foo (HackStuff)\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1245 | "+ (id)init;\n" |
| 1246 | "@end"); |
| 1247 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 1248 | verifyFormat("@interface Foo ()\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1249 | "+ (id)init;\n" |
| 1250 | "@end"); |
| 1251 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1252 | verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1253 | "+ (id)init;\n" |
| 1254 | "@end"); |
| 1255 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1256 | verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" |
| 1257 | "+ (id)init;\n" |
| 1258 | "@end"); |
| 1259 | |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1260 | verifyFormat("@interface Foo {\n" |
| 1261 | " int _i;\n" |
| 1262 | "}\n" |
| 1263 | "+ (id)init;\n" |
| 1264 | "@end"); |
| 1265 | |
| 1266 | verifyFormat("@interface Foo : Bar {\n" |
| 1267 | " int _i;\n" |
| 1268 | "}\n" |
| 1269 | "+ (id)init;\n" |
| 1270 | "@end"); |
| 1271 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1272 | verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1273 | " int _i;\n" |
| 1274 | "}\n" |
| 1275 | "+ (id)init;\n" |
| 1276 | "@end"); |
| 1277 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 1278 | verifyFormat("@interface Foo (HackStuff) {\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1279 | " int _i;\n" |
| 1280 | "}\n" |
| 1281 | "+ (id)init;\n" |
| 1282 | "@end"); |
| 1283 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 1284 | verifyFormat("@interface Foo () {\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1285 | " int _i;\n" |
| 1286 | "}\n" |
| 1287 | "+ (id)init;\n" |
| 1288 | "@end"); |
| 1289 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1290 | verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1291 | " int _i;\n" |
| 1292 | "}\n" |
| 1293 | "+ (id)init;\n" |
| 1294 | "@end"); |
| 1295 | } |
| 1296 | |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1297 | TEST_F(FormatTest, FormatObjCImplementation) { |
| 1298 | verifyFormat("@implementation Foo : NSObject {\n" |
| 1299 | "@public\n" |
| 1300 | " int field1;\n" |
| 1301 | "@protected\n" |
| 1302 | " int field2;\n" |
| 1303 | "@private\n" |
| 1304 | " int field3;\n" |
| 1305 | "@package\n" |
| 1306 | " int field4;\n" |
| 1307 | "}\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1308 | "+ (id)init {}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1309 | "@end"); |
| 1310 | |
| 1311 | verifyGoogleFormat("@implementation Foo : NSObject {\n" |
| 1312 | " @public\n" |
| 1313 | " int field1;\n" |
| 1314 | " @protected\n" |
| 1315 | " int field2;\n" |
| 1316 | " @private\n" |
| 1317 | " int field3;\n" |
| 1318 | " @package\n" |
| 1319 | " int field4;\n" |
| 1320 | "}\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1321 | "+ (id)init {}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1322 | "@end"); |
| 1323 | |
| 1324 | verifyFormat("@implementation Foo\n" |
| 1325 | "+ (id)init {\n" |
| 1326 | " if (true)\n" |
| 1327 | " return nil;\n" |
| 1328 | "}\n" |
| 1329 | "// Look, a comment!\n" |
| 1330 | "- (int)answerWith:(int)i {\n" |
| 1331 | " return i;\n" |
| 1332 | "}\n" |
| 1333 | "@end"); |
| 1334 | |
| 1335 | verifyFormat("@implementation Foo\n" |
| 1336 | "@end\n" |
| 1337 | "@implementation Bar\n" |
| 1338 | "@end"); |
| 1339 | |
| 1340 | verifyFormat("@implementation Foo : Bar\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1341 | "+ (id)init {}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1342 | "@end"); |
| 1343 | |
| 1344 | verifyFormat("@implementation Foo {\n" |
| 1345 | " int _i;\n" |
| 1346 | "}\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1347 | "+ (id)init {}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1348 | "@end"); |
| 1349 | |
| 1350 | verifyFormat("@implementation Foo : Bar {\n" |
| 1351 | " int _i;\n" |
| 1352 | "}\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1353 | "+ (id)init {}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1354 | "@end"); |
| 1355 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 1356 | verifyFormat("@implementation Foo (HackStuff)\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1357 | "+ (id)init {}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1358 | "@end"); |
| 1359 | } |
| 1360 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1361 | TEST_F(FormatTest, FormatObjCProtocol) { |
| 1362 | verifyFormat("@protocol Foo\n" |
| 1363 | "@property(weak) id delegate;\n" |
| 1364 | "- (NSUInteger)numberOfThings;\n" |
| 1365 | "@end"); |
| 1366 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1367 | verifyFormat("@protocol MyProtocol <NSObject>\n" |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1368 | "- (NSUInteger)numberOfThings;\n" |
| 1369 | "@end"); |
| 1370 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1371 | verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" |
| 1372 | "- (NSUInteger)numberOfThings;\n" |
| 1373 | "@end"); |
| 1374 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1375 | verifyFormat("@protocol Foo;\n" |
| 1376 | "@protocol Bar;\n"); |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 1377 | |
| 1378 | verifyFormat("@protocol Foo\n" |
| 1379 | "@end\n" |
| 1380 | "@protocol Bar\n" |
| 1381 | "@end"); |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 1382 | |
| 1383 | verifyFormat("@protocol myProtocol\n" |
| 1384 | "- (void)mandatoryWithInt:(int)i;\n" |
| 1385 | "@optional\n" |
| 1386 | "- (void)optional;\n" |
| 1387 | "@required\n" |
| 1388 | "- (void)required;\n" |
Nico Weber | 880e538 | 2013-01-10 00:42:07 +0000 | [diff] [blame] | 1389 | "@optional\n" |
| 1390 | "@property(assign) int madProp;\n" |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 1391 | "@end\n"); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 1394 | TEST_F(FormatTest, ObjCAt) { |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1395 | verifyFormat("@autoreleasepool"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1396 | verifyFormat("@catch"); |
| 1397 | verifyFormat("@class"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1398 | verifyFormat("@compatibility_alias"); |
| 1399 | verifyFormat("@defs"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 1400 | verifyFormat("@dynamic"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1401 | verifyFormat("@encode"); |
| 1402 | verifyFormat("@end"); |
| 1403 | verifyFormat("@finally"); |
| 1404 | verifyFormat("@implementation"); |
| 1405 | verifyFormat("@import"); |
| 1406 | verifyFormat("@interface"); |
| 1407 | verifyFormat("@optional"); |
| 1408 | verifyFormat("@package"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1409 | verifyFormat("@private"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1410 | verifyFormat("@property"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1411 | verifyFormat("@protected"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1412 | verifyFormat("@protocol"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1413 | verifyFormat("@public"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1414 | verifyFormat("@required"); |
| 1415 | verifyFormat("@selector"); |
| 1416 | verifyFormat("@synchronized"); |
| 1417 | verifyFormat("@synthesize"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1418 | verifyFormat("@throw"); |
| 1419 | verifyFormat("@try"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1420 | |
Nico Weber | cb4d690 | 2013-01-08 19:40:21 +0000 | [diff] [blame] | 1421 | verifyFormat("@\"String\""); |
| 1422 | verifyFormat("@1"); |
Nico Weber | 81ed2f1 | 2013-01-10 19:36:35 +0000 | [diff] [blame] | 1423 | verifyFormat("@+4.8"); |
| 1424 | verifyFormat("@-4"); |
Nico Weber | cb4d690 | 2013-01-08 19:40:21 +0000 | [diff] [blame] | 1425 | verifyFormat("@1LL"); |
| 1426 | verifyFormat("@.5"); |
| 1427 | verifyFormat("@'c'"); |
| 1428 | verifyFormat("@true"); |
| 1429 | verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); |
| 1430 | verifyFormat("@["); |
| 1431 | verifyFormat("@{"); |
| 1432 | |
| 1433 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 1434 | EXPECT_EQ("@interface", format("@ interface")); |
| 1435 | |
| 1436 | // The precise formatting of this doesn't matter, nobody writes code like |
| 1437 | // this. |
| 1438 | verifyFormat("@ /*foo*/ interface"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1441 | TEST_F(FormatTest, ObjCSnippets) { |
| 1442 | // FIXME: Make the uncommented lines below pass. |
| 1443 | verifyFormat("@autoreleasepool {\n" |
| 1444 | " foo();\n" |
| 1445 | "}"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1446 | verifyFormat("@class Foo, Bar;"); |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1447 | verifyFormat("@compatibility_alias AliasName ExistingClass;"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1448 | verifyFormat("@dynamic textColor;"); |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1449 | //verifyFormat("char *buf1 = @encode(int **);"); |
Nico Weber | 3a2673e | 2013-01-08 20:16:23 +0000 | [diff] [blame] | 1450 | verifyFormat("Protocol *proto = @protocol(p1);"); |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1451 | //verifyFormat("SEL s = @selector(foo:);"); |
Nico Weber | 3a2673e | 2013-01-08 20:16:23 +0000 | [diff] [blame] | 1452 | verifyFormat("@synchronized(self) {\n" |
| 1453 | " f();\n" |
| 1454 | "}"); |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1455 | verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1456 | |
| 1457 | // FIXME: "getter=bar" should not be surround by spaces in @property. |
| 1458 | verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); |
| 1459 | } |
| 1460 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1461 | } // end namespace tooling |
| 1462 | } // end namespace clang |