Skip to content

Commit 908fd09

Browse files
authored
[clang-format] Simplify the AfterPlacementOperator option (#79796)
Change AfterPlacementOperator to a boolean and deprecate SBPO_Never, which meant never inserting a space except when after new/delete. Fixes #78892.
1 parent a8279a8 commit 908fd09

File tree

6 files changed

+38
-130
lines changed

6 files changed

+38
-130
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5277,15 +5277,9 @@ the configuration (without a prefix: ``Auto``).
52775277
Possible values:
52785278

52795279
* ``SBPO_Never`` (in configuration: ``Never``)
5280-
Never put a space before opening parentheses.
5281-
5282-
.. code-block:: c++
5283-
5284-
void f() {
5285-
if(true) {
5286-
f();
5287-
}
5288-
}
5280+
This is **deprecated** and replaced by ``Custom`` below, with all
5281+
``SpaceBeforeParensOptions`` but ``AfterPlacementOperator`` set to
5282+
``false``.
52895283

52905284
* ``SBPO_ControlStatements`` (in configuration: ``ControlStatements``)
52915285
Put a space before opening parentheses only after control statement
@@ -5425,32 +5419,14 @@ the configuration (without a prefix: ``Auto``).
54255419
void operator++ (int a); vs. void operator++(int a);
54265420
object.operator++ (10); object.operator++(10);
54275421

5428-
* ``AfterPlacementOperatorStyle AfterPlacementOperator`` :versionbadge:`clang-format 18`
5429-
5430-
Defines in which cases to put a space between ``new/delete`` operators
5431-
and opening parentheses.
5432-
5433-
Possible values:
5434-
5435-
* ``APO_Never`` (in configuration: ``Never``)
5436-
Remove space after ``new/delete`` operators and before ``(``.
5437-
5438-
.. code-block:: c++
5439-
5440-
new(buf) T;
5441-
delete(buf) T;
5442-
5443-
* ``APO_Always`` (in configuration: ``Always``)
5444-
Always add space after ``new/delete`` operators and before ``(``.
5422+
* ``bool AfterPlacementOperator`` If ``true``, put a space between operator ``new``/``delete`` and opening
5423+
parenthesis.
54455424

5446-
.. code-block:: c++
5447-
5448-
new (buf) T;
5449-
delete (buf) T;
5450-
5451-
* ``APO_Leave`` (in configuration: ``Leave``)
5452-
Leave placement ``new/delete`` expressions as they are.
5425+
.. code-block:: c++
54535426

5427+
true: false:
5428+
new (buf) T; vs. new(buf) T;
5429+
delete (buf) T; delete(buf) T;
54545430

54555431
* ``bool AfterRequiresInClause`` If ``true``, put space between requires keyword in a requires clause and
54565432
opening parentheses, if there is one.

clang/include/clang/Format/Format.h

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4157,14 +4157,9 @@ struct FormatStyle {
41574157

41584158
/// Different ways to put a space before opening parentheses.
41594159
enum SpaceBeforeParensStyle : int8_t {
4160-
/// Never put a space before opening parentheses.
4161-
/// \code
4162-
/// void f() {
4163-
/// if(true) {
4164-
/// f();
4165-
/// }
4166-
/// }
4167-
/// \endcode
4160+
/// This is **deprecated** and replaced by ``Custom`` below, with all
4161+
/// ``SpaceBeforeParensOptions`` but ``AfterPlacementOperator`` set to
4162+
/// ``false``.
41684163
SBPO_Never,
41694164
/// Put a space before opening parentheses only after control statement
41704165
/// keywords (``for/if/while...``).
@@ -4273,28 +4268,14 @@ struct FormatStyle {
42734268
/// object.operator++ (10); object.operator++(10);
42744269
/// \endcode
42754270
bool AfterOverloadedOperator;
4276-
/// Styles for adding spacing between ``new/delete`` operators and opening
4277-
/// parentheses.
4278-
enum AfterPlacementOperatorStyle : int8_t {
4279-
/// Remove space after ``new/delete`` operators and before ``(``.
4280-
/// \code
4281-
/// new(buf) T;
4282-
/// delete(buf) T;
4283-
/// \endcode
4284-
APO_Never,
4285-
/// Always add space after ``new/delete`` operators and before ``(``.
4286-
/// \code
4287-
/// new (buf) T;
4288-
/// delete (buf) T;
4289-
/// \endcode
4290-
APO_Always,
4291-
/// Leave placement ``new/delete`` expressions as they are.
4292-
APO_Leave,
4293-
};
4294-
/// Defines in which cases to put a space between ``new/delete`` operators
4295-
/// and opening parentheses.
4296-
/// \version 18
4297-
AfterPlacementOperatorStyle AfterPlacementOperator;
4271+
/// If ``true``, put a space between operator ``new``/``delete`` and opening
4272+
/// parenthesis.
4273+
/// \code
4274+
/// true: false:
4275+
/// new (buf) T; vs. new(buf) T;
4276+
/// delete (buf) T; delete(buf) T;
4277+
/// \endcode
4278+
bool AfterPlacementOperator;
42984279
/// If ``true``, put space between requires keyword in a requires clause and
42994280
/// opening parentheses, if there is one.
43004281
/// \code
@@ -4327,7 +4308,7 @@ struct FormatStyle {
43274308
: AfterControlStatements(false), AfterForeachMacros(false),
43284309
AfterFunctionDeclarationName(false),
43294310
AfterFunctionDefinitionName(false), AfterIfMacros(false),
4330-
AfterOverloadedOperator(false), AfterPlacementOperator(APO_Leave),
4311+
AfterOverloadedOperator(false), AfterPlacementOperator(true),
43314312
AfterRequiresInClause(false), AfterRequiresInExpression(false),
43324313
BeforeNonEmptyParentheses(false) {}
43334314

clang/lib/Format/Format.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -504,22 +504,6 @@ struct ScalarEnumerationTraits<FormatStyle::QualifierAlignmentStyle> {
504504
}
505505
};
506506

507-
template <>
508-
struct MappingTraits<
509-
FormatStyle::SpaceBeforeParensCustom::AfterPlacementOperatorStyle> {
510-
static void
511-
mapping(IO &IO,
512-
FormatStyle::SpaceBeforeParensCustom::AfterPlacementOperatorStyle
513-
&Value) {
514-
IO.enumCase(Value, "Always",
515-
FormatStyle::SpaceBeforeParensCustom::APO_Always);
516-
IO.enumCase(Value, "Never",
517-
FormatStyle::SpaceBeforeParensCustom::APO_Never);
518-
IO.enumCase(Value, "Leave",
519-
FormatStyle::SpaceBeforeParensCustom::APO_Leave);
520-
}
521-
};
522-
523507
template <> struct MappingTraits<FormatStyle::RawStringFormat> {
524508
static void mapping(IO &IO, FormatStyle::RawStringFormat &Format) {
525509
IO.mapOptional("Language", Format.Language);
@@ -1388,12 +1372,9 @@ static void expandPresetsSpaceBeforeParens(FormatStyle &Expanded) {
13881372
return;
13891373
// Reset all flags
13901374
Expanded.SpaceBeforeParensOptions = {};
1375+
Expanded.SpaceBeforeParensOptions.AfterPlacementOperator = true;
13911376

13921377
switch (Expanded.SpaceBeforeParens) {
1393-
case FormatStyle::SBPO_Never:
1394-
Expanded.SpaceBeforeParensOptions.AfterPlacementOperator =
1395-
FormatStyle::SpaceBeforeParensCustom::APO_Never;
1396-
break;
13971378
case FormatStyle::SBPO_ControlStatements:
13981379
Expanded.SpaceBeforeParensOptions.AfterControlStatements = true;
13991380
Expanded.SpaceBeforeParensOptions.AfterForeachMacros = true;
@@ -1405,8 +1386,6 @@ static void expandPresetsSpaceBeforeParens(FormatStyle &Expanded) {
14051386
case FormatStyle::SBPO_NonEmptyParentheses:
14061387
Expanded.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
14071388
break;
1408-
case FormatStyle::SBPO_Always:
1409-
break;
14101389
default:
14111390
break;
14121391
}

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4274,14 +4274,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
42744274
Left.isOneOf(tok::kw_new, tok::kw_delete) &&
42754275
Right.isNot(TT_OverloadedOperatorLParen) &&
42764276
!(Line.MightBeFunctionDecl && Left.is(TT_FunctionDeclarationName))) {
4277-
if (Style.SpaceBeforeParensOptions.AfterPlacementOperator ==
4278-
FormatStyle::SpaceBeforeParensCustom::APO_Always ||
4279-
(Style.SpaceBeforeParensOptions.AfterPlacementOperator ==
4280-
FormatStyle::SpaceBeforeParensCustom::APO_Leave &&
4281-
Right.hasWhitespaceBefore())) {
4282-
return true;
4283-
}
4284-
return false;
4277+
return Style.SpaceBeforeParensOptions.AfterPlacementOperator;
42854278
}
42864279
if (Line.Type == LT_ObjCDecl)
42874280
return true;

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
231231
AfterFunctionDefinitionName);
232232
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
233233
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
234+
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterPlacementOperator);
234235
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
235236
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InCStyleCasts);
236237
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements);
@@ -609,24 +610,6 @@ TEST(ConfigParseTest, ParsesConfiguration) {
609610
SpaceBeforeParens,
610611
FormatStyle::SBPO_ControlStatementsExceptControlMacros);
611612

612-
Style.SpaceBeforeParens = FormatStyle::SBPO_Custom;
613-
Style.SpaceBeforeParensOptions.AfterPlacementOperator =
614-
FormatStyle::SpaceBeforeParensCustom::APO_Always;
615-
CHECK_PARSE("SpaceBeforeParensOptions:\n"
616-
" AfterPlacementOperator: Never",
617-
SpaceBeforeParensOptions.AfterPlacementOperator,
618-
FormatStyle::SpaceBeforeParensCustom::APO_Never);
619-
620-
CHECK_PARSE("SpaceBeforeParensOptions:\n"
621-
" AfterPlacementOperator: Always",
622-
SpaceBeforeParensOptions.AfterPlacementOperator,
623-
FormatStyle::SpaceBeforeParensCustom::APO_Always);
624-
625-
CHECK_PARSE("SpaceBeforeParensOptions:\n"
626-
" AfterPlacementOperator: Leave",
627-
SpaceBeforeParensOptions.AfterPlacementOperator,
628-
FormatStyle::SpaceBeforeParensCustom::APO_Leave);
629-
630613
// For backward compatibility:
631614
Style.SpacesInParens = FormatStyle::SIPO_Never;
632615
Style.SpacesInParensOptions = {};

clang/unittests/Format/FormatTest.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11347,35 +11347,31 @@ TEST_F(FormatTest, UnderstandsNewAndDelete) {
1134711347

1134811348
FormatStyle AfterPlacementOperator = getLLVMStyle();
1134911349
AfterPlacementOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
11350-
EXPECT_EQ(
11351-
AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator,
11352-
FormatStyle::SpaceBeforeParensCustom::APO_Leave);
11350+
EXPECT_TRUE(
11351+
AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator);
1135311352
verifyFormat("new (buf) int;", AfterPlacementOperator);
11354-
verifyFormat("new(buf) int;", AfterPlacementOperator);
11355-
11356-
AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator =
11357-
FormatStyle::SpaceBeforeParensCustom::APO_Never;
1135811353
verifyFormat("struct A {\n"
1135911354
" int *a;\n"
11360-
" A(int *p) : a(new(p) int) {\n"
11361-
" new(p) int;\n"
11362-
" int *b = new(p) int;\n"
11363-
" int *c = new(p) int(3);\n"
11364-
" delete(b);\n"
11355+
" A(int *p) : a(new (p) int) {\n"
11356+
" new (p) int;\n"
11357+
" int *b = new (p) int;\n"
11358+
" int *c = new (p) int(3);\n"
11359+
" delete (b);\n"
1136511360
" }\n"
1136611361
"};",
1136711362
AfterPlacementOperator);
1136811363
verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);
1136911364

1137011365
AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator =
11371-
FormatStyle::SpaceBeforeParensCustom::APO_Always;
11366+
false;
11367+
verifyFormat("new(buf) int;", AfterPlacementOperator);
1137211368
verifyFormat("struct A {\n"
1137311369
" int *a;\n"
11374-
" A(int *p) : a(new (p) int) {\n"
11375-
" new (p) int;\n"
11376-
" int *b = new (p) int;\n"
11377-
" int *c = new (p) int(3);\n"
11378-
" delete (b);\n"
11370+
" A(int *p) : a(new(p) int) {\n"
11371+
" new(p) int;\n"
11372+
" int *b = new(p) int;\n"
11373+
" int *c = new(p) int(3);\n"
11374+
" delete(b);\n"
1137911375
" }\n"
1138011376
"};",
1138111377
AfterPlacementOperator);

0 commit comments

Comments
 (0)