diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -4248,6 +4248,13 @@ true: false: IF (...) vs. IF(...) + + * ``bool AfterPlacementNew`` If ``true``, put space between placement new and opening parentheses. + + .. code-block:: c++ + + true: false: + new (buf) T; vs. new(buf) T; * ``bool AfterOverloadedOperator`` If ``true``, put a space between operator overloading and opening parentheses. diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -3490,6 +3490,12 @@ /// /// \endcode bool AfterIfMacros; + /// If ``true``, put space between placement new and opening parentheses. + /// \code + /// true: false: + /// new (buf) T; vs. new(buf) T; + /// \endcode + bool AfterPlacementNew; /// If ``true``, put a space between operator overloading and opening /// parentheses. /// \code @@ -3530,8 +3536,9 @@ : AfterControlStatements(false), AfterForeachMacros(false), AfterFunctionDeclarationName(false), AfterFunctionDefinitionName(false), AfterIfMacros(false), - AfterOverloadedOperator(false), AfterRequiresInClause(false), - AfterRequiresInExpression(false), BeforeNonEmptyParentheses(false) {} + AfterPlacementNew(true), AfterOverloadedOperator(false), + AfterRequiresInClause(false), AfterRequiresInExpression(false), + BeforeNonEmptyParentheses(false) {} bool operator==(const SpaceBeforeParensCustom &Other) const { return AfterControlStatements == Other.AfterControlStatements && @@ -3540,6 +3547,7 @@ Other.AfterFunctionDeclarationName && AfterFunctionDefinitionName == Other.AfterFunctionDefinitionName && AfterIfMacros == Other.AfterIfMacros && + AfterPlacementNew == Other.AfterPlacementNew && AfterOverloadedOperator == Other.AfterOverloadedOperator && AfterRequiresInClause == Other.AfterRequiresInClause && AfterRequiresInExpression == Other.AfterRequiresInExpression && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -935,6 +935,7 @@ IO.mapOptional("AfterFunctionDeclarationName", Spacing.AfterFunctionDeclarationName); IO.mapOptional("AfterIfMacros", Spacing.AfterIfMacros); + IO.mapOptional("AfterPlacementNew", Spacing.AfterPlacementNew); IO.mapOptional("AfterOverloadedOperator", Spacing.AfterOverloadedOperator); IO.mapOptional("AfterRequiresInClause", Spacing.AfterRequiresInClause); IO.mapOptional("AfterRequiresInExpression", @@ -1310,6 +1311,7 @@ LLVMStyle.SpaceBeforeParensOptions.AfterControlStatements = true; LLVMStyle.SpaceBeforeParensOptions.AfterForeachMacros = true; LLVMStyle.SpaceBeforeParensOptions.AfterIfMacros = true; + LLVMStyle.SpaceBeforeParensOptions.AfterPlacementNew = true; LLVMStyle.SpaceBeforeRangeBasedForLoopColon = true; LLVMStyle.SpaceBeforeAssignmentOperators = true; LLVMStyle.SpaceBeforeCpp11BracedList = false; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3393,6 +3393,10 @@ if (Left.is(TT_IfMacro)) return Style.SpaceBeforeParensOptions.AfterIfMacros || spaceRequiredBeforeParens(Right); + if (Left.is(tok::kw_new) && Right.isNot(TT_OverloadedOperatorLParen) && + !Line.MightBeFunctionDecl) + return Style.SpaceBeforeParensOptions.AfterPlacementNew || + spaceRequiredBeforeParens(Right); if (Line.Type == LT_ObjCDecl) return true; if (Left.is(tok::semi)) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -15273,6 +15273,18 @@ verifyFormat("M(std::size_t R, std::size_t C) : C(C), data(R) {}", SpaceFuncDef); + FormatStyle SpacePlacementNew = getLLVMStyle(); + SpacePlacementNew.SpaceBeforeParens = FormatStyle::SBPO_Custom; + SpacePlacementNew.SpaceBeforeParensOptions.AfterPlacementNew = true; + verifyFormat("new (buf) T;", SpacePlacementNew); + verifyFormat("T *p = new (buf) T;", SpacePlacementNew); + verifyFormat("T *p = new (buf) T(3);", SpacePlacementNew); + verifyFormat("T *new() {}", SpacePlacementNew); + verifyFormat("T *new();", SpacePlacementNew); + verifyFormat("T *operator new(size_t size) {}", SpacePlacementNew); + verifyFormat("new T;", SpacePlacementNew); + verifyFormat("T *p = new T;", SpacePlacementNew); + FormatStyle SpaceIfMacros = getLLVMStyle(); SpaceIfMacros.IfMacros.clear(); SpaceIfMacros.IfMacros.push_back("MYIF");