Skip to content

Commit 91f60b4

Browse files
committed
[clang-format] Optionally insert a space after unary ! operator
llvm-svn: 357908
1 parent b743b45 commit 91f60b4

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,6 +1952,13 @@ the configuration (without a prefix: ``Auto``).
19521952
true: false:
19531953
(int) i; vs. (int)i;
19541954

1955+
**SpaceAfterLogicalNot** (``bool``)
1956+
If ``true``, a space is inserted after the logical not operator (``!``).
1957+
.. code-block:: c++
1958+
1959+
true: false:
1960+
! someExpression(); vs. !someExpression();
1961+
19551962
**SpaceAfterTemplateKeyword** (``bool``)
19561963
If ``true``, a space will be inserted after the 'template' keyword.
19571964

clang/include/clang/Format/Format.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,13 @@ struct FormatStyle {
16281628
/// \endcode
16291629
bool SpaceAfterCStyleCast;
16301630

1631+
/// If ``true``, a space is inserted after the logical not operator (``!``).
1632+
/// \code
1633+
/// true: false:
1634+
/// ! someExpression(); vs. !someExpression();
1635+
/// \endcode
1636+
bool SpaceAfterLogicalNot;
1637+
16311638
/// If \c true, a space will be inserted after the 'template' keyword.
16321639
/// \code
16331640
/// true: false:
@@ -1911,6 +1918,7 @@ struct FormatStyle {
19111918
PointerAlignment == R.PointerAlignment &&
19121919
RawStringFormats == R.RawStringFormats &&
19131920
SpaceAfterCStyleCast == R.SpaceAfterCStyleCast &&
1921+
SpaceAfterLogicalNot == R.SpaceAfterLogicalNot &&
19141922
SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword &&
19151923
SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators &&
19161924
SpaceBeforeCpp11BracedList == R.SpaceBeforeCpp11BracedList &&

clang/lib/Format/Format.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ template <> struct MappingTraits<FormatStyle> {
478478
IO.mapOptional("SortIncludes", Style.SortIncludes);
479479
IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
480480
IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
481+
IO.mapOptional("SpaceAfterLogicalNot", Style.SpaceAfterLogicalNot);
481482
IO.mapOptional("SpaceAfterTemplateKeyword",
482483
Style.SpaceAfterTemplateKeyword);
483484
IO.mapOptional("SpaceBeforeAssignmentOperators",
@@ -730,6 +731,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
730731
LLVMStyle.SpacesInContainerLiterals = true;
731732
LLVMStyle.SpacesInCStyleCastParentheses = false;
732733
LLVMStyle.SpaceAfterCStyleCast = false;
734+
LLVMStyle.SpaceAfterLogicalNot = false;
733735
LLVMStyle.SpaceAfterTemplateKeyword = true;
734736
LLVMStyle.SpaceBeforeCtorInitializerColon = true;
735737
LLVMStyle.SpaceBeforeInheritanceColon = true;

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2832,7 +2832,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
28322832
return true;
28332833
}
28342834
if (Left.is(TT_UnaryOperator))
2835-
return Right.is(TT_BinaryOperator);
2835+
return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) ||
2836+
Right.is(TT_BinaryOperator);
28362837

28372838
// If the next token is a binary operator or a selector name, we have
28382839
// incorrectly classified the parenthesis as a cast. FIXME: Detect correctly.

clang/unittests/Format/FormatTest.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9719,6 +9719,17 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
97199719
verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
97209720
}
97219721

9722+
TEST_F(FormatTest, SpaceAfterLogicalNot) {
9723+
FormatStyle Spaces = getLLVMStyle();
9724+
Spaces.SpaceAfterLogicalNot = true;
9725+
9726+
verifyFormat("bool x = ! y", Spaces);
9727+
verifyFormat("if (! isFailure())", Spaces);
9728+
verifyFormat("if (! (a && b))", Spaces);
9729+
verifyFormat("\"Error!\"", Spaces);
9730+
verifyFormat("! ! x", Spaces);
9731+
}
9732+
97229733
TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
97239734
FormatStyle Spaces = getLLVMStyle();
97249735

@@ -11511,6 +11522,12 @@ TEST_F(FormatTest, ParsesConfiguration) {
1151111522
CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
1151211523
FormatStyle::SBPO_ControlStatements);
1151311524

11525+
Style.SpaceAfterLogicalNot = false;
11526+
CHECK_PARSE("SpaceAfterLogicalNot: true", SpaceAfterLogicalNot,
11527+
true);
11528+
CHECK_PARSE("SpaceAfterLogicalNot: false", SpaceAfterLogicalNot,
11529+
false);
11530+
1151411531
Style.ColumnLimit = 123;
1151511532
FormatStyle BaseStyle = getLLVMStyle();
1151611533
CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);

0 commit comments

Comments
 (0)