From c6321bda9d00e3377613a8937b451131192ba6e8 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Sun, 18 Aug 2019 18:51:39 +0000 Subject: [PATCH] [clang-format] Fix a bug that joins template closer and = Also fixes the documentation for SpaceBeforeAssignmentOperators. See discussions at https://reviews.llvm.org/D66332 Differential Revision: https://reviews.llvm.org/D66384 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@369214 91177308-0d34-0410-b5e6-96231b3b80d8 --- docs/ClangFormatStyleOptions.rst | 4 ++-- include/clang/Format/Format.h | 4 ++-- lib/Format/TokenAnnotator.cpp | 2 +- unittests/Format/FormatTest.cpp | 9 ++++++++- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/docs/ClangFormatStyleOptions.rst b/docs/ClangFormatStyleOptions.rst index 50d8b365e5..5662ad4a44 100644 --- a/docs/ClangFormatStyleOptions.rst +++ b/docs/ClangFormatStyleOptions.rst @@ -2050,8 +2050,8 @@ the configuration (without a prefix: ``Auto``). .. code-block:: c++ true: false: - int a = 5; vs. int a=5; - a += 42 a+=42; + int a = 5; vs. int a= 5; + a += 42; a+= 42; **SpaceBeforeCpp11BracedList** (``bool``) If ``true``, a space will be inserted before a C++11 braced list diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index 7a6688bbdb..d1db5055de 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -1738,8 +1738,8 @@ struct FormatStyle { /// If ``false``, spaces will be removed before assignment operators. /// \code /// true: false: - /// int a = 5; vs. int a=5; - /// a += 42 a+=42; + /// int a = 5; vs. int a= 5; + /// a += 42; a+= 42; /// \endcode bool SpaceBeforeAssignmentOperators; diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 62f7130dc8..c391c489dc 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -2870,7 +2870,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line, Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) || (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod))) return false; - if (!Style.SpaceBeforeAssignmentOperators && + if (!Style.SpaceBeforeAssignmentOperators && Left.isNot(TT_TemplateCloser) && Right.getPrecedence() == prec::Assignment) return false; if (Style.Language == FormatStyle::LK_Java && Right.is(tok::coloncolon) && diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 0aeecc0e12..d7859eb86e 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -6620,8 +6620,15 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) { verifyFormat("A> a;", getChromiumStyle(FormatStyle::LK_Cpp)); - verifyFormat("int i = a<1> >> 1;"); + // template closer followed by a token that starts with > or = verifyFormat("bool b = a<1> > 1;"); + verifyFormat("bool b = a<1> >= 1;"); + verifyFormat("int i = a<1> >> 1;"); + FormatStyle Style = getLLVMStyle(); + Style.SpaceBeforeAssignmentOperators = false; + verifyFormat("bool b= a<1> == 1;", Style); + verifyFormat("a = 1;", Style); + verifyFormat("a >>= 1;", Style); verifyFormat("test >> a >> b;"); verifyFormat("test << a >> b;"); -- 2.40.0