Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2596,15 +2596,15 @@ the configuration (without a prefix: ``Auto``).
};

* ``BWER_BeforeBrace`` (in configuration: ``BeforeBrace``)
Only wrap before brace (equivalent to ``SplitEmptyRecord=false``).
Only wrap before brace.

.. code-block:: c++

class foo
{};

* ``BWER_Never`` (in configuration: ``Never``)
Wrap neither before or after the brace.
Wrap neither before nor after the brace.

.. code-block:: c++

Expand Down
6 changes: 3 additions & 3 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1365,13 +1365,13 @@ struct FormatStyle {
/// };
/// \endcode
BWER_Default,
/// Only wrap before brace (equivalent to ``SplitEmptyRecord=false``).
/// Only wrap before brace.
/// \code
/// class foo
/// {};
/// \endcode
BWER_BeforeBrace,
/// Wrap neither before or after the brace.
/// Wrap neither before nor after the brace.
/// \code
/// class foo {};
/// \endcode
Expand Down Expand Up @@ -1596,7 +1596,7 @@ struct FormatStyle {
/// }
/// \endcode
// bool SplitEmptyRecord;

/// If ``false``, empty namespace body can be put on a single line.
/// This option is used only if the opening brace of the namespace has
/// already been wrapped, i.e. the ``AfterNamespace`` brace wrapping mode is
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ template <> struct ScalarEnumerationTraits<FormatStyle::BraceBreakingStyle> {

template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
static void mapping(IO &IO, FormatStyle::BraceWrappingFlags &Wrapping) {
// For backward compatibility.
if (!IO.outputting())
IO.mapOptional("SplitEmptyRecord", Wrapping.WrapEmptyRecord);

IO.mapOptional("AfterCaseLabel", Wrapping.AfterCaseLabel);
IO.mapOptional("AfterClass", Wrapping.AfterClass);
IO.mapOptional("AfterControlStatement", Wrapping.AfterControlStatement);
Expand All @@ -200,9 +204,6 @@ template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
IO.mapOptional("SplitEmptyFunction", Wrapping.SplitEmptyFunction);
IO.mapOptional("SplitEmptyNamespace", Wrapping.SplitEmptyNamespace);
IO.mapOptional("WrapEmptyRecord", Wrapping.WrapEmptyRecord);

// For backward compatibility.
IO.mapOptional("SplitEmptyRecord", Wrapping.WrapEmptyRecord);
}
};

Expand Down Expand Up @@ -240,6 +241,7 @@ struct ScalarEnumerationTraits<FormatStyle::BraceWrapEmptyRecordStyle> {
static void enumeration(IO &IO,
FormatStyle::BraceWrapEmptyRecordStyle &Value) {
IO.enumCase(Value, "Default", FormatStyle::BWER_Default);
IO.enumCase(Value, "BeforeBrace", FormatStyle::BWER_BeforeBrace);
IO.enumCase(Value, "Never", FormatStyle::BWER_Never);

// For backward compatibility.
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5933,12 +5933,14 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
return true;
}

// Don't attempt to interpret struct return types as structs.
// Don't attempt to interpret record return types as records.
if (Right.isNot(TT_FunctionLBrace)) {
return ((Line.startsWith(tok::kw_class) &&
Style.BraceWrapping.AfterClass) ||
(Line.startsWith(tok::kw_struct) &&
Style.BraceWrapping.AfterStruct)) &&
Style.BraceWrapping.AfterStruct) ||
(Line.startsWith(tok::kw_union) &&
Style.BraceWrapping.AfterUnion)) &&
Style.BraceWrapping.WrapEmptyRecord == FormatStyle::BWER_Default;
}
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Format/UnwrappedLineFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ class LineJoiner {
}

// Merge an empty class or struct only if WrapEmptyRecord
// is not set to Default
// is not set to Default.
if (PreviousLine &&
Style.BraceWrapping.WrapEmptyRecord == FormatStyle::BWER_Default &&
TheLine->Last->is(tok::l_brace) && PreviousLine->Last) {
Expand Down
18 changes: 18 additions & 0 deletions clang/unittests/Format/ConfigParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,24 @@ TEST(ConfigParseTest, ParsesConfiguration) {
" AfterControlStatement: false",
BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);

Style.BraceWrapping.WrapEmptyRecord = FormatStyle::BWER_Default;
CHECK_PARSE("BraceWrapping:\n"
" WrapEmptyRecord: BeforeBrace",
BraceWrapping.WrapEmptyRecord, FormatStyle::BWER_BeforeBrace);
CHECK_PARSE("BraceWrapping:\n"
" WrapEmptyRecord: Default",
BraceWrapping.WrapEmptyRecord, FormatStyle::BWER_Default);
CHECK_PARSE("BraceWrapping:\n"
" WrapEmptyRecord: Never",
BraceWrapping.WrapEmptyRecord, FormatStyle::BWER_Never);
// For backward compatibility:
CHECK_PARSE("BraceWrapping:\n"
" WrapEmptyRecord: true",
BraceWrapping.WrapEmptyRecord, FormatStyle::BWER_Default);
CHECK_PARSE("BraceWrapping:\n"
" WrapEmptyRecord: false",
BraceWrapping.WrapEmptyRecord, FormatStyle::BWER_BeforeBrace);

Style.BreakAfterReturnType = FormatStyle::RTBS_All;
CHECK_PARSE("BreakAfterReturnType: None", BreakAfterReturnType,
FormatStyle::RTBS_None);
Expand Down
13 changes: 13 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8625,6 +8625,19 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
Style);
}

TEST_F(FormatTest, BreakFunctionsReturningRecords) {
FormatStyle Style = getLLVMStyle();
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
Style.BraceWrapping.AfterFunction = true;
Style.BraceWrapping.AfterClass = false;
Style.BraceWrapping.AfterStruct = false;
Style.BraceWrapping.AfterUnion = false;

verifyFormat("class Bar foo() {}", Style);
verifyFormat("struct Bar foo() {}", Style);
verifyFormat("union Bar foo() {}", Style);
}

TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
// Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
// Prefer keeping `::` followed by `operator` together.
Expand Down
Loading