[clang-format] SpaceBeforeParens for lambda expressions
authorAndrew Ng <anng.sw@gmail.com>
Tue, 26 Feb 2019 14:34:49 +0000 (14:34 +0000)
committerAndrew Ng <anng.sw@gmail.com>
Tue, 26 Feb 2019 14:34:49 +0000 (14:34 +0000)
Add support for lambda expressions to the SpaceBeforeParens formatting
option.

Differential Revision: https://reviews.llvm.org/D58241

llvm-svn: 354880

clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp

index c7ddbf5..9f4e180 100644 (file)
@@ -2547,7 +2547,9 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
               (!Left.Previous || Left.Previous->isNot(tok::period))))) ||
            (Style.SpaceBeforeParens == FormatStyle::SBPO_Always &&
             (Left.is(tok::identifier) || Left.isFunctionLikeKeyword() ||
-             Left.is(tok::r_paren)) &&
+             Left.is(tok::r_paren) ||
+             (Left.is(tok::r_square) && Left.MatchingParen &&
+              Left.MatchingParen->is(TT_LambdaLSquare))) &&
             Line.Type != LT_PreprocessorDirective);
   }
   if (Left.is(tok::at) && Right.Tok.getObjCKeywordID() != tok::objc_not_keyword)
index eea1844..064ece1 100644 (file)
@@ -9238,6 +9238,7 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
   verifyFormat("typedef void (*cb)(int);", NoSpace);
   verifyFormat("T A::operator()();", NoSpace);
   verifyFormat("X A::operator++(T);", NoSpace);
+  verifyFormat("auto lambda = []() { return 0; };", NoSpace);
 
   FormatStyle Space = getLLVMStyle();
   Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
@@ -9285,6 +9286,7 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
   verifyFormat("typedef void (*cb) (int);", Space);
   verifyFormat("T A::operator() ();", Space);
   verifyFormat("X A::operator++ (T);", Space);
+  verifyFormat("auto lambda = [] () { return 0; };", Space);
 }
 
 TEST_F(FormatTest, ConfigurableSpacesInParentheses) {