I met OCLint

OCLint is a static code analysis tool that improves quality and reduces defects by examining C, C++, and Objective-C code and looking for potential problems such as:

  • Possible pitfalls – Empty if/else/try/catch/finally statements
  • Unused code – Unused local variables and parameters
  • Complex code – high cyclomatic complexity,NPath complexity and high NCSS
  • Code smell – Long list of methods and arguments
  • Bad practice for long methods and argument lists – reverse logic and argument reallocation
  • … Static code analysis is a key technique for detecting defects that are not visible to compilation.

Note: * NPath complexity is the sum of all possible execution paths in a method; NCSS * Valid lines of code above translated from OCLint official website

The installation

The latest XCTool build method is deprecated and cannot be used with OCLint. Xcpretty is recommended

OCLint brew tap OCLint/Formulae brew install OCLint/xcpretty gem install xcprettyCopy the code

If you have not installed Homebrew, go to the official website to install it

update

Brew update brew Upgrade oclint may need to be updated laterCopy the code

use

Go to the project directory on the terminal, change the name of workspace and the name of scheme, paste the modified command to the terminal, and wait for the command execution to be completed

myworkspace=haha.xcworkspace # Replace the name workspace
myscheme=haha # Replace the name of scheme
xcodebuild -workspace $myworkspace -scheme $myscheme clean&&
xcodebuild -workspace $myworkspace -scheme $myscheme \
-configuration Debug \
| xcpretty -r json-compilation-database -o compile_commands.json&&
oclint-json-compilation-database -e Pods -- \
-report-type html -o oclint_result.html \
-rc LONG_LINE=200 \
-max-priority-1=100000 \
-max-priority-2=100000 \
-max-priority-3=100000; \
rm compile_commands.json;
if [ -f ./oclint_result.html ]; then echo '----- analysis completed -----'
else echo "----- analysis failed -----"; fi
Copy the code

There may be a wait after Build Succeeded because OCLint is waiting for command execution to complete in the analysis file. The terminal will print “—– analysis completed —–” and open the project directory. You will see an additional ** oclint_result.html ** file in the directory

If you do not use cocoapods, delete myworkspace=haha. Xcworkspace, -workspace $myworkspace, -workspace $myworkspace, and -workspace $myworkspace. Sh file, paste the above code into it, and run bash xx.sh in the xx.sh directory every time on the command line

To view

Double-click on the oclint_result. HTML file to open the OCLint analysis results in the following style: The Priority level is from Priority 1, Priority 2, Priority 3 Total Files Files with Violations Number of Files Compiler Warnings ⚠️ Compiler Errors Compiler Errors Location indicates the Location of the warning in the report is actually very clear, generally find the code Location, with the code understanding, you can directly copy the path to Chrome open view, right click to view the source code has the line number

Custom rules

-e debug. m -report-type HTML Indicates that the output file type after the analysis is HTML. View other supported file types -o oclint_result.html to output to oclint_result.html () -rc LONG_LINE=200 to specify a maximum length of 200 bytes per line (the default is 100, I feel like 100 is not enough in OC.

Comments on some common rules
# - named
Maximum number of bytes of variable name
#-rc=LONG_VARIABLE_NAME=20 \
The shortest byte of the variable name
#-disable-rule ShortVariableName \
# --size
# cyclomatic complexity
#-re=CYCLOMATIC_COMPLEXITY=10 \
# maximum number of lines per class
#-rc=LONG_CLASS=700 \
Number of bytes per line
#-rc=LONG_LINE=200 \
Number of lines per method
#-rc=LONG_METHOD=80 \
# ignore valid lines of code after parentheses after comments
#-rc=NCSS_METHOD=40 \
# nesting depth
#-rc=NESTED_BLOCK_DEPTH=5 \
# number of fields
#-rc=TOO_MANY_FIELDS=20 \
# number of methods
#-rc=TOO_MANY_METHODS=30 \
# method parameters
#-rc=TOO_MANY_PARAMETERS=6
Copy the code

Oclint – json – compilation – database command manual oclint command oclint custom rules introduced all rules introduced oclint document with each rule inside the demo and the corresponding command name, didn’t introduce to you can go to the above document query

Xcode integration OCLint

Here has the guidance website url, it is easy oclint – docs. Readthedocs. IO/en/stable/g… But there is a catch: the following error may occur

in `===’: invalid byte sequence in US-ASCII

At the beginning of your xcode script, add:

# specify code
export LANG="zh_CN.UTF-8"
export LC_COLLATE="zh_CN.UTF-8"
export LC_CTYPE="zh_CN.UTF-8"
export LC_MESSAGES="zh_CN.UTF-8"
export LC_MONETARY="zh_CN.UTF-8"
export LC_NUMERIC="zh_CN.UTF-8"
export LC_TIME="zh_CN.UTF-8"
export LC_ALL=
Copy the code

I’ve written a simple script file called oclint_xcode that you can download for yourself. The substitutable variables have been annotated in the script

Create an Aggregate of OCLint, place the Script in the project root directory, add the following code to the Run Script, and click Run

bash ./oclint.sh
Copy the code

Jenkins integration OCLint

1. Create onePMDOclint.shCopy the following code into Jenkins’ root directory (for example, mine)/Users/buildWork/.jenkins/PMDOclint.sh), my own scripts were put into the repositoryoclint_apply, you can download it from insideapply_jenkins/PMDOclint.sh
2. Jenkins needs to be installedPMD PluginThe plug-in
3. Jenkins creates a new project and configures Git into a code base that requires code analysis
4. Configure the rest according to the following figurePMDOclint.shPut it in Jenkins’ home directory. If there is a problem, please check the path involved.)

Effect of 5.


Warning ❗ ️

When xcodebuild fails, com + opt + shift + k and com + shift + k have an 80% chance of resolving 😂 (2). One Compiler Command contains multiple Jobs:

1. If there are two Xcodes installed in the computer, the above error may be reported. Delete the others and only one is left
After xcode9 (OCLint version 0.13, version 9.2 beta (9C34b) test available) :
2.1 Update to the latestoclint.brew upgrade oclint
2.2 Go to Github to download the latest versionoclint.shAnd replace the old one
3. Try manual project =>Build Settings= > searchCOMPILER_INDEX_STORE_ENABLEThis configuration is set to NO


Reference article:

Static analysis of iOS code using OCLint

OCLint rules and results analysis