提交 a33eaf5f 编写于 作者: W wujing 提交者: lifeng68

add clang-tidy static check tool && fix some code

Signed-off-by: Nwujing <wujing50@huawei.com>
上级 3278f512
......@@ -14,24 +14,20 @@
******************************************************************************/
#ifndef __CLIENT_BASH_H
#define __CLIENT_BASH_H
#include <fstream>
#include <grpc++/grpc++.h>
#include <iostream>
#include <string>
#include <memory>
#include <grpc++/grpc++.h>
#include <sstream>
#include <fstream>
#include <string>
#include "certificate.h"
#include "connect.h"
#include "error.h"
#include "isula_libutils/log.h"
#include "connect.h"
#include "utils.h"
#include "certificate.h"
using grpc::Channel;
using grpc::ClientContext;
using grpc::ClientReader;
using grpc::ClientReaderWriter;
using grpc::ClientWriter;
using grpc::Status;
namespace ClientBaseConstants {
......@@ -99,9 +95,9 @@ public:
response->cc = ISULAD_ERR_EXEC;
}
virtual int run(const RQ *request, RP *response)
virtual auto run(const RQ *request, RP *response) -> int
{
int ret;
int ret = 0;
gRQ req;
gRP reply;
ClientContext context;
......@@ -154,24 +150,24 @@ public:
}
protected:
virtual int request_to_grpc(const RQ *rq, gRQ *grq)
virtual auto request_to_grpc(const RQ * /*rq*/, gRQ * /*grq*/) -> int
{
return 0;
};
virtual int response_from_grpc(gRP *reply, RP *response)
virtual auto response_from_grpc(gRP * /*reply*/, RP * /*response*/) -> int
{
return 0;
};
virtual int check_parameter(const gRQ &grq)
virtual auto check_parameter(const gRQ & /*grq*/) -> int
{
return 0;
};
virtual Status grpc_call(ClientContext *context, const gRQ &req, gRP *reply)
virtual auto grpc_call(ClientContext * /*context*/, const gRQ & /*req*/, gRP * /*reply*/) -> Status
{
return Status::OK;
};
std::string ReadTextFile(const char *file)
auto ReadTextFile(const char *file) -> std::string
{
char *real_file = verify_file_and_get_real_path(file);
if (real_file == nullptr) {
......@@ -191,7 +187,7 @@ protected:
return ss.str();
}
int SetMetadataInfo(ClientContext &context)
auto SetMetadataInfo(ClientContext &context) -> int
{
// Set common name from cert.perm
char common_name_value[ClientBaseConstants::COMMON_NAME_LEN] = { 0 };
......@@ -215,7 +211,7 @@ protected:
};
template <class REQUEST, class RESPONSE, class FUNC>
int container_func(const REQUEST *request, RESPONSE *response, void *arg) noexcept
auto container_func(const REQUEST *request, RESPONSE *response, void *arg) noexcept -> int
{
if (request == nullptr || response == nullptr || arg == nullptr) {
ERROR("Receive NULL args");
......
......@@ -20,10 +20,10 @@
#include <stdio.h>
#include "constants.h"
#include "io_wrapper.h"
#include "isula_libutils/container_path_stat.h"
#include "isula_libutils/json_common.h"
#include "utils_timestamp.h"
#include "io_wrapper.h"
#ifdef __cplusplus
extern "C" {
......
......@@ -17,8 +17,7 @@
##- @Create: 2019-04-25
#######################################################################
CURRENT_PATH=$(pwd)
export EULER_CODE_PATH="$(realpath ${CURRENT_PATH}/..)"
CURRENT_PATH=$(dirname $(readlink -f "$0"))
function usage() {
echo -e "\
......@@ -33,6 +32,8 @@ function usage() {
echo "Personal level build static check script for iSulad project"
echo "Options:"
echo " -s, --codestyle Perform codestyle(codedex) code static check"
echo " -c, --tidy-check Perform clang-tidy code static check"
echo " -x, --tidy-fix Quick fix code with clang-tidy"
echo " -a, --all Perform all checks and statistics"
echo " -i, --incremental-check Perform incremental check"
echo " -f, --quick-format Incremental format code by astyle/clang-format"
......@@ -332,6 +333,131 @@ function cpp_check() {
fi
}
function clang_tidy_check() {
which clang-tidy > /dev/null
if [[ $? -ne 0 ]]; then
echo "please install clang-tidy tool first"
exit 1
fi
echo -e "\
=================================================================================================\033[1;33m
████████╗██╗██████╗ ██╗ ██╗ ██████╗██╗ ██╗███████╗ ██████╗██╗ ██╗
╚══██╔══╝██║██╔══██╗╚██╗ ██╔╝ ██╔════╝██║ ██║██╔════╝██╔════╝██║ ██╔╝
██║ ██║██║ ██║ ╚████╔╝ ██║ ███████║█████╗ ██║ █████╔╝
██║ ██║██║ ██║ ╚██╔╝ ██║ ██╔══██║██╔══╝ ██║ ██╔═██╗
██║ ██║██████╔╝ ██║ ╚██████╗██║ ██║███████╗╚██████╗██║ ██╗
╚═╝ ╚═╝╚═════╝ ╚═╝ ╚═════╝╚═╝ ╚═╝╚══════╝ ╚═════╝╚═╝ ╚═╝\033[0m
================================================================================================="
local start_time=$(date +%s)
if [[ ! -f ${CURRENT_PATH}/../compile_commands.json ]]; then
echo "compile_commands.json file not found in project root dirctory, generating..."
mkdir -p ${CURRENT_PATH}/../build
cd ${CURRENT_PATH}/../build
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ../
cp compile_commands.json ../
cd ${CURRENT_PATH}
fi
local files
if [[ ${1} == "all" ]]; then
files=$(find ./src ./test -regextype posix-extended -regex ".*\.(h|c|cc)")
elif [[ ${1} == "incremental" ]]; then
files=$(git diff --name-only HEAD | grep -E "*.h$|*.c$|*.cc$")
elif [[ -f "$1" ]]; then
files="$1"
elif [[ -d "$1" ]]; then
files=$(find "$1" -regextype posix-extended -regex ".*\.(h|c|cc)")
fi
files=(${files// / })
local total=${#files[@]}
local index=1
logfile=${CURRENT_PATH}/../clang-tidy-check.log
echo "" > ${logfile}
if [[ ${total} -eq 1 ]]; then
clang-tidy -checks='-*,abseil-*,bugprone-*,cert-*,clang-analyzer-*,cppcoreguidelines-*, \
google-*,hicpp-*,linuxkernel-*,llvm-*,llvmlibc-*,-llvm-header-guard,misc-*,modernize-*,performance-*,portability-*,readability-*' ${files[0]}
return 0
fi
for file in ${files[@]}
do
echo ">>>>>>>>>>>>>>>>>>checking: ${file}" >> ${logfile}
clang-tidy -checks='-*,abseil-*,bugprone-*,cert-*,clang-analyzer-*,cppcoreguidelines-*, \
google-*,hicpp-*,linuxkernel-*,llvm-*,llvmlibc-*,-llvm-header-guard,misc-*,modernize-*,performance-*,portability-*,readability-*' ${file} \
>> ${logfile} 2>&1
echo ">>>>>>>>>>>>>>>>>>checked: ${file}" >> ${logfile}
printf "[\033[1;36m%03d\033[0m\033[1;33m/\033[0m\033[1;34m%03d\033[0m]@%-80s \033[1;32m%-5s\033[0m\n" \
${index} ${total} ${file} "[CHECKED]" | sed -e 's/ /-/g' -e 's/@/ /' -e 's/-/ /'
index=$((index+1))
done
printf "%0.s=" {1..96}
printf "\n"
local end_time=$(date +%s)
local duration=$((${end_time} - ${start_time}))
echo -e "\033[1;36mTotal files: ${total}\033[0m. \033[1;33mSpend time: ${duration} seconds\033[0m"
echo -e "\033[1;31mCode analysis report ${logfile} is in the root directory of the project, please refer to the modification\033[0m"
}
function clang_tidy_fix() {
which clang-tidy > /dev/null
if [[ $? -ne 0 ]]; then
echo "please install clang-tidy tool first"
exit 1
fi
echo -e "\
=================================================================================================\033[1;33m
████████╗██╗██████╗ ██╗ ██╗ ██████╗ ██████╗ ██████╗ ███████╗ ███████╗██╗██╗ ██╗
╚══██╔══╝██║██╔══██╗╚██╗ ██╔╝ ██╔════╝██╔═══██╗██╔══██╗██╔════╝ ██╔════╝██║╚██╗██╔╝
██║ ██║██║ ██║ ╚████╔╝ ██║ ██║ ██║██║ ██║█████╗ █████╗ ██║ ╚███╔╝
██║ ██║██║ ██║ ╚██╔╝ ██║ ██║ ██║██║ ██║██╔══╝ ██╔══╝ ██║ ██╔██╗
██║ ██║██████╔╝ ██║ ╚██████╗╚██████╔╝██████╔╝███████╗ ██║ ██║██╔╝ ██╗
╚═╝ ╚═╝╚═════╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝ ╚═╝ ╚═╝╚═╝ ╚═╝\033[0m
================================================================================================="
local start_time=$(date +%s)
if [[ ! -f ${CURRENT_PATH}/../compile_commands.json ]]; then
echo "compile_commands.json file not found in project root dirctory, generating..."
mkdir -p ${CURRENT_PATH}/../build
cd ${CURRENT_PATH}/../build
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ../
cp compile_commands.json ../
cd ${CURRENT_PATH}
fi
local files
if [[ ${1} == "all" ]]; then
files=$(find ./src ./test -regextype posix-extended -regex ".*\.(h|c|cc)")
elif [[ ${1} == "incremental" ]]; then
files=$(git diff --name-only HEAD | grep -E "*.h$|*.c$|*.cc$")
elif [[ -f "$1" ]]; then
files="$1"
elif [[ -d "$1" ]]; then
files=$(find "$1" -regextype posix-extended -regex ".*\.(h|c|cc)")
fi
files=(${files// / })
local total=${#files[@]}
local index=1
if [[ ${total} -eq 1 ]]; then
clang-tidy -checks='-*,abseil-*,bugprone-*,cert-*,clang-analyzer-*,cppcoreguidelines-*, \
google-*,hicpp-*,linuxkernel-*,llvm-*,llvmlibc-*,-llvm-header-guard,misc-*,modernize-*,performance-*,portability-*,readability-*' --fix ${files[0]}
return 0
fi
for file in ${files[@]}
do
clang-tidy -checks='-*,abseil-*,bugprone-*,cert-*,clang-analyzer-*,cppcoreguidelines-*, \
google-*,hicpp-*,linuxkernel-*,llvm-*,llvmlibc-*,-llvm-header-guard,misc-*,modernize-*,performance-*,portability-*,readability-*' --fix ${file} >/dev/null 2>&1
printf "[\033[1;36m%03d\033[0m\033[1;33m/\033[0m\033[1;34m%03d\033[0m]@%-80s \033[1;32m%-5s\033[0m\n" \
${index} ${total} ${file} "[REPAIRED]" | sed -e 's/ /-/g' -e 's/@/ /' -e 's/-/ /'
index=$((index+1))
done
printf "%0.s=" {1..96}
printf "\n"
local end_time=$(date +%s)
local duration=$((${end_time} - ${start_time}))
echo -e "\033[1;36mTotal files: ${total}\033[0m. \033[1;33mSpend time: ${duration} seconds\033[0m"
echo -e "\033[1;31mThe code is repaired, please recompile and check\033[0m"
}
function incremental_check() {
style_check "incremental"
if [[ $? -ne 0 ]]; then
......@@ -362,13 +488,15 @@ function static_check_all() {
fi
}
args=`getopt -o siaf:kh --long codestyle,incremental-check,all,quick-format:,style-check,cpp-check,help -- "$@"`
args=`getopt -o sc:x:iaf:kh --long codestyle,tidy-check:,tidy-fix:,incremental-check,all,quick-format:,style-check,cpp-check,help -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$args"
while true; do
case "$1" in
-s|--codestyle) codestyle_check "all" || (err "failed to perfrom codestyle(codedex) code static check" && exit -1); shift ;;
-c|--tidy-check) clang_tidy_check "$2" || (err "failed to perform clang-tidy code static check" && exit -1); shift 2;;
-x|--tidy-fix) clang_tidy_fix "$2" || (err "failed to quick fix code with clang-tidy" && exit -1); shift 2;;
-i|--incremental-check) incremental_check || (err "failed to perform incremental check" && exit -1); shift ;;
-a|--all) static_check_all || (err "failed to perform all checks and statistics" && exit -1); shift ;;
-f|--quick-format) quick_format $2 || (err "failed to format code" && exit -1); shift 2 ;;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册