Brief Introduction:
homepage
OCLint is a static code analysis tool for improving quality and reducing defects by inspecting C, C++ and Objective-C code and looking for potential problems like:
- Possible bugs - empty if/else/try/catch/finally statements
- Unused code - unused local variables and parameters
- Complicated code - high cyclomatic complexity, NPath complexity and high NCSS
- Redundant code - redundant if statement and useless parentheses
- Code smells - long method and long parameter list
- Bad practices - inverted logic and parameter reassignment
Get OCLint
You can get the latest version by building from source code or downloading pre-compiled binaries.
Installation¶
Both pre-compiled binary distribution and local build bundle should end up with an OCLint release with which file tree similar to this:
oclint-release
|-bin
|-lib
|---clang
|-----3.4
|-------include
|-------lib
|---oclint
|-----rules
|-----reporters
Even without installation, oclint is able to be invoked directly from bin directory now.
In order to ease the invocation, it’s recommended to add OCLint’s bin folder to system PATH, the environment variable that tells system which directories to search for executable files.
(即使没有安装,oclint能够直接从bin目录调用。
为了缓解调用,它建议添加OCLint的bin文件夹到系统路径,环境变量,告诉系统目录来搜索可执行文件。)
Option 1: Directly Adding to PATH¶
Following code snippet is an example for the .bashrc or .bash_profile file that is sourced when terminal launches.
OCLINT_HOME=/path/to/oclint-release
export PATH=$OCLINT_HOME/bin:$PATH
Option 2: Copying OCLint to System PATH¶
A few directories are supposed to be in the system PATH already, to mention a few, /usr/local/bin, /usr/bin, /bin, etc. Therefore, it’s also possible to copy the OCLint binaries into one of these folders, and move the dependencies over. As an example, presumes /usr/local/bin is in the PATH (may require root permission).
- cp bin/oclint* /usr/local/bin/
- cp -rp lib/* /usr/local/lib/
Dependency libraries are required to be put into appropriate directory, because oclint executable searches $(/path/to/bin/oclint)/../lib/clang,$(/path/to/bin/oclint)/../lib/oclint/rules and $(/path/to/bin/oclint)/../lib/oclint/reporters for builtin headers and dynamic libraries by default.
Verifying Installation¶
Open a new terminal prompt, and execute oclint directly from there and expect message similar to below:
$ oclint
oclint: Not enough positional command line arguments specified!
Must specify at least 1 positional arguments: See: oclint -help
That’s it – if OCLint is pretty new to you, tutorial would lead you by applying the tool to a sample code, and explaining a few concepts along the way.
This is the documentation for the development version of OCLint, see the Releases page for released documentations.
Using OCLint in Xcode
This document shows one solution of using OCLint to analyze the code quality of a Xcode project.
Prerequisite¶
- oclint-xcodebuild Manual
- Apple’s official xcodebuild Manual Page
- Using OCLint with xcodebuild
or
- Using OCLint with xctool
Background¶
This idea was originally posted in this blog. We hope to share it with more developers, and hope to motivate more ideas.
Setting up Target¶
- Add a new target in the project, and choose Aggregate as the template.
- Name the new target, here we simply call it “OCLint”, you could have more than one targets that focus on different aspects of the code analysis.
- Add a new build phase in the target we just created. Choose And Run Script for the phase type.
- In the script editor, we could enter the script which does the real work. We can also modify the script from this very generic version. We may need to change the xcodebuild options to use a particular scheme or target. In addition, based on the discussions we had, we can decide whether to use clean and dry run features.
- For xctool users, the script can be largely simplified to something like this
Running Analysis¶
- Choose the correct build scheme, here we choose OCLint.
- Click to build, or use the shortcut Command+B.
- When the progress bar scrolls to the very right, the analysis is done, then we can check out the analysis results same as compile warnings.