]> granicus.if.org Git - clang/commitdiff
clang-format: Fix AlwaysBreakAfterDefinitionReturnType in Stroutrup style
authorDaniel Jasper <djasper@google.com>
Thu, 14 Aug 2014 11:36:03 +0000 (11:36 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 14 Aug 2014 11:36:03 +0000 (11:36 +0000)
Before:
  template <class T>
  T *f(T &c)  // Problem here: no line break before f
  {
    return NULL;
  }

After:
  template <class T>
  T *
  f(T &c)
  {
    return NULL;
  }

Patch by Marek Kurdej, thank you!

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

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

index 3289d19d34eb1b8ad615fcea173c7d6cc32e1c4b..47bfb986c29194479ee0b71fa800a5040cc022f6 100644 (file)
@@ -1305,7 +1305,9 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) {
 
     if (Style.AlwaysBreakAfterDefinitionReturnType &&
         InFunctionDecl && Current->Type == TT_FunctionDeclarationName &&
-        Line.Last->is(tok::l_brace))  // Only for definitions.
+        !Line.Last->isOneOf(tok::semi, tok::comment))  // Only for definitions.
+      // FIXME: Line.Last points to other characters than tok::semi
+      // and tok::lbrace.
       Current->MustBreakBefore = true;
 
     Current->CanBreakBefore =
index c59201f6119b99fad19c6ebbb15a4cfd6c21348f..926e40c926c0a30c9f0cd64c942e64113b384908 100644 (file)
@@ -4156,6 +4156,29 @@ TEST_F(FormatTest, AlwaysBreakAfterDefinitionReturnType) {
                "}\n"
                "const char *bar(void);\n",  // No break here.
                AfterType);
+  verifyFormat("template <class T>\n"
+               "T *\n"
+               "f(T &c) {\n"  // Break here.
+               "  return NULL;\n"
+               "}\n"
+               "template <class T> T *f(T &c);\n",  // No break here.
+               AfterType);
+  AfterType.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
+  verifyFormat("const char *\n"
+               "f(void)\n"  // Break here.
+               "{\n"
+               "  return \"\";\n"
+               "}\n"
+               "const char *bar(void);\n",  // No break here.
+               AfterType);
+  verifyFormat("template <class T>\n"
+               "T *\n"  // Problem here: no line break
+               "f(T &c)\n"  // Break here.
+               "{\n"
+               "  return NULL;\n"
+               "}\n"
+               "template <class T> T *f(T &c);\n",  // No break here.
+               AfterType);
 }
 
 TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {