omarahmed created this revision.
omarahmed added a reviewer: MyDeveloperDay.
Herald added a project: All.
omarahmed requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Add AfterPlacementNew option to SpaceBeforeParensOptions to have more control
on placement new expressions.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D127270
Files:
clang/docs/ClangFormatStyleOptions.rst
clang/include/clang/Format/Format.h
clang/lib/Format/Format.cpp
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ 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");
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ 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))
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ 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;
Index: clang/include/clang/Format/Format.h
===================================================================
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -3490,6 +3490,12 @@
/// <conditional-body> <conditional-body>
/// \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 &&
Index: clang/docs/ClangFormatStyleOptions.rst
===================================================================
--- clang/docs/ClangFormatStyleOptions.rst
+++ clang/docs/ClangFormatStyleOptions.rst
@@ -4248,6 +4248,13 @@
true: false:
IF (...) vs. IF(...)
<conditional-body> <conditional-body>
+
+ * ``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.
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits