TscanCode代码扫描工具

TscanCode是由腾讯研发的静态代码扫描工具,支持C++、C#、Lua等多种语言,能有效检测空指针、越界等问题。适用于游戏开发代码扫描,具有较高的准确率和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TscanCode介绍

TscanCode 是腾讯研发的静态代码扫描工具,最早的版本是基于 cppcheck 二次开发。之后又重新自研,不仅支持 C++,还支持 C#,Lua 语言,在发掘 C++ 空指针、越界、未初始化、C#空引用、Lua变量未初始化等比较有效。TScanCode 比较适用于游戏开发代码扫描,有着不错的准确率和效率,其性能测试可以见:https://blog.csdn.net/wetest_tencent/article/details/51516347
TscanCode 主要能够发现的问题如下:
1、自动变量检查: 返回自动变量(局部变量)指针;
2、越界检查:数组越界返回自动变量(局部变量)指针;
3、类检查:构造函数初始化;
4、内存泄露检查;
5、空指针检查;
6、废弃函数检查;

下载TscanCode

TscanCode 已经在 Github 上开源,地址是:https://github.com/Tencent/TscanCode
其中项目文件夹对应的如下:

release	->编译后的二进制文件,分别有Linux、Mac、Windows平台
samples	->测试的代码样例,分别有C++、C#、Lua语言
trunk		->TscanCode源代码

为了方便起见,下载对应平台的二进制可执行文件便可以运行,其中 Win 平台是有 GUI 图形界面,Linux 中要使用命令和手动配置规则,下面介绍一下在 Win 和 Linux 平台使用 TscanCode。

Win下使用TscanCode

安装 TscanCode 后,双击打开后,主界面上选择:扫描文件夹,然后打开代码的文件夹,点击:开始扫描。
win_tscancode_1
扫描完成后会出现结果:
win_tscancode_2
双击可以查看代码中存在的错误,比如:
win_tscancode_3
对于扫描后的结果,可以保存为 xml 配置文件,方便下一次直接在主界上直接打开:结果查看,选中该文件即可。
还可以在设置中选择扫描规则,每个规则都有对应的代码实例可供参考。
win_tscancode_4

Linux下使用TscanCode

我们将 Linux 版本二进制压缩包解压,然后进入 TscanCodeV2.14.2395.linux 目录,有一个 cfg 文件夹和一个 tscancode 二进制文件,需要使用 chmod +x tscancode 对其加上可执行权限。
在 Linux 下可通过 cfg/cfg.xml 对扫描的规则进行配置,其中通过设置 value=0 则禁用,value=1 则启用,或直接使用默认的扫描规则。
需要注意的是:扫描的路径中不能包含 root 文件夹,TscanCode特殊性。
然后再执行如下命令对 samples/cpp/下的所有 C++ 文件代码进行扫描:

./tscancode --xml --enable=all -q /home/crazyang/samples/cpp/ >scan_result.xml 2>&1

执行后,就会看到在该目录下生成了 scan_result.xml 文件,文件中就是扫描的结果,如果我们需要扫描结果重定向到文件中,最好加上 -q 参数,否则会将扫描的进度信息也重定向到文件中,这些信息仅在终端中显示进度有用。
注意:如果不熟悉后面的参数,可以使用 ./tscancode -h 查看帮助文档。

$ ./tscancode -h
TscanCode - A tool for static C/C++ code analysis

Syntax:
    tscancode [OPTIONS] [files or paths]

If a directory is given instead of a filename, *.cpp, *.cxx, *.cc, *.c++, *.c,
*.tpp, and *.txx files are checked recursively from the given directory.

Options:
    -D<ID>               Define preprocessor symbol. Unless --max-configs or
                         --force is used, TscanCode will only check the given
                         configuration when -D is used.
                         Example: '-DDEBUG=1 -D__cplusplus'.
    -U<ID>               Undefine preprocessor symbol. Use -U to explicitly
                         hide certain #ifdef <ID> code paths from checking.
                         Example: '-UDEBUG'
    --enable=<id>        Enable additional checks. The available ids are:
                          * all
                                  Enable all checks. It is recommended to only
                                  use --enable=all when the whole program is
                                  scanned, because this enables unusedFunction.
                          * warning
                                  Enable warning messages
                          * style
                                  Enable all coding style checks. All messages
                                  with the severities 'style', 'performance' and
                                  'portability' are enabled.
                          * performance
                                  Enable performance messages
                          * portability
                                  Enable portability messages
                          * information
                                  Enable information messages
                          * unusedFunction
                                  Check for unused functions. It is recommend
                                  to only enable this when the whole program is
                                  scanned.
                          * missingInclude
                                  Warn if there are missing includes. For
                                  detailed information, use '--check-config'.
                         Several ids can be given if you separate them with
                         commas. See also --std
    -h, --help           Print this help.
    -I <dir>             Give path to search for include files. Give several -I
                         parameters to give several paths. First given path is
                         searched for contained header files first. If paths are
                         relative to source files, this is not needed.
    -j <jobs>            Start [jobs] threads to do the checking simultaneously.
    -q, --quiet          Do not show progress reports.
    --xml                Write results in xml format to error stream (stderr).

Example usage:
  # Recursively check the current folder. Print the progress on the screen and
  # write errors to a file:
  tscancode . 2> err.txt

  # Recursively check ../myproject/ and don't print progress:
  tscancode --quiet ../myproject/

  # Check test.cpp, enable all checks:
  tscancode --enable=all test.cpp

  # Check f.cpp and search include files from inc1/ and inc2/:
  tscancode -I inc1/ -I inc2/ f.cpp

最后得到的 scan_result.xml 结果文件,可以下载下来使用 Excel 工具打开 XML 报告(为了处理更直观),在左侧插入一列处理情况。
开发人员根据报告对代码上下文进行分析,判断是否为工具误报。
对于确认为问题的代码,由开发人员处理后重新进行代码安全静态扫描,直到问题关闭。

参考:
https://github.com/Tencent/TscanCode
静态代码检查
C++代码质量扫描主流工具深度比较
代码扫描工具TScanCode

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

code_peak

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

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

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

打赏作者

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

抵扣说明:

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

余额充值