Skip to content

Commit 8d93d7f

Browse files
committed
[clang-format] Add options to AllowShortIfStatementsOnASingleLine to apply to "else if" and "else".
This fixes the bug http://llvm.org/pr50019. Reviewed By: MyDeveloperDay Differential Revision: https://reviews.llvm.org/D100727
1 parent f9c8ebd commit 8d93d7f

File tree

5 files changed

+412
-17
lines changed

5 files changed

+412
-17
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
========================================
1+
q========================================
22
Clang 13.0.0 (In-Progress) Release Notes
33
========================================
44

@@ -239,6 +239,10 @@ clang-format
239239
- Option ``SpacesInAngles`` has been improved, it now accepts ``Leave`` value
240240
that allows to keep spaces where they are already present.
241241

242+
- Option ``AllowShortIfStatementsOnASingleLine`` has been improved, it now
243+
accepts ``AllIfsAndElse`` value that allows to put "else if" and "else" short
244+
statements on a single line. (Fixes https://llvm.org/PR50019.)
245+
242246
libclang
243247
--------
244248

clang/include/clang/Format/Format.h

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -620,37 +620,74 @@ struct FormatStyle {
620620
/// single line.
621621
ShortFunctionStyle AllowShortFunctionsOnASingleLine;
622622

623-
/// Different styles for handling short if lines
623+
/// Different styles for handling short if statements.
624624
enum ShortIfStyle : unsigned char {
625625
/// Never put short ifs on the same line.
626626
/// \code
627627
/// if (a)
628-
/// return ;
628+
/// return;
629+
///
630+
/// if (b)
631+
/// return;
632+
/// else
633+
/// return;
634+
///
635+
/// if (c)
636+
/// return;
629637
/// else {
630638
/// return;
631639
/// }
632640
/// \endcode
633641
SIS_Never,
634-
/// Without else put short ifs on the same line only if
635-
/// the else is not a compound statement.
642+
/// Put short ifs on the same line only if there is no else statement.
636643
/// \code
637644
/// if (a) return;
645+
///
646+
/// if (b)
647+
/// return;
638648
/// else
639649
/// return;
650+
///
651+
/// if (c)
652+
/// return;
653+
/// else {
654+
/// return;
655+
/// }
640656
/// \endcode
641657
SIS_WithoutElse,
642-
/// Always put short ifs on the same line if
643-
/// the else is not a compound statement or not.
658+
/// Put short ifs, but not else ifs nor else statements, on the same line.
644659
/// \code
645660
/// if (a) return;
661+
///
662+
/// if (b) return;
663+
/// else if (b)
664+
/// return;
665+
/// else
666+
/// return;
667+
///
668+
/// if (c) return;
669+
/// else {
670+
/// return;
671+
/// }
672+
/// \endcode
673+
SIS_OnlyFirstIf,
674+
/// Always put short ifs, else ifs and else statements on the same
675+
/// line.
676+
/// \code
677+
/// if (a) return;
678+
///
679+
/// if (b) return;
680+
/// else return;
681+
///
682+
/// if (c) return;
646683
/// else {
647684
/// return;
648685
/// }
649686
/// \endcode
650-
SIS_Always,
687+
SIS_AllIfsAndElse,
651688
};
652689

653-
/// If ``true``, ``if (a) return;`` can be put on a single line.
690+
/// Dependent on the value, ``if (a) return;`` can be put on a single line.
654691
ShortIfStyle AllowShortIfStatementsOnASingleLine;
655692

656693
/// Different styles for merging short lambdas containing at most one

clang/lib/Format/Format.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,12 @@ template <> struct ScalarEnumerationTraits<FormatStyle::AlignConsecutiveStyle> {
146146
template <> struct ScalarEnumerationTraits<FormatStyle::ShortIfStyle> {
147147
static void enumeration(IO &IO, FormatStyle::ShortIfStyle &Value) {
148148
IO.enumCase(Value, "Never", FormatStyle::SIS_Never);
149-
IO.enumCase(Value, "Always", FormatStyle::SIS_Always);
150149
IO.enumCase(Value, "WithoutElse", FormatStyle::SIS_WithoutElse);
150+
IO.enumCase(Value, "OnlyFirstIf", FormatStyle::SIS_OnlyFirstIf);
151+
IO.enumCase(Value, "AllIfsAndElse", FormatStyle::SIS_AllIfsAndElse);
151152

152153
// For backward compatibility.
154+
IO.enumCase(Value, "Always", FormatStyle::SIS_OnlyFirstIf);
153155
IO.enumCase(Value, "false", FormatStyle::SIS_Never);
154156
IO.enumCase(Value, "true", FormatStyle::SIS_WithoutElse);
155157
}

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,16 @@ class LineJoiner {
421421
}
422422
return MergedLines;
423423
}
424-
if (TheLine->First->is(tok::kw_if)) {
424+
auto IsElseLine = [&First = TheLine->First]() -> bool {
425+
if (First->is(tok::kw_else))
426+
return true;
427+
428+
return First->is(tok::r_brace) && First->Next &&
429+
First->Next->is(tok::kw_else);
430+
};
431+
if (TheLine->First->is(tok::kw_if) ||
432+
(IsElseLine() && (Style.AllowShortIfStatementsOnASingleLine ==
433+
FormatStyle::SIS_AllIfsAndElse))) {
425434
return Style.AllowShortIfStatementsOnASingleLine
426435
? tryMergeSimpleControlStatement(I, E, Limit)
427436
: 0;
@@ -471,7 +480,8 @@ class LineJoiner {
471480
return 0;
472481
Limit = limitConsideringMacros(I + 1, E, Limit);
473482
AnnotatedLine &Line = **I;
474-
if (!Line.First->is(tok::kw_do) && Line.Last->isNot(tok::r_paren))
483+
if (!Line.First->is(tok::kw_do) && !Line.First->is(tok::kw_else) &&
484+
!Line.Last->is(tok::kw_else) && Line.Last->isNot(tok::r_paren))
475485
return 0;
476486
// Only merge do while if do is the only statement on the line.
477487
if (Line.First->is(tok::kw_do) && !Line.Last->is(tok::kw_do))
@@ -482,7 +492,8 @@ class LineJoiner {
482492
TT_LineComment))
483493
return 0;
484494
// Only inline simple if's (no nested if or else), unless specified
485-
if (Style.AllowShortIfStatementsOnASingleLine != FormatStyle::SIS_Always) {
495+
if (Style.AllowShortIfStatementsOnASingleLine ==
496+
FormatStyle::SIS_WithoutElse) {
486497
if (I + 2 != E && Line.startsWith(tok::kw_if) &&
487498
I[2]->First->is(tok::kw_else))
488499
return 0;

0 commit comments

Comments
 (0)