<div dir="ltr">I've tried many source code formatters and none worked for me, until I recently<br>found `clang-format'. Its output comes close to my personal style. Of course,<br>there are some differences, and it seems these cannot be configured using the<br>
currently available options. I'd like to format like the following example<br>(which is not meaningful, just an example):<br><br>#include <iostream><br>#include <vector><br><br>template< typename T ><br>
class Test<br>{<br>        T a_;<br>        T b_;<br>        T c_;<br><br>        public:<br>        Test(const T& arg)<br>        : a_(arg),<br>          b_(arg),<br>          c_(arg)<br>        {<br>        }<br>};<br>
<br>int main()<br>{<br>        Test< int > a(5);<br>        Test< std::vector< int > > b(std::vector< int >());<br>}<br><br>My current `.clang-format' looks like this:<br><br>BasedOnStyle: WebKit<br>
<br>AccessModifierOffset: 0<br>AllowShortIfStatementsOnASingleLine: true<br>AlwaysBreakTemplateDeclarations: true<br>BinPackParameters: false<br>BreakBeforeBraces: Linux<br>BreakConstructorInitializersBeforeComma: false<br>
ColumnLimit: 79<br>ConstructorInitializerAllOnOneLineOrOnePerLine: false<br>ConstructorInitializerIndentWidth: 0<br>Cpp11BracedListStyle: true<br>IndentCaseLabels: true<br>IndentWidth: 8<br>MaxEmptyLinesToKeep: 1<br>NamespaceIndentation: None<br>
PointerBindsToType: true<br>SpaceAfterControlStatementKeyword: true<br>SpaceBeforeAssignmentOperators: true<br>SpaceInEmptyParentheses: false<br>SpacesBeforeTrailingComments: 1<br>SpacesInCStyleCastParentheses: false<br>SpacesInParentheses: false<br>
Standard: Cpp03<br>TabWidth: 8<br>UseTab: Always<br><br>The above output was achieved by using the following diff against revision<br>192886 of `TokenAnnotator.cpp':<br><br>Index: lib/Format/TokenAnnotator.cpp<br>===================================================================<br>
--- lib/Format/TokenAnnotator.cpp       (revision 192886)<br>+++ lib/Format/TokenAnnotator.cpp       (working copy)<br>@@ -1320,6 +1320,12 @@<br>   if (Tok.Type == TT_TrailingReturnArrow ||<br>       Tok.Previous->Type == TT_TrailingReturnArrow)<br>
     return true;<br>+  if (Tok.Type == TT_TemplateOpener)<br>+    return false;<br>+  if (Tok.Type == TT_TemplateCloser)<br>+    return true;<br>+  if (Tok.Previous->Type == TT_TemplateOpener)<br>+    return true;<br>
   if (Tok.Previous->is(tok::comma))<br>     return true;<br>   if (Tok.is(tok::comma))<br>@@ -1381,10 +1387,18 @@<br>              Style.AlwaysBreakTemplateDeclarations) {<br>     // FIXME: Fix horrible hack of using BindingStrength to find top-level <>.<br>
     return true;<br>+  } else if (Right.Type == TT_CtorInitializerColon) {<br>+    return true;<br>+  } else if (Right.Type == TT_CtorInitializerComma) {<br>+    return false;<br>+  } else if (Right.Previous->Type == TT_CtorInitializerComma) {<br>
+    return true;<br>+#if 0<br>   } else if (Right.Type == TT_CtorInitializerComma &&<br>              Style.BreakConstructorInitializersBeforeComma &&<br>              !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) {<br>
     return true;<br>+#endif<br>   } else if (Right.Previous->BlockKind == BK_Block &&<br>              Right.Previous->isNot(tok::r_brace) && Right.isNot(tok::r_brace)) {<br>     return true;<br><br>
Now my questions:<br><br>- Are there FormatStyle options planned that make my formatting<br>  style possible using `clang-format'? I particularly like the<br>  spacing of templates and the initializer list as displayed.<br>
- Am I on the right track? I scanned the source but that was the best location<br>  I could find to add my patch. Of course, as it is now it breaks other options<br>  so there would have to be a new FormatStyle member that allows configuration.<br>
- How is development done for `clang-format'? Is there a chance to add more<br>  options without pissing off someone? How are such features coordinated?<br><br>Cheers,<br>Thomas.<br></div>