| 1 |
//===- unittest/Format/BracesInserterTest.cpp -----------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "clang/Format/Format.h" |
| 10 |
|
| 11 |
#include "../Tooling/ReplacementTest.h" |
| 12 |
#include "FormatTestUtils.h" |
| 13 |
|
| 14 |
#define DEBUG_TYPE "braces-inserter-test" |
| 15 |
|
| 16 |
namespace clang { |
| 17 |
namespace format { |
| 18 |
namespace { |
| 19 |
|
| 20 |
class BracesInserterTest : public ::testing::Test { |
| 21 |
protected: |
| 22 |
std::string format(llvm::StringRef Code, const FormatStyle &Style, |
| 23 |
const std::vector<tooling::Range> &Ranges) { |
| 24 |
LLVM_DEBUG(llvm::errs() << "---\n"); |
| 25 |
LLVM_DEBUG(llvm::errs() << Code << "\n\n"); |
| 26 |
auto NonEmptyRanges = Ranges; |
| 27 |
if (Ranges.empty()) |
| 28 |
NonEmptyRanges = {1, tooling::Range(0, Code.size())}; |
| 29 |
FormattingAttemptStatus Status; |
| 30 |
tooling::Replacements Replaces = |
| 31 |
reformat(Style, Code, NonEmptyRanges, "<stdin>", &Status); |
| 32 |
EXPECT_EQ(true, Status.FormatComplete) << Code << "\n\n"; |
| 33 |
ReplacementCount = Replaces.size(); |
| 34 |
auto Result = applyAllReplacements(Code, Replaces); |
| 35 |
EXPECT_TRUE(static_cast<bool>(Result)); |
| 36 |
LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); |
| 37 |
return *Result; |
| 38 |
} |
| 39 |
|
| 40 |
void _verifyFormat(const char *File, int Line, llvm::StringRef Expected, |
| 41 |
llvm::StringRef Code, |
| 42 |
const FormatStyle &Style = getLLVMStyle(), |
| 43 |
const std::vector<tooling::Range> &Ranges = {}) { |
| 44 |
testing::ScopedTrace t(File, Line, ::testing::Message() << Code.str()); |
| 45 |
EXPECT_EQ(Expected.str(), format(Expected, Style, Ranges)) |
| 46 |
<< "Expected code is not stable"; |
| 47 |
EXPECT_EQ(Expected.str(), format(Code, Style, Ranges)); |
| 48 |
if (Style.Language == FormatStyle::LK_Cpp && Ranges.empty()) { |
| 49 |
// Objective-C++ is a superset of C++, so everything checked for C++ |
| 50 |
// needs to be checked for Objective-C++ as well. |
| 51 |
FormatStyle ObjCStyle = Style; |
| 52 |
ObjCStyle.Language = FormatStyle::LK_ObjC; |
| 53 |
EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle, Ranges)); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
void _verifyFormat(const char *File, int Line, llvm::StringRef Code, |
| 58 |
const FormatStyle &Style = getLLVMStyle(), |
| 59 |
const std::vector<tooling::Range> &Ranges = {}) { |
| 60 |
_verifyFormat(File, Line, Code, Code, Style, Ranges); |
| 61 |
} |
| 62 |
|
| 63 |
int ReplacementCount; |
| 64 |
}; |
| 65 |
|
| 66 |
#define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__) |
| 67 |
|
| 68 |
TEST_F(BracesInserterTest, InsertBraces) { |
| 69 |
FormatStyle Style = getLLVMStyle(); |
| 70 |
Style.InsertBraces = true; |
| 71 |
|
| 72 |
verifyFormat("// clang-format off\n" |
| 73 |
"// comment\n" |
| 74 |
"if (a) f();\n" |
| 75 |
"// clang-format on\n" |
| 76 |
"if (b) {\n" |
| 77 |
" g();\n" |
| 78 |
"}", |
| 79 |
"// clang-format off\n" |
| 80 |
"// comment\n" |
| 81 |
"if (a) f();\n" |
| 82 |
"// clang-format on\n" |
| 83 |
"if (b) g();", |
| 84 |
Style); |
| 85 |
|
| 86 |
verifyFormat("if (a) {\n" |
| 87 |
" switch (b) {\n" |
| 88 |
" case 1:\n" |
| 89 |
" c = 0;\n" |
| 90 |
" break;\n" |
| 91 |
" default:\n" |
| 92 |
" c = 1;\n" |
| 93 |
" }\n" |
| 94 |
"}", |
| 95 |
"if (a)\n" |
| 96 |
" switch (b) {\n" |
| 97 |
" case 1:\n" |
| 98 |
" c = 0;\n" |
| 99 |
" break;\n" |
| 100 |
" default:\n" |
| 101 |
" c = 1;\n" |
| 102 |
" }", |
| 103 |
Style); |
| 104 |
|
| 105 |
verifyFormat("for (auto node : nodes) {\n" |
| 106 |
" if (node) {\n" |
| 107 |
" break;\n" |
| 108 |
" }\n" |
| 109 |
"}", |
| 110 |
"for (auto node : nodes)\n" |
| 111 |
" if (node)\n" |
| 112 |
" break;", |
| 113 |
Style); |
| 114 |
|
| 115 |
verifyFormat("for (auto node : nodes) {\n" |
| 116 |
" if (node)\n" |
| 117 |
"}", |
| 118 |
"for (auto node : nodes)\n" |
| 119 |
" if (node)", |
| 120 |
Style); |
| 121 |
|
| 122 |
verifyFormat("do {\n" |
| 123 |
" --a;\n" |
| 124 |
"} while (a);", |
| 125 |
"do\n" |
| 126 |
" --a;\n" |
| 127 |
"while (a);", |
| 128 |
Style); |
| 129 |
|
| 130 |
verifyFormat("if (i) {\n" |
| 131 |
" ++i;\n" |
| 132 |
"} else {\n" |
| 133 |
" --i;\n" |
| 134 |
"}", |
| 135 |
"if (i)\n" |
| 136 |
" ++i;\n" |
| 137 |
"else {\n" |
| 138 |
" --i;\n" |
| 139 |
"}", |
| 140 |
Style); |
| 141 |
|
| 142 |
verifyFormat("void f() {\n" |
| 143 |
" while (j--) {\n" |
| 144 |
" while (i) {\n" |
| 145 |
" --i;\n" |
| 146 |
" }\n" |
| 147 |
" }\n" |
| 148 |
"}", |
| 149 |
"void f() {\n" |
| 150 |
" while (j--)\n" |
| 151 |
" while (i)\n" |
| 152 |
" --i;\n" |
| 153 |
"}", |
| 154 |
Style); |
| 155 |
|
| 156 |
verifyFormat("f({\n" |
| 157 |
" if (a) {\n" |
| 158 |
" g();\n" |
| 159 |
" }\n" |
| 160 |
"});", |
| 161 |
"f({\n" |
| 162 |
" if (a)\n" |
| 163 |
" g();\n" |
| 164 |
"});", |
| 165 |
Style); |
| 166 |
|
| 167 |
verifyFormat("if (a) {\n" |
| 168 |
" f();\n" |
| 169 |
"} else if (b) {\n" |
| 170 |
" g();\n" |
| 171 |
"} else {\n" |
| 172 |
" h();\n" |
| 173 |
"}", |
| 174 |
"if (a)\n" |
| 175 |
" f();\n" |
| 176 |
"else if (b)\n" |
| 177 |
" g();\n" |
| 178 |
"else\n" |
| 179 |
" h();", |
| 180 |
Style); |
| 181 |
|
| 182 |
verifyFormat("if (a) {\n" |
| 183 |
" f();\n" |
| 184 |
"}\n" |
| 185 |
"// comment\n" |
| 186 |
"/* comment */", |
| 187 |
"if (a)\n" |
| 188 |
" f();\n" |
| 189 |
"// comment\n" |
| 190 |
"/* comment */", |
| 191 |
Style); |
| 192 |
|
| 193 |
verifyFormat("if (a) {\n" |
| 194 |
" // foo\n" |
| 195 |
" // bar\n" |
| 196 |
" f();\n" |
| 197 |
"}", |
| 198 |
"if (a)\n" |
| 199 |
" // foo\n" |
| 200 |
" // bar\n" |
| 201 |
" f();", |
| 202 |
Style); |
| 203 |
|
| 204 |
verifyFormat("if (a) { //\n" |
| 205 |
" b = 1;\n" |
| 206 |
"}", |
| 207 |
"if (a) //\n" |
| 208 |
" b = 1;", |
| 209 |
Style); |
| 210 |
|
| 211 |
verifyFormat("if (a) { // comment\n" |
| 212 |
" // comment\n" |
| 213 |
" f();\n" |
| 214 |
"}", |
| 215 |
"if (a) // comment\n" |
| 216 |
" // comment\n" |
| 217 |
" f();", |
| 218 |
Style); |
| 219 |
|
| 220 |
verifyFormat("if (a) {\n" |
| 221 |
" f(); // comment\n" |
| 222 |
"}", |
| 223 |
"if (a)\n" |
| 224 |
" f(); // comment", |
| 225 |
Style); |
| 226 |
|
| 227 |
verifyFormat("if (a) {\n" |
| 228 |
" f();\n" |
| 229 |
"}\n" |
| 230 |
"#undef A\n" |
| 231 |
"#undef B", |
| 232 |
"if (a)\n" |
| 233 |
" f();\n" |
| 234 |
"#undef A\n" |
| 235 |
"#undef B", |
| 236 |
Style); |
| 237 |
|
| 238 |
verifyFormat("if (a)\n" |
| 239 |
"#ifdef A\n" |
| 240 |
" f();\n" |
| 241 |
"#else\n" |
| 242 |
" g();\n" |
| 243 |
"#endif", |
| 244 |
Style); |
| 245 |
|
| 246 |
verifyFormat("#if 0\n" |
| 247 |
"#elif 1\n" |
| 248 |
"#endif\n" |
| 249 |
"void f() {\n" |
| 250 |
" if (a) {\n" |
| 251 |
" g();\n" |
| 252 |
" }\n" |
| 253 |
"}", |
| 254 |
"#if 0\n" |
| 255 |
"#elif 1\n" |
| 256 |
"#endif\n" |
| 257 |
"void f() {\n" |
| 258 |
" if (a) g();\n" |
| 259 |
"}", |
| 260 |
Style); |
| 261 |
|
| 262 |
verifyFormat("do {\n" |
| 263 |
"#if 0\n" |
| 264 |
"#else\n" |
| 265 |
" if (b) {\n" |
| 266 |
"#endif\n" |
| 267 |
" }\n" |
| 268 |
"} while (0);", |
| 269 |
Style); |
| 270 |
|
| 271 |
Style.RemoveBracesLLVM = true; |
| 272 |
verifyFormat("if (a) //\n" |
| 273 |
" return b;", |
| 274 |
Style); |
| 275 |
Style.RemoveBracesLLVM = false; |
| 276 |
|
| 277 |
Style.ColumnLimit = 15; |
| 278 |
|
| 279 |
verifyFormat("#define A \\\n" |
| 280 |
" if (a) \\\n" |
| 281 |
" f();", |
| 282 |
Style); |
| 283 |
|
| 284 |
verifyFormat("if (a + b >\n" |
| 285 |
" c) {\n" |
| 286 |
" f();\n" |
| 287 |
"}", |
| 288 |
"if (a + b > c)\n" |
| 289 |
" f();", |
| 290 |
Style); |
| 291 |
|
| 292 |
Style.BreakBeforeBraces = FormatStyle::BS_Custom; |
| 293 |
Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; |
| 294 |
|
| 295 |
verifyFormat("if (a) //\n" |
| 296 |
"{\n" |
| 297 |
" b = 1;\n" |
| 298 |
"}", |
| 299 |
"if (a) //\n" |
| 300 |
" b = 1;", |
| 301 |
Style); |
| 302 |
} |
| 303 |
|
| 304 |
TEST_F(BracesInserterTest, InsertBracesRange) { |
| 305 |
FormatStyle Style = getLLVMStyle(); |
| 306 |
Style.InsertBraces = true; |
| 307 |
|
| 308 |
const StringRef Code("while (a)\n" |
| 309 |
" if (b)\n" |
| 310 |
" return;"); |
| 311 |
|
| 312 |
verifyFormat("while (a) {\n" |
| 313 |
" if (b)\n" |
| 314 |
" return;\n" |
| 315 |
"}", |
| 316 |
Code, Style, {tooling::Range(0, 9)}); // line 1 |
| 317 |
|
| 318 |
verifyFormat("while (a) {\n" |
| 319 |
" if (b) {\n" |
| 320 |
" return;\n" |
| 321 |
" }\n" |
| 322 |
"}", |
| 323 |
Code, Style, {tooling::Range(0, 18)}); // lines 1-2 |
| 324 |
|
| 325 |
verifyFormat("while (a)\n" |
| 326 |
" if (b) {\n" |
| 327 |
" return;\n" |
| 328 |
" }", |
| 329 |
Code, Style, {tooling::Range(10, 8)}); // line 2 |
| 330 |
|
| 331 |
verifyFormat(Code, Style, {tooling::Range(19, 11)}); // line 3 |
| 332 |
} |
| 333 |
|
| 334 |
} // namespace |
| 335 |
} // namespace format |
| 336 |
} // namespace clang |