[cmake-developers] Code style auto-formatting

Daniel Pfeifer daniel at pfeifer-mail.de
Wed Apr 27 19:12:35 EDT 2016


On Thu, Nov 19, 2015 at 3:26 PM, Brad King <brad.king at kitware.com> wrote:
> On 11/19/2015 09:22 AM, Robert Dailey wrote:
>> Tonight I will do some testing and submit a patch + example files
>> converted to the style Brad suggested. We can fine-tune it as needed.
>
> Rather than a patch please work on a script to perform the conversion.
> Then at any time we can choose to run the script as the one-shot conversion
> and commit as a robot user.  Any sweeping patch would be too big to
> post to the list anyway.  Ideally the script should be simple enough
> to cut-n-paste into a shell so we can just record it in the commit
> message.
>
> See this commit for an example of this approach:
>
>  https://cmake.org/gitweb?p=cmake.git;a=commit;h=7bbaa428

It took a while, but i figured out .clang-format definitions (there
are two required) and a script that, after applying, does not break
compilation and unit tests.

The .clang-format for the root directory looks like this:

---
BasedOnStyle: Mozilla
AlignAfterOpenBracket: false
AlignOperands: false
AllowShortFunctionsOnASingleLine: None
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
ColumnLimit: 79
ContinuationIndentWidth: 4
IncludeCategories:
 - Regex: 'cmStandardIncludes'
   Priority: -1
Standard: Cpp03
...

This is the script to perform the reformatting of most C/C++ source code:

git ls-files -z -- '*.c' '*.cc' '*.cpp' '*.cxx' '*.h' '*.hh' '*.hpp' '*.hxx' \
| egrep -z -v '(Lexer|Parser|ParserHelper)\.' \
| egrep -z -v '^(Utilities/(KW|cm).*/|Source/(kwsys|CursesDialog/form)/)' \
| egrep -z -v '^Tests/Module/GenerateExportHeader' \
| egrep -z -v '^Tests/RunCMake/CommandLine/cmake_depends/test_UTF-16LE.h' \
| xargs -0 clang-format -i

I have attached a patch that adds the .clang-format and two other
patches that need to be applied before reformatting.
I have pushed all those changes (including reformatting) to github:
https://github.com/purpleKarrot/CMake/commits/clang-format


Notes:

* Trying to reformat
Tests/RunCMake/CommandLine/cmake_depends/test_UTF-16LE.h makes
clang-format crash.

* Clang-Format dedents preprocessor directives. See also:
https://llvm.org/bugs/show_bug.cgi?id=17362

* I did not include '*.in' files in the list, because Clang-Format
does strange things to @VARIALES at .


Please feel free to add/remove options from the .clang-format files.
IncludeCategories and Standard should stay as they are.

cheers, Daniel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-CursesDialog-add-missing-cmState-include.patch
Type: text/x-patch
Size: 703 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/cmake-developers/attachments/20160428/605dde8e/attachment-0003.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0002-StringFileTest-add-blank-line-between-includes.patch
Type: text/x-patch
Size: 858 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/cmake-developers/attachments/20160428/605dde8e/attachment-0004.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0003-add-.clang-format-configuration-files.patch
Type: text/x-patch
Size: 1724 bytes
Desc: not available
URL: <http://public.kitware.com/pipermail/cmake-developers/attachments/20160428/605dde8e/attachment-0005.bin>


More information about the cmake-developers mailing list