]> granicus.if.org Git - clang/commitdiff
clang-format: Fix formatting bug with comment in weird place.
authorDaniel Jasper <djasper@google.com>
Fri, 27 Sep 2013 07:49:08 +0000 (07:49 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 27 Sep 2013 07:49:08 +0000 (07:49 +0000)
Before:
  template <typename T>
  // T should be one of {A, B}.
      void f() {}

After:
  template <typename T>
  // T should be one of {A, B}.
  void f() {}

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

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

index 87c022294e470dc76b8b14ad99dc043f3fdf36fc..8b0baead67af06787d69a917bcf08e0df4158b1c 100644 (file)
@@ -192,6 +192,8 @@ unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline,
                                                unsigned ExtraSpaces) {
   const FormatToken &Current = *State.NextToken;
   const FormatToken &Previous = *State.NextToken->Previous;
+  const FormatToken *PreviousNonComment =
+      State.NextToken->getPreviousNonComment();
 
   // Extra penalty that needs to be added because of the way certain line
   // breaks are chosen.
@@ -253,7 +255,8 @@ unsigned ContinuationIndenter::addTokenToState(LineState &State, bool Newline,
       State.Column = State.Stack.back().QuestionColumn;
     } else if (Previous.is(tok::comma) && State.Stack.back().VariablePos != 0) {
       State.Column = State.Stack.back().VariablePos;
-    } else if (Previous.ClosesTemplateDeclaration ||
+    } else if ((PreviousNonComment &&
+                PreviousNonComment->ClosesTemplateDeclaration) ||
                ((Current.Type == TT_StartOfName ||
                  Current.is(tok::kw_operator)) &&
                 State.ParenLevel == 0 &&
index 0175413a50c4366f33885a356b832eac0d538946..be2a45d1996b92a4b41a493ee216c14eb0d5db72 100644 (file)
@@ -3500,6 +3500,9 @@ TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) {
 TEST_F(FormatTest, WrapsTemplateDeclarations) {
   verifyFormat("template <typename T>\n"
                "virtual void loooooooooooongFunction(int Param1, int Param2);");
+  verifyFormat("template <typename T>\n"
+               "// T should be one of {A, B}.\n"
+               "virtual void loooooooooooongFunction(int Param1, int Param2);");
   verifyFormat(
       "template <typename T>\n"
       "using comment_to_xml_conversion = comment_to_xml_conversion<T, int>;");