[Development] clang-format

Mitch Curtis mitch.curtis at qt.io
Mon Oct 14 14:51:41 CEST 2019

> -----Original Message-----
> From: Development <development-bounces at qt-project.org> On Behalf Of
> Frederik Gladhorn
> Sent: Monday, 14 October 2019 1:46 PM
> To: development at qt-project.org
> Subject: [Development] clang-format
> Hi,
> after just a bit over a year, we have a hint of progress on the state of using
> clang-format.
> If you use qt5.git, you'll get the git hook for it set up, to automatically run
> clang-format on the lines you changed.
> To benefit today, get the dev branch of qt5 and run init-repository -f --force-
> hooks.
> The commit hook runs and shows a diff of what should be changed, but
> doesn't add anything, so your changes should be safe from unwanted
> reformatting no matter what.
> If you think the changes make sense, just run "git clang-format" and all the
> things you have added to the staging area will be reformatted.
> git add myfile # stage a file
> git commit # try to commit, clang-format thinks you need to reformat git
> clang-format # apply the reformatting to the index git diff # see what clang-
> format did
> The hook script which is probably far from optimal (written in sh) is found in
> qt/qtrepotools. You can also just link https://code.qt.io/cgit/qt/
> qtrepotools.git/tree/git-hooks/clang-format-pre-commit as pre-commit into
> your .git/hooks directory and you get to enjoy it.
> I bet we'll have some issues and it's not quite perfect, but hey, small
> progress.
> Enjoy re-formatting and feel relaxed about it too ;)
> Cheers,
> Frederik

For anyone curious about how it looks, here's a dummy test I did:

mitch at mitch-ubuntu-18:~/dev/qt5.13/qtquickcontrols2$ git commit
clang-format output:
diff --git a/tests/auto/qquickmenubar/tst_qquickmenubar.cpp b/tests/auto/qquickmenubar/tst_qquickmenubar.cpp
index c0d9f8ed..9a22d26f 100644
--- a/tests/auto/qquickmenubar/tst_qquickmenubar.cpp
+++ b/tests/auto/qquickmenubar/tst_qquickmenubar.cpp
@@ -543,8 +543,7 @@ void tst_qquickmenubar::addRemove()
     QCOMPARE(menuBar->count(), 1);
-    QCoreApplication::sendPostedEvents(
-        menu1.data(), QEvent::DeferredDelete);
+    QCoreApplication::sendPostedEvents(menu1.data(), QEvent::DeferredDelete);
     QCoreApplication::sendPostedEvents(menuBarItem1, QEvent::DeferredDelete);
clang-format detected change in format, please run:
  git clang-format
and amend the change.

As a side note, I installed it on Ubuntu 18.04.3 with:

sudo apt-get install clang-format

but there was no git-clang-format binary, even though https://packages.ubuntu.com/search?searchon=contents&keywords=git-clang-format&mode=exactfilename&suite=disco&arch=any said there should be, so I just made a symbolic link to it:

sudo ln -s /usr/bin/git-clang-format-6.0 /usr/bin/git-clang-format

Would be great to know if there's a proper way to do this that I'm missing.

> _______________________________________________
> Development mailing list
> Development at qt-project.org
> https://lists.qt-project.org/listinfo/development

More information about the Development mailing list