当前位置: 首页 > news >正文

使用shell脚本进行clang-tidy静态代码分析

文章目录

    • 0. 引言
    • 1. 完整检测脚本代码 clang-tidy-check.sh
      • 1.1 流程图
      • 1.2 脚本功能概述
    • 2. 该脚本优缺点

0. 引言

clang-tidy 是基于 Clang 的工具,提供了丰富的代码检查功能,可以根据用户配置文件进行定制化的检查和规则定义。
之前的文章《使用 Clang-Tidy 进行静态代码分析:完整的配置与 CMake 集成实例》已经对clang-tidy的安装和配置做了基本介绍,并指明了如何与CMake集成。
本文将介绍如何使用 shell脚本进行clang-tidy静态代码分析。

1. 完整检测脚本代码 clang-tidy-check.sh

#!/bin/bash
set -e# Default build directory
DEFAULT_BUILD_DIR="build"# Check if source dir path and optionally a build dir path are provided as arguments
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; thenecho "Usage: $0 <source_dir_path> [build_dir_path]"echo "Error: Exactly one or two arguments expected."exit 1
fi# Save the user-inputted source dir path to a variable
SOURCE_DIR="$1"# If a second argument (build directory) is provided, use it; otherwise, use the default
if [ "$#" -eq 2 ]; thenBUILD_DIR="$2"echo "$2"
elseBUILD_DIR="$DEFAULT_BUILD_DIR"echo "No build directory specified, using default: '${BUILD_DIR}'"
fi# Check if the source path is an existing directory
if [ ! -d "${SOURCE_DIR}" ]; thenecho "Error: SOURCE_DIR '${SOURCE_DIR}' does not exist."exit 1
elseecho "SOURCE_DIR is '${SOURCE_DIR}'"
fi# Optionally, you can also check if the BUILD_DIR exists or handle its absence according to your needs
if [ ! -d "${BUILD_DIR}" ]; thenecho "Warning: BUILD_DIR '${BUILD_DIR}' does not exist. Depending on your script's logic, this may or may not be a problem."
fi
############## Set the source directory and build directory
ROOT_DIR="$(pwd)"
CLANG_TIDY_CONFIG="${ROOT_DIR}/.clang-tidy"
COMPILE_COMMANDS="${BUILD_DIR}/compile_commands.json"# Check if compile_commands.json exists
if [ ! -f "${COMPILE_COMMANDS}" ]; thenecho "Error: compile_commands.json not found in ${BUILD_DIR}."exit 1
fi# Find all .cc files in the source directory recursively
ALL_STATIC_CHECK_FILES=$(find "${SOURCE_DIR}" -type f -name '*.cpp')filter_clang_tidy_output() {awk '/^clang-diagnostic-unused-command-line-argument/ {next}/^[0-9]+ warnings generated/ {next}/^Suppressed [0-9]+ warnings/ {next}/^Use -header-filter=.*$/ {next}/^Use -system-headers .*$/ {next}/\/usr\// ||/\/opt\// {skipping=1} # Start skipping upon a match with any of the specified patternsskipping == 1 && $0 ~ /\| * *\^/ {skipping=0; next} # Stop skipping when encountering the flexible patternskipping == 0 {print} # Print lines when not skipping'
}# Function to run clang-tidy
run_clang_tidy() {echo "Running clang-tidy..."for file in $ALL_STATIC_CHECK_FILES; doclang-tidy-18 "${file}" -p "${BUILD_DIR}" --warnings-as-errors=* \-config-file="${CLANG_TIDY_CONFIG}" -extra-arg=-std=c++14 \2>&1| filter_clang_tidy_output \|| touch "${BUILD_DIR}/clang_tidy_failed"done
}# Run clang-tidy
run_clang_tidy# Check the results
if [ -f "${BUILD_DIR}/clang_tidy_failed" ]; thenecho "Clang-tidy detected issues."exit 1
elseecho "No Clang-tidy issues found."
fi

