測試代碼
先寫個簡單的代碼
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的,而開發(fā)機多是x86,clang默認參數(shù)也分析不了。
剛才分析結(jié)果第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
生成的結(jié)果是文本,查看起來還是比較費力的,目前有個簡單的轉(zhuǎn)換為html格式,
雖然也比較簡陋,但比文本還是方便多了。
審核編輯:劉清
-
ARM
+關(guān)注
關(guān)注
134文章
9027瀏覽量
366479 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1261瀏覽量
39838 -
gcc編譯器
+關(guān)注
關(guān)注
0文章
78瀏覽量
3346
原文標題:在RT-Thread的scons基礎(chǔ)上,使用clang-tidy做靜態(tài)分析
文章出處:【微信號:RTThread,微信公眾號:RTThread物聯(lián)網(wǎng)操作系統(tǒng)】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
評論