Skip to content

Commit

Permalink
[clang-format] Simplify the AfterPlacementOperator option
Browse files Browse the repository at this point in the history
Change AfterPlacementOperator to a boolean. Also add SBPO_None for never
inserting a space before a left parenthesis and deprecate SBPO_Never, which
meant never inserting a space except when after new/delete.

Fixes #78892.
  • Loading branch information
owenca committed Jan 29, 2024
1 parent 179ade6 commit 46f4a74
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 114 deletions.
37 changes: 12 additions & 25 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5276,7 +5276,7 @@ the configuration (without a prefix: ``Auto``).

Possible values:

* ``SBPO_Never`` (in configuration: ``Never``)
* ``SBPO_None`` (in configuration: ``None``)
Never put a space before opening parentheses.

.. code-block:: c++
Expand All @@ -5287,6 +5287,11 @@ the configuration (without a prefix: ``Auto``).
}
}

* ``SBPO_Never`` (in configuration: ``Never``)
This is **deprecated** and replaced by ``Custom`` below, with all
``SpaceBeforeParensOptions`` but ``AfterPlacementOperator`` set to
``false``.

* ``SBPO_ControlStatements`` (in configuration: ``ControlStatements``)
Put a space before opening parentheses only after control statement
keywords (``for/if/while...``).
Expand Down Expand Up @@ -5425,32 +5430,14 @@ the configuration (without a prefix: ``Auto``).
void operator++ (int a); vs. void operator++(int a);
object.operator++ (10); object.operator++(10);

* ``AfterPlacementOperatorStyle AfterPlacementOperator`` :versionbadge:`clang-format 18`

Defines in which cases to put a space between ``new/delete`` operators
and opening parentheses.
* ``bool AfterPlacementOperator`` If ``true``, put a space between operator ``new``/``delete`` and opening
parenthesis.

Possible values:

* ``APO_Never`` (in configuration: ``Never``)
Remove space after ``new/delete`` operators and before ``(``.

.. code-block:: c++

new(buf) T;
delete(buf) T;

* ``APO_Always`` (in configuration: ``Always``)
Always add space after ``new/delete`` operators and before ``(``.

.. code-block:: c++

new (buf) T;
delete (buf) T;

* ``APO_Leave`` (in configuration: ``Leave``)
Leave placement ``new/delete`` expressions as they are.
.. code-block:: c++

true: false:
new (buf) T; vs. new(buf) T;
delete (buf) T; delete(buf) T;

* ``bool AfterRequiresInClause`` If ``true``, put space between requires keyword in a requires clause and
opening parentheses, if there is one.
Expand Down
36 changes: 13 additions & 23 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -4165,6 +4165,10 @@ struct FormatStyle {
/// }
/// }
/// \endcode
SBPO_None,
/// This is **deprecated** and replaced by ``Custom`` below, with all
/// ``SpaceBeforeParensOptions`` but ``AfterPlacementOperator`` set to
/// ``false``.
SBPO_Never,
/// Put a space before opening parentheses only after control statement
/// keywords (``for/if/while...``).
Expand Down Expand Up @@ -4273,28 +4277,14 @@ struct FormatStyle {
/// object.operator++ (10); object.operator++(10);
/// \endcode
bool AfterOverloadedOperator;
/// Styles for adding spacing between ``new/delete`` operators and opening
/// parentheses.
enum AfterPlacementOperatorStyle : int8_t {
/// Remove space after ``new/delete`` operators and before ``(``.
/// \code
/// new(buf) T;
/// delete(buf) T;
/// \endcode
APO_Never,
/// Always add space after ``new/delete`` operators and before ``(``.
/// \code
/// new (buf) T;
/// delete (buf) T;
/// \endcode
APO_Always,
/// Leave placement ``new/delete`` expressions as they are.
APO_Leave,
};
/// Defines in which cases to put a space between ``new/delete`` operators
/// and opening parentheses.
/// \version 18
AfterPlacementOperatorStyle AfterPlacementOperator;
/// If ``true``, put a space between operator ``new``/``delete`` and opening
/// parenthesis.
/// \code
/// true: false:
/// new (buf) T; vs. new(buf) T;
/// delete (buf) T; delete(buf) T;
/// \endcode
bool AfterPlacementOperator;
/// If ``true``, put space between requires keyword in a requires clause and
/// opening parentheses, if there is one.
/// \code
Expand Down Expand Up @@ -4327,7 +4317,7 @@ struct FormatStyle {
: AfterControlStatements(false), AfterForeachMacros(false),
AfterFunctionDeclarationName(false),
AfterFunctionDefinitionName(false), AfterIfMacros(false),
AfterOverloadedOperator(false), AfterPlacementOperator(APO_Leave),
AfterOverloadedOperator(false), AfterPlacementOperator(true),
AfterRequiresInClause(false), AfterRequiresInExpression(false),
BeforeNonEmptyParentheses(false) {}

Expand Down
28 changes: 6 additions & 22 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,22 +504,6 @@ struct ScalarEnumerationTraits<FormatStyle::QualifierAlignmentStyle> {
}
};

template <>
struct MappingTraits<
FormatStyle::SpaceBeforeParensCustom::AfterPlacementOperatorStyle> {
static void
mapping(IO &IO,
FormatStyle::SpaceBeforeParensCustom::AfterPlacementOperatorStyle
&Value) {
IO.enumCase(Value, "Always",
FormatStyle::SpaceBeforeParensCustom::APO_Always);
IO.enumCase(Value, "Never",
FormatStyle::SpaceBeforeParensCustom::APO_Never);
IO.enumCase(Value, "Leave",
FormatStyle::SpaceBeforeParensCustom::APO_Leave);
}
};

template <> struct MappingTraits<FormatStyle::RawStringFormat> {
static void mapping(IO &IO, FormatStyle::RawStringFormat &Format) {
IO.mapOptional("Language", Format.Language);
Expand Down Expand Up @@ -707,6 +691,7 @@ template <> struct MappingTraits<FormatStyle::SpaceBeforeParensCustom> {
template <>
struct ScalarEnumerationTraits<FormatStyle::SpaceBeforeParensStyle> {
static void enumeration(IO &IO, FormatStyle::SpaceBeforeParensStyle &Value) {
IO.enumCase(Value, "None", FormatStyle::SBPO_None);
IO.enumCase(Value, "Never", FormatStyle::SBPO_Never);
IO.enumCase(Value, "ControlStatements",
FormatStyle::SBPO_ControlStatements);
Expand Down Expand Up @@ -1389,11 +1374,12 @@ static void expandPresetsSpaceBeforeParens(FormatStyle &Expanded) {
// Reset all flags
Expanded.SpaceBeforeParensOptions = {};

if (Expanded.SpaceBeforeParens == FormatStyle::SBPO_None)
return;

Expanded.SpaceBeforeParensOptions.AfterPlacementOperator = true;

switch (Expanded.SpaceBeforeParens) {
case FormatStyle::SBPO_Never:
Expanded.SpaceBeforeParensOptions.AfterPlacementOperator =
FormatStyle::SpaceBeforeParensCustom::APO_Never;
break;
case FormatStyle::SBPO_ControlStatements:
Expanded.SpaceBeforeParensOptions.AfterControlStatements = true;
Expanded.SpaceBeforeParensOptions.AfterForeachMacros = true;
Expand All @@ -1405,8 +1391,6 @@ static void expandPresetsSpaceBeforeParens(FormatStyle &Expanded) {
case FormatStyle::SBPO_NonEmptyParentheses:
Expanded.SpaceBeforeParensOptions.BeforeNonEmptyParentheses = true;
break;
case FormatStyle::SBPO_Always:
break;
default:
break;
}
Expand Down
9 changes: 1 addition & 8 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4274,14 +4274,7 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
Left.isOneOf(tok::kw_new, tok::kw_delete) &&
Right.isNot(TT_OverloadedOperatorLParen) &&
!(Line.MightBeFunctionDecl && Left.is(TT_FunctionDeclarationName))) {
if (Style.SpaceBeforeParensOptions.AfterPlacementOperator ==
FormatStyle::SpaceBeforeParensCustom::APO_Always ||
(Style.SpaceBeforeParensOptions.AfterPlacementOperator ==
FormatStyle::SpaceBeforeParensCustom::APO_Leave &&
Right.hasWhitespaceBefore())) {
return true;
}
return false;
return Style.SpaceBeforeParensOptions.AfterPlacementOperator;
}
if (Line.Type == LT_ObjCDecl)
return true;
Expand Down
21 changes: 3 additions & 18 deletions clang/unittests/Format/ConfigParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
AfterFunctionDefinitionName);
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterIfMacros);
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterOverloadedOperator);
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, AfterPlacementOperator);
CHECK_PARSE_NESTED_BOOL(SpaceBeforeParensOptions, BeforeNonEmptyParentheses);
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InCStyleCasts);
CHECK_PARSE_NESTED_BOOL(SpacesInParensOptions, InConditionalStatements);
Expand Down Expand Up @@ -587,6 +588,8 @@ TEST(ConfigParseTest, ParsesConfiguration) {
SpaceAroundPointerQualifiers, FormatStyle::SAPQ_Both);

Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
CHECK_PARSE("SpaceBeforeParens: None", SpaceBeforeParens,
FormatStyle::SBPO_None);
CHECK_PARSE("SpaceBeforeParens: Never", SpaceBeforeParens,
FormatStyle::SBPO_Never);
CHECK_PARSE("SpaceBeforeParens: Always", SpaceBeforeParens,
Expand All @@ -609,24 +612,6 @@ TEST(ConfigParseTest, ParsesConfiguration) {
SpaceBeforeParens,
FormatStyle::SBPO_ControlStatementsExceptControlMacros);

Style.SpaceBeforeParens = FormatStyle::SBPO_Custom;
Style.SpaceBeforeParensOptions.AfterPlacementOperator =
FormatStyle::SpaceBeforeParensCustom::APO_Always;
CHECK_PARSE("SpaceBeforeParensOptions:\n"
" AfterPlacementOperator: Never",
SpaceBeforeParensOptions.AfterPlacementOperator,
FormatStyle::SpaceBeforeParensCustom::APO_Never);

CHECK_PARSE("SpaceBeforeParensOptions:\n"
" AfterPlacementOperator: Always",
SpaceBeforeParensOptions.AfterPlacementOperator,
FormatStyle::SpaceBeforeParensCustom::APO_Always);

CHECK_PARSE("SpaceBeforeParensOptions:\n"
" AfterPlacementOperator: Leave",
SpaceBeforeParensOptions.AfterPlacementOperator,
FormatStyle::SpaceBeforeParensCustom::APO_Leave);

// For backward compatibility:
Style.SpacesInParens = FormatStyle::SIPO_Never;
Style.SpacesInParensOptions = {};
Expand Down
32 changes: 14 additions & 18 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11347,35 +11347,31 @@ TEST_F(FormatTest, UnderstandsNewAndDelete) {

FormatStyle AfterPlacementOperator = getLLVMStyle();
AfterPlacementOperator.SpaceBeforeParens = FormatStyle::SBPO_Custom;
EXPECT_EQ(
AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator,
FormatStyle::SpaceBeforeParensCustom::APO_Leave);
EXPECT_TRUE(
AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator);
verifyFormat("new (buf) int;", AfterPlacementOperator);
verifyFormat("new(buf) int;", AfterPlacementOperator);

AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator =
FormatStyle::SpaceBeforeParensCustom::APO_Never;
verifyFormat("struct A {\n"
" int *a;\n"
" A(int *p) : a(new(p) int) {\n"
" new(p) int;\n"
" int *b = new(p) int;\n"
" int *c = new(p) int(3);\n"
" delete(b);\n"
" A(int *p) : a(new (p) int) {\n"
" new (p) int;\n"
" int *b = new (p) int;\n"
" int *c = new (p) int(3);\n"
" delete (b);\n"
" }\n"
"};",
AfterPlacementOperator);
verifyFormat("void operator new(void *foo) ATTRIB;", AfterPlacementOperator);

AfterPlacementOperator.SpaceBeforeParensOptions.AfterPlacementOperator =
FormatStyle::SpaceBeforeParensCustom::APO_Always;
false;
verifyFormat("new(buf) int;", AfterPlacementOperator);
verifyFormat("struct A {\n"
" int *a;\n"
" A(int *p) : a(new (p) int) {\n"
" new (p) int;\n"
" int *b = new (p) int;\n"
" int *c = new (p) int(3);\n"
" delete (b);\n"
" A(int *p) : a(new(p) int) {\n"
" new(p) int;\n"
" int *b = new(p) int;\n"
" int *c = new(p) int(3);\n"
" delete(b);\n"
" }\n"
"};",
AfterPlacementOperator);
Expand Down

0 comments on commit 46f4a74

Please sign in to comment.