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