Skip to contentSkip to conversation

.clang-format: introduce the use of clang-format

PATCH Superseded Ramkumar Ramachandra 3 participants 7 messages Jun 15, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 .clang-format: introduce the use of clang-format

Instead of manually eyeballing style in reviews, just ask all contributors to run their patches through [git-]clang-format.

---
The idea is to introduce the community to this new toy I found called clang-format. Whether or not it's actually going to be used doesn't bother me too much.

I'm not 100% sure of the style, but I'll leave you to tweak that using http://clang.llvm.org/docs/ClangFormatStyleOptions.html

The current code isn't terribly conformant, but I suppose that'll change with time.

 .clang-format | 7 +++++++
 1 file changed, 7 insertions(+)
 create mode 100644 .clang-format
.clang-format new file +7 -0
new file mode 100644
index 0000000..63a53e0
@@ -0,0 +1,7 @@
+BasedOnStyle: LLVM
+IndentWidth: 8
+UseTab: Always
+BreakBeforeBraces: Linux
+AllowShortBlocksOnASingleLine: false
+AllowShortIfStatementsOnASingleLine: false
+IndentCaseLabels: false
\ No newline at end of file
Signature · 1 line
2.2.1
#1 lore raw

Am 17.01.2015 um 22:30 schrieb Ramkumar Ramachandra:

Quoted 25 lines

Instead of manually eyeballing style in reviews, just ask all contributors to run their patches through [git-]clang-format.

---
The idea is to introduce the community to this new toy I found called clang-format. Whether or not it's actually going to be used doesn't bother me too much.

I'm not 100% sure of the style, but I'll leave you to tweak that using http://clang.llvm.org/docs/ClangFormatStyleOptions.html

The current code isn't terribly conformant, but I suppose that'll change with time.

  .clang-format | 7 +++++++
  1 file changed, 7 insertions(+)

create mode 100644 .clang-format

.clang-format new file +0 -0
new file mode 100644
index 0000000..63a53e0

.clang-format line 7

Quoted 8 lines
.clang-format new file +7 -0
@@ -0,0 +1,7 @@
+BasedOnStyle: LLVM
+IndentWidth: 8
+UseTab: Always
+BreakBeforeBraces: Linux
+AllowShortBlocksOnASingleLine: false
+AllowShortIfStatementsOnASingleLine: false
+IndentCaseLabels: false
\ No newline at end of file

Why no newline on the last line?

These one would be needed as well to match our style, I think:

AllowShortFunctionsOnASingleLine: None
ContinuationIndentWidth: 8

And probably this one:

Cpp11BracedListStyle: false

However, even then struct declarations that are combined with variable declaration and initialization get mangled:

	struct a {
		int n;
		const char *s;
	} arr[] = {
		{ 1, "one" },
		{ 2, "two" }
	};

becomes:

	struct a
	{
		int n;
		const char *s;
	} arr[] = { { 1, "one" }, { 2, "two" } };

It gets formatted better if arr is declared separately.

And this one helps get rid of the added line break between struct a and the following brace:

BreakBeforeBraces: Stroustrup

#2 lore raw
Quoted 2 lines · René Scharfe

However, even then struct declarations that are combined with variable declaration and initialization get mangled:

I'm pretty sure this is a bug in clang-format. It might be semi-trivial to fix too.

Thanks for your inputs.

#3 lore raw

PATCH v2 1/1 .clang-format: introduce the use of clang-format

In reply to Ramkumar Ramachandra

Instead of manually eyeballing style in reviews, just ask all contributors to run their patches through [git-]clang-format.

However, struct declarations that are combined with variable declaration and initialization get mangled:

        struct a {
                int n;
                const char *s;
        } arr[] = {
                { 1, "one" },
                { 2, "two" }
        };

becomes:

        struct a {
                int n;
                const char *s;
        } arr[] = { { 1, "one" }, { 2, "two" } };

It gets formatted better if arr is declared separately.

---
 .clang-format | 11 +++++++++++
 1 file changed, 11 insertions(+)
 create mode 100644 .clang-format
.clang-format new file +11 -0
new file mode 100644
index 0000000..a336438
@@ -0,0 +1,11 @@
+BasedOnStyle: LLVM
+IndentWidth: 8
+UseTab: Always
+BreakBeforeBraces: Linux
+AllowShortBlocksOnASingleLine: false
+AllowShortIfStatementsOnASingleLine: false
+IndentCaseLabels: false
+AllowShortFunctionsOnASingleLine: None
+ContinuationIndentWidth: 8
+Cpp11BracedListStyle: false
+BreakBeforeBraces: Stroustrup
Signature · 1 line
2.2.1
#4 lore raw
Quoted 2 lines · Ramkumar Ramachandra

Instead of manually eyeballing style in reviews, just ask all contributors to run their patches through [git-]clang-format.

Thanks for mentioning this; I hadn't seen the tool before.

