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))); |
| 29 | LangOptions LangOpts; |
| 30 | LangOpts.CPlusPlus = 1; |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 31 | LangOpts.CPlusPlus11 = 1; |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 32 | LangOpts.ObjC1 = 1; |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame^] | 33 | LangOpts.ObjC2 = 1; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 34 | Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, LangOpts); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 35 | tooling::Replacements Replace = reformat(Style, Lex, Context.Sources, |
| 36 | Ranges); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 37 | EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); |
| 38 | return Context.getRewrittenText(ID); |
| 39 | } |
| 40 | |
| 41 | std::string format(llvm::StringRef Code, |
| 42 | const FormatStyle &Style = getLLVMStyle()) { |
| 43 | return format(Code, 0, Code.size(), Style); |
| 44 | } |
| 45 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 46 | std::string messUp(llvm::StringRef Code) { |
| 47 | std::string MessedUp(Code.str()); |
| 48 | bool InComment = false; |
| 49 | bool JustReplacedNewline = false; |
| 50 | for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { |
| 51 | if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { |
| 52 | if (JustReplacedNewline) |
| 53 | MessedUp[i - 1] = '\n'; |
| 54 | InComment = 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] = ' '; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 57 | } else if (MessedUp[i] == '\n') { |
| 58 | if (InComment) { |
| 59 | InComment = false; |
| 60 | } else { |
| 61 | JustReplacedNewline = true; |
| 62 | MessedUp[i] = ' '; |
| 63 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 64 | } else if (MessedUp[i] != ' ') { |
| 65 | JustReplacedNewline = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 66 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 67 | } |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 68 | return MessedUp; |
| 69 | } |
| 70 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 71 | FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { |
| 72 | FormatStyle Style = getLLVMStyle(); |
| 73 | Style.ColumnLimit = ColumnLimit; |
| 74 | return Style; |
| 75 | } |
| 76 | |
| 77 | void verifyFormat(llvm::StringRef Code, |
| 78 | const FormatStyle &Style = getLLVMStyle()) { |
| 79 | EXPECT_EQ(Code.str(), format(messUp(Code), Style)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void verifyGoogleFormat(llvm::StringRef Code) { |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 83 | verifyFormat(Code, getGoogleStyle()); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 84 | } |
| 85 | }; |
| 86 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 87 | //===----------------------------------------------------------------------===// |
| 88 | // Basic function tests. |
| 89 | //===----------------------------------------------------------------------===// |
| 90 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 91 | TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { |
| 92 | EXPECT_EQ(";", format(";")); |
| 93 | } |
| 94 | |
| 95 | TEST_F(FormatTest, FormatsGlobalStatementsAt0) { |
| 96 | EXPECT_EQ("int i;", format(" int i;")); |
| 97 | EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); |
| 98 | EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); |
| 99 | EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); |
| 100 | } |
| 101 | |
| 102 | TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { |
| 103 | EXPECT_EQ("int i;", format("int\ni;")); |
| 104 | } |
| 105 | |
| 106 | TEST_F(FormatTest, FormatsNestedBlockStatements) { |
| 107 | EXPECT_EQ("{\n {\n {\n }\n }\n}", format("{{{}}}")); |
| 108 | } |
| 109 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 110 | TEST_F(FormatTest, FormatsNestedCall) { |
| 111 | verifyFormat("Method(f1, f2(f3));"); |
| 112 | verifyFormat("Method(f1(f2, f3()));"); |
| 113 | } |
| 114 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 115 | //===----------------------------------------------------------------------===// |
| 116 | // Tests for control statements. |
| 117 | //===----------------------------------------------------------------------===// |
| 118 | |
| 119 | TEST_F(FormatTest, FormatIfWithoutCompountStatement) { |
| 120 | verifyFormat("if (true)\n f();\ng();"); |
| 121 | verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); |
| 122 | verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 123 | verifyFormat("if (a)\n" |
| 124 | " // comment\n" |
| 125 | " f();"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | TEST_F(FormatTest, ParseIfElse) { |
| 129 | verifyFormat("if (true)\n" |
| 130 | " if (true)\n" |
| 131 | " if (true)\n" |
| 132 | " f();\n" |
| 133 | " else\n" |
| 134 | " g();\n" |
| 135 | " else\n" |
| 136 | " h();\n" |
| 137 | "else\n" |
| 138 | " i();"); |
| 139 | verifyFormat("if (true)\n" |
| 140 | " if (true)\n" |
| 141 | " if (true) {\n" |
| 142 | " if (true)\n" |
| 143 | " f();\n" |
| 144 | " } else {\n" |
| 145 | " g();\n" |
| 146 | " }\n" |
| 147 | " else\n" |
| 148 | " h();\n" |
| 149 | "else {\n" |
| 150 | " i();\n" |
| 151 | "}"); |
| 152 | } |
| 153 | |
| 154 | TEST_F(FormatTest, ElseIf) { |
| 155 | verifyFormat("if (a) {\n" |
| 156 | "} else if (b) {\n" |
| 157 | "}"); |
| 158 | verifyFormat("if (a)\n" |
| 159 | " f();\n" |
| 160 | "else if (b)\n" |
| 161 | " g();\n" |
| 162 | "else\n" |
| 163 | " h();"); |
| 164 | } |
| 165 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 166 | TEST_F(FormatTest, FormatsForLoop) { |
| 167 | verifyFormat( |
| 168 | "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 169 | " ++VeryVeryLongLoopVariable)\n" |
| 170 | " ;"); |
| 171 | verifyFormat("for (;;)\n" |
| 172 | " f();"); |
| 173 | verifyFormat("for (;;) {\n" |
| 174 | "}"); |
| 175 | verifyFormat("for (;;) {\n" |
| 176 | " f();\n" |
| 177 | "}"); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 178 | |
| 179 | verifyFormat( |
| 180 | "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" |
| 181 | " E = UnwrappedLines.end();\n" |
| 182 | " I != E; ++I) {\n}"); |
| 183 | |
| 184 | verifyFormat( |
| 185 | "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" |
| 186 | " ++IIIII) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | TEST_F(FormatTest, FormatsWhileLoop) { |
| 190 | verifyFormat("while (true) {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 191 | verifyFormat("while (true)\n" |
| 192 | " f();"); |
| 193 | verifyFormat("while () {\n" |
| 194 | "}"); |
| 195 | verifyFormat("while () {\n" |
| 196 | " f();\n" |
| 197 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 200 | TEST_F(FormatTest, FormatsDoWhile) { |
| 201 | verifyFormat("do {\n" |
| 202 | " do_something();\n" |
| 203 | "} while (something());"); |
| 204 | verifyFormat("do\n" |
| 205 | " do_something();\n" |
| 206 | "while (something());"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 209 | TEST_F(FormatTest, FormatsSwitchStatement) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 210 | verifyFormat("switch (x) {\n" |
| 211 | "case 1:\n" |
| 212 | " f();\n" |
| 213 | " break;\n" |
| 214 | "case kFoo:\n" |
| 215 | "case ns::kBar:\n" |
| 216 | "case kBaz:\n" |
| 217 | " break;\n" |
| 218 | "default:\n" |
| 219 | " g();\n" |
| 220 | " break;\n" |
| 221 | "}"); |
| 222 | verifyFormat("switch (x) {\n" |
| 223 | "case 1: {\n" |
| 224 | " f();\n" |
| 225 | " break;\n" |
| 226 | "}\n" |
| 227 | "}"); |
| 228 | verifyFormat("switch (test)\n" |
| 229 | " ;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 230 | verifyGoogleFormat("switch (x) {\n" |
| 231 | " case 1:\n" |
| 232 | " f();\n" |
| 233 | " break;\n" |
| 234 | " case kFoo:\n" |
| 235 | " case ns::kBar:\n" |
| 236 | " case kBaz:\n" |
| 237 | " break;\n" |
| 238 | " default:\n" |
| 239 | " g();\n" |
| 240 | " break;\n" |
| 241 | "}"); |
| 242 | verifyGoogleFormat("switch (x) {\n" |
| 243 | " case 1: {\n" |
| 244 | " f();\n" |
| 245 | " break;\n" |
| 246 | " }\n" |
| 247 | "}"); |
| 248 | verifyGoogleFormat("switch (test)\n" |
| 249 | " ;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 252 | TEST_F(FormatTest, FormatsLabels) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 253 | verifyFormat("void f() {\n" |
| 254 | " some_code();\n" |
| 255 | "test_label:\n" |
| 256 | " some_other_code();\n" |
| 257 | " {\n" |
| 258 | " some_more_code();\n" |
| 259 | " another_label:\n" |
| 260 | " some_more_code();\n" |
| 261 | " }\n" |
| 262 | "}"); |
| 263 | verifyFormat("some_code();\n" |
| 264 | "test_label:\n" |
| 265 | "some_other_code();"); |
| 266 | } |
| 267 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 268 | //===----------------------------------------------------------------------===// |
| 269 | // Tests for comments. |
| 270 | //===----------------------------------------------------------------------===// |
| 271 | |
| 272 | TEST_F(FormatTest, UnderstandsSingleLineComments) { |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 273 | verifyFormat("// line 1\n" |
| 274 | "// line 2\n" |
| 275 | "void f() {\n}\n"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 276 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 277 | verifyFormat("void f() {\n" |
| 278 | " // Doesn't do anything\n" |
| 279 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 280 | |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 281 | verifyFormat("int i // This is a fancy variable\n" |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 282 | " = 5;"); |
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("enum E {\n" |
| 285 | " // comment\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 286 | " VAL_A, // comment\n" |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 287 | " VAL_B\n" |
| 288 | "};"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 289 | |
| 290 | verifyFormat( |
| 291 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 292 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 293 | |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 294 | EXPECT_EQ("int i; // single line trailing comment", |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 295 | format("int i;\\\n// single line trailing comment")); |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 296 | |
| 297 | verifyGoogleFormat("int a; // Trailing comment."); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | TEST_F(FormatTest, UnderstandsMultiLineComments) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 301 | verifyFormat("f(/*test=*/ true);"); |
| 302 | } |
| 303 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 304 | //===----------------------------------------------------------------------===// |
| 305 | // Tests for classes, namespaces, etc. |
| 306 | //===----------------------------------------------------------------------===// |
| 307 | |
| 308 | TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { |
| 309 | verifyFormat("class A {\n};"); |
| 310 | } |
| 311 | |
| 312 | TEST_F(FormatTest, UnderstandsAccessSpecifiers) { |
| 313 | verifyFormat("class A {\n" |
| 314 | "public:\n" |
| 315 | "protected:\n" |
| 316 | "private:\n" |
| 317 | " void f() {\n" |
| 318 | " }\n" |
| 319 | "};"); |
| 320 | verifyGoogleFormat("class A {\n" |
| 321 | " public:\n" |
| 322 | " protected:\n" |
| 323 | " private:\n" |
| 324 | " void f() {\n" |
| 325 | " }\n" |
| 326 | "};"); |
| 327 | } |
| 328 | |
| 329 | TEST_F(FormatTest, FormatsDerivedClass) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 330 | verifyFormat("class A : public B {\n" |
| 331 | "};"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 332 | verifyFormat("class A : public ::B {\n" |
| 333 | "};"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 336 | TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { |
| 337 | verifyFormat("class A {\n" |
| 338 | "} a, b;"); |
| 339 | verifyFormat("struct A {\n" |
| 340 | "} a, b;"); |
| 341 | } |
| 342 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 343 | TEST_F(FormatTest, FormatsEnum) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 344 | verifyFormat("enum {\n" |
| 345 | " Zero,\n" |
| 346 | " One = 1,\n" |
| 347 | " Two = One + 1,\n" |
| 348 | " Three = (One + Two),\n" |
| 349 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
| 350 | " Five = (One, Two, Three, Four, 5)\n" |
| 351 | "};"); |
| 352 | verifyFormat("enum Enum {\n" |
| 353 | "};"); |
| 354 | verifyFormat("enum {\n" |
| 355 | "};"); |
| 356 | } |
| 357 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 358 | TEST_F(FormatTest, FormatsNamespaces) { |
| 359 | verifyFormat("namespace some_namespace {\n" |
| 360 | "class A {\n" |
| 361 | "};\n" |
| 362 | "void f() {\n" |
| 363 | " f();\n" |
| 364 | "}\n" |
| 365 | "}"); |
| 366 | verifyFormat("namespace {\n" |
| 367 | "class A {\n" |
| 368 | "};\n" |
| 369 | "void f() {\n" |
| 370 | " f();\n" |
| 371 | "}\n" |
| 372 | "}"); |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 373 | verifyFormat("inline namespace X {\n" |
| 374 | "class A {\n" |
| 375 | "};\n" |
| 376 | "void f() {\n" |
| 377 | " f();\n" |
| 378 | "}\n" |
| 379 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 380 | verifyFormat("using namespace some_namespace;\n" |
| 381 | "class A {\n" |
| 382 | "};\n" |
| 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" |
| 408 | " A() try : a(0) {\n" |
| 409 | " }\n" |
| 410 | " catch (...) {\n" |
| 411 | " throw;\n" |
| 412 | " }\n" |
| 413 | "};\n"); |
| 414 | } |
| 415 | |
| 416 | TEST_F(FormatTest, FormatObjCTryCatch) { |
| 417 | verifyFormat("@try {\n" |
| 418 | " f();\n" |
| 419 | "}\n" |
| 420 | "@catch (NSException e) {\n" |
| 421 | " @throw;\n" |
| 422 | "}\n" |
| 423 | "@finally {\n" |
| 424 | " exit(42);\n" |
| 425 | "}"); |
| 426 | } |
| 427 | |
Nico Weber | 6092d4e | 2013-01-07 19:05:19 +0000 | [diff] [blame^] | 428 | TEST_F(FormatTest, FormatObjCInterface) { |
| 429 | verifyFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" |
| 430 | "@public\n" |
| 431 | " int field1;\n" |
| 432 | "@protected\n" |
| 433 | " int field2;\n" |
| 434 | "@private\n" |
| 435 | " int field3;\n" |
| 436 | "@package\n" |
| 437 | " int field4;\n" |
| 438 | "}\n" |
| 439 | "+ (id)init;\n" |
| 440 | "@end"); |
| 441 | |
| 442 | verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" |
| 443 | " @public\n" |
| 444 | " int field1;\n" |
| 445 | " @protected\n" |
| 446 | " int field2;\n" |
| 447 | " @private\n" |
| 448 | " int field3;\n" |
| 449 | " @package\n" |
| 450 | " int field4;\n" |
| 451 | "}\n" |
| 452 | "+ (id)init;\n" |
| 453 | "@end"); |
| 454 | } |
| 455 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 456 | TEST_F(FormatTest, StaticInitializers) { |
| 457 | verifyFormat("static SomeClass SC = { 1, 'a' };"); |
| 458 | |
| 459 | // FIXME: Format like enums if the static initializer does not fit on a line. |
| 460 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 461 | "static SomeClass WithALoooooooooooooooooooongName = {\n" |
| 462 | " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 465 | TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { |
| 466 | verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 467 | " \\\n" |
| 468 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); |
| 469 | } |
| 470 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 471 | TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 472 | verifyFormat("virtual void write(ELFWriter *writerrr,\n" |
| 473 | " OwningPtr<FileOutputBuffer> &buffer) = 0;"); |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 476 | TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) { |
| 477 | EXPECT_EQ("#\n;", format("#;")); |
Manuel Klimek | dd5b101 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 478 | verifyFormat("#\n;\n;\n;"); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { |
| 482 | EXPECT_EQ("#line 42 \"test\"\n", |
| 483 | format("# \\\n line \\\n 42 \\\n \"test\"\n")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 484 | EXPECT_EQ("#define A \\\n B\n", |
| 485 | format("# \\\n define \\\n A \\\n B\n", |
| 486 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | TEST_F(FormatTest, EndOfFileEndsPPDirective) { |
| 490 | EXPECT_EQ("#line 42 \"test\"", |
| 491 | format("# \\\n line \\\n 42 \\\n \"test\"")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 492 | EXPECT_EQ("#define A \\\n B", |
| 493 | format("# \\\n define \\\n A \\\n B", |
| 494 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 497 | TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 498 | // If the macro fits in one line, we still do not get the full |
| 499 | // line, as only the next line decides whether we need an escaped newline and |
| 500 | // thus use the last column. |
| 501 | verifyFormat("#define A(B)", getLLVMStyleWithColumns(13)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 502 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 503 | verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12)); |
| 504 | verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 505 | verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); |
| 506 | } |
| 507 | |
| 508 | TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 509 | EXPECT_EQ("// some comment\n" |
| 510 | "#include \"a.h\"\n" |
| 511 | "#define A(A,\\\n" |
| 512 | " B)\n" |
| 513 | "#include \"b.h\"\n" |
| 514 | "// some comment\n", |
| 515 | format(" // some comment\n" |
| 516 | " #include \"a.h\"\n" |
| 517 | "#define A(A,\\\n" |
| 518 | " B)\n" |
| 519 | " #include \"b.h\"\n" |
| 520 | " // some comment\n", getLLVMStyleWithColumns(13))); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 523 | TEST_F(FormatTest, LayoutSingleHash) { |
| 524 | EXPECT_EQ("#\na;", format("#\na;")); |
| 525 | } |
| 526 | |
| 527 | TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { |
| 528 | EXPECT_EQ("#define A \\\n" |
| 529 | " c; \\\n" |
| 530 | " e;\n" |
| 531 | "f;", format("#define A c; e;\n" |
| 532 | "f;", getLLVMStyleWithColumns(14))); |
| 533 | } |
| 534 | |
| 535 | TEST_F(FormatTest, LayoutRemainingTokens) { |
| 536 | EXPECT_EQ("{\n}", format("{}")); |
| 537 | } |
| 538 | |
| 539 | TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) { |
| 540 | EXPECT_EQ("#define A \\\n b;", |
| 541 | format("#define A b;", 10, 2, getLLVMStyleWithColumns(11))); |
| 542 | } |
| 543 | |
| 544 | TEST_F(FormatTest, MacroDefinitionInsideStatement) { |
| 545 | EXPECT_EQ("int x,\n#define A\ny;", format("int x,\n#define A\ny;")); |
| 546 | } |
| 547 | |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 548 | TEST_F(FormatTest, HashInMacroDefinition) { |
| 549 | verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); |
| 550 | verifyFormat("#define A \\\n" |
| 551 | " { \\\n" |
| 552 | " f(#c);\\\n" |
| 553 | " }", getLLVMStyleWithColumns(11)); |
| 554 | } |
| 555 | |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 556 | TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { |
| 557 | EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); |
| 558 | } |
| 559 | |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 560 | TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { |
| 561 | verifyFormat("{\n {\n a #c;\n }\n}"); |
| 562 | } |
| 563 | |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 564 | TEST_F(FormatTest, FormatUnbalancedStructuralElements) { |
| 565 | EXPECT_EQ("#define A \\\n { \\\n {\nint i;", |
| 566 | format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); |
| 567 | EXPECT_EQ("#define A \\\n } \\\n }\nint i;", |
| 568 | format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); |
| 569 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 570 | |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 571 | TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 572 | EXPECT_EQ( |
| 573 | "#define A \\\n int i; \\\n int j;", |
| 574 | format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 577 | TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { |
| 578 | verifyFormat("#define A \\\n" |
| 579 | " int v( \\\n" |
| 580 | " a); \\\n" |
| 581 | " int i;", getLLVMStyleWithColumns(11)); |
| 582 | } |
| 583 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 584 | TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 585 | EXPECT_EQ( |
| 586 | "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 587 | " \\\n" |
| 588 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 589 | "\n" |
| 590 | "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 591 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", |
| 592 | format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 593 | "\\\n" |
| 594 | "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 595 | " \n" |
| 596 | " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 597 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 600 | //===----------------------------------------------------------------------===// |
| 601 | // Line break tests. |
| 602 | //===----------------------------------------------------------------------===// |
| 603 | |
| 604 | TEST_F(FormatTest, FormatsFunctionDefinition) { |
| 605 | verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," |
| 606 | " int h, int j, int f,\n" |
| 607 | " int c, int ddddddddddddd) {\n" |
| 608 | "}"); |
| 609 | } |
| 610 | |
| 611 | TEST_F(FormatTest, FormatsAwesomeMethodCall) { |
| 612 | verifyFormat( |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 613 | "SomeLongMethodName(SomeReallyLongMethod(\n" |
| 614 | " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n" |
| 615 | " SecondLongCall(parameter));"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 618 | TEST_F(FormatTest, ConstructorInitializers) { |
| 619 | verifyFormat("Constructor() : Initializer(FitsOnTheLine) {\n}"); |
| 620 | |
| 621 | verifyFormat( |
| 622 | "SomeClass::Constructor()\n" |
| 623 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n" |
| 624 | "}"); |
| 625 | |
| 626 | verifyFormat( |
| 627 | "SomeClass::Constructor()\n" |
| 628 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 629 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n" |
| 630 | "}"); |
| 631 | |
| 632 | verifyFormat("Constructor()\n" |
| 633 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 634 | " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 635 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 636 | " aaaaaaaaaaaaaaaaaaaaaaa() {\n" |
| 637 | "}"); |
| 638 | |
| 639 | // Here a line could be saved by splitting the second initializer onto two |
| 640 | // lines, but that is not desireable. |
| 641 | verifyFormat("Constructor()\n" |
| 642 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 643 | " aaaaaaaaaaa(aaaaaaaaaaa),\n" |
| 644 | " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 645 | "}"); |
| 646 | |
| 647 | verifyGoogleFormat("MyClass::MyClass(int var)\n" |
| 648 | " : some_var_(var), // 4 space indent\n" |
| 649 | " some_other_var_(var + 1) { // lined up\n" |
| 650 | "}"); |
| 651 | } |
| 652 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 653 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
| 654 | verifyFormat( |
| 655 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
| 656 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
| 657 | " f();"); |
| 658 | } |
| 659 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 660 | TEST_F(FormatTest, BreaksDesireably) { |
| 661 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 662 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 663 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 664 | |
| 665 | verifyFormat( |
| 666 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 667 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
| 668 | |
| 669 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 670 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 671 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 672 | |
| 673 | verifyFormat( |
| 674 | "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 675 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 676 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 677 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 678 | |
| 679 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 680 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 681 | |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 682 | verifyFormat( |
| 683 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" |
| 684 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 685 | |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 686 | // This test case breaks on an incorrect memoization, i.e. an optimization not |
| 687 | // taking into account the StopAt value. |
| 688 | verifyFormat( |
| 689 | "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 690 | " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 691 | " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 692 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 693 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 694 | verifyFormat("{\n {\n {\n" |
| 695 | " Annotation.SpaceRequiredBefore =\n" |
| 696 | " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" |
| 697 | " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" |
| 698 | " }\n }\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 701 | TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { |
| 702 | verifyFormat( |
| 703 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 704 | " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 705 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
| 706 | " ccccccccccccccccccccccccc) {\n}"); |
| 707 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
| 708 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 709 | verifyFormat( |
| 710 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" |
| 711 | " ccccccccccccccccccccccccc) {\n}"); |
| 712 | } |
| 713 | |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 714 | TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) { |
| 715 | verifyFormat( |
| 716 | "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n" |
| 717 | " SI->getAlignment(),\n" |
| 718 | " SI->getPointerAddressSpaceee());\n"); |
| 719 | verifyFormat( |
| 720 | "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" |
| 721 | " Line.Tokens.front().Tok.getLocation(),\n" |
| 722 | " Line.Tokens.back().Tok.getLocation());"); |
| 723 | } |
| 724 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 725 | TEST_F(FormatTest, AlignsAfterAssignments) { |
| 726 | verifyFormat( |
| 727 | "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 728 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 729 | verifyFormat( |
| 730 | "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 731 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 732 | verifyFormat( |
| 733 | "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 734 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 735 | verifyFormat( |
| 736 | "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 737 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 738 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 739 | "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 740 | " aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 741 | " aaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | TEST_F(FormatTest, AlignsAfterReturn) { |
| 745 | verifyFormat( |
| 746 | "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 747 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 748 | verifyFormat( |
| 749 | "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 750 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 751 | } |
| 752 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 753 | TEST_F(FormatTest, AlignsStringLiterals) { |
| 754 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
| 755 | " \"short literal\");"); |
| 756 | verifyFormat( |
| 757 | "looooooooooooooooooooooooongFunction(\n" |
| 758 | " \"short literal\"\n" |
| 759 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
| 760 | } |
| 761 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 762 | TEST_F(FormatTest, AlignsPipes) { |
| 763 | verifyFormat( |
| 764 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 765 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 766 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 767 | verifyFormat( |
| 768 | "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" |
| 769 | " << aaaaaaaaaaaaaaaaaaaa;"); |
| 770 | verifyFormat( |
| 771 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 772 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 773 | verifyFormat( |
| 774 | "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 775 | " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" |
| 776 | " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); |
| 777 | verifyFormat( |
| 778 | "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 779 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 780 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 781 | } |
| 782 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 783 | TEST_F(FormatTest, UnderstandsEquals) { |
| 784 | verifyFormat( |
| 785 | "aaaaaaaaaaaaaaaaa =\n" |
| 786 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 787 | verifyFormat( |
| 788 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 789 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 790 | "}"); |
| 791 | verifyFormat( |
| 792 | "if (a) {\n" |
| 793 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 794 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 795 | "}"); |
| 796 | |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 797 | verifyFormat( |
| 798 | "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n" |
| 799 | " 10000000) {\n" |
| 800 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 803 | TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 804 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 805 | " .looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 806 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 807 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 808 | " ->looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 809 | |
| 810 | verifyFormat( |
| 811 | "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" |
| 812 | " Parameter2);"); |
| 813 | |
| 814 | verifyFormat( |
| 815 | "ShortObject->shortFunction(\n" |
| 816 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" |
| 817 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); |
| 818 | |
| 819 | verifyFormat("loooooooooooooongFunction(\n" |
| 820 | " LoooooooooooooongObject->looooooooooooooooongFunction());"); |
| 821 | |
| 822 | verifyFormat( |
| 823 | "function(LoooooooooooooooooooooooooooooooooooongObject\n" |
| 824 | " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); |
| 825 | |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 826 | // Here, it is not necessary to wrap at "." or "->". |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 827 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" |
| 828 | " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 829 | "}"); |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 830 | verifyFormat( |
| 831 | "aaaaaaaaaaa->aaaaaaaaa(\n" |
| 832 | " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 833 | " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 836 | TEST_F(FormatTest, WrapsTemplateDeclarations) { |
| 837 | verifyFormat("template <typename T>\n" |
| 838 | "virtual void loooooooooooongFunction(int Param1, int Param2);"); |
| 839 | verifyFormat( |
| 840 | "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" |
| 841 | " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); |
| 842 | verifyFormat( |
| 843 | "template <typename T>\n" |
| 844 | "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" |
| 845 | " int Paaaaaaaaaaaaaaaaaaaaram2);"); |
Daniel Jasper | 5eda31e | 2013-01-02 18:30:06 +0000 | [diff] [blame] | 846 | verifyFormat( |
| 847 | "template <typename T>\n" |
| 848 | "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" |
| 849 | " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" |
| 850 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 851 | |
| 852 | } |
| 853 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 854 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
| 855 | verifyFormat("A<int> a;"); |
| 856 | verifyFormat("A<A<A<int> > > a;"); |
| 857 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
| 858 | verifyFormat("bool x = a < 1 || 2 > a;"); |
| 859 | verifyFormat("bool x = 5 < f<int>();"); |
| 860 | verifyFormat("bool x = f<int>() > 5;"); |
| 861 | verifyFormat("bool x = 5 < a<int>::x;"); |
| 862 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
| 863 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
| 864 | |
| 865 | verifyGoogleFormat("A<A<int>> a;"); |
| 866 | verifyGoogleFormat("A<A<A<int>>> a;"); |
| 867 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
| 868 | |
| 869 | verifyFormat("test >> a >> b;"); |
| 870 | verifyFormat("test << a >> b;"); |
| 871 | |
| 872 | verifyFormat("f<int>();"); |
| 873 | verifyFormat("template <typename T> void f() {\n}"); |
| 874 | } |
| 875 | |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 876 | TEST_F(FormatTest, UnderstandsUnaryOperators) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 877 | verifyFormat("int a = -2;"); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 878 | verifyFormat("f(-1, -2, -3);"); |
| 879 | verifyFormat("a[-1] = 5;"); |
| 880 | verifyFormat("int a = 5 + -2;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 881 | verifyFormat("if (i == -1) {\n}"); |
| 882 | verifyFormat("if (i != -1) {\n}"); |
| 883 | verifyFormat("if (i > -1) {\n}"); |
| 884 | verifyFormat("if (i < -1) {\n}"); |
Daniel Jasper | d56a737 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 885 | verifyFormat("++(a->f());"); |
| 886 | verifyFormat("--(a->f());"); |
| 887 | verifyFormat("if (!(a->f())) {\n}"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 888 | |
| 889 | verifyFormat("a-- > b;"); |
| 890 | verifyFormat("b ? -a : c;"); |
| 891 | verifyFormat("n * sizeof char16;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 892 | verifyFormat("n * alignof char16;"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 893 | verifyFormat("sizeof(char);"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 894 | verifyFormat("alignof(char);"); |
Daniel Jasper | 1f0754b | 2013-01-02 15:26:16 +0000 | [diff] [blame] | 895 | |
| 896 | verifyFormat("return -1;"); |
| 897 | verifyFormat("switch (a) {\n" |
| 898 | "case -1:\n" |
| 899 | " break;\n" |
| 900 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | TEST_F(FormatTest, UndestandsOverloadedOperators) { |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 904 | verifyFormat("bool operator<();"); |
| 905 | verifyFormat("bool operator>();"); |
| 906 | verifyFormat("bool operator=();"); |
| 907 | verifyFormat("bool operator==();"); |
| 908 | verifyFormat("bool operator!=();"); |
| 909 | verifyFormat("int operator+();"); |
| 910 | verifyFormat("int operator++();"); |
| 911 | verifyFormat("bool operator();"); |
| 912 | verifyFormat("bool operator()();"); |
| 913 | verifyFormat("bool operator[]();"); |
| 914 | verifyFormat("operator bool();"); |
| 915 | verifyFormat("operator SomeType<int>();"); |
| 916 | verifyFormat("void *operator new(std::size_t size);"); |
| 917 | verifyFormat("void *operator new[](std::size_t size);"); |
| 918 | verifyFormat("void operator delete(void *ptr);"); |
| 919 | verifyFormat("void operator delete[](void *ptr);"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 922 | TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 923 | verifyFormat("int *f(int *a) {\n}"); |
| 924 | verifyFormat("f(a, *a);"); |
| 925 | verifyFormat("f(*a);"); |
| 926 | verifyFormat("int a = b * 10;"); |
| 927 | verifyFormat("int a = 10 * b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 928 | verifyFormat("int a = b * c;"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 929 | verifyFormat("int a += b * c;"); |
| 930 | verifyFormat("int a -= b * c;"); |
| 931 | verifyFormat("int a *= b * c;"); |
| 932 | verifyFormat("int a /= b * c;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 933 | verifyFormat("int a = *b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 934 | verifyFormat("int a = *b * c;"); |
| 935 | verifyFormat("int a = b * *c;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 936 | verifyFormat("int main(int argc, char **argv) {\n}"); |
Nico Weber | 00d5a04 | 2012-12-23 01:07:46 +0000 | [diff] [blame] | 937 | verifyFormat("return 10 * b;"); |
| 938 | verifyFormat("return *b * *c;"); |
| 939 | verifyFormat("return a & ~b;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 940 | verifyFormat("f(b ? *c : *d);"); |
| 941 | verifyFormat("int a = b ? *c : *d;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 942 | verifyFormat("*b = a;"); |
| 943 | verifyFormat("a * ~b;"); |
| 944 | verifyFormat("a * !b;"); |
| 945 | verifyFormat("a * +b;"); |
| 946 | verifyFormat("a * -b;"); |
| 947 | verifyFormat("a * ++b;"); |
| 948 | verifyFormat("a * --b;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 949 | |
Daniel Jasper | 9bb0d28 | 2013-01-04 20:46:38 +0000 | [diff] [blame] | 950 | verifyFormat("InvalidRegions[*R] = 0;"); |
| 951 | |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 952 | // FIXME: Is this desired for LLVM? Fix if not. |
| 953 | verifyFormat("A<int *> a;"); |
| 954 | verifyFormat("A<int **> a;"); |
| 955 | verifyFormat("A<int *, int *> a;"); |
| 956 | verifyFormat("A<int **, int **> a;"); |
Daniel Jasper | ef5b9c3 | 2013-01-02 15:46:59 +0000 | [diff] [blame] | 957 | verifyFormat("Type *A = static_cast<Type *>(P);"); |
| 958 | verifyFormat("Type *A = (Type *) P;"); |
| 959 | verifyFormat("Type *A = (vector<Type *, int *>) P;"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 960 | |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 961 | verifyGoogleFormat("int main(int argc, char** argv) {\n}"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 962 | verifyGoogleFormat("A<int*> a;"); |
| 963 | verifyGoogleFormat("A<int**> a;"); |
| 964 | verifyGoogleFormat("A<int*, int*> a;"); |
| 965 | verifyGoogleFormat("A<int**, int**> a;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 966 | verifyGoogleFormat("f(b ? *c : *d);"); |
| 967 | verifyGoogleFormat("int a = b ? *c : *d;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 970 | TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 971 | verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n" |
| 972 | " int LoooooooooooooooongParam2) {\n}"); |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 973 | verifyFormat( |
| 974 | "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" |
| 975 | " SourceLocation L, IdentifierIn *II,\n" |
| 976 | " Type *T) {\n}"); |
| 977 | } |
| 978 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 979 | TEST_F(FormatTest, LineStartsWithSpecialCharacter) { |
| 980 | verifyFormat("(a)->b();"); |
| 981 | verifyFormat("--a;"); |
| 982 | } |
| 983 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 984 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
| 985 | EXPECT_EQ("#include <string>\n", format("#include <string>\n")); |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 986 | 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] | 987 | EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n")); |
| 988 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
| 989 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
Nico Weber | b23ae0c | 2012-12-21 18:21:56 +0000 | [diff] [blame] | 990 | |
| 991 | EXPECT_EQ("#import <string>\n", format("#import <string>\n")); |
| 992 | EXPECT_EQ("#import <a/b/c.h>\n", format("#import <a/b/c.h>\n")); |
| 993 | EXPECT_EQ("#import \"a/b/string\"\n", format("#import \"a/b/string\"\n")); |
| 994 | EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); |
| 995 | EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 998 | //===----------------------------------------------------------------------===// |
| 999 | // Error recovery tests. |
| 1000 | //===----------------------------------------------------------------------===// |
| 1001 | |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1002 | TEST_F(FormatTest, IncorrectAccessSpecifier) { |
| 1003 | verifyFormat("public:"); |
| 1004 | verifyFormat("class A {\n" |
| 1005 | "public\n" |
| 1006 | " void f() {\n" |
| 1007 | " }\n" |
| 1008 | "};"); |
| 1009 | verifyFormat("public\n" |
| 1010 | "int qwerty;"); |
| 1011 | verifyFormat("public\n" |
| 1012 | "B {\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1013 | "}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1014 | verifyFormat("public\n" |
| 1015 | "{\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1016 | "}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1017 | verifyFormat("public\n" |
| 1018 | "B {\n" |
| 1019 | " int x;\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1020 | "}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1021 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1022 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1023 | TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { |
| 1024 | verifyFormat("{"); |
| 1025 | } |
| 1026 | |
| 1027 | TEST_F(FormatTest, IncorrectCodeDoNoWhile) { |
| 1028 | verifyFormat("do {\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1029 | "}"); |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1030 | verifyFormat("do {\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1031 | "}\n" |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1032 | "f();"); |
| 1033 | verifyFormat("do {\n" |
| 1034 | "}\n" |
| 1035 | "wheeee(fun);"); |
| 1036 | verifyFormat("do {\n" |
| 1037 | " f();\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1038 | "}"); |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1041 | TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { |
| 1042 | verifyFormat("namespace {\n" |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1043 | "class Foo { Foo ( }; } // comment"); |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1046 | TEST_F(FormatTest, IncorrectCodeErrorDetection) { |
| 1047 | EXPECT_EQ("{\n{\n}\n", format("{\n{\n}\n")); |
| 1048 | EXPECT_EQ("{\n {\n}\n", format("{\n {\n}\n")); |
| 1049 | EXPECT_EQ("{\n {\n }\n", format("{\n {\n }\n")); |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 1050 | EXPECT_EQ("{\n {\n }\n }\n}\n", format("{\n {\n }\n }\n}\n")); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1051 | |
| 1052 | FormatStyle Style = getLLVMStyle(); |
| 1053 | Style.ColumnLimit = 10; |
| 1054 | EXPECT_EQ("{\n" |
| 1055 | " {\n" |
| 1056 | " breakme(\n" |
| 1057 | " qwe);\n" |
| 1058 | "}\n", format("{\n" |
| 1059 | " {\n" |
| 1060 | " breakme(qwe);\n" |
| 1061 | "}\n", Style)); |
| 1062 | |
| 1063 | } |
| 1064 | |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1065 | TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { |
| 1066 | verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); |
| 1067 | EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", |
| 1068 | format("-(NSUInteger)indexOfObject:(id)anObject;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1069 | EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1070 | EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); |
| 1071 | EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", |
| 1072 | format("-(NSInteger)Method3:(id)anObject;")); |
| 1073 | EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", |
| 1074 | format("-(NSInteger)Method4:(id)anObject;")); |
| 1075 | EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", |
| 1076 | format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); |
| 1077 | EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", |
| 1078 | format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1079 | EXPECT_EQ( |
| 1080 | "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", |
| 1081 | format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); |
Fariborz Jahanian | 9b3f02c | 2012-12-21 22:51:18 +0000 | [diff] [blame] | 1082 | |
| 1083 | // Very long objectiveC method declaration. |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1084 | EXPECT_EQ( |
| 1085 | "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n " |
| 1086 | "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n " |
| 1087 | "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n " |
| 1088 | "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n " |
| 1089 | "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n " |
| 1090 | "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;", |
| 1091 | format( |
| 1092 | "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range " |
| 1093 | "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 " |
| 1094 | "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 " |
| 1095 | "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 " |
| 1096 | "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 " |
| 1097 | "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;")); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 1100 | TEST_F(FormatTest, ObjCAt) { |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1101 | verifyFormat("@autoreleasepool"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1102 | verifyFormat("@catch"); |
| 1103 | verifyFormat("@class"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1104 | verifyFormat("@compatibility_alias"); |
| 1105 | verifyFormat("@defs"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 1106 | verifyFormat("@dynamic"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1107 | verifyFormat("@encode"); |
| 1108 | verifyFormat("@end"); |
| 1109 | verifyFormat("@finally"); |
| 1110 | verifyFormat("@implementation"); |
| 1111 | verifyFormat("@import"); |
| 1112 | verifyFormat("@interface"); |
| 1113 | verifyFormat("@optional"); |
| 1114 | verifyFormat("@package"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1115 | verifyFormat("@private"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1116 | verifyFormat("@property"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1117 | verifyFormat("@protected"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1118 | verifyFormat("@protocol"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1119 | verifyFormat("@public"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1120 | verifyFormat("@required"); |
| 1121 | verifyFormat("@selector"); |
| 1122 | verifyFormat("@synchronized"); |
| 1123 | verifyFormat("@synthesize"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1124 | verifyFormat("@throw"); |
| 1125 | verifyFormat("@try"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1126 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 1127 | EXPECT_EQ("@interface", format("@ interface")); |
| 1128 | |
| 1129 | // The precise formatting of this doesn't matter, nobody writes code like |
| 1130 | // this. |
| 1131 | verifyFormat("@ /*foo*/ interface"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1134 | } // end namespace tooling |
| 1135 | } // end namespace clang |