Differences between revisions 9 and 18 (spanning 9 versions)
Revision 9 as of 2015-10-05 13:10:37
Size: 3481
Editor: JendrikSeipp
Comment:
Revision 18 as of 2023-07-19 09:23:58
Size: 1620
Editor: JendrikSeipp
Comment: Switch to uncrustofy 0.72.0
Deletions are marked like this. Additions are marked like this.
Line 9: Line 9:
== Mercurial "uncrustify" extension == == Installing uncrustify ==
Line 11: Line 11:
The repository contains a Mercurial extension (in file {{{misc/style/uncrustify.py}}}) that provides the new command Please note that we require a specific uncrustify version. On Ubuntu 22.04, 22.10 and 23.04, you get this uncrustify version by calling {{{sudo apt install uncrustify}}}, so on these Ubuntu versions you don't need the steps below.
Line 13: Line 14:
hg uncrustify
}}}
to automatically run {{{uncrustify}}} with the correct settings on your working directory.

=== Using the extension ===

Examples:
{{{
hg uncrustify
hg uncrustify .
hg uncrustify --diff bar/baz.cc foo
hg uncrustify --modify foo
hg uncrustify --modify --no-backup foo
wget https://github.com/uncrustify/uncrustify/archive/uncrustify-0.72.0.tar.gz
tar -xzvf uncrustify-0.72.0.tar.gz
cd uncrustify-uncrustify-0.72.0
mkdir build
cd build
cmake ../
make -j4
sudo cp uncrustify /usr/local/bin # Add binary to a directory on PATH.
Line 28: Line 24:
In order, these do the following:
 * List all files in the repository that need to be uncrustified (i.e. which violate the formatting conventions).
 * List all files in the repository below the current working directory that need to be uncrustified.
 * Show the diff that would need to be applied to uncrustify source file {{{bar/baz.cc}}} as well as all source files below directory {{{foo}}}.
 * Uncrustify all source files below directory {{{foo}}}. Backup files with extension {{{.crusty}}} are generated for all modified files, containing the original (not yet uncrustified) source.
 * Like the previous command, but without creating backups.
== Running uncrustify ==
Line 35: Line 26:
For more details, run To check the formatting of all C++ files, use `./misc/style/run-uncrustify.py`. To actually edit the files that need to be uncrustified, use `./misc/style/run-uncrustify.py --modify`. This script is also part of our tox tests:
Line 37: Line 29:
hg help uncrustify sudo apt install tox
cd misc/
tox -e fix-style
Line 40: Line 34:
=== Setting up the extension ===

/!\ Warning: Mercurial extensions can execute arbitrary code on your machine. If you don't trust everyone with write access to whatever repository you grabbed {{{uncrustify.py}}} from, don't use it.

First of all, you will need {{{uncrustify}}} itself. We require at least version 0.61 which added support for C++11. Since this version is in none of the Ubuntu repositories, we provide the steps for compiling and installing it:
{{{
# Grab uncrustify-0.61.tar.gz from Sourceforge: http://sourceforge.net/projects/uncrustify/files/uncrustify/
# Github also hosts uncrustify releases, but "uncrustify --version" will print "uncrustify 0.60" for the github 0.61 version.
tar -xzvf uncrustify-0.61.tar.gz
cd uncrustify-0.61
./configure
make
sudo cp src/uncrustify /usr/local/bin # Or somewhere else on your PATH.
hash -r # Clear PATH cache.
uncrustify --version # Check version.
}}}

To set up the actual extension, add the following lines to your {{{hgrc}}}: {{{
[extensions]
uncrustify = FULL_PATH_TO/uncrustify.py

[alias]
uncrustify = uncrustify -X re:^src/VAL
}}}
where {{{FULL_PATH_TO/uncrustify.py}}} is the full path to the {{{uncrustify.py}}} file (absolute, but may use {{{~}}} expansion). Alternatively, install {{{uncrustify.py}}} in some directory on your {{{PYTHONPATH}}} and write {{{
[extensions]
uncrustify =
}}}
in your {{{hgrc}}} instead of providing the full path to the source file.

The {{{alias}}} section above is optional. It makes {{{hg uncrustify}}} ignore the {{{src/VAL}}} directory, which does not need to be uncrustified.
To check the style of Python and C++ files, you can use `tox -e style`.

Back to developer page.

Using uncrustify to fix code layout

We use uncrustify with a Fast Downward configuration file (.uncrustify.cfg in the repository root) to enforce some of our formatting conventions. If a source file is properly formatted, applying uncrustify should be an idempotent operation (i.e., result in an identical file). This is important so that we can verify our style rules automatically, at least for those rules which uncrustify can handle.

Not all aspects of source layout are handled by uncrustify, so please still pay attention to our ../Whitespace rules and other ../CodingConventions.

Installing uncrustify

Please note that we require a specific uncrustify version. On Ubuntu 22.04, 22.10 and 23.04, you get this uncrustify version by calling sudo apt install uncrustify, so on these Ubuntu versions you don't need the steps below.

wget https://github.com/uncrustify/uncrustify/archive/uncrustify-0.72.0.tar.gz
tar -xzvf uncrustify-0.72.0.tar.gz
cd uncrustify-uncrustify-0.72.0
mkdir build
cd build
cmake ../
make -j4
sudo cp uncrustify /usr/local/bin  # Add binary to a directory on PATH.

Running uncrustify

To check the formatting of all C++ files, use ./misc/style/run-uncrustify.py. To actually edit the files that need to be uncrustified, use ./misc/style/run-uncrustify.py --modify. This script is also part of our tox tests:

sudo apt install tox
cd misc/
tox -e fix-style

To check the style of Python and C++ files, you can use tox -e style.

FastDownward: ForDevelopers/Uncrustify (last edited 2023-07-19 09:23:58 by JendrikSeipp)