clang-tidy 使用

1. 安裝clang-tidy和clang編譯工具

  • sudo apt install clang-tidy clang(ubuntu 16.04默認安裝的是3.8版本)
  • sudo apt install clang-tidy-6.0 clang-6.0 clang-tool-6.0(ubuntu 16.04 apt最高可安裝6.0版本)
  • 多版本可使用update-alternatives來管理

2. 簡單使用

  • 查看所有可靜態分析內容參考clang-tidy -list-checks -checks="*",分爲以下幾大類:
    android- Checks related to Android.
    boost- Checks related to Boost library.
    bugprone- Checks that target bugprone code constructs.
    cert- Checks related to CERT Secure Coding Guidelines.
    cppcoreguidelines- Checks related to C++ Core Guidelines.
    clang-analyzer- Clang Static Analyzer checks.
    fuchsia- Checks related to Fuchsia coding conventions.
    google- Checks related to Google coding conventions.
    hicpp- Checks related to High Integrity C++ Coding Standard.
    llvm- Checks related to the LLVM coding conventions.
    misc- Checks that we didn’t have a better category for.
    modernize- Checks that advocate usage of modern (currently “modern” means “C++11”) language constructs.
    mpi- Checks related to MPI (Message Passing Interface).
    objc- Checks related to Objective-C coding conventions.
    performance- Checks that target performance-related issues.
    portability- Checks that target portability-related issues that don’t relate to any particular coding style.
    readability- Checks that target readability-related issues that don’t relate to any particular coding style.
    zircon- Checks related to Zircon kernel coding conventions.

  • readability-identifier-naming

      lower_case,
      UPPER_CASE,
      camelBack,
      CamelCase,
      camel_Snake_Back,
      Camel_Snake_Case,
      aNy_CasE.
    
  • modernize 具體內容:參考
    modernize-avoid-bind: 使用lambda替換std::binding。
    modernize-deprecated-headers: 將C標準庫的頭文件include替換成C++style,#include <assert.h> => #include 。
    modernize-loop-convert: 使用for-range loop替換for(…;…;…😉, 並更新for語句的相關變量。
    modernize-make-shared: 找出所有顯式創建std::shared_ptr變量的表達式,並使用make_shared替換。
    modernize-make-unique: 跟make-shared一樣,使用std::make_unique替換所有std::unique_ptr顯式創建表達式。
    modernize-pass-by-value: 在構造函數中使用move語義
    modernize-raw-string-literal: 用C++11的raw string literal(R"…")替換原來的string literal, 這樣的好處就是不用再添加轉義符\了。
    modernize-redundant-void-arg: 去掉void函數參數。
    modernize-replace-auto-ptr: 用std::unique_ptr替換std::shared_ptr, std::shared_ptr是不推薦使用的,即使在C++98。
    modernize-shrink-to-fit: 在C++03中,如果我們想修改STL容器的capacity,只能通過copy & swap的方式,C++11提供了shink_to_fit的方法。
    modernize-use-auto: 在變量定義的時候,使用auto代替顯式的類型聲明,這個在定義STL容器類的Iterator特別方便。
    modernize-use-bool-literals: 找出所有隱式從int轉成bool的literal, 使用true或者false代替。
    modernize-use-default: 對於沒有任何自定義行爲(定義爲{})的特殊的成員函數,構造函數,析構函數,移動/複製構造函數,用=default代替掉{}。
    modernize-use-emplace: 使用STL容器中的emplace代替push_back。
    modernize-use-equals-delete: 在C++98中,類設計爲了實現禁止調用某些特殊的成員函數,通常把它們聲明成private;在C++11中,只需要在聲明中體檢=delete,找出所有private的特殊成員函數,並將它們標記成=delete。
    modernize-use-nullptr: 用nullptr代替NULL。
    modernize-use-override: 對於子類改寫父類的virtual方法,在方法後面添加override, 並刪掉virtual前綴,即virtual void NewOveride() => void NewOverride() override {}。
    modernize-use-using: 用using代替typedef, 如typedef int V => using V = int。

  • cmake 中的使用(cmake時加上-DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang)

#clang-tidy
if (NOT WIN32)
  	set(CLANG-TIDY-EXE "/usr/bin/clang-tidy-6.0")
endif()
if (EXISTS "${CLANG-TIDY-EXE}")
	message(STATUS "use clang-tidy to check")	
  	set(CMAKE_CXX_CLANG_TIDY "${CLANG-TIDY-EXE};-checks=-*,modernize-use-nullptr;-warnings-as-errors=*;-p=${CMAKE_BUILD_DIR};-export-fixes=clang-tidy-fixes.yml")
	if(CLANG_TIDY_FIX)
		message(STATUS "apply suggested fix" )
		set(CMAKE_CXX_CLANG_TIDY "${CMAKE_CXX_CLANG_TIDY};-fix")
	endif()
endif()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章