1 //===- unittest/Format/FormatTestSelective.cpp - Formatting unit tests ----===//
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
7 //===----------------------------------------------------------------------===//
9 #include "FormatTestUtils.h"
10 #include "clang/Format/Format.h"
11 #include "llvm/Support/Debug.h"
12 #include "gtest/gtest.h"
14 #define DEBUG_TYPE "format-test"
20 class FormatTestSelective
: public testing::Test
{
22 std::string
format(StringRef Code
, unsigned Offset
, unsigned Length
) {
23 LLVM_DEBUG(llvm::errs() << "---\n");
24 LLVM_DEBUG(llvm::errs() << Code
<< "\n\n");
25 std::vector
<tooling::Range
> Ranges(1, tooling::Range(Offset
, Length
));
26 FormattingAttemptStatus Status
;
27 tooling::Replacements Replaces
=
28 reformat(Style
, Code
, Ranges
, "<stdin>", &Status
);
29 EXPECT_TRUE(Status
.FormatComplete
) << Code
<< "\n\n";
30 auto Result
= applyAllReplacements(Code
, Replaces
);
31 EXPECT_TRUE(static_cast<bool>(Result
));
32 LLVM_DEBUG(llvm::errs() << "\n" << *Result
<< "\n\n");
36 FormatStyle Style
= getLLVMStyle();
39 TEST_F(FormatTestSelective
, RemovesTrailingWhitespaceOfFormattedLine
) {
40 EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0));
41 EXPECT_EQ("int a;", format("int a; ", 0, 0));
42 EXPECT_EQ("int a;\n", format("int a; \n \n \n ", 0, 0));
43 EXPECT_EQ("int a;\nint b; ", format("int a; \nint b; ", 0, 0));
46 TEST_F(FormatTestSelective
, FormatsCorrectRegionForLeadingWhitespace
) {
50 format("{int b;\n int a;}", 8, 0));
54 format("{int b;\n int a;}", 7, 0));
56 Style
.ColumnLimit
= 12;
57 EXPECT_EQ("#define A \\\n"
60 format("#define A \\\n"
64 EXPECT_EQ("#define A \\\n"
67 format("#define A \\\n"
73 TEST_F(FormatTestSelective
, FormatLineWhenInvokedOnTrailingNewline
) {
74 EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 8, 0));
75 EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 7, 0));
77 // This might not strictly be correct, but is likely good in all practical
79 EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0));
82 TEST_F(FormatTestSelective
, RemovesWhitespaceWhenTriggeredOnEmptyLine
) {
83 EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 8, 0));
84 EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 9, 0));
87 TEST_F(FormatTestSelective
, ReformatsMovedLines
) {
89 "template <typename T> T *getFETokenInfo() const {\n"
90 " return static_cast<T *>(FETokenInfo);\n"
92 "int a; // <- Should not be formatted",
94 "template<typename T>\n"
95 "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
96 "int a; // <- Should not be formatted",
100 TEST_F(FormatTestSelective
, FormatsIfWithoutCompoundStatement
) {
101 Style
.AllowShortIfStatementsOnASingleLine
= FormatStyle::SIS_WithoutElse
;
102 EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1));
103 EXPECT_EQ("if (a) return; // comment",
104 format("if(a)\nreturn; // comment", 20, 1));
107 TEST_F(FormatTestSelective
, FormatsCommentsLocally
) {
108 EXPECT_EQ("int a; // comment\n"
110 format("int a; // comment\n"
113 EXPECT_EQ("int a; // comment\n"
116 format("int a; // comment\n"
120 EXPECT_EQ("int a; // comment\n"
123 format("int a; // comment\n"
127 EXPECT_EQ("int aaaaaa; // comment\n"
129 "int c; // unrelated comment",
130 format("int aaaaaa; // comment\n"
132 "int c; // unrelated comment",
135 EXPECT_EQ("int a; // This\n"
138 format("int a; // This\n"
142 EXPECT_EQ("int a; // This\n"
147 format("int a; // This\n"
153 EXPECT_EQ("int a; // This\n"
157 "//This is unrelated",
158 format("int a; // This\n"
162 "//This is unrelated",
166 "// not formatted. ",
169 "// not formatted. ",
171 EXPECT_EQ("int x; // Format this line.\n"
174 format("int x; // Format this line.\n"
180 TEST_F(FormatTestSelective
, ContinueReindenting
) {
181 // When we change an indent, we continue formatting as long as following
182 // lines are not indented correctly.
198 TEST_F(FormatTestSelective
, ReindentClosingBrace
) {
212 EXPECT_EQ("void f() {\n"
220 format("void f() {\n"
229 EXPECT_EQ("int i = []() {\n"
236 format("int i = []() {\n"
246 TEST_F(FormatTestSelective
, IndividualStatementsOfNestedBlocks
) {
247 EXPECT_EQ("DEBUG({\n"
256 EXPECT_EQ("DEBUG( {\n"
265 EXPECT_EQ("DEBUG( {\n"
274 EXPECT_EQ("DEBUG({\n"
284 EXPECT_EQ("Debug({\n"
285 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
290 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
295 EXPECT_EQ("DEBUG({\n"
304 " int b;\n" // Format this line only.
305 " }) ;\n" // Don't touch this line.
308 EXPECT_EQ("DEBUG({\n"
315 EXPECT_EQ("someFunction(\n"
317 " // Only with this comment.\n"
318 " int i; // invoke formatting here.\n"
319 " }, // force line break\n"
321 format("someFunction(\n"
323 " // Only with this comment.\n"
324 " int i; // invoke formatting here.\n"
325 " }, // force line break\n"
329 EXPECT_EQ("int longlongname; // comment\n"
331 " int x; // comment\n"
332 " int y; // comment\n"
334 format("int longlongname; // comment\n"
336 " int x; // comment\n"
337 " int y; // comment\n"
340 EXPECT_EQ("int s = f({\n"
346 format("int s = f({\n"
353 EXPECT_EQ("SomeFunction(\n"
356 " return i;\n" // Format this line.
359 " return 2;\n" // Don't fix this.
361 format("SomeFunction(\n"
364 " return i;\n" // Format this line.
367 " return 2;\n" // Don't fix this.
372 TEST_F(FormatTestSelective
, WrongIndent
) {
373 EXPECT_EQ("namespace {\n"
377 format("namespace {\n"
378 " int i;\n" // Format here.
382 EXPECT_EQ("namespace {\n"
386 format("namespace {\n"
388 " int j;\n" // Format here.
393 TEST_F(FormatTestSelective
, AlwaysFormatsEntireMacroDefinitions
) {
394 Style
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
405 8, 0)); // 8: position of "#define".
416 45, 0)); // 45: position of "j".
419 TEST_F(FormatTestSelective
, ReformatRegionAdjustsIndent
) {
487 EXPECT_EQ("void f() {}\n"
489 format("void f() {}\n"
492 EXPECT_EQ("int a; // comment\n"
495 format("int a; // comment\n"
500 EXPECT_EQ(" void f() {\n"
503 format(" void f() {\n"
504 " #define A 1\n" // Format this line.
507 EXPECT_EQ(" void f() {\n"
514 format(" void f() {\n"
519 " int k;\n" // Format this line.
523 Style
.ColumnLimit
= 11;
524 EXPECT_EQ(" int a;\n"
532 // https://github.com/llvm/llvm-project/issues/59178
533 Style
= getMozillaStyle();
534 EXPECT_EQ("int a()\n"
548 "return 42;\n" // Format this line only
553 TEST_F(FormatTestSelective
, UnderstandsTabs
) {
554 Style
.IndentWidth
= 8;
555 Style
.UseTab
= FormatStyle::UT_Always
;
556 Style
.AlignEscapedNewlines
= FormatStyle::ENAS_Left
;
557 EXPECT_EQ("void f() {\n"
561 format("void f() {\n"
566 EXPECT_EQ("void f() {\n"
570 format("void f() {\n"
575 EXPECT_EQ("void f() {\n"
579 format("void f() {\n"
586 TEST_F(FormatTestSelective
, StopFormattingWhenLeavingScope
) {
596 format("void f() {\n"
597 " if (a) {\n" // Assume this was added without the closing brace.
602 "void g() {\n" // Make sure not to format this.
607 TEST_F(FormatTestSelective
, SelectivelyRequoteJavaScript
) {
608 Style
= getGoogleStyle(FormatStyle::LK_JavaScript
);
609 EXPECT_EQ("var x = \"a\";\n"
612 format("var x = \"a\";\n"
618 TEST_F(FormatTestSelective
, KeepsIndentAfterCommentSectionImport
) {
619 std::string Code
= "#include <a> // line 1\n" // 23 chars long
620 " // line 2\n" // 23 chars long
621 "\n" // this newline is char 47
622 "int i;"; // this line is not indented
623 EXPECT_EQ(Code
, format(Code
, 47, 1));
626 TEST_F(FormatTestSelective
, DontAssert
) {
627 // https://llvm.org/PR53880
628 std::string Code
= "void f() {\n"
629 " return a == 8 ? 32 : 16;\n"
631 EXPECT_EQ(Code
, format(Code
, 40, 0));
633 // https://llvm.org/PR56352
634 Style
.CompactNamespaces
= true;
635 Style
.NamespaceIndentation
= FormatStyle::NI_All
;
637 "namespace ns1 { namespace ns2 {\n"
639 EXPECT_EQ(Code
, format(Code
, 0, 0));
641 // https://reviews.llvm.org/D151047#4369742
642 Style
= getLLVMStyle();
643 Style
.FixNamespaceComments
= false;
644 Code
= "namespace ns {\n"
645 "#define REF(alias) alias alias_var;\n"
646 "}"; // Format this line only
647 EXPECT_EQ(Code
, format(Code
, 51, 0));
650 TEST_F(FormatTestSelective
, FormatMacroRegardlessOfPreviousIndent
) {
651 // clang-format currently does not (or should not) take into account the
652 // indent of previous unformatted lines when formatting a PP directive.
653 // Technically speaking, LevelIndentTracker::IndentForLevel is only for non-PP
654 // lines. So these tests here check that the indent of previous non-PP lines
655 // do not affect the formatting. If this requirement changes, the tests here
656 // need to be adapted.
657 Style
= getLLVMStyle();
659 const StringRef Code
{" class Foo {\n"
662 " #define some\n" // format this line
666 EXPECT_EQ(Style
.IndentPPDirectives
,
667 FormatStyle::PPDirectiveIndentStyle::PPDIS_None
);
668 EXPECT_EQ(" class Foo {\n"
671 "#define some\n" // Formatted line
672 "#endif\n" // That this line is also formatted might be a bug.
673 " }};", // Ditto: Bug?
674 format(Code
, 57, 0));
676 Style
.IndentPPDirectives
=
677 FormatStyle::PPDirectiveIndentStyle::PPDIS_BeforeHash
;
678 EXPECT_EQ(" class Foo {\n"
681 " #define some\n" // Formatted line
684 format(Code
, 57, 0));
686 Style
.IndentPPDirectives
=
687 FormatStyle::PPDirectiveIndentStyle::PPDIS_AfterHash
;
688 EXPECT_EQ(" class Foo {\n"
691 "# define some\n" // Formatted line
692 "#endif\n" // That this line is also formatted might be a bug.
694 format(Code
, 57, 0));
698 } // end namespace format
699 } // end namespace clang