Skip to content

Commit

Permalink
[clang-format] Correct line breaks in C# generic type constraints
Browse files Browse the repository at this point in the history
Reviewers: krasimir

Reviewed By: krasimir

Subscribers: cfe-commits

Tags: #clang-format, #clang

Differential Revision: https://reviews.llvm.org/D77064
  • Loading branch information
jbcoe committed Mar 31, 2020
1 parent 38aebe5 commit d1b412a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions clang/lib/Format/ContinuationIndenter.cpp
Expand Up @@ -346,6 +346,11 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
Current.startsSequence(TT_SelectorName, tok::colon, tok::caret)) {
return true;
}
// Avoid producing inconsistent states by requiring breaks where they are not
// permitted for C# generic type constraints.
if (State.Stack.back().IsCSharpGenericTypeConstraint &&
Previous.isNot(TT_CSharpGenericTypeConstraintComma))
return false;
if ((startsNextParameter(Current, Style) || Previous.is(tok::semi) ||
(Previous.is(TT_TemplateCloser) && Current.is(TT_StartOfName) &&
Style.isCpp() &&
Expand Down
7 changes: 6 additions & 1 deletion clang/lib/Format/TokenAnnotator.cpp
Expand Up @@ -1060,15 +1060,20 @@ class AnnotatingParser {
}

void parseCSharpGenericTypeConstraint() {
int OpenAngleBracketsCount = 0;
while (CurrentToken) {
if (CurrentToken->is(tok::less)) {
// parseAngle is too greedy and will consume the whole line.
CurrentToken->Type = TT_TemplateOpener;
++OpenAngleBracketsCount;
next();
} else if (CurrentToken->is(tok::greater)) {
CurrentToken->Type = TT_TemplateCloser;
--OpenAngleBracketsCount;
next();
} else if (CurrentToken->is(tok::comma)) {
} else if (CurrentToken->is(tok::comma) && OpenAngleBracketsCount == 0) {
// We allow line breaks after GenericTypeConstraintComma's
// so do not flag commas in Generics as GenericTypeConstraintComma's.
CurrentToken->Type = TT_CSharpGenericTypeConstraintComma;
next();
} else if (CurrentToken->is(Keywords.kw_where)) {
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Format/UnwrappedLineParser.cpp
Expand Up @@ -2340,6 +2340,12 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
}
if (FormatTok->Tok.is(tok::semi))
return;
if (Style.isCSharp() && FormatTok->is(Keywords.kw_where)) {
addUnwrappedLine();
nextToken();
parseCSharpGenericTypeConstraint();
break;
}
nextToken();
}
}
Expand Down
11 changes: 10 additions & 1 deletion clang/unittests/Format/FormatTestCSharp.cpp
Expand Up @@ -564,7 +564,7 @@ var myDict = new Dictionary<string, string> {

TEST_F(FormatTestCSharp, CSharpArrayInitializers) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);

verifyFormat(R"(//
private MySet<Node>[] setPoints = {
new Point<Node>(),
Expand Down Expand Up @@ -710,6 +710,15 @@ class ItemFactory<T>
IAnotherInterfaceStill<T> {})",
Style);

Style.ColumnLimit = 50; // Force lines to be wrapped.
verifyFormat(R"(//
class ItemFactory<T, U>
where T : new(),
IAnInterface<T>,
IAnotherInterface<T, U>,
IAnotherInterfaceStill<T, U> {})",
Style);

// In other languages `where` can be used as a normal identifier.
// This example is in C++!
verifyFormat(R"(//
Expand Down

0 comments on commit d1b412a

Please sign in to comment.