Skip to content

Commit

Permalink
[clang-format] Fix a bug in removing braces for the LLVM style
Browse files Browse the repository at this point in the history
When an l_brace is wrapped and the line above it ends with a
comment, the annotator adds ColumnLimit to the TotalLength of the
l_brace, so the actual column position of the l_brace must be
adjusted accordingly.

Fixes #57376.

Differential Revision: https://reviews.llvm.org/D132805
  • Loading branch information
owenca committed Aug 28, 2022
1 parent bb6a437 commit f00f2b3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions clang/lib/Format/UnwrappedLineParser.cpp
Expand Up @@ -815,6 +815,10 @@ bool UnwrappedLineParser::mightFitOnOneLine(
auto Length = LastToken->TotalLength;
if (OpeningBrace) {
assert(OpeningBrace != Tokens.front().Tok);
if (auto Prev = OpeningBrace->Previous;
Prev && Prev->TotalLength + ColumnLimit == OpeningBrace->TotalLength) {
Length -= ColumnLimit;
}
Length -= OpeningBrace->TokenText.size() + 1;
}

Expand Down
8 changes: 8 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Expand Up @@ -25815,6 +25815,14 @@ TEST_F(FormatTest, RemoveBraces) {
"}",
Style);

verifyFormat("if (a) // comment\n"
" b = 1;",
"if (a) // comment\n"
"{\n"
" b = 1;\n"
"}",
Style);

verifyFormat("if (a) {\n"
"Label:\n"
"}",
Expand Down

0 comments on commit f00f2b3

Please sign in to comment.