使用cpplint.py 进行批量静态代码分析

本文使用python编写脚本,读取.ini配置文件中源码路径,对C++源码进行代码规范静态分析,并生成分析结果报告。使用谷歌的开源工具cpplint.py ,所检测代码符合谷歌代码规范。

cpplint.py默认支持的文件格式包含:‘c’, ‘cc’, ‘cpp’, ‘cxx’, ‘c++’, 'cu’及其头文件,默认不支持.hpp文件。

python代码最好使用notepad编写,视图——显示符号——显示空格和制表符,注意缩进使用空格键
一、在编写脚本前先熟悉一下python-读取配置文件

1 基本的读取操作:

 -read(filename)              直接读取文件内容
 -sections()                  得到所有的section,并以列表的形式返回
 -options(section)            得到该section的所有option
 -items(section)              得到该section的所有键值对
 -get(section,option)         得到section中option的值,返回为string类型
 -getint(section,option)      得到section中option的值,返回为int类型,还有相应的getboolean()getfloat() 函数

2 基本的写入操作:

 -write(fp)  将config对象写入至某个 .init 格式的文件  Write an .ini-format representation of the configuration state.
 -add_section(section)   添加一个新的section
 -set( section, option, value   对section中的option进行设置,需要调用write将内容写入配置文件
 -remove_section(section)  删除某个 section
 -remove_option(section, option) 

二、然后开始编写脚本

  1. 批处理python文件
import configparser
import os

def get_file(root_path, all_files):   
    files = os.listdir(root_path)    
    for file in files:
        if not os.path.isdir(root_path + '/' + file):   # not a dir
            all_files.append(root_path + '/' + file)            
        else:  # is a dir
            get_file((root_path+'/'+file),all_files)        
    return all_files

cf = configparser.ConfigParser()
cf.read("src_path.ini")  # 读取配置文件,如果写文件的绝对路径,就可以不用os模块
for item in cf.options('srcpath'):    
    rootdir = cf.get('srcpath', item)
    list = os.listdir(rootdir) #列出文件夹下所有的目录与文件    
    print("len(list)", len(list),rootdir)
    paths = get_file(rootdir,[]);
    for path in paths:
        str = "python cpplint.py %(path)s --counting=toplevel --output=vs7 >> %(item)s 2>&1"%{'path':path, 'item':item}       
        print("==========path==========", path)
        os.system(str)
print("------end--------")
  1. src_path.ini配置文件
[srcpath]
report1.txt=D:\code_standard\code_standard1  
report2.txt=D:\code_standard\code_standard2
report3.txt=D:\code_standard\code_standard3
report4.txt=D:\code_standard\code_standard4
  1. 说明
report1.txt 是静态分析产生的结果报告名称
D:\code_standard\code_standard1 是代码路径
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
如果你的C++代码是通过Python脚本进行编译的,那么可以按照以下步骤使用Infer进行静态代码分析: 1. 确保你的C++代码能够被Python脚本正确编译: 在终端中进入到Python脚本所在目录,执行以下命令进行编译: ``` $ python compile.py ``` 如果编译成功,会在当前目录下生成相应的库文件(例如,`.so`文件或`.a`文件)。 2. 生成编译命令: 使用Infer分析C++代码需要生成编译命令。由于你的C++代码是通过Python脚本进行编译的,因此需要先生成Python脚本的编译命令,然后将其传递给Infer进行分析。 可以使用以下命令生成Python脚本的编译命令: ``` $ python compile.py --infer ``` 这样会输出Python脚本的编译命令,例如: ``` g++ -c -o foo.o -I./include foo.cpp g++ -c -o bar.o -I./include bar.cpp ar rcs libfoo.a foo.o bar.o ``` 将这些命令保存到一个文件中,以便下一步使用。 3. 使用Infer进行静态代码分析: 在终端中进入到C++代码所在目录,执行以下命令: ``` $ infer run --compilation-database /path/to/compile_commands.json ``` 其中,`/path/to/compile_commands.json`是上一步生成的编译命令文件。通过这个命令,Infer会对C++代码进行静态分析,并输出分析结果。 需要注意的是,`--compilation-database`参数用于指定编译命令文件,这个文件需要符合JSON Compilation Database格式。如果你使用的是Makefile进行编译,可以使用`infer -- make`命令进行分析,无需生成编译命令文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水笙赵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值