1. 程式人生 > >OCLint靜態語法分析工具(安裝使用步驟)

OCLint靜態語法分析工具(安裝使用步驟)

Brief Introduction:

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


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).

  1. cp bin/oclint* /usr/local/bin/
  2. 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.