tools: Automatic code format tool added

Tool allows auto formatting of code according to Google coding standard
for easier control and fixing formatting issues.
This commit is contained in:
pkosko 2020-12-10 02:38:22 +01:00 committed by GitHub
parent 1ed611386d
commit 1712e7d83f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 6522 additions and 0 deletions

1
code_format Symbolic link
View file

@ -0,0 +1 @@
code_formatter.sh

13
tools/codestyle/README Normal file
View file

@ -0,0 +1,13 @@
[Tools] Added scripts for code style validation and auto fixing
These tools are based on:
https://review.tizen.org/gerrit/gitweb?p=platform/core/api/webapi-plugins.git;a=tree;f=tools/codestyle;h=8a8bd7adfcfe7ed144e8937fda00ae4e3793c33a;hb=refs/heads/tizen
[Features]
- tools/codestyle/code_formatter.sh - formatter for C/C++ (changes files)
- tools/codestyle/c++_clang_formatter.sh - for auto formating C++ files
- tools/codestyle/cpplint_tizen_160919.py and tools/codestyle/cpplint_tizen_dir.sh
scripts for C++ code style validation, provided by HQ
- code_format - link to script for easier access
[Help] You can check usage for code_format using '-h' option

View file

@ -0,0 +1,59 @@
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$SCRIPT_DIR/$SOURCE"
# if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
# https://clangformat.com/
if [ ! "$(command -v clang-format)" ]; then
echo >&2 "clang-format is required, but it's not installed";
echo "-------------------------------------------------------------------------------------";
echo "To install clang-format on Debian/Ubuntu, execute the following commands."
echo " sudo apt-get install clang-format-3.9"
echo " sudo ln -s /usr/bin/clang-format-3.9 /usr/bin/clang-format"
echo "-------------------------------------------------------------------------------------";
exit 1;
fi
config='{
BasedOnStyle: Google,
Language: Cpp,
Standard: Cpp11,
UseTab: Never,
IndentWidth: 2,
ColumnLimit: 100,
PointerBindsToType: true,
AllowShortFunctionsOnASingleLine: false,
BreakBeforeBraces: GNU,
}'
formatC++() {
printf "."
find -- "$1" -type f \( -name '*.cc' -o -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \) | \
grep -v 'src/google' | xargs -n1 --no-run-if-empty clang-format -i -style="$config"
}
checkUpdatedFiles() {
while IFS= read -r -d '' f; do
if [ "${f: -3}" == ".cc" ] || [ "${f: -4}" == ".cpp" ] || [ "${f: -2}" == ".h" ] || \
[ "${f: -4}" == ".hpp" ]; then
printf '.'
clang-format -i -style="$config" "$f";
fi
done < <(git diff-index -z --name-only --diff-filter=AM HEAD --)
}
printf "C/C++ reformatting: ";
if [[ $# -eq 0 ]]; then
formatC++ "$SCRIPT_DIR/../../src/"
elif [[ "$1" == "-u" ]]; then
checkUpdatedFiles
else
formatC++ "$1"
fi
printf " DONE C/C++ reformatting\n"

View file

@ -0,0 +1,90 @@
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$SCRIPT_DIR/$SOURCE"
# if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
echo "$SCRIPT_DIR"
dir2analyze="$SCRIPT_DIR/../../src"
analyzeC="false";
files_to_update="";
printHelp() {
echo "Script for automatic fixing some coding style rules issues."
echo "In case of any issues of analysed code, the script would adjust it to follow"
echo "coding style. THIS COULD CHANGE YOUR FILES. Please commit your changes locally"
echo "if you want to have meaningful changes divided from style-only changes"
echo "Using it without specifying a path would analyse \"src\" from current directory."
printf -- "\nUsage: %s [directory] [options ...]\n" "$(basename "$0")"
printf -- "Options:\n"
printf -- "-u\t\tcheck all C/C++ files that has been modified since last commit and fix issues\n"
printf -- "-c\t\tcheck C/C++ files and fix issues\n"
printf -- "-a [--all]\tcheck all C/C++ files and fix issues\n"
printf -- "-h [--help]\tshow this help\n"
echo "When using any of 'u' options, all not commited files will be backed up."
}
formatC++() {
path="$(readlink -f "$1")"
if [[ "$analyzeC" == "true" ]]; then
echo "Reformatting C++ sources recursively for directory $path";
"$SCRIPT_DIR/c++_clang_formatter.sh" "$path";
else
echo "no C++ sources reformatting - please use '-c' option to enable it";
fi
}
backupUpdatedFiles() {
files_to_update="$(git diff-index -z --name-only --diff-filter=AM HEAD --)";
if [[ "$files_to_update" == "" ]]; then
echo "There are no updated files to format";
exit;
fi
time_stamp=$(date +%Y-%m-%d-%T);
dir_name="backup_${time_stamp}";
echo "Creating backup of not commited changes in directory: $dir_name";
mkdir -p "${dir_name}";
while IFS= read -r -d '' file; do
cp -- "$file" "$dir_name";
done <<< "$files_to_update"
}
formatUpdatedC++() {
if [[ "$files_to_update" == "" ]]; then
exit
fi
echo "Reformatting C++ sources from list:";
git diff-index --name-only --diff-filter=AM HEAD --;
"$SCRIPT_DIR/c++_clang_formatter.sh" -u;
}
for arg in "$@";
do
if [[ "$arg" == "-u" ]]; then
backupUpdatedFiles
formatUpdatedC++
exit
elif [[ "$arg" == "-c" ]]; then
analyzeC="true";
elif [[ "$arg" == "--all" ]] || [[ "$arg" == "-a" ]]; then
analyzeC="true";
elif [[ "$arg" == "--help" ]] || [[ "$arg" == "-h" ]]; then
printHelp
exit
else
if [[ -d "$arg" ]]; then
dir2analyze="$arg";
else
(>&2 printf "ERROR: directory %s does not exist\n\n" "$arg")
printHelp
exit 1
fi
fi
done
formatC++ "$dir2analyze"

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,16 @@
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$SCRIPT_DIR/$SOURCE"
# if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
SCRIPT_DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
patch_cpp="$SCRIPT_DIR/cpplint_tizen_160919.py"
# check rule to cpp
find -- "$1" \( -name "*.cpp" -o name "*.cc" \) -exec python "$patch_cpp" {} \;
find -- "$1" -name "*.h" -exec python "$patch_cpp" {} \;