]> granicus.if.org Git - clang/commitdiff
Fix comment indentation before labels.
authorDaniel Jasper <djasper@google.com>
Wed, 13 Mar 2013 15:53:12 +0000 (15:53 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 13 Mar 2013 15:53:12 +0000 (15:53 +0000)
Before:
switch (x) {
  // if 1, do f()
case 1:
  f();
}

After:
switch (x) {
// if 1, do f()
case 1:
  f();
}

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

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

index 67ca928a1adc450213bd446699f0626f1c7181eb..63edc6e23ee82eea226108ae4f7f1ae7b9dc7118 100644 (file)
@@ -1135,6 +1135,13 @@ public:
     deriveLocalStyle();
     for (unsigned i = 0, e = AnnotatedLines.size(); i != e; ++i) {
       Annotator.calculateFormattingInformation(AnnotatedLines[i]);
+
+      // Adapt level to the next line if this is a comment.
+      // FIXME: Can/should this be done in the UnwrappedLineParser?
+      if (i + 1 != e && AnnotatedLines[i].First.is(tok::comment) &&
+          AnnotatedLines[i].First.Children.empty() &&
+          AnnotatedLines[i + 1].First.isNot(tok::r_brace))
+        AnnotatedLines[i].Level = AnnotatedLines[i + 1].Level;
     }
     std::vector<int> IndentForLevel;
     bool PreviousLineWasTouched = false;
index 79163a6818391f924724bf18426962e0590a9624..17aeacae5c83879c7140c9ee8c6ee27a20bb70eb 100644 (file)
@@ -427,6 +427,11 @@ TEST_F(FormatTest, FormatsSwitchStatement) {
                "default: {\n"
                "  // Do nothing.\n"
                "}");
+  verifyFormat("switch (x) {\n"
+               "// if 1, do f()\n"
+               "case 1:\n"
+               "  f();\n"
+               "}");
 
   verifyGoogleFormat("switch (x) {\n"
                      "  case 1:\n"