OCLint使用

image.png

OCLint的分析結(jié)果:
優(yōu)先級(jí)的級(jí)別是從Priority 1, Priority 2, Priority 3 依次降低的
Total Files 總文件數(shù)
Files with Violations 違規(guī)文件數(shù)
Compiler Warnings 表示項(xiàng)目中的警告??
Compiler Errors 表示編譯錯(cuò)誤
Location 表示警告的位置
報(bào)告中的描述其實(shí)非常清晰,一般找到代碼位置,結(jié)合代碼理解

一、OCLint 安裝
使用Homebrew 安裝:在安裝前,確保安裝了 homebrew。

$ brew tap oclint/formulae
$ brew install oclint

安裝完以后驗(yàn)證下 OCLint 是否安裝成功。輸入
$ oclint --version

二、xcpretty 的安裝
在安裝前,確保安裝了 Ruby gem.
$gem install xcpretty

三、 使用 oclint
1.如果項(xiàng)目使用了 Cocopod,則需要指定 -workspace xxx.workspace
2.每次編譯之前需要 clean
3.cd 進(jìn)入項(xiàng)目
4.查看項(xiàng)目基本信息
$xcodebuild -list

sikong@localhost OCLintDemo % xcodebuild -list 
Command line invocation:
    /Applications/Xcode.app/Contents/Developer/usr/bin/xcodebuild -list

Information about project "OCLintDemo":
    Targets:
        OCLintDemo
        OCLintDemoTests

    Build Configurations:
        Debug
        Release

    If no build configuration is specified and -scheme is not passed then "Release" is used.

    Schemes:
        OCLintDemo

sikong@localhost OCLintDemo % 

5.編譯.編譯成功后,會(huì)在項(xiàng)目的文件夾下出現(xiàn) compile_commands.json 文件

$xcodebuild -scheme OCLintDemo -workspace OCLintDemo.xcworkspace clean && xcodebuild -scheme OCLintDemo -workspace OCLintDemo.xcworkspace -configuration Debug | xcpretty -r json-compilation-database -o compile_commands.json

6.生成 html 報(bào)表
$ oclint-json-compilation-database -e Pods -- -report-type html -o oclintReport.html

看到有報(bào)錯(cuò),但是報(bào)錯(cuò)信息太多了,不好定位,利用下面的腳本則可以將報(bào)錯(cuò)信息寫入 log 文件,方便查看
oclint-json-compilation-database -e Pods -- -report-type html -o oclintReport.html 2>&1 | tee 1.log

報(bào)錯(cuò)信息:oclint: error: one compiler command contains multiple jobs:的解決方案如下:

ProjectTargetsBuilding Settings下的 COMPILER_INDEX_STORE_ENABLE 設(shè)置為NO
podfiletarget 'xx' do前面添加下面的腳本:

post_install do |installer|
  installer.pods_project.targets.each do |target|
      target.build_configurations.each do |config|
          config.build_settings['COMPILER_INDEX_STORE_ENABLE'] = "NO"
      end
  end
end

報(bào)錯(cuò)信息:oclint: error: violations exceed threshold: P1=0[0] P2=323[10] P3=4523[20]的解決方案如下:
看到報(bào)錯(cuò)信息是默認(rèn)的警告數(shù)量超過(guò)限制,則 lint 失敗。事實(shí)上 lint 后可以跟參數(shù),所以我們修改腳本如下:
oclint-json-compilation-database -e Pods -- -report-type html -o oclintReport.html -rc LONG_LINE=300 -max-priority-1=9999 -max-priority-2=9999 -max-priority-3=9999

四、oclint規(guī)則


image

規(guī)則默認(rèn)值:

名稱 描述 默認(rèn)閾值
CYCLOMATIC_COMPLEXITY 方法的循環(huán)復(fù)雜性(圈負(fù)責(zé)度) 10
LONG_CLASS C類或Objective-C接口,類別,協(xié)議和實(shí)現(xiàn)的行數(shù) 1000
LONG_LINE 一行代碼的字符數(shù) 100
LONG_METHOD 方法或函數(shù)的行數(shù) 50
LONG_VARIABLE_NAME 變量名稱的字符數(shù) 20
MAXIMUM_IF_LENGTH if語(yǔ)句的行數(shù) 15
MINIMUM_CASES_IN_SWITCH switch語(yǔ)句中的case數(shù) 3
NPATH_COMPLEXITY 方法的NPath復(fù)雜性 200
NCSS_METHOD 一個(gè)沒(méi)有注釋的方法語(yǔ)句數(shù) 30
NESTED_BLOCK_DEPTH 塊或復(fù)合語(yǔ)句的深度 5
SHORT_VARIABLE_NAME 變量名稱的字符數(shù) 3
TOO_MANY_FIELDS 類的字段數(shù) 20
TOO_MANY_METHODS 類的方法數(shù) 30
TOO_MANY_PARAMETERS 方法的參數(shù)數(shù) 10

根據(jù)實(shí)際需要修改部分檢測(cè)規(guī)則:
oclint-json-compilation-database -e Pods -e 3rd -- -rc=LONG_LINE=300 -rc=LONG_METHOD=200 -rc=TOO_MANY_METHODS=100 -rc=CYCLOMATIC_COMPLEXITY=50 -rc=NCSS_METHOD=120 -disable-rule ShortVariableName -disable-rule ObjCAssignIvarOutsideAccessors -disable-rule AssignIvarOutsideAccessors -max-priority-1=100000 -max-priority-2=100000 -max-priority-3=100000 -report-type html -o oclintReport.html

參考:https://juejin.cn/post/6844903853775650830#heading-6
http://docs.oclint.org/en/stable/howto/rcfile.html
http://docs.oclint.org/en/stable/rules/index.html
http://www.itdecent.cn/p/35cf99c07eaa

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前言 OCLint是基于 Clang Tooling 開(kāi)發(fā)的靜態(tài)分析工具,主要用來(lái)發(fā)現(xiàn)編譯器檢查不到的那些潛在的關(guān)...
    張聰_2048閱讀 5,927評(píng)論 3 20
  • 前言 oclint 是對(duì)C、C++、Objective-C進(jìn)行靜態(tài)代碼檢查的工具 ,使用 oclint 對(duì)代碼進(jìn)行...
    麥兜兜二寶閱讀 2,239評(píng)論 0 5
  • 前言 OCLint 是一個(gè)靜態(tài)分析代碼的工具,支持自定義規(guī)則,可以很好地幫助我們規(guī)范代碼,這篇文章主要介紹通過(guò)OC...
    alanwangmodify閱讀 4,418評(píng)論 3 53
  • OCLint OCLint 是基于 Clang 的靜態(tài)分析工具,支持對(duì) C、C++ 和 Objective-C 代...
    Demonboy閱讀 3,843評(píng)論 0 1
  • 一.簡(jiǎn)介 OCLint是基于 Clang Tooling 開(kāi)發(fā)的靜態(tài)分析工具,主要用來(lái)發(fā)現(xiàn)編譯器檢查不到的那些潛在...
    Money_YC閱讀 11,079評(píng)論 1 10

友情鏈接更多精彩內(nèi)容