summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorReuben Thomas <reuben.thomas@me.com>2019-04-08 12:54:48 +0000
committerReuben Thomas <reuben.thomas@me.com>2019-04-08 12:54:48 +0000
commit91f60b44958fbcaf6450e82ef40965e92aedda5f (patch)
treef56eb0ee89c5c4ff7d62a2b1fbbab17959ba29bf
parentb743b45ebf1bc0b62cde614e334ef44cfbc9926e (diff)
downloadbcm5719-llvm-91f60b44958fbcaf6450e82ef40965e92aedda5f.tar.gz
bcm5719-llvm-91f60b44958fbcaf6450e82ef40965e92aedda5f.zip
[clang-format] Optionally insert a space after unary ! operator
llvm-svn: 357908
-rw-r--r--clang/docs/ClangFormatStyleOptions.rst7
-rw-r--r--clang/include/clang/Format/Format.h8
-rw-r--r--clang/lib/Format/Format.cpp2
-rw-r--r--clang/lib/Format/TokenAnnotator.cpp3
-rw-r--r--clang/unittests/Format/FormatTest.cpp17
5 files changed, 36 insertions, 1 deletions
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 0614af36344..dc517e44e97 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -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.
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index f5f93dd8b3e..ac12dcb542c 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -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:
@@ -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 &&
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index e2326423a7a..2d8596a42aa 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -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",
@@ -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;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 8f64e3cf985..032be722b28 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -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.
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 3256ea54ab6..1839172047e 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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();
@@ -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);
OpenPOWER on IntegriCloud