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 | |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 10 | #define DEBUG_TYPE "format-test" |
| 11 | |
Chandler Carruth | 1050e8b | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 12 | #include "clang/Format/Format.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 13 | #include "../Tooling/RewriterTestContext.h" |
Chandler Carruth | b1ba0ef | 2013-01-19 08:09:44 +0000 | [diff] [blame] | 14 | #include "clang/Lex/Lexer.h" |
| 15 | #include "llvm/Support/Debug.h" |
| 16 | #include "gtest/gtest.h" |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 17 | |
| 18 | // Uncomment to get debug output from tests: |
| 19 | // #define DEBUG_WITH_TYPE(T, X) do { X; } while(0) |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 20 | |
| 21 | namespace clang { |
| 22 | namespace format { |
| 23 | |
| 24 | class FormatTest : public ::testing::Test { |
| 25 | protected: |
| 26 | std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, |
| 27 | const FormatStyle &Style) { |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 28 | DEBUG(llvm::errs() << "---\n"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 29 | RewriterTestContext Context; |
| 30 | FileID ID = Context.createInMemoryFile("input.cc", Code); |
| 31 | SourceLocation Start = |
| 32 | Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset); |
| 33 | std::vector<CharSourceRange> Ranges( |
| 34 | 1, |
| 35 | CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length))); |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 36 | Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, |
| 37 | getFormattingLangOpts()); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 38 | tooling::Replacements Replace = reformat(Style, Lex, Context.Sources, |
Alexander Kornienko | a4ae9f3 | 2013-01-14 11:34:14 +0000 | [diff] [blame] | 39 | Ranges, |
| 40 | new IgnoringDiagConsumer()); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 41 | EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 42 | DEBUG(llvm::errs() << "\n" << Context.getRewrittenText(ID) << "\n\n"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 43 | return Context.getRewrittenText(ID); |
| 44 | } |
| 45 | |
| 46 | std::string format(llvm::StringRef Code, |
| 47 | const FormatStyle &Style = getLLVMStyle()) { |
| 48 | return format(Code, 0, Code.size(), Style); |
| 49 | } |
| 50 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 51 | std::string messUp(llvm::StringRef Code) { |
| 52 | std::string MessedUp(Code.str()); |
| 53 | bool InComment = false; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 54 | bool InPreprocessorDirective = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 55 | bool JustReplacedNewline = false; |
| 56 | for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { |
| 57 | if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { |
| 58 | if (JustReplacedNewline) |
| 59 | MessedUp[i - 1] = '\n'; |
| 60 | InComment = true; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 61 | } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) { |
| 62 | if (i != 0) MessedUp[i - 1] = '\n'; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 63 | InPreprocessorDirective = true; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 64 | } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') { |
| 65 | MessedUp[i] = ' '; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 66 | MessedUp[i + 1] = ' '; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 67 | } else if (MessedUp[i] == '\n') { |
| 68 | if (InComment) { |
| 69 | InComment = false; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 70 | } else if (InPreprocessorDirective) { |
| 71 | InPreprocessorDirective = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 72 | } else { |
| 73 | JustReplacedNewline = true; |
| 74 | MessedUp[i] = ' '; |
| 75 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 76 | } else if (MessedUp[i] != ' ') { |
| 77 | JustReplacedNewline = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 78 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 79 | } |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 80 | return MessedUp; |
| 81 | } |
| 82 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 83 | FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { |
| 84 | FormatStyle Style = getLLVMStyle(); |
| 85 | Style.ColumnLimit = ColumnLimit; |
| 86 | return Style; |
| 87 | } |
| 88 | |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 89 | FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { |
| 90 | FormatStyle Style = getGoogleStyle(); |
| 91 | Style.ColumnLimit = ColumnLimit; |
| 92 | return Style; |
| 93 | } |
| 94 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 95 | void verifyFormat(llvm::StringRef Code, |
| 96 | const FormatStyle &Style = getLLVMStyle()) { |
| 97 | EXPECT_EQ(Code.str(), format(messUp(Code), Style)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void verifyGoogleFormat(llvm::StringRef Code) { |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 101 | verifyFormat(Code, getGoogleStyle()); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 102 | } |
| 103 | }; |
| 104 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 105 | TEST_F(FormatTest, MessUp) { |
| 106 | EXPECT_EQ("1 2 3", messUp("1 2 3")); |
| 107 | EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n")); |
| 108 | EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc")); |
| 109 | EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc")); |
| 110 | EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne")); |
| 111 | } |
| 112 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 113 | //===----------------------------------------------------------------------===// |
| 114 | // Basic function tests. |
| 115 | //===----------------------------------------------------------------------===// |
| 116 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 117 | TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { |
| 118 | EXPECT_EQ(";", format(";")); |
| 119 | } |
| 120 | |
| 121 | TEST_F(FormatTest, FormatsGlobalStatementsAt0) { |
| 122 | EXPECT_EQ("int i;", format(" int i;")); |
| 123 | EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); |
| 124 | EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); |
| 125 | EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); |
| 126 | } |
| 127 | |
| 128 | TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { |
| 129 | EXPECT_EQ("int i;", format("int\ni;")); |
| 130 | } |
| 131 | |
| 132 | TEST_F(FormatTest, FormatsNestedBlockStatements) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 133 | EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 136 | TEST_F(FormatTest, FormatsNestedCall) { |
| 137 | verifyFormat("Method(f1, f2(f3));"); |
| 138 | verifyFormat("Method(f1(f2, f3()));"); |
Daniel Jasper | 4e9008a | 2013-01-13 08:19:51 +0000 | [diff] [blame] | 139 | verifyFormat("Method(f1(f2, (f3())));"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Daniel Jasper | 6b825c2 | 2013-01-16 07:19:28 +0000 | [diff] [blame] | 142 | TEST_F(FormatTest, ImportantSpaces) { |
| 143 | verifyFormat("vector< ::Type> v;"); |
| 144 | } |
| 145 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 146 | //===----------------------------------------------------------------------===// |
| 147 | // Tests for control statements. |
| 148 | //===----------------------------------------------------------------------===// |
| 149 | |
| 150 | TEST_F(FormatTest, FormatIfWithoutCompountStatement) { |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 151 | verifyFormat("if (true)\n f();\ng();"); |
| 152 | verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 153 | verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 154 | |
| 155 | FormatStyle AllowsMergedIf = getGoogleStyle(); |
| 156 | AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; |
| 157 | verifyFormat("if (a)\n" |
| 158 | " // comment\n" |
| 159 | " f();", AllowsMergedIf); |
| 160 | |
| 161 | verifyFormat("if (a) // Can't merge this\n" |
| 162 | " f();\n", AllowsMergedIf); |
| 163 | verifyFormat("if (a) /* still don't merge */\n" |
| 164 | " f();", AllowsMergedIf); |
| 165 | verifyFormat("if (a) { // Never merge this\n" |
| 166 | " f();\n" |
| 167 | "}", AllowsMergedIf); |
| 168 | verifyFormat("if (a) { /* Never merge this */\n" |
| 169 | " f();\n" |
| 170 | "}", AllowsMergedIf); |
| 171 | |
| 172 | AllowsMergedIf.ColumnLimit = 14; |
| 173 | verifyFormat("if (a) return;", AllowsMergedIf); |
Daniel Jasper | fd0ca97 | 2013-01-14 16:02:06 +0000 | [diff] [blame] | 174 | verifyFormat("if (aaaaaaaaa)\n" |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 175 | " return;", AllowsMergedIf); |
| 176 | |
| 177 | AllowsMergedIf.ColumnLimit = 13; |
| 178 | verifyFormat("if (a)\n return;", AllowsMergedIf); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | TEST_F(FormatTest, ParseIfElse) { |
| 182 | verifyFormat("if (true)\n" |
| 183 | " if (true)\n" |
| 184 | " if (true)\n" |
| 185 | " f();\n" |
| 186 | " else\n" |
| 187 | " g();\n" |
| 188 | " else\n" |
| 189 | " h();\n" |
| 190 | "else\n" |
| 191 | " i();"); |
| 192 | verifyFormat("if (true)\n" |
| 193 | " if (true)\n" |
| 194 | " if (true) {\n" |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 195 | " if (true)\n" |
| 196 | " f();\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 197 | " } else {\n" |
| 198 | " g();\n" |
| 199 | " }\n" |
| 200 | " else\n" |
| 201 | " h();\n" |
| 202 | "else {\n" |
| 203 | " i();\n" |
| 204 | "}"); |
| 205 | } |
| 206 | |
| 207 | TEST_F(FormatTest, ElseIf) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 208 | verifyFormat("if (a) {\n} else if (b) {\n}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 209 | verifyFormat("if (a)\n" |
| 210 | " f();\n" |
| 211 | "else if (b)\n" |
| 212 | " g();\n" |
| 213 | "else\n" |
| 214 | " h();"); |
| 215 | } |
| 216 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 217 | TEST_F(FormatTest, FormatsForLoop) { |
| 218 | verifyFormat( |
| 219 | "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 220 | " ++VeryVeryLongLoopVariable)\n" |
| 221 | " ;"); |
| 222 | verifyFormat("for (;;)\n" |
| 223 | " f();"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 224 | verifyFormat("for (;;) {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 225 | verifyFormat("for (;;) {\n" |
| 226 | " f();\n" |
| 227 | "}"); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 228 | |
| 229 | verifyFormat( |
| 230 | "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" |
| 231 | " E = UnwrappedLines.end();\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 232 | " I != E; ++I) {\n}"); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 233 | |
| 234 | verifyFormat( |
| 235 | "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 236 | " ++IIIII) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | TEST_F(FormatTest, FormatsWhileLoop) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 240 | verifyFormat("while (true) {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 241 | verifyFormat("while (true)\n" |
| 242 | " f();"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 243 | verifyFormat("while () {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 244 | verifyFormat("while () {\n" |
| 245 | " f();\n" |
| 246 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 249 | TEST_F(FormatTest, FormatsDoWhile) { |
| 250 | verifyFormat("do {\n" |
| 251 | " do_something();\n" |
| 252 | "} while (something());"); |
| 253 | verifyFormat("do\n" |
| 254 | " do_something();\n" |
| 255 | "while (something());"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 258 | TEST_F(FormatTest, FormatsSwitchStatement) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 259 | verifyFormat("switch (x) {\n" |
| 260 | "case 1:\n" |
| 261 | " f();\n" |
| 262 | " break;\n" |
| 263 | "case kFoo:\n" |
| 264 | "case ns::kBar:\n" |
| 265 | "case kBaz:\n" |
| 266 | " break;\n" |
| 267 | "default:\n" |
| 268 | " g();\n" |
| 269 | " break;\n" |
| 270 | "}"); |
| 271 | verifyFormat("switch (x) {\n" |
| 272 | "case 1: {\n" |
| 273 | " f();\n" |
| 274 | " break;\n" |
| 275 | "}\n" |
| 276 | "}"); |
Nico Weber | 94fb729 | 2013-01-18 05:50:57 +0000 | [diff] [blame] | 277 | verifyFormat("switch (x) {\n" |
| 278 | "case 1: {\n" |
| 279 | " f();\n" |
| 280 | " {\n" |
| 281 | " g();\n" |
| 282 | " h();\n" |
| 283 | " }\n" |
| 284 | " break;\n" |
| 285 | "}\n" |
| 286 | "}"); |
| 287 | verifyFormat("switch (x) {\n" |
| 288 | "case 1: {\n" |
| 289 | " f();\n" |
| 290 | " if (foo) {\n" |
| 291 | " g();\n" |
| 292 | " h();\n" |
| 293 | " }\n" |
| 294 | " break;\n" |
| 295 | "}\n" |
| 296 | "}"); |
| 297 | verifyFormat("switch (x) {\n" |
| 298 | "case 1: {\n" |
| 299 | " f();\n" |
| 300 | " g();\n" |
| 301 | "} break;\n" |
| 302 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 303 | verifyFormat("switch (test)\n" |
| 304 | " ;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 305 | verifyGoogleFormat("switch (x) {\n" |
| 306 | " case 1:\n" |
| 307 | " f();\n" |
| 308 | " break;\n" |
| 309 | " case kFoo:\n" |
| 310 | " case ns::kBar:\n" |
| 311 | " case kBaz:\n" |
| 312 | " break;\n" |
| 313 | " default:\n" |
| 314 | " g();\n" |
| 315 | " break;\n" |
| 316 | "}"); |
| 317 | verifyGoogleFormat("switch (x) {\n" |
| 318 | " case 1: {\n" |
| 319 | " f();\n" |
| 320 | " break;\n" |
| 321 | " }\n" |
| 322 | "}"); |
| 323 | verifyGoogleFormat("switch (test)\n" |
| 324 | " ;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 327 | TEST_F(FormatTest, FormatsLabels) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 328 | verifyFormat("void f() {\n" |
| 329 | " some_code();\n" |
| 330 | "test_label:\n" |
| 331 | " some_other_code();\n" |
| 332 | " {\n" |
| 333 | " some_more_code();\n" |
| 334 | " another_label:\n" |
| 335 | " some_more_code();\n" |
| 336 | " }\n" |
| 337 | "}"); |
| 338 | verifyFormat("some_code();\n" |
| 339 | "test_label:\n" |
| 340 | "some_other_code();"); |
| 341 | } |
| 342 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 343 | //===----------------------------------------------------------------------===// |
| 344 | // Tests for comments. |
| 345 | //===----------------------------------------------------------------------===// |
| 346 | |
| 347 | TEST_F(FormatTest, UnderstandsSingleLineComments) { |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 348 | verifyFormat("// line 1\n" |
| 349 | "// line 2\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 350 | "void f() {}\n"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 351 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 352 | verifyFormat("void f() {\n" |
| 353 | " // Doesn't do anything\n" |
| 354 | "}"); |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 355 | verifyFormat("void f(int i, // some comment (probably for i)\n" |
| 356 | " int j, // some comment (probably for j)\n" |
Daniel Jasper | 487f64b | 2013-01-13 16:10:20 +0000 | [diff] [blame] | 357 | " int k); // some comment (probably for k)"); |
| 358 | verifyFormat("void f(int i,\n" |
| 359 | " // some comment (probably for j)\n" |
| 360 | " int j,\n" |
| 361 | " // some comment (probably for k)\n" |
| 362 | " int k);"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 363 | |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 364 | verifyFormat("int i // This is a fancy variable\n" |
| 365 | " = 5; // with nicely aligned comment."); |
| 366 | |
| 367 | verifyFormat("// Leading comment.\n" |
| 368 | "int a; // Trailing comment."); |
| 369 | verifyFormat("int a; // Trailing comment\n" |
| 370 | " // on 2\n" |
| 371 | " // or 3 lines.\n" |
| 372 | "int b;"); |
| 373 | verifyFormat("int a; // Trailing comment\n" |
| 374 | "\n" |
| 375 | "// Leading comment.\n" |
| 376 | "int b;"); |
| 377 | verifyFormat("int a; // Comment.\n" |
| 378 | " // More details.\n" |
| 379 | "int bbbb; // Another comment."); |
| 380 | verifyFormat( |
| 381 | "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" |
| 382 | "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" |
| 383 | "int cccccccccccccccccccccccccccccc; // comment\n" |
| 384 | "int ddd; // looooooooooooooooooooooooong comment\n" |
| 385 | "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" |
| 386 | "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" |
| 387 | "int ccccccccccccccccccc; // comment"); |
| 388 | |
Daniel Jasper | 7d1185d | 2013-01-18 09:19:33 +0000 | [diff] [blame] | 389 | verifyFormat("#include \"a\" // comment\n" |
| 390 | "#include \"a/b/c\" // comment"); |
| 391 | verifyFormat("#include <a> // comment\n" |
| 392 | "#include <a/b/c> // comment"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 393 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 394 | verifyFormat("enum E {\n" |
| 395 | " // comment\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 396 | " VAL_A, // comment\n" |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 397 | " VAL_B\n" |
| 398 | "};"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 399 | |
| 400 | verifyFormat( |
| 401 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 402 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); |
Daniel Jasper | 043835a | 2013-01-09 09:33:39 +0000 | [diff] [blame] | 403 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 404 | " // Comment inside a statement.\n" |
| 405 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 406 | |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 407 | EXPECT_EQ("int i; // single line trailing comment", |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 408 | format("int i;\\\n// single line trailing comment")); |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 409 | |
| 410 | verifyGoogleFormat("int a; // Trailing comment."); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | TEST_F(FormatTest, UnderstandsMultiLineComments) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 414 | verifyFormat("f(/*test=*/ true);"); |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 415 | EXPECT_EQ( |
| 416 | "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" |
| 417 | " bbbbbbbbbbbbbbbbbbbbbbbbb);", |
| 418 | format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , /* Trailing comment for aa... */\n" |
| 419 | " bbbbbbbbbbbbbbbbbbbbbbbbb);")); |
| 420 | EXPECT_EQ( |
| 421 | "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 422 | " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", |
| 423 | format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" |
| 424 | "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); |
| 425 | } |
| 426 | |
| 427 | TEST_F(FormatTest, CommentsInStaticInitializers) { |
| 428 | EXPECT_EQ( |
| 429 | "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n" |
| 430 | " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" |
| 431 | " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" |
| 432 | " aaaaaaaaaaaaaaaaaaaa, // comment\n" |
| 433 | " aaaaaaaaaaaaaaaaaaaa };", |
| 434 | format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" |
| 435 | " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" |
| 436 | " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" |
| 437 | " aaaaaaaaaaaaaaaaaaaa , // comment\n" |
| 438 | " aaaaaaaaaaaaaaaaaaaa };")); |
| 439 | verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n" |
| 440 | " bbbbbbbbbbb, ccccccccccc };"); |
| 441 | verifyFormat("static SomeType type = { aaaaaaaaaaa,\n" |
| 442 | " // comment for bb....\n" |
| 443 | " bbbbbbbbbbb, ccccccccccc };"); |
| 444 | verifyGoogleFormat( |
| 445 | "static SomeType type = { aaaaaaaaaaa, // comment for aa...\n" |
| 446 | " bbbbbbbbbbb,\n" |
| 447 | " ccccccccccc };"); |
| 448 | verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n" |
| 449 | " // comment for bb....\n" |
| 450 | " bbbbbbbbbbb,\n" |
| 451 | " ccccccccccc };"); |
| 452 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 455 | //===----------------------------------------------------------------------===// |
| 456 | // Tests for classes, namespaces, etc. |
| 457 | //===----------------------------------------------------------------------===// |
| 458 | |
| 459 | TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 460 | verifyFormat("class A {\n};"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | TEST_F(FormatTest, UnderstandsAccessSpecifiers) { |
| 464 | verifyFormat("class A {\n" |
| 465 | "public:\n" |
| 466 | "protected:\n" |
| 467 | "private:\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 468 | " void f() {}\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 469 | "};"); |
| 470 | verifyGoogleFormat("class A {\n" |
| 471 | " public:\n" |
| 472 | " protected:\n" |
| 473 | " private:\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 474 | " void f() {}\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 475 | "};"); |
| 476 | } |
| 477 | |
| 478 | TEST_F(FormatTest, FormatsDerivedClass) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 479 | verifyFormat("class A : public B {\n};"); |
| 480 | verifyFormat("class A : public ::B {\n};"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 483 | TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 484 | verifyFormat("class A {\n} a, b;"); |
| 485 | verifyFormat("struct A {\n} a, b;"); |
| 486 | verifyFormat("union A {\n} a;"); |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 489 | TEST_F(FormatTest, FormatsEnum) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 490 | verifyFormat("enum {\n" |
| 491 | " Zero,\n" |
| 492 | " One = 1,\n" |
| 493 | " Two = One + 1,\n" |
| 494 | " Three = (One + Two),\n" |
| 495 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
| 496 | " Five = (One, Two, Three, Four, 5)\n" |
| 497 | "};"); |
| 498 | verifyFormat("enum Enum {\n" |
| 499 | "};"); |
| 500 | verifyFormat("enum {\n" |
| 501 | "};"); |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 502 | verifyFormat("enum X E {\n} d;"); |
| 503 | verifyFormat("enum __attribute__((...)) E {\n} d;"); |
| 504 | verifyFormat("enum __declspec__((...)) E {\n} d;"); |
| 505 | verifyFormat("enum X f() {\n a();\n return 42;\n}"); |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Nico Weber | efaddc0 | 2013-01-14 05:49:49 +0000 | [diff] [blame] | 508 | TEST_F(FormatTest, FormatsBitfields) { |
| 509 | verifyFormat("struct Bitfields {\n" |
| 510 | " unsigned sClass : 8;\n" |
| 511 | " unsigned ValueKind : 2;\n" |
| 512 | "};"); |
| 513 | } |
| 514 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 515 | TEST_F(FormatTest, FormatsNamespaces) { |
| 516 | verifyFormat("namespace some_namespace {\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 517 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 518 | "void f() { f(); }\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 519 | "}"); |
| 520 | verifyFormat("namespace {\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 521 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 522 | "void f() { f(); }\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 523 | "}"); |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 524 | verifyFormat("inline namespace X {\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 525 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 526 | "void f() { f(); }\n" |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 527 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 528 | verifyFormat("using namespace some_namespace;\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 529 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 530 | "void f() { f(); }"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 533 | TEST_F(FormatTest, FormatsExternC) { |
| 534 | verifyFormat("extern \"C\" {\nint a;"); |
| 535 | } |
| 536 | |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 537 | TEST_F(FormatTest, FormatTryCatch) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 538 | // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll |
| 539 | // also not create single-line-blocks. |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 540 | verifyFormat("try {\n" |
| 541 | " throw a * b;\n" |
| 542 | "}\n" |
| 543 | "catch (int a) {\n" |
| 544 | " // Do nothing.\n" |
| 545 | "}\n" |
| 546 | "catch (...) {\n" |
| 547 | " exit(42);\n" |
| 548 | "}"); |
| 549 | |
| 550 | // Function-level try statements. |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 551 | verifyFormat("int f() try { return 4; }\n" |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 552 | "catch (...) {\n" |
| 553 | " return 5;\n" |
| 554 | "}"); |
| 555 | verifyFormat("class A {\n" |
| 556 | " int a;\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 557 | " A() try : a(0) {}\n" |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 558 | " catch (...) {\n" |
| 559 | " throw;\n" |
| 560 | " }\n" |
| 561 | "};\n"); |
| 562 | } |
| 563 | |
| 564 | TEST_F(FormatTest, FormatObjCTryCatch) { |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 565 | verifyFormat("@try {\n" |
| 566 | " f();\n" |
| 567 | "}\n" |
| 568 | "@catch (NSException e) {\n" |
| 569 | " @throw;\n" |
| 570 | "}\n" |
| 571 | "@finally {\n" |
| 572 | " exit(42);\n" |
| 573 | "}"); |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 576 | TEST_F(FormatTest, StaticInitializers) { |
| 577 | verifyFormat("static SomeClass SC = { 1, 'a' };"); |
| 578 | |
| 579 | // FIXME: Format like enums if the static initializer does not fit on a line. |
| 580 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 581 | "static SomeClass WithALoooooooooooooooooooongName = {\n" |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 582 | " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 583 | "};"); |
| 584 | |
| 585 | verifyFormat( |
| 586 | "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" |
| 587 | " looooooooooooooooooooooooooooooooooongname,\n" |
| 588 | " looooooooooooooooooooooooooooooong };"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 591 | TEST_F(FormatTest, NestedStaticInitializers) { |
| 592 | verifyFormat("static A x = { { {} } };\n"); |
| 593 | verifyFormat( |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 594 | "static A x = { { { init1, init2, init3, init4 },\n" |
| 595 | " { init1, init2, init3, init4 } } };"); |
| 596 | |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 597 | // FIXME: Fix this in general and verify that it works in LLVM style again. |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 598 | verifyGoogleFormat( |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 599 | "somes Status::global_reps[3] = {\n" |
| 600 | " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n" |
| 601 | " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n" |
| 602 | " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n" |
| 603 | "};"); |
| 604 | verifyFormat( |
| 605 | "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n" |
| 606 | " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop" |
| 607 | " } };"); |
| 608 | |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 609 | // FIXME: We might at some point want to handle this similar to parameter |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 610 | // lists, where we have an option to put each on a single line. |
| 611 | verifyFormat("struct {\n" |
| 612 | " unsigned bit;\n" |
| 613 | " const char *const name;\n" |
| 614 | "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n" |
| 615 | " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };"); |
| 616 | } |
| 617 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 618 | TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { |
| 619 | verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 620 | " \\\n" |
| 621 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); |
| 622 | } |
| 623 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 624 | TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 625 | verifyFormat("virtual void write(ELFWriter *writerrr,\n" |
| 626 | " OwningPtr<FileOutputBuffer> &buffer) = 0;"); |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 629 | TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) { |
| 630 | EXPECT_EQ("#\n;", format("#;")); |
Manuel Klimek | dd5b101 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 631 | verifyFormat("#\n;\n;\n;"); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { |
| 635 | EXPECT_EQ("#line 42 \"test\"\n", |
| 636 | format("# \\\n line \\\n 42 \\\n \"test\"\n")); |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 637 | EXPECT_EQ("#define A B\n", |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 638 | format("# \\\n define \\\n A \\\n B\n", |
| 639 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | TEST_F(FormatTest, EndOfFileEndsPPDirective) { |
| 643 | EXPECT_EQ("#line 42 \"test\"", |
| 644 | format("# \\\n line \\\n 42 \\\n \"test\"")); |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 645 | EXPECT_EQ("#define A B", |
| 646 | format("# \\\n define \\\n A \\\n B")); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 649 | TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 650 | // If the macro fits in one line, we still do not get the full |
| 651 | // line, as only the next line decides whether we need an escaped newline and |
| 652 | // thus use the last column. |
| 653 | verifyFormat("#define A(B)", getLLVMStyleWithColumns(13)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 654 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 655 | verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12)); |
| 656 | verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 657 | verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 658 | |
| 659 | verifyFormat("#define A A\n#define A A"); |
| 660 | verifyFormat("#define A(X) A\n#define A A"); |
| 661 | |
| 662 | verifyFormat("#define Something Other", getLLVMStyleWithColumns(24)); |
| 663 | verifyFormat("#define Something \\\n" |
| 664 | " Other", getLLVMStyleWithColumns(23)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 668 | EXPECT_EQ("// some comment\n" |
| 669 | "#include \"a.h\"\n" |
| 670 | "#define A(A,\\\n" |
| 671 | " B)\n" |
| 672 | "#include \"b.h\"\n" |
| 673 | "// some comment\n", |
| 674 | format(" // some comment\n" |
| 675 | " #include \"a.h\"\n" |
| 676 | "#define A(A,\\\n" |
| 677 | " B)\n" |
| 678 | " #include \"b.h\"\n" |
| 679 | " // some comment\n", getLLVMStyleWithColumns(13))); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 682 | TEST_F(FormatTest, LayoutSingleHash) { |
| 683 | EXPECT_EQ("#\na;", format("#\na;")); |
| 684 | } |
| 685 | |
| 686 | TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { |
| 687 | EXPECT_EQ("#define A \\\n" |
| 688 | " c; \\\n" |
| 689 | " e;\n" |
| 690 | "f;", format("#define A c; e;\n" |
| 691 | "f;", getLLVMStyleWithColumns(14))); |
| 692 | } |
| 693 | |
| 694 | TEST_F(FormatTest, LayoutRemainingTokens) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 695 | EXPECT_EQ("{}", format("{}")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 699 | EXPECT_EQ("# define A\\\n b;", |
| 700 | format("# define A b;", 11, 2, getLLVMStyleWithColumns(11))); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | TEST_F(FormatTest, MacroDefinitionInsideStatement) { |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 704 | EXPECT_EQ("int x,\n" |
| 705 | "#define A\n" |
| 706 | " y;", format("int x,\n#define A\ny;")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 709 | TEST_F(FormatTest, HashInMacroDefinition) { |
| 710 | verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); |
| 711 | verifyFormat("#define A \\\n" |
| 712 | " { \\\n" |
| 713 | " f(#c);\\\n" |
| 714 | " }", getLLVMStyleWithColumns(11)); |
Daniel Jasper | 765561f | 2013-01-08 16:17:54 +0000 | [diff] [blame] | 715 | |
| 716 | verifyFormat("#define A(X) \\\n" |
| 717 | " void function##X()", getLLVMStyleWithColumns(22)); |
| 718 | |
| 719 | verifyFormat("#define A(a, b, c) \\\n" |
| 720 | " void a##b##c()", getLLVMStyleWithColumns(22)); |
| 721 | |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 722 | verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 725 | TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { |
| 726 | EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); |
| 727 | } |
| 728 | |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 729 | TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 730 | verifyFormat("{\n { a #c; }\n}"); |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 733 | TEST_F(FormatTest, FormatUnbalancedStructuralElements) { |
| 734 | EXPECT_EQ("#define A \\\n { \\\n {\nint i;", |
| 735 | format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); |
| 736 | EXPECT_EQ("#define A \\\n } \\\n }\nint i;", |
| 737 | format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); |
| 738 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 739 | |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 740 | TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 741 | EXPECT_EQ( |
| 742 | "#define A \\\n int i; \\\n int j;", |
| 743 | format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 746 | TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { |
| 747 | verifyFormat("#define A \\\n" |
| 748 | " int v( \\\n" |
| 749 | " a); \\\n" |
| 750 | " int i;", getLLVMStyleWithColumns(11)); |
| 751 | } |
| 752 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 753 | TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 754 | EXPECT_EQ( |
| 755 | "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 756 | " \\\n" |
| 757 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 758 | "\n" |
| 759 | "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 760 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", |
| 761 | format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 762 | "\\\n" |
| 763 | "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 764 | " \n" |
| 765 | " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 766 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 769 | TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { |
| 770 | EXPECT_EQ("int\n" |
| 771 | "#define A\n" |
| 772 | " a;", |
| 773 | format("int\n#define A\na;")); |
| 774 | verifyFormat( |
| 775 | "functionCallTo(someOtherFunction(\n" |
| 776 | " withSomeParameters, whichInSequence,\n" |
| 777 | " areLongerThanALine(andAnotherCall,\n" |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 778 | "#define A B\n" |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 779 | " withMoreParamters,\n" |
| 780 | " whichStronglyInfluenceTheLayout),\n" |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 781 | " andMoreParameters), trailing);", getLLVMStyleWithColumns(69)); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 784 | TEST_F(FormatTest, LayoutBlockInsideParens) { |
| 785 | EXPECT_EQ("functionCall({\n" |
| 786 | " int i;\n" |
| 787 | "});", format(" functionCall ( {int i;} );")); |
| 788 | } |
| 789 | |
| 790 | TEST_F(FormatTest, LayoutBlockInsideStatement) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 791 | EXPECT_EQ("SOME_MACRO { int i; }\n" |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 792 | "int i;", format(" SOME_MACRO {int i;} int i;")); |
| 793 | } |
| 794 | |
| 795 | TEST_F(FormatTest, LayoutNestedBlocks) { |
| 796 | verifyFormat("void AddOsStrings(unsigned bitmask) {\n" |
| 797 | " struct s {\n" |
| 798 | " int i;\n" |
| 799 | " };\n" |
| 800 | " s kBitsToOs[] = { { 10 } };\n" |
| 801 | " for (int i = 0; i < 10; ++i)\n" |
| 802 | " return;\n" |
| 803 | "}"); |
| 804 | } |
| 805 | |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 806 | TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { |
| 807 | EXPECT_EQ("{}", format("{}")); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 808 | |
| 809 | // Negative test for enum. |
| 810 | verifyFormat("enum E {\n};"); |
| 811 | |
| 812 | // Note that when there's a missing ';', we still join... |
| 813 | verifyFormat("enum E {}"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 816 | //===----------------------------------------------------------------------===// |
| 817 | // Line break tests. |
| 818 | //===----------------------------------------------------------------------===// |
| 819 | |
| 820 | TEST_F(FormatTest, FormatsFunctionDefinition) { |
| 821 | verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," |
| 822 | " int h, int j, int f,\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 823 | " int c, int ddddddddddddd) {\n}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | TEST_F(FormatTest, FormatsAwesomeMethodCall) { |
| 827 | verifyFormat( |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 828 | "SomeLongMethodName(SomeReallyLongMethod(\n" |
| 829 | " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n" |
| 830 | " SecondLongCall(parameter));"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 833 | TEST_F(FormatTest, ConstructorInitializers) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 834 | verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 835 | verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", |
| 836 | getLLVMStyleWithColumns(45)); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 837 | verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {\n}", |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 838 | getLLVMStyleWithColumns(44)); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 839 | verifyFormat("Constructor()\n" |
| 840 | " : Inttializer(FitsOnTheLine) {\n}", |
| 841 | getLLVMStyleWithColumns(43)); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 842 | |
| 843 | verifyFormat( |
| 844 | "SomeClass::Constructor()\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 845 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 846 | |
| 847 | verifyFormat( |
| 848 | "SomeClass::Constructor()\n" |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 849 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 850 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 851 | verifyGoogleFormat( |
| 852 | "SomeClass::Constructor()\n" |
| 853 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
| 854 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 855 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 1661824 | 2013-01-16 17:00:50 +0000 | [diff] [blame] | 856 | verifyGoogleFormat( |
| 857 | "SomeClass::Constructor()\n" |
| 858 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" |
| 859 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 860 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 861 | |
| 862 | verifyFormat( |
| 863 | "SomeClass::Constructor()\n" |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 864 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 865 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 866 | |
| 867 | verifyFormat("Constructor()\n" |
| 868 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 869 | " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 870 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 871 | " aaaaaaaaaaaaaaaaaaaaaaa() {\n}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 872 | |
| 873 | // Here a line could be saved by splitting the second initializer onto two |
| 874 | // lines, but that is not desireable. |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 875 | verifyFormat( |
| 876 | "Constructor()\n" |
| 877 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 878 | " aaaaaaaaaaa(aaaaaaaaaaa),\n" |
| 879 | " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 880 | |
| 881 | verifyGoogleFormat("MyClass::MyClass(int var)\n" |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 882 | " : some_var_(var), // 4 space indent\n" |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 883 | " some_other_var_(var + 1) { // lined up\n" |
| 884 | "}"); |
Daniel Jasper | b312314 | 2013-01-12 07:36:22 +0000 | [diff] [blame] | 885 | |
| 886 | // This test takes VERY long when memoization is broken. |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 887 | std::string input = "Constructor()\n" |
| 888 | " : aaaa(a,\n"; |
| 889 | for (unsigned i = 0, e = 80; i != e; ++i) { |
| 890 | input += " a,\n"; |
| 891 | } |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 892 | input += " a) {\n}"; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 893 | verifyGoogleFormat(input); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 896 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
| 897 | verifyFormat( |
| 898 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
| 899 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
| 900 | " f();"); |
| 901 | } |
| 902 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 903 | TEST_F(FormatTest, BreaksDesireably) { |
| 904 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 905 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 906 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 907 | |
| 908 | verifyFormat( |
| 909 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 910 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 911 | |
| 912 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 913 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 914 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 915 | |
| 916 | verifyFormat( |
| 917 | "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 918 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 919 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 920 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 921 | |
| 922 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 923 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 924 | |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 925 | verifyFormat( |
| 926 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" |
| 927 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 928 | |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 929 | // This test case breaks on an incorrect memoization, i.e. an optimization not |
| 930 | // taking into account the StopAt value. |
| 931 | verifyFormat( |
| 932 | "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 933 | " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 934 | " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 935 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 936 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 937 | verifyFormat("{\n {\n {\n" |
| 938 | " Annotation.SpaceRequiredBefore =\n" |
| 939 | " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" |
| 940 | " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" |
| 941 | " }\n }\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 944 | TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { |
| 945 | verifyGoogleFormat( |
| 946 | "aaaaaaaa(aaaaaaaaaaaaa,\n" |
| 947 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 948 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 949 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 950 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
| 951 | verifyGoogleFormat( |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 952 | "aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 953 | " .aaaaaaaaaaaaaaaaaa();"); |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 954 | verifyGoogleFormat( |
| 955 | "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" |
| 956 | " ddddddddddddddddddddddddddddd),\n" |
| 957 | " test);"); |
| 958 | |
| 959 | verifyGoogleFormat( |
| 960 | "std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 961 | " aaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 962 | " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;"); |
| 963 | verifyGoogleFormat("a(\"a\"\n" |
| 964 | " \"a\",\n" |
| 965 | " a);"); |
| 966 | } |
| 967 | |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 968 | TEST_F(FormatTest, FormatsBuilderPattern) { |
| 969 | verifyFormat( |
| 970 | "return llvm::StringSwitch<Reference::Kind>(name)\n" |
| 971 | " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" |
| 972 | " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n" |
| 973 | " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n" |
| 974 | " .Default(ORDER_TEXT);\n"); |
| 975 | } |
| 976 | |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 977 | TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) { |
| 978 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 979 | " GUARDED_BY(aaaaaaaaaaaaa);"); |
Daniel Jasper | 60ca75d | 2013-01-17 13:31:52 +0000 | [diff] [blame] | 980 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" |
| 981 | " GUARDED_BY(aaaaaaaaaaaaa);"); |
| 982 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 983 | " GUARDED_BY(aaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 984 | } |
| 985 | |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 986 | TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { |
| 987 | verifyFormat( |
| 988 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 989 | " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 990 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 991 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 992 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 993 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 994 | verifyFormat( |
| 995 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 996 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 997 | } |
| 998 | |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 999 | TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) { |
| 1000 | verifyFormat( |
| 1001 | "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n" |
| 1002 | " SI->getAlignment(),\n" |
| 1003 | " SI->getPointerAddressSpaceee());\n"); |
| 1004 | verifyFormat( |
| 1005 | "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" |
| 1006 | " Line.Tokens.front().Tok.getLocation(),\n" |
| 1007 | " Line.Tokens.back().Tok.getLocation());"); |
| 1008 | } |
| 1009 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1010 | TEST_F(FormatTest, AlignsAfterAssignments) { |
| 1011 | verifyFormat( |
| 1012 | "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1013 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1014 | verifyFormat( |
| 1015 | "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1016 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1017 | verifyFormat( |
| 1018 | "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1019 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1020 | verifyFormat( |
| 1021 | "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1022 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1023 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1024 | "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 1025 | " aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 1026 | " aaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | TEST_F(FormatTest, AlignsAfterReturn) { |
| 1030 | verifyFormat( |
| 1031 | "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 1032 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1033 | verifyFormat( |
| 1034 | "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 1035 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1036 | } |
| 1037 | |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 1038 | TEST_F(FormatTest, BreaksConditionalExpressions) { |
| 1039 | verifyFormat( |
| 1040 | "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" |
| 1041 | " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" |
| 1042 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1043 | verifyFormat("aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa ?\n" |
| 1044 | " aaaaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 1f2b078 | 2013-01-16 16:23:19 +0000 | [diff] [blame] | 1045 | verifyFormat( |
| 1046 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa) :\n" |
| 1047 | " aaaaaaaaaaaaa);"); |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Nico Weber | 7d37b8b | 2013-01-12 01:28:06 +0000 | [diff] [blame] | 1050 | TEST_F(FormatTest, ConditionalExpressionsInBrackets) { |
| 1051 | verifyFormat("arr[foo ? bar : baz];"); |
| 1052 | verifyFormat("f()[foo ? bar : baz];"); |
| 1053 | verifyFormat("(a + b)[foo ? bar : baz];"); |
| 1054 | verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); |
| 1055 | } |
| 1056 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1057 | TEST_F(FormatTest, AlignsStringLiterals) { |
| 1058 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
| 1059 | " \"short literal\");"); |
| 1060 | verifyFormat( |
| 1061 | "looooooooooooooooooooooooongFunction(\n" |
| 1062 | " \"short literal\"\n" |
| 1063 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
| 1064 | } |
| 1065 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1066 | TEST_F(FormatTest, AlignsPipes) { |
| 1067 | verifyFormat( |
| 1068 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1069 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1070 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1071 | verifyFormat( |
| 1072 | "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" |
| 1073 | " << aaaaaaaaaaaaaaaaaaaa;"); |
| 1074 | verifyFormat( |
| 1075 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1076 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1077 | verifyFormat( |
| 1078 | "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 1079 | " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" |
| 1080 | " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); |
| 1081 | verifyFormat( |
| 1082 | "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1083 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 1084 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1085 | } |
| 1086 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1087 | TEST_F(FormatTest, UnderstandsEquals) { |
| 1088 | verifyFormat( |
| 1089 | "aaaaaaaaaaaaaaaaa =\n" |
| 1090 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1091 | verifyFormat( |
| 1092 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1093 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1094 | verifyFormat( |
| 1095 | "if (a) {\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1096 | " f();\n" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1097 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1098 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 1099 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1100 | |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1101 | verifyFormat( |
Daniel Jasper | 3fc0bb7 | 2013-01-09 10:40:23 +0000 | [diff] [blame] | 1102 | // FIXME: Does an expression like this ever make sense? If yes, fix. |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1103 | "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1104 | " 10000000) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 1107 | TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1108 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 1109 | " .looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 1110 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1111 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 1112 | " ->looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 1113 | |
| 1114 | verifyFormat( |
| 1115 | "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" |
| 1116 | " Parameter2);"); |
| 1117 | |
| 1118 | verifyFormat( |
| 1119 | "ShortObject->shortFunction(\n" |
| 1120 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" |
| 1121 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); |
| 1122 | |
| 1123 | verifyFormat("loooooooooooooongFunction(\n" |
| 1124 | " LoooooooooooooongObject->looooooooooooooooongFunction());"); |
| 1125 | |
| 1126 | verifyFormat( |
| 1127 | "function(LoooooooooooooooooooooooooooooooooooongObject\n" |
| 1128 | " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); |
| 1129 | |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 1130 | // Here, it is not necessary to wrap at "." or "->". |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1131 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1132 | " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 1133 | verifyFormat( |
| 1134 | "aaaaaaaaaaa->aaaaaaaaa(\n" |
| 1135 | " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1136 | " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 1139 | TEST_F(FormatTest, WrapsTemplateDeclarations) { |
| 1140 | verifyFormat("template <typename T>\n" |
| 1141 | "virtual void loooooooooooongFunction(int Param1, int Param2);"); |
| 1142 | verifyFormat( |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 1143 | "template <typename T>\n" |
| 1144 | "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" |
| 1145 | " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 1146 | verifyFormat( |
| 1147 | "template <typename T>\n" |
| 1148 | "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" |
| 1149 | " int Paaaaaaaaaaaaaaaaaaaaram2);"); |
Daniel Jasper | 5eda31e | 2013-01-02 18:30:06 +0000 | [diff] [blame] | 1150 | verifyFormat( |
| 1151 | "template <typename T>\n" |
| 1152 | "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" |
| 1153 | " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" |
| 1154 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | d64f738 | 2013-01-09 09:50:48 +0000 | [diff] [blame] | 1155 | verifyFormat("template <typename T>\n" |
| 1156 | "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1157 | " int aaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 3fc0bb7 | 2013-01-09 10:40:23 +0000 | [diff] [blame] | 1158 | verifyFormat( |
| 1159 | "template <typename T1, typename T2 = char, typename T3 = char,\n" |
| 1160 | " typename T4 = char>\n" |
| 1161 | "void f();"); |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1164 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
| 1165 | verifyFormat("A<int> a;"); |
| 1166 | verifyFormat("A<A<A<int> > > a;"); |
| 1167 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
| 1168 | verifyFormat("bool x = a < 1 || 2 > a;"); |
| 1169 | verifyFormat("bool x = 5 < f<int>();"); |
| 1170 | verifyFormat("bool x = f<int>() > 5;"); |
| 1171 | verifyFormat("bool x = 5 < a<int>::x;"); |
| 1172 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
| 1173 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
| 1174 | |
| 1175 | verifyGoogleFormat("A<A<int>> a;"); |
| 1176 | verifyGoogleFormat("A<A<A<int>>> a;"); |
| 1177 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
| 1178 | |
| 1179 | verifyFormat("test >> a >> b;"); |
| 1180 | verifyFormat("test << a >> b;"); |
| 1181 | |
| 1182 | verifyFormat("f<int>();"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1183 | verifyFormat("template <typename T> void f() {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1186 | TEST_F(FormatTest, UnderstandsUnaryOperators) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1187 | verifyFormat("int a = -2;"); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 1188 | verifyFormat("f(-1, -2, -3);"); |
| 1189 | verifyFormat("a[-1] = 5;"); |
| 1190 | verifyFormat("int a = 5 + -2;"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1191 | verifyFormat("if (i == -1) {\n}"); |
| 1192 | verifyFormat("if (i != -1) {\n}"); |
| 1193 | verifyFormat("if (i > -1) {\n}"); |
| 1194 | verifyFormat("if (i < -1) {\n}"); |
Daniel Jasper | d56a737 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 1195 | verifyFormat("++(a->f());"); |
| 1196 | verifyFormat("--(a->f());"); |
Daniel Jasper | 4abbb53 | 2013-01-14 12:18:19 +0000 | [diff] [blame] | 1197 | verifyFormat("(a->f())++;"); |
| 1198 | verifyFormat("a[42]++;"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1199 | verifyFormat("if (!(a->f())) {\n}"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1200 | |
| 1201 | verifyFormat("a-- > b;"); |
| 1202 | verifyFormat("b ? -a : c;"); |
| 1203 | verifyFormat("n * sizeof char16;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 1204 | verifyFormat("n * alignof char16;"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1205 | verifyFormat("sizeof(char);"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 1206 | verifyFormat("alignof(char);"); |
Daniel Jasper | 1f0754b | 2013-01-02 15:26:16 +0000 | [diff] [blame] | 1207 | |
| 1208 | verifyFormat("return -1;"); |
| 1209 | verifyFormat("switch (a) {\n" |
| 1210 | "case -1:\n" |
| 1211 | " break;\n" |
| 1212 | "}"); |
Nico Weber | cc191d1 | 2013-01-12 05:41:23 +0000 | [diff] [blame] | 1213 | |
| 1214 | verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };"); |
| 1215 | verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };"); |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1216 | |
| 1217 | verifyFormat("int a = /* confusing comment */ -1;"); |
| 1218 | // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. |
| 1219 | verifyFormat("int a = i /* confusing comment */++;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | TEST_F(FormatTest, UndestandsOverloadedOperators) { |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 1223 | verifyFormat("bool operator<();"); |
| 1224 | verifyFormat("bool operator>();"); |
| 1225 | verifyFormat("bool operator=();"); |
| 1226 | verifyFormat("bool operator==();"); |
| 1227 | verifyFormat("bool operator!=();"); |
| 1228 | verifyFormat("int operator+();"); |
| 1229 | verifyFormat("int operator++();"); |
| 1230 | verifyFormat("bool operator();"); |
| 1231 | verifyFormat("bool operator()();"); |
| 1232 | verifyFormat("bool operator[]();"); |
| 1233 | verifyFormat("operator bool();"); |
| 1234 | verifyFormat("operator SomeType<int>();"); |
| 1235 | verifyFormat("void *operator new(std::size_t size);"); |
| 1236 | verifyFormat("void *operator new[](std::size_t size);"); |
| 1237 | verifyFormat("void operator delete(void *ptr);"); |
| 1238 | verifyFormat("void operator delete[](void *ptr);"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Daniel Jasper | 088dab5 | 2013-01-11 16:09:04 +0000 | [diff] [blame] | 1241 | TEST_F(FormatTest, UnderstandsNewAndDelete) { |
| 1242 | verifyFormat("A *a = new A;"); |
| 1243 | verifyFormat("A *a = new (placement) A;"); |
| 1244 | verifyFormat("delete a;"); |
| 1245 | verifyFormat("delete (A *)a;"); |
| 1246 | } |
| 1247 | |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 1248 | TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1249 | verifyFormat("int *f(int *a) {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1250 | verifyFormat("f(a, *a);"); |
| 1251 | verifyFormat("f(*a);"); |
| 1252 | verifyFormat("int a = b * 10;"); |
| 1253 | verifyFormat("int a = 10 * b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 1254 | verifyFormat("int a = b * c;"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 1255 | verifyFormat("int a += b * c;"); |
| 1256 | verifyFormat("int a -= b * c;"); |
| 1257 | verifyFormat("int a *= b * c;"); |
| 1258 | verifyFormat("int a /= b * c;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1259 | verifyFormat("int a = *b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 1260 | verifyFormat("int a = *b * c;"); |
| 1261 | verifyFormat("int a = b * *c;"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1262 | verifyFormat("int main(int argc, char **argv) {}"); |
Nico Weber | 00d5a04 | 2012-12-23 01:07:46 +0000 | [diff] [blame] | 1263 | verifyFormat("return 10 * b;"); |
| 1264 | verifyFormat("return *b * *c;"); |
| 1265 | verifyFormat("return a & ~b;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 1266 | verifyFormat("f(b ? *c : *d);"); |
| 1267 | verifyFormat("int a = b ? *c : *d;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 1268 | verifyFormat("*b = a;"); |
| 1269 | verifyFormat("a * ~b;"); |
| 1270 | verifyFormat("a * !b;"); |
| 1271 | verifyFormat("a * +b;"); |
| 1272 | verifyFormat("a * -b;"); |
| 1273 | verifyFormat("a * ++b;"); |
| 1274 | verifyFormat("a * --b;"); |
Nico Weber | 2355ceb | 2013-01-12 05:47:16 +0000 | [diff] [blame] | 1275 | verifyFormat("a[4] * b;"); |
| 1276 | verifyFormat("f() * b;"); |
Nico Weber | 5e9f91c | 2013-01-12 05:50:48 +0000 | [diff] [blame] | 1277 | verifyFormat("a * [self dostuff];"); |
| 1278 | verifyFormat("a * (a + b);"); |
| 1279 | verifyFormat("(a *)(a + b);"); |
Daniel Jasper | 5cf7cf3 | 2013-01-10 11:14:08 +0000 | [diff] [blame] | 1280 | verifyFormat("int *pa = (int *)&a;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 1281 | |
Daniel Jasper | 9bb0d28 | 2013-01-04 20:46:38 +0000 | [diff] [blame] | 1282 | verifyFormat("InvalidRegions[*R] = 0;"); |
| 1283 | |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 1284 | verifyFormat("A<int *> a;"); |
| 1285 | verifyFormat("A<int **> a;"); |
| 1286 | verifyFormat("A<int *, int *> a;"); |
| 1287 | verifyFormat("A<int **, int **> a;"); |
| 1288 | |
Daniel Jasper | 2db356d | 2013-01-08 20:03:18 +0000 | [diff] [blame] | 1289 | verifyFormat( |
| 1290 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1291 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1292 | |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1293 | verifyGoogleFormat("int main(int argc, char** argv) {}"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 1294 | verifyGoogleFormat("A<int*> a;"); |
| 1295 | verifyGoogleFormat("A<int**> a;"); |
| 1296 | verifyGoogleFormat("A<int*, int*> a;"); |
| 1297 | verifyGoogleFormat("A<int**, int**> a;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 1298 | verifyGoogleFormat("f(b ? *c : *d);"); |
| 1299 | verifyGoogleFormat("int a = b ? *c : *d;"); |
Daniel Jasper | 48bd7b7 | 2013-01-16 16:04:06 +0000 | [diff] [blame] | 1300 | verifyGoogleFormat("Type* t = **x;"); |
| 1301 | verifyGoogleFormat("Type* t = *++*x;"); |
| 1302 | verifyGoogleFormat("*++*x;"); |
| 1303 | verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); |
| 1304 | verifyGoogleFormat("Type* t = x++ * y;"); |
Manuel Klimek | 1b6f4bd | 2013-01-14 10:58:01 +0000 | [diff] [blame] | 1305 | |
| 1306 | verifyFormat("a = *(x + y);"); |
| 1307 | verifyFormat("a = &(x + y);"); |
| 1308 | verifyFormat("*(x + y).call();"); |
| 1309 | verifyFormat("&(x + y)->call();"); |
| 1310 | verifyFormat("&(*I).first"); |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1311 | |
| 1312 | verifyFormat("f(b * /* confusing comment */ ++c);"); |
| 1313 | verifyFormat( |
| 1314 | "int *MyValues = {\n" |
| 1315 | " *A, // Operator detection might be confused by the '{'\n" |
| 1316 | " *BB // Operator detection might be confused by previous comment\n" |
| 1317 | "};"); |
Nico Weber | 5096a44 | 2013-01-17 17:17:19 +0000 | [diff] [blame] | 1318 | |
| 1319 | verifyFormat("if (int *a = &b)"); |
| 1320 | verifyFormat("if (int &a = *b)"); |
| 1321 | verifyFormat("if (a & b[i])"); |
| 1322 | verifyFormat("if (a::b::c::d & b[i])"); |
| 1323 | verifyFormat("if (*b[i])"); |
| 1324 | verifyFormat("if (int *a = (&b))"); |
| 1325 | verifyFormat("while (int *a = &b)"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Daniel Jasper | 4981bd0 | 2013-01-13 08:01:36 +0000 | [diff] [blame] | 1328 | TEST_F(FormatTest, FormatsCasts) { |
| 1329 | verifyFormat("Type *A = static_cast<Type *>(P);"); |
| 1330 | verifyFormat("Type *A = (Type *)P;"); |
| 1331 | verifyFormat("Type *A = (vector<Type *, int *>)P;"); |
| 1332 | verifyFormat("int a = (int)(2.0f);"); |
| 1333 | |
| 1334 | // FIXME: These also need to be identified. |
| 1335 | verifyFormat("int a = (int) 2.0f;"); |
| 1336 | verifyFormat("int a = (int) * b;"); |
| 1337 | |
| 1338 | // These are not casts. |
| 1339 | verifyFormat("void f(int *) {}"); |
| 1340 | verifyFormat("void f(int *);"); |
| 1341 | verifyFormat("void f(int *) = 0;"); |
| 1342 | verifyFormat("void f(SmallVector<int>) {}"); |
| 1343 | verifyFormat("void f(SmallVector<int>);"); |
| 1344 | verifyFormat("void f(SmallVector<int>) = 0;"); |
| 1345 | } |
| 1346 | |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1347 | TEST_F(FormatTest, FormatsFunctionTypes) { |
| 1348 | // FIXME: Determine the cases that need a space after the return type and fix. |
| 1349 | verifyFormat("A<bool()> a;"); |
| 1350 | verifyFormat("A<SomeType()> a;"); |
| 1351 | verifyFormat("A<void(*)(int, std::string)> a;"); |
| 1352 | |
| 1353 | verifyFormat("int(*func)(void *);"); |
| 1354 | } |
| 1355 | |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 1356 | TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1357 | verifyFormat("int *someFunction(int LoooooooooooooooongParam1,\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1358 | " int LoooooooooooooooongParam2) {\n}"); |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 1359 | verifyFormat( |
| 1360 | "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" |
| 1361 | " SourceLocation L, IdentifierIn *II,\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1362 | " Type *T) {\n}"); |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 1365 | TEST_F(FormatTest, LineStartsWithSpecialCharacter) { |
| 1366 | verifyFormat("(a)->b();"); |
| 1367 | verifyFormat("--a;"); |
| 1368 | } |
| 1369 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 1370 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
Daniel Jasper | 2b9c10b | 2013-01-14 15:52:06 +0000 | [diff] [blame] | 1371 | verifyFormat("#include <string>\n" |
| 1372 | "#include <a/b/c.h>\n" |
| 1373 | "#include \"a/b/string\"\n" |
| 1374 | "#include \"string.h\"\n" |
| 1375 | "#include \"string.h\"\n" |
Manuel Klimek | 407a31a | 2013-01-15 15:50:27 +0000 | [diff] [blame] | 1376 | "#include <a-a>\n" |
| 1377 | "#include < path with space >\n"); |
Nico Weber | b23ae0c | 2012-12-21 18:21:56 +0000 | [diff] [blame] | 1378 | |
Daniel Jasper | 8134e1e | 2013-01-13 08:12:18 +0000 | [diff] [blame] | 1379 | verifyFormat("#import <string>"); |
| 1380 | verifyFormat("#import <a/b/c.h>"); |
| 1381 | verifyFormat("#import \"a/b/string\""); |
| 1382 | verifyFormat("#import \"string.h\""); |
| 1383 | verifyFormat("#import \"string.h\""); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1386 | //===----------------------------------------------------------------------===// |
| 1387 | // Error recovery tests. |
| 1388 | //===----------------------------------------------------------------------===// |
| 1389 | |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 1390 | TEST_F(FormatTest, IncorrectCodeTrailingStuff) { |
Alexander Kornienko | 51c23fa | 2013-01-16 11:45:16 +0000 | [diff] [blame] | 1391 | verifyFormat("void f() { return; }\n42"); |
| 1392 | verifyFormat("void f() {\n" |
| 1393 | " if (0)\n" |
| 1394 | " return;\n" |
| 1395 | "}\n" |
| 1396 | "42"); |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 1397 | verifyFormat("void f() { return }\n42"); |
| 1398 | verifyFormat("void f() {\n" |
| 1399 | " if (0)\n" |
| 1400 | " return\n" |
| 1401 | "}\n" |
| 1402 | "42"); |
| 1403 | } |
| 1404 | |
| 1405 | TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { |
| 1406 | EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); |
| 1407 | EXPECT_EQ("void f() {\n" |
| 1408 | " if (a)\n" |
| 1409 | " return\n" |
| 1410 | "}", format("void f ( ) { if ( a ) return }")); |
| 1411 | EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }")); |
| 1412 | EXPECT_EQ("namespace N {\n" |
| 1413 | "void f() {}\n" |
| 1414 | "void g()\n" |
| 1415 | "}", format("namespace N { void f( ) { } void g( ) }")); |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 1418 | TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { |
| 1419 | verifyFormat("int aaaaaaaa =\n" |
| 1420 | " // Overly long comment\n" |
| 1421 | " b;", getLLVMStyleWithColumns(20)); |
| 1422 | verifyFormat("function(\n" |
| 1423 | " ShortArgument,\n" |
| 1424 | " LoooooooooooongArgument);\n", getLLVMStyleWithColumns(20)); |
| 1425 | } |
| 1426 | |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1427 | TEST_F(FormatTest, IncorrectAccessSpecifier) { |
| 1428 | verifyFormat("public:"); |
| 1429 | verifyFormat("class A {\n" |
| 1430 | "public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1431 | " void f() {}\n" |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1432 | "};"); |
| 1433 | verifyFormat("public\n" |
| 1434 | "int qwerty;"); |
| 1435 | verifyFormat("public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1436 | "B {}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1437 | verifyFormat("public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1438 | "{}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1439 | verifyFormat("public\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1440 | "B { int x; }"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 1441 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1442 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1443 | TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { |
| 1444 | verifyFormat("{"); |
| 1445 | } |
| 1446 | |
| 1447 | TEST_F(FormatTest, IncorrectCodeDoNoWhile) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1448 | verifyFormat("do {\n}"); |
| 1449 | verifyFormat("do {\n}\n" |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1450 | "f();"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1451 | verifyFormat("do {\n}\n" |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1452 | "wheeee(fun);"); |
| 1453 | verifyFormat("do {\n" |
| 1454 | " f();\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 1455 | "}"); |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 1458 | TEST_F(FormatTest, IncorrectCodeMissingParens) { |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 1459 | verifyFormat("if {\n foo;\n foo();\n}"); |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 1460 | verifyFormat("switch {\n foo;\n foo();\n}"); |
| 1461 | verifyFormat("for {\n foo;\n foo();\n}"); |
| 1462 | verifyFormat("while {\n foo;\n foo();\n}"); |
| 1463 | verifyFormat("do {\n foo;\n foo();\n} while;"); |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1466 | TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { |
| 1467 | verifyFormat("namespace {\n" |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1468 | "class Foo { Foo ( }; } // comment"); |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1471 | TEST_F(FormatTest, IncorrectCodeErrorDetection) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1472 | EXPECT_EQ("{\n{}\n", format("{\n{\n}\n")); |
| 1473 | EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); |
| 1474 | EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); |
| 1475 | EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n")); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1476 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1477 | EXPECT_EQ("{\n" |
| 1478 | " {\n" |
| 1479 | " breakme(\n" |
| 1480 | " qwe);\n" |
| 1481 | "}\n", format("{\n" |
| 1482 | " {\n" |
| 1483 | " breakme(qwe);\n" |
Alexander Kornienko | a4ae9f3 | 2013-01-14 11:34:14 +0000 | [diff] [blame] | 1484 | "}\n", getLLVMStyleWithColumns(10))); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 1487 | TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { |
| 1488 | verifyFormat( |
| 1489 | "int x = {\n" |
| 1490 | " avariable,\n" |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 1491 | " b(alongervariable)\n" |
| 1492 | "};", getLLVMStyleWithColumns(25)); |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 1495 | TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { |
| 1496 | verifyFormat("return (a)(b) { 1, 2, 3 };"); |
| 1497 | } |
| 1498 | |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 1499 | TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) { |
| 1500 | verifyFormat( |
| 1501 | "Aaa({\n" |
| 1502 | " int i;\n" |
| 1503 | "}, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" |
| 1504 | " ccccccccccccccccc));"); |
| 1505 | } |
| 1506 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1507 | TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { |
| 1508 | verifyFormat("void f() { return 42; }"); |
| 1509 | verifyFormat("void f() {\n" |
| 1510 | " // Comment\n" |
| 1511 | "}"); |
| 1512 | verifyFormat("{\n" |
| 1513 | "#error {\n" |
| 1514 | " int a;\n" |
| 1515 | "}"); |
| 1516 | verifyFormat("{\n" |
| 1517 | " int a;\n" |
| 1518 | "#error {\n" |
| 1519 | "}"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1520 | |
| 1521 | verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); |
| 1522 | verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); |
| 1523 | |
| 1524 | verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); |
| 1525 | verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1528 | TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { |
| 1529 | // Elaborate type variable declarations. |
Manuel Klimek | 606e07e | 2013-01-11 18:13:04 +0000 | [diff] [blame] | 1530 | verifyFormat("struct foo a = { bar };\nint n;"); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1531 | verifyFormat("class foo a = { bar };\nint n;"); |
| 1532 | verifyFormat("union foo a = { bar };\nint n;"); |
| 1533 | |
| 1534 | // Elaborate types inside function definitions. |
| 1535 | verifyFormat("struct foo f() {}\nint n;"); |
| 1536 | verifyFormat("class foo f() {}\nint n;"); |
| 1537 | verifyFormat("union foo f() {}\nint n;"); |
| 1538 | |
| 1539 | // Templates. |
| 1540 | verifyFormat("template <class X> void f() {}\nint n;"); |
| 1541 | verifyFormat("template <struct X> void f() {}\nint n;"); |
| 1542 | verifyFormat("template <union X> void f() {}\nint n;"); |
| 1543 | |
| 1544 | // Actual definitions... |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1545 | verifyFormat("struct {\n} n;"); |
| 1546 | verifyFormat( |
| 1547 | "template <template <class T, class Y>, class Z> class X {\n} n;"); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1548 | verifyFormat("union Z {\n int n;\n} x;"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1549 | verifyFormat("class MACRO Z {\n} n;"); |
| 1550 | verifyFormat("class MACRO(X) Z {\n} n;"); |
| 1551 | verifyFormat("class __attribute__(X) Z {\n} n;"); |
| 1552 | verifyFormat("class __declspec(X) Z {\n} n;"); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1553 | |
Manuel Klimek | 7f5b025 | 2013-01-21 10:17:14 +0000 | [diff] [blame] | 1554 | // Redefinition from nested context: |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1555 | verifyFormat("class A::B::C {\n} n;"); |
Manuel Klimek | 7f5b025 | 2013-01-21 10:17:14 +0000 | [diff] [blame] | 1556 | |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 1557 | // Template definitions. |
| 1558 | // FIXME: This is still incorrectly handled at the formatter side. |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1559 | verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};"); |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 1560 | |
| 1561 | // FIXME: |
| 1562 | // This now gets parsed incorrectly as class definition. |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1563 | // verifyFormat("class A<int> f() {\n}\nint n;"); |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 1564 | |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 1565 | // Elaborate types where incorrectly parsing the structural element would |
| 1566 | // break the indent. |
| 1567 | verifyFormat("if (true)\n" |
| 1568 | " class X x;\n" |
| 1569 | "else\n" |
| 1570 | " f();\n"); |
Manuel Klimek | 606e07e | 2013-01-11 18:13:04 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Manuel Klimek | 407a31a | 2013-01-15 15:50:27 +0000 | [diff] [blame] | 1573 | TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { |
| 1574 | verifyFormat("#error Leave all white!!!!! space* alone!\n"); |
| 1575 | verifyFormat("#warning Leave all white!!!!! space* alone!\n"); |
| 1576 | EXPECT_EQ("#error 1", format(" # error 1")); |
| 1577 | EXPECT_EQ("#warning 1", format(" # warning 1")); |
| 1578 | } |
| 1579 | |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1580 | TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { |
| 1581 | FormatStyle AllowsMergedIf = getGoogleStyle(); |
| 1582 | AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; |
| 1583 | verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); |
| 1584 | verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 1585 | verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); |
| 1586 | EXPECT_EQ("if (true) return 42;", |
| 1587 | format("if (true)\nreturn 42;", AllowsMergedIf)); |
| 1588 | FormatStyle ShortMergedIf = AllowsMergedIf; |
| 1589 | ShortMergedIf.ColumnLimit = 25; |
| 1590 | verifyFormat("#define A \\\n" |
| 1591 | " if (true) return 42;", ShortMergedIf); |
| 1592 | verifyFormat("#define A \\\n" |
| 1593 | " f(); \\\n" |
| 1594 | " if (true)\n" |
| 1595 | "#define B", ShortMergedIf); |
| 1596 | verifyFormat("#define A \\\n" |
| 1597 | " f(); \\\n" |
| 1598 | " if (true)\n" |
| 1599 | "g();", ShortMergedIf); |
Manuel Klimek | 0fbe008 | 2013-01-21 14:16:56 +0000 | [diff] [blame] | 1600 | verifyFormat("{\n" |
| 1601 | "#ifdef A\n" |
| 1602 | " // Comment\n" |
| 1603 | " if (true) continue;\n" |
| 1604 | "#endif\n" |
| 1605 | " // Comment\n" |
| 1606 | " if (true) continue;", ShortMergedIf); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1609 | //===----------------------------------------------------------------------===// |
| 1610 | // Objective-C tests. |
| 1611 | //===----------------------------------------------------------------------===// |
| 1612 | |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1613 | TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { |
| 1614 | verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); |
| 1615 | EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", |
| 1616 | format("-(NSUInteger)indexOfObject:(id)anObject;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1617 | EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1618 | EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); |
| 1619 | EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", |
| 1620 | format("-(NSInteger)Method3:(id)anObject;")); |
| 1621 | EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", |
| 1622 | format("-(NSInteger)Method4:(id)anObject;")); |
| 1623 | EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", |
| 1624 | format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); |
| 1625 | EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", |
| 1626 | format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1627 | EXPECT_EQ( |
| 1628 | "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", |
| 1629 | format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); |
Fariborz Jahanian | 9b3f02c | 2012-12-21 22:51:18 +0000 | [diff] [blame] | 1630 | |
| 1631 | // Very long objectiveC method declaration. |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1632 | EXPECT_EQ( |
| 1633 | "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n " |
| 1634 | "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n " |
| 1635 | "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n " |
| 1636 | "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n " |
| 1637 | "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n " |
| 1638 | "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;", |
| 1639 | format( |
| 1640 | "- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range " |
| 1641 | "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 " |
| 1642 | "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 " |
| 1643 | "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 " |
| 1644 | "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 " |
| 1645 | "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;")); |
Nico Weber | e0fd429 | 2013-01-10 20:18:33 +0000 | [diff] [blame] | 1646 | |
| 1647 | verifyFormat("- (int)sum:(vector<int>)numbers;"); |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 1648 | verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); |
Nico Weber | e0fd429 | 2013-01-10 20:18:33 +0000 | [diff] [blame] | 1649 | // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC |
| 1650 | // protocol lists (but not for template classes): |
| 1651 | //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 1652 | |
| 1653 | verifyFormat("- (int(*)())foo:(int(*)())f;"); |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 1654 | verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;"); |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 1655 | |
| 1656 | // If there's no return type (very rare in practice!), LLVM and Google style |
| 1657 | // agree. |
| 1658 | verifyFormat("- foo:(int)f;"); |
| 1659 | verifyGoogleFormat("- foo:(int)foo;"); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 1662 | TEST_F(FormatTest, FormatObjCBlocks) { |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 1663 | verifyFormat("int (^Block)(int, int);"); |
| 1664 | verifyFormat("int (^Block1)(int, int) = ^(int i, int j)"); |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1667 | TEST_F(FormatTest, FormatObjCInterface) { |
| 1668 | // FIXME: Handle comments like in "@interface /* wait for it */ Foo", PR14875 |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1669 | verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1670 | "@public\n" |
| 1671 | " int field1;\n" |
| 1672 | "@protected\n" |
| 1673 | " int field2;\n" |
| 1674 | "@private\n" |
| 1675 | " int field3;\n" |
| 1676 | "@package\n" |
| 1677 | " int field4;\n" |
| 1678 | "}\n" |
| 1679 | "+ (id)init;\n" |
| 1680 | "@end"); |
| 1681 | |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1682 | verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" |
| 1683 | " @public\n" |
| 1684 | " int field1;\n" |
| 1685 | " @protected\n" |
| 1686 | " int field2;\n" |
| 1687 | " @private\n" |
| 1688 | " int field3;\n" |
| 1689 | " @package\n" |
| 1690 | " int field4;\n" |
| 1691 | "}\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 1692 | "+ (id)init;\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1693 | "@end"); |
| 1694 | |
| 1695 | verifyFormat("@interface Foo\n" |
| 1696 | "+ (id)init;\n" |
| 1697 | "// Look, a comment!\n" |
| 1698 | "- (int)answerWith:(int)i;\n" |
| 1699 | "@end"); |
| 1700 | |
| 1701 | verifyFormat("@interface Foo\n" |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 1702 | "@end\n" |
| 1703 | "@interface Bar\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1704 | "@end"); |
| 1705 | |
| 1706 | verifyFormat("@interface Foo : Bar\n" |
| 1707 | "+ (id)init;\n" |
| 1708 | "@end"); |
| 1709 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1710 | verifyFormat("@interface Foo : Bar <Baz, Quux>\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1711 | "+ (id)init;\n" |
| 1712 | "@end"); |
| 1713 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1714 | verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 1715 | "+ (id)init;\n" |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1716 | "@end"); |
| 1717 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 1718 | verifyFormat("@interface Foo (HackStuff)\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1719 | "+ (id)init;\n" |
| 1720 | "@end"); |
| 1721 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 1722 | verifyFormat("@interface Foo ()\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1723 | "+ (id)init;\n" |
| 1724 | "@end"); |
| 1725 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1726 | verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1727 | "+ (id)init;\n" |
| 1728 | "@end"); |
| 1729 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1730 | verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 1731 | "+ (id)init;\n" |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1732 | "@end"); |
| 1733 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1734 | verifyFormat("@interface Foo {\n" |
| 1735 | " int _i;\n" |
| 1736 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1737 | "+ (id)init;\n" |
| 1738 | "@end"); |
| 1739 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1740 | verifyFormat("@interface Foo : Bar {\n" |
| 1741 | " int _i;\n" |
| 1742 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1743 | "+ (id)init;\n" |
| 1744 | "@end"); |
| 1745 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1746 | verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" |
| 1747 | " int _i;\n" |
| 1748 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1749 | "+ (id)init;\n" |
| 1750 | "@end"); |
| 1751 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1752 | verifyFormat("@interface Foo (HackStuff) {\n" |
| 1753 | " int _i;\n" |
| 1754 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1755 | "+ (id)init;\n" |
| 1756 | "@end"); |
| 1757 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1758 | verifyFormat("@interface Foo () {\n" |
| 1759 | " int _i;\n" |
| 1760 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1761 | "+ (id)init;\n" |
| 1762 | "@end"); |
| 1763 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1764 | verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" |
| 1765 | " int _i;\n" |
| 1766 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 1767 | "+ (id)init;\n" |
| 1768 | "@end"); |
| 1769 | } |
| 1770 | |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1771 | TEST_F(FormatTest, FormatObjCImplementation) { |
| 1772 | verifyFormat("@implementation Foo : NSObject {\n" |
| 1773 | "@public\n" |
| 1774 | " int field1;\n" |
| 1775 | "@protected\n" |
| 1776 | " int field2;\n" |
| 1777 | "@private\n" |
| 1778 | " int field3;\n" |
| 1779 | "@package\n" |
| 1780 | " int field4;\n" |
| 1781 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1782 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1783 | "@end"); |
| 1784 | |
| 1785 | verifyGoogleFormat("@implementation Foo : NSObject {\n" |
| 1786 | " @public\n" |
| 1787 | " int field1;\n" |
| 1788 | " @protected\n" |
| 1789 | " int field2;\n" |
| 1790 | " @private\n" |
| 1791 | " int field3;\n" |
| 1792 | " @package\n" |
| 1793 | " int field4;\n" |
| 1794 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1795 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1796 | "@end"); |
| 1797 | |
| 1798 | verifyFormat("@implementation Foo\n" |
| 1799 | "+ (id)init {\n" |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 1800 | " if (true)\n" |
| 1801 | " return nil;\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1802 | "}\n" |
| 1803 | "// Look, a comment!\n" |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1804 | "- (int)answerWith:(int)i {\n" |
| 1805 | " return i;\n" |
| 1806 | "}\n" |
Nico Weber | 67015ed | 2013-01-11 21:14:08 +0000 | [diff] [blame] | 1807 | "+ (int)answerWith:(int)i {\n" |
| 1808 | " return i;\n" |
| 1809 | "}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1810 | "@end"); |
| 1811 | |
| 1812 | verifyFormat("@implementation Foo\n" |
| 1813 | "@end\n" |
| 1814 | "@implementation Bar\n" |
| 1815 | "@end"); |
| 1816 | |
| 1817 | verifyFormat("@implementation Foo : Bar\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1818 | "+ (id)init {\n}\n" |
| 1819 | "- (void)foo {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1820 | "@end"); |
| 1821 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1822 | verifyFormat("@implementation Foo {\n" |
| 1823 | " int _i;\n" |
| 1824 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1825 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1826 | "@end"); |
| 1827 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1828 | verifyFormat("@implementation Foo : Bar {\n" |
| 1829 | " int _i;\n" |
| 1830 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1831 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1832 | "@end"); |
| 1833 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 1834 | verifyFormat("@implementation Foo (HackStuff)\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1835 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 1836 | "@end"); |
| 1837 | } |
| 1838 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1839 | TEST_F(FormatTest, FormatObjCProtocol) { |
| 1840 | verifyFormat("@protocol Foo\n" |
| 1841 | "@property(weak) id delegate;\n" |
| 1842 | "- (NSUInteger)numberOfThings;\n" |
| 1843 | "@end"); |
| 1844 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1845 | verifyFormat("@protocol MyProtocol <NSObject>\n" |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1846 | "- (NSUInteger)numberOfThings;\n" |
| 1847 | "@end"); |
| 1848 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1849 | verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 1850 | "- (NSUInteger)numberOfThings;\n" |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 1851 | "@end"); |
| 1852 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1853 | verifyFormat("@protocol Foo;\n" |
| 1854 | "@protocol Bar;\n"); |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 1855 | |
| 1856 | verifyFormat("@protocol Foo\n" |
| 1857 | "@end\n" |
| 1858 | "@protocol Bar\n" |
| 1859 | "@end"); |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 1860 | |
| 1861 | verifyFormat("@protocol myProtocol\n" |
| 1862 | "- (void)mandatoryWithInt:(int)i;\n" |
| 1863 | "@optional\n" |
| 1864 | "- (void)optional;\n" |
| 1865 | "@required\n" |
| 1866 | "- (void)required;\n" |
Nico Weber | 880e538 | 2013-01-10 00:42:07 +0000 | [diff] [blame] | 1867 | "@optional\n" |
| 1868 | "@property(assign) int madProp;\n" |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 1869 | "@end\n"); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 1872 | TEST_F(FormatTest, FormatObjCMethodExpr) { |
| 1873 | verifyFormat("[foo bar:baz];"); |
| 1874 | verifyFormat("return [foo bar:baz];"); |
| 1875 | verifyFormat("f([foo bar:baz]);"); |
| 1876 | verifyFormat("f(2, [foo bar:baz]);"); |
| 1877 | verifyFormat("f(2, a ? b : c);"); |
| 1878 | verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); |
| 1879 | |
| 1880 | verifyFormat("[foo bar:baz], [foo bar:baz];"); |
| 1881 | verifyFormat("[foo bar:baz] = [foo bar:baz];"); |
| 1882 | verifyFormat("[foo bar:baz] *= [foo bar:baz];"); |
| 1883 | verifyFormat("[foo bar:baz] /= [foo bar:baz];"); |
| 1884 | verifyFormat("[foo bar:baz] %= [foo bar:baz];"); |
| 1885 | verifyFormat("[foo bar:baz] += [foo bar:baz];"); |
| 1886 | verifyFormat("[foo bar:baz] -= [foo bar:baz];"); |
| 1887 | verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); |
| 1888 | verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); |
| 1889 | verifyFormat("[foo bar:baz] &= [foo bar:baz];"); |
| 1890 | verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); |
| 1891 | verifyFormat("[foo bar:baz] |= [foo bar:baz];"); |
| 1892 | verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); |
| 1893 | verifyFormat("[foo bar:baz] || [foo bar:baz];"); |
| 1894 | verifyFormat("[foo bar:baz] && [foo bar:baz];"); |
| 1895 | verifyFormat("[foo bar:baz] | [foo bar:baz];"); |
| 1896 | verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); |
| 1897 | verifyFormat("[foo bar:baz] & [foo bar:baz];"); |
| 1898 | verifyFormat("[foo bar:baz] == [foo bar:baz];"); |
| 1899 | verifyFormat("[foo bar:baz] != [foo bar:baz];"); |
| 1900 | verifyFormat("[foo bar:baz] >= [foo bar:baz];"); |
| 1901 | verifyFormat("[foo bar:baz] <= [foo bar:baz];"); |
| 1902 | verifyFormat("[foo bar:baz] > [foo bar:baz];"); |
| 1903 | verifyFormat("[foo bar:baz] < [foo bar:baz];"); |
| 1904 | verifyFormat("[foo bar:baz] >> [foo bar:baz];"); |
| 1905 | verifyFormat("[foo bar:baz] << [foo bar:baz];"); |
| 1906 | verifyFormat("[foo bar:baz] - [foo bar:baz];"); |
| 1907 | verifyFormat("[foo bar:baz] + [foo bar:baz];"); |
| 1908 | verifyFormat("[foo bar:baz] * [foo bar:baz];"); |
| 1909 | verifyFormat("[foo bar:baz] / [foo bar:baz];"); |
| 1910 | verifyFormat("[foo bar:baz] % [foo bar:baz];"); |
| 1911 | // Whew! |
| 1912 | |
| 1913 | verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); |
| 1914 | verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); |
| 1915 | verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); |
| 1916 | verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); |
| 1917 | verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); |
Nico Weber | 6292dd4 | 2013-01-12 23:41:33 +0000 | [diff] [blame] | 1918 | verifyFormat("[button setAction:@selector(zoomOut:)];"); |
Nico Weber | cd45833 | 2013-01-12 23:48:49 +0000 | [diff] [blame] | 1919 | verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 1920 | |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 1921 | verifyFormat("arr[[self indexForFoo:a]];"); |
| 1922 | verifyFormat("throw [self errorFor:a];"); |
| 1923 | verifyFormat("@throw [self errorFor:a];"); |
| 1924 | |
Nico Weber | e8ccc81 | 2013-01-12 22:48:47 +0000 | [diff] [blame] | 1925 | // This tests that the formatter doesn't break after "backing" but before ":", |
| 1926 | // which would be at 80 columns. |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 1927 | verifyFormat( |
| 1928 | "void f() {\n" |
Nico Weber | e8ccc81 | 2013-01-12 22:48:47 +0000 | [diff] [blame] | 1929 | " if ((self = [super initWithContentRect:contentRect styleMask:styleMask\n" |
| 1930 | " backing:NSBackingStoreBuffered defer:YES]))"); |
| 1931 | |
Nico Weber | e8ccc81 | 2013-01-12 22:48:47 +0000 | [diff] [blame] | 1932 | verifyFormat("[foo checkThatBreakingAfterColonWorksOk:\n" |
| 1933 | " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 1934 | |
| 1935 | } |
| 1936 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 1937 | TEST_F(FormatTest, ObjCAt) { |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1938 | verifyFormat("@autoreleasepool"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1939 | verifyFormat("@catch"); |
| 1940 | verifyFormat("@class"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1941 | verifyFormat("@compatibility_alias"); |
| 1942 | verifyFormat("@defs"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 1943 | verifyFormat("@dynamic"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1944 | verifyFormat("@encode"); |
| 1945 | verifyFormat("@end"); |
| 1946 | verifyFormat("@finally"); |
| 1947 | verifyFormat("@implementation"); |
| 1948 | verifyFormat("@import"); |
| 1949 | verifyFormat("@interface"); |
| 1950 | verifyFormat("@optional"); |
| 1951 | verifyFormat("@package"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1952 | verifyFormat("@private"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1953 | verifyFormat("@property"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1954 | verifyFormat("@protected"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1955 | verifyFormat("@protocol"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1956 | verifyFormat("@public"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1957 | verifyFormat("@required"); |
| 1958 | verifyFormat("@selector"); |
| 1959 | verifyFormat("@synchronized"); |
| 1960 | verifyFormat("@synthesize"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 1961 | verifyFormat("@throw"); |
| 1962 | verifyFormat("@try"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 1963 | |
Nico Weber | cb4d690 | 2013-01-08 19:40:21 +0000 | [diff] [blame] | 1964 | verifyFormat("@\"String\""); |
| 1965 | verifyFormat("@1"); |
Nico Weber | 81ed2f1 | 2013-01-10 19:36:35 +0000 | [diff] [blame] | 1966 | verifyFormat("@+4.8"); |
| 1967 | verifyFormat("@-4"); |
Nico Weber | cb4d690 | 2013-01-08 19:40:21 +0000 | [diff] [blame] | 1968 | verifyFormat("@1LL"); |
| 1969 | verifyFormat("@.5"); |
| 1970 | verifyFormat("@'c'"); |
| 1971 | verifyFormat("@true"); |
| 1972 | verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); |
Nico Weber | e8ccc81 | 2013-01-12 22:48:47 +0000 | [diff] [blame] | 1973 | // FIXME: Array and dictionary literals need more work. |
Nico Weber | cb4d690 | 2013-01-08 19:40:21 +0000 | [diff] [blame] | 1974 | verifyFormat("@["); |
| 1975 | verifyFormat("@{"); |
| 1976 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 1977 | EXPECT_EQ("@interface", format("@ interface")); |
| 1978 | |
| 1979 | // The precise formatting of this doesn't matter, nobody writes code like |
| 1980 | // this. |
| 1981 | verifyFormat("@ /*foo*/ interface"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1984 | TEST_F(FormatTest, ObjCSnippets) { |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1985 | verifyFormat("@autoreleasepool {\n" |
| 1986 | " foo();\n" |
| 1987 | "}"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1988 | verifyFormat("@class Foo, Bar;"); |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1989 | verifyFormat("@compatibility_alias AliasName ExistingClass;"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 1990 | verifyFormat("@dynamic textColor;"); |
Nico Weber | 5dfe9b4 | 2013-01-18 05:11:47 +0000 | [diff] [blame] | 1991 | verifyFormat("char *buf1 = @encode(int *);"); |
| 1992 | verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); |
| 1993 | // FIXME: Enable once PR14884 is fixed: |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 1994 | //verifyFormat("char *buf1 = @encode(int **);"); |
Nico Weber | 3a2673e | 2013-01-08 20:16:23 +0000 | [diff] [blame] | 1995 | verifyFormat("Protocol *proto = @protocol(p1);"); |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 1996 | verifyFormat("SEL s = @selector(foo:);"); |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 1997 | verifyFormat("@synchronized(self) {\n" |
| 1998 | " f();\n" |
| 1999 | "}"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 2000 | |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 2001 | verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); |
| 2002 | verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); |
| 2003 | |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 2004 | verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 2005 | verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); |
| 2006 | verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 2009 | } // end namespace tooling |
| 2010 | } // end namespace clang |