Skip to content

How to format code

Adam Dickin edited this page Jun 13, 2025 · 9 revisions

Format via make commands

There are now two make commands in the repo that can help with formatting.

make check_format: this will check your format and output an error if something is incorrectly formatted. Does not make changes
make format: this will format any files that are incorrectly formatted automatically. Makes Changes

Format Via External Script

Run this script from the root of MIOpen tree (where the .clang-format file resides) prior committing your edits to avoid formatting failures:

CLANG_FORMAT='clang-format-12'
if ! command -v $CLANG_FORMAT 2>&1 /dev/null
then
    echo "${CLANG_FORMAT} is not installed or in your path"
    exit 1
fi

find . -iname '*.h' \
    -o -iname '*.hpp' \
    -o -iname '*.cpp' \
    -o -iname '*.h.in' \
    -o -iname '*.hpp.in' \
    -o -iname '*.cpp.in' \
    -o -iname '*.cl' \
| grep -v -E '(build/)|(install/)' \
| xargs -n 1 -P $(nproc) -I{} -t ${CLANG_FORMAT} -style=file {} -i 2>/dev/null

The script reformats source files, when necessary. Please do not forget to commit reformatted source files after that.

Note: | grep -v -E '(build/)|(install/)' is used to avoid formatting within ./build/ and ./install/; please fix this line according to your needs.

Clone this wiki locally