测试代码
先写个简单的代码
main.c
1#include2#include 3#include 4intmain(intargc,char*argv[]) 5{ 6char*tmp=(char*)malloc(argc); 7if(tmp) 8{ 9strncpy(tmp,argv[0],argc); 10printf("%s",tmp); 11if(argc>2) 12{ 13free(tmp); 14} 15} 16printf("helloworld! "); 17return0; 18}
使用clang-tidy分析一下
1#aptinstall-yclang-tidy 2#clang-tidymain.c 3Errorwhiletryingtoloadacompilationdatabase: 4Couldnotauto-detectcompilationdatabaseforfile"main.c" 5Nocompilationdatabasefoundin/work/analyzeroranyparentdirectory 6fixed-compilation-database:Errorwhileopeningfixeddatabase:Nosuchfileordirectory 7json-compilation-database:ErrorwhileopeningJSONdatabase:Nosuchfileordirectory 8Runningwithoutflags. 92warningsgenerated. 10/work/analyzer/main.c:12:3:warning:Calltofunction'strncpy'isinsecureasitdoesnotprovidesecuritychecksintroducedintheC11standard.Replacewithanalogousfunctionsthatsupportlengthargumentsorprovidesboundarycheckssuchas'strncpy_s'incaseofC11[clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling] 11strncpy(tmp,argv[0],argc); 12^~~~~~~ 13/work/analyzer/main.c:12:3:note:Calltofunction'strncpy'isinsecureasitdoesnotprovidesecuritychecksintroducedintheC11standard.Replacewithanalogousfunctionsthatsupportlengthargumentsorprovidesboundarycheckssuchas'strncpy_s'incaseofC11 14strncpy(tmp,argv[0],argc); 15^~~~~~~ 16/work/analyzer/main.c:21:2:warning:Potentialleakofmemorypointedtoby'tmp'[clang-analyzer-unix.Malloc] 17printf("helloworld! "); 18^ 19/work/analyzer/main.c:8:22:note:Memoryisallocated 20char*tmp=(char*)malloc(argc); 21^~~~~~~~~~~~ 22/work/analyzer/main.c:10:5:note:Assuming'tmp'isnon-null 23if(tmp) 24^~~ 25/work/analyzer/main.c2:note:Takingtruebranch 26if(tmp) 27^ 28/work/analyzer/main.c6:note:Assuming'argc'is<= 2 29 if(argc >2) 30^~~~~~~~ 31/work/analyzer/main.c:15:3:note:Takingfalsebranch 32if(argc>2) 33^ 34/work/analyzer/main.c2:note:Potentialleakofmemorypointedtoby'tmp' 35printf("helloworld! "); 36^
compile_commands.json
还是有点用的,但我们RT-Thread项目中代码那么多,一个一个输入太麻烦了。
而且我们代码多是arm和gcc的,而开发机多是x86,clang默认参数也分析不了。
刚才分析结果第1行就有提示,没有找到compilation database,其实scons就可以生成
需要比较新的版本
1python3-mpipinstallscons 2python3-mSCons-v 3SConsbyStevenKnightetal.: 4SCons:v4.0.1.c289977f8b34786ab6c334311e232886da7e8df1,2020-07-1701:50:03,bybdbaddogonProDog2020 5SConspath:['/usr/lib/python3/dist-packages/SCons'] 6Copyright(c)2001-2020TheSConsFoundation
然后更新下Scons脚本,让生成compile_commands.json
1#gitdiffSConstruct 2+env.Tool('compilation_db') 3+env.CompilationDatabase() 4#makeabuilding 5DoBuilding(TARGET,objs)
run-clang-tidy
然后使用 run-clang-tidy 就可以自动分析所有的源代码了。
输出html
生成的结果是文本,查看起来还是比较费力的,目前有个简单的转换为html格式,
虽然也比较简陋,但比文本还是方便多了。
审核编辑:刘清
-
ARM
+关注
关注
134文章
9034浏览量
366597 -
RT-Thread
+关注
关注
31文章
1266浏览量
39868 -
gcc编译器
+关注
关注
0文章
78浏览量
3349
原文标题:在RT-Thread的scons基础上,使用clang-tidy做静态分析
文章出处:【微信号:RTThread,微信公众号:RTThread物联网操作系统】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录
相关推荐
评论