Skip to content

Commit 51fe279

Browse files
author
Marianne Mailhot-Sarrasin
committedApr 14, 2016
clang-format: Implemented tab usage for continuation and indentation
Use tabs to fill whitespace at the start of a line. Patch by Maxime Beaulieu Differential Revision: http://reviews.llvm.org/D19028 llvm-svn: 266320
1 parent 4988fa1 commit 51fe279

File tree

4 files changed

+238
-0
lines changed

4 files changed

+238
-0
lines changed
 

‎clang/include/clang/Format/Format.h

+2
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,8 @@ struct FormatStyle {
603603
UT_Never,
604604
/// Use tabs only for indentation.
605605
UT_ForIndentation,
606+
/// Use tabs only for line continuation and indentation.
607+
UT_ForContinuationAndIndentation,
606608
/// Use tabs whenever we need to fill whitespace that spans at least from
607609
/// one tab stop to the next one.
608610
UT_Always

‎clang/lib/Format/Format.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> {
6969
IO.enumCase(Value, "Always", FormatStyle::UT_Always);
7070
IO.enumCase(Value, "true", FormatStyle::UT_Always);
7171
IO.enumCase(Value, "ForIndentation", FormatStyle::UT_ForIndentation);
72+
IO.enumCase(Value, "ForContinuationAndIndentation",
73+
FormatStyle::UT_ForContinuationAndIndentation);
7274
}
7375
};
7476

‎clang/lib/Format/WhitespaceManager.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,14 @@ void WhitespaceManager::appendIndentText(std::string &Text,
558558
}
559559
Text.append(Spaces, ' ');
560560
break;
561+
case FormatStyle::UT_ForContinuationAndIndentation:
562+
if (WhitespaceStartColumn == 0) {
563+
unsigned Tabs = Spaces / Style.TabWidth;
564+
Text.append(Tabs, '\t');
565+
Spaces -= Tabs * Style.TabWidth;
566+
}
567+
Text.append(Spaces, ' ');
568+
break;
561569
}
562570
}
563571

‎clang/unittests/Format/FormatTest.cpp

