Skip to content

Commit

Permalink
[clang-format] Optionally insert a space after unary ! operator
Browse files Browse the repository at this point in the history
llvm-svn: 357908
  • Loading branch information
reuk committed Apr 8, 2019
1 parent b743b45 commit 91f60b4
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
7 changes: 7 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1952,6 +1952,13 @@ the configuration (without a prefix: ``Auto``).
true: false:
(int) i; vs. (int)i;

**SpaceAfterLogicalNot** (``bool``)
If ``true``, a space is inserted after the logical not operator (``!``).
.. code-block:: c++

true: false:
! someExpression(); vs. !someExpression();

**SpaceAfterTemplateKeyword** (``bool``)
If ``true``, a space will be inserted after the 'template' keyword.

Expand Down
8 changes: 8 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,13 @@ struct FormatStyle {
/// \endcode
bool SpaceAfterCStyleCast;

/// If ``true``, a space is inserted after the logical not operator (``!``).
/// \code
/// true: false:
/// ! someExpression(); vs. !someExpression();
/// \endcode
bool SpaceAfterLogicalNot;

/// If \c true, a space will be inserted after the 'template' keyword.
/// \code
/// true: false:
Expand Down Expand Up @@ -1911,6 +1918,7 @@ struct FormatStyle {
PointerAlignment == R.PointerAlignment &&
RawStringFormats == R.RawStringFormats &&
SpaceAfterCStyleCast == R.SpaceAfterCStyleCast &&
SpaceAfterLogicalNot == R.SpaceAfterLogicalNot &&
SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword &&
SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators &&
SpaceBeforeCpp11BracedList == R.SpaceBeforeCpp11BracedList &&
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("SortIncludes", Style.SortIncludes);
IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
IO.mapOptional("SpaceAfterCStyleCast", Style.SpaceAfterCStyleCast);
IO.mapOptional("SpaceAfterLogicalNot", Style.SpaceAfterLogicalNot);
IO.mapOptional("SpaceAfterTemplateKeyword",
Style.SpaceAfterTemplateKeyword);
IO.mapOptional("SpaceBeforeAssignmentOperators",
Expand Down Expand Up @@ -730,6 +731,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.SpacesInContainerLiterals = true;
LLVMStyle.SpacesInCStyleCastParentheses = false;
LLVMStyle.SpaceAfterCStyleCast = false;
LLVMStyle.SpaceAfterLogicalNot = false;
LLVMStyle.SpaceAfterTemplateKeyword = true;
LLVMStyle.SpaceBeforeCtorInitializerColon = true;
LLVMStyle.SpaceBeforeInheritanceColon = true;
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2832,7 +2832,8 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
return true;
}
if (Left.is(TT_UnaryOperator))
return Right.is(TT_BinaryOperator);
return (Style.SpaceAfterLogicalNot && Left.is(tok::exclaim)) ||
Right.is(TT_BinaryOperator);

// If the next token is a binary operator or a selector name, we have
// incorrectly classified the parenthesis as a cast. FIXME: Detect correctly.
Expand Down
17 changes: 17 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9719,6 +9719,17 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
verifyFormat("auto lambda = []() { return 0; };", SomeSpace);
}

TEST_F(FormatTest, SpaceAfterLogicalNot) {
FormatStyle Spaces = getLLVMStyle();
Spaces.SpaceAfterLogicalNot = true;

verifyFormat("bool x = ! y", Spaces);
verifyFormat("if (! isFailure())", Spaces);
verifyFormat("if (! (a && b))", Spaces);
verifyFormat("\"Error!\"", Spaces);
verifyFormat("! ! x", Spaces);
}

TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
FormatStyle Spaces = getLLVMStyle();

Expand Down Expand Up @@ -11511,6 +11522,12 @@ TEST_F(FormatTest, ParsesConfiguration) {
CHECK_PARSE("SpaceAfterControlStatementKeyword: true", SpaceBeforeParens,
FormatStyle::SBPO_ControlStatements);

Style.SpaceAfterLogicalNot = false;
CHECK_PARSE("SpaceAfterLogicalNot: true", SpaceAfterLogicalNot,
true);
CHECK_PARSE("SpaceAfterLogicalNot: false", SpaceAfterLogicalNot,
false);

Style.ColumnLimit = 123;
FormatStyle BaseStyle = getLLVMStyle();
CHECK_PARSE("BasedOnStyle: LLVM", ColumnLimit, BaseStyle.ColumnLimit);
Expand Down

0 comments on commit 91f60b4

Please sign in to comment.