使用OClint進行iOS項目的靜態代碼掃描


使用OClint進行iOS項目的靜態代碼掃描 

原文鏈接:http://blog.yourtion.com/static-code-analysis-ios-using-oclint.html


最近需要一個靜態分析代碼工具,幫助我們發布運行應用前找到代碼潛在的問題。

其實對於iOS開發,我們的日常開發上已經用到了這樣一個靜態分析的工具,那就是 Clang, Clang 是支持CC++Objective-C 和 Swift 的一個前端編譯工具,他將 OC 或者 Swift 的代碼輸出抽象語法樹(Abstract Syntax Tree),然后編譯成 LLVM 的 bitcode,最后由 LLVM 編譯成 machine code。這個工具支撐着我們日常的開發和調試。

OCLint 就是一個建立在 Clang 上的工具,能夠發現代碼中潛在的問題。具體的功能請見官方文檔,這里主要介紹的是其安裝與使用。

安裝軟件

XCtool

brew install xctools 

OClint

brew tap oclint/formulae brew install oclint 

測試

進入項目目錄(以 SuperLogger 為例),

下載項目切換到有問題的位置(),並進入 SuperLoggerDemo 目錄:

git clone https://github.com/yourtion/SuperLogger.git cd SuperLogger git checkout 0e64637459996ed91e0dd15718efb5d7200a9971 cd SuperLoggerDemo 

測試執行:

# Cleanup before building rm -f compile_commands.json xctool -project SuperLoggerDemo.xcodeproj -scheme SuperLoggerDemo clean # Build Project xctool build \ -project SuperLoggerDemo.xcodeproj -scheme SuperLoggerDemo \ -reporter json-compilation-database:compile_commands.json # Analyze Project oclint-json-compilation-database -e Pods -- \ -max-priority-1=100000 \ -max-priority-2=100000 -max-priority-3=100000 \ -disable-rule=InvertedLogic \ -disable-rule=CollapsibleIfStatements \ -disable-rule=UnusedMethodParameter \ -disable-rule=LongLine \ -disable-rule=LongVariableName \ -disable-rule=ShortVariableName \ -disable-rule=UselessParentheses \ -disable-rule=IvarAssignmentOutsideAccessorsOrInit | sed 's/\(.*\.\m\{1,2\}:[0-9]*:[0-9]*:\)/\1 warning:/' # Final cleanup rm -f compile_commands.json 

輸出

OCLint Report

Summary: TotalFiles=14 FilesWithViolations=4 P1=0 P2=2 P3=6

SuperLoggerPreviewView.m:77:37: warning: replace with container literal [migration|P3] SuperLogerListView.m:206:37: warning: empty catch statement [empty|P2] SuperLogerListView.m:25:15: warning: empty if statement [empty|P2] SuperLogerListView.m:119:1: warning: long method [size|P3] Method with 92 lines exceeds limit of 50 SuperLogerListView.m:171:41: warning: replace with container literal [migration|P3] SuperLogerListView.m:110:21: warning: replace with object subscripting [migration|P3] SuperLogger.m:60:30: warning: replace with object subscripting [migration|P3] SuperLogger.m:108:31: warning: replace with object subscripting [migration|P3] [OCLint (http://oclint.org) v0.10.2] 

具體問題可以參考:http://docs.oclint.org/en/stable/rules/index.html

修復后

OCLint Report Summary: TotalFiles=14 FilesWithViolations=0 P1=0 P2=0 P3=0 [OCLint (http://oclint.org) v0.10.2] 

大概的功能就是這樣,使用過程有什么問題歡迎大家一起交流

原文鏈接:http://blog.yourtion.com/static-code-analysis-ios-using-oclint.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM