[clang-format] Honour PointerAlignment in statements with initializers.
authorMarek Kurdej <marek.kurdej+llvm.org@gmail.com>
Tue, 15 Feb 2022 08:52:33 +0000 (09:52 +0100)
committerMarek Kurdej <marek.kurdej+llvm.org@gmail.com>
Tue, 15 Feb 2022 17:06:32 +0000 (18:06 +0100)
Fixes https://github.com/llvm/llvm-project/issues/53843.

Reviewed By: HazardyKnusperkeks, owenpan

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

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

index 72f4947..a2985eb 100644 (file)
@@ -3142,7 +3142,8 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
     // initializers.
     if (Line.IsMultiVariableDeclStmt &&
         (Left.NestingLevel == Line.First->NestingLevel ||
-         startsWithInitStatement(Line)))
+         ((Left.NestingLevel == Line.First->NestingLevel + 1) &&
+          startsWithInitStatement(Line))))
       return false;
     return Left.Previous && !Left.Previous->isOneOf(
                                 tok::l_paren, tok::coloncolon, tok::l_square);
index d230964..1b99b53 100644 (file)
@@ -8417,6 +8417,13 @@ TEST_F(FormatTest, DeclarationsOfMultipleVariables) {
   verifyFormat(
       "/*comment*/ switch (int *p, *q; p != q) {\n  default:\n    break;\n}",
       Style);
+
+  verifyFormat("if ([](int* p, int* q) {}()) {\n}", Style);
+  verifyFormat("for ([](int* p, int* q) {}();;) {\n}", Style);
+  verifyFormat("for (; [](int* p, int* q) {}();) {\n}", Style);
+  verifyFormat("for (;; [](int* p, int* q) {}()) {\n}", Style);
+  verifyFormat("switch ([](int* p, int* q) {}()) {\n  default:\n    break;\n}",
+               Style);
 }
 
 TEST_F(FormatTest, ConditionalExpressionsInBrackets) {