Commit 710a986c authored by Brad King's avatar Brad King
Browse files

clang-format-kwsys: new formatter specific to KWSys

KWSys has sources that use `@KWSYS_NAMESPACE@` and similar placeholders
that `clang-format` does not understand.  Create a special formatter
that filters sources before and after running `clang-format` in order
to protect such placeholders.
parent 180e9acc
add_formatter(clang-format reformat clang-format)
add_formatter(clang-format-kwsys reformat clang-format)
formatter_clang-format:
kind: "clang-format"
script: "@CMAKE_INSTALL_PREFIX@/bin/format.clang-format-kwsys"
config_files:
- ".clang-format"
#!/bin/sh
# This formatter is meant specifically for the KWSys project.
# It performs special filtering before and after running
# `clang-format` in order to protect `@KWSYS_NAMESPACE@`
# and other placholders using `@`.
#
# This formatter uses the `clang-format` tool to perform
# formatting of files written in C, C++, and other C-like
# languages (CUDA, GLSL, etc.).
#
# It uses the `clang-format` executable in `$PATH` and checks for
# the existence of the `.clang-format` file as its configuration.
set -e
readonly path="$1"
shift
if ! [ -f "$path" ]; then
echo >&2 'error: could not find file to format: '"$path"
exit 4
fi
if ! which "clang-format" > /dev/null; then
echo >&2 'error: could not find the `clang-format` binary'
exit 3
fi
if ! [ -f ".clang-format" ]; then
echo >&2 'error: the `.clang-format` file is missing'
exit 2
fi
# Transform the `@KWSYS_...@` placeholders to `xKWSYS_...x` so
# clang-format can understand the symbols and preserve lengths.
sed -i 's/@\(KWSYS_[A-Z0-9_]\+\)@/x\1x/g' "$path"
clang-format -i "$path"
# Restore the original `@KWSYS_...@` placeholders.
sed -i 's/x\(KWSYS_[A-Z0-9_]\+\)x/@\1@/g' "$path"
add_formatter_test(clang-format-kwsys pass format.c pass)
add_formatter_test(clang-format-kwsys format format.c format)
add_formatter_test(clang-format-kwsys no-config-file format.c fail)
@KWSYS_NAMEPACE@_EXPORT dummy();
int main(int argc, char *argv[]) {
return 0;
}
@KWSYS_NAMEPACE@_EXPORT dummy();
int main(int argc, char *argv[]) {
return 0;
}
@KWSYS_NAMEPACE@_EXPORT dummy();
int main(int argc, char *argv[]) { return 0; }
@KWSYS_NAMESPACE@_EXPORT dummy();
int main(int argc, char *argv[]) { return 0; }
@KWSYS_NAMESPACE@_EXPORT dummy();
int main(int argc, char *argv[]) { return 0; }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment