]> granicus.if.org Git - clang/commitdiff
Do not consider comments when adjusting to local indent style.
authorDaniel Jasper <djasper@google.com>
Wed, 20 Mar 2013 14:31:47 +0000 (14:31 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 20 Mar 2013 14:31:47 +0000 (14:31 +0000)
Before (when only reformatting "int b"):
int a; // comment
       // comment
       int b;

After:
int a; // comment
       // comment
int b;

This also fixes llvm.org/PR15433.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@177524 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/Format.cpp
unittests/Format/FormatTest.cpp

index 1a7dbe0936511686704e6d5f6313e51c5edd7382..b1005b5de6c2c7c6e975a6ff8a112d4f6de488e7 100644 (file)
@@ -1324,7 +1324,8 @@ public:
           unsigned LevelIndent = Indent;
           if (static_cast<int>(LevelIndent) - Offset >= 0)
             LevelIndent -= Offset;
-          IndentForLevel[TheLine.Level] = LevelIndent;
+          if (TheLine.First.isNot(tok::comment))
+            IndentForLevel[TheLine.Level] = LevelIndent;
 
           // Remove trailing whitespace of the previous line if it was touched.
           if (PreviousLineWasTouched || touchesEmptyLineBefore(TheLine))
index 7f9cecb031ee91b94d66f15448b09c1a6f356bda..40f09539e7ae28b9fd20ff04717f3446aa3098d0 100644 (file)
@@ -3279,6 +3279,13 @@ TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
             format("void f() {}\n"
                    "void g() {}",
                    13, 0, getLLVMStyle()));
+  EXPECT_EQ("int a; // comment\n"
+            "       // line 2\n"
+            "int b;",
+            format("int a; // comment\n"
+                   "       // line 2\n"
+                   "  int b;",
+                   35, 0, getLLVMStyle()));
 }
 
 TEST_F(FormatTest, BreakStringLiterals) {