1 | //===- NamespaceEndCommentsFixerTest.cpp - Formatting unit tests ----------===// |
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 "llvm/Support/Debug.h" |
12 | #include "gtest/gtest.h" |
13 | |
14 | #define DEBUG_TYPE "namespace-end-comments-fixer-test" |
15 | |
16 | namespace clang { |
17 | namespace format { |
18 | namespace { |
19 | |
20 | class : public ::testing::Test { |
21 | protected: |
22 | std::string |
23 | (llvm::StringRef Code, |
24 | const std::vector<tooling::Range> &Ranges, |
25 | const FormatStyle &Style = getLLVMStyle()) { |
26 | LLVM_DEBUG(llvm::errs() << "---\n" ); |
27 | LLVM_DEBUG(llvm::errs() << Code << "\n\n" ); |
28 | tooling::Replacements Replaces = |
29 | clang::format::fixNamespaceEndComments(Style, Code, Ranges, FileName: "<stdin>" ); |
30 | auto Result = applyAllReplacements(Code, Replaces); |
31 | EXPECT_TRUE(static_cast<bool>(Result)); |
32 | LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n" ); |
33 | return *Result; |
34 | } |
35 | |
36 | std::string |
37 | (llvm::StringRef Code, |
38 | const FormatStyle &Style = getLLVMStyle()) { |
39 | return fixNamespaceEndComments( |
40 | Code, |
41 | /*Ranges=*/{1, tooling::Range(0, Code.size())}, Style); |
42 | } |
43 | |
44 | bool (StringRef Code, const std::vector<tooling::Range> &Ranges, |
45 | const FormatStyle &Style = getLLVMStyle()) const { |
46 | return clang::format::fixNamespaceEndComments(Style, Code, Ranges, |
47 | FileName: "<stdin>" ) |
48 | .empty(); |
49 | } |
50 | |
51 | bool (StringRef Code, |
52 | const FormatStyle &Style = getLLVMStyle()) const { |
53 | return isFormatted(Code, Ranges: {1, tooling::Range(0, Code.size())}, Style); |
54 | } |
55 | }; |
56 | |
57 | TEST_F(NamespaceEndCommentsFixerTest, AddsEndComment) { |
58 | EXPECT_EQ("namespace {\n" |
59 | "int i;\n" |
60 | "int j;\n" |
61 | "} // namespace" , |
62 | fixNamespaceEndComments("namespace {\n" |
63 | "int i;\n" |
64 | "int j;\n" |
65 | "}" )); |
66 | |
67 | EXPECT_EQ("namespace {\n" |
68 | "int i;\n" |
69 | "int j;\n" |
70 | "} // namespace" , |
71 | fixNamespaceEndComments("namespace {\n" |
72 | "int i;\n" |
73 | "int j;\n" |
74 | "}" )); |
75 | EXPECT_EQ("namespace A {\n" |
76 | "int i;\n" |
77 | "int j;\n" |
78 | "} // namespace A" , |
79 | fixNamespaceEndComments("namespace A {\n" |
80 | "int i;\n" |
81 | "int j;\n" |
82 | "}" )); |
83 | EXPECT_EQ("#define M(x) x##x\n" |
84 | "namespace M(x) {\n" |
85 | "int i;\n" |
86 | "int j;\n" |
87 | "} // namespace M(x)" , |
88 | fixNamespaceEndComments("#define M(x) x##x\n" |
89 | "namespace M(x) {\n" |
90 | "int i;\n" |
91 | "int j;\n" |
92 | "}" )); |
93 | EXPECT_EQ("#define M(x) x##x\n" |
94 | "namespace A::M(x) {\n" |
95 | "int i;\n" |
96 | "int j;\n" |
97 | "} // namespace A::M(x)" , |
98 | fixNamespaceEndComments("#define M(x) x##x\n" |
99 | "namespace A::M(x) {\n" |
100 | "int i;\n" |
101 | "int j;\n" |
102 | "}" )); |
103 | EXPECT_EQ("#define M(x) x##x\n" |
104 | "namespace M(x)::A {\n" |
105 | "int i;\n" |
106 | "int j;\n" |
107 | "} // namespace M(x)::A" , |
108 | fixNamespaceEndComments("#define M(x) x##x\n" |
109 | "namespace M(x)::A {\n" |
110 | "int i;\n" |
111 | "int j;\n" |
112 | "}" )); |
113 | EXPECT_EQ("#define M(x) x##x\n" |
114 | "namespace A::inline M(x)::B {\n" |
115 | "int i;\n" |
116 | "int j;\n" |
117 | "} // namespace A::inline M(x)::B" , |
118 | fixNamespaceEndComments("#define M(x) x##x\n" |
119 | "namespace A::inline M(x)::B {\n" |
120 | "int i;\n" |
121 | "int j;\n" |
122 | "}" )); |
123 | EXPECT_EQ("#define M(x) x##x\n" |
124 | "namespace [[deprecated(\"foo\")]] A::inline M(x)::A {\n" |
125 | "int i;\n" |
126 | "int j;\n" |
127 | "} // namespace A::inline M(x)::A" , |
128 | fixNamespaceEndComments( |
129 | "#define M(x) x##x\n" |
130 | "namespace [[deprecated(\"foo\")]] A::inline M(x)::A {\n" |
131 | "int i;\n" |
132 | "int j;\n" |
133 | "}" )); |
134 | EXPECT_EQ( |
135 | "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ A {\n" |
136 | "int i;\n" |
137 | "int j;\n" |
138 | "} // namespace A" , |
139 | fixNamespaceEndComments( |
140 | "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ A {\n" |
141 | "int i;\n" |
142 | "int j;\n" |
143 | "}" )); |
144 | EXPECT_EQ("namespace /* comment */ [[deprecated(\"foo\")]] A {\n" |
145 | "int i;\n" |
146 | "int j;\n" |
147 | "} // namespace A" , |
148 | fixNamespaceEndComments( |
149 | "namespace /* comment */ [[deprecated(\"foo\")]] A {\n" |
150 | "int i;\n" |
151 | "int j;\n" |
152 | "}" )); |
153 | EXPECT_EQ( |
154 | "#define M(x) x##x\n" |
155 | "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ M(x) {\n" |
156 | "int i;\n" |
157 | "int j;\n" |
158 | "} // namespace M(x)" , |
159 | fixNamespaceEndComments("#define M(x) x##x\n" |
160 | "namespace /* comment */ " |
161 | "[[deprecated(\"foo\")]] /* comment */ M(x) {\n" |
162 | "int i;\n" |
163 | "int j;\n" |
164 | "}" )); |
165 | EXPECT_EQ("#define M(x) x##x\n" |
166 | "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ " |
167 | "A::M(x) {\n" |
168 | "int i;\n" |
169 | "int j;\n" |
170 | "} // namespace A::M(x)" , |
171 | fixNamespaceEndComments( |
172 | "#define M(x) x##x\n" |
173 | "namespace /* comment */ " |
174 | "[[deprecated(\"foo\")]] /* comment */ A::M(x) {\n" |
175 | "int i;\n" |
176 | "int j;\n" |
177 | "}" )); |
178 | EXPECT_EQ("#define M(x) x##x\n" |
179 | "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ " |
180 | "M(x) /* comment */ {\n" |
181 | "int i;\n" |
182 | "int j;\n" |
183 | "} // namespace M(x)" , |
184 | fixNamespaceEndComments( |
185 | "#define M(x) x##x\n" |
186 | "namespace /* comment */ [[deprecated(\"foo\")]] /* comment " |
187 | "*/ M(x) /* comment */ {\n" |
188 | "int i;\n" |
189 | "int j;\n" |
190 | "}" )); |
191 | EXPECT_EQ("#define M(x) x##x\n" |
192 | "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ " |
193 | "A::M(x) /* comment */ {\n" |
194 | "int i;\n" |
195 | "int j;\n" |
196 | "} // namespace A::M(x)" , |
197 | fixNamespaceEndComments( |
198 | "#define M(x) x##x\n" |
199 | "namespace /* comment */ [[deprecated(\"foo\")]] /* comment " |
200 | "*/ A::M(x) /* comment */ {\n" |
201 | "int i;\n" |
202 | "int j;\n" |
203 | "}" )); |
204 | EXPECT_EQ("#define M(x) x##x\n" |
205 | "namespace A M(x) {\n" |
206 | "int i;\n" |
207 | "int j;\n" |
208 | "} // namespace A M(x)" , |
209 | fixNamespaceEndComments("#define M(x) x##x\n" |
210 | "namespace A M(x) {\n" |
211 | "int i;\n" |
212 | "int j;\n" |
213 | "}" )); |
214 | EXPECT_EQ( |
215 | "#define B __attribute__((availability(macos, introduced=10.15)))\n" |
216 | "namespace A B {\n" |
217 | "int i;\n" |
218 | "int j;\n" |
219 | "} // namespace A B" , |
220 | fixNamespaceEndComments( |
221 | "#define B __attribute__((availability(macos, introduced=10.15)))\n" |
222 | "namespace A B {\n" |
223 | "int i;\n" |
224 | "int j;\n" |
225 | "}" )); |
226 | EXPECT_EQ("#define M(x) x##x\n" |
227 | "namespace A::B M(x) {\n" |
228 | "int i;\n" |
229 | "int j;\n" |
230 | "} // namespace A::B" , |
231 | fixNamespaceEndComments("#define M(x) x##x\n" |
232 | "namespace A::B M(x) {\n" |
233 | "int i;\n" |
234 | "int j;\n" |
235 | "}" )); |
236 | EXPECT_EQ( |
237 | "namespace A __attribute__((availability(macos, introduced=10.15))) {\n" |
238 | "int i;\n" |
239 | "int j;\n" |
240 | "} // namespace A" , |
241 | fixNamespaceEndComments( |
242 | "namespace A __attribute__((availability(macos, introduced=10.15))) " |
243 | "{\n" |
244 | "int i;\n" |
245 | "int j;\n" |
246 | "}" )); |
247 | EXPECT_EQ("inline namespace A {\n" |
248 | "int i;\n" |
249 | "int j;\n" |
250 | "} // namespace A" , |
251 | fixNamespaceEndComments("inline namespace A {\n" |
252 | "int i;\n" |
253 | "int j;\n" |
254 | "}" )); |
255 | |
256 | EXPECT_EQ("namespace [[deprecated(\"foo\")]] A::B {\n" |
257 | "int i;\n" |
258 | "int j;\n" |
259 | "} // namespace A::B" , |
260 | fixNamespaceEndComments("namespace [[deprecated(\"foo\")]] A::B {\n" |
261 | "int i;\n" |
262 | "int j;\n" |
263 | "}" )); |
264 | |
265 | EXPECT_EQ("namespace [[deprecated(\"foo\")]] A::inline B::inline C {\n" |
266 | "int i;\n" |
267 | "int j;\n" |
268 | "} // namespace A::inline B::inline C" , |
269 | fixNamespaceEndComments( |
270 | "namespace [[deprecated(\"foo\")]] A::inline B::inline C {\n" |
271 | "int i;\n" |
272 | "int j;\n" |
273 | "}" )); |
274 | |
275 | EXPECT_EQ("namespace DEPRECATED A::B {\n" |
276 | "int i;\n" |
277 | "int j;\n" |
278 | "} // namespace A::B" , |
279 | fixNamespaceEndComments("namespace DEPRECATED A::B {\n" |
280 | "int i;\n" |
281 | "int j;\n" |
282 | "}" )); |
283 | |
284 | EXPECT_EQ("inline namespace [[deprecated]] A {\n" |
285 | "int i;\n" |
286 | "int j;\n" |
287 | "} // namespace A" , |
288 | fixNamespaceEndComments("inline namespace [[deprecated]] A {\n" |
289 | "int i;\n" |
290 | "int j;\n" |
291 | "}" )); |
292 | |
293 | EXPECT_EQ("namespace ::A {\n" |
294 | "int i;\n" |
295 | "int j;\n" |
296 | "} // namespace ::A" , |
297 | fixNamespaceEndComments("namespace ::A {\n" |
298 | "int i;\n" |
299 | "int j;\n" |
300 | "}" )); |
301 | EXPECT_EQ("namespace ::A::B {\n" |
302 | "int i;\n" |
303 | "int j;\n" |
304 | "} // namespace ::A::B" , |
305 | fixNamespaceEndComments("namespace ::A::B {\n" |
306 | "int i;\n" |
307 | "int j;\n" |
308 | "}" )); |
309 | EXPECT_EQ("namespace /**/::/**/A/**/::/**/B/**/ {\n" |
310 | "int i;\n" |
311 | "int j;\n" |
312 | "} // namespace ::A::B" , |
313 | fixNamespaceEndComments("namespace /**/::/**/A/**/::/**/B/**/ {\n" |
314 | "int i;\n" |
315 | "int j;\n" |
316 | "}" )); |
317 | EXPECT_EQ("namespace A {\n" |
318 | "namespace B {\n" |
319 | "int i;\n" |
320 | "}\n" |
321 | "} // namespace A" , |
322 | fixNamespaceEndComments("namespace A {\n" |
323 | "namespace B {\n" |
324 | "int i;\n" |
325 | "}\n" |
326 | "}" )); |
327 | EXPECT_EQ("namespace A {\n" |
328 | "namespace B {\n" |
329 | "int i;\n" |
330 | "int j;\n" |
331 | "} // namespace B\n" |
332 | "} // namespace A" , |
333 | fixNamespaceEndComments("namespace A {\n" |
334 | "namespace B {\n" |
335 | "int i;\n" |
336 | "int j;\n" |
337 | "}\n" |
338 | "}" )); |
339 | EXPECT_EQ("namespace A {\n" |
340 | "int a;\n" |
341 | "int b;\n" |
342 | "} // namespace A\n" |
343 | "namespace B {\n" |
344 | "int b;\n" |
345 | "int a;\n" |
346 | "} // namespace B" , |
347 | fixNamespaceEndComments("namespace A {\n" |
348 | "int a;\n" |
349 | "int b;\n" |
350 | "}\n" |
351 | "namespace B {\n" |
352 | "int b;\n" |
353 | "int a;\n" |
354 | "}" )); |
355 | EXPECT_EQ("namespace A {\n" |
356 | "int a1;\n" |
357 | "int a2;\n" |
358 | "} // namespace A\n" |
359 | "namespace A {\n" |
360 | "int a2;\n" |
361 | "int a1;\n" |
362 | "} // namespace A" , |
363 | fixNamespaceEndComments("namespace A {\n" |
364 | "int a1;\n" |
365 | "int a2;\n" |
366 | "}\n" |
367 | "namespace A {\n" |
368 | "int a2;\n" |
369 | "int a1;\n" |
370 | "}" )); |
371 | EXPECT_EQ("namespace A {\n" |
372 | "int a;\n" |
373 | "int b;\n" |
374 | "} // namespace A\n" |
375 | "// comment about b\n" |
376 | "int b;" , |
377 | fixNamespaceEndComments("namespace A {\n" |
378 | "int a;\n" |
379 | "int b;\n" |
380 | "}\n" |
381 | "// comment about b\n" |
382 | "int b;" )); |
383 | |
384 | EXPECT_EQ("namespace A {\n" |
385 | "namespace B {\n" |
386 | "namespace C {\n" |
387 | "namespace D {\n" |
388 | "}\n" |
389 | "} // namespace C\n" |
390 | "} // namespace B\n" |
391 | "} // namespace A" , |
392 | fixNamespaceEndComments("namespace A {\n" |
393 | "namespace B {\n" |
394 | "namespace C {\n" |
395 | "namespace D {\n" |
396 | "}\n" |
397 | "}\n" |
398 | "}\n" |
399 | "}" )); |
400 | |
401 | // Add comment for namespaces which will be 'compacted' |
402 | FormatStyle CompactNamespacesStyle = getLLVMStyle(); |
403 | CompactNamespacesStyle.CompactNamespaces = true; |
404 | EXPECT_EQ("namespace out { namespace in {\n" |
405 | "int i;\n" |
406 | "int j;\n" |
407 | "}} // namespace out::in" , |
408 | fixNamespaceEndComments("namespace out { namespace in {\n" |
409 | "int i;\n" |
410 | "int j;\n" |
411 | "}}" , |
412 | CompactNamespacesStyle)); |
413 | EXPECT_EQ("namespace out {\n" |
414 | "namespace in {\n" |
415 | "int i;\n" |
416 | "int j;\n" |
417 | "}\n" |
418 | "} // namespace out::in" , |
419 | fixNamespaceEndComments("namespace out {\n" |
420 | "namespace in {\n" |
421 | "int i;\n" |
422 | "int j;\n" |
423 | "}\n" |
424 | "}" , |
425 | CompactNamespacesStyle)); |
426 | EXPECT_EQ("namespace out { namespace in {\n" |
427 | "int i;\n" |
428 | "int j;\n" |
429 | "};} // namespace out::in" , |
430 | fixNamespaceEndComments("namespace out { namespace in {\n" |
431 | "int i;\n" |
432 | "int j;\n" |
433 | "};}" , |
434 | CompactNamespacesStyle)); |
435 | EXPECT_EQ("namespace out { namespace {\n" |
436 | "int i;\n" |
437 | "int j;\n" |
438 | "}} // namespace out" , |
439 | fixNamespaceEndComments("namespace out { namespace {\n" |
440 | "int i;\n" |
441 | "int j;\n" |
442 | "}}" , |
443 | CompactNamespacesStyle)); |
444 | |
445 | // Adds an end comment after a semicolon. |
446 | EXPECT_EQ("namespace {\n" |
447 | "int i;\n" |
448 | "int j;\n" |
449 | "}; // namespace" , |
450 | fixNamespaceEndComments("namespace {\n" |
451 | "int i;\n" |
452 | "int j;\n" |
453 | "};" )); |
454 | EXPECT_EQ("namespace A {\n" |
455 | "int i;\n" |
456 | "int j;\n" |
457 | "}; // namespace A" , |
458 | fixNamespaceEndComments("namespace A {\n" |
459 | "int i;\n" |
460 | "int j;\n" |
461 | "};" )); |
462 | EXPECT_EQ("namespace A {\n" |
463 | "int i;\n" |
464 | "int j;\n" |
465 | "}; // namespace A\n" |
466 | "// unrelated" , |
467 | fixNamespaceEndComments("namespace A {\n" |
468 | "int i;\n" |
469 | "int j;\n" |
470 | "};\n" |
471 | "// unrelated" )); |
472 | } |
473 | |
474 | TEST_F(NamespaceEndCommentsFixerTest, WorksForObjCpp) { |
475 | FormatStyle ObjCppStyle = getLLVMStyle(); |
476 | ObjCppStyle.Language = FormatStyle::LK_ObjC; |
477 | EXPECT_EQ("namespace {\n" |
478 | "int i;\n" |
479 | "int j;\n" |
480 | "} // namespace" , |
481 | fixNamespaceEndComments("namespace {\n" |
482 | "int i;\n" |
483 | "int j;\n" |
484 | "}" , |
485 | ObjCppStyle)); |
486 | } |
487 | |
488 | TEST_F(NamespaceEndCommentsFixerTest, AddsMacroEndComment) { |
489 | FormatStyle Style = getLLVMStyle(); |
490 | Style.NamespaceMacros.push_back(x: "TESTSUITE" ); |
491 | |
492 | EXPECT_EQ("TESTSUITE() {\n" |
493 | "int i;\n" |
494 | "int j;\n" |
495 | "} // TESTSUITE()" , |
496 | fixNamespaceEndComments("TESTSUITE() {\n" |
497 | "int i;\n" |
498 | "int j;\n" |
499 | "}" , |
500 | Style)); |
501 | |
502 | EXPECT_EQ("TESTSUITE(A) {\n" |
503 | "int i;\n" |
504 | "int j;\n" |
505 | "} // TESTSUITE(A)" , |
506 | fixNamespaceEndComments("TESTSUITE(A) {\n" |
507 | "int i;\n" |
508 | "int j;\n" |
509 | "}" , |
510 | Style)); |
511 | EXPECT_EQ("inline TESTSUITE(A) {\n" |
512 | "int i;\n" |
513 | "int j;\n" |
514 | "} // TESTSUITE(A)" , |
515 | fixNamespaceEndComments("inline TESTSUITE(A) {\n" |
516 | "int i;\n" |
517 | "int j;\n" |
518 | "}" , |
519 | Style)); |
520 | EXPECT_EQ("TESTSUITE(::A) {\n" |
521 | "int i;\n" |
522 | "int j;\n" |
523 | "} // TESTSUITE(::A)" , |
524 | fixNamespaceEndComments("TESTSUITE(::A) {\n" |
525 | "int i;\n" |
526 | "int j;\n" |
527 | "}" , |
528 | Style)); |
529 | EXPECT_EQ("TESTSUITE(::A::B) {\n" |
530 | "int i;\n" |
531 | "int j;\n" |
532 | "} // TESTSUITE(::A::B)" , |
533 | fixNamespaceEndComments("TESTSUITE(::A::B) {\n" |
534 | "int i;\n" |
535 | "int j;\n" |
536 | "}" , |
537 | Style)); |
538 | EXPECT_EQ("TESTSUITE(/**/::/**/A/**/::/**/B/**/) {\n" |
539 | "int i;\n" |
540 | "int j;\n" |
541 | "} // TESTSUITE(::A::B)" , |
542 | fixNamespaceEndComments("TESTSUITE(/**/::/**/A/**/::/**/B/**/) {\n" |
543 | "int i;\n" |
544 | "int j;\n" |
545 | "}" , |
546 | Style)); |
547 | EXPECT_EQ("TESTSUITE(A, B) {\n" |
548 | "int i;\n" |
549 | "int j;\n" |
550 | "} // TESTSUITE(A)" , |
551 | fixNamespaceEndComments("TESTSUITE(A, B) {\n" |
552 | "int i;\n" |
553 | "int j;\n" |
554 | "}" , |
555 | Style)); |
556 | EXPECT_EQ("TESTSUITE(\"Test1\") {\n" |
557 | "int i;\n" |
558 | "int j;\n" |
559 | "} // TESTSUITE(\"Test1\")" , |
560 | fixNamespaceEndComments("TESTSUITE(\"Test1\") {\n" |
561 | "int i;\n" |
562 | "int j;\n" |
563 | "}" , |
564 | Style)); |
565 | } |
566 | |
567 | TEST_F(NamespaceEndCommentsFixerTest, AddsNewlineIfNeeded) { |
568 | EXPECT_EQ("namespace A {\n" |
569 | "int i;\n" |
570 | "int j;\n" |
571 | "} // namespace A\n" |
572 | " int k;" , |
573 | fixNamespaceEndComments("namespace A {\n" |
574 | "int i;\n" |
575 | "int j;\n" |
576 | "} int k;" )); |
577 | EXPECT_EQ("namespace {\n" |
578 | "int i;\n" |
579 | "int j;\n" |
580 | "} // namespace\n" |
581 | " int k;" , |
582 | fixNamespaceEndComments("namespace {\n" |
583 | "int i;\n" |
584 | "int j;\n" |
585 | "} int k;" )); |
586 | EXPECT_EQ("namespace A {\n" |
587 | "int i;\n" |
588 | "int j;\n" |
589 | "} // namespace A\n" |
590 | " namespace B {\n" |
591 | "int j;\n" |
592 | "int k;\n" |
593 | "} // namespace B" , |
594 | fixNamespaceEndComments("namespace A {\n" |
595 | "int i;\n" |
596 | "int j;\n" |
597 | "} namespace B {\n" |
598 | "int j;\n" |
599 | "int k;\n" |
600 | "}" )); |
601 | EXPECT_EQ("namespace {\n" |
602 | "int i;\n" |
603 | "int j;\n" |
604 | "}; // namespace\n" |
605 | "int k;" , |
606 | fixNamespaceEndComments("namespace {\n" |
607 | "int i;\n" |
608 | "int j;\n" |
609 | "};int k;" )); |
610 | EXPECT_EQ("namespace {\n" |
611 | "int i;\n" |
612 | "int j;\n" |
613 | "}; // namespace\n" |
614 | ";" , |
615 | fixNamespaceEndComments("namespace {\n" |
616 | "int i;\n" |
617 | "int j;\n" |
618 | "};;" )); |
619 | } |
620 | |
621 | TEST_F(NamespaceEndCommentsFixerTest, DoesNotAddEndCommentForShortNamespace) { |
622 | EXPECT_EQ("namespace {}" , fixNamespaceEndComments("namespace {}" )); |
623 | EXPECT_EQ("namespace A {}" , fixNamespaceEndComments("namespace A {}" )); |
624 | EXPECT_EQ("namespace A { a }" , fixNamespaceEndComments("namespace A { a }" )); |
625 | EXPECT_EQ("namespace A { a };" , |
626 | fixNamespaceEndComments("namespace A { a };" )); |
627 | } |
628 | |
629 | TEST_F(NamespaceEndCommentsFixerTest, DoesNotAddCommentAfterUnaffectedRBrace) { |
630 | EXPECT_EQ("namespace A {\n" |
631 | "int i;\n" |
632 | "}" , |
633 | fixNamespaceEndComments("namespace A {\n" |
634 | "int i;\n" |
635 | "}" , |
636 | // The range (16, 3) spans the 'int' above. |
637 | /*Ranges=*/{1, tooling::Range(16, 3)})); |
638 | EXPECT_EQ("namespace A {\n" |
639 | "int i;\n" |
640 | "};" , |
641 | fixNamespaceEndComments("namespace A {\n" |
642 | "int i;\n" |
643 | "};" , |
644 | // The range (16, 3) spans the 'int' above. |
645 | /*Ranges=*/{1, tooling::Range(16, 3)})); |
646 | } |
647 | |
648 | TEST_F(NamespaceEndCommentsFixerTest, |
649 | DoesNotAddCommentAfterRBraceInPPDirective) { |
650 | EXPECT_EQ("#define SAD \\\n" |
651 | "namespace A { \\\n" |
652 | "int i; \\\n" |
653 | "}" , |
654 | fixNamespaceEndComments("#define SAD \\\n" |
655 | "namespace A { \\\n" |
656 | "int i; \\\n" |
657 | "}" )); |
658 | } |
659 | |
660 | TEST_F(NamespaceEndCommentsFixerTest, KeepsValidEndComment) { |
661 | EXPECT_TRUE(isFormatted("namespace {\n" |
662 | "int i;\n" |
663 | "} // end anonymous namespace" )); |
664 | EXPECT_TRUE(isFormatted("namespace A {\n" |
665 | "int i;\n" |
666 | "} /* end of namespace A */" )); |
667 | EXPECT_TRUE(isFormatted("namespace A {\n" |
668 | "int i;\n" |
669 | "} // namespace A" )); |
670 | EXPECT_TRUE(isFormatted("namespace A::B {\n" |
671 | "int i;\n" |
672 | "} // end namespace A::B" )); |
673 | EXPECT_TRUE(isFormatted("namespace A {\n" |
674 | "int i;\n" |
675 | "}; // end namespace A" )); |
676 | EXPECT_TRUE(isFormatted("namespace {\n" |
677 | "int i;\n" |
678 | "}; /* unnamed namespace */" )); |
679 | EXPECT_TRUE(isFormatted("namespace a::inline b {\n" |
680 | "int c;\n" |
681 | "}; // namespace a::inline b" )); |
682 | } |
683 | |
684 | TEST_F(NamespaceEndCommentsFixerTest, KeepsValidMacroEndComment) { |
685 | FormatStyle Style = getLLVMStyle(); |
686 | Style.NamespaceMacros.push_back(x: "TESTSUITE" ); |
687 | |
688 | EXPECT_TRUE(isFormatted("TESTSUITE() {\n" |
689 | "int i;\n" |
690 | "} // end anonymous TESTSUITE()" , |
691 | Style)); |
692 | EXPECT_TRUE(isFormatted("TESTSUITE(A) {\n" |
693 | "int i;\n" |
694 | "} /* end of TESTSUITE(A) */" , |
695 | Style)); |
696 | EXPECT_TRUE(isFormatted("TESTSUITE(A) {\n" |
697 | "int i;\n" |
698 | "} // TESTSUITE(A)" , |
699 | Style)); |
700 | EXPECT_TRUE(isFormatted("TESTSUITE(A::B) {\n" |
701 | "int i;\n" |
702 | "} // end TESTSUITE(A::B)" , |
703 | Style)); |
704 | EXPECT_TRUE(isFormatted("TESTSUITE(A) {\n" |
705 | "int i;\n" |
706 | "}; // end TESTSUITE(A)" , |
707 | Style)); |
708 | EXPECT_TRUE(isFormatted("TESTSUITE() {\n" |
709 | "int i;\n" |
710 | "}; /* unnamed TESTSUITE() */" , |
711 | Style)); |
712 | EXPECT_TRUE(isFormatted("TESTSUITE(\"foo\") {\n" |
713 | "int i;\n" |
714 | "} // TESTSUITE(\"foo\")" , |
715 | Style)); |
716 | } |
717 | |
718 | TEST_F(NamespaceEndCommentsFixerTest, UpdatesInvalidEndLineComment) { |
719 | EXPECT_EQ("namespace {\n" |
720 | "int i;\n" |
721 | "} // namespace" , |
722 | fixNamespaceEndComments("namespace {\n" |
723 | "int i;\n" |
724 | "} // namespace A" )); |
725 | EXPECT_EQ("namespace A {\n" |
726 | "int i;\n" |
727 | "} // namespace A" , |
728 | fixNamespaceEndComments("namespace A {\n" |
729 | "int i;\n" |
730 | "} // namespace" )); |
731 | EXPECT_EQ("namespace A {\n" |
732 | "int i;\n" |
733 | "} // namespace A" , |
734 | fixNamespaceEndComments("namespace A {\n" |
735 | "int i;\n" |
736 | "} //" )); |
737 | EXPECT_EQ("namespace A {\n" |
738 | "int i;\n" |
739 | "}; // namespace A" , |
740 | fixNamespaceEndComments("namespace A {\n" |
741 | "int i;\n" |
742 | "}; //" )); |
743 | |
744 | EXPECT_EQ("namespace A {\n" |
745 | "int i;\n" |
746 | "} // namespace A" , |
747 | fixNamespaceEndComments("namespace A {\n" |
748 | "int i;\n" |
749 | "} // banamespace A" )); |
750 | EXPECT_EQ("namespace A {\n" |
751 | "int i;\n" |
752 | "}; // namespace A" , |
753 | fixNamespaceEndComments("namespace A {\n" |
754 | "int i;\n" |
755 | "}; // banamespace A" )); |
756 | // Updates invalid line comments even for short namespaces. |
757 | EXPECT_EQ("namespace A {} // namespace A" , |
758 | fixNamespaceEndComments("namespace A {} // namespace" )); |
759 | EXPECT_EQ("namespace A {}; // namespace A" , |
760 | fixNamespaceEndComments("namespace A {}; // namespace" )); |
761 | |
762 | // Update invalid comments for compacted namespaces. |
763 | FormatStyle CompactNamespacesStyle = getLLVMStyle(); |
764 | CompactNamespacesStyle.CompactNamespaces = true; |
765 | EXPECT_EQ("namespace out { namespace in {\n" |
766 | "}} // namespace out::in" , |
767 | fixNamespaceEndComments("namespace out { namespace in {\n" |
768 | "}} // namespace out" , |
769 | CompactNamespacesStyle)); |
770 | EXPECT_EQ("namespace out { namespace in {\n" |
771 | "}} // namespace out::in" , |
772 | fixNamespaceEndComments("namespace out { namespace in {\n" |
773 | "}} // namespace in" , |
774 | CompactNamespacesStyle)); |
775 | EXPECT_EQ("namespace out { namespace in {\n" |
776 | "}\n" |
777 | "} // namespace out::in" , |
778 | fixNamespaceEndComments("namespace out { namespace in {\n" |
779 | "}// banamespace in\n" |
780 | "} // namespace out" , |
781 | CompactNamespacesStyle)); |
782 | EXPECT_EQ("namespace out { namespace {\n" |
783 | "}} // namespace out" , |
784 | fixNamespaceEndComments("namespace out { namespace {\n" |
785 | "}} // namespace out::" , |
786 | CompactNamespacesStyle)); |
787 | } |
788 | |
789 | TEST_F(NamespaceEndCommentsFixerTest, UpdatesInvalidMacroEndLineComment) { |
790 | FormatStyle Style = getLLVMStyle(); |
791 | Style.NamespaceMacros.push_back(x: "TESTSUITE" ); |
792 | |
793 | EXPECT_EQ("TESTSUITE() {\n" |
794 | "int i;\n" |
795 | "} // TESTSUITE()" , |
796 | fixNamespaceEndComments("TESTSUITE() {\n" |
797 | "int i;\n" |
798 | "} // TESTSUITE(A)" , |
799 | Style)); |
800 | EXPECT_EQ("TESTSUITE(A) {\n" |
801 | "int i;\n" |
802 | "} // TESTSUITE(A)" , |
803 | fixNamespaceEndComments("TESTSUITE(A) {\n" |
804 | "int i;\n" |
805 | "} // TESTSUITE()" , |
806 | Style)); |
807 | EXPECT_EQ("TESTSUITE(A) {\n" |
808 | "int i;\n" |
809 | "} // TESTSUITE(A)" , |
810 | fixNamespaceEndComments("TESTSUITE(A) {\n" |
811 | "int i;\n" |
812 | "} //" , |
813 | Style)); |
814 | EXPECT_EQ("TESTSUITE(A) {\n" |
815 | "int i;\n" |
816 | "}; // TESTSUITE(A)" , |
817 | fixNamespaceEndComments("TESTSUITE(A) {\n" |
818 | "int i;\n" |
819 | "}; //" , |
820 | Style)); |
821 | EXPECT_EQ("TESTSUITE(A) {\n" |
822 | "int i;\n" |
823 | "} // TESTSUITE(A)" , |
824 | fixNamespaceEndComments("TESTSUITE(A) {\n" |
825 | "int i;\n" |
826 | "} // TESTSUITE A" , |
827 | Style)); |
828 | EXPECT_EQ("TESTSUITE() {\n" |
829 | "int i;\n" |
830 | "} // TESTSUITE()" , |
831 | fixNamespaceEndComments("TESTSUITE() {\n" |
832 | "int i;\n" |
833 | "} // TESTSUITE" , |
834 | Style)); |
835 | EXPECT_EQ("TESTSUITE(A) {\n" |
836 | "int i;\n" |
837 | "} // TESTSUITE(A)" , |
838 | fixNamespaceEndComments("TESTSUITE(A) {\n" |
839 | "int i;\n" |
840 | "} // TOASTSUITE(A)" , |
841 | Style)); |
842 | EXPECT_EQ("TESTSUITE(A) {\n" |
843 | "int i;\n" |
844 | "}; // TESTSUITE(A)" , |
845 | fixNamespaceEndComments("TESTSUITE(A) {\n" |
846 | "int i;\n" |
847 | "}; // TOASTSUITE(A)" , |
848 | Style)); |
849 | // Updates invalid line comments even for short namespaces. |
850 | EXPECT_EQ("TESTSUITE(A) {} // TESTSUITE(A)" , |
851 | fixNamespaceEndComments("TESTSUITE(A) {} // TESTSUITE()" , Style)); |
852 | EXPECT_EQ("TESTSUITE(A) {}; // TESTSUITE(A)" , |
853 | fixNamespaceEndComments("TESTSUITE(A) {}; // TESTSUITE()" , Style)); |
854 | |
855 | // Update invalid comments for compacted namespaces. |
856 | FormatStyle CompactNamespacesStyle = getLLVMStyle(); |
857 | CompactNamespacesStyle.CompactNamespaces = true; |
858 | CompactNamespacesStyle.NamespaceMacros.push_back(x: "TESTSUITE" ); |
859 | |
860 | EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" |
861 | "}} // TESTSUITE(out::in)" , |
862 | fixNamespaceEndComments("TESTSUITE(out) { TESTSUITE(in) {\n" |
863 | "}} // TESTSUITE(out)" , |
864 | CompactNamespacesStyle)); |
865 | EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" |
866 | "}} // TESTSUITE(out::in)" , |
867 | fixNamespaceEndComments("TESTSUITE(out) { TESTSUITE(in) {\n" |
868 | "}} // TESTSUITE(in)" , |
869 | CompactNamespacesStyle)); |
870 | EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" |
871 | "}\n" |
872 | "} // TESTSUITE(out::in)" , |
873 | fixNamespaceEndComments("TESTSUITE(out) { TESTSUITE(in) {\n" |
874 | "}// TAOSTSUITE(in)\n" |
875 | "} // TESTSUITE(out)" , |
876 | CompactNamespacesStyle)); |
877 | } |
878 | |
879 | TEST_F(NamespaceEndCommentsFixerTest, UpdatesInvalidEndBlockComment) { |
880 | EXPECT_EQ("namespace {\n" |
881 | "int i;\n" |
882 | "} // namespace" , |
883 | fixNamespaceEndComments("namespace {\n" |
884 | "int i;\n" |
885 | "} /* namespace A */" )); |
886 | EXPECT_EQ("namespace A {\n" |
887 | "int i;\n" |
888 | "} // namespace A" , |
889 | fixNamespaceEndComments("namespace A {\n" |
890 | "int i;\n" |
891 | "} /* end namespace */" )); |
892 | EXPECT_EQ("namespace A {\n" |
893 | "int i;\n" |
894 | "} // namespace A" , |
895 | fixNamespaceEndComments("namespace A {\n" |
896 | "int i;\n" |
897 | "} /**/" )); |
898 | EXPECT_EQ("namespace A {\n" |
899 | "int i;\n" |
900 | "} // namespace A" , |
901 | fixNamespaceEndComments("namespace A {\n" |
902 | "int i;\n" |
903 | "} /* end unnamed namespace */" )); |
904 | EXPECT_EQ("namespace A {\n" |
905 | "int i;\n" |
906 | "} // namespace A" , |
907 | fixNamespaceEndComments("namespace A {\n" |
908 | "int i;\n" |
909 | "} /* banamespace A */" )); |
910 | EXPECT_EQ("namespace A {\n" |
911 | "int i;\n" |
912 | "}; // namespace A" , |
913 | fixNamespaceEndComments("namespace A {\n" |
914 | "int i;\n" |
915 | "}; /* banamespace A */" )); |
916 | EXPECT_EQ("namespace A {} // namespace A" , |
917 | fixNamespaceEndComments("namespace A {} /**/" )); |
918 | EXPECT_EQ("namespace A {}; // namespace A" , |
919 | fixNamespaceEndComments("namespace A {}; /**/" )); |
920 | } |
921 | |
922 | TEST_F(NamespaceEndCommentsFixerTest, UpdatesInvalidMacroEndBlockComment) { |
923 | FormatStyle Style = getLLVMStyle(); |
924 | Style.NamespaceMacros.push_back(x: "TESTSUITE" ); |
925 | |
926 | EXPECT_EQ("TESTSUITE() {\n" |
927 | "int i;\n" |
928 | "} // TESTSUITE()" , |
929 | fixNamespaceEndComments("TESTSUITE() {\n" |
930 | "int i;\n" |
931 | "} /* TESTSUITE(A) */" , |
932 | Style)); |
933 | EXPECT_EQ("TESTSUITE(A) {\n" |
934 | "int i;\n" |
935 | "} // TESTSUITE(A)" , |
936 | fixNamespaceEndComments("TESTSUITE(A) {\n" |
937 | "int i;\n" |
938 | "} /* end TESTSUITE() */" , |
939 | Style)); |
940 | EXPECT_EQ("TESTSUITE(A) {\n" |
941 | "int i;\n" |
942 | "} // TESTSUITE(A)" , |
943 | fixNamespaceEndComments("TESTSUITE(A) {\n" |
944 | "int i;\n" |
945 | "} /**/" , |
946 | Style)); |
947 | EXPECT_EQ("TESTSUITE(A) {\n" |
948 | "int i;\n" |
949 | "} // TESTSUITE(A)" , |
950 | fixNamespaceEndComments("TESTSUITE(A) {\n" |
951 | "int i;\n" |
952 | "} /* end unnamed TESTSUITE() */" , |
953 | Style)); |
954 | EXPECT_EQ("TESTSUITE(A) {\n" |
955 | "int i;\n" |
956 | "} // TESTSUITE(A)" , |
957 | fixNamespaceEndComments("TESTSUITE(A) {\n" |
958 | "int i;\n" |
959 | "} /* TOASTSUITE(A) */" , |
960 | Style)); |
961 | EXPECT_EQ("TESTSUITE(A) {\n" |
962 | "int i;\n" |
963 | "}; // TESTSUITE(A)" , |
964 | fixNamespaceEndComments("TESTSUITE(A) {\n" |
965 | "int i;\n" |
966 | "}; /* TAOSTSUITE(A) */" , |
967 | Style)); |
968 | EXPECT_EQ("TESTSUITE(A) {} // TESTSUITE(A)" , |
969 | fixNamespaceEndComments("TESTSUITE(A) {} /**/" , Style)); |
970 | EXPECT_EQ("TESTSUITE(A) {}; // TESTSUITE(A)" , |
971 | fixNamespaceEndComments("TESTSUITE(A) {}; /**/" , Style)); |
972 | } |
973 | |
974 | TEST_F(NamespaceEndCommentsFixerTest, |
975 | DoesNotAddEndCommentForNamespacesControlledByMacros) { |
976 | EXPECT_EQ("#ifdef 1\n" |
977 | "namespace A {\n" |
978 | "#elseif\n" |
979 | "namespace B {\n" |
980 | "#endif\n" |
981 | "int i;\n" |
982 | "}\n" |
983 | "}" , |
984 | fixNamespaceEndComments("#ifdef 1\n" |
985 | "namespace A {\n" |
986 | "#elseif\n" |
987 | "namespace B {\n" |
988 | "#endif\n" |
989 | "int i;\n" |
990 | "}\n" |
991 | "}" )); |
992 | } |
993 | |
994 | TEST_F(NamespaceEndCommentsFixerTest, AddsEndCommentForNamespacesAroundMacros) { |
995 | // Conditional blocks around are fine |
996 | EXPECT_EQ("namespace A {\n" |
997 | "#if 1\n" |
998 | "int i;\n" |
999 | "#endif\n" |
1000 | "} // namespace A" , |
1001 | fixNamespaceEndComments("namespace A {\n" |
1002 | "#if 1\n" |
1003 | "int i;\n" |
1004 | "#endif\n" |
1005 | "}" )); |
1006 | EXPECT_EQ("#if 1\n" |
1007 | "#endif\n" |
1008 | "namespace A {\n" |
1009 | "int i;\n" |
1010 | "int j;\n" |
1011 | "} // namespace A" , |
1012 | fixNamespaceEndComments("#if 1\n" |
1013 | "#endif\n" |
1014 | "namespace A {\n" |
1015 | "int i;\n" |
1016 | "int j;\n" |
1017 | "}" )); |
1018 | EXPECT_EQ("namespace A {\n" |
1019 | "int i;\n" |
1020 | "int j;\n" |
1021 | "} // namespace A\n" |
1022 | "#if 1\n" |
1023 | "#endif" , |
1024 | fixNamespaceEndComments("namespace A {\n" |
1025 | "int i;\n" |
1026 | "int j;\n" |
1027 | "}\n" |
1028 | "#if 1\n" |
1029 | "#endif" )); |
1030 | EXPECT_EQ("#if 1\n" |
1031 | "namespace A {\n" |
1032 | "int i;\n" |
1033 | "int j;\n" |
1034 | "} // namespace A\n" |
1035 | "#endif" , |
1036 | fixNamespaceEndComments("#if 1\n" |
1037 | "namespace A {\n" |
1038 | "int i;\n" |
1039 | "int j;\n" |
1040 | "}\n" |
1041 | "#endif" )); |
1042 | |
1043 | // Macro definition has no impact |
1044 | EXPECT_EQ("namespace A {\n" |
1045 | "#define FOO\n" |
1046 | "int i;\n" |
1047 | "} // namespace A" , |
1048 | fixNamespaceEndComments("namespace A {\n" |
1049 | "#define FOO\n" |
1050 | "int i;\n" |
1051 | "}" )); |
1052 | EXPECT_EQ("#define FOO\n" |
1053 | "namespace A {\n" |
1054 | "int i;\n" |
1055 | "int j;\n" |
1056 | "} // namespace A" , |
1057 | fixNamespaceEndComments("#define FOO\n" |
1058 | "namespace A {\n" |
1059 | "int i;\n" |
1060 | "int j;\n" |
1061 | "}" )); |
1062 | EXPECT_EQ("namespace A {\n" |
1063 | "int i;\n" |
1064 | "int j;\n" |
1065 | "} // namespace A\n" |
1066 | "#define FOO" , |
1067 | fixNamespaceEndComments("namespace A {\n" |
1068 | "int i;\n" |
1069 | "int j;\n" |
1070 | "}\n" |
1071 | "#define FOO" )); |
1072 | |
1073 | // No replacement if open & close in different conditional blocks |
1074 | EXPECT_EQ("#if 1\n" |
1075 | "namespace A {\n" |
1076 | "#endif\n" |
1077 | "int i;\n" |
1078 | "int j;\n" |
1079 | "#if 1\n" |
1080 | "}\n" |
1081 | "#endif" , |
1082 | fixNamespaceEndComments("#if 1\n" |
1083 | "namespace A {\n" |
1084 | "#endif\n" |
1085 | "int i;\n" |
1086 | "int j;\n" |
1087 | "#if 1\n" |
1088 | "}\n" |
1089 | "#endif" )); |
1090 | EXPECT_EQ("#ifdef A\n" |
1091 | "namespace A {\n" |
1092 | "#endif\n" |
1093 | "int i;\n" |
1094 | "int j;\n" |
1095 | "#ifdef B\n" |
1096 | "}\n" |
1097 | "#endif" , |
1098 | fixNamespaceEndComments("#ifdef A\n" |
1099 | "namespace A {\n" |
1100 | "#endif\n" |
1101 | "int i;\n" |
1102 | "int j;\n" |
1103 | "#ifdef B\n" |
1104 | "}\n" |
1105 | "#endif" )); |
1106 | |
1107 | // No replacement inside unreachable conditional block |
1108 | EXPECT_EQ("#if 0\n" |
1109 | "namespace A {\n" |
1110 | "int i;\n" |
1111 | "int j;\n" |
1112 | "}\n" |
1113 | "#endif" , |
1114 | fixNamespaceEndComments("#if 0\n" |
1115 | "namespace A {\n" |
1116 | "int i;\n" |
1117 | "int j;\n" |
1118 | "}\n" |
1119 | "#endif" )); |
1120 | } |
1121 | |
1122 | TEST_F(NamespaceEndCommentsFixerTest, |
1123 | DoesNotAddEndCommentForNamespacesInMacroDeclarations) { |
1124 | EXPECT_EQ("#ifdef 1\n" |
1125 | "namespace A {\n" |
1126 | "#elseif\n" |
1127 | "namespace B {\n" |
1128 | "#endif\n" |
1129 | "int i;\n" |
1130 | "}\n" |
1131 | "}" , |
1132 | fixNamespaceEndComments("#ifdef 1\n" |
1133 | "namespace A {\n" |
1134 | "#elseif\n" |
1135 | "namespace B {\n" |
1136 | "#endif\n" |
1137 | "int i;\n" |
1138 | "}\n" |
1139 | "}" )); |
1140 | EXPECT_EQ("namespace {\n" |
1141 | "int i;\n" |
1142 | "int j;\n" |
1143 | "} // namespace\n" |
1144 | "#if A\n" |
1145 | "int i;\n" |
1146 | "#else\n" |
1147 | "int j;\n" |
1148 | "#endif" , |
1149 | fixNamespaceEndComments("namespace {\n" |
1150 | "int i;\n" |
1151 | "int j;\n" |
1152 | "}\n" |
1153 | "#if A\n" |
1154 | "int i;\n" |
1155 | "#else\n" |
1156 | "int j;\n" |
1157 | "#endif" )); |
1158 | EXPECT_EQ("#if A\n" |
1159 | "namespace A {\n" |
1160 | "#else\n" |
1161 | "namespace B {\n" |
1162 | "#endif\n" |
1163 | "int i;\n" |
1164 | "int j;\n" |
1165 | "}" , |
1166 | fixNamespaceEndComments("#if A\n" |
1167 | "namespace A {\n" |
1168 | "#else\n" |
1169 | "namespace B {\n" |
1170 | "#endif\n" |
1171 | "int i;\n" |
1172 | "int j;\n" |
1173 | "}" )); |
1174 | EXPECT_EQ("#if A\n" |
1175 | "namespace A {\n" |
1176 | "#else\n" |
1177 | "namespace B {\n" |
1178 | "#endif\n" |
1179 | "int i;\n" |
1180 | "int j;\n" |
1181 | "} // namespace A" , |
1182 | fixNamespaceEndComments("#if A\n" |
1183 | "namespace A {\n" |
1184 | "#else\n" |
1185 | "namespace B {\n" |
1186 | "#endif\n" |
1187 | "int i;\n" |
1188 | "int j;\n" |
1189 | "} // namespace A" )); |
1190 | EXPECT_EQ("#if A\n" |
1191 | "namespace A {\n" |
1192 | "#else\n" |
1193 | "namespace B {\n" |
1194 | "#endif\n" |
1195 | "int i;\n" |
1196 | "int j;\n" |
1197 | "} // namespace B" , |
1198 | fixNamespaceEndComments("#if A\n" |
1199 | "namespace A {\n" |
1200 | "#else\n" |
1201 | "namespace B {\n" |
1202 | "#endif\n" |
1203 | "int i;\n" |
1204 | "int j;\n" |
1205 | "} // namespace B" )); |
1206 | EXPECT_EQ("namespace A\n" |
1207 | "int i;\n" |
1208 | "int j;\n" |
1209 | "#if A\n" |
1210 | "}\n" |
1211 | "#else\n" |
1212 | "}\n" |
1213 | "#endif" , |
1214 | fixNamespaceEndComments("namespace A\n" |
1215 | "int i;\n" |
1216 | "int j;\n" |
1217 | "#if A\n" |
1218 | "}\n" |
1219 | "#else\n" |
1220 | "}\n" |
1221 | "#endif" )); |
1222 | EXPECT_EQ("namespace A\n" |
1223 | "int i;\n" |
1224 | "int j;\n" |
1225 | "#if A\n" |
1226 | "} // namespace A\n" |
1227 | "#else\n" |
1228 | "} // namespace A\n" |
1229 | "#endif" , |
1230 | fixNamespaceEndComments("namespace A\n" |
1231 | "int i;\n" |
1232 | "int j;\n" |
1233 | "#if A\n" |
1234 | "} // namespace A\n" |
1235 | "#else\n" |
1236 | "} // namespace A\n" |
1237 | "#endif" )); |
1238 | } |
1239 | |
1240 | TEST_F(NamespaceEndCommentsFixerTest, |
1241 | DoesNotAddEndCommentForUnbalancedRBracesAfterNamespaceEnd) { |
1242 | EXPECT_EQ("namespace {\n" |
1243 | "int i;\n" |
1244 | "} // namespace\n" |
1245 | "}" , |
1246 | fixNamespaceEndComments("namespace {\n" |
1247 | "int i;\n" |
1248 | "} // namespace\n" |
1249 | "}" )); |
1250 | } |
1251 | |
1252 | TEST_F(NamespaceEndCommentsFixerTest, HandlesInlineAtEndOfLine_PR32438) { |
1253 | EXPECT_EQ("template <int> struct a {};\n" |
1254 | "struct a<bool{}> b() {\n" |
1255 | "}\n" |
1256 | "#define c inline\n" |
1257 | "void d() {\n" |
1258 | "}" , |
1259 | fixNamespaceEndComments("template <int> struct a {};\n" |
1260 | "struct a<bool{}> b() {\n" |
1261 | "}\n" |
1262 | "#define c inline\n" |
1263 | "void d() {\n" |
1264 | "}" )); |
1265 | } |
1266 | |
1267 | TEST_F(NamespaceEndCommentsFixerTest, IgnoreUnbalanced) { |
1268 | EXPECT_EQ("namespace A {\n" |
1269 | "class Foo {\n" |
1270 | "}\n" |
1271 | "} // namespace A" , |
1272 | fixNamespaceEndComments("namespace A {\n" |
1273 | "class Foo {\n" |
1274 | "}\n" |
1275 | "}" )); |
1276 | EXPECT_EQ("namespace A {\n" |
1277 | "class Foo {\n" |
1278 | "}" , |
1279 | fixNamespaceEndComments("namespace A {\n" |
1280 | "class Foo {\n" |
1281 | "}" )); |
1282 | |
1283 | EXPECT_EQ("namespace A {\n" |
1284 | "class Foo {\n" |
1285 | "}\n" |
1286 | "}\n" |
1287 | "}" , |
1288 | fixNamespaceEndComments("namespace A {\n" |
1289 | "class Foo {\n" |
1290 | "}\n" |
1291 | "}\n" |
1292 | "}" )); |
1293 | } |
1294 | |
1295 | using ShortNamespaceLinesTest = NamespaceEndCommentsFixerTest; |
1296 | |
1297 | TEST_F(ShortNamespaceLinesTest, ZeroUnwrappedLines) { |
1298 | auto Style = getLLVMStyle(); |
1299 | Style.ShortNamespaceLines = 0u; |
1300 | |
1301 | EXPECT_EQ("namespace OneLinerNamespace {}" , |
1302 | fixNamespaceEndComments("namespace OneLinerNamespace {}" , Style)); |
1303 | EXPECT_EQ("namespace ShortNamespace {\n" |
1304 | "}" , |
1305 | fixNamespaceEndComments("namespace ShortNamespace {\n" |
1306 | "}" , |
1307 | Style)); |
1308 | EXPECT_EQ("namespace LongNamespace {\n" |
1309 | "int i;\n" |
1310 | "} // namespace LongNamespace" , |
1311 | fixNamespaceEndComments("namespace LongNamespace {\n" |
1312 | "int i;\n" |
1313 | "}" , |
1314 | Style)); |
1315 | } |
1316 | |
1317 | TEST_F(ShortNamespaceLinesTest, OneUnwrappedLine) { |
1318 | constexpr auto DefaultUnwrappedLines = 1u; |
1319 | auto const Style = getLLVMStyle(); |
1320 | |
1321 | EXPECT_EQ(DefaultUnwrappedLines, Style.ShortNamespaceLines); |
1322 | EXPECT_EQ("namespace ShortNamespace {\n" |
1323 | "int i;\n" |
1324 | "}" , |
1325 | fixNamespaceEndComments("namespace ShortNamespace {\n" |
1326 | "int i;\n" |
1327 | "}" )); |
1328 | EXPECT_EQ("namespace LongNamespace {\n" |
1329 | "int i;\n" |
1330 | "int j;\n" |
1331 | "} // namespace LongNamespace" , |
1332 | fixNamespaceEndComments("namespace LongNamespace {\n" |
1333 | "int i;\n" |
1334 | "int j;\n" |
1335 | "}" )); |
1336 | } |
1337 | |
1338 | TEST_F(ShortNamespaceLinesTest, MultipleUnwrappedLine) { |
1339 | auto Style = getLLVMStyle(); |
1340 | Style.ShortNamespaceLines = 2u; |
1341 | |
1342 | EXPECT_EQ("namespace ShortNamespace {\n" |
1343 | "int i;\n" |
1344 | "int j;\n" |
1345 | "}" , |
1346 | fixNamespaceEndComments("namespace ShortNamespace {\n" |
1347 | "int i;\n" |
1348 | "int j;\n" |
1349 | "}" , |
1350 | Style)); |
1351 | EXPECT_EQ("namespace LongNamespace {\n" |
1352 | "int i;\n" |
1353 | "int j;\n" |
1354 | "int k;\n" |
1355 | "} // namespace LongNamespace" , |
1356 | fixNamespaceEndComments("namespace LongNamespace {\n" |
1357 | "int i;\n" |
1358 | "int j;\n" |
1359 | "int k;\n" |
1360 | "}" , |
1361 | Style)); |
1362 | |
1363 | // The namespace body has 5 unwrapped/annotated lines. |
1364 | const std::string NestedLambdas{"namespace foo {\n" |
1365 | "auto bar = [] {\n" // line 1 |
1366 | " int i;\n" // line 2 |
1367 | " return [] {\n" // line 3 |
1368 | " int j;" // line 4 |
1369 | " return 0;\n" // line 5 |
1370 | " };\n" // part of line 3 |
1371 | "};\n" // part of line 1 |
1372 | "}" }; |
1373 | Style.ShortNamespaceLines = 4; |
1374 | EXPECT_EQ(NestedLambdas + " // namespace foo" , |
1375 | fixNamespaceEndComments(NestedLambdas, Style)); |
1376 | ++Style.ShortNamespaceLines; |
1377 | EXPECT_EQ(NestedLambdas, fixNamespaceEndComments(NestedLambdas, Style)); |
1378 | } |
1379 | |
1380 | TEST_F(ShortNamespaceLinesTest, NamespaceAlias) { |
1381 | auto Style = getLLVMStyle(); |
1382 | |
1383 | EXPECT_EQ("namespace n = nn;\n" |
1384 | "{\n" |
1385 | " int i;\n" |
1386 | " int j;\n" |
1387 | "}" , |
1388 | fixNamespaceEndComments("namespace n = nn;\n" |
1389 | "{\n" |
1390 | " int i;\n" |
1391 | " int j;\n" |
1392 | "}" , |
1393 | Style)); |
1394 | |
1395 | EXPECT_EQ("namespace n = nn; // comment\n" |
1396 | "{\n" |
1397 | " int i;\n" |
1398 | " int j;\n" |
1399 | "}" , |
1400 | fixNamespaceEndComments("namespace n = nn; // comment\n" |
1401 | "{\n" |
1402 | " int i;\n" |
1403 | " int j;\n" |
1404 | "}" , |
1405 | Style)); |
1406 | |
1407 | EXPECT_EQ("namespace n = nn; /* comment */\n" |
1408 | "{\n" |
1409 | " int i;\n" |
1410 | " int j;\n" |
1411 | "}" , |
1412 | fixNamespaceEndComments("namespace n = nn; /* comment */\n" |
1413 | "{\n" |
1414 | " int i;\n" |
1415 | " int j;\n" |
1416 | "}" , |
1417 | Style)); |
1418 | |
1419 | EXPECT_EQ( |
1420 | "namespace n = nn; /* comment */ /* comment2 */\n" |
1421 | "{\n" |
1422 | " int i;\n" |
1423 | " int j;\n" |
1424 | "}" , |
1425 | fixNamespaceEndComments("namespace n = nn; /* comment */ /* comment2 */\n" |
1426 | "{\n" |
1427 | " int i;\n" |
1428 | " int j;\n" |
1429 | "}" , |
1430 | Style)); |
1431 | |
1432 | EXPECT_EQ("namespace n = nn; {\n" |
1433 | " int i;\n" |
1434 | " int j;\n" |
1435 | "}" , |
1436 | fixNamespaceEndComments("namespace n = nn; {\n" |
1437 | " int i;\n" |
1438 | " int j;\n" |
1439 | "}" , |
1440 | Style)); |
1441 | EXPECT_EQ("int foo;\n" |
1442 | "namespace n\n" |
1443 | "{\n" |
1444 | " int i;\n" |
1445 | " int j;\n" |
1446 | "} // namespace n" , |
1447 | fixNamespaceEndComments("int foo;\n" |
1448 | "namespace n\n" |
1449 | "{\n" |
1450 | " int i;\n" |
1451 | " int j;\n" |
1452 | "}" , |
1453 | Style)); |
1454 | } |
1455 | } // end namespace |
1456 | } // end namespace format |
1457 | } // end namespace clang |
1458 | |