Skip to content

Clang Format Style

TJC edited this page Nov 16, 2021 · 1 revision

Clang Format

OpenStudio uses the clang-format utility to enforce code styling rules that are defined in the .clang-format file. While the CI system will automatically run the clang-format utility to check and make sure the code is properly formatted, it is up to the developer to apply these rules to the code. The clang format tool is easy to run and is supported on all platforms.

Install

The installer binaries can be downloaded from https://releases.llvm.org/. OpenStudio is using version 11.

Running

Once the clang-format is installed, you can run it by using the CI script that compares cpp|hpp|c|h file types that have been modified by your branch. For example, if I created a branch issue_4000 and I want to create a pull request to the develop branch, I would run the clang-format.sh as follows:

./ci/clang-format.sh issue_4000 develop

The above will first identify the files that have changed between issue_4000 and develop and then run the clang-format utility on those files. The clang-format utility will automatically apply styling rules to any files that need to be updated. After it runs, commit any changed files back the branch and then re-run the tool again to verify than no changes have been made by clang-format.

Another way to run clang-format is to run it on the entire code base, or a sub-directory of the code base. For example, here are some useful commands to run clang-format on every cpp|hpp|c|h file under ./src directory

linux and windows (git-bash enabled windows only)

find ./src -regextype posix-extended -regex '.*\.(cpp|hpp|c|h)$' | xargs clang-format -style=file -i -fallback-style=none

mac os

find -E ./src -regex '.*\.(cpp|hpp|c|h)$' | xargs clang-format -style=file -i -fallback-style=none

Integrating IDE with clang-format

Many IDEs allow integration of the clang-format utility. Some useful links below to setup for popular IDEs.