+226
Original file line numberDiff line numberDiff line change
@@ -8572,6 +8572,230 @@ TEST_F(FormatTest, ConfigurableUseOfTab) {
85728572
"\t */\n"
85738573
"\t int i;\n"
85748574
"}"));
8575+
8576+
Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation;
8577+
Tab.TabWidth = 8;
8578+
Tab.IndentWidth = 8;
8579+
EXPECT_EQ("if (aaaaaaaa && // q\n"
8580+
" bb) // w\n"
8581+
"\t;",
8582+
format("if (aaaaaaaa &&// q\n"
8583+
"bb)// w\n"
8584+
";",
8585+
Tab));
8586+
EXPECT_EQ("if (aaa && bbb) // w\n"
8587+
"\t;",
8588+
format("if(aaa&&bbb)// w\n"
8589+
";",
8590+
Tab));
8591+
verifyFormat("class X {\n"
8592+
"\tvoid f() {\n"
8593+
"\t\tsomeFunction(parameter1,\n"
8594+
"\t\t\t parameter2);\n"
8595+
"\t}\n"
8596+
"};",
8597+
Tab);
8598+
verifyFormat("#define A \\\n"
8599+
"\tvoid f() { \\\n"
8600+
"\t\tsomeFunction( \\\n"
8601+
"\t\t parameter1, \\\n"
8602+
"\t\t parameter2); \\\n"
8603+
"\t}",
8604+
Tab);
8605+
Tab.TabWidth = 4;
8606+
Tab.IndentWidth = 8;
8607+
verifyFormat("class TabWidth4Indent8 {\n"
8608+
"\t\tvoid f() {\n"
8609+
"\t\t\t\tsomeFunction(parameter1,\n"
8610+
"\t\t\t\t\t\t\t parameter2);\n"
8611+
"\t\t}\n"
8612+
"};",
8613+
Tab);
8614+
Tab.TabWidth = 4;
8615+
Tab.IndentWidth = 4;
8616+
verifyFormat("class TabWidth4Indent4 {\n"
8617+
"\tvoid f() {\n"
8618+
"\t\tsomeFunction(parameter1,\n"
8619+
"\t\t\t\t\t parameter2);\n"
8620+
"\t}\n"
8621+
"};",
8622+
Tab);
8623+
Tab.TabWidth = 8;
8624+
Tab.IndentWidth = 4;
8625+
verifyFormat("class TabWidth8Indent4 {\n"
8626+
" void f() {\n"
8627+
"\tsomeFunction(parameter1,\n"
8628+
"\t\t parameter2);\n"
8629+
" }\n"
8630+
"};",
8631+
Tab);
8632+
Tab.TabWidth = 8;
8633+
Tab.IndentWidth = 8;
8634+
EXPECT_EQ("/*\n"
8635+
"\t a\t\tcomment\n"
8636+
"\t in multiple lines\n"
8637+
" */",
8638+
format(" /*\t \t \n"
8639+
" \t \t a\t\tcomment\t \t\n"
8640+
" \t \t in multiple lines\t\n"
8641+
" \t */",
8642+
Tab));
8643+
verifyFormat("{\n"
8644+
"\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8645+
"\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8646+
"\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8647+
"\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8648+
"\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8649+
"\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n"
8650+
"};",
8651+
Tab);
8652+
verifyFormat("enum AA {\n"
8653+
"\ta1, // Force multiple lines\n"
8654+
"\ta2,\n"
8655+
"\ta3\n"
8656+
"};",
8657+
Tab);
8658+
EXPECT_EQ("if (aaaaaaaa && // q\n"
8659+
" bb) // w\n"
8660+
"\t;",
8661+
format("if (aaaaaaaa &&// q\n"
8662+
"bb)// w\n"
8663+
";",
8664+
Tab));
8665+
verifyFormat("class X {\n"
8666+
"\tvoid f() {\n"
8667+
"\t\tsomeFunction(parameter1,\n"
8668+
"\t\t\t parameter2);\n"
8669+
"\t}\n"
8670+
"};",
8671+
Tab);
8672+
verifyFormat("{\n"
8673+
"\tQ(\n"
8674+
"\t {\n"
8675+
"\t\t int a;\n"
8676+
"\t\t someFunction(aaaaaaaa,\n"
8677+
"\t\t\t\t bbbbbbb);\n"
8678+
"\t },\n"
8679+
"\t p);\n"
8680+
"}",
8681+
Tab);
8682+
EXPECT_EQ("{\n"
8683+
"\t/* aaaa\n"
8684+
"\t bbbb */\n"
8685+
"}",
8686+
format("{\n"
8687+
"/* aaaa\n"
8688+
" bbbb */\n"
8689+
"}",
8690+
Tab));
8691+
EXPECT_EQ("{\n"
8692+
"\t/*\n"
8693+
"\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8694+
"\t bbbbbbbbbbbbb\n"
8695+
"\t*/\n"
8696+
"}",
8697+
format("{\n"
8698+
"/*\n"
8699+
" aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8700+
"*/\n"
8701+
"}",
8702+
Tab));
8703+
EXPECT_EQ("{\n"
8704+
"\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8705+
"\t// bbbbbbbbbbbbb\n"
8706+
"}",
8707+
format("{\n"
8708+
"\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8709+
"}",
8710+
Tab));
8711+
EXPECT_EQ("{\n"
8712+
"\t/*\n"
8713+
"\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n"
8714+
"\t bbbbbbbbbbbbb\n"
8715+
"\t*/\n"
8716+
"}",
8717+
format("{\n"
8718+
"\t/*\n"
8719+
"\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n"
8720+
"\t*/\n"
8721+
"}",
8722+
Tab));
8723+
EXPECT_EQ("{\n"
8724+
"\t/*\n"
8725+
"\n"
8726+
"\t*/\n"
8727+
"}",
8728+
format("{\n"
8729+
"\t/*\n"
8730+
"\n"
8731+
"\t*/\n"
8732+
"}",
8733+
Tab));
8734+
EXPECT_EQ("{\n"
8735+
"\t/*\n"
8736+
" asdf\n"
8737+
"\t*/\n"
8738+
"}",
8739+
format("{\n"
8740+
"\t/*\n"
8741+
" asdf\n"
8742+
"\t*/\n"
8743+
"}",
8744+
Tab));
8745+
EXPECT_EQ("/*\n"
8746+
"\t a\t\tcomment\n"
8747+
"\t in multiple lines\n"
8748+
" */",
8749+
format(" /*\t \t \n"
8750+
" \t \t a\t\tcomment\t \t\n"
8751+
" \t \t in multiple lines\t\n"
8752+
" \t */",
8753+
Tab));
8754+
EXPECT_EQ("/* some\n"
8755+
" comment */",
8756+
format(" \t \t /* some\n"
8757+
" \t \t comment */",
8758+
Tab));
8759+
EXPECT_EQ("int a; /* some\n"
8760+
" comment */",
8761+
format(" \t \t int a; /* some\n"
8762+
" \t \t comment */",
8763+
Tab));
8764+
EXPECT_EQ("int a; /* some\n"
8765+
"comment */",
8766+
format(" \t \t int\ta; /* some\n"
8767+
" \t \t comment */",
8768+
Tab));
8769+
EXPECT_EQ("f(\"\t\t\"); /* some\n"
8770+
" comment */",
8771+
format(" \t \t f(\"\t\t\"); /* some\n"
8772+
" \t \t comment */",
8773+
Tab));
8774+
EXPECT_EQ("{\n"
8775+
" /*\n"
8776+
" * Comment\n"
8777+
" */\n"
8778+
" int i;\n"
8779+
"}",
8780+
format("{\n"
8781+
"\t/*\n"
8782+
"\t * Comment\n"
8783+
"\t */\n"
8784+
"\t int i;\n"
8785+
"}"));
8786+
Tab.AlignConsecutiveAssignments = true;
8787+
Tab.AlignConsecutiveDeclarations = true;
8788+
Tab.TabWidth = 4;
8789+
Tab.IndentWidth = 4;
8790+
verifyFormat("class Assign {\n"
8791+
"\tvoid f() {\n"
8792+
"\t\tint x = 123;\n"
8793+
"\t\tint random = 4;\n"
8794+
"\t\tstd::string alphabet =\n"
8795+
"\t\t\t\"abcdefghijklmnopqrstuvwxyz\";\n"
8796+
"\t}\n"
8797+
"};",
8798+
Tab);
85758799
}
85768800

85778801
TEST_F(FormatTest, CalculatesOriginalColumn) {
@@ -10015,6 +10239,8 @@ TEST_F(FormatTest, ParsesConfiguration) {
1001510239
CHECK_PARSE("UseTab: Never", UseTab, FormatStyle::UT_Never);
1001610240
CHECK_PARSE("UseTab: ForIndentation", UseTab, FormatStyle::UT_ForIndentation);
1001710241
CHECK_PARSE("UseTab: Always", UseTab, FormatStyle::UT_Always);
10242+
CHECK_PARSE("UseTab: ForContinuationAndIndentation", UseTab,
10243+
FormatStyle::UT_ForContinuationAndIndentation);
1001810244
// For backward compatibility:
1001910245
CHECK_PARSE("UseTab: false", UseTab, FormatStyle::UT_Never);
1002010246
CHECK_PARSE("UseTab: true", UseTab, FormatStyle::UT_Always);

0 commit comments

Comments
 (0)
Please sign in to comment.