【編程基礎】CppLint工具使用過程


前言

coding最好要形成一定的編程風格,一般常用的開源風格有google code style,可以使用cpplint工具檢查是否符合該編程風格。

目錄

1. linux系統使用過程;

2. windows系統使用過程;

注意,目前cpplint.py是基於python2的腳本,若要使用python3請按照python3改寫cpplint.py。

操作

1. linux系統使用過程

1.1 安裝cpplint;

$pip install cpplint

1.2 保存cpplint.py文件;

1.3 檢測單個文件;

cpplint_dir$python2 cpplint.py test.cpp

1.4 檢測批量文件;

根據要檢測的文件編寫shell腳本,然后運行即可。

注意,sh和bat適應於不同的系統,bat文件是window系統的執行文件,sh是linux系統的批處理文件。

cpplint_shell.sh實例;

#! /bin/bash
echo "^@^cpplint code style check through shell====^@^"
index=0
config=""
pwd_path=`pwd`
cpplint_path="$pwd_path/cpplint.py"
echo cpplint_path=$cpplint_path

src_path="$pwd_path/src"
echo src_path=$src_path
# add file to an array,and check file in array last
# for file in `find $src_path -name "*.h" -type f`
for file in `find $src_path -maxdepth 1 -type f | grep -E "\.h$|\.cc$|\.cu$|\.cpp$"`
do
    echo file=$file
    echo -e "\033[36m ===> [FILE] \033[0m \033[47;31m $file \033[0m"
    check_files[$index]=$file
    index=$(($index+1))
done
# accroding to config options make a checking command
# first check if python2 exists
check_cmd=""
is_python2_exists=`ls /usr/bin/ | grep -E '^python2$' | wc -l`
if [ $is_python2_exists -ge 1 ]
then
    # read cpplint.ini to decide which option to add
    for file in ${check_files[*]}
    do
        check_cmd="python2 $cpplint_path --linelength=80"
        echo -e "\033[33m =========== check file $file =============\033[0m"
        check_cmd="$check_cmd"$file
        eval $check_cmd
        echo -e "\033[45m ==========================================\033[0m"
    done
fi

 運行:

cpplint_dir$sudo bash cpplint_shell.sh

這個腳本是直接在terminal終端打印信息,更進一步,其實可以嘗試生成檢測報告,也可以嘗試將檢測信息當成注釋放在源文件。

注意,一定要使用bash,被這個問題搞了半天,(# ̄~ ̄#)

這是因為sh和bash的shell解析器版本不一樣,版本不同,解析規則有不一樣的地方。

使用sh運行的時候出現bad substitution的錯誤,使用bash就沒有問題啦。。。

2. windows系統使用過程

2.1 安裝python,注意設置系統環境變量,將python的安裝目錄寫入path環境變量,保存cpplint.py文件;

2.2 運行命令或者腳本文件;

命令行:

cpplint_py_dir$python cpplint.py test.cpp

batch文件:

echo off
python cpplint.py test.cc --output=vs7 > report.txt 2>&1
pause

執行bat文件即可;

2.3 批量檢測分析;

步驟:在目錄下創建src和reports目錄;將要檢測的源文件置於src目錄;修改batch文件並執行;

echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set suffix=.report.txt
set src_path=src
set reports_path=reports
for %%i in (%src_path%/*.cpp) do (
echo %%i
set report_name=%reports_path%/%%i%suffix%
set src_name=%src_path%/%%i
echo src_name=!src_name!
echo report_name=!report_name!
python cpplint.py !src_name! --counting=toplevel --output=vs7 > !report_name! 2>&1
)
pause

注意,1)等號=兩邊沒有空格等。2)必須在dos下實驗目錄運行bat文件。3)bat文件是dos下的批處理文件。

運行腳本:

cpplint_py_dir$./cpplint_batch.bat

參考

1. 靜態代碼分析工具匯總;

2. Google 開源項目風格指南 (中文版);

3. google code style on github;

4. Google代碼規范工具Cpplint的使用_fengbingchun_windows;

5. cpplint_github;

6. cpplint使用技巧;

7. windows_batch_process;

8. cpplint_comment_error;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM