diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3270,12 +3270,13 @@ return Right.WhitespaceRange.getBegin() != Right.WhitespaceRange.getEnd(); if (Right.is(tok::coloncolon) && !Left.isOneOf(tok::l_brace, tok::comment, tok::l_paren)) + // Put a space between < and :: in vector< ::std::string > return (Left.is(TT_TemplateOpener) && - Style.Standard < FormatStyle::LS_Cpp11) || + (Style.Standard < FormatStyle::LS_Cpp11 || Style.SpacesInAngles)) || !(Left.isOneOf(tok::l_paren, tok::r_paren, tok::l_square, - tok::kw___super, TT_TemplateCloser, - TT_TemplateOpener)) || - (Left.is(tok ::l_paren) && Style.SpacesInParentheses); + tok::kw___super, TT_TemplateOpener, + TT_TemplateCloser)) || + (Left.is(tok::l_paren) && Style.SpacesInParentheses); if ((Left.is(TT_TemplateOpener)) != (Right.is(TT_TemplateCloser))) return Style.SpacesInAngles; // Space before TT_StructuredBindingLSquare. diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -8526,6 +8526,20 @@ verifyFormat("vector< int > x{ };", SpaceBetweenBraces); } +TEST_F(FormatTest, FormatSpacesInAngles) { + FormatStyle SpaceInAngles = getLLVMStyle(); + SpaceInAngles.SpacesInAngles = true; + verifyFormat("vector< ::std::string > x1;", SpaceInAngles); + verifyFormat("Foo< int, Bar > x2;", SpaceInAngles); + verifyFormat("Foo< ::int, ::Bar > x3;", SpaceInAngles); + + SpaceInAngles.SpacesInAngles = false; + verifyFormat("vector<::std::string> x4;", SpaceInAngles); + verifyFormat("vector x5;", SpaceInAngles); + verifyFormat("Foo x6;", SpaceInAngles); + verifyFormat("Foo<::int, ::Bar> x7;", SpaceInAngles); +} + TEST_F(FormatTest, FormatsBracedListsInColumnLayout) { verifyFormat("vector x = {1, 22, 333, 4444, 55555, 666666, 7777777,\n" " 1, 22, 333, 4444, 55555, 666666, 7777777,\n"