I didn't see it mentioned here, but for those who are also new to the tool, it has modes both for checking the content itself as well as diffs (so you are not stuck wading through its reformats of code you didn't touch).

.clang-format line 11

Quoted 3 lines
.clang-format new file +2 -0
+BreakBeforeBraces: Linux
[...]
+BreakBeforeBraces: Stroustrup

These seem conflicting. It looks like you added "Stroustrup" to keep the brace on the line with the "struct" keyword. But this does the wrong thing for "cuddled else"s like:

  if (...) {
     ...
  } else {
     ...
  }

I don't think clang-format has a mode that expresses our style.

I ran some of my recent patches through clang-format-diff, and it generated quite a bit of output. Here are a few notes on what I saw. Feel free to ignore. They are not your problem, but others evaluating the tool might find it useful (and a few of them might suggest some settings for .clang-format).

- It really wants to break function declarations that go over the column limit, even though we often do not do so. I think we're pretty inconsistent here, and I'd be fine going either way with it.

- It really wanted to left-align some of my asterisks, like:

     struct foo_list {
       ...
     } * foo, **foo_tail;

The odd thing is that it gets the second one right, but not the first one (which should be "*foo" with no space). Setting:

DerivePointerAlignment: false
PointerAlignment: Right

cleared it up, but I'm curious why the auto-deriver didn't work.

- It really doesn't like list-alignment, like:

      #define FOO    1
      #define LONGER 2

and would prefer only a single space between "FOO" and "1". I think I'm OK with that, but we have a lot of aligned bits in the existing code.

- It really wants to put function __attribute__ macros on the same line as the function. We often have it on a line above (especially it can be so long). I couldn't find a way to specify this.

- I had a long ternary operator broken across three lines, like:

     foo = bar ?
           some_long_thing(...) :
	   some_other_long_thing(...);

It put it all on one long line, which was much less readable. I set BreakBeforeTernaryOperators to "true", but it did nothing. I set it to "false", and then it broke. Which seems like a bug. It also insisted on indenting it like:

foo = bar ?
some_long_thing(...) :
some_other_long_thing(...);

which I found less readable.

So overall I think it has some promise, but I do not think it is quite flexible enough yet for us to use day-to-day. I'm slightly dubious that any automated formatter can ever be _perfect_ (sometimes human-subjective readability trumps a hard-and-fast rule), but this seems like it might have some promise. And over other indenters I have seen:

1. It's built on clang, so we know the parsing is solid.

2. It can operate on patches (and generates patches for you to apply! You could add a git-add--interactive mode to selectively take its suggestions).

Again, thanks for sharing.

-Peff

#5 lore raw
Quoted 4 lines · Jeff King

.clang-format line 11

Quoted 3 lines · Ramkumar Ramachandra
.clang-format new file +2 -0
+BreakBeforeBraces: Linux
[...]
+BreakBeforeBraces: Stroustrup

Oh, oops.

Quoted 10 lines

- It really wants to break function declarations that go over the column limit, even though we often do not do so. I think we're pretty inconsistent here, and I'd be fine going either way with it.

- It really wanted to left-align some of my asterisks, like:

     struct foo_list {
       ...
     } * foo, **foo_tail;
Quoted 7 lines

The odd thing is that it gets the second one right, but not the first one (which should be "*foo" with no space). Setting:

DerivePointerAlignment: false
PointerAlignment: Right

cleared it up, but I'm curious why the auto-deriver didn't work.

Sounds like a bug.

Quoted 5 lines

- It really doesn't like list-alignment, like:

      #define FOO    1
      #define LONGER 2
Quoted 7 lines

and would prefer only a single space between "FOO" and "1". I think I'm OK with that, but we have a lot of aligned bits in the existing code.

- It really wants to put function __attribute__ macros on the same line as the function. We often have it on a line above (especially it can be so long). I couldn't find a way to specify this.

You have to compromise a bit if you want to use an auto-formatting tool, without losing your head patching every little detail :)

Quoted 11 lines

- I had a long ternary operator broken across three lines, like:

     foo = bar ?
           some_long_thing(...) :
           some_other_long_thing(...);

It put it all on one long line, which was much less readable. I set BreakBeforeTernaryOperators to "true", but it did nothing. I set it to "false", and then it broke. Which seems like a bug. It also insisted on indenting it like:

Quoted 5 lines

foo = bar ?
some_long_thing(...) :
some_other_long_thing(...);

which I found less readable.

To be honest, the LLVM community doesn't fix bugs just because they can be fixed: it's quite heavily driven by commercial interest. And I really don't find long ternary operators in a modern C++ codebase.

Quoted 4 lines

I'm slightly dubious that
any automated formatter can ever be _perfect_ (sometimes human-subjective readability trumps a hard-and-fast rule), but this seems like it might have some promise.

It works almost perfectly for the LLVM umbrella of projects. When they want to change a coding convention (like leading Uppercase for variable names), they write a clang-tidy thing to do it automatically.

Quoted 2 lines

