Skip to content

Commit 7a074db

Browse files
committed
[clang-format] allow short function body on a single line
Signed-off-by: Lidong Yan <[email protected]>
1 parent 3e42146 commit 7a074db

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

clang/include/clang/Format/Format.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,12 @@ struct FormatStyle {
878878
/// \version 3.5
879879
ShortFunctionStyle AllowShortFunctionsOnASingleLine;
880880

881+
/// Dependent on the value, function body like ``{ return 0; }`` can be
882+
/// put on a single line. Only when AllowShortFunctionsOnASingleLine = None
883+
/// and AllowShortBlocksOnASingleLine != Never, the value of this option
884+
/// is true.
885+
bool AllowShortFunctionBodiesOnASingleLine;
886+
881887
/// Different styles for handling short if statements.
882888
enum ShortIfStyle : int8_t {
883889
/// Never put short ifs on the same line.

clang/lib/Format/Format.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3828,6 +3828,9 @@ reformat(const FormatStyle &Style, StringRef Code,
38283828
default:
38293829
break;
38303830
}
3831+
Expanded.AllowShortFunctionBodiesOnASingleLine =
3832+
Expanded.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None &&
3833+
Expanded.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never;
38313834

38323835
if (Expanded.DisableFormat)
38333836
return {tooling::Replacements(), 0};

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ class LineJoiner {
257257
return tryMergeSimpleBlock(I, E, Limit);
258258
}
259259

260+
if (TheLine->Last->is(TT_FunctionLBrace) &&
261+
TheLine->First == TheLine->Last &&
262+
Style.AllowShortFunctionBodiesOnASingleLine) {
263+
return tryMergeSimpleBlock(I, E, Limit);
264+
}
265+
260266
const auto *PreviousLine = I != AnnotatedLines.begin() ? I[-1] : nullptr;
261267
// Handle empty record blocks where the brace has already been wrapped.
262268
if (PreviousLine && TheLine->Last->is(tok::l_brace) &&
@@ -532,6 +538,20 @@ class LineJoiner {
532538
}
533539
return MergedLines;
534540
}
541+
542+
// Previously, UnwrappedLineParser would move the left brace to a new line
543+
// when AllowShortFunctionBodiesOnASingleLine is enabled. However, if the
544+
// function body cannot fit on a single line, and Style.BraceWrapping.AfterFunction
545+
// is false, we should merge the function name and the left brace back onto
546+
// the same line
547+
if (NextLine.First->is(TT_FunctionLBrace) &&
548+
Style.AllowShortFunctionBodiesOnASingleLine &&
549+
!Style.BraceWrapping.AfterFunction) {
550+
unsigned MergedLines = 0;
551+
MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
552+
return MergedLines > 0 ? 0 : 1;
553+
}
554+
535555
auto IsElseLine = [&TheLine]() -> bool {
536556
const FormatToken *First = TheLine->First;
537557
if (First->is(tok::kw_else))

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1921,6 +1921,10 @@ void UnwrappedLineParser::parseStructuralElement(
19211921
}
19221922
} else if (Style.BraceWrapping.AfterFunction) {
19231923
addUnwrappedLine();
1924+
} else if (Style.AllowShortFunctionBodiesOnASingleLine) {
1925+
// Wrap the left brace here; we'll try to merge it back
1926+
// later if needed.
1927+
addUnwrappedLine();
19241928
}
19251929
if (!Previous || Previous->isNot(TT_TypeDeclarationParen))
19261930
FormatTok->setFinalizedType(TT_FunctionLBrace);

0 commit comments

Comments
 (0)