clang-format
is a tool within the clang
ecosystem, that may be used to enforce code styling policies (code formatter).
In addition to applying the desired format, we may also use it to check if a set of files conforms to the code styling policy by running clang-format
with a combination of the --dry-run
and -Werror
flags. E.g. let myfile.cpp
#include <iostream>
auto main() -> int {
// Note the single space indentation width
std::cout << "Hello!" << std::endl;
}
and .clang-format
BasedOnStyle: LLVM
IndentWidth: 4
then running clang-format
as follows will return with an exit code of 1 alongside a couple of log lines reporting formatting errors:
clang-format --style=file --dry-run -Werror myfile.cpp
The essential parts of the call are:
--style=file
: tellsclang-format
to look for the closest.clang-format
file and check for the policies declared in it;--dry-run
: does not apply formatting fixes for the configured policies; and-Werror
: reports style policy violations as errors instead of warnings and returns a non-zero exit code in case there are errors.