1.1 流程图

合法
不合法
有指定构建目录
存在
不存在
存在
不存在
存在
不存在
有问题
无问题
开始
检查参数数量和合法性
保存源代码路径
显示错误并退出
设置默认构建目录
保存构建目录路径
显示默认构建目录
显示指定构建目录
检查源代码目录存在性
显示源代码目录路径
显示错误并退出
检查构建目录存在性
显示构建目录路径
显示警告
设置根目录和配置文件路径
检查compile_commands.json文件存在性
显示文件存在
显示错误并退出
查找所有.cpp文件
运行clang-tidy
检查clang-tidy结果
显示问题并退出
显示无问题
结束

1.2 脚本功能概述

这段脚本的主要功能包括:

  1. 检查输入参数的合法性,确保源代码目录路径正确,并根据需要指定构建目录。
  2. 检查是否存在编译命令文件 compile_commands.json,该文件是 clang-tidy 进行分析所必需的。
  3. 使用 find 命令递归查找源代码目录中的所有 .cpp 文件。
  4. 运行 clang-tidy 对每个找到的 .cpp 文件进行静态代码分析,输出详细的警告和建议。
  5. 过滤和处理 clang-tidy 的输出,以排除不必要的警告信息。
  6. 根据分析结果判断是否有代码问题,并相应地处理结果。

2. 该脚本优缺点

这段脚本的优点在于:

  • 自动化分析:有 compile_commands.json 文件即可进行分析,不用依赖CMakeLists.txt
  • 灵活处理: 可以根据分析结果,灵活地处理代码中的问题或警告,确保代码质量和稳定性。

然而,该脚本也存在一些潜在的缺点:

  • 依赖性问题: 脚本依赖于正确配置的 clang-tidycompile_commands.json 文件,因为不像CMake每次可以实时更新 compile_commands.json 文件,如果配置不正确可能导致分析失败。
  • 效率问题: 因为是依次遍历列表中的文件,对大型代码库进行全面的静态分析可能会消耗较多的时间和计算资源,影响效率。
http://www.lryc.cn/news/389485.html

相关文章:

  • PHP和phpSpider:如何应对网站变动导致的数据爬取失败?
  • 聊聊etsy平台,一个年入百万的项目
  • SyntaxError: Unexpected token ‘??=‘
  • python如何输出list
  • 【面试系列】SQL 高频面试题
  • 【代码随想录训练营】【Day 66】【图论-3】| 卡码 101-104
  • 【面试系列】C#高频面试题
  • AI助力校园安全:EasyCVR视频智能技术在校园欺凌中的应用
  • Yolov8可视化界面使用说明,含代码
  • 怎么使用MarkDown画矩阵
  • Kafka入门-基础概念及参数
  • Clickhouse 常见操作
  • Docker使用daocloud镜像加速
  • flink的窗口
  • lodash.js 工具库
  • 使用ElementUI组件库
  • 【SkiaSharp绘图14】SKCanvas方法详解(三)URL注释、按顶点绘制、 是否裁切区域之外、旋转、缩放、倾斜、平移、保存/恢复画布
  • WebDriver API (2)
  • GCP FrontendConfig 详解:优化您的云负载均衡
  • TensorFlow代码逻辑 vs PyTorch代码逻辑
  • boost asio异步服务器(4)处理粘包
  • 【QT】常用控件|widget|QPushButton|RadioButton|核心属性
  • 【C++ Primer Plus学习记录】函数参数和按值传递
  • MySQL:设计数据库与操作
  • OBS 免费的录屏软件
  • uniapp微信小程序使用xr加载模型
  • 机器人运动范围检测 c++
  • kettle从入门到精通 第七十四课 ETL之kettle kettle调用https接口教程,忽略SSL校验
  • C++轻量级 线程间异步消息架构(向曾经工作的ROSA-RB以及共事的DOPRA的老兄弟们致敬)
  • Kotlin中的类