From e7b368b885a958d6ccdff81288723ede68008c6d Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Fri, 29 Nov 2013 09:27:43 +0000 Subject: [PATCH] clang-format: Extends formatted ranges to subsequent lines comments. Before: int aaaa; // This line is formatted. // The comment continues .. // .. here. Before: int aaaa; // This line is formatted. // The comment continues .. // .. here. This fixes llvm.org/PR17914. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@195954 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 22 +++++++++++------- unittests/Format/FormatTest.cpp | 40 ++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 13 deletions(-) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index a7d7d5eb10..66dd387ef2 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -1275,7 +1275,7 @@ private: bool computeAffectedLines(SmallVectorImpl::iterator I, SmallVectorImpl::iterator E) { bool SomeLineAffected = false; - bool PreviousLineAffected = false; + const AnnotatedLine *PreviousLine = NULL; while (I != E) { AnnotatedLine *Line = *I; Line->LeadingEmptyLinesAffected = affectsLeadingEmptyLines(*Line->First); @@ -1299,9 +1299,10 @@ private: continue; } - if (nonPPLineAffected(Line, &PreviousLineAffected)) + if (nonPPLineAffected(Line, PreviousLine)) SomeLineAffected = true; + PreviousLine = Line; ++I; } return SomeLineAffected; @@ -1309,7 +1310,8 @@ private: // Determines whether 'Line' is affected by the SourceRanges given as input. // Returns \c true if line or one if its children is affected. - bool nonPPLineAffected(AnnotatedLine *Line, bool *PreviousLineAffected) { + bool nonPPLineAffected(AnnotatedLine *Line, + const AnnotatedLine *PreviousLine) { bool SomeLineAffected = false; Line->ChildrenAffected = computeAffectedLines(Line->Children.begin(), Line->Children.end()); @@ -1340,14 +1342,18 @@ private: // Was this line moved, i.e. has it previously been on the same line as an // affected line? - bool LineMoved = *PreviousLineAffected && Line->First->NewlinesBefore == 0; + bool LineMoved = PreviousLine && PreviousLine->Affected && + Line->First->NewlinesBefore == 0; + + bool IsContinuedComment = Line->First->is(tok::comment) && + Line->First->Next == NULL && + Line->First->NewlinesBefore < 2 && PreviousLine && + PreviousLine->Last->is(tok::comment); - if (SomeTokenAffected || SomeFirstChildAffected || LineMoved) { + if (SomeTokenAffected || SomeFirstChildAffected || LineMoved || + IsContinuedComment) { Line->Affected = true; - *PreviousLineAffected = true; SomeLineAffected = true; - } else { - *PreviousLineAffected = false; } return SomeLineAffected; } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index ccad1d4b00..76796e4f28 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -829,6 +829,36 @@ TEST_F(FormatTest, CanFormatCommentsLocally) { "int b;\n" "int c; // unrelated comment", 31, 0, getLLVMStyle())); + + EXPECT_EQ("int a; // This\n" + " // is\n" + " // a", + format("int a; // This\n" + " // is\n" + " // a", + 0, 0, getLLVMStyle())); + EXPECT_EQ("int a; // This\n" + " // is\n" + " // a\n" + "// This is b\n" + "int b;", + format("int a; // This\n" + " // is\n" + " // a\n" + "// This is b\n" + "int b;", + 0, 0, getLLVMStyle())); + EXPECT_EQ("int a; // This\n" + " // is\n" + " // a\n" + "\n" + " // This is unrelated", + format("int a; // This\n" + " // is\n" + " // a\n" + "\n" + " // This is unrelated", + 0, 0, getLLVMStyle())); } TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { @@ -843,11 +873,11 @@ TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { TEST_F(FormatTest, UnderstandsBlockComments) { verifyFormat("f(/*noSpaceAfterParameterNamingComment=*/true);"); verifyFormat("void f() { g(/*aaa=*/x, /*bbb=*/!y); }"); - EXPECT_EQ( - "f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" - " bbbbbbbbbbbbbbbbbbbbbbbbb);", - format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n/* Trailing comment for aa... */\n" - " bbbbbbbbbbbbbbbbbbbbbbbbb);")); + EXPECT_EQ("f(aaaaaaaaaaaaaaaaaaaaaaaaa, /* Trailing comment for aa... */\n" + " bbbbbbbbbbbbbbbbbbbbbbbbb);", + format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \\\n" + "/* Trailing comment for aa... */\n" + " bbbbbbbbbbbbbbbbbbbbbbbbb);")); EXPECT_EQ( "f(aaaaaaaaaaaaaaaaaaaaaaaaa,\n" " /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);", -- 2.40.0