Skip to content

Commit dd978ae

Browse files
committed
clang-format: Option to control spacing in template argument lists.
Same as SpacesInParentheses, this option allows adding a space inside the '<' and '>' of a template parameter list. Patch by Christopher Olsen. This fixes llvm.org/PR17301. llvm-svn: 193614
1 parent f34ac3e commit dd978ae

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

clang/include/clang/Format/Format.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,13 @@ struct FormatStyle {
221221
/// are not also definitions after the type.
222222
bool IndentFunctionDeclarationAfterType;
223223

224-
/// \brief If \c true, spaces will be inserted after every '(' and before
225-
/// every ')'.
224+
/// \brief If \c true, spaces will be inserted after '(' and before ')'.
226225
bool SpacesInParentheses;
227226

227+
/// \brief If \c true, spaces will be inserted after '<' and before '>' in
228+
/// template argument lists
229+
bool SpacesInAngles;
230+
228231
/// \brief If \c false, spaces may be inserted into '()'.
229232
bool SpaceInEmptyParentheses;
230233

@@ -284,6 +287,7 @@ struct FormatStyle {
284287
Cpp11BracedListStyle == R.Cpp11BracedListStyle &&
285288
Standard == R.Standard && TabWidth == R.TabWidth &&
286289
UseTab == R.UseTab && SpacesInParentheses == R.SpacesInParentheses &&
290+
SpacesInAngles == R.SpacesInAngles &&
287291
SpaceInEmptyParentheses == R.SpaceInEmptyParentheses &&
288292
SpacesInCStyleCastParentheses == R.SpacesInCStyleCastParentheses &&
289293
SpaceAfterControlStatementKeyword ==

clang/lib/Format/Format.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ template <> struct MappingTraits<clang::format::FormatStyle> {
158158
IO.mapOptional("IndentFunctionDeclarationAfterType",
159159
Style.IndentFunctionDeclarationAfterType);
160160
IO.mapOptional("SpacesInParentheses", Style.SpacesInParentheses);
161+
IO.mapOptional("SpacesInAngles", Style.SpacesInAngles);
161162
IO.mapOptional("SpaceInEmptyParentheses", Style.SpaceInEmptyParentheses);
162163
IO.mapOptional("SpacesInCStyleCastParentheses",
163164
Style.SpacesInCStyleCastParentheses);
@@ -218,6 +219,7 @@ FormatStyle getLLVMStyle() {
218219
LLVMStyle.SpaceAfterControlStatementKeyword = true;
219220
LLVMStyle.SpaceBeforeAssignmentOperators = true;
220221
LLVMStyle.ContinuationIndentWidth = 4;
222+
LLVMStyle.SpacesInAngles = false;
221223

222224
setDefaultPenalties(LLVMStyle);
223225
LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
@@ -263,6 +265,7 @@ FormatStyle getGoogleStyle() {
263265
GoogleStyle.SpaceAfterControlStatementKeyword = true;
264266
GoogleStyle.SpaceBeforeAssignmentOperators = true;
265267
GoogleStyle.ContinuationIndentWidth = 4;
268+
GoogleStyle.SpacesInAngles = false;
266269

267270
setDefaultPenalties(GoogleStyle);
268271
GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,9 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
12211221
(Left.MatchingParen && Left.MatchingParen->Type == TT_CastRParen))
12221222
? Style.SpacesInCStyleCastParentheses
12231223
: Style.SpacesInParentheses;
1224+
if (Style.SpacesInAngles &&
1225+
((Left.Type == TT_TemplateOpener) != (Right.Type == TT_TemplateCloser)))
1226+
return true;
12241227
if (Right.isOneOf(tok::semi, tok::comma))
12251228
return false;
12261229
if (Right.is(tok::less) &&
@@ -1350,7 +1353,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
13501353
if (Tok.Previous->is(tok::greater) && Tok.is(tok::greater)) {
13511354
return Tok.Type == TT_TemplateCloser &&
13521355
Tok.Previous->Type == TT_TemplateCloser &&
1353-
Style.Standard != FormatStyle::LS_Cpp11;
1356+
(Style.Standard != FormatStyle::LS_Cpp11 || Style.SpacesInAngles);
13541357
}
13551358
if (Tok.isOneOf(tok::arrowstar, tok::periodstar) ||
13561359
Tok.Previous->isOneOf(tok::arrowstar, tok::periodstar))

clang/unittests/Format/FormatTest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6592,6 +6592,7 @@ TEST_F(FormatTest, ParsesConfiguration) {
65926592
CHECK_PARSE_BOOL(Cpp11BracedListStyle);
65936593
CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType);
65946594
CHECK_PARSE_BOOL(SpacesInParentheses);
6595+
CHECK_PARSE_BOOL(SpacesInAngles);
65956596
CHECK_PARSE_BOOL(SpaceInEmptyParentheses);
65966597
CHECK_PARSE_BOOL(SpacesInCStyleCastParentheses);
65976598
CHECK_PARSE_BOOL(SpaceAfterControlStatementKeyword);
@@ -7010,5 +7011,30 @@ TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
70107011
format("int i = longFunction(arg);", SixIndent));
70117012
}
70127013

7014+
TEST_F(FormatTest, SpacesInAngles) {
7015+
FormatStyle Spaces = getLLVMStyle();
7016+
Spaces.SpacesInAngles = true;
7017+
7018+
verifyFormat("static_cast< int >(arg);", Spaces);
7019+
verifyFormat("template < typename T0, typename T1 > void f() {}", Spaces);
7020+
verifyFormat("f< int, float >();", Spaces);
7021+
verifyFormat("template <> g() {}", Spaces);
7022+
verifyFormat("template < std::vector< int > > f() {}", Spaces);
7023+
7024+
Spaces.Standard = FormatStyle::LS_Cpp03;
7025+
Spaces.SpacesInAngles = true;
7026+
verifyFormat("A< A< int > >();", Spaces);
7027+
7028+
Spaces.SpacesInAngles = false;
7029+
verifyFormat("A<A<int> >();", Spaces);
7030+
7031+
Spaces.Standard = FormatStyle::LS_Cpp11;
7032+
Spaces.SpacesInAngles = true;
7033+
verifyFormat("A< A< int > >();", Spaces);
7034+
7035+
Spaces.SpacesInAngles = false;
7036+
verifyFormat("A<A<int>>();", Spaces);
7037+
}
7038+
70137039
} // end namespace tooling
70147040
} // end namespace clang

0 commit comments

Comments
 (0)