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 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 18 | namespace clang { |
| 19 | namespace format { |
| 20 | |
| 21 | class FormatTest : public ::testing::Test { |
| 22 | protected: |
| 23 | std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, |
| 24 | const FormatStyle &Style) { |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 25 | DEBUG(llvm::errs() << "---\n"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 26 | RewriterTestContext Context; |
| 27 | FileID ID = Context.createInMemoryFile("input.cc", Code); |
| 28 | SourceLocation Start = |
| 29 | Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset); |
| 30 | std::vector<CharSourceRange> Ranges( |
| 31 | 1, |
| 32 | CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length))); |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 33 | Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, |
| 34 | getFormattingLangOpts()); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 35 | tooling::Replacements Replace = reformat( |
| 36 | Style, Lex, Context.Sources, Ranges, new IgnoringDiagConsumer()); |
Daniel Jasper | afcbd85 | 2013-01-30 09:46:12 +0000 | [diff] [blame] | 37 | ReplacementCount = Replace.size(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 38 | EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); |
Manuel Klimek | ca547db | 2013-01-16 14:55:28 +0000 | [diff] [blame] | 39 | DEBUG(llvm::errs() << "\n" << Context.getRewrittenText(ID) << "\n\n"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 40 | return Context.getRewrittenText(ID); |
| 41 | } |
| 42 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 43 | std::string |
| 44 | format(llvm::StringRef Code, const FormatStyle &Style = getLLVMStyle()) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 45 | return format(Code, 0, Code.size(), Style); |
| 46 | } |
| 47 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 48 | std::string messUp(llvm::StringRef Code) { |
| 49 | std::string MessedUp(Code.str()); |
| 50 | bool InComment = false; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 51 | bool InPreprocessorDirective = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 52 | bool JustReplacedNewline = false; |
| 53 | for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { |
| 54 | if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { |
| 55 | if (JustReplacedNewline) |
| 56 | MessedUp[i - 1] = '\n'; |
| 57 | InComment = true; |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 58 | } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) { |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 59 | if (i != 0) |
| 60 | MessedUp[i - 1] = '\n'; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 61 | InPreprocessorDirective = true; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 62 | } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') { |
| 63 | MessedUp[i] = ' '; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 64 | MessedUp[i + 1] = ' '; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 65 | } else if (MessedUp[i] == '\n') { |
| 66 | if (InComment) { |
| 67 | InComment = false; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 68 | } else if (InPreprocessorDirective) { |
| 69 | InPreprocessorDirective = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 70 | } else { |
| 71 | JustReplacedNewline = true; |
| 72 | MessedUp[i] = ' '; |
| 73 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 74 | } else if (MessedUp[i] != ' ') { |
| 75 | JustReplacedNewline = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 76 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 77 | } |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 78 | return MessedUp; |
| 79 | } |
| 80 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 81 | FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { |
| 82 | FormatStyle Style = getLLVMStyle(); |
| 83 | Style.ColumnLimit = ColumnLimit; |
| 84 | return Style; |
| 85 | } |
| 86 | |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 87 | FormatStyle getGoogleStyleWithColumns(unsigned ColumnLimit) { |
| 88 | FormatStyle Style = getGoogleStyle(); |
| 89 | Style.ColumnLimit = ColumnLimit; |
| 90 | return Style; |
| 91 | } |
| 92 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 93 | void verifyFormat(llvm::StringRef Code, |
| 94 | const FormatStyle &Style = getLLVMStyle()) { |
| 95 | EXPECT_EQ(Code.str(), format(messUp(Code), Style)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void verifyGoogleFormat(llvm::StringRef Code) { |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 99 | verifyFormat(Code, getGoogleStyle()); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 100 | } |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 101 | |
| 102 | void verifyIndependentOfContext(llvm::StringRef text) { |
| 103 | verifyFormat(text); |
| 104 | verifyFormat(llvm::Twine("void f() { " + text + " }").str()); |
| 105 | } |
Daniel Jasper | afcbd85 | 2013-01-30 09:46:12 +0000 | [diff] [blame] | 106 | |
| 107 | int ReplacementCount; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 110 | TEST_F(FormatTest, MessUp) { |
| 111 | EXPECT_EQ("1 2 3", messUp("1 2 3")); |
| 112 | EXPECT_EQ("1 2 3\n", messUp("1\n2\n3\n")); |
| 113 | EXPECT_EQ("a\n//b\nc", messUp("a\n//b\nc")); |
| 114 | EXPECT_EQ("a\n#b\nc", messUp("a\n#b\nc")); |
| 115 | EXPECT_EQ("a\n#b c d\ne", messUp("a\n#b\\\nc\\\nd\ne")); |
| 116 | } |
| 117 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 118 | //===----------------------------------------------------------------------===// |
| 119 | // Basic function tests. |
| 120 | //===----------------------------------------------------------------------===// |
| 121 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 122 | TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { |
| 123 | EXPECT_EQ(";", format(";")); |
| 124 | } |
| 125 | |
| 126 | TEST_F(FormatTest, FormatsGlobalStatementsAt0) { |
| 127 | EXPECT_EQ("int i;", format(" int i;")); |
| 128 | EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); |
| 129 | EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); |
| 130 | EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); |
| 131 | } |
| 132 | |
| 133 | TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { |
| 134 | EXPECT_EQ("int i;", format("int\ni;")); |
| 135 | } |
| 136 | |
| 137 | TEST_F(FormatTest, FormatsNestedBlockStatements) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 138 | EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 141 | TEST_F(FormatTest, FormatsNestedCall) { |
| 142 | verifyFormat("Method(f1, f2(f3));"); |
| 143 | verifyFormat("Method(f1(f2, f3()));"); |
Daniel Jasper | 4e9008a | 2013-01-13 08:19:51 +0000 | [diff] [blame] | 144 | verifyFormat("Method(f1(f2, (f3())));"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Daniel Jasper | daf1a15 | 2013-02-07 21:08:36 +0000 | [diff] [blame] | 147 | TEST_F(FormatTest, NestedNameSpecifiers) { |
Daniel Jasper | 6b825c2 | 2013-01-16 07:19:28 +0000 | [diff] [blame] | 148 | verifyFormat("vector< ::Type> v;"); |
Daniel Jasper | daf1a15 | 2013-02-07 21:08:36 +0000 | [diff] [blame] | 149 | verifyFormat("::ns::SomeFunction(::ns::SomeOtherFunction())"); |
Daniel Jasper | 6b825c2 | 2013-01-16 07:19:28 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Daniel Jasper | afcbd85 | 2013-01-30 09:46:12 +0000 | [diff] [blame] | 152 | TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { |
| 153 | EXPECT_EQ("if (a) {\n" |
| 154 | " f();\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 155 | "}", |
| 156 | format("if(a){f();}")); |
Daniel Jasper | afcbd85 | 2013-01-30 09:46:12 +0000 | [diff] [blame] | 157 | EXPECT_EQ(4, ReplacementCount); |
| 158 | EXPECT_EQ("if (a) {\n" |
| 159 | " f();\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 160 | "}", |
| 161 | format("if (a) {\n" |
| 162 | " f();\n" |
| 163 | "}")); |
Daniel Jasper | afcbd85 | 2013-01-30 09:46:12 +0000 | [diff] [blame] | 164 | EXPECT_EQ(0, ReplacementCount); |
| 165 | } |
| 166 | |
Daniel Jasper | c78c6b3 | 2013-02-14 09:58:41 +0000 | [diff] [blame] | 167 | TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) { |
| 168 | EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle())); |
Daniel Jasper | 516fb31 | 2013-03-01 18:11:39 +0000 | [diff] [blame] | 169 | EXPECT_EQ("int a;", format("int a; ")); |
| 170 | EXPECT_EQ("int a;\n", format("int a; \n \n \n ")); |
| 171 | EXPECT_EQ("int a;\nint b; ", |
| 172 | format("int a; \nint b; ", 0, 0, getLLVMStyle())); |
Daniel Jasper | c78c6b3 | 2013-02-14 09:58:41 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Daniel Jasper | 1eee6c4 | 2013-03-04 13:43:19 +0000 | [diff] [blame] | 175 | TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) { |
| 176 | EXPECT_EQ("int b;\nint a;", |
| 177 | format("int b;\n int a;", 7, 0, getLLVMStyle())); |
| 178 | EXPECT_EQ("int b;\n int a;", |
| 179 | format("int b;\n int a;", 6, 0, getLLVMStyle())); |
| 180 | |
| 181 | EXPECT_EQ("#define A \\\n" |
| 182 | " int a; \\\n" |
| 183 | " int b;", |
| 184 | format("#define A \\\n" |
| 185 | " int a; \\\n" |
| 186 | " int b;", |
| 187 | 26, 0, getLLVMStyleWithColumns(12))); |
| 188 | EXPECT_EQ("#define A \\\n" |
| 189 | " int a; \\\n" |
| 190 | " int b;", |
| 191 | format("#define A \\\n" |
| 192 | " int a; \\\n" |
| 193 | " int b;", |
| 194 | 25, 0, getLLVMStyleWithColumns(12))); |
| 195 | } |
| 196 | |
Daniel Jasper | f302354 | 2013-03-07 20:50:00 +0000 | [diff] [blame] | 197 | TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) { |
| 198 | EXPECT_EQ("int a;\n\n int b;", |
| 199 | format("int a;\n \n\n int b;", 7, 0, getLLVMStyle())); |
| 200 | EXPECT_EQ("int a;\n\n int b;", |
| 201 | format("int a;\n \n\n int b;", 9, 0, getLLVMStyle())); |
| 202 | } |
| 203 | |
Daniel Jasper | 2933316 | 2013-02-18 13:08:03 +0000 | [diff] [blame] | 204 | TEST_F(FormatTest, ReformatsMovedLines) { |
| 205 | EXPECT_EQ( |
| 206 | "template <typename T> T *getFETokenInfo() const {\n" |
| 207 | " return static_cast<T *>(FETokenInfo);\n" |
| 208 | "}\n" |
| 209 | " int a; // <- Should not be formatted", |
| 210 | format( |
| 211 | "template<typename T>\n" |
| 212 | "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n" |
| 213 | " int a; // <- Should not be formatted", |
| 214 | 9, 5, getLLVMStyle())); |
| 215 | } |
| 216 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 217 | //===----------------------------------------------------------------------===// |
| 218 | // Tests for control statements. |
| 219 | //===----------------------------------------------------------------------===// |
| 220 | |
| 221 | TEST_F(FormatTest, FormatIfWithoutCompountStatement) { |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 222 | verifyFormat("if (true)\n f();\ng();"); |
| 223 | verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 224 | verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 225 | |
| 226 | FormatStyle AllowsMergedIf = getGoogleStyle(); |
| 227 | AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; |
| 228 | verifyFormat("if (a)\n" |
| 229 | " // comment\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 230 | " f();", |
| 231 | AllowsMergedIf); |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 232 | |
| 233 | verifyFormat("if (a) // Can't merge this\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 234 | " f();\n", |
| 235 | AllowsMergedIf); |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 236 | verifyFormat("if (a) /* still don't merge */\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 237 | " f();", |
| 238 | AllowsMergedIf); |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 239 | verifyFormat("if (a) { // Never merge this\n" |
| 240 | " f();\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 241 | "}", |
| 242 | AllowsMergedIf); |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 243 | verifyFormat("if (a) { /* Never merge this */\n" |
| 244 | " f();\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 245 | "}", |
| 246 | AllowsMergedIf); |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 247 | |
| 248 | AllowsMergedIf.ColumnLimit = 14; |
| 249 | verifyFormat("if (a) return;", AllowsMergedIf); |
Daniel Jasper | fd0ca97 | 2013-01-14 16:02:06 +0000 | [diff] [blame] | 250 | verifyFormat("if (aaaaaaaaa)\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 251 | " return;", |
| 252 | AllowsMergedIf); |
Daniel Jasper | df3736a | 2013-01-16 15:44:34 +0000 | [diff] [blame] | 253 | |
| 254 | AllowsMergedIf.ColumnLimit = 13; |
| 255 | verifyFormat("if (a)\n return;", AllowsMergedIf); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | TEST_F(FormatTest, ParseIfElse) { |
| 259 | verifyFormat("if (true)\n" |
| 260 | " if (true)\n" |
| 261 | " if (true)\n" |
| 262 | " f();\n" |
| 263 | " else\n" |
| 264 | " g();\n" |
| 265 | " else\n" |
| 266 | " h();\n" |
| 267 | "else\n" |
| 268 | " i();"); |
| 269 | verifyFormat("if (true)\n" |
| 270 | " if (true)\n" |
| 271 | " if (true) {\n" |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 272 | " if (true)\n" |
| 273 | " f();\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 274 | " } else {\n" |
| 275 | " g();\n" |
| 276 | " }\n" |
| 277 | " else\n" |
| 278 | " h();\n" |
| 279 | "else {\n" |
| 280 | " i();\n" |
| 281 | "}"); |
| 282 | } |
| 283 | |
| 284 | TEST_F(FormatTest, ElseIf) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 285 | verifyFormat("if (a) {\n} else if (b) {\n}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 286 | verifyFormat("if (a)\n" |
| 287 | " f();\n" |
| 288 | "else if (b)\n" |
| 289 | " g();\n" |
| 290 | "else\n" |
| 291 | " h();"); |
| 292 | } |
| 293 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 294 | TEST_F(FormatTest, FormatsForLoop) { |
| 295 | verifyFormat( |
| 296 | "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 297 | " ++VeryVeryLongLoopVariable)\n" |
| 298 | " ;"); |
| 299 | verifyFormat("for (;;)\n" |
| 300 | " f();"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 301 | verifyFormat("for (;;) {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 302 | verifyFormat("for (;;) {\n" |
| 303 | " f();\n" |
| 304 | "}"); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 305 | |
| 306 | verifyFormat( |
| 307 | "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" |
| 308 | " E = UnwrappedLines.end();\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 309 | " I != E; ++I) {\n}"); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 310 | |
| 311 | verifyFormat( |
| 312 | "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 313 | " ++IIIII) {\n}"); |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 314 | verifyFormat("for (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 315 | " aaaaaaaaaaa = aaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaa;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 316 | " aaaaaaaaaaa != aaaaaaaaaaaaaaaaaaa; ++aaaaaaaaaaa) {\n}"); |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 317 | verifyFormat("for (llvm::ArrayRef<NamedDecl *>::iterator\n" |
| 318 | " I = FD->getDeclsInPrototypeScope().begin(),\n" |
| 319 | " E = FD->getDeclsInPrototypeScope().end();\n" |
| 320 | " I != E; ++I) {\n}"); |
Daniel Jasper | 7522318 | 2013-01-28 17:30:17 +0000 | [diff] [blame] | 321 | |
| 322 | // FIXME: Not sure whether we want extra identation in line 3 here: |
| 323 | verifyFormat( |
| 324 | "for (aaaaaaaaaaaaaaaaa aaaaaaaaaaa = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n" |
| 325 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa !=\n" |
| 326 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 327 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" |
| 328 | " ++aaaaaaaaaaa) {\n}"); |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 329 | verifyFormat("for (int aaaaaaaaaaa = 1; aaaaaaaaaaa <= bbbbbbbbbbbbbbb;\n" |
| 330 | " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" |
| 331 | "}"); |
Daniel Jasper | 8159d2f | 2013-02-04 07:30:30 +0000 | [diff] [blame] | 332 | |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 333 | FormatStyle NoBinPacking = getLLVMStyle(); |
| 334 | NoBinPacking.BinPackParameters = false; |
| 335 | verifyFormat("for (int aaaaaaaaaaa = 1;\n" |
| 336 | " aaaaaaaaaaa <= aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa,\n" |
| 337 | " aaaaaaaaaaaaaaaa,\n" |
| 338 | " aaaaaaaaaaaaaaaa,\n" |
| 339 | " aaaaaaaaaaaaaaaa);\n" |
| 340 | " aaaaaaaaaaa++, bbbbbbbbbbbbbbbbb++) {\n" |
| 341 | "}", |
| 342 | NoBinPacking); |
| 343 | verifyFormat( |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 344 | "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" |
| 345 | " E = UnwrappedLines.end();\n" |
| 346 | " I != E;\n" |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 347 | " ++I) {\n}", |
| 348 | NoBinPacking); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Daniel Jasper | 3b9a8fc | 2013-01-28 13:21:16 +0000 | [diff] [blame] | 351 | TEST_F(FormatTest, RangeBasedForLoops) { |
| 352 | verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa :\n" |
| 353 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
| 354 | verifyFormat("for (auto aaaaaaaaaaaaaaaaaaaaa :\n" |
| 355 | " aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa, aaaaaaaaaaaaa)) {\n}"); |
Daniel Jasper | 84a1a63 | 2013-02-26 13:18:08 +0000 | [diff] [blame] | 356 | verifyFormat("for (const aaaaaaaaaaaaaaaaaaaaa &aaaaaaaaa :\n" |
| 357 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 3b9a8fc | 2013-01-28 13:21:16 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 360 | TEST_F(FormatTest, FormatsWhileLoop) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 361 | verifyFormat("while (true) {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 362 | verifyFormat("while (true)\n" |
| 363 | " f();"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 364 | verifyFormat("while () {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 365 | verifyFormat("while () {\n" |
| 366 | " f();\n" |
| 367 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 370 | TEST_F(FormatTest, FormatsDoWhile) { |
| 371 | verifyFormat("do {\n" |
| 372 | " do_something();\n" |
| 373 | "} while (something());"); |
| 374 | verifyFormat("do\n" |
| 375 | " do_something();\n" |
| 376 | "while (something());"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 379 | TEST_F(FormatTest, FormatsSwitchStatement) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 380 | verifyFormat("switch (x) {\n" |
| 381 | "case 1:\n" |
| 382 | " f();\n" |
| 383 | " break;\n" |
| 384 | "case kFoo:\n" |
| 385 | "case ns::kBar:\n" |
| 386 | "case kBaz:\n" |
| 387 | " break;\n" |
| 388 | "default:\n" |
| 389 | " g();\n" |
| 390 | " break;\n" |
| 391 | "}"); |
| 392 | verifyFormat("switch (x) {\n" |
| 393 | "case 1: {\n" |
| 394 | " f();\n" |
| 395 | " break;\n" |
| 396 | "}\n" |
| 397 | "}"); |
Nico Weber | 94fb729 | 2013-01-18 05:50:57 +0000 | [diff] [blame] | 398 | verifyFormat("switch (x) {\n" |
| 399 | "case 1: {\n" |
| 400 | " f();\n" |
| 401 | " {\n" |
| 402 | " g();\n" |
| 403 | " h();\n" |
| 404 | " }\n" |
| 405 | " break;\n" |
| 406 | "}\n" |
| 407 | "}"); |
| 408 | verifyFormat("switch (x) {\n" |
| 409 | "case 1: {\n" |
| 410 | " f();\n" |
| 411 | " if (foo) {\n" |
| 412 | " g();\n" |
| 413 | " h();\n" |
| 414 | " }\n" |
| 415 | " break;\n" |
| 416 | "}\n" |
| 417 | "}"); |
| 418 | verifyFormat("switch (x) {\n" |
| 419 | "case 1: {\n" |
| 420 | " f();\n" |
| 421 | " g();\n" |
| 422 | "} break;\n" |
| 423 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 424 | verifyFormat("switch (test)\n" |
| 425 | " ;"); |
Daniel Jasper | 4ba9bfd | 2013-03-12 12:26:55 +0000 | [diff] [blame] | 426 | verifyFormat("switch (x) {\n" |
| 427 | "default: {\n" |
| 428 | " // Do nothing.\n" |
| 429 | "}"); |
Daniel Jasper | 6050a1e | 2013-03-13 15:53:12 +0000 | [diff] [blame] | 430 | verifyFormat("switch (x) {\n" |
| 431 | "// if 1, do f()\n" |
| 432 | "case 1:\n" |
| 433 | " f();\n" |
| 434 | "}"); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 435 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 436 | verifyGoogleFormat("switch (x) {\n" |
| 437 | " case 1:\n" |
| 438 | " f();\n" |
| 439 | " break;\n" |
| 440 | " case kFoo:\n" |
| 441 | " case ns::kBar:\n" |
| 442 | " case kBaz:\n" |
| 443 | " break;\n" |
| 444 | " default:\n" |
| 445 | " g();\n" |
| 446 | " break;\n" |
| 447 | "}"); |
| 448 | verifyGoogleFormat("switch (x) {\n" |
| 449 | " case 1: {\n" |
| 450 | " f();\n" |
| 451 | " break;\n" |
| 452 | " }\n" |
| 453 | "}"); |
| 454 | verifyGoogleFormat("switch (test)\n" |
| 455 | " ;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 458 | TEST_F(FormatTest, FormatsLabels) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 459 | verifyFormat("void f() {\n" |
| 460 | " some_code();\n" |
| 461 | "test_label:\n" |
| 462 | " some_other_code();\n" |
| 463 | " {\n" |
| 464 | " some_more_code();\n" |
| 465 | " another_label:\n" |
| 466 | " some_more_code();\n" |
| 467 | " }\n" |
| 468 | "}"); |
| 469 | verifyFormat("some_code();\n" |
| 470 | "test_label:\n" |
| 471 | "some_other_code();"); |
| 472 | } |
| 473 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 474 | //===----------------------------------------------------------------------===// |
| 475 | // Tests for comments. |
| 476 | //===----------------------------------------------------------------------===// |
| 477 | |
| 478 | TEST_F(FormatTest, UnderstandsSingleLineComments) { |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 479 | verifyFormat("// line 1\n" |
| 480 | "// line 2\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 481 | "void f() {}\n"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 482 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 483 | verifyFormat("void f() {\n" |
| 484 | " // Doesn't do anything\n" |
| 485 | "}"); |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 486 | verifyFormat("void f(int i, // some comment (probably for i)\n" |
| 487 | " int j, // some comment (probably for j)\n" |
Daniel Jasper | 487f64b | 2013-01-13 16:10:20 +0000 | [diff] [blame] | 488 | " int k); // some comment (probably for k)"); |
| 489 | verifyFormat("void f(int i,\n" |
| 490 | " // some comment (probably for j)\n" |
| 491 | " int j,\n" |
| 492 | " // some comment (probably for k)\n" |
| 493 | " int k);"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 494 | |
Daniel Jasper | dcc2a62 | 2013-01-18 08:44:07 +0000 | [diff] [blame] | 495 | verifyFormat("int i // This is a fancy variable\n" |
| 496 | " = 5; // with nicely aligned comment."); |
| 497 | |
| 498 | verifyFormat("// Leading comment.\n" |
| 499 | "int a; // Trailing comment."); |
| 500 | verifyFormat("int a; // Trailing comment\n" |
| 501 | " // on 2\n" |
| 502 | " // or 3 lines.\n" |
| 503 | "int b;"); |
| 504 | verifyFormat("int a; // Trailing comment\n" |
| 505 | "\n" |
| 506 | "// Leading comment.\n" |
| 507 | "int b;"); |
| 508 | verifyFormat("int a; // Comment.\n" |
| 509 | " // More details.\n" |
| 510 | "int bbbb; // Another comment."); |
| 511 | verifyFormat( |
| 512 | "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; // comment\n" |
| 513 | "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // comment\n" |
| 514 | "int cccccccccccccccccccccccccccccc; // comment\n" |
| 515 | "int ddd; // looooooooooooooooooooooooong comment\n" |
| 516 | "int aaaaaaaaaaaaaaaaaaaaaaa; // comment\n" |
| 517 | "int bbbbbbbbbbbbbbbbbbbbb; // comment\n" |
| 518 | "int ccccccccccccccccccc; // comment"); |
| 519 | |
Daniel Jasper | 7d1185d | 2013-01-18 09:19:33 +0000 | [diff] [blame] | 520 | verifyFormat("#include \"a\" // comment\n" |
| 521 | "#include \"a/b/c\" // comment"); |
| 522 | verifyFormat("#include <a> // comment\n" |
| 523 | "#include <a/b/c> // comment"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 524 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 525 | verifyFormat("enum E {\n" |
| 526 | " // comment\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 527 | " VAL_A, // comment\n" |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 528 | " VAL_B\n" |
| 529 | "};"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 530 | |
| 531 | verifyFormat( |
| 532 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 533 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); |
Daniel Jasper | 043835a | 2013-01-09 09:33:39 +0000 | [diff] [blame] | 534 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 535 | " // Comment inside a statement.\n" |
| 536 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); |
Daniel Jasper | 36ba0ff | 2013-02-06 15:23:09 +0000 | [diff] [blame] | 537 | verifyFormat( |
| 538 | "bool aaaaaaaaaaaaa = // comment\n" |
| 539 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 540 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 541 | |
Daniel Jasper | 42f458d | 2013-02-13 19:25:54 +0000 | [diff] [blame] | 542 | verifyFormat("int aaaa; // aaaaa\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 543 | "int aa; // aaaaaaa", |
| 544 | getLLVMStyleWithColumns(20)); |
Daniel Jasper | 42f458d | 2013-02-13 19:25:54 +0000 | [diff] [blame] | 545 | |
Daniel Jasper | 821627e | 2013-01-21 22:49:20 +0000 | [diff] [blame] | 546 | EXPECT_EQ("void f() { // This does something ..\n" |
| 547 | "}\n" |
| 548 | "int a; // This is unrelated", |
| 549 | format("void f() { // This does something ..\n" |
| 550 | " }\n" |
| 551 | "int a; // This is unrelated")); |
| 552 | EXPECT_EQ("void f() { // This does something ..\n" |
| 553 | "} // awesome..\n" |
| 554 | "\n" |
| 555 | "int a; // This is unrelated", |
| 556 | format("void f() { // This does something ..\n" |
| 557 | " } // awesome..\n" |
| 558 | " \n" |
| 559 | "int a; // This is unrelated")); |
| 560 | |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 561 | EXPECT_EQ("int i; // single line trailing comment", |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 562 | format("int i;\\\n// single line trailing comment")); |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 563 | |
| 564 | verifyGoogleFormat("int a; // Trailing comment."); |
Daniel Jasper | d4f2c2e | 2013-01-29 19:41:55 +0000 | [diff] [blame] | 565 | |
| 566 | verifyFormat("someFunction(anotherFunction( // Force break.\n" |
| 567 | " parameter));"); |
Daniel Jasper | 5b7e7b0 | 2013-02-05 09:34:14 +0000 | [diff] [blame] | 568 | |
| 569 | verifyGoogleFormat("#endif // HEADER_GUARD"); |
Daniel Jasper | 474e462 | 2013-02-06 22:04:05 +0000 | [diff] [blame] | 570 | |
| 571 | verifyFormat("const char *test[] = {\n" |
| 572 | " // A\n" |
| 573 | " \"aaaa\",\n" |
| 574 | " // B\n" |
| 575 | " \"aaaaa\",\n" |
| 576 | "};"); |
Daniel Jasper | 729a743 | 2013-02-11 12:36:37 +0000 | [diff] [blame] | 577 | verifyGoogleFormat( |
| 578 | "aaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 579 | " aaaaaaaaaaaaaaaaaaaaaa); // 81 cols with this comment"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Daniel Jasper | 812c045 | 2013-03-01 16:45:59 +0000 | [diff] [blame] | 582 | TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { |
| 583 | EXPECT_EQ("// comment", format("// comment ")); |
| 584 | EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", |
| 585 | format("int aaaaaaa, bbbbbbb; // comment ", |
| 586 | getLLVMStyleWithColumns(33))); |
| 587 | } |
| 588 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 589 | TEST_F(FormatTest, UnderstandsMultiLineComments) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 590 | verifyFormat("f(/*test=*/ true);"); |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 591 | EXPECT_EQ( |
| 592 | "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" |
| 593 | " bbbbbbbbbbbbbbbbbbbbbbbbb);", |
| 594 | format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , /* Trailing comment for aa... */\n" |
| 595 | " bbbbbbbbbbbbbbbbbbbbbbbbb);")); |
| 596 | EXPECT_EQ( |
| 597 | "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 598 | " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", |
| 599 | format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n" |
| 600 | "/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);")); |
Daniel Jasper | d4f2c2e | 2013-01-29 19:41:55 +0000 | [diff] [blame] | 601 | |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 602 | FormatStyle NoBinPacking = getLLVMStyle(); |
| 603 | NoBinPacking.BinPackParameters = false; |
| 604 | verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" |
| 605 | " /* parameter 2 */ aaaaaa,\n" |
| 606 | " /* parameter 3 */ aaaaaa,\n" |
| 607 | " /* parameter 4 */ aaaaaa);", |
| 608 | NoBinPacking); |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Alexander Kornienko | f753615 | 2013-03-14 16:10:54 +0000 | [diff] [blame] | 611 | TEST_F(FormatTest, AlignsMultiLineComments) { |
| 612 | EXPECT_EQ("/*\n" |
| 613 | " * Really multi-line\n" |
| 614 | " * comment.\n" |
| 615 | " */\n" |
| 616 | "void f() {}", |
| 617 | format(" /*\n" |
| 618 | " * Really multi-line\n" |
| 619 | " * comment.\n" |
| 620 | " */\n" |
| 621 | " void f() {}")); |
Alexander Kornienko | f753615 | 2013-03-14 16:10:54 +0000 | [diff] [blame] | 622 | EXPECT_EQ("class C {\n" |
| 623 | " /*\n" |
| 624 | " * Another multi-line\n" |
| 625 | " * comment.\n" |
| 626 | " */\n" |
| 627 | " void f() {}\n" |
| 628 | "};", |
| 629 | format("class C {\n" |
| 630 | "/*\n" |
| 631 | " * Another multi-line\n" |
| 632 | " * comment.\n" |
| 633 | " */\n" |
| 634 | "void f() {}\n" |
| 635 | "};")); |
Alexander Kornienko | 1fdd8b3 | 2013-03-15 13:42:02 +0000 | [diff] [blame] | 636 | EXPECT_EQ("/*\n" |
| 637 | " 1. This is a comment with non-trivial formatting.\n" |
| 638 | " 1.1. We have to indent/outdent all lines equally\n" |
| 639 | " 1.1.1. to keep the formatting.\n" |
| 640 | " */", |
| 641 | format(" /*\n" |
| 642 | " 1. This is a comment with non-trivial formatting.\n" |
| 643 | " 1.1. We have to indent/outdent all lines equally\n" |
| 644 | " 1.1.1. to keep the formatting.\n" |
| 645 | " */")); |
| 646 | EXPECT_EQ("/*\n" |
| 647 | " Don't try to outdent if there's not enough inentation.\n" |
| 648 | " */", |
| 649 | format(" /*\n" |
| 650 | " Don't try to outdent if there's not enough inentation.\n" |
| 651 | " */")); |
Alexander Kornienko | f753615 | 2013-03-14 16:10:54 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 654 | TEST_F(FormatTest, CommentsInStaticInitializers) { |
| 655 | EXPECT_EQ( |
| 656 | "static SomeType type = { aaaaaaaaaaaaaaaaaaaa, /* comment */\n" |
| 657 | " aaaaaaaaaaaaaaaaaaaa /* comment */,\n" |
| 658 | " /* comment */ aaaaaaaaaaaaaaaaaaaa,\n" |
| 659 | " aaaaaaaaaaaaaaaaaaaa, // comment\n" |
| 660 | " aaaaaaaaaaaaaaaaaaaa };", |
| 661 | format("static SomeType type = { aaaaaaaaaaaaaaaaaaaa , /* comment */\n" |
| 662 | " aaaaaaaaaaaaaaaaaaaa /* comment */ ,\n" |
| 663 | " /* comment */ aaaaaaaaaaaaaaaaaaaa ,\n" |
| 664 | " aaaaaaaaaaaaaaaaaaaa , // comment\n" |
| 665 | " aaaaaaaaaaaaaaaaaaaa };")); |
| 666 | verifyFormat("static SomeType type = { aaaaaaaaaaa, // comment for aa...\n" |
| 667 | " bbbbbbbbbbb, ccccccccccc };"); |
| 668 | verifyFormat("static SomeType type = { aaaaaaaaaaa,\n" |
| 669 | " // comment for bb....\n" |
| 670 | " bbbbbbbbbbb, ccccccccccc };"); |
| 671 | verifyGoogleFormat( |
| 672 | "static SomeType type = { aaaaaaaaaaa, // comment for aa...\n" |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 673 | " bbbbbbbbbbb, ccccccccccc };"); |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 674 | verifyGoogleFormat("static SomeType type = { aaaaaaaaaaa,\n" |
| 675 | " // comment for bb....\n" |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 676 | " bbbbbbbbbbb, ccccccccccc };"); |
Daniel Jasper | 2c6cc48 | 2013-01-17 12:53:34 +0000 | [diff] [blame] | 677 | |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 678 | verifyFormat("S s = { { a, b, c }, // Group #1\n" |
| 679 | " { d, e, f }, // Group #2\n" |
| 680 | " { g, h, i } }; // Group #3"); |
| 681 | verifyFormat("S s = { { // Group #1\n" |
| 682 | " a, b, c },\n" |
| 683 | " { // Group #2\n" |
| 684 | " d, e, f },\n" |
| 685 | " { // Group #3\n" |
| 686 | " g, h, i } };"); |
Daniel Jasper | c4615b7 | 2013-02-20 12:56:39 +0000 | [diff] [blame] | 687 | |
| 688 | EXPECT_EQ("S s = {\n" |
| 689 | " // Some comment\n" |
Daniel Jasper | 1ef81d5 | 2013-02-26 13:10:34 +0000 | [diff] [blame] | 690 | " a,\n" |
Daniel Jasper | c4615b7 | 2013-02-20 12:56:39 +0000 | [diff] [blame] | 691 | "\n" |
| 692 | " // Comment after empty line\n" |
| 693 | " b\n" |
Daniel Jasper | 1ef81d5 | 2013-02-26 13:10:34 +0000 | [diff] [blame] | 694 | "}", |
| 695 | format("S s = {\n" |
| 696 | " // Some comment\n" |
| 697 | " a,\n" |
| 698 | " \n" |
| 699 | " // Comment after empty line\n" |
| 700 | " b\n" |
| 701 | "}")); |
| 702 | EXPECT_EQ("S s = { a, b };", format("S s = {\n" |
Daniel Jasper | 84a1a63 | 2013-02-26 13:18:08 +0000 | [diff] [blame] | 703 | " a,\n" |
| 704 | "\n" |
| 705 | " b\n" |
| 706 | "};")); |
Daniel Jasper | 25e81b2 | 2013-02-28 15:04:12 +0000 | [diff] [blame] | 707 | verifyFormat("const uint8_t aaaaaaaaaaaaaaaaaaaaaa[0] = {\n" |
| 708 | " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" |
| 709 | " 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // comment\n" |
| 710 | " 0x00, 0x00, 0x00, 0x00 // comment\n" |
| 711 | "};"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 712 | } |
| 713 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 714 | //===----------------------------------------------------------------------===// |
| 715 | // Tests for classes, namespaces, etc. |
| 716 | //===----------------------------------------------------------------------===// |
| 717 | |
| 718 | TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 719 | verifyFormat("class A {\n};"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | TEST_F(FormatTest, UnderstandsAccessSpecifiers) { |
| 723 | verifyFormat("class A {\n" |
| 724 | "public:\n" |
| 725 | "protected:\n" |
| 726 | "private:\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 727 | " void f() {}\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 728 | "};"); |
| 729 | verifyGoogleFormat("class A {\n" |
| 730 | " public:\n" |
| 731 | " protected:\n" |
| 732 | " private:\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 733 | " void f() {}\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 734 | "};"); |
| 735 | } |
| 736 | |
| 737 | TEST_F(FormatTest, FormatsDerivedClass) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 738 | verifyFormat("class A : public B {\n};"); |
| 739 | verifyFormat("class A : public ::B {\n};"); |
Daniel Jasper | 6cabab4 | 2013-02-14 08:42:54 +0000 | [diff] [blame] | 740 | |
| 741 | verifyFormat( |
| 742 | "class AAAAAAAAAAAAAAAAAAAA : public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" |
| 743 | " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n" |
| 744 | "};\n"); |
| 745 | verifyFormat("class AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA :\n" |
| 746 | " public BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB,\n" |
| 747 | " public CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC {\n" |
| 748 | "};\n"); |
| 749 | verifyFormat( |
| 750 | "class A : public B, public C, public D, public E, public F, public G {\n" |
| 751 | "};"); |
| 752 | verifyFormat("class AAAAAAAAAAAA : public B,\n" |
| 753 | " public C,\n" |
| 754 | " public D,\n" |
| 755 | " public E,\n" |
| 756 | " public F,\n" |
| 757 | " public G {\n" |
| 758 | "};"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 761 | TEST_F(FormatTest, FormatsVariableDeclarationsAfterStructOrClass) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 762 | verifyFormat("class A {\n} a, b;"); |
| 763 | verifyFormat("struct A {\n} a, b;"); |
| 764 | verifyFormat("union A {\n} a;"); |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 767 | TEST_F(FormatTest, FormatsEnum) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 768 | verifyFormat("enum {\n" |
| 769 | " Zero,\n" |
| 770 | " One = 1,\n" |
| 771 | " Two = One + 1,\n" |
| 772 | " Three = (One + Two),\n" |
| 773 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
| 774 | " Five = (One, Two, Three, Four, 5)\n" |
| 775 | "};"); |
| 776 | verifyFormat("enum Enum {\n" |
| 777 | "};"); |
| 778 | verifyFormat("enum {\n" |
| 779 | "};"); |
Manuel Klimek | 308232c | 2013-01-21 19:17:52 +0000 | [diff] [blame] | 780 | verifyFormat("enum X E {\n} d;"); |
| 781 | verifyFormat("enum __attribute__((...)) E {\n} d;"); |
| 782 | verifyFormat("enum __declspec__((...)) E {\n} d;"); |
| 783 | verifyFormat("enum X f() {\n a();\n return 42;\n}"); |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Nico Weber | efaddc0 | 2013-01-14 05:49:49 +0000 | [diff] [blame] | 786 | TEST_F(FormatTest, FormatsBitfields) { |
| 787 | verifyFormat("struct Bitfields {\n" |
| 788 | " unsigned sClass : 8;\n" |
| 789 | " unsigned ValueKind : 2;\n" |
| 790 | "};"); |
| 791 | } |
| 792 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 793 | TEST_F(FormatTest, FormatsNamespaces) { |
| 794 | verifyFormat("namespace some_namespace {\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 795 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 796 | "void f() { f(); }\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 797 | "}"); |
| 798 | verifyFormat("namespace {\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 799 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 800 | "void f() { f(); }\n" |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 801 | "}"); |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 802 | verifyFormat("inline namespace X {\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 803 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 804 | "void f() { f(); }\n" |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 805 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 806 | verifyFormat("using namespace some_namespace;\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 807 | "class A {\n};\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 808 | "void f() { f(); }"); |
Manuel Klimek | 7fc2db0 | 2013-02-06 16:08:09 +0000 | [diff] [blame] | 809 | |
| 810 | // This code is more common than we thought; if we |
| 811 | // layout this correctly the semicolon will go into |
| 812 | // its own line, which is undesireable. |
| 813 | verifyFormat("namespace {\n};"); |
| 814 | verifyFormat("namespace {\n" |
| 815 | "class A {\n" |
| 816 | "};\n" |
| 817 | "};"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 820 | TEST_F(FormatTest, FormatsExternC) { verifyFormat("extern \"C\" {\nint a;"); } |
Manuel Klimek | d19dc2d | 2013-01-21 14:32:05 +0000 | [diff] [blame] | 821 | |
Daniel Jasper | 923ebef | 2013-03-14 13:45:21 +0000 | [diff] [blame] | 822 | TEST_F(FormatTest, FormatsInlineASM) { |
| 823 | verifyFormat("asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"); |
| 824 | verifyFormat( |
| 825 | "asm(\"movq\\t%%rbx, %%rsi\\n\\t\"\n" |
| 826 | " \"cpuid\\n\\t\"\n" |
| 827 | " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" |
| 828 | " : \"=a\" (*rEAX), \"=S\" (*rEBX), \"=c\" (*rECX), \"=d\" (*rEDX)\n" |
| 829 | " : \"a\"(value));"); |
| 830 | } |
| 831 | |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 832 | TEST_F(FormatTest, FormatTryCatch) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 833 | // FIXME: Handle try-catch explicitly in the UnwrappedLineParser, then we'll |
| 834 | // also not create single-line-blocks. |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 835 | verifyFormat("try {\n" |
| 836 | " throw a * b;\n" |
| 837 | "}\n" |
| 838 | "catch (int a) {\n" |
| 839 | " // Do nothing.\n" |
| 840 | "}\n" |
| 841 | "catch (...) {\n" |
| 842 | " exit(42);\n" |
| 843 | "}"); |
| 844 | |
| 845 | // Function-level try statements. |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 846 | verifyFormat("int f() try { return 4; }\n" |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 847 | "catch (...) {\n" |
| 848 | " return 5;\n" |
| 849 | "}"); |
| 850 | verifyFormat("class A {\n" |
| 851 | " int a;\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 852 | " A() try : a(0) {}\n" |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 853 | " catch (...) {\n" |
| 854 | " throw;\n" |
| 855 | " }\n" |
| 856 | "};\n"); |
| 857 | } |
| 858 | |
| 859 | TEST_F(FormatTest, FormatObjCTryCatch) { |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 860 | verifyFormat("@try {\n" |
| 861 | " f();\n" |
| 862 | "}\n" |
| 863 | "@catch (NSException e) {\n" |
| 864 | " @throw;\n" |
| 865 | "}\n" |
| 866 | "@finally {\n" |
| 867 | " exit(42);\n" |
| 868 | "}"); |
Nico Weber | a9ccdd1 | 2013-01-07 16:36:17 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 871 | TEST_F(FormatTest, StaticInitializers) { |
| 872 | verifyFormat("static SomeClass SC = { 1, 'a' };"); |
| 873 | |
| 874 | // FIXME: Format like enums if the static initializer does not fit on a line. |
| 875 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 876 | "static SomeClass WithALoooooooooooooooooooongName = {\n" |
Manuel Klimek | c8c8a47 | 2013-01-10 15:58:26 +0000 | [diff] [blame] | 877 | " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 878 | "};"); |
| 879 | |
| 880 | verifyFormat( |
| 881 | "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" |
| 882 | " looooooooooooooooooooooooooooooooooongname,\n" |
| 883 | " looooooooooooooooooooooooooooooong };"); |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 884 | // Allow bin-packing in static initializers as this would often lead to |
| 885 | // terrible results, e.g.: |
| 886 | verifyGoogleFormat( |
| 887 | "static SomeClass = { a, b, c, d, e, f, g, h, i, j,\n" |
| 888 | " looooooooooooooooooooooooooooooooooongname,\n" |
| 889 | " looooooooooooooooooooooooooooooong };"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 892 | TEST_F(FormatTest, NestedStaticInitializers) { |
| 893 | verifyFormat("static A x = { { {} } };\n"); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 894 | verifyFormat("static A x = { { { init1, init2, init3, init4 },\n" |
| 895 | " { init1, init2, init3, init4 } } };"); |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 896 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 897 | verifyFormat("somes Status::global_reps[3] = {\n" |
| 898 | " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n" |
| 899 | " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n" |
| 900 | " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n" |
| 901 | "};"); |
| 902 | verifyGoogleFormat("somes Status::global_reps[3] = {\n" |
| 903 | " { kGlobalRef, OK_CODE, NULL, NULL, NULL },\n" |
| 904 | " { kGlobalRef, CANCELLED_CODE, NULL, NULL, NULL },\n" |
| 905 | " { kGlobalRef, UNKNOWN_CODE, NULL, NULL, NULL }\n" |
| 906 | "};"); |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 907 | verifyFormat( |
| 908 | "CGRect cg_rect = { { rect.fLeft, rect.fTop },\n" |
| 909 | " { rect.fRight - rect.fLeft, rect.fBottom - rect.fTop" |
| 910 | " } };"); |
| 911 | |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 912 | verifyFormat( |
| 913 | "SomeArrayOfSomeType a = { { { 1, 2, 3 }, { 1, 2, 3 },\n" |
| 914 | " { 111111111111111111111111111111,\n" |
| 915 | " 222222222222222222222222222222,\n" |
| 916 | " 333333333333333333333333333333 },\n" |
| 917 | " { 1, 2, 3 }, { 1, 2, 3 } } };"); |
Daniel Jasper | 6bd0232 | 2013-02-03 18:07:15 +0000 | [diff] [blame] | 918 | verifyFormat( |
| 919 | "SomeArrayOfSomeType a = { { { 1, 2, 3 } }, { { 1, 2, 3 } },\n" |
| 920 | " { { 111111111111111111111111111111,\n" |
| 921 | " 222222222222222222222222222222,\n" |
| 922 | " 333333333333333333333333333333 } },\n" |
| 923 | " { { 1, 2, 3 } }, { { 1, 2, 3 } } };"); |
Daniel Jasper | f343cab | 2013-01-31 14:59:26 +0000 | [diff] [blame] | 924 | |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 925 | // 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] | 926 | // lists, where we have an option to put each on a single line. |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 927 | verifyFormat( |
| 928 | "struct {\n" |
| 929 | " unsigned bit;\n" |
| 930 | " const char *const name;\n" |
| 931 | "} kBitsToOs[] = { { kOsMac, \"Mac\" }, { kOsWin, \"Windows\" },\n" |
| 932 | " { kOsLinux, \"Linux\" }, { kOsCrOS, \"Chrome OS\" } };"); |
Manuel Klimek | 2c7739e | 2013-01-14 16:41:43 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 935 | TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { |
| 936 | verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 937 | " \\\n" |
| 938 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); |
| 939 | } |
| 940 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 941 | TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 942 | verifyFormat("virtual void write(ELFWriter *writerrr,\n" |
| 943 | " OwningPtr<FileOutputBuffer> &buffer) = 0;"); |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Manuel Klimek | bd04f2a | 2013-01-31 15:58:48 +0000 | [diff] [blame] | 946 | TEST_F(FormatTest, LayoutUnknownPPDirective) { |
| 947 | EXPECT_EQ("#123 \"A string literal\"", |
| 948 | format(" # 123 \"A string literal\"")); |
| 949 | EXPECT_EQ("#;", format("#;")); |
Manuel Klimek | dd5b101 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 950 | verifyFormat("#\n;\n;\n;"); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { |
| 954 | EXPECT_EQ("#line 42 \"test\"\n", |
| 955 | format("# \\\n line \\\n 42 \\\n \"test\"\n")); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 956 | EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", |
| 957 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | TEST_F(FormatTest, EndOfFileEndsPPDirective) { |
| 961 | EXPECT_EQ("#line 42 \"test\"", |
| 962 | format("# \\\n line \\\n 42 \\\n \"test\"")); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 963 | EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 966 | TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 967 | verifyFormat("#define A(BB)", getLLVMStyleWithColumns(13)); |
| 968 | verifyFormat("#define A( \\\n BB)", getLLVMStyleWithColumns(12)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 969 | verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 970 | // FIXME: We never break before the macro name. |
| 971 | verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12)); |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 972 | |
| 973 | verifyFormat("#define A A\n#define A A"); |
| 974 | verifyFormat("#define A(X) A\n#define A A"); |
| 975 | |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 976 | verifyFormat("#define Something Other", getLLVMStyleWithColumns(23)); |
| 977 | verifyFormat("#define Something \\\n Other", getLLVMStyleWithColumns(22)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 981 | EXPECT_EQ("// some comment\n" |
| 982 | "#include \"a.h\"\n" |
Daniel Jasper | a4d4621 | 2013-02-28 11:05:57 +0000 | [diff] [blame] | 983 | "#define A( \\\n" |
| 984 | " A, B)\n" |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 985 | "#include \"b.h\"\n" |
| 986 | "// some comment\n", |
| 987 | format(" // some comment\n" |
| 988 | " #include \"a.h\"\n" |
| 989 | "#define A(A,\\\n" |
| 990 | " B)\n" |
| 991 | " #include \"b.h\"\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 992 | " // some comment\n", |
| 993 | getLLVMStyleWithColumns(13))); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 996 | TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 997 | |
| 998 | TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { |
| 999 | EXPECT_EQ("#define A \\\n" |
| 1000 | " c; \\\n" |
| 1001 | " e;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1002 | "f;", |
| 1003 | format("#define A c; e;\n" |
| 1004 | "f;", |
| 1005 | getLLVMStyleWithColumns(14))); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1008 | TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1009 | |
| 1010 | TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) { |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 1011 | EXPECT_EQ("# define A\\\n b;", |
| 1012 | format("# define A b;", 11, 2, getLLVMStyleWithColumns(11))); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | TEST_F(FormatTest, MacroDefinitionInsideStatement) { |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1016 | EXPECT_EQ("int x,\n" |
| 1017 | "#define A\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1018 | " y;", |
| 1019 | format("int x,\n#define A\ny;")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 1022 | TEST_F(FormatTest, HashInMacroDefinition) { |
| 1023 | verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); |
| 1024 | verifyFormat("#define A \\\n" |
| 1025 | " { \\\n" |
| 1026 | " f(#c);\\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1027 | " }", |
| 1028 | getLLVMStyleWithColumns(11)); |
Daniel Jasper | 765561f | 2013-01-08 16:17:54 +0000 | [diff] [blame] | 1029 | |
| 1030 | verifyFormat("#define A(X) \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1031 | " void function##X()", |
| 1032 | getLLVMStyleWithColumns(22)); |
Daniel Jasper | 765561f | 2013-01-08 16:17:54 +0000 | [diff] [blame] | 1033 | |
| 1034 | verifyFormat("#define A(a, b, c) \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1035 | " void a##b##c()", |
| 1036 | getLLVMStyleWithColumns(22)); |
Daniel Jasper | 765561f | 2013-01-08 16:17:54 +0000 | [diff] [blame] | 1037 | |
Daniel Jasper | e0b15ea | 2013-01-14 15:40:57 +0000 | [diff] [blame] | 1038 | verifyFormat("#define A void # ## #", getLLVMStyleWithColumns(22)); |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Manuel Klimek | 7ccbc21 | 2013-01-23 14:37:36 +0000 | [diff] [blame] | 1041 | TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { |
| 1042 | verifyFormat("#define A (1)"); |
| 1043 | } |
| 1044 | |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 1045 | TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { |
| 1046 | EXPECT_EQ("#define A b;", format("#define A \\\n" |
| 1047 | " \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1048 | " b;", |
| 1049 | getLLVMStyleWithColumns(25))); |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 1050 | EXPECT_EQ("#define A \\\n" |
| 1051 | " \\\n" |
| 1052 | " a; \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1053 | " b;", |
| 1054 | format("#define A \\\n" |
| 1055 | " \\\n" |
| 1056 | " a; \\\n" |
| 1057 | " b;", |
| 1058 | getLLVMStyleWithColumns(11))); |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 1059 | EXPECT_EQ("#define A \\\n" |
| 1060 | " a; \\\n" |
| 1061 | " \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1062 | " b;", |
| 1063 | format("#define A \\\n" |
| 1064 | " a; \\\n" |
| 1065 | " \\\n" |
| 1066 | " b;", |
| 1067 | getLLVMStyleWithColumns(11))); |
Manuel Klimek | a28fc06 | 2013-02-11 12:33:24 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Daniel Jasper | 34511fb | 2013-02-19 17:14:38 +0000 | [diff] [blame] | 1070 | TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { |
| 1071 | // FIXME: Improve formatting of case labels in macros. |
| 1072 | verifyFormat("#define SOMECASES \\\n" |
| 1073 | "case 1: \\\n" |
| 1074 | " case 2\n", |
| 1075 | getLLVMStyleWithColumns(20)); |
| 1076 | |
| 1077 | verifyFormat("#define A template <typename T>"); |
Daniel Jasper | 03628b8 | 2013-02-19 20:05:41 +0000 | [diff] [blame] | 1078 | verifyFormat("#define STR(x) #x\n" |
| 1079 | "f(STR(this_is_a_string_literal{));"); |
Daniel Jasper | 34511fb | 2013-02-19 17:14:38 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 1082 | TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { |
| 1083 | EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); |
| 1084 | } |
| 1085 | |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 1086 | TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1087 | verifyFormat("{\n { a #c; }\n}"); |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 1090 | TEST_F(FormatTest, FormatUnbalancedStructuralElements) { |
| 1091 | EXPECT_EQ("#define A \\\n { \\\n {\nint i;", |
| 1092 | format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); |
| 1093 | EXPECT_EQ("#define A \\\n } \\\n }\nint i;", |
| 1094 | format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); |
| 1095 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1096 | |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1097 | TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) { |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1098 | EXPECT_EQ( |
| 1099 | "#define A \\\n int i; \\\n int j;", |
| 1100 | format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11))); |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 1103 | TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { |
| 1104 | verifyFormat("#define A \\\n" |
| 1105 | " int v( \\\n" |
| 1106 | " a); \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1107 | " int i;", |
| 1108 | getLLVMStyleWithColumns(11)); |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 1111 | TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 1112 | EXPECT_EQ( |
| 1113 | "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 1114 | " \\\n" |
| 1115 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 1116 | "\n" |
| 1117 | "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 1118 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", |
| 1119 | format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 1120 | "\\\n" |
| 1121 | "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 1122 | " \n" |
| 1123 | " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 1124 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1127 | TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { |
| 1128 | EXPECT_EQ("int\n" |
| 1129 | "#define A\n" |
| 1130 | " a;", |
| 1131 | format("int\n#define A\na;")); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1132 | verifyFormat("functionCallTo(\n" |
| 1133 | " someOtherFunction(\n" |
| 1134 | " withSomeParameters, whichInSequence,\n" |
| 1135 | " areLongerThanALine(andAnotherCall,\n" |
| 1136 | "#define A B\n" |
| 1137 | " withMoreParamters,\n" |
| 1138 | " whichStronglyInfluenceTheLayout),\n" |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 1139 | " andMoreParameters),\n" |
| 1140 | " trailing);", |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1141 | getLLVMStyleWithColumns(69)); |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1144 | TEST_F(FormatTest, LayoutBlockInsideParens) { |
| 1145 | EXPECT_EQ("functionCall({\n" |
| 1146 | " int i;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1147 | "});", |
| 1148 | format(" functionCall ( {int i;} );")); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | TEST_F(FormatTest, LayoutBlockInsideStatement) { |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 1152 | EXPECT_EQ("SOME_MACRO { int i; }\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1153 | "int i;", |
| 1154 | format(" SOME_MACRO {int i;} int i;")); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | TEST_F(FormatTest, LayoutNestedBlocks) { |
| 1158 | verifyFormat("void AddOsStrings(unsigned bitmask) {\n" |
| 1159 | " struct s {\n" |
| 1160 | " int i;\n" |
| 1161 | " };\n" |
| 1162 | " s kBitsToOs[] = { { 10 } };\n" |
| 1163 | " for (int i = 0; i < 10; ++i)\n" |
| 1164 | " return;\n" |
| 1165 | "}"); |
| 1166 | } |
| 1167 | |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1168 | TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { |
| 1169 | EXPECT_EQ("{}", format("{}")); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1170 | |
| 1171 | // Negative test for enum. |
| 1172 | verifyFormat("enum E {\n};"); |
| 1173 | |
| 1174 | // Note that when there's a missing ';', we still join... |
| 1175 | verifyFormat("enum E {}"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1178 | //===----------------------------------------------------------------------===// |
| 1179 | // Line break tests. |
| 1180 | //===----------------------------------------------------------------------===// |
| 1181 | |
| 1182 | TEST_F(FormatTest, FormatsFunctionDefinition) { |
| 1183 | verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," |
| 1184 | " int h, int j, int f,\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1185 | " int c, int ddddddddddddd) {}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | TEST_F(FormatTest, FormatsAwesomeMethodCall) { |
| 1189 | verifyFormat( |
Daniel Jasper | 986e17f | 2013-01-28 07:35:34 +0000 | [diff] [blame] | 1190 | "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" |
| 1191 | " parameter, parameter, parameter)),\n" |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 1192 | " SecondLongCall(parameter));"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Daniel Jasper | ae8699b | 2013-01-28 09:35:24 +0000 | [diff] [blame] | 1195 | TEST_F(FormatTest, PreventConfusingIndents) { |
Daniel Jasper | 986e17f | 2013-01-28 07:35:34 +0000 | [diff] [blame] | 1196 | verifyFormat( |
| 1197 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1198 | " aaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1199 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 1200 | " aaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1201 | verifyFormat( |
| 1202 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[\n" |
| 1203 | " aaaaaaaaaaaaaaaaaaaaaaaa[\n" |
| 1204 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa],\n" |
| 1205 | " aaaaaaaaaaaaaaaaaaaaaaaa];"); |
| 1206 | verifyFormat( |
| 1207 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa<\n" |
| 1208 | " aaaaaaaaaaaaaaaaaaaaaaaa<\n" |
| 1209 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>,\n" |
| 1210 | " aaaaaaaaaaaaaaaaaaaaaaaa>;"); |
Daniel Jasper | ae8699b | 2013-01-28 09:35:24 +0000 | [diff] [blame] | 1211 | verifyFormat("int a = bbbb && ccc && fffff(\n" |
| 1212 | "#define A Just forcing a new line\n" |
| 1213 | " ddd);"); |
Daniel Jasper | 986e17f | 2013-01-28 07:35:34 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1216 | TEST_F(FormatTest, ConstructorInitializers) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1217 | verifyFormat("Constructor() : Initializer(FitsOnTheLine) {}"); |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 1218 | verifyFormat("Constructor() : Inttializer(FitsOnTheLine) {}", |
| 1219 | getLLVMStyleWithColumns(45)); |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1220 | verifyFormat("Constructor()\n" |
| 1221 | " : Inttializer(FitsOnTheLine) {}", |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 1222 | getLLVMStyleWithColumns(44)); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1223 | verifyFormat("Constructor()\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1224 | " : Inttializer(FitsOnTheLine) {}", |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1225 | getLLVMStyleWithColumns(43)); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1226 | |
| 1227 | verifyFormat( |
| 1228 | "SomeClass::Constructor()\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1229 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1230 | |
| 1231 | verifyFormat( |
| 1232 | "SomeClass::Constructor()\n" |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 1233 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1234 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}"); |
Daniel Jasper | 7e9bf8c | 2013-01-11 11:37:55 +0000 | [diff] [blame] | 1235 | verifyFormat( |
| 1236 | "SomeClass::Constructor()\n" |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1237 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1238 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1239 | |
| 1240 | verifyFormat("Constructor()\n" |
| 1241 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 1242 | " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1243 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1244 | " aaaaaaaaaaaaaaaaaaaaaaa() {}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1245 | |
Daniel Jasper | 02b771e | 2013-01-28 13:31:35 +0000 | [diff] [blame] | 1246 | verifyFormat("Constructor()\n" |
| 1247 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1248 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
Daniel Jasper | 02b771e | 2013-01-28 13:31:35 +0000 | [diff] [blame] | 1249 | |
Daniel Jasper | 9c3e71a | 2013-02-25 15:59:54 +0000 | [diff] [blame] | 1250 | verifyFormat("Constructor(int Parameter = 0)\n" |
| 1251 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa),\n" |
| 1252 | " aaaaaaaaaaaa(aaaaaaaaaaaaaaaaa) {}"); |
| 1253 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1254 | // Here a line could be saved by splitting the second initializer onto two |
| 1255 | // lines, but that is not desireable. |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1256 | verifyFormat("Constructor()\n" |
| 1257 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 1258 | " aaaaaaaaaaa(aaaaaaaaaaa),\n" |
| 1259 | " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1260 | |
Daniel Jasper | b130a54 | 2013-02-15 16:49:44 +0000 | [diff] [blame] | 1261 | FormatStyle OnePerLine = getLLVMStyle(); |
| 1262 | OnePerLine.ConstructorInitializerAllOnOneLineOrOnePerLine = true; |
| 1263 | verifyFormat("SomeClass::Constructor()\n" |
| 1264 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
| 1265 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1266 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1267 | OnePerLine); |
Daniel Jasper | b130a54 | 2013-02-15 16:49:44 +0000 | [diff] [blame] | 1268 | verifyFormat("SomeClass::Constructor()\n" |
| 1269 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), // Some comment\n" |
| 1270 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1271 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}", |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1272 | OnePerLine); |
Daniel Jasper | b130a54 | 2013-02-15 16:49:44 +0000 | [diff] [blame] | 1273 | verifyFormat("MyClass::MyClass(int var)\n" |
| 1274 | " : some_var_(var), // 4 space indent\n" |
| 1275 | " some_other_var_(var + 1) { // lined up\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1276 | "}", |
| 1277 | OnePerLine); |
Daniel Jasper | 3a39ac7 | 2013-02-28 09:39:12 +0000 | [diff] [blame] | 1278 | verifyFormat("Constructor()\n" |
| 1279 | " : aaaaa(aaaaaa),\n" |
| 1280 | " aaaaa(aaaaaa),\n" |
| 1281 | " aaaaa(aaaaaa),\n" |
| 1282 | " aaaaa(aaaaaa),\n" |
| 1283 | " aaaaa(aaaaaa) {}", |
| 1284 | OnePerLine); |
Daniel Jasper | b312314 | 2013-01-12 07:36:22 +0000 | [diff] [blame] | 1285 | |
| 1286 | // This test takes VERY long when memoization is broken. |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1287 | OnePerLine.BinPackParameters = false; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1288 | std::string input = "Constructor()\n" |
Daniel Jasper | 3499dda | 2013-01-25 15:43:32 +0000 | [diff] [blame] | 1289 | " : aaaa(a,\n"; |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1290 | for (unsigned i = 0, e = 80; i != e; ++i) { |
| 1291 | input += " a,\n"; |
| 1292 | } |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1293 | input += " a) {}"; |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1294 | verifyFormat(input, OnePerLine); |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1297 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
| 1298 | verifyFormat( |
Alexander Kornienko | 3fd9ccd | 2013-03-12 16:28:18 +0000 | [diff] [blame] | 1299 | "void f() {\n" |
| 1300 | " if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
| 1301 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
| 1302 | " f();\n" |
| 1303 | "}"); |
Daniel Jasper | 518ee34 | 2013-02-26 13:59:14 +0000 | [diff] [blame] | 1304 | verifyFormat("if (Intervals[i].getRange().getFirst() <\n" |
| 1305 | " Intervals[i - 1].getRange().getLast()) {\n}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1308 | TEST_F(FormatTest, BreaksDesireably) { |
| 1309 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 1310 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1311 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n}"); |
Daniel Jasper | e438bac | 2013-01-23 20:41:06 +0000 | [diff] [blame] | 1312 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1313 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)) {\n" |
| 1314 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1315 | |
| 1316 | verifyFormat( |
| 1317 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 1318 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1319 | |
| 1320 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1321 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1322 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 1323 | |
| 1324 | verifyFormat( |
| 1325 | "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1326 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 1327 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1328 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 1329 | |
| 1330 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 1331 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1332 | |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 1333 | verifyFormat( |
Alexander Kornienko | 3fd9ccd | 2013-03-12 16:28:18 +0000 | [diff] [blame] | 1334 | "void f() {\n" |
| 1335 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" |
| 1336 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n" |
| 1337 | "}"); |
Daniel Jasper | 3499dda | 2013-01-25 15:43:32 +0000 | [diff] [blame] | 1338 | verifyFormat( |
| 1339 | "aaaaaa(new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1340 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
| 1341 | verifyFormat( |
| 1342 | "aaaaaa(aaa, new Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1343 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 1344 | |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 1345 | // This test case breaks on an incorrect memoization, i.e. an optimization not |
| 1346 | // taking into account the StopAt value. |
| 1347 | verifyFormat( |
| 1348 | "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1349 | " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 1350 | " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 1351 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 1352 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1353 | verifyFormat("{\n {\n {\n" |
| 1354 | " Annotation.SpaceRequiredBefore =\n" |
| 1355 | " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" |
| 1356 | " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" |
| 1357 | " }\n }\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1360 | TEST_F(FormatTest, FormatsOneParameterPerLineIfNecessary) { |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1361 | FormatStyle NoBinPacking = getLLVMStyle(); |
| 1362 | NoBinPacking.BinPackParameters = false; |
| 1363 | verifyFormat("f(aaaaaaaaaaaaaaaaaaaa,\n" |
| 1364 | " aaaaaaaaaaaaaaaaaaaa,\n" |
| 1365 | " aaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaa);", |
| 1366 | NoBinPacking); |
| 1367 | verifyFormat("aaaaaaa(aaaaaaaaaaaaa,\n" |
| 1368 | " aaaaaaaaaaaaa,\n" |
| 1369 | " aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));", |
| 1370 | NoBinPacking); |
| 1371 | verifyFormat( |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1372 | "aaaaaaaa(aaaaaaaaaaaaa,\n" |
| 1373 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1374 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 1375 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1376 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));", |
| 1377 | NoBinPacking); |
| 1378 | verifyFormat("aaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" |
| 1379 | " .aaaaaaaaaaaaaaaaaa();", |
| 1380 | NoBinPacking); |
| 1381 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1382 | " aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaa, aaaaaaaaaaa);", |
| 1383 | NoBinPacking); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1384 | |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1385 | verifyFormat( |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 1386 | "aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1387 | " aaaaaaaaaaaa,\n" |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1388 | " aaaaaaaaaaaa);", |
| 1389 | NoBinPacking); |
| 1390 | verifyFormat( |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1391 | "somefunction(someotherFunction(ddddddddddddddddddddddddddddddddddd,\n" |
| 1392 | " ddddddddddddddddddddddddddddd),\n" |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1393 | " test);", |
| 1394 | NoBinPacking); |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1395 | |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1396 | verifyFormat("std::vector<aaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1397 | " aaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1398 | " aaaaaaaaaaaaaaaaaaaaaaa> aaaaaaaaaaaaaaaaaa;", |
| 1399 | NoBinPacking); |
| 1400 | verifyFormat("a(\"a\"\n" |
| 1401 | " \"a\",\n" |
| 1402 | " a);"); |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 1403 | |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1404 | NoBinPacking.AllowAllParametersOfDeclarationOnNextLine = false; |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 1405 | verifyFormat("void aaaaaaaaaa(aaaaaaaaa,\n" |
Daniel Jasper | 8f4bd7a | 2013-01-23 10:08:28 +0000 | [diff] [blame] | 1406 | " aaaaaaaaa,\n" |
Daniel Jasper | f157960 | 2013-01-29 16:03:49 +0000 | [diff] [blame] | 1407 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1408 | NoBinPacking); |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 1409 | verifyFormat( |
| 1410 | "void f() {\n" |
| 1411 | " aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaa, aaaaaaaaa, aaaaaaaaaaaaaaaaaaaaa)\n" |
| 1412 | " .aaaaaaa();\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1413 | "}", |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1414 | NoBinPacking); |
Daniel Jasper | 0df6acd | 2013-01-16 14:59:02 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 1417 | TEST_F(FormatTest, FormatsBuilderPattern) { |
| 1418 | verifyFormat( |
| 1419 | "return llvm::StringSwitch<Reference::Kind>(name)\n" |
Daniel Jasper | f39c885 | 2013-01-23 16:58:21 +0000 | [diff] [blame] | 1420 | " .StartsWith(\".eh_frame_hdr\", ORDER_EH_FRAMEHDR)\n" |
| 1421 | " .StartsWith(\".eh_frame\", ORDER_EH_FRAME).StartsWith(\".init\", ORDER_INIT)\n" |
| 1422 | " .StartsWith(\".fini\", ORDER_FINI).StartsWith(\".hash\", ORDER_HASH)\n" |
| 1423 | " .Default(ORDER_TEXT);\n"); |
Daniel Jasper | 4a544e5 | 2013-02-15 20:33:06 +0000 | [diff] [blame] | 1424 | |
| 1425 | verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" |
| 1426 | " aaaaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); |
Daniel Jasper | 515f65d | 2013-02-18 13:24:21 +0000 | [diff] [blame] | 1427 | verifyFormat( |
| 1428 | "aaaaaaa->aaaaaaa\n" |
| 1429 | " ->aaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 1430 | " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); |
Daniel Jasper | 82282dc | 2013-02-18 13:52:06 +0000 | [diff] [blame] | 1431 | verifyFormat( |
| 1432 | "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" |
Daniel Jasper | 2484971 | 2013-03-01 16:48:32 +0000 | [diff] [blame] | 1433 | " aaaaaaaaaaaaaa);"); |
Daniel Jasper | 82282dc | 2013-02-18 13:52:06 +0000 | [diff] [blame] | 1434 | verifyFormat( |
| 1435 | "aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa = aaaaaa->aaaaaaaaaaaa()\n" |
| 1436 | " ->aaaaaaaaaaaaaaaa(\n" |
| 1437 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 1438 | " ->aaaaaaaaaaaaaaaaa();"); |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1441 | TEST_F(FormatTest, DoesNotBreakTrailingAnnotation) { |
| 1442 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
Daniel Jasper | 8ef19a2 | 2013-03-14 09:50:46 +0000 | [diff] [blame] | 1443 | " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); |
Daniel Jasper | 60ca75d | 2013-01-17 13:31:52 +0000 | [diff] [blame] | 1444 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" |
Daniel Jasper | 8ef19a2 | 2013-03-14 09:50:46 +0000 | [diff] [blame] | 1445 | " LOCKS_EXCLUDED(aaaaaaaaaaaaa);"); |
Daniel Jasper | 60ca75d | 2013-01-17 13:31:52 +0000 | [diff] [blame] | 1446 | verifyFormat("void aaaaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) const\n" |
Daniel Jasper | 8ef19a2 | 2013-03-14 09:50:46 +0000 | [diff] [blame] | 1447 | " LOCKS_EXCLUDED(aaaaaaaaaaaaa) {}"); |
Daniel Jasper | 8ed4100 | 2013-02-28 14:44:25 +0000 | [diff] [blame] | 1448 | verifyFormat( |
| 1449 | "void aaaaaaaaaaaaaaaaaa()\n" |
| 1450 | " __attribute__((aaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1451 | " aaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | 8ef19a2 | 2013-03-14 09:50:46 +0000 | [diff] [blame] | 1452 | verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1453 | " __attribute__((unused));"); |
| 1454 | |
| 1455 | // FIXME: This is bad indentation, but generally hard to distinguish from a |
| 1456 | // function declaration. |
| 1457 | verifyFormat( |
| 1458 | "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1459 | "GUARDED_BY(aaaaaaaaaaaa);"); |
Daniel Jasper | 7d19bc2 | 2013-01-11 14:23:32 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 1462 | TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { |
| 1463 | verifyFormat( |
| 1464 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1465 | " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1466 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1467 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 1468 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1469 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 1470 | verifyFormat( |
| 1471 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1472 | " ccccccccccccccccccccccccc) {\n}"); |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 1473 | verifyFormat("return aaaa & AAAAAAAAAAAAAAAAAAAAAAAAAAAAA ||\n" |
| 1474 | " bbbb & BBBBBBBBBBBBBBBBBBBBBBBBBBBBB ||\n" |
| 1475 | " cccc & CCCCCCCCCCCCCCCCCCCCCCCCCC ||\n" |
| 1476 | " dddd & DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD;"); |
| 1477 | verifyFormat("if ((aaaaaaaaaa != aaaaaaaaaaaaaaa ||\n" |
| 1478 | " aaaaaaaaaaaaaaaaaaaaaaaa() >= aaaaaaaaaaaaaaaaaaaa) &&\n" |
| 1479 | " aaaaaaaaaaaaaaa != aa) {\n}"); |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Daniel Jasper | 7006e7e | 2013-01-23 12:27:43 +0000 | [diff] [blame] | 1482 | TEST_F(FormatTest, BreaksAfterAssignments) { |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1483 | verifyFormat( |
Daniel Jasper | 7006e7e | 2013-01-23 12:27:43 +0000 | [diff] [blame] | 1484 | "unsigned Cost =\n" |
| 1485 | " TTI.getMemoryOpCost(I->getOpcode(), VectorTy, SI->getAlignment(),\n" |
| 1486 | " SI->getPointerAddressSpaceee());\n"); |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1487 | verifyFormat( |
Daniel Jasper | 13cb7c2 | 2013-01-23 15:55:19 +0000 | [diff] [blame] | 1488 | "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" |
| 1489 | " Line.Tokens.front().Tok.getLo(), Line.Tokens.back().Tok.getLoc());"); |
Daniel Jasper | f39c885 | 2013-01-23 16:58:21 +0000 | [diff] [blame] | 1490 | |
| 1491 | verifyFormat( |
| 1492 | "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaa = aaaaaaaaaaaaaa(0).aaaa()\n" |
| 1493 | " .aaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1496 | TEST_F(FormatTest, AlignsAfterAssignments) { |
| 1497 | verifyFormat( |
| 1498 | "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1499 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1500 | verifyFormat( |
| 1501 | "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1502 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1503 | verifyFormat( |
| 1504 | "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1505 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1506 | verifyFormat( |
| 1507 | "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame] | 1508 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1509 | verifyFormat("double LooooooooooooooooooooooooongResult =\n" |
| 1510 | " aaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 1511 | " aaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | TEST_F(FormatTest, AlignsAfterReturn) { |
| 1515 | verifyFormat( |
| 1516 | "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 1517 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1518 | verifyFormat( |
| 1519 | "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 1520 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1521 | } |
| 1522 | |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 1523 | TEST_F(FormatTest, BreaksConditionalExpressions) { |
| 1524 | verifyFormat( |
| 1525 | "aaaa(aaaaaaaaaaaaaaaaaaaa,\n" |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 1526 | " aaaaaaaaaaaaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1527 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 1f2b078 | 2013-01-16 16:23:19 +0000 | [diff] [blame] | 1528 | verifyFormat( |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 1529 | "aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1530 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1531 | verifyFormat( |
| 1532 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaa ? aaaa(aaaaaa)\n" |
| 1533 | " : aaaaaaaaaaaaa);"); |
| 1534 | verifyFormat( |
| 1535 | "aaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1536 | " aaaaaaaaaaaaaaaa ? aaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1537 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1538 | " aaaaaaaaaaaaa);"); |
| 1539 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1540 | " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1541 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 1542 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1543 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1544 | verifyFormat("aaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1545 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1546 | " ? aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1547 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 1548 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1549 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 1550 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 7878a7b | 2013-02-15 11:07:25 +0000 | [diff] [blame] | 1551 | |
| 1552 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1553 | " ? aaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1554 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 1555 | verifyFormat( |
| 1556 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1557 | " ? aaaaaaaaaaaaaaa\n" |
| 1558 | " : aaaaaaaaaaaaaaa;"); |
| 1559 | verifyFormat("f(aaaaaaaaaaaaaaaa == // force break\n" |
| 1560 | " aaaaaaaaa\n" |
| 1561 | " ? b\n" |
| 1562 | " : c);"); |
Daniel Jasper | 087387a | 2013-02-08 16:49:27 +0000 | [diff] [blame] | 1563 | verifyFormat( |
Daniel Jasper | 7878a7b | 2013-02-15 11:07:25 +0000 | [diff] [blame] | 1564 | "unsigned Indent =\n" |
| 1565 | " format(TheLine.First, IndentForLevel[TheLine.Level] >= 0\n" |
| 1566 | " ? IndentForLevel[TheLine.Level]\n" |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 1567 | " : TheLine * 2,\n" |
| 1568 | " TheLine.InPPDirective, PreviousEndOfLineColumn);", |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1569 | getLLVMStyleWithColumns(70)); |
Daniel Jasper | 7878a7b | 2013-02-15 11:07:25 +0000 | [diff] [blame] | 1570 | |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1571 | FormatStyle NoBinPacking = getLLVMStyle(); |
| 1572 | NoBinPacking.BinPackParameters = false; |
| 1573 | verifyFormat( |
Daniel Jasper | 237d4c1 | 2013-02-23 21:01:55 +0000 | [diff] [blame] | 1574 | "void f() {\n" |
| 1575 | " g(aaa,\n" |
| 1576 | " aaaaaaaaaa == aaaaaaaaaa ? aaaa : aaaaa,\n" |
| 1577 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa == aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1578 | " ? aaaaaaaaaaaaaaa\n" |
| 1579 | " : aaaaaaaaaaaaaaa);\n" |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1580 | "}", |
| 1581 | NoBinPacking); |
Daniel Jasper | 9c837d0 | 2013-01-09 07:06:56 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 1584 | TEST_F(FormatTest, DeclarationsOfMultipleVariables) { |
| 1585 | verifyFormat("bool aaaaaaaaaaaaaaaaa = aaaaaa->aaaaaaaaaaaaaaaaa(),\n" |
| 1586 | " aaaaaaaaaaa = aaaaaa->aaaaaaaaaaa();"); |
| 1587 | verifyFormat("bool a = true, b = false;"); |
| 1588 | |
| 1589 | // FIXME: Indentation looks weird. |
| 1590 | verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 1591 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaa),\n" |
| 1592 | " bbbbbbbbbbbbbbbbbbbbbbbbb =\n" |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 1593 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(bbbbbbbbbbbbbbbb);"); |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 1594 | verifyFormat( |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 1595 | "bool aaaaaaaaaaaaaaaaaaaaa =\n" |
| 1596 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && cccccccccccccccccccccccccccc,\n" |
| 1597 | " d = e && f;"); |
Daniel Jasper | 2e60377 | 2013-01-29 11:21:01 +0000 | [diff] [blame] | 1598 | |
| 1599 | } |
| 1600 | |
Nico Weber | 7d37b8b | 2013-01-12 01:28:06 +0000 | [diff] [blame] | 1601 | TEST_F(FormatTest, ConditionalExpressionsInBrackets) { |
| 1602 | verifyFormat("arr[foo ? bar : baz];"); |
| 1603 | verifyFormat("f()[foo ? bar : baz];"); |
| 1604 | verifyFormat("(a + b)[foo ? bar : baz];"); |
| 1605 | verifyFormat("arr[foo ? (4 > 5 ? 4 : 5) : 5 < 5 ? 5 : 7];"); |
| 1606 | } |
| 1607 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1608 | TEST_F(FormatTest, AlignsStringLiterals) { |
| 1609 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
| 1610 | " \"short literal\");"); |
| 1611 | verifyFormat( |
| 1612 | "looooooooooooooooooooooooongFunction(\n" |
| 1613 | " \"short literal\"\n" |
| 1614 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1615 | verifyFormat("someFunction(\"Always break between multi-line\"\n" |
| 1616 | " \" string literals\",\n" |
| 1617 | " and, other, parameters);"); |
Manuel Klimek | b56b6d1 | 2013-02-20 15:25:48 +0000 | [diff] [blame] | 1618 | EXPECT_EQ("fun + \"1243\" /* comment */\n" |
| 1619 | " \"5678\";", |
| 1620 | format("fun + \"1243\" /* comment */\n" |
| 1621 | " \"5678\";", |
| 1622 | getLLVMStyleWithColumns(28))); |
| 1623 | EXPECT_EQ( |
| 1624 | "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" |
| 1625 | " \"aaaaaaaaaaaaaaaaaaaaa\"\n" |
| 1626 | " \"aaaaaaaaaaaaaaaa\";", |
| 1627 | format("aaaaaa =" |
| 1628 | "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " |
| 1629 | "aaaaaaaaaaaaaaaaaaaaa\" " |
| 1630 | "\"aaaaaaaaaaaaaaaa\";")); |
Manuel Klimek | e9a6226 | 2013-02-20 15:32:58 +0000 | [diff] [blame] | 1631 | verifyFormat("a = a + \"a\"\n" |
| 1632 | " \"a\"\n" |
| 1633 | " \"a\";"); |
Daniel Jasper | 3a20441 | 2013-02-23 07:46:38 +0000 | [diff] [blame] | 1634 | |
| 1635 | verifyFormat( |
| 1636 | "#define LL_FORMAT \"ll\"\n" |
| 1637 | "printf(\"aaaaa: %d, bbbbbb: %\" LL_FORMAT \"d, cccccccc: %\" LL_FORMAT\n" |
| 1638 | " \"d, ddddddddd: %\" LL_FORMAT \"d\");"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1641 | TEST_F(FormatTest, AlignsPipes) { |
| 1642 | verifyFormat( |
| 1643 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1644 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1645 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1646 | verifyFormat( |
| 1647 | "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" |
| 1648 | " << aaaaaaaaaaaaaaaaaaaa;"); |
| 1649 | verifyFormat( |
| 1650 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1651 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1652 | verifyFormat( |
| 1653 | "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 1654 | " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" |
| 1655 | " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); |
| 1656 | verifyFormat( |
| 1657 | "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 1658 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 1659 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | fa543ac | 2013-02-04 07:34:48 +0000 | [diff] [blame] | 1660 | |
| 1661 | verifyFormat("return out << \"somepacket = {\\n\"\n" |
| 1662 | " << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n" |
| 1663 | " << \" bbbb = \" << pkt.bbbb << \"\\n\"\n" |
| 1664 | " << \" cccccc = \" << pkt.cccccc << \"\\n\"\n" |
| 1665 | " << \" ddd = [\" << pkt.ddd << \"]\\n\"\n" |
| 1666 | " << \"}\";"); |
Daniel Jasper | 4e8a7b4 | 2013-02-06 21:04:05 +0000 | [diff] [blame] | 1667 | |
| 1668 | verifyFormat( |
| 1669 | "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n" |
| 1670 | " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n" |
| 1671 | " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n" |
| 1672 | " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n" |
| 1673 | " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;"); |
Daniel Jasper | bfa1edd | 2013-03-14 14:00:17 +0000 | [diff] [blame] | 1674 | verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n" |
| 1675 | " << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1678 | TEST_F(FormatTest, UnderstandsEquals) { |
| 1679 | verifyFormat( |
| 1680 | "aaaaaaaaaaaaaaaaa =\n" |
| 1681 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 1682 | verifyFormat( |
| 1683 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1684 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1685 | verifyFormat( |
| 1686 | "if (a) {\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1687 | " f();\n" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1688 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1689 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 1690 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1691 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1692 | verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 1693 | " 100000000 + 10000000) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 1696 | TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1697 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 1698 | " .looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 1699 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1700 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 1701 | " ->looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 1702 | |
| 1703 | verifyFormat( |
| 1704 | "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" |
| 1705 | " Parameter2);"); |
| 1706 | |
| 1707 | verifyFormat( |
| 1708 | "ShortObject->shortFunction(\n" |
| 1709 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" |
| 1710 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); |
| 1711 | |
| 1712 | verifyFormat("loooooooooooooongFunction(\n" |
| 1713 | " LoooooooooooooongObject->looooooooooooooooongFunction());"); |
| 1714 | |
| 1715 | verifyFormat( |
| 1716 | "function(LoooooooooooooooooooooooooooooooooooongObject\n" |
| 1717 | " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); |
| 1718 | |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 1719 | verifyFormat("EXPECT_CALL(SomeObject, SomeFunction(Parameter))\n" |
| 1720 | " .WillRepeatedly(Return(SomeValue));"); |
| 1721 | verifyFormat("SomeMap[std::pair(aaaaaaaaaaaa, bbbbbbbbbbbbbbb)]\n" |
| 1722 | " .insert(ccccccccccccccccccccccc);"); |
Daniel Jasper | 3af59ce | 2013-03-15 14:57:30 +0000 | [diff] [blame] | 1723 | verifyFormat( |
| 1724 | "aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1725 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 1726 | " .aaaaaaaaaaaaaaa(\n" |
| 1727 | " aa(aaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1728 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | fc75908 | 2013-02-14 14:26:07 +0000 | [diff] [blame] | 1729 | |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 1730 | // Here, it is not necessary to wrap at "." or "->". |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 1731 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1732 | " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 1733 | verifyFormat( |
| 1734 | "aaaaaaaaaaa->aaaaaaaaa(\n" |
| 1735 | " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1736 | " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); |
Daniel Jasper | 9fc56f2 | 2013-02-14 15:01:34 +0000 | [diff] [blame] | 1737 | |
| 1738 | verifyFormat( |
| 1739 | "aaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1740 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa().aaaaaaaaaaaaaaaaa());"); |
Daniel Jasper | 8d0d19a | 2013-03-13 15:37:48 +0000 | [diff] [blame] | 1741 | verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() *\n" |
| 1742 | " aaaaaaaaa()->aaaaaa()->aaaaa());"); |
| 1743 | verifyFormat("a->aaaaaa()->aaaaaaaaaaa(aaaaaaaa()->aaaaaa()->aaaaa() ||\n" |
| 1744 | " aaaaaaaaa()->aaaaaa()->aaaaa());"); |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1745 | |
Daniel Jasper | 3af59ce | 2013-03-15 14:57:30 +0000 | [diff] [blame] | 1746 | // FIXME: Should we break before .a()? |
| 1747 | verifyFormat("aaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1748 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa).a();"); |
| 1749 | |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 1750 | FormatStyle NoBinPacking = getLLVMStyle(); |
| 1751 | NoBinPacking.BinPackParameters = false; |
| 1752 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" |
| 1753 | " .aaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" |
| 1754 | " .aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaa,\n" |
| 1755 | " aaaaaaaaaaaaaaaaaaa,\n" |
| 1756 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", |
| 1757 | NoBinPacking); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 1760 | TEST_F(FormatTest, WrapsTemplateDeclarations) { |
| 1761 | verifyFormat("template <typename T>\n" |
| 1762 | "virtual void loooooooooooongFunction(int Param1, int Param2);"); |
| 1763 | verifyFormat( |
Daniel Jasper | c79afda | 2013-01-18 10:56:38 +0000 | [diff] [blame] | 1764 | "template <typename T>\n" |
Daniel Jasper | 29f123b | 2013-02-08 15:28:42 +0000 | [diff] [blame] | 1765 | "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;"); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1766 | verifyFormat("template <typename T>\n" |
| 1767 | "void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" |
| 1768 | " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 1769 | verifyFormat( |
| 1770 | "template <typename T>\n" |
| 1771 | "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" |
| 1772 | " int Paaaaaaaaaaaaaaaaaaaaram2);"); |
Daniel Jasper | 5eda31e | 2013-01-02 18:30:06 +0000 | [diff] [blame] | 1773 | verifyFormat( |
| 1774 | "template <typename T>\n" |
| 1775 | "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" |
| 1776 | " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" |
| 1777 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | d64f738 | 2013-01-09 09:50:48 +0000 | [diff] [blame] | 1778 | verifyFormat("template <typename T>\n" |
| 1779 | "void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1780 | " int aaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 3fc0bb7 | 2013-01-09 10:40:23 +0000 | [diff] [blame] | 1781 | verifyFormat( |
| 1782 | "template <typename T1, typename T2 = char, typename T3 = char,\n" |
| 1783 | " typename T4 = char>\n" |
| 1784 | "void f();"); |
Daniel Jasper | 3499dda | 2013-01-25 15:43:32 +0000 | [diff] [blame] | 1785 | verifyFormat( |
| 1786 | "aaaaaaaaaaaaaaaaaaaaaaaa<aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa>(\n" |
| 1787 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 0178673 | 2013-02-04 07:21:18 +0000 | [diff] [blame] | 1788 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 1789 | verifyFormat("a<aaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaa>(\n" |
| 1790 | " a(aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa));"); |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Daniel Jasper | 63f0036 | 2013-01-25 10:57:27 +0000 | [diff] [blame] | 1793 | TEST_F(FormatTest, WrapsAtNestedNameSpecifiers) { |
| 1794 | verifyFormat( |
| 1795 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 1796 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
| 1797 | verifyFormat( |
| 1798 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 1799 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1800 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa());"); |
| 1801 | |
| 1802 | // FIXME: Should we have an extra indent after the second break? |
| 1803 | verifyFormat( |
| 1804 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 1805 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 1806 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); |
| 1807 | |
| 1808 | // FIXME: Look into whether we should indent 4 from the start or 4 from |
| 1809 | // "bbbbb..." here instead of what we are doing now. |
| 1810 | verifyFormat( |
| 1811 | "aaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb::\n" |
Daniel Jasper | 5f2173e | 2013-01-28 07:43:15 +0000 | [diff] [blame] | 1812 | " cccccccccccccccccccccccccccccccccccccccccccccc());"); |
Daniel Jasper | 63f0036 | 2013-01-25 10:57:27 +0000 | [diff] [blame] | 1813 | |
| 1814 | // Breaking at nested name specifiers is generally not desirable. |
| 1815 | verifyFormat( |
| 1816 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1817 | " aaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | bfe6fd4 | 2013-01-28 12:45:14 +0000 | [diff] [blame] | 1818 | |
| 1819 | verifyFormat( |
| 1820 | "aaaaaaaaaaaaaaaaaa(aaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa::\n" |
| 1821 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 1822 | " aaaaaaaaaaaaaaaaaaaaa);", |
| 1823 | getLLVMStyleWithColumns(74)); |
Daniel Jasper | 63f0036 | 2013-01-25 10:57:27 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1826 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
| 1827 | verifyFormat("A<int> a;"); |
| 1828 | verifyFormat("A<A<A<int> > > a;"); |
| 1829 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
| 1830 | verifyFormat("bool x = a < 1 || 2 > a;"); |
| 1831 | verifyFormat("bool x = 5 < f<int>();"); |
| 1832 | verifyFormat("bool x = f<int>() > 5;"); |
| 1833 | verifyFormat("bool x = 5 < a<int>::x;"); |
| 1834 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
| 1835 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
| 1836 | |
| 1837 | verifyGoogleFormat("A<A<int>> a;"); |
| 1838 | verifyGoogleFormat("A<A<A<int>>> a;"); |
| 1839 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 1840 | verifyGoogleFormat("A<A<int> > a;"); |
| 1841 | verifyGoogleFormat("A<A<A<int> > > a;"); |
| 1842 | verifyGoogleFormat("A<A<A<A<int> > > > a;"); |
| 1843 | EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A> >> a;", getGoogleStyle())); |
| 1844 | EXPECT_EQ("A<A<A<A>>> a;", format("A<A<A<A>> > a;", getGoogleStyle())); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1845 | |
| 1846 | verifyFormat("test >> a >> b;"); |
| 1847 | verifyFormat("test << a >> b;"); |
| 1848 | |
| 1849 | verifyFormat("f<int>();"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1850 | verifyFormat("template <typename T> void f() {}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
Daniel Jasper | 807809c | 2013-02-28 09:21:10 +0000 | [diff] [blame] | 1853 | TEST_F(FormatTest, UnderstandsBinaryOperators) { |
| 1854 | verifyFormat("COMPARE(a, ==, b);"); |
Daniel Jasper | 9c3c7b3 | 2013-02-28 13:40:17 +0000 | [diff] [blame] | 1855 | verifyFormat("(a->*f)()"); |
Daniel Jasper | 807809c | 2013-02-28 09:21:10 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1858 | TEST_F(FormatTest, UnderstandsUnaryOperators) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1859 | verifyFormat("int a = -2;"); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 1860 | verifyFormat("f(-1, -2, -3);"); |
| 1861 | verifyFormat("a[-1] = 5;"); |
| 1862 | verifyFormat("int a = 5 + -2;"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1863 | verifyFormat("if (i == -1) {\n}"); |
| 1864 | verifyFormat("if (i != -1) {\n}"); |
| 1865 | verifyFormat("if (i > -1) {\n}"); |
| 1866 | verifyFormat("if (i < -1) {\n}"); |
Daniel Jasper | d56a737 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 1867 | verifyFormat("++(a->f());"); |
| 1868 | verifyFormat("--(a->f());"); |
Daniel Jasper | 4abbb53 | 2013-01-14 12:18:19 +0000 | [diff] [blame] | 1869 | verifyFormat("(a->f())++;"); |
| 1870 | verifyFormat("a[42]++;"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 1871 | verifyFormat("if (!(a->f())) {\n}"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1872 | |
| 1873 | verifyFormat("a-- > b;"); |
| 1874 | verifyFormat("b ? -a : c;"); |
| 1875 | verifyFormat("n * sizeof char16;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 1876 | verifyFormat("n * alignof char16;"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 1877 | verifyFormat("sizeof(char);"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 1878 | verifyFormat("alignof(char);"); |
Daniel Jasper | 1f0754b | 2013-01-02 15:26:16 +0000 | [diff] [blame] | 1879 | |
| 1880 | verifyFormat("return -1;"); |
| 1881 | verifyFormat("switch (a) {\n" |
| 1882 | "case -1:\n" |
| 1883 | " break;\n" |
| 1884 | "}"); |
Nico Weber | cc191d1 | 2013-01-12 05:41:23 +0000 | [diff] [blame] | 1885 | |
| 1886 | verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { -5, +3 };"); |
| 1887 | verifyFormat("const NSPoint kBrowserFrameViewPatternOffset = { +5, -3 };"); |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 1888 | |
| 1889 | verifyFormat("int a = /* confusing comment */ -1;"); |
| 1890 | // FIXME: The space after 'i' is wrong, but hopefully, this is a rare case. |
| 1891 | verifyFormat("int a = i /* confusing comment */++;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | TEST_F(FormatTest, UndestandsOverloadedOperators) { |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 1895 | verifyFormat("bool operator<();"); |
| 1896 | verifyFormat("bool operator>();"); |
| 1897 | verifyFormat("bool operator=();"); |
| 1898 | verifyFormat("bool operator==();"); |
| 1899 | verifyFormat("bool operator!=();"); |
| 1900 | verifyFormat("int operator+();"); |
| 1901 | verifyFormat("int operator++();"); |
| 1902 | verifyFormat("bool operator();"); |
| 1903 | verifyFormat("bool operator()();"); |
| 1904 | verifyFormat("bool operator[]();"); |
| 1905 | verifyFormat("operator bool();"); |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 1906 | verifyFormat("operator int();"); |
| 1907 | verifyFormat("operator void *();"); |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 1908 | verifyFormat("operator SomeType<int>();"); |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 1909 | verifyFormat("operator SomeType<int, int>();"); |
| 1910 | verifyFormat("operator SomeType<SomeType<int> >();"); |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 1911 | verifyFormat("void *operator new(std::size_t size);"); |
| 1912 | verifyFormat("void *operator new[](std::size_t size);"); |
| 1913 | verifyFormat("void operator delete(void *ptr);"); |
| 1914 | verifyFormat("void operator delete[](void *ptr);"); |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 1915 | |
Daniel Jasper | dc2efa1 | 2013-02-15 19:24:08 +0000 | [diff] [blame] | 1916 | verifyFormat( |
| 1917 | "ostream &operator<<(ostream &OutputStream,\n" |
| 1918 | " SomeReallyLongType WithSomeReallyLongValue);"); |
| 1919 | |
Daniel Jasper | 2b4c924 | 2013-02-11 08:01:18 +0000 | [diff] [blame] | 1920 | verifyGoogleFormat("operator void*();"); |
| 1921 | verifyGoogleFormat("operator SomeType<SomeType<int>>();"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Daniel Jasper | 088dab5 | 2013-01-11 16:09:04 +0000 | [diff] [blame] | 1924 | TEST_F(FormatTest, UnderstandsNewAndDelete) { |
Daniel Jasper | 37eff83 | 2013-02-23 08:07:18 +0000 | [diff] [blame] | 1925 | verifyFormat("void f() {\n" |
| 1926 | " A *a = new A;\n" |
| 1927 | " A *a = new (placement) A;\n" |
| 1928 | " delete a;\n" |
| 1929 | " delete (A *)a;\n" |
| 1930 | "}"); |
Daniel Jasper | 088dab5 | 2013-01-11 16:09:04 +0000 | [diff] [blame] | 1931 | } |
| 1932 | |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 1933 | TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1934 | verifyFormat("int *f(int *a) {}"); |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1935 | verifyFormat("int main(int argc, char **argv) {}"); |
Daniel Jasper | d0f349b | 2013-02-18 12:44:35 +0000 | [diff] [blame] | 1936 | verifyFormat("Test::Test(int b) : a(b * b) {}"); |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1937 | verifyIndependentOfContext("f(a, *a);"); |
Daniel Jasper | 8a5d7cd | 2013-03-01 17:13:29 +0000 | [diff] [blame] | 1938 | verifyFormat("void g() { f(*a); }"); |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1939 | verifyIndependentOfContext("int a = b * 10;"); |
| 1940 | verifyIndependentOfContext("int a = 10 * b;"); |
| 1941 | verifyIndependentOfContext("int a = b * c;"); |
| 1942 | verifyIndependentOfContext("int a += b * c;"); |
| 1943 | verifyIndependentOfContext("int a -= b * c;"); |
| 1944 | verifyIndependentOfContext("int a *= b * c;"); |
| 1945 | verifyIndependentOfContext("int a /= b * c;"); |
| 1946 | verifyIndependentOfContext("int a = *b;"); |
| 1947 | verifyIndependentOfContext("int a = *b * c;"); |
| 1948 | verifyIndependentOfContext("int a = b * *c;"); |
| 1949 | verifyIndependentOfContext("return 10 * b;"); |
| 1950 | verifyIndependentOfContext("return *b * *c;"); |
| 1951 | verifyIndependentOfContext("return a & ~b;"); |
| 1952 | verifyIndependentOfContext("f(b ? *c : *d);"); |
| 1953 | verifyIndependentOfContext("int a = b ? *c : *d;"); |
| 1954 | verifyIndependentOfContext("*b = a;"); |
| 1955 | verifyIndependentOfContext("a * ~b;"); |
| 1956 | verifyIndependentOfContext("a * !b;"); |
| 1957 | verifyIndependentOfContext("a * +b;"); |
| 1958 | verifyIndependentOfContext("a * -b;"); |
| 1959 | verifyIndependentOfContext("a * ++b;"); |
| 1960 | verifyIndependentOfContext("a * --b;"); |
| 1961 | verifyIndependentOfContext("a[4] * b;"); |
Daniel Jasper | 9c65b06 | 2013-02-27 11:43:50 +0000 | [diff] [blame] | 1962 | verifyIndependentOfContext("a[a * a] = 1;"); |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1963 | verifyIndependentOfContext("f() * b;"); |
| 1964 | verifyIndependentOfContext("a * [self dostuff];"); |
| 1965 | verifyIndependentOfContext("a * (a + b);"); |
| 1966 | verifyIndependentOfContext("(a *)(a + b);"); |
| 1967 | verifyIndependentOfContext("int *pa = (int *)&a;"); |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 1968 | verifyIndependentOfContext("return sizeof(int **);"); |
| 1969 | verifyIndependentOfContext("return sizeof(int ******);"); |
| 1970 | verifyIndependentOfContext("return (int **&)a;"); |
Daniel Jasper | 8a5d7cd | 2013-03-01 17:13:29 +0000 | [diff] [blame] | 1971 | verifyFormat("void f(Type (*parameter)[10]) {}"); |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 1972 | verifyGoogleFormat("return sizeof(int**);"); |
| 1973 | verifyIndependentOfContext("Type **A = static_cast<Type **>(P);"); |
| 1974 | verifyGoogleFormat("Type** A = static_cast<Type**>(P);"); |
| 1975 | // FIXME: The newline is wrong. |
| 1976 | verifyFormat("auto a = [](int **&, int ***) {}\n;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 1977 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1978 | verifyIndependentOfContext("InvalidRegions[*R] = 0;"); |
Daniel Jasper | 9bb0d28 | 2013-01-04 20:46:38 +0000 | [diff] [blame] | 1979 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1980 | verifyIndependentOfContext("A<int *> a;"); |
| 1981 | verifyIndependentOfContext("A<int **> a;"); |
| 1982 | verifyIndependentOfContext("A<int *, int *> a;"); |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 1983 | verifyIndependentOfContext( |
| 1984 | "const char *const p = reinterpret_cast<const char *const>(q);"); |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 1985 | verifyIndependentOfContext("A<int **, int **> a;"); |
Daniel Jasper | 6b5ba8b | 2013-02-06 10:57:42 +0000 | [diff] [blame] | 1986 | verifyIndependentOfContext("void f(int *a = d * e, int *b = c * d);"); |
Daniel Jasper | d3cf17b | 2013-03-14 10:50:25 +0000 | [diff] [blame] | 1987 | verifyFormat("for (char **a = b; *a; ++a) {\n}"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 1988 | |
Daniel Jasper | 2db356d | 2013-01-08 20:03:18 +0000 | [diff] [blame] | 1989 | verifyFormat( |
| 1990 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 1991 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, *aaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 1992 | |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 1993 | verifyGoogleFormat("int main(int argc, char** argv) {}"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 1994 | verifyGoogleFormat("A<int*> a;"); |
| 1995 | verifyGoogleFormat("A<int**> a;"); |
| 1996 | verifyGoogleFormat("A<int*, int*> a;"); |
| 1997 | verifyGoogleFormat("A<int**, int**> a;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 1998 | verifyGoogleFormat("f(b ? *c : *d);"); |
| 1999 | verifyGoogleFormat("int a = b ? *c : *d;"); |
Daniel Jasper | 48bd7b7 | 2013-01-16 16:04:06 +0000 | [diff] [blame] | 2000 | verifyGoogleFormat("Type* t = **x;"); |
| 2001 | verifyGoogleFormat("Type* t = *++*x;"); |
| 2002 | verifyGoogleFormat("*++*x;"); |
| 2003 | verifyGoogleFormat("Type* t = const_cast<T*>(&*x);"); |
| 2004 | verifyGoogleFormat("Type* t = x++ * y;"); |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 2005 | verifyGoogleFormat( |
| 2006 | "const char* const p = reinterpret_cast<const char* const>(q);"); |
Manuel Klimek | 1b6f4bd | 2013-01-14 10:58:01 +0000 | [diff] [blame] | 2007 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 2008 | verifyIndependentOfContext("a = *(x + y);"); |
| 2009 | verifyIndependentOfContext("a = &(x + y);"); |
| 2010 | verifyIndependentOfContext("*(x + y).call();"); |
| 2011 | verifyIndependentOfContext("&(x + y)->call();"); |
Daniel Jasper | 8a5d7cd | 2013-03-01 17:13:29 +0000 | [diff] [blame] | 2012 | verifyFormat("void f() { &(*I).first; }"); |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 2013 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 2014 | verifyIndependentOfContext("f(b * /* confusing comment */ ++c);"); |
Daniel Jasper | b369c2c | 2013-01-15 14:27:39 +0000 | [diff] [blame] | 2015 | verifyFormat( |
| 2016 | "int *MyValues = {\n" |
| 2017 | " *A, // Operator detection might be confused by the '{'\n" |
| 2018 | " *BB // Operator detection might be confused by previous comment\n" |
| 2019 | "};"); |
Nico Weber | 5096a44 | 2013-01-17 17:17:19 +0000 | [diff] [blame] | 2020 | |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 2021 | verifyIndependentOfContext("if (int *a = &b)"); |
| 2022 | verifyIndependentOfContext("if (int &a = *b)"); |
| 2023 | verifyIndependentOfContext("if (a & b[i])"); |
| 2024 | verifyIndependentOfContext("if (a::b::c::d & b[i])"); |
| 2025 | verifyIndependentOfContext("if (*b[i])"); |
| 2026 | verifyIndependentOfContext("if (int *a = (&b))"); |
| 2027 | verifyIndependentOfContext("while (int *a = &b)"); |
Daniel Jasper | 20d3583 | 2013-01-23 12:58:14 +0000 | [diff] [blame] | 2028 | verifyFormat("void f() {\n" |
| 2029 | " for (const int &v : Values) {\n" |
| 2030 | " }\n" |
| 2031 | "}"); |
Daniel Jasper | d0f349b | 2013-02-18 12:44:35 +0000 | [diff] [blame] | 2032 | verifyFormat("for (int i = a * a; i < 10; ++i) {\n}"); |
| 2033 | verifyFormat("for (int i = 0; i < a * a; ++i) {\n}"); |
Daniel Jasper | ffee171 | 2013-01-22 11:46:26 +0000 | [diff] [blame] | 2034 | |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 2035 | verifyIndependentOfContext("A = new SomeType *[Length];"); |
Daniel Jasper | 4bfc65a | 2013-01-23 12:10:53 +0000 | [diff] [blame] | 2036 | verifyIndependentOfContext("A = new SomeType *[Length]();"); |
Daniel Jasper | ffee171 | 2013-01-22 11:46:26 +0000 | [diff] [blame] | 2037 | verifyGoogleFormat("A = new SomeType* [Length]();"); |
Daniel Jasper | 6f21a98 | 2013-03-13 07:49:51 +0000 | [diff] [blame] | 2038 | verifyGoogleFormat("A = new SomeType* [Length];"); |
Daniel Jasper | 8a5d7cd | 2013-03-01 17:13:29 +0000 | [diff] [blame] | 2039 | } |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 2040 | |
Daniel Jasper | 8a5d7cd | 2013-03-01 17:13:29 +0000 | [diff] [blame] | 2041 | TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 2042 | EXPECT_EQ("int *a;\n" |
| 2043 | "int *a;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2044 | "int *a;", |
| 2045 | format("int *a;\n" |
| 2046 | "int* a;\n" |
| 2047 | "int *a;", |
| 2048 | getGoogleStyle())); |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 2049 | EXPECT_EQ("int* a;\n" |
| 2050 | "int* a;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2051 | "int* a;", |
| 2052 | format("int* a;\n" |
| 2053 | "int* a;\n" |
| 2054 | "int *a;", |
| 2055 | getGoogleStyle())); |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 2056 | EXPECT_EQ("int *a;\n" |
| 2057 | "int *a;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2058 | "int *a;", |
| 2059 | format("int *a;\n" |
| 2060 | "int * a;\n" |
| 2061 | "int * a;", |
| 2062 | getGoogleStyle())); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Alexander Kornienko | 3fd9ccd | 2013-03-12 16:28:18 +0000 | [diff] [blame] | 2065 | TEST_F(FormatTest, UnderstandsRvalueReferences) { |
| 2066 | verifyFormat("int f(int &&a) {}"); |
| 2067 | verifyFormat("int f(int a, char &&b) {}"); |
| 2068 | verifyFormat("void f() { int &&a = b; }"); |
| 2069 | verifyGoogleFormat("int f(int a, char&& b) {}"); |
| 2070 | verifyGoogleFormat("void f() { int&& a = b; }"); |
| 2071 | |
| 2072 | // FIXME: These require somewhat deeper changes in template arguments |
| 2073 | // formatting. |
| 2074 | // verifyIndependentOfContext("A<int &&> a;"); |
| 2075 | // verifyIndependentOfContext("A<int &&, int &&> a;"); |
| 2076 | // verifyGoogleFormat("A<int&&> a;"); |
| 2077 | // verifyGoogleFormat("A<int&&, int&&> a;"); |
| 2078 | } |
| 2079 | |
Manuel Klimek | a32a7fd | 2013-01-23 14:08:21 +0000 | [diff] [blame] | 2080 | TEST_F(FormatTest, FormatsBinaryOperatorsPrecedingEquals) { |
| 2081 | verifyFormat("void f() {\n" |
| 2082 | " x[aaaaaaaaa -\n" |
| 2083 | " b] = 23;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2084 | "}", |
| 2085 | getLLVMStyleWithColumns(15)); |
Manuel Klimek | a32a7fd | 2013-01-23 14:08:21 +0000 | [diff] [blame] | 2086 | } |
| 2087 | |
Daniel Jasper | 4981bd0 | 2013-01-13 08:01:36 +0000 | [diff] [blame] | 2088 | TEST_F(FormatTest, FormatsCasts) { |
| 2089 | verifyFormat("Type *A = static_cast<Type *>(P);"); |
| 2090 | verifyFormat("Type *A = (Type *)P;"); |
| 2091 | verifyFormat("Type *A = (vector<Type *, int *>)P;"); |
| 2092 | verifyFormat("int a = (int)(2.0f);"); |
| 2093 | |
| 2094 | // FIXME: These also need to be identified. |
| 2095 | verifyFormat("int a = (int) 2.0f;"); |
| 2096 | verifyFormat("int a = (int) * b;"); |
| 2097 | |
| 2098 | // These are not casts. |
| 2099 | verifyFormat("void f(int *) {}"); |
Nico Weber | 6fa7e74 | 2013-02-13 04:32:57 +0000 | [diff] [blame] | 2100 | verifyFormat("f(foo)->b;"); |
| 2101 | verifyFormat("f(foo).b;"); |
| 2102 | verifyFormat("f(foo)(b);"); |
| 2103 | verifyFormat("f(foo)[b];"); |
| 2104 | verifyFormat("[](foo) { return 4; }(bar)];"); |
| 2105 | verifyFormat("(*funptr)(foo)[4];"); |
| 2106 | verifyFormat("funptrs[4](foo)[4];"); |
Daniel Jasper | 4981bd0 | 2013-01-13 08:01:36 +0000 | [diff] [blame] | 2107 | verifyFormat("void f(int *);"); |
| 2108 | verifyFormat("void f(int *) = 0;"); |
| 2109 | verifyFormat("void f(SmallVector<int>) {}"); |
| 2110 | verifyFormat("void f(SmallVector<int>);"); |
| 2111 | verifyFormat("void f(SmallVector<int>) = 0;"); |
Nico Weber | 7728057 | 2013-02-09 18:02:07 +0000 | [diff] [blame] | 2112 | verifyFormat("void f(int i = (kValue) * kMask) {}"); |
| 2113 | verifyFormat("void f(int i = (kA * kB) & kMask) {}"); |
Nico Weber | 6fa7e74 | 2013-02-13 04:32:57 +0000 | [diff] [blame] | 2114 | verifyFormat("int a = sizeof(int) * b;"); |
| 2115 | verifyFormat("int a = alignof(int) * b;"); |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 2116 | |
Daniel Jasper | 37eff83 | 2013-02-23 08:07:18 +0000 | [diff] [blame] | 2117 | // These are not casts, but at some point were confused with casts. |
| 2118 | verifyFormat("virtual void foo(int *) override;"); |
| 2119 | verifyFormat("virtual void foo(char &) const;"); |
| 2120 | verifyFormat("virtual void foo(int *a, char *) const;"); |
Daniel Jasper | 6a365aa | 2013-03-13 17:13:53 +0000 | [diff] [blame] | 2121 | verifyFormat("int a = sizeof(int *) + b;"); |
| 2122 | verifyFormat("int a = alignof(int *) + b;"); |
Daniel Jasper | 4981bd0 | 2013-01-13 08:01:36 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 2125 | TEST_F(FormatTest, FormatsFunctionTypes) { |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 2126 | verifyFormat("A<bool()> a;"); |
| 2127 | verifyFormat("A<SomeType()> a;"); |
| 2128 | verifyFormat("A<void(*)(int, std::string)> a;"); |
| 2129 | |
Daniel Jasper | 8a5d7cd | 2013-03-01 17:13:29 +0000 | [diff] [blame] | 2130 | // FIXME: Inconsistent. |
| 2131 | verifyFormat("int (*func)(void *);"); |
| 2132 | verifyFormat("void f() { int(*func)(void *); }"); |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 2135 | TEST_F(FormatTest, BreaksLongDeclarations) { |
Daniel Jasper | 83f25ba | 2013-01-28 15:16:31 +0000 | [diff] [blame] | 2136 | verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 2137 | " int LoooooooooooooooooooongParam2) {}"); |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 2138 | verifyFormat( |
Daniel Jasper | 83f25ba | 2013-01-28 15:16:31 +0000 | [diff] [blame] | 2139 | "TypeSpecDecl *\n" |
| 2140 | "TypeSpecDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 2141 | " IdentifierIn *II, Type *T) {}"); |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 2142 | verifyFormat("ReallyLongReturnType<TemplateParam1, TemplateParam2>\n" |
| 2143 | "ReallyReallyLongFunctionName(\n" |
| 2144 | " const std::string &SomeParameter,\n" |
| 2145 | " const SomeType<string, SomeOtherTemplateParameter> &\n" |
| 2146 | " ReallyReallyLongParameterName,\n" |
| 2147 | " const SomeType<string, SomeOtherTemplateParameter> &\n" |
| 2148 | " AnotherLongParameterName) {}"); |
| 2149 | verifyFormat( |
| 2150 | "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa<aaaaaaaaaaaaa, aaaaaaaaaaaa>\n" |
| 2151 | "aaaaaaaaaaaaaaaaaaaaaaa;"); |
| 2152 | |
Daniel Jasper | d75ff64 | 2013-01-28 15:40:20 +0000 | [diff] [blame] | 2153 | verifyGoogleFormat( |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 2154 | "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" |
| 2155 | " SourceLocation L) {}"); |
Daniel Jasper | a03ab10 | 2013-02-13 20:33:44 +0000 | [diff] [blame] | 2156 | verifyGoogleFormat( |
| 2157 | "some_namespace::LongReturnType\n" |
| 2158 | "long_namespace::SomeVeryLongClass::SomeVeryLongFunction(\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 2159 | " int first_long_parameter, int second_parameter) {}"); |
Daniel Jasper | a03ab10 | 2013-02-13 20:33:44 +0000 | [diff] [blame] | 2160 | |
| 2161 | verifyGoogleFormat("template <typename T>\n" |
| 2162 | "aaaaaaaa::aaaaa::aaaaaa<T, aaaaaaaaaaaaaaaaaaaaaaaaa>\n" |
Daniel Jasper | f11a705 | 2013-02-21 21:33:55 +0000 | [diff] [blame] | 2163 | "aaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaa() {}"); |
Daniel Jasper | b6f02f3 | 2013-02-28 10:06:05 +0000 | [diff] [blame] | 2164 | verifyGoogleFormat("A<A<A>> aaaaaaaaaa(int aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 2165 | " int aaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 2168 | TEST_F(FormatTest, LineStartsWithSpecialCharacter) { |
| 2169 | verifyFormat("(a)->b();"); |
| 2170 | verifyFormat("--a;"); |
| 2171 | } |
| 2172 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 2173 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
Daniel Jasper | 2b9c10b | 2013-01-14 15:52:06 +0000 | [diff] [blame] | 2174 | verifyFormat("#include <string>\n" |
| 2175 | "#include <a/b/c.h>\n" |
| 2176 | "#include \"a/b/string\"\n" |
| 2177 | "#include \"string.h\"\n" |
| 2178 | "#include \"string.h\"\n" |
Manuel Klimek | 407a31a | 2013-01-15 15:50:27 +0000 | [diff] [blame] | 2179 | "#include <a-a>\n" |
Daniel Jasper | 3a20441 | 2013-02-23 07:46:38 +0000 | [diff] [blame] | 2180 | "#include < path with space >\n" |
| 2181 | "#include \"some very long include paaaaaaaaaaaaaaaaaaaaaaath\"", |
| 2182 | getLLVMStyleWithColumns(35)); |
Nico Weber | b23ae0c | 2012-12-21 18:21:56 +0000 | [diff] [blame] | 2183 | |
Daniel Jasper | 8134e1e | 2013-01-13 08:12:18 +0000 | [diff] [blame] | 2184 | verifyFormat("#import <string>"); |
| 2185 | verifyFormat("#import <a/b/c.h>"); |
| 2186 | verifyFormat("#import \"a/b/string\""); |
| 2187 | verifyFormat("#import \"string.h\""); |
| 2188 | verifyFormat("#import \"string.h\""); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 2191 | //===----------------------------------------------------------------------===// |
| 2192 | // Error recovery tests. |
| 2193 | //===----------------------------------------------------------------------===// |
| 2194 | |
Daniel Jasper | c5cfa49 | 2013-02-14 09:19:04 +0000 | [diff] [blame] | 2195 | TEST_F(FormatTest, IncompleteParameterLists) { |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 2196 | FormatStyle NoBinPacking = getLLVMStyle(); |
| 2197 | NoBinPacking.BinPackParameters = false; |
| 2198 | verifyFormat("void aaaaaaaaaaaaaaaaaa(int level,\n" |
| 2199 | " double *min_x,\n" |
| 2200 | " double *max_x,\n" |
| 2201 | " double *min_y,\n" |
| 2202 | " double *max_y,\n" |
| 2203 | " double *min_z,\n" |
| 2204 | " double *max_z, ) {}", |
| 2205 | NoBinPacking); |
Daniel Jasper | c5cfa49 | 2013-02-14 09:19:04 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 2208 | TEST_F(FormatTest, IncorrectCodeTrailingStuff) { |
Alexander Kornienko | 51c23fa | 2013-01-16 11:45:16 +0000 | [diff] [blame] | 2209 | verifyFormat("void f() { return; }\n42"); |
| 2210 | verifyFormat("void f() {\n" |
| 2211 | " if (0)\n" |
| 2212 | " return;\n" |
| 2213 | "}\n" |
| 2214 | "42"); |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 2215 | verifyFormat("void f() { return }\n42"); |
| 2216 | verifyFormat("void f() {\n" |
| 2217 | " if (0)\n" |
| 2218 | " return\n" |
| 2219 | "}\n" |
| 2220 | "42"); |
| 2221 | } |
| 2222 | |
| 2223 | TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { |
| 2224 | EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); |
| 2225 | EXPECT_EQ("void f() {\n" |
| 2226 | " if (a)\n" |
| 2227 | " return\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2228 | "}", |
| 2229 | format("void f ( ) { if ( a ) return }")); |
Alexander Kornienko | d881875 | 2013-01-16 11:43:46 +0000 | [diff] [blame] | 2230 | EXPECT_EQ("namespace N { void f() }", format("namespace N { void f() }")); |
| 2231 | EXPECT_EQ("namespace N {\n" |
| 2232 | "void f() {}\n" |
| 2233 | "void g()\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2234 | "}", |
| 2235 | format("namespace N { void f( ) { } void g( ) }")); |
Daniel Jasper | 700e710 | 2013-01-10 09:26:47 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 2238 | TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { |
| 2239 | verifyFormat("int aaaaaaaa =\n" |
| 2240 | " // Overly long comment\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2241 | " b;", |
| 2242 | getLLVMStyleWithColumns(20)); |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 2243 | verifyFormat("function(\n" |
| 2244 | " ShortArgument,\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2245 | " LoooooooooooongArgument);\n", |
| 2246 | getLLVMStyleWithColumns(20)); |
Daniel Jasper | ceb99ab | 2013-01-09 10:16:05 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 2249 | TEST_F(FormatTest, IncorrectAccessSpecifier) { |
| 2250 | verifyFormat("public:"); |
| 2251 | verifyFormat("class A {\n" |
| 2252 | "public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 2253 | " void f() {}\n" |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 2254 | "};"); |
| 2255 | verifyFormat("public\n" |
| 2256 | "int qwerty;"); |
| 2257 | verifyFormat("public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 2258 | "B {}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 2259 | verifyFormat("public\n" |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 2260 | "{}"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 2261 | verifyFormat("public\n" |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 2262 | "B { int x; }"); |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 2263 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2264 | |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2265 | TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { verifyFormat("{"); } |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 2266 | |
| 2267 | TEST_F(FormatTest, IncorrectCodeDoNoWhile) { |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2268 | verifyFormat("do {\n}"); |
| 2269 | verifyFormat("do {\n}\n" |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 2270 | "f();"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2271 | verifyFormat("do {\n}\n" |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 2272 | "wheeee(fun);"); |
| 2273 | verifyFormat("do {\n" |
| 2274 | " f();\n" |
Manuel Klimek | de76854 | 2013-01-07 18:10:23 +0000 | [diff] [blame] | 2275 | "}"); |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 2278 | TEST_F(FormatTest, IncorrectCodeMissingParens) { |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 2279 | verifyFormat("if {\n foo;\n foo();\n}"); |
Manuel Klimek | 6eca03f | 2013-01-11 19:23:05 +0000 | [diff] [blame] | 2280 | verifyFormat("switch {\n foo;\n foo();\n}"); |
| 2281 | verifyFormat("for {\n foo;\n foo();\n}"); |
| 2282 | verifyFormat("while {\n foo;\n foo();\n}"); |
| 2283 | verifyFormat("do {\n foo;\n foo();\n} while;"); |
Manuel Klimek | d465843 | 2013-01-11 18:28:36 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 2286 | TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { |
| 2287 | verifyFormat("namespace {\n" |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 2288 | "class Foo { Foo ( }; } // comment"); |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 2291 | TEST_F(FormatTest, IncorrectCodeErrorDetection) { |
Manuel Klimek | 36fab8d | 2013-01-10 13:24:24 +0000 | [diff] [blame] | 2292 | EXPECT_EQ("{\n{}\n", format("{\n{\n}\n")); |
| 2293 | EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); |
| 2294 | EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); |
| 2295 | EXPECT_EQ("{\n {}\n }\n}\n", format("{\n {\n }\n }\n}\n")); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 2296 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 2297 | EXPECT_EQ("{\n" |
| 2298 | " {\n" |
| 2299 | " breakme(\n" |
| 2300 | " qwe);\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2301 | "}\n", |
| 2302 | format("{\n" |
| 2303 | " {\n" |
| 2304 | " breakme(qwe);\n" |
| 2305 | "}\n", |
| 2306 | getLLVMStyleWithColumns(10))); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 2309 | TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2310 | verifyFormat("int x = {\n" |
| 2311 | " avariable,\n" |
| 2312 | " b(alongervariable)\n" |
| 2313 | "};", |
| 2314 | getLLVMStyleWithColumns(25)); |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 2317 | TEST_F(FormatTest, LayoutBraceInitializersInReturnStatement) { |
| 2318 | verifyFormat("return (a)(b) { 1, 2, 3 };"); |
| 2319 | } |
| 2320 | |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 2321 | TEST_F(FormatTest, LayoutTokensFollowingBlockInParentheses) { |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 2322 | // FIXME: This is bad, find a better and more generic solution. |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 2323 | verifyFormat( |
| 2324 | "Aaa({\n" |
| 2325 | " int i;\n" |
Daniel Jasper | 7d81281 | 2013-02-21 15:00:29 +0000 | [diff] [blame] | 2326 | "},\n" |
| 2327 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" |
| 2328 | " ccccccccccccccccc));"); |
Manuel Klimek | 2851c16 | 2013-01-10 14:36:46 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 2331 | TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { |
| 2332 | verifyFormat("void f() { return 42; }"); |
| 2333 | verifyFormat("void f() {\n" |
| 2334 | " // Comment\n" |
| 2335 | "}"); |
| 2336 | verifyFormat("{\n" |
| 2337 | "#error {\n" |
| 2338 | " int a;\n" |
| 2339 | "}"); |
| 2340 | verifyFormat("{\n" |
| 2341 | " int a;\n" |
| 2342 | "#error {\n" |
| 2343 | "}"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2344 | |
| 2345 | verifyFormat("void f() { return 42; }", getLLVMStyleWithColumns(23)); |
| 2346 | verifyFormat("void f() {\n return 42;\n}", getLLVMStyleWithColumns(22)); |
| 2347 | |
| 2348 | verifyFormat("void f() {}", getLLVMStyleWithColumns(11)); |
| 2349 | verifyFormat("void f() {\n}", getLLVMStyleWithColumns(10)); |
Manuel Klimek | 517e894 | 2013-01-11 17:54:10 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2352 | TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { |
| 2353 | // Elaborate type variable declarations. |
Manuel Klimek | 606e07e | 2013-01-11 18:13:04 +0000 | [diff] [blame] | 2354 | verifyFormat("struct foo a = { bar };\nint n;"); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2355 | verifyFormat("class foo a = { bar };\nint n;"); |
| 2356 | verifyFormat("union foo a = { bar };\nint n;"); |
| 2357 | |
| 2358 | // Elaborate types inside function definitions. |
| 2359 | verifyFormat("struct foo f() {}\nint n;"); |
| 2360 | verifyFormat("class foo f() {}\nint n;"); |
| 2361 | verifyFormat("union foo f() {}\nint n;"); |
| 2362 | |
| 2363 | // Templates. |
| 2364 | verifyFormat("template <class X> void f() {}\nint n;"); |
| 2365 | verifyFormat("template <struct X> void f() {}\nint n;"); |
| 2366 | verifyFormat("template <union X> void f() {}\nint n;"); |
| 2367 | |
| 2368 | // Actual definitions... |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2369 | verifyFormat("struct {\n} n;"); |
| 2370 | verifyFormat( |
| 2371 | "template <template <class T, class Y>, class Z> class X {\n} n;"); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2372 | verifyFormat("union Z {\n int n;\n} x;"); |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2373 | verifyFormat("class MACRO Z {\n} n;"); |
| 2374 | verifyFormat("class MACRO(X) Z {\n} n;"); |
| 2375 | verifyFormat("class __attribute__(X) Z {\n} n;"); |
| 2376 | verifyFormat("class __declspec(X) Z {\n} n;"); |
Manuel Klimek | b8b1ce1 | 2013-02-06 15:57:54 +0000 | [diff] [blame] | 2377 | verifyFormat("class A##B##C {\n} n;"); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2378 | |
Manuel Klimek | 7f5b025 | 2013-01-21 10:17:14 +0000 | [diff] [blame] | 2379 | // Redefinition from nested context: |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2380 | verifyFormat("class A::B::C {\n} n;"); |
Manuel Klimek | 7f5b025 | 2013-01-21 10:17:14 +0000 | [diff] [blame] | 2381 | |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 2382 | // Template definitions. |
| 2383 | // FIXME: This is still incorrectly handled at the formatter side. |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2384 | verifyFormat("template <> struct X < 15, i < 3 && 42 < 50 && 33<28> {\n};"); |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 2385 | |
| 2386 | // FIXME: |
| 2387 | // This now gets parsed incorrectly as class definition. |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2388 | // verifyFormat("class A<int> f() {\n}\nint n;"); |
Manuel Klimek | 3a3408c | 2013-01-21 13:58:54 +0000 | [diff] [blame] | 2389 | |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 2390 | // Elaborate types where incorrectly parsing the structural element would |
| 2391 | // break the indent. |
| 2392 | verifyFormat("if (true)\n" |
| 2393 | " class X x;\n" |
| 2394 | "else\n" |
| 2395 | " f();\n"); |
Manuel Klimek | 606e07e | 2013-01-11 18:13:04 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
Manuel Klimek | 407a31a | 2013-01-15 15:50:27 +0000 | [diff] [blame] | 2398 | TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { |
| 2399 | verifyFormat("#error Leave all white!!!!! space* alone!\n"); |
| 2400 | verifyFormat("#warning Leave all white!!!!! space* alone!\n"); |
| 2401 | EXPECT_EQ("#error 1", format(" # error 1")); |
| 2402 | EXPECT_EQ("#warning 1", format(" # warning 1")); |
| 2403 | } |
| 2404 | |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 2405 | TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { |
| 2406 | FormatStyle AllowsMergedIf = getGoogleStyle(); |
| 2407 | AllowsMergedIf.AllowShortIfStatementsOnASingleLine = true; |
| 2408 | verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); |
| 2409 | verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 2410 | verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); |
| 2411 | EXPECT_EQ("if (true) return 42;", |
| 2412 | format("if (true)\nreturn 42;", AllowsMergedIf)); |
| 2413 | FormatStyle ShortMergedIf = AllowsMergedIf; |
| 2414 | ShortMergedIf.ColumnLimit = 25; |
| 2415 | verifyFormat("#define A \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2416 | " if (true) return 42;", |
| 2417 | ShortMergedIf); |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 2418 | verifyFormat("#define A \\\n" |
| 2419 | " f(); \\\n" |
| 2420 | " if (true)\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2421 | "#define B", |
| 2422 | ShortMergedIf); |
Manuel Klimek | 4c12812 | 2013-01-18 14:46:43 +0000 | [diff] [blame] | 2423 | verifyFormat("#define A \\\n" |
| 2424 | " f(); \\\n" |
| 2425 | " if (true)\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2426 | "g();", |
| 2427 | ShortMergedIf); |
Manuel Klimek | 0fbe008 | 2013-01-21 14:16:56 +0000 | [diff] [blame] | 2428 | verifyFormat("{\n" |
| 2429 | "#ifdef A\n" |
| 2430 | " // Comment\n" |
| 2431 | " if (true) continue;\n" |
| 2432 | "#endif\n" |
| 2433 | " // Comment\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2434 | " if (true) continue;", |
| 2435 | ShortMergedIf); |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2438 | TEST_F(FormatTest, BlockCommentsInControlLoops) { |
| 2439 | verifyFormat("if (0) /* a comment in a strange place */ {\n" |
| 2440 | " f();\n" |
| 2441 | "}"); |
| 2442 | verifyFormat("if (0) /* a comment in a strange place */ {\n" |
| 2443 | " f();\n" |
| 2444 | "} /* another comment */ else /* comment #3 */ {\n" |
| 2445 | " g();\n" |
| 2446 | "}"); |
| 2447 | verifyFormat("while (0) /* a comment in a strange place */ {\n" |
| 2448 | " f();\n" |
| 2449 | "}"); |
| 2450 | verifyFormat("for (;;) /* a comment in a strange place */ {\n" |
| 2451 | " f();\n" |
| 2452 | "}"); |
| 2453 | verifyFormat("do /* a comment in a strange place */ {\n" |
| 2454 | " f();\n" |
| 2455 | "} /* another comment */ while (0);"); |
| 2456 | } |
| 2457 | |
| 2458 | TEST_F(FormatTest, BlockComments) { |
| 2459 | EXPECT_EQ("/* */ /* */ /* */\n/* */ /* */ /* */", |
| 2460 | format("/* *//* */ /* */\n/* *//* */ /* */")); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2461 | EXPECT_EQ("/* */ a /* */ b;", format(" /* */ a/* */ b;")); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2462 | EXPECT_EQ("#define A /* */\\\n" |
| 2463 | " b\n" |
| 2464 | "/* */\n" |
| 2465 | "someCall(\n" |
| 2466 | " parameter);", |
| 2467 | format("#define A /* */ b\n" |
| 2468 | "/* */\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2469 | "someCall(parameter);", |
| 2470 | getLLVMStyleWithColumns(15))); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2471 | |
| 2472 | EXPECT_EQ("#define A\n" |
| 2473 | "/* */ someCall(\n" |
| 2474 | " parameter);", |
| 2475 | format("#define A\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2476 | "/* */someCall(parameter);", |
| 2477 | getLLVMStyleWithColumns(15))); |
Daniel Jasper | 2752ff3 | 2013-02-04 07:32:14 +0000 | [diff] [blame] | 2478 | |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 2479 | FormatStyle NoBinPacking = getLLVMStyle(); |
| 2480 | NoBinPacking.BinPackParameters = false; |
Daniel Jasper | 2752ff3 | 2013-02-04 07:32:14 +0000 | [diff] [blame] | 2481 | EXPECT_EQ("someFunction(1, /* comment 1 */\n" |
| 2482 | " 2, /* comment 2 */\n" |
| 2483 | " 3, /* comment 3 */\n" |
Daniel Jasper | cda1650 | 2013-02-04 08:34:57 +0000 | [diff] [blame] | 2484 | " aaaa,\n" |
| 2485 | " bbbb);", |
Daniel Jasper | 2752ff3 | 2013-02-04 07:32:14 +0000 | [diff] [blame] | 2486 | format("someFunction (1, /* comment 1 */\n" |
| 2487 | " 2, /* comment 2 */ \n" |
| 2488 | " 3, /* comment 3 */\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2489 | "aaaa, bbbb );", |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 2490 | NoBinPacking)); |
Daniel Jasper | 36ba0ff | 2013-02-06 15:23:09 +0000 | [diff] [blame] | 2491 | verifyFormat( |
| 2492 | "bool aaaaaaaaaaaaa = /* comment: */ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 2493 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 2494 | EXPECT_EQ( |
| 2495 | "bool aaaaaaaaaaaaa = /* trailing comment */\n" |
| 2496 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 2497 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;", |
| 2498 | format( |
| 2499 | "bool aaaaaaaaaaaaa = /* trailing comment */\n" |
| 2500 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa||aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 2501 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaaaaa;")); |
Daniel Jasper | 15417ef | 2013-02-06 20:07:35 +0000 | [diff] [blame] | 2502 | EXPECT_EQ( |
| 2503 | "int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" |
| 2504 | "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" |
| 2505 | "int cccccccccccccccccccccccccccccc; /* comment */\n", |
| 2506 | format("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa; /* comment */\n" |
| 2507 | "int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; /* comment */\n" |
| 2508 | "int cccccccccccccccccccccccccccccc; /* comment */\n")); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 2509 | } |
| 2510 | |
Manuel Klimek | b3507cd | 2013-02-06 16:40:56 +0000 | [diff] [blame] | 2511 | TEST_F(FormatTest, BlockCommentsInMacros) { |
| 2512 | EXPECT_EQ("#define A \\\n" |
| 2513 | " { \\\n" |
| 2514 | " /* one line */ \\\n" |
| 2515 | " someCall();", |
| 2516 | format("#define A { \\\n" |
| 2517 | " /* one line */ \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2518 | " someCall();", |
| 2519 | getLLVMStyleWithColumns(20))); |
Manuel Klimek | b3507cd | 2013-02-06 16:40:56 +0000 | [diff] [blame] | 2520 | EXPECT_EQ("#define A \\\n" |
| 2521 | " { \\\n" |
| 2522 | " /* previous */ \\\n" |
| 2523 | " /* one line */ \\\n" |
| 2524 | " someCall();", |
| 2525 | format("#define A { \\\n" |
| 2526 | " /* previous */ \\\n" |
| 2527 | " /* one line */ \\\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2528 | " someCall();", |
| 2529 | getLLVMStyleWithColumns(20))); |
Manuel Klimek | b3507cd | 2013-02-06 16:40:56 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { |
| 2533 | // FIXME: This is not what we want... |
| 2534 | verifyFormat("{\n" |
| 2535 | "// a" |
| 2536 | "// b"); |
| 2537 | } |
| 2538 | |
Manuel Klimek | 092a2c7 | 2013-01-23 10:09:28 +0000 | [diff] [blame] | 2539 | TEST_F(FormatTest, FormatStarDependingOnContext) { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 2540 | verifyFormat("void f(int *a);"); |
| 2541 | verifyFormat("void f() { f(fint * b); }"); |
Manuel Klimek | 836b58f | 2013-01-23 11:03:04 +0000 | [diff] [blame] | 2542 | verifyFormat("class A {\n void f(int *a);\n};"); |
| 2543 | verifyFormat("class A {\n int *a;\n};"); |
| 2544 | verifyFormat("namespace a {\n" |
| 2545 | "namespace b {\n" |
| 2546 | "class A {\n" |
| 2547 | " void f() {}\n" |
| 2548 | " int *a;\n" |
| 2549 | "};\n" |
| 2550 | "}\n" |
| 2551 | "}"); |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 2552 | } |
| 2553 | |
Manuel Klimek | 092a2c7 | 2013-01-23 10:09:28 +0000 | [diff] [blame] | 2554 | TEST_F(FormatTest, SpecialTokensAtEndOfLine) { |
| 2555 | verifyFormat("while"); |
| 2556 | verifyFormat("operator"); |
| 2557 | } |
| 2558 | |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 2559 | //===----------------------------------------------------------------------===// |
| 2560 | // Objective-C tests. |
| 2561 | //===----------------------------------------------------------------------===// |
| 2562 | |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 2563 | TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { |
| 2564 | verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); |
| 2565 | EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", |
| 2566 | format("-(NSUInteger)indexOfObject:(id)anObject;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 2567 | EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 2568 | EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); |
| 2569 | EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", |
| 2570 | format("-(NSInteger)Method3:(id)anObject;")); |
| 2571 | EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", |
| 2572 | format("-(NSInteger)Method4:(id)anObject;")); |
| 2573 | EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", |
| 2574 | format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); |
| 2575 | EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", |
| 2576 | format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 2577 | EXPECT_EQ( |
| 2578 | "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 2579 | format( |
| 2580 | "- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); |
Fariborz Jahanian | 9b3f02c | 2012-12-21 22:51:18 +0000 | [diff] [blame] | 2581 | |
| 2582 | // Very long objectiveC method declaration. |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 2583 | verifyFormat("- (NSUInteger)indexOfObject:(id)anObject\n" |
| 2584 | " inRange:(NSRange)range\n" |
| 2585 | " outRange:(NSRange)out_range\n" |
| 2586 | " outRange1:(NSRange)out_range1\n" |
| 2587 | " outRange2:(NSRange)out_range2\n" |
| 2588 | " outRange3:(NSRange)out_range3\n" |
| 2589 | " outRange4:(NSRange)out_range4\n" |
| 2590 | " outRange5:(NSRange)out_range5\n" |
| 2591 | " outRange6:(NSRange)out_range6\n" |
| 2592 | " outRange7:(NSRange)out_range7\n" |
| 2593 | " outRange8:(NSRange)out_range8\n" |
| 2594 | " outRange9:(NSRange)out_range9;"); |
Nico Weber | e0fd429 | 2013-01-10 20:18:33 +0000 | [diff] [blame] | 2595 | |
| 2596 | verifyFormat("- (int)sum:(vector<int>)numbers;"); |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 2597 | verifyGoogleFormat("- (void)setDelegate:(id<Protocol>)delegate;"); |
Nico Weber | e0fd429 | 2013-01-10 20:18:33 +0000 | [diff] [blame] | 2598 | // FIXME: In LLVM style, there should be a space in front of a '<' for ObjC |
| 2599 | // protocol lists (but not for template classes): |
| 2600 | //verifyFormat("- (void)setDelegate:(id <Protocol>)delegate;"); |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 2601 | |
| 2602 | verifyFormat("- (int(*)())foo:(int(*)())f;"); |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 2603 | verifyGoogleFormat("- (int(*)())foo:(int(*)())foo;"); |
Nico Weber | cd52bda | 2013-01-10 23:11:41 +0000 | [diff] [blame] | 2604 | |
| 2605 | // If there's no return type (very rare in practice!), LLVM and Google style |
| 2606 | // agree. |
| 2607 | verifyFormat("- foo:(int)f;"); |
| 2608 | verifyGoogleFormat("- foo:(int)foo;"); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 2611 | TEST_F(FormatTest, FormatObjCBlocks) { |
Daniel Jasper | 46ef852 | 2013-01-10 13:08:12 +0000 | [diff] [blame] | 2612 | verifyFormat("int (^Block)(int, int);"); |
| 2613 | verifyFormat("int (^Block1)(int, int) = ^(int i, int j)"); |
Daniel Jasper | 886568d | 2013-01-09 08:36:49 +0000 | [diff] [blame] | 2614 | } |
| 2615 | |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2616 | TEST_F(FormatTest, FormatObjCInterface) { |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2617 | verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2618 | "@public\n" |
| 2619 | " int field1;\n" |
| 2620 | "@protected\n" |
| 2621 | " int field2;\n" |
| 2622 | "@private\n" |
| 2623 | " int field3;\n" |
| 2624 | "@package\n" |
| 2625 | " int field4;\n" |
| 2626 | "}\n" |
| 2627 | "+ (id)init;\n" |
| 2628 | "@end"); |
| 2629 | |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2630 | verifyGoogleFormat("@interface Foo : NSObject<NSSomeDelegate> {\n" |
| 2631 | " @public\n" |
| 2632 | " int field1;\n" |
| 2633 | " @protected\n" |
| 2634 | " int field2;\n" |
| 2635 | " @private\n" |
| 2636 | " int field3;\n" |
| 2637 | " @package\n" |
| 2638 | " int field4;\n" |
| 2639 | "}\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 2640 | "+ (id)init;\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2641 | "@end"); |
| 2642 | |
Nico Weber | f5ecfa5 | 2013-01-22 16:53:59 +0000 | [diff] [blame] | 2643 | verifyFormat("@interface /* wait for it */ Foo\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2644 | "+ (id)init;\n" |
| 2645 | "// Look, a comment!\n" |
| 2646 | "- (int)answerWith:(int)i;\n" |
| 2647 | "@end"); |
| 2648 | |
| 2649 | verifyFormat("@interface Foo\n" |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 2650 | "@end\n" |
| 2651 | "@interface Bar\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2652 | "@end"); |
| 2653 | |
| 2654 | verifyFormat("@interface Foo : Bar\n" |
| 2655 | "+ (id)init;\n" |
| 2656 | "@end"); |
| 2657 | |
Nico Weber | f5ecfa5 | 2013-01-22 16:53:59 +0000 | [diff] [blame] | 2658 | verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2659 | "+ (id)init;\n" |
| 2660 | "@end"); |
| 2661 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2662 | verifyGoogleFormat("@interface Foo : Bar<Baz, Quux>\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 2663 | "+ (id)init;\n" |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2664 | "@end"); |
| 2665 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 2666 | verifyFormat("@interface Foo (HackStuff)\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2667 | "+ (id)init;\n" |
| 2668 | "@end"); |
| 2669 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 2670 | verifyFormat("@interface Foo ()\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2671 | "+ (id)init;\n" |
| 2672 | "@end"); |
| 2673 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2674 | verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2675 | "+ (id)init;\n" |
| 2676 | "@end"); |
| 2677 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2678 | verifyGoogleFormat("@interface Foo (HackStuff)<MyProtocol>\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 2679 | "+ (id)init;\n" |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2680 | "@end"); |
| 2681 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2682 | verifyFormat("@interface Foo {\n" |
| 2683 | " int _i;\n" |
| 2684 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2685 | "+ (id)init;\n" |
| 2686 | "@end"); |
| 2687 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2688 | verifyFormat("@interface Foo : Bar {\n" |
| 2689 | " int _i;\n" |
| 2690 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2691 | "+ (id)init;\n" |
| 2692 | "@end"); |
| 2693 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2694 | verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" |
| 2695 | " int _i;\n" |
| 2696 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2697 | "+ (id)init;\n" |
| 2698 | "@end"); |
| 2699 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2700 | verifyFormat("@interface Foo (HackStuff) {\n" |
| 2701 | " int _i;\n" |
| 2702 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2703 | "+ (id)init;\n" |
| 2704 | "@end"); |
| 2705 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2706 | verifyFormat("@interface Foo () {\n" |
| 2707 | " int _i;\n" |
| 2708 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2709 | "+ (id)init;\n" |
| 2710 | "@end"); |
| 2711 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2712 | verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" |
| 2713 | " int _i;\n" |
| 2714 | "}\n" |
Nico Weber | 27d1367 | 2013-01-09 20:25:35 +0000 | [diff] [blame] | 2715 | "+ (id)init;\n" |
| 2716 | "@end"); |
| 2717 | } |
| 2718 | |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2719 | TEST_F(FormatTest, FormatObjCImplementation) { |
| 2720 | verifyFormat("@implementation Foo : NSObject {\n" |
| 2721 | "@public\n" |
| 2722 | " int field1;\n" |
| 2723 | "@protected\n" |
| 2724 | " int field2;\n" |
| 2725 | "@private\n" |
| 2726 | " int field3;\n" |
| 2727 | "@package\n" |
| 2728 | " int field4;\n" |
| 2729 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2730 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2731 | "@end"); |
| 2732 | |
| 2733 | verifyGoogleFormat("@implementation Foo : NSObject {\n" |
| 2734 | " @public\n" |
| 2735 | " int field1;\n" |
| 2736 | " @protected\n" |
| 2737 | " int field2;\n" |
| 2738 | " @private\n" |
| 2739 | " int field3;\n" |
| 2740 | " @package\n" |
| 2741 | " int field4;\n" |
| 2742 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2743 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2744 | "@end"); |
| 2745 | |
| 2746 | verifyFormat("@implementation Foo\n" |
| 2747 | "+ (id)init {\n" |
Daniel Jasper | 6f5bb2c | 2013-01-14 16:24:39 +0000 | [diff] [blame] | 2748 | " if (true)\n" |
| 2749 | " return nil;\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2750 | "}\n" |
| 2751 | "// Look, a comment!\n" |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2752 | "- (int)answerWith:(int)i {\n" |
| 2753 | " return i;\n" |
| 2754 | "}\n" |
Nico Weber | 67015ed | 2013-01-11 21:14:08 +0000 | [diff] [blame] | 2755 | "+ (int)answerWith:(int)i {\n" |
| 2756 | " return i;\n" |
| 2757 | "}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2758 | "@end"); |
| 2759 | |
| 2760 | verifyFormat("@implementation Foo\n" |
| 2761 | "@end\n" |
| 2762 | "@implementation Bar\n" |
| 2763 | "@end"); |
| 2764 | |
| 2765 | verifyFormat("@implementation Foo : Bar\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2766 | "+ (id)init {\n}\n" |
| 2767 | "- (void)foo {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2768 | "@end"); |
| 2769 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2770 | verifyFormat("@implementation Foo {\n" |
| 2771 | " int _i;\n" |
| 2772 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2773 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2774 | "@end"); |
| 2775 | |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2776 | verifyFormat("@implementation Foo : Bar {\n" |
| 2777 | " int _i;\n" |
| 2778 | "}\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2779 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2780 | "@end"); |
| 2781 | |
Nico Weber | ed91bba | 2013-01-10 19:19:14 +0000 | [diff] [blame] | 2782 | verifyFormat("@implementation Foo (HackStuff)\n" |
Manuel Klimek | 2f1ac41 | 2013-01-21 16:42:44 +0000 | [diff] [blame] | 2783 | "+ (id)init {\n}\n" |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 2784 | "@end"); |
| 2785 | } |
| 2786 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2787 | TEST_F(FormatTest, FormatObjCProtocol) { |
| 2788 | verifyFormat("@protocol Foo\n" |
| 2789 | "@property(weak) id delegate;\n" |
| 2790 | "- (NSUInteger)numberOfThings;\n" |
| 2791 | "@end"); |
| 2792 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2793 | verifyFormat("@protocol MyProtocol <NSObject>\n" |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2794 | "- (NSUInteger)numberOfThings;\n" |
| 2795 | "@end"); |
| 2796 | |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2797 | verifyGoogleFormat("@protocol MyProtocol<NSObject>\n" |
Nico Weber | aab6005 | 2013-01-17 06:14:50 +0000 | [diff] [blame] | 2798 | "- (NSUInteger)numberOfThings;\n" |
Nico Weber | 5f500df | 2013-01-10 20:12:55 +0000 | [diff] [blame] | 2799 | "@end"); |
| 2800 | |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2801 | verifyFormat("@protocol Foo;\n" |
| 2802 | "@protocol Bar;\n"); |
Nico Weber | 049c447 | 2013-01-09 21:42:32 +0000 | [diff] [blame] | 2803 | |
| 2804 | verifyFormat("@protocol Foo\n" |
| 2805 | "@end\n" |
| 2806 | "@protocol Bar\n" |
| 2807 | "@end"); |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 2808 | |
| 2809 | verifyFormat("@protocol myProtocol\n" |
| 2810 | "- (void)mandatoryWithInt:(int)i;\n" |
| 2811 | "@optional\n" |
| 2812 | "- (void)optional;\n" |
| 2813 | "@required\n" |
| 2814 | "- (void)required;\n" |
Nico Weber | 880e538 | 2013-01-10 00:42:07 +0000 | [diff] [blame] | 2815 | "@optional\n" |
| 2816 | "@property(assign) int madProp;\n" |
Nico Weber | b530fa3 | 2013-01-10 00:25:19 +0000 | [diff] [blame] | 2817 | "@end\n"); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 2818 | } |
| 2819 | |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 2820 | TEST_F(FormatTest, FormatObjCMethodDeclarations) { |
| 2821 | verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" |
| 2822 | " rect:(NSRect)theRect\n" |
| 2823 | " interval:(float)theInterval {\n" |
| 2824 | "}"); |
| 2825 | verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" |
| 2826 | " longKeyword:(NSRect)theRect\n" |
| 2827 | " evenLongerKeyword:(float)theInterval\n" |
| 2828 | " error:(NSError **)theError {\n" |
| 2829 | "}"); |
| 2830 | } |
| 2831 | |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 2832 | TEST_F(FormatTest, FormatObjCMethodExpr) { |
| 2833 | verifyFormat("[foo bar:baz];"); |
| 2834 | verifyFormat("return [foo bar:baz];"); |
| 2835 | verifyFormat("f([foo bar:baz]);"); |
| 2836 | verifyFormat("f(2, [foo bar:baz]);"); |
| 2837 | verifyFormat("f(2, a ? b : c);"); |
| 2838 | verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); |
| 2839 | |
Nico Weber | ee0feec | 2013-02-05 16:21:00 +0000 | [diff] [blame] | 2840 | // Unary operators. |
| 2841 | verifyFormat("int a = +[foo bar:baz];"); |
| 2842 | verifyFormat("int a = -[foo bar:baz];"); |
| 2843 | verifyFormat("int a = ![foo bar:baz];"); |
| 2844 | verifyFormat("int a = ~[foo bar:baz];"); |
| 2845 | verifyFormat("int a = ++[foo bar:baz];"); |
| 2846 | verifyFormat("int a = --[foo bar:baz];"); |
| 2847 | verifyFormat("int a = sizeof [foo bar:baz];"); |
| 2848 | verifyFormat("int a = alignof [foo bar:baz];"); |
Nico Weber | e8a9798 | 2013-02-06 06:20:11 +0000 | [diff] [blame] | 2849 | verifyFormat("int a = &[foo bar:baz];"); |
| 2850 | verifyFormat("int a = *[foo bar:baz];"); |
Nico Weber | ee0feec | 2013-02-05 16:21:00 +0000 | [diff] [blame] | 2851 | // FIXME: Make casts work, without breaking f()[4]. |
Nico Weber | 4c2cc60 | 2013-02-13 03:48:27 +0000 | [diff] [blame] | 2852 | //verifyFormat("int a = (int)[foo bar:baz];"); |
Nico Weber | e0dc7c7 | 2013-02-13 04:40:03 +0000 | [diff] [blame] | 2853 | //verifyFormat("return (int)[foo bar:baz];"); |
| 2854 | //verifyFormat("(void)[foo bar:baz];"); |
Nico Weber | 4c2cc60 | 2013-02-13 03:48:27 +0000 | [diff] [blame] | 2855 | verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); |
Nico Weber | ee0feec | 2013-02-05 16:21:00 +0000 | [diff] [blame] | 2856 | |
| 2857 | // Binary operators. |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 2858 | verifyFormat("[foo bar:baz], [foo bar:baz];"); |
| 2859 | verifyFormat("[foo bar:baz] = [foo bar:baz];"); |
| 2860 | verifyFormat("[foo bar:baz] *= [foo bar:baz];"); |
| 2861 | verifyFormat("[foo bar:baz] /= [foo bar:baz];"); |
| 2862 | verifyFormat("[foo bar:baz] %= [foo bar:baz];"); |
| 2863 | verifyFormat("[foo bar:baz] += [foo bar:baz];"); |
| 2864 | verifyFormat("[foo bar:baz] -= [foo bar:baz];"); |
| 2865 | verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); |
| 2866 | verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); |
| 2867 | verifyFormat("[foo bar:baz] &= [foo bar:baz];"); |
| 2868 | verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); |
| 2869 | verifyFormat("[foo bar:baz] |= [foo bar:baz];"); |
| 2870 | verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); |
| 2871 | verifyFormat("[foo bar:baz] || [foo bar:baz];"); |
| 2872 | verifyFormat("[foo bar:baz] && [foo bar:baz];"); |
| 2873 | verifyFormat("[foo bar:baz] | [foo bar:baz];"); |
| 2874 | verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); |
| 2875 | verifyFormat("[foo bar:baz] & [foo bar:baz];"); |
| 2876 | verifyFormat("[foo bar:baz] == [foo bar:baz];"); |
| 2877 | verifyFormat("[foo bar:baz] != [foo bar:baz];"); |
| 2878 | verifyFormat("[foo bar:baz] >= [foo bar:baz];"); |
| 2879 | verifyFormat("[foo bar:baz] <= [foo bar:baz];"); |
| 2880 | verifyFormat("[foo bar:baz] > [foo bar:baz];"); |
| 2881 | verifyFormat("[foo bar:baz] < [foo bar:baz];"); |
| 2882 | verifyFormat("[foo bar:baz] >> [foo bar:baz];"); |
| 2883 | verifyFormat("[foo bar:baz] << [foo bar:baz];"); |
| 2884 | verifyFormat("[foo bar:baz] - [foo bar:baz];"); |
| 2885 | verifyFormat("[foo bar:baz] + [foo bar:baz];"); |
| 2886 | verifyFormat("[foo bar:baz] * [foo bar:baz];"); |
| 2887 | verifyFormat("[foo bar:baz] / [foo bar:baz];"); |
| 2888 | verifyFormat("[foo bar:baz] % [foo bar:baz];"); |
| 2889 | // Whew! |
| 2890 | |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame] | 2891 | verifyFormat("return in[42];"); |
| 2892 | verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" |
| 2893 | "}"); |
| 2894 | |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 2895 | verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); |
| 2896 | verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); |
| 2897 | verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); |
| 2898 | verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); |
| 2899 | verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); |
Nico Weber | 6292dd4 | 2013-01-12 23:41:33 +0000 | [diff] [blame] | 2900 | verifyFormat("[button setAction:@selector(zoomOut:)];"); |
Nico Weber | cd45833 | 2013-01-12 23:48:49 +0000 | [diff] [blame] | 2901 | verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 2902 | |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 2903 | verifyFormat("arr[[self indexForFoo:a]];"); |
| 2904 | verifyFormat("throw [self errorFor:a];"); |
| 2905 | verifyFormat("@throw [self errorFor:a];"); |
| 2906 | |
Nico Weber | e8ccc81 | 2013-01-12 22:48:47 +0000 | [diff] [blame] | 2907 | // This tests that the formatter doesn't break after "backing" but before ":", |
| 2908 | // which would be at 80 columns. |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 2909 | verifyFormat( |
| 2910 | "void f() {\n" |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 2911 | " if ((self = [super initWithContentRect:contentRect\n" |
| 2912 | " styleMask:styleMask\n" |
| 2913 | " backing:NSBackingStoreBuffered\n" |
| 2914 | " defer:YES]))"); |
| 2915 | |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 2916 | verifyFormat( |
| 2917 | "[foo checkThatBreakingAfterColonWorksOk:\n" |
| 2918 | " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); |
Daniel Jasper | 63d7ced | 2013-02-05 10:07:47 +0000 | [diff] [blame] | 2919 | |
| 2920 | verifyFormat("[myObj short:arg1 // Force line break\n" |
| 2921 | " longKeyword:arg2\n" |
| 2922 | " evenLongerKeyword:arg3\n" |
| 2923 | " error:arg4];"); |
| 2924 | verifyFormat( |
| 2925 | "void f() {\n" |
| 2926 | " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" |
| 2927 | " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" |
| 2928 | " pos.width(), pos.height())\n" |
| 2929 | " styleMask:NSBorderlessWindowMask\n" |
| 2930 | " backing:NSBackingStoreBuffered\n" |
| 2931 | " defer:NO]);\n" |
| 2932 | "}"); |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 2933 | verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" |
| 2934 | " with:contentsNativeView];"); |
| 2935 | |
| 2936 | verifyFormat( |
| 2937 | "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" |
| 2938 | " owner:nillllll];"); |
| 2939 | |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 2940 | verifyFormat( |
Daniel Jasper | ce3d1a6 | 2013-02-08 08:22:00 +0000 | [diff] [blame] | 2941 | "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" |
Daniel Jasper | 4e77809 | 2013-02-06 10:05:46 +0000 | [diff] [blame] | 2942 | " forType:kBookmarkButtonDragType];"); |
| 2943 | |
| 2944 | verifyFormat("[defaultCenter addObserver:self\n" |
| 2945 | " selector:@selector(willEnterFullscreen)\n" |
| 2946 | " name:kWillEnterFullscreenNotification\n" |
| 2947 | " object:nil];"); |
Daniel Jasper | 9e9e6e0 | 2013-02-06 16:00:26 +0000 | [diff] [blame] | 2948 | verifyFormat("[image_rep drawInRect:drawRect\n" |
| 2949 | " fromRect:NSZeroRect\n" |
| 2950 | " operation:NSCompositeCopy\n" |
| 2951 | " fraction:1.0\n" |
| 2952 | " respectFlipped:NO\n" |
| 2953 | " hints:nil];"); |
Nico Weber | 4ed7f3e | 2013-02-06 16:54:35 +0000 | [diff] [blame] | 2954 | |
| 2955 | verifyFormat( |
| 2956 | "scoped_nsobject<NSTextField> message(\n" |
| 2957 | " // The frame will be fixed up when |-setMessageText:| is called.\n" |
| 2958 | " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); |
Nico Weber | bcfdd26 | 2013-01-12 06:18:40 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 2961 | TEST_F(FormatTest, ObjCAt) { |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2962 | verifyFormat("@autoreleasepool"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 2963 | verifyFormat("@catch"); |
| 2964 | verifyFormat("@class"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2965 | verifyFormat("@compatibility_alias"); |
| 2966 | verifyFormat("@defs"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 2967 | verifyFormat("@dynamic"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2968 | verifyFormat("@encode"); |
| 2969 | verifyFormat("@end"); |
| 2970 | verifyFormat("@finally"); |
| 2971 | verifyFormat("@implementation"); |
| 2972 | verifyFormat("@import"); |
| 2973 | verifyFormat("@interface"); |
| 2974 | verifyFormat("@optional"); |
| 2975 | verifyFormat("@package"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 2976 | verifyFormat("@private"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2977 | verifyFormat("@property"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 2978 | verifyFormat("@protected"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2979 | verifyFormat("@protocol"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 2980 | verifyFormat("@public"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2981 | verifyFormat("@required"); |
| 2982 | verifyFormat("@selector"); |
| 2983 | verifyFormat("@synchronized"); |
| 2984 | verifyFormat("@synthesize"); |
Nico Weber | d0af4b4 | 2013-01-07 16:14:28 +0000 | [diff] [blame] | 2985 | verifyFormat("@throw"); |
| 2986 | verifyFormat("@try"); |
Nico Weber | 4a293a2 | 2013-01-07 16:07:07 +0000 | [diff] [blame] | 2987 | |
Nico Weber | 581f557 | 2013-01-07 15:56:25 +0000 | [diff] [blame] | 2988 | EXPECT_EQ("@interface", format("@ interface")); |
| 2989 | |
| 2990 | // The precise formatting of this doesn't matter, nobody writes code like |
| 2991 | // this. |
| 2992 | verifyFormat("@ /*foo*/ interface"); |
Nico Weber | efcfe73 | 2013-01-07 15:17:23 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 2995 | TEST_F(FormatTest, ObjCSnippets) { |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 2996 | verifyFormat("@autoreleasepool {\n" |
| 2997 | " foo();\n" |
| 2998 | "}"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 2999 | verifyFormat("@class Foo, Bar;"); |
Nico Weber | c31689a | 2013-01-08 19:15:23 +0000 | [diff] [blame] | 3000 | verifyFormat("@compatibility_alias AliasName ExistingClass;"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 3001 | verifyFormat("@dynamic textColor;"); |
Nico Weber | 5dfe9b4 | 2013-01-18 05:11:47 +0000 | [diff] [blame] | 3002 | verifyFormat("char *buf1 = @encode(int *);"); |
| 3003 | verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); |
Nico Weber | 95e8e46 | 2013-02-12 16:17:07 +0000 | [diff] [blame] | 3004 | verifyFormat("char *buf1 = @encode(int **);"); |
Nico Weber | 3a2673e | 2013-01-08 20:16:23 +0000 | [diff] [blame] | 3005 | verifyFormat("Protocol *proto = @protocol(p1);"); |
Nico Weber | 6a21a55 | 2013-01-18 02:43:57 +0000 | [diff] [blame] | 3006 | verifyFormat("SEL s = @selector(foo:);"); |
Manuel Klimek | d5688cf | 2013-01-11 19:17:44 +0000 | [diff] [blame] | 3007 | verifyFormat("@synchronized(self) {\n" |
| 3008 | " f();\n" |
| 3009 | "}"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 3010 | |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 3011 | verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); |
| 3012 | verifyGoogleFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); |
| 3013 | |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 3014 | verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); |
Nico Weber | 7084823 | 2013-01-10 21:30:42 +0000 | [diff] [blame] | 3015 | verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); |
| 3016 | verifyGoogleFormat("@property(assign, getter=isEditable) BOOL editable;"); |
Nico Weber | cf4a79c | 2013-01-08 17:56:31 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
Nico Weber | 712f5b3 | 2013-02-07 00:19:29 +0000 | [diff] [blame] | 3019 | TEST_F(FormatTest, ObjCLiterals) { |
| 3020 | verifyFormat("@\"String\""); |
| 3021 | verifyFormat("@1"); |
| 3022 | verifyFormat("@+4.8"); |
| 3023 | verifyFormat("@-4"); |
| 3024 | verifyFormat("@1LL"); |
| 3025 | verifyFormat("@.5"); |
| 3026 | verifyFormat("@'c'"); |
| 3027 | verifyFormat("@true"); |
| 3028 | |
| 3029 | verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); |
| 3030 | verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); |
| 3031 | verifyFormat("NSNumber *favoriteColor = @(Green);"); |
| 3032 | verifyFormat("NSString *path = @(getenv(\"PATH\"));"); |
| 3033 | |
Nico Weber | 712f5b3 | 2013-02-07 00:19:29 +0000 | [diff] [blame] | 3034 | verifyFormat("@["); |
Nico Weber | 051860e | 2013-02-10 02:08:05 +0000 | [diff] [blame] | 3035 | verifyFormat("@[]"); |
| 3036 | verifyFormat( |
| 3037 | "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); |
| 3038 | verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); |
| 3039 | |
Nico Weber | 712f5b3 | 2013-02-07 00:19:29 +0000 | [diff] [blame] | 3040 | verifyFormat("@{"); |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 3041 | verifyFormat("@{}"); |
| 3042 | verifyFormat("@{ @\"one\" : @1 }"); |
| 3043 | verifyFormat("return @{ @\"one\" : @1 };"); |
| 3044 | verifyFormat("@{ @\"one\" : @1, }"); |
| 3045 | verifyFormat("@{ @\"one\" : @{ @2 : @1 } }"); |
| 3046 | verifyFormat("@{ @\"one\" : @{ @2 : @1 }, }"); |
| 3047 | verifyFormat("@{ 1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2 }"); |
| 3048 | verifyFormat("[self setDict:@{}"); |
| 3049 | verifyFormat("[self setDict:@{ @1 : @2 }"); |
| 3050 | verifyFormat("NSLog(@\"%@\", @{ @1 : @2, @2 : @3 }[@1]);"); |
| 3051 | verifyFormat( |
| 3052 | "NSDictionary *masses = @{ @\"H\" : @1.0078, @\"He\" : @4.0026 };"); |
Nico Weber | e626200 | 2013-02-10 20:39:05 +0000 | [diff] [blame] | 3053 | verifyFormat( |
| 3054 | "NSDictionary *settings = @{ AVEncoderKey : @(AVAudioQualityMax) };"); |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3055 | |
Nico Weber | d74fcdb | 2013-02-10 20:35:35 +0000 | [diff] [blame] | 3056 | // FIXME: Nested and multi-line array and dictionary literals need more work. |
Nico Weber | 0ac717b | 2013-02-10 20:48:24 +0000 | [diff] [blame] | 3057 | verifyFormat( |
| 3058 | "NSDictionary *d = @{ @\"nam\" : NSUserNam(), @\"dte\" : [NSDate date],\n" |
| 3059 | " @\"processInfo\" : [NSProcessInfo processInfo] };"); |
Nico Weber | 712f5b3 | 2013-02-07 00:19:29 +0000 | [diff] [blame] | 3060 | } |
| 3061 | |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 3062 | TEST_F(FormatTest, ReformatRegionAdjustsIndent) { |
| 3063 | EXPECT_EQ("{\n" |
| 3064 | "{\n" |
| 3065 | "a;\n" |
| 3066 | "b;\n" |
| 3067 | "}\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3068 | "}", |
| 3069 | format("{\n" |
| 3070 | "{\n" |
| 3071 | "a;\n" |
| 3072 | " b;\n" |
| 3073 | "}\n" |
| 3074 | "}", |
| 3075 | 13, 2, getLLVMStyle())); |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 3076 | EXPECT_EQ("{\n" |
| 3077 | "{\n" |
| 3078 | " a;\n" |
| 3079 | "b;\n" |
| 3080 | "}\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3081 | "}", |
| 3082 | format("{\n" |
| 3083 | "{\n" |
| 3084 | " a;\n" |
| 3085 | "b;\n" |
| 3086 | "}\n" |
| 3087 | "}", |
| 3088 | 9, 2, getLLVMStyle())); |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 3089 | EXPECT_EQ("{\n" |
| 3090 | "{\n" |
| 3091 | "public:\n" |
| 3092 | " b;\n" |
| 3093 | "}\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3094 | "}", |
| 3095 | format("{\n" |
| 3096 | "{\n" |
| 3097 | "public:\n" |
| 3098 | " b;\n" |
| 3099 | "}\n" |
| 3100 | "}", |
| 3101 | 17, 2, getLLVMStyle())); |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 3102 | EXPECT_EQ("{\n" |
| 3103 | "{\n" |
| 3104 | "a;\n" |
| 3105 | "}\n" |
| 3106 | "{\n" |
| 3107 | " b;\n" |
| 3108 | "}\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3109 | "}", |
| 3110 | format("{\n" |
| 3111 | "{\n" |
| 3112 | "a;\n" |
| 3113 | "}\n" |
| 3114 | "{\n" |
| 3115 | " b;\n" |
| 3116 | "}\n" |
| 3117 | "}", |
| 3118 | 22, 2, getLLVMStyle())); |
Manuel Klimek | 52635ff | 2013-02-08 19:53:32 +0000 | [diff] [blame] | 3119 | EXPECT_EQ(" {\n" |
| 3120 | " a;\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3121 | " }", |
| 3122 | format(" {\n" |
| 3123 | "a;\n" |
| 3124 | " }", |
| 3125 | 4, 2, getLLVMStyle())); |
Daniel Jasper | 9ece2bb | 2013-02-12 16:51:23 +0000 | [diff] [blame] | 3126 | EXPECT_EQ("void f() {}\n" |
Daniel Jasper | 66d19bd | 2013-02-18 11:59:17 +0000 | [diff] [blame] | 3127 | "void g() {}", |
| 3128 | format("void f() {}\n" |
| 3129 | "void g() {}", |
| 3130 | 13, 0, getLLVMStyle())); |
Manuel Klimek | 52635ff | 2013-02-08 19:53:32 +0000 | [diff] [blame] | 3131 | } |
Manuel Klimek | 547d5db | 2013-02-08 17:38:27 +0000 | [diff] [blame] | 3132 | |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 3133 | TEST_F(FormatTest, BreakStringLiterals) { |
| 3134 | EXPECT_EQ("\"some text \"\n" |
| 3135 | "\"other\";", |
| 3136 | format("\"some text other\";", getLLVMStyleWithColumns(12))); |
| 3137 | EXPECT_EQ( |
| 3138 | "#define A \\\n" |
| 3139 | " \"some \" \\\n" |
| 3140 | " \"text \" \\\n" |
| 3141 | " \"other\";", |
| 3142 | format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); |
| 3143 | EXPECT_EQ( |
| 3144 | "#define A \\\n" |
| 3145 | " \"so \" \\\n" |
| 3146 | " \"text \" \\\n" |
| 3147 | " \"other\";", |
| 3148 | format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); |
| 3149 | |
| 3150 | EXPECT_EQ("\"some text\"", |
| 3151 | format("\"some text\"", getLLVMStyleWithColumns(1))); |
| 3152 | EXPECT_EQ("\"some text\"", |
| 3153 | format("\"some text\"", getLLVMStyleWithColumns(11))); |
| 3154 | EXPECT_EQ("\"some \"\n" |
| 3155 | "\"text\"", |
| 3156 | format("\"some text\"", getLLVMStyleWithColumns(10))); |
| 3157 | EXPECT_EQ("\"some \"\n" |
| 3158 | "\"text\"", |
| 3159 | format("\"some text\"", getLLVMStyleWithColumns(7))); |
Manuel Klimek | af31fd7 | 2013-03-01 13:14:08 +0000 | [diff] [blame] | 3160 | EXPECT_EQ("\"some\"\n" |
| 3161 | "\" text\"", |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 3162 | format("\"some text\"", getLLVMStyleWithColumns(6))); |
Manuel Klimek | 0090591 | 2013-03-04 20:03:38 +0000 | [diff] [blame] | 3163 | EXPECT_EQ("\"some\"\n" |
| 3164 | "\" tex\"\n" |
| 3165 | "\" and\"", |
| 3166 | format("\"some tex and\"", getLLVMStyleWithColumns(6))); |
| 3167 | EXPECT_EQ("\"some\"\n" |
| 3168 | "\"/tex\"\n" |
| 3169 | "\"/and\"", |
| 3170 | format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 3171 | |
| 3172 | EXPECT_EQ("variable =\n" |
| 3173 | " \"long string \"\n" |
| 3174 | " \"literal\";", |
| 3175 | format("variable = \"long string literal\";", |
| 3176 | getLLVMStyleWithColumns(20))); |
| 3177 | |
| 3178 | EXPECT_EQ("variable = f(\n" |
| 3179 | " \"long string \"\n" |
Daniel Jasper | faab0d3 | 2013-02-27 09:47:53 +0000 | [diff] [blame] | 3180 | " \"literal\",\n" |
| 3181 | " short,\n" |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 3182 | " loooooooooooooooooooong);", |
| 3183 | format("variable = f(\"long string literal\", short, " |
| 3184 | "loooooooooooooooooooong);", |
| 3185 | getLLVMStyleWithColumns(20))); |
| 3186 | EXPECT_EQ( |
| 3187 | "f(\"one two\".split(\n" |
| 3188 | " variable));", |
| 3189 | format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); |
| 3190 | EXPECT_EQ("f(\"one two three four five six \"\n" |
| 3191 | " \"seven\".split(\n" |
| 3192 | " really_looooong_variable));", |
| 3193 | format("f(\"one two three four five six seven\"." |
| 3194 | "split(really_looooong_variable));", |
| 3195 | getLLVMStyleWithColumns(33))); |
| 3196 | |
| 3197 | EXPECT_EQ("f(\"some \"\n" |
| 3198 | " \"text\",\n" |
| 3199 | " other);", |
| 3200 | format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); |
Daniel Jasper | 0fb382b | 2013-02-26 12:52:34 +0000 | [diff] [blame] | 3201 | |
| 3202 | // Only break as a last resort. |
| 3203 | verifyFormat( |
| 3204 | "aaaaaaaaaaaaaaaaaaaa(\n" |
| 3205 | " aaaaaaaaaaaaaaaaaaaa,\n" |
| 3206 | " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); |
Manuel Klimek | af31fd7 | 2013-03-01 13:14:08 +0000 | [diff] [blame] | 3207 | |
| 3208 | EXPECT_EQ( |
| 3209 | "\"splitmea\"\n" |
Manuel Klimek | bc30c71 | 2013-03-01 13:29:19 +0000 | [diff] [blame] | 3210 | "\"trandomp\"\n" |
| 3211 | "\"oint\"", |
Manuel Klimek | af31fd7 | 2013-03-01 13:14:08 +0000 | [diff] [blame] | 3212 | format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); |
Manuel Klimek | bc30c71 | 2013-03-01 13:29:19 +0000 | [diff] [blame] | 3213 | |
| 3214 | EXPECT_EQ( |
| 3215 | "\"split/\"\n" |
| 3216 | "\"pathat/\"\n" |
| 3217 | "\"slashes\"", |
| 3218 | format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); |
Manuel Klimek | 8092a94 | 2013-02-20 10:15:13 +0000 | [diff] [blame] | 3219 | } |
| 3220 | |
Manuel Klimek | aa62d0c | 2013-03-08 18:59:48 +0000 | [diff] [blame] | 3221 | TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { |
| 3222 | EXPECT_EQ("\"\\a\"", |
| 3223 | format("\"\\a\"", getLLVMStyleWithColumns(3))); |
| 3224 | EXPECT_EQ("\"\\\"", |
| 3225 | format("\"\\\"", getLLVMStyleWithColumns(2))); |
| 3226 | EXPECT_EQ("\"test\"\n" |
| 3227 | "\"\\n\"", |
| 3228 | format("\"test\\n\"", getLLVMStyleWithColumns(7))); |
| 3229 | EXPECT_EQ("\"tes\\\\\"\n" |
| 3230 | "\"n\"", |
| 3231 | format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); |
| 3232 | EXPECT_EQ("\"\\\\\\\\\"\n" |
| 3233 | "\"\\n\"", |
| 3234 | format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); |
| 3235 | EXPECT_EQ("\"\\uff01\"", |
| 3236 | format("\"\\uff01\"", getLLVMStyleWithColumns(7))); |
| 3237 | EXPECT_EQ("\"\\uff01\"\n" |
| 3238 | "\"test\"", |
| 3239 | format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); |
| 3240 | EXPECT_EQ("\"\\Uff01ff02\"", |
| 3241 | format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); |
| 3242 | EXPECT_EQ("\"\\x000000000001\"\n" |
| 3243 | "\"next\"", |
| 3244 | format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); |
| 3245 | EXPECT_EQ("\"\\x000000000001next\"", |
| 3246 | format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); |
| 3247 | EXPECT_EQ("\"\\x000000000001\"", |
| 3248 | format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); |
| 3249 | EXPECT_EQ("\"test\"\n" |
| 3250 | "\"\\000000\"\n" |
| 3251 | "\"000001\"", |
| 3252 | format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); |
| 3253 | EXPECT_EQ("\"test\\000\"\n" |
| 3254 | "\"000000001\"", |
| 3255 | format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); |
| 3256 | EXPECT_EQ("R\"(\\x\\x00)\"\n", |
| 3257 | format("R\"(\\x\\x00)\"\n", getLLVMStyleWithColumns(7))); |
| 3258 | } |
| 3259 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 3260 | } // end namespace tooling |
| 3261 | } // end namespace clang |