Skip to content

Commit

Permalink
[clang-format] Don't count template template parameter as declaration (
Browse files Browse the repository at this point in the history
…#95025)

In ContinuationIndenter::mustBreak, a break is required between a
template declaration and the function/class declaration it applies to,
if the template declaration spans multiple lines.

However, this also includes template template parameters, which can
cause extra erroneous line breaks in some declarations.

This patch makes template template parameters not be counted as template
declarations.

Fixes #93793
Fixes #48746
  • Loading branch information
rymiel authored Jun 22, 2024
1 parent 8e9c6bf commit 4a7bf42
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
30 changes: 19 additions & 11 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class AnnotatingParser {
SmallVector<ScopeType> &Scopes)
: Style(Style), Line(Line), CurrentToken(Line.First), AutoFound(false),
IsCpp(Style.isCpp()), LangOpts(getFormattingLangOpts(Style)),
Keywords(Keywords), Scopes(Scopes) {
Keywords(Keywords), Scopes(Scopes), TemplateDeclarationDepth(0) {
assert(IsCpp == LangOpts.CXXOperatorNames);
Contexts.push_back(Context(tok::unknown, 1, /*IsExpression=*/false));
resetTokenMetadata();
Expand Down Expand Up @@ -1266,16 +1266,22 @@ class AnnotatingParser {
}

bool parseTemplateDeclaration() {
if (CurrentToken && CurrentToken->is(tok::less)) {
CurrentToken->setType(TT_TemplateOpener);
next();
if (!parseAngle())
return false;
if (CurrentToken)
CurrentToken->Previous->ClosesTemplateDeclaration = true;
return true;
}
return false;
if (!CurrentToken || CurrentToken->isNot(tok::less))
return false;

CurrentToken->setType(TT_TemplateOpener);
next();

TemplateDeclarationDepth++;
const bool WellFormed = parseAngle();
TemplateDeclarationDepth--;
if (!WellFormed)
return false;

if (CurrentToken && TemplateDeclarationDepth == 0)
CurrentToken->Previous->ClosesTemplateDeclaration = true;

return true;
}

bool consumeToken() {
Expand Down Expand Up @@ -3091,6 +3097,8 @@ class AnnotatingParser {
// same decision irrespective of the decisions for tokens leading up to it.
// Store this information to prevent this from causing exponential runtime.
llvm::SmallPtrSet<FormatToken *, 16> NonTemplateLess;

int TemplateDeclarationDepth;
};

static const int PrecedenceUnaryOperator = prec::PointerToMember + 1;
Expand Down
17 changes: 17 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,23 @@ TEST_F(TokenAnnotatorTest, UnderstandsNonTemplateAngleBrackets) {
EXPECT_TOKEN(Tokens[20], tok::greater, TT_BinaryOperator);
}

TEST_F(TokenAnnotatorTest, UnderstandsTemplateTemplateParameters) {
auto Tokens = annotate("template <template <typename...> typename X,\n"
" template <typename...> class Y,\n"
" typename... T>\n"
"class A {};");
ASSERT_EQ(Tokens.size(), 28u) << Tokens;
EXPECT_TOKEN(Tokens[1], tok::less, TT_TemplateOpener);
EXPECT_TOKEN(Tokens[3], tok::less, TT_TemplateOpener);
EXPECT_TOKEN(Tokens[6], tok::greater, TT_TemplateCloser);
EXPECT_FALSE(Tokens[6]->ClosesTemplateDeclaration);
EXPECT_TOKEN(Tokens[11], tok::less, TT_TemplateOpener);
EXPECT_TOKEN(Tokens[14], tok::greater, TT_TemplateCloser);
EXPECT_FALSE(Tokens[14]->ClosesTemplateDeclaration);
EXPECT_TOKEN(Tokens[21], tok::greater, TT_TemplateCloser);
EXPECT_TRUE(Tokens[21]->ClosesTemplateDeclaration);
}

TEST_F(TokenAnnotatorTest, UnderstandsWhitespaceSensitiveMacros) {
FormatStyle Style = getLLVMStyle();
Style.WhitespaceSensitiveMacros.push_back("FOO");
Expand Down

0 comments on commit 4a7bf42

Please sign in to comment.