[cfe-dev] New format option

Thomas Tröger tstroege at googlemail.com
Thu Oct 17 09:01:37 PDT 2013

I've tried many source code formatters and none worked for me, until I
found `clang-format'. Its output comes close to my personal style. Of
there are some differences, and it seems these cannot be configured using
currently available options. I'd like to format like the following example
(which is not meaningful, just an example):

#include <iostream>
#include <vector>

template< typename T >
class Test
        T a_;
        T b_;
        T c_;

        Test(const T& arg)
        : a_(arg),

int main()
        Test< int > a(5);
        Test< std::vector< int > > b(std::vector< int >());

My current `.clang-format' looks like this:

BasedOnStyle: WebKit

AccessModifierOffset: 0
AllowShortIfStatementsOnASingleLine: true
AlwaysBreakTemplateDeclarations: true
BinPackParameters: false
BreakBeforeBraces: Linux
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 79
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 0
Cpp11BracedListStyle: true
IndentCaseLabels: true
IndentWidth: 8
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PointerBindsToType: true
SpaceAfterControlStatementKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
Standard: Cpp03
TabWidth: 8
UseTab: Always

The above output was achieved by using the following diff against revision
192886 of `TokenAnnotator.cpp':

Index: lib/Format/TokenAnnotator.cpp
--- lib/Format/TokenAnnotator.cpp       (revision 192886)
+++ lib/Format/TokenAnnotator.cpp       (working copy)
@@ -1320,6 +1320,12 @@
   if (Tok.Type == TT_TrailingReturnArrow ||
       Tok.Previous->Type == TT_TrailingReturnArrow)
     return true;
+  if (Tok.Type == TT_TemplateOpener)
+    return false;
+  if (Tok.Type == TT_TemplateCloser)
+    return true;
+  if (Tok.Previous->Type == TT_TemplateOpener)
+    return true;
   if (Tok.Previous->is(tok::comma))
     return true;
   if (Tok.is(tok::comma))
@@ -1381,10 +1387,18 @@
              Style.AlwaysBreakTemplateDeclarations) {
     // FIXME: Fix horrible hack of using BindingStrength to find top-level
     return true;
+  } else if (Right.Type == TT_CtorInitializerColon) {
+    return true;
+  } else if (Right.Type == TT_CtorInitializerComma) {
+    return false;
+  } else if (Right.Previous->Type == TT_CtorInitializerComma) {
+    return true;
+#if 0
   } else if (Right.Type == TT_CtorInitializerComma &&
              Style.BreakConstructorInitializersBeforeComma &&
              !Style.ConstructorInitializerAllOnOneLineOrOnePerLine) {
     return true;
   } else if (Right.Previous->BlockKind == BK_Block &&
              Right.Previous->isNot(tok::r_brace) &&
Right.isNot(tok::r_brace)) {
     return true;

Now my questions:

- Are there FormatStyle options planned that make my formatting
  style possible using `clang-format'? I particularly like the
  spacing of templates and the initializer list as displayed.
- Am I on the right track? I scanned the source but that was the best
  I could find to add my patch. Of course, as it is now it breaks other
  so there would have to be a new FormatStyle member that allows
- How is development done for `clang-format'? Is there a chance to add more
  options without pissing off someone? How are such features coordinated?

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20131017/35759dba/attachment.html>

More information about the cfe-dev mailing list