Coding Format
February 26, 2026 ยท View on GitHub
The C/C++ code under the compiler directory is formatted using clang-format.
The format is specified by the .clang-format
file. Every commit must be formatted using this tool. The OMR project runs a
linter to check the coding format of the compiler component.
Developer Tools
There are a few resources available to developers to improve their workflow.
clang-format Dockerfile
The output generated by clang-format can vary by version (even minor version);
therefore, the OMR project uses a
Dockerfile to encapuslate the
tool. The linter, as well as the scripts below, use this Dockerfile.
To build:
pushd .
cd buildenv/jenkins/clang_format
docker build -t clang-format -f Dockerfile .
popd
To run:
docker run -v $PWD:/src clang-format:latest clang-format -style=file:compiler/.clang-format <FILE> > <FILE>.formatted
mv <FILE>.formatted <FILE>
clang-format binary
Alternatively, you may want to use the clang-format binary directly. To
acquire the correct version of the binary, look at the URL in the Dockerfile
and either use that if you are running on x86, or download the exact version
specified in the URL.
Rebase Helper
The rebase helper script helps prepare commits created prior to the Code Formatting PR so that they can be rebased across it. However, this script has the limitation that it does not work if there are multiple commits for the same file.
#!/bin/bash
if [ -z "\$1" ] ; then
echo "Specify the number of commits to prepare for rebase."
exit
fi
rebase_clang_format_helper="/tmp/rebase_clang_format_helper.sh"
cat <<RCFH > $rebase_clang_format_helper
#!/bin/bash
for FILE in \$(git diff --name-only HEAD~ HEAD)
do
formatted="\$FILE.formatted"
case "\$FILE" in
compiler/*.c | \
compiler/*.cpp | \
compiler/*.h | \
compiler/*.hpp)
docker run --rm -v $PWD:/src localhost/clang-format:latest clang-format -style=file:compiler/.clang-format \$FILE > \$formatted
mv \$formatted \$FILE
;;
*)
;;
esac
done
if [ "\$(git diff --name-only)" != "" ] ; then
git commit -a --fixup \$(git rev-parse HEAD)
fi
true
RCFH
chmod u+rx $rebase_clang_format_helper
rebaseSHA=$(git rev-parse HEAD~\$1)
git rebase -x $rebase_clang_format_helper $rebaseSHA
GIT_EDITOR=true git rebase -i --autosquash $rebaseSHA
rm $rebase_clang_format_helper
git pre-commit hook
The pre-commit git hook can be used to automatically format the code for each
commit before actually creating the commit. This frees up the developer from
having to think about whether they have formatted their code correctly.
#!/usr/bin/bash
for FILE in $(git diff --cached --name-only)
do
formatted="$FILE.formatted"
case "$FILE" in
compiler/*.c | \
compiler/*.cpp | \
compiler/*.h | \
compiler/*.hpp)
docker run --rm -v $PWD:/src clang-format:latest clang-format -style=file:compiler/.clang-format $FILE > $formatted
mv $formatted $FILE
git add $FILE
;;
*)
;;
esac
done