So overall I think it has some promise, but I do not think it is quite flexible enough yet for us to use day-to-day.

The big negative is that it will probably never be. I'll try to look at the larger issues later this week, if you can compromise on the fine details that are probably too hard to fix.

Quoted 3 lines

2. It can operate on patches (and generates patches for you to apply! You could add a git-add--interactive mode to selectively take its suggestions).

There's a git-clang-format in the $CLANG_ROOT/tools/clang-format/. I do:

   $ g cf @~

... with the appropriate aliases.

#6 lore raw
Quoted 6 lines · Ramkumar Ramachandra
Quoted 2 lines

So overall I think it has some promise, but I do not think it is quite flexible enough yet for us to use day-to-day.

The big negative is that it will probably never be. I'll try to look at the larger issues later this week, if you can compromise on the fine details that are probably too hard to fix.

The key to me is that we do not have necessarily take every suggestion the tool makes. So it does not have to be perfect, just "pretty good".

But...I think it is not quite so simple. The clang-format-diff script (and git-clang-format) _do_ seem to operate on more than just the lines I've changed. I'm not clear on whether they're examining the whole file (just with the patches applied), or there's something in between happening.

So rejecting the tool's suggestion one day may mean it suggests the same change to you any other time you touch nearby parts of the file, which could be annoying.

Quoted 9 lines
Quoted 3 lines

2. It can operate on patches (and generates patches for you to apply! You could add a git-add--interactive mode to selectively take its suggestions).

There's a git-clang-format in the $CLANG_ROOT/tools/clang-format/. I do:

   $ g cf @~

... with the appropriate aliases.

Neat. Debian's package does not ship with that. I hacked-in very rudimentary interactive-add support for clang-format-diff (below) before getting your response. It would be better built around "git-clang-format --diff" (though that script would need to be taught to do the right thing with the --color argument).

However, because of the "suggest the same change" thing I mentioned above, I am not sure whether interactively selecting is a good idea or not. You might end up having to say "no" to the same suggestions a lot.

Anyway, here it is, for reference. You can use it like:

  git add--interactive --patch=format --

and you could probably even stick an "exec" line into an interactive rebase to go through and fixup individual patches in a whole series.

---

.gitignore +1 -0
index a052419..6f5b815 100644
@@ -30,6 +30,7 @@
 /git-checkout-index
 /git-cherry
 /git-cherry-pick
+/git-clang-format-diff
 /git-clean
 /git-clone
 /git-column
Makefile +1 -0
index c44eb3a..113534e 100644
@@ -455,6 +455,7 @@ unexport CDPATH
 
 SCRIPT_SH += git-am.sh
 SCRIPT_SH += git-bisect.sh
+SCRIPT_SH += git-clang-format-diff.sh
 SCRIPT_SH += git-difftool--helper.sh
 SCRIPT_SH += git-filter-branch.sh
 SCRIPT_SH += git-merge-octopus.sh
git-add--interactive.perl +19 -0
index c725674..fd83adf 100755
@@ -167,6 +167,16 @@ my %patch_modes = (
 		FILTER => undef,
 		IS_REVERSE => 0,
 	},
+	'format' => {
+		DIFF => 'clang-format-diff',
+		APPLY => sub { apply_patch_for_checkout_commit '', @_ },
+		APPLY_CHECK => 'apply',
+		VERB => 'Apply',
+		TARGET => 'to index and worktree',
+		PARTICIPLE => 'applying',
+		FILTER => undef,
+		IS_REVERSE => 0
+	},
 );
 
 my %patch_mode_flavour = %{$patch_modes{stage}};
@@ -1591,6 +1601,15 @@ sub process_args {
 						       'checkout_head' : 'checkout_nothead');
 					$arg = shift @ARGV or die "missing --";
 				}
+			} elsif ($1 eq 'format') {
+				$patch_mode = $1;
+				$arg = shift @ARGV or die "missing --";
+				if ($arg eq '--') {
+					$patch_mode_revision = 'HEAD^';
+				} else {
+					$patch_mode_revision = $arg;
+					$arg = shift @ARGV or die "missing --";
+				}
 			} elsif ($1 eq 'stage' or $1 eq 'stash') {
 				$patch_mode = $1;
 				$arg = shift @ARGV or die "missing --";
git-clang-format-diff.sh new file +20 -0
new file mode 100755
index 0000000..9351883
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+# This is what it's called in the Debian package, but it seems
+# like there ought to be a symlink without the version...
+CFD=clang-format-diff-3.6
+
+# Strip out --color, as clang's patch reader cannot handle it.
+# Robustly handling arrays in bourne shell is insane.
+eval "set -- $(
+	for i in "$@"; do
+		test "--color" = "$i" && continue
+		printf " '"
+		printf '%s' "$i" | sed "s/'/'\\\\''/g"
+		printf "'"
+	done
+)"
+
+git diff-index -p "$@" |
+$CFD -p1 |
+sed -e 's,^--- ,&a/,' -e 's,^+++ ,&b/,'