OCLint特点
- 优点:有更多的检查规则和定制,和很多工具集成,接入持续集成相对简单
- 缺点: 使用复杂,检测速度慢,致命类问题检测较少
- 针对语言:针对C,C++和Objective-C
示例
官方
示例
xcodebuild -target DemoProject -configuration Debug -scheme DemoProject | xcpretty -r json-compilation-database -o compile_commands.json
oclint-json-compilation-database -e DemoProject -- -report-type html -rc LONG_LINE=200 -rc NCSS_METHOD=100 -o report.html
命令详解
USAGE: oclint [subcommand] [options] <source0> [... <sourceN>]
选项解释
-R=<directory>
# Add directory to rule loading path
# 添加目录到规则加载路径
-allow-duplicated-violations
# Allow duplicated violations in the OCLint report
# 允许在报告中重复出现违规行为
-disable-rule=<rule name>
# Disable rules
# 不允许的规则
-enable-clang-static-analyzer
# Enable Clang Static Analyzer, and integrate results into OCLint report
# 使用clang的静态分析并将结果添加到报告中
-enable-global-analysis
# Compile every source, and analyze across global contexts (depends on number of source files, could results in high memory load)
# 编译每个源文件,并跨全局上下文进行分析(取决于源文件的数量,可能导致高内存负载)
-extra-arg=<string>
# Additional argument to append to the compiler command line
# 编译器命令行后面添加的参数
-extra-arg-before=<string>
# Additional argument to prepend to the compiler command line
# 编译器命令行前面添加的参数
-list-enabled-rules
# List enabled rules
# 启用规则列表
-max-priority-1=<threshold>
# The max allowed number of priority 1 violations
# 优先级1的最大允许违规次数
-max-priority-2=<threshold>
# The max allowed number of priority 2 violations
# 优先级2的最大允许违规次数
-max-priority-3=<threshold>
# The max allowed number of priority 3 violations
# 优先级3的最大允许违规次数
-no-analytics
# Disable the anonymous analytics
# 禁用匿名分析
-o <path>
# Write output to <path>
# 输出报告路径
-p <string>
# Build path
# 构建路径
-rc <parameter>=<value>
# Override the default behavior of rules
# 重载默认规则
-report-type <name>
# Change output report type
# 改变输出报告类型
-rule <rule name>
# Explicitly pick rules
# 选择规则
-p <build-path>
# is used to read a compile command database.
# 用于读取编译命令数据库
<source0> ...
# specify the paths of source files.
# 指定源文件的路径
规则
Basic
BitwiseOperatorInConditional
# 禁用条件语句中的位操作符
BrokenNullCheck
# 检查条件自身crash
BrokenNilCheck
# 检查条件,出现相反结果
BrokenOddnessCheck
# 奇偶数检查(x % 2 == 1检查奇度对负数无效)
CollapsibleIfStatements
# 连续if语句可以合并
ConstantConditionalOperator
# 常数条件运算符
ConstantIfExpression
# 常数if条件
DeadCode
# 无用代码
DoubleNegative
# 双重否定
ForLoopShouldBeWhileLoop
# 某些for循环,应该使用while循环
GotoStatement
# goto语句使用
JumbledIncrementer
# 增量词混乱
MisplacedNullCheck
# Null检查位置错误
MisplacedNilCheck
# Nil检查位置错误
MultipleUnaryOperator
# 多个一元运算符
ReturnFromFinallyBlock finally
# 块中包含return
ThrowExceptionFromFinallyBlock
# finaly块中抛出异常
Cocoa
MissingHashMethod
# 实现isEqual方法,必须要实现hash方法
MissingCallToBaseMethod
# 当一个方法用attribute注释,必须调用父类方法
CallingProhibitedMethod
# 当一个方法用attribute注释声明时,它的所有用法都将被禁止。
CallingProtectedMethod attribute
# 修饰的方法,在外部调用时,有提示。
MissingAbstractMethodImplementation
# 抽象方法,子类必须实现
Convention
AvoidBranchingStatementAsLastInLoop
# 避免break这种语句在循环的最后面
ProblematicBaseClassDestructor
# 积累的析构函数应该是** virtual or protected**
UnnecessaryDefaultStatement
# switch中不需要的default语句
MisplacedDefaultLabel
# switch中default放置位置错误
DestructorOfVirtualClass
# 虚拟类的析构函数必须是虚拟的。
InvertedLogic
# 反向逻辑
MissingBreakInSwitchStatement
# switch语句没有break
NonCaseLabelInSwitchStatement
# switch中条件有非case条件
AssignIvarOutsideAccessors
# 禁止在getter、setter和init方法之外分配synthesize ivar
ParameterReassignment
# 禁止将值重新分配给参数,容易出问题
PreferEarlyExit
# 更早return比较好。
MissingDefaultStatement
# switch应该有default
TooFewBranchesInSwitchStatement
# switch分支较少时,应该用if代替。
Design
AvoidDefaultArgumentsOnVirtualMethods
# 避免虚拟函数中添加默认参数
AvoidPrivateStaticMembers
# 避免私有静态变量 ,破坏封装性
Empty
EmptyCatchStatement
# 空的catch
EmptyDoWhileStatement
# 空的Do while中的while
EmptyElseBlock
# 空的else
EmptyFinallyStatement
# 空的finally
EmptyForStatement
# 空的for
EmptyIfStatement
# 空的if
EmptySwitchStatement
# 空的switch
EmptyTryStatement
# 空的try
EmptyWhileStatement
# 空的while
Igration
UseBoxedExpression
# 使用装箱表达,可以用字面量语法
UseContainerLiteral
# 使用容器的literal语法
UseNumberLiteral
# 使用number的literal语法
UseObjectSubscripting
# 使用下标访问对象
Naming
LongVariableName
# 太长的变量名(20)
ShortVariableName
# 太短的变量名(3)
Redundant
RedundantConditionalOperator
# 冗余条件
RedundantIfStatement
# 冗余if条件
RedundantLocalVariable
# 冗余本地变量
RedundantNilCheck
# 冗余nil检查
UnnecessaryElseStatement
# 冗余else条件
UnnecessaryNullCheckForDealloc
# 冗余的dealloc,nil 检查
UselessParentheses
# 使用更少的括号
Size
HighCyclomaticComplexity
# 圈复杂度,通过决策点数量判断(10)
LongClass
# 长的类(1000)
LongLine
# 长的字符串(100)
LongMethod
# 过长的方法(50)
HighNcssMethod
# 过长的没注释方法(100)
DeepNestedBlock
# 嵌套过多(5)
HighNPathComplexity
# npath复杂度(200)
TooManyFields
# 过多字段变量(20)
TooManyMethods
# 过多方法(30)
TooManyParameters
# 过多参数(10)
Unused
UnusedLocalVariable
# 没有使用的本地变量
UnusedMethodParameter
# 没有使用的方法参数
可用阈值
名称 | 描述 | 默认值 |
---|---|---|
CYCLOMATIC_COMPLEXITY | 方法的圈复杂度 | 10 |
LONG_CLASS | C类或Objective-C接口的行数,类别,协议和实现 | 1000 |
LONG_LINE | 一行代码的字符数 | 100 |
LONG_METHOD | 方法或函数的行数 | 50 |
LONG_VARIABLE_NAME | 变量名称的字符数 | 20 |
MAXIMUM_IF_LENGTH | Number of lines for the if block that would prefer an early exists | 15 |
MINIMUM_CASES_IN_SWITCH | switch语句中case语句的计数 | 3 |
NPATH_COMPLEXITY | NPath方法的复杂性 | 200 |
NCSS_METHOD | 方法的非注释源语句数 | 30 |
NESTED_BLOCK_DEPTH | 块或复合语句的深度 | 5 |
SHORT_VARIABLE_NAME | 变量名称的字符数 | 3 |
TOO_MANY_FIELDS | 一类的字段数量 | 20 |
TOO_MANY_METHODS | 一类方法的数量 | 30 |
TOO_MANY_PARAMETERS | 方法的参数数量 | 10 |
OCLint使用感悟
OCLint检查的规则非常多,也可以定制检查规则,但是OCLint扫描的问题中,更多的是偏向于代码规范,可读性等代码写的好不好的问题,而不是对不对,比如:方法行数、类文件函数,变量命名等,对于代码中存在的致命bug的检查,效果不是很好,所以想要扫描出一些致命问题,需要自己定制检查规则,使用成本较高,infer相对于OCLint,在致命问题扫描中,比较有优势,所以可以根据项目现状,可以挑选具体使用的工具。