自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何寫出優(yōu)雅的C++代碼

開發(fā) 后端
工欲善其事必先利其器,優(yōu)雅的代碼離不開靜態(tài)代碼檢查工具,大家可能平時(shí)使用較多的是cppcheck,但今天我想跟大家分享另一個(gè)靜態(tài)代碼檢查工具clang-tidy。

[[373379]]

本文轉(zhuǎn)載自微信公眾號(hào)「程序喵大人」,作者程序喵大人 。轉(zhuǎn)載本文請(qǐng)聯(lián)系程序喵大人公眾號(hào)。

 工欲善其事必先利其器,優(yōu)雅的代碼離不開靜態(tài)代碼檢查工具,大家可能平時(shí)使用較多的是cppcheck,但今天我想跟大家分享另一個(gè)靜態(tài)代碼檢查工具clang-tidy。

不同于cppcheck使用正則表達(dá)式進(jìn)行靜態(tài)代碼分析,clang-tidy是基于語法分析樹的靜態(tài)代碼檢查工具,雖然它的速度比正則表達(dá)式慢一些,但是它檢查的更準(zhǔn)確、全面,而且不僅可以做靜態(tài)檢查,還可以做一些修復(fù)工作,自行添加一些自定義檢查規(guī)則。

話不多說,上代碼:

  1. #include <iostream> 
  2.  
  3. int main() { 
  4.     int a = 1.2; 
  5.     return 0; 

這里有隱式類型轉(zhuǎn)換,可以使用clang-tidy來檢測:

  1. ~/test$ clang-tidy -checks=* test_lint.cpp -- 
  2. 7748 warnings generated. 
  3. /home/wangzhiqiang/test/test_lint.cpp:20:13: warning: implicit conversion from 'double' to 'int' changes value from 1.2 to 1 [clang-diagnostic-literal-conversion] 
  4.     int a = 1.2; 
  5.             ^ 
  6. Suppressed 7747 warnings (7747 in non-user code). 
  7. Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well. 

這里也許你有疑問了,這不就是一個(gè)普通的編譯警告嘛,正常使用編譯器也可以檢查出來,那再看一段代碼:

  1. #include <iostream> 
  2.  
  3. int main() { 
  4.     char* d = NULL
  5.     return 0; 

我們都知道在C++中應(yīng)該更多的使用nullptr而不是NULL,這里使用了NULL而不是使用nullptr,可能我們?cè)陂_發(fā)過程中沒有注意到這種用法,所以clang-tidy派上了用場:

  1. ~/test$ clang-tidy -checks=* test_lint.cpp -- 
  2. 7748 warnings generated. 
  3. /home/wangzhiqiang/test/test_lint.cpp:20:15: warning: use nullptr [modernize-use-nullptr] 
  4.     char* d = NULL
  5.               ^~~~~ 
  6.               nullptr 
  7. Suppressed 7747 warnings (7747 in non-user code). 
  8. Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well. 

再舉一個(gè)例子:

  1. struct Base { 
  2.     virtual void func() { 
  3.  
  4.     } 
  5. }; 
  6.  
  7. struct Derive : Base { 
  8.     virtual void func() { 
  9.  
  10.     } 
  11. }; 

這里可能我們乍一看沒有任何問題,其實(shí)在C++11里派生類繼承父類,重寫了某些函數(shù)時(shí)最好加上override關(guān)鍵字,通過clang-tidy還是可以檢測出來:

  1. ~/test$ clang-tidy -checks=* test_lint.cpp -- 
  2. 7749 warnings generated. 
  3. /home/wangzhiqiang/test/test_lint.cpp:14:18: warning: prefer using 'override' or (rarely) 'final' instead of 'virtual' [hicpp-use-override] 
  4.     virtual void func() { 
  5.     ~~~~~~~~~~~~~^ 
  6.                         override 
  7. Suppressed 7747 warnings (7747 in non-user code). 
  8. Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well. 

該工具還可以檢查代碼是否符合編碼規(guī)范,例如Google編碼規(guī)范等,看這段頭文件相關(guān)代碼:

  1. #include <iostream> 
  2. #include <string> 
  3. #include <memory> 

這里其實(shí)有一點(diǎn)點(diǎn)問題,頭文件引用順序不滿足編碼規(guī)范,這里其實(shí)clang-format都可以檢測出來,但clang-tidy也可以檢測出來,通過-fix還可以進(jìn)行自動(dòng)修復(fù):

  1. ~/test$ clang-tidy -checks=* test_lint.cpp -- 
  2. 8961 warnings generated. 
  3. /home/wangzhiqiang/test/test_lint.cpp:2:1: warning: #includes are not sorted properly [llvm-include-order
  4. #include <string> 
  5. ^        ~~~~~~~~ 
  6. Suppressed 8960 warnings (8960 in non-user code). 
  7. Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well 

它還可以檢測隱藏的內(nèi)存泄漏:

  1. int main() { 
  2.     char* ct = (char*)malloc(323); 
  3.     return 0; 

這是使用clang-tidy的檢測結(jié)果:

  1. ~/test$ clang-tidy -checks=* test_lint.cpp -- 
  2. 7756 warnings generated. 
  3. /home/wangzhiqiang/test/test_lint.cpp:20:5: warning: initializing non-owner 'char *' with a newly created 'gsl::owner<>' [cppcoreguidelines-owning-memory] 
  4.     char* ct = (char*)malloc(323); 
  5.     ^ 
  6. /home/wangzhiqiang/test/test_lint.cpp:20:5: warning: use auto when initializing with a cast to avoid duplicating the type name [hicpp-use-auto] 
  7.     char* ct = (char*)malloc(323); 
  8.     ^~~~~ 
  9.     auto 
  10. /home/wangzhiqiang/test/test_lint.cpp:20:11: warning: Value stored to 'ct' during its initialization is never read [clang-analyzer-deadcode.DeadStores] 
  11.     char* ct = (char*)malloc(323); 
  12.           ^ 
  13. /home/wangzhiqiang/test/test_lint.cpp:20:11: note: Value stored to 'ct' during its initialization is never read 
  14. /home/wangzhiqiang/test/test_lint.cpp:20:16: warning: C-style casts are discouraged; use static_cast [google-readability-casting] 
  15.     char* ct = (char*)malloc(323); 
  16.                ^~~~~~~~~~~~~     ~ 
  17.                static_cast<char*>( ) 
  18. /home/wangzhiqiang/test/test_lint.cpp:20:16: warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast
  19. /home/wangzhiqiang/test/test_lint.cpp:20:23: warning: do not manage memory manually; consider a container or a smart pointer [cppcoreguidelines-no-malloc] 
  20.     char* ct = (char*)malloc(323); 
  21.                       ^ 
  22. /home/wangzhiqiang/test/test_lint.cpp:21:5: warning: Potential leak of memory pointed to by 'ct' [clang-analyzer-unix.Malloc] 
  23.     return 0; 
  24.     ^ 
  25. /home/wangzhiqiang/test/test_lint.cpp:20:23: note: Memory is allocated 
  26.     char* ct = (char*)malloc(323); 
  27.                       ^ 
  28. /home/wangzhiqiang/test/test_lint.cpp:21:5: note: Potential leak of memory pointed to by 'ct' 
  29.     return 0; 
  30.     ^ 
  31. Suppressed 7747 warnings (7747 in non-user code). 
  32. Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well 

clang-tidy還有很多高端功能,大概可以檢測出250種問題,大體主要分為幾大類:

  • abseil:檢測abseil庫的相關(guān)問題
  • android:檢測Android相關(guān)問題
  • boost:檢測boost庫的相關(guān)問題
  • cert:檢測CERT的代碼規(guī)范
  • cpp-core-guidelines:檢測是否違反cpp-core-guidelines
  • google:檢測是否違反google編碼規(guī)范
  • llvm:檢測是否違反llvm編碼規(guī)范
  • performance:檢測性能相關(guān)的問題
  • readability:檢測與可讀性相關(guān),但又不屬于某些編碼規(guī)范的問題
  • modernize:檢測是否使用現(xiàn)代C++11相關(guān)的代碼問題

而且適用于Windows/Linux/MacOS多平臺(tái),還支持命令行,CLion/VSCode/VSStudio插件等,檢測規(guī)則還可以定制,重要的是免費(fèi)開源,快去用起來吧,寫出優(yōu)雅的C++代碼~

參考資料:

https://clang.llvm.org/extra/clang-tidy/

https://www.bilibili.com/video/av96166240/

 

責(zé)任編輯:武曉燕 來源: 程序喵大人
相關(guān)推薦

2019-09-20 15:47:24

代碼JavaScript副作用

2022-03-11 12:14:43

CSS代碼前端

2020-05-14 09:15:52

設(shè)計(jì)模式SOLID 原則JS

2021-12-07 08:16:34

React 前端 組件

2020-07-15 08:17:16

代碼

2020-05-08 14:45:00

JS代碼變量

2020-05-11 15:23:58

CQRS代碼命令

2021-09-01 08:55:20

JavaScript代碼開發(fā)

2013-06-07 14:00:23

代碼維護(hù)

2021-11-30 10:20:24

JavaScript代碼前端

2022-02-08 19:33:13

技巧代碼格式

2022-02-17 10:05:21

CSS代碼前端

2020-12-19 10:45:08

Python代碼開發(fā)

2020-05-19 15:00:26

Bug代碼語言

2021-12-13 14:37:37

React組件前端

2022-10-24 08:10:21

SQL代碼業(yè)務(wù)

2015-09-28 10:49:59

代碼程序員

2019-06-24 10:26:15

代碼程序注釋

2021-04-29 21:54:44

Python代碼語言

2015-05-11 10:48:28

代碼干凈的代碼越少越干凈
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)