Skip to content

Using code formating tools

Marcin Tomiec edited this page Apr 24, 2018 · 3 revisions

1. Overview of code formatting tools

There are many code formatting tools. During the research we have chosen these two tools for further comparision:

  • AStyle
  • clang-format

2. Installing and using AStyle

AStyle can work on the Linux, Windows and MacOSX operating systems. Detailed instruction of installing AStyle on each platform are available on the AStyle project website: http://astyle.sourceforge.net/install.html

3. Using AStyle:

3.1 Using in command line

AStyle can be used from command line on every platform. Before using it in this way be sure, that the directory with AStyle binaries is in PATH variable.

Example of usage:
   
   > AStyle --style=attach -s4 -xC80 .\example1.hpp
Explanation:
* --style=attach - attached braces (Java style)
* -s4            - indent using 4 spaces
* -xC80          - maximum size of line = 80 columns


List of all parameters with explanation is available after entering:

  ` > AStyle --help`

3.2 Integrating with IDE

AStyle can be integrated with various IDEs, for example with Visual Studio, for which the ready to use AStyle extension is available:

Using of that extension is very easy. Firstly we should define the schema of code correcting. To do that go to: Tools -> Options -> AStyle Formatter -> C/C++ -> Settings

Now we are able to modify the parameters of code formatting:

After setting formatting options we can format our code by going to: Edit -> Advanced -> Format Document (AStyle) or Format Selection (AStyle)

Or we can enable "Format on save" option in AStyle Formatter options.

4. Installing clang-format

Sources and pre-built binaries of the LLVM package which include clang-format are available on the website below: http://releases.llvm.org

Also the plugin for Visual Studio is available: https://marketplace.visualstudio.com/items?itemName=LLVMExtensions.ClangFormat

5. Using clang-format

5.1 Using from command line

Clang-format, like AStyle, can be used from command line. But the way of using clang-format is a bit different. We don't give the formatting options as the parameters. Clang-format is loading the list of options form predefined schemas (LLVM, Google, Chromium, Mozilla and WebKit) or from file defined by user. It is possible to display the predefined options by using the command:

clang-format --style=llvm -dump-config

And then we can build our own list of options. Example list of options is in link below:

https://github.com/mtszkw/impl-przemyslowe/blob/code_formatting/_clang-format

The formatting options should be placed in file named ".clang-format" (Linux) or "_clang-format" (Windows) If we have the proper file in root of project, we can format the code using this command:

clang-format -style=file -i <code files>

Parameter -i is causing overwriting of original files.

5.2 Integrating with IDE

Clang-format also can be integrated with various IDEs, for example with Visual Studio, for which there is a plugin which I have mentioned earlier.

After installing plugin we can configure it by going to: Tools->Options->LLVM/Clang->ClangFormat

Here, in "Style" option we can select the coding style. If we choose "file", we must remember, that the file "_clang-format" with proper parameters must be placed in root directory of the project.

After saving the settings we can do the code formatting by going to: Tools->Clang Format Selection or Clang Format Document

6. Which one is better?

After testing of this tools we decided to select clang-format. First of all parameters, which are placed in file are more understandable, because they have better description than parameters given in command line in AStyle. And it's much easier to share the list of parameters by sending the "_clang-format" file. During the tests we have also discovered, that AStyle have some troubles with very long lines. Sometimes it wasn't able to break the lines properly.

7. Integrating clang-format with Cmake

Clang-format can be easily integrated with Cmake. To do that we have to write the proper script. The example of script is in link below: https://github.com/mtszkw/impl-przemyslowe/blob/code_formatting/cmake/clang-cxx-dev-tools.cmake

Then we have to include it in CMakeLists.txt file:

include(cmake/clang-cxx-dev-tools.cmake)

After building the project using CMake we should be able to perform code formatting by

make clang-format

Make sure, that _clang-format file is in root directory of project.