[PATCH] Insert extra new line before access specifiers.

Alexander Kornienko alexfh at google.com
Wed Mar 27 06:55:58 PDT 2013


Hi djasper,

Insert extra new line before access specifiers.

http://llvm-reviews.chandlerc.com/D581

Files:
  lib/Format/Format.cpp
  unittests/Format/FormatTest.cpp

Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -442,8 +442,7 @@
     while (I != E) {
       if (!I->Untouchable) {
         unsigned Spaces = I->Spaces + Column - I->MinColumn;
-        storeReplacement(
-            I->Tok, std::string(I->NewLines, '\n') + std::string(Spaces, ' '));
+        storeReplacement(I->Tok, getNewLineText(I->NewLines, Spaces));
       }
       ++I;
     }
@@ -1376,7 +1375,17 @@
           Indent = LevelIndent =
               SourceMgr.getSpellingColumnNumber(FirstTok.Tok.getLocation()) - 1;
         } else {
-          formatFirstToken(TheLine.First, Indent, TheLine.InPPDirective,
+          // Insert extra new line before access specifiers.
+          AnnotatedToken Tok = TheLine.First;
+          if (I != AnnotatedLines.begin() &&
+              (I - 1)->Last &&
+              (I - 1)->Last->isOneOf(tok::semi, tok::r_brace) &&
+              Tok.isOneOf(tok::kw_public, tok::kw_private, tok::kw_protected) &&
+              Tok.Children.size() > 0 && Tok.Children[0].is(tok::colon) &&
+              Tok.FormatTok.NewlinesBefore == 1)
+            ++Tok.FormatTok.NewlinesBefore;
+
+          formatFirstToken(Tok, Indent, TheLine.InPPDirective,
                            PreviousEndOfLineColumn);
         }
         tryFitMultipleLinesInOne(Indent, I, E);
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -937,6 +937,25 @@
                      "};");
 }
 
+TEST_F(FormatTest, InsertsNewLineBeforeAccessSpecifiers) {
+  EXPECT_EQ("class A {\n"
+            "public:\n"
+            "  void f();\n"
+            "\n"
+            "private:\n"
+            "  void g() {}\n"
+            "\n"
+            "protected:\n"
+            "};",
+            format("class A {\n"
+                   "public:\n"
+                   "void f();\n"
+                   "private:\n"
+                   "void g() {}\n"
+                   "protected:\n"
+                   "};"));
+}
+
 TEST_F(FormatTest, FormatsDerivedClass) {
   verifyFormat("class A : public B {\n};");
   verifyFormat("class A : public ::B {\n};");
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D581.1.patch
Type: text/x-patch
Size: 2295 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130327/f74499f3/attachment.bin>


More information about the cfe-commits mailing list