日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
如何寫(xiě)出優(yōu)雅的C++代碼

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

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

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

話不多說(shuō),上代碼:

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

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

 
 
 
  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. 

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

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

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

 
 
 
  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. }; 

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

 
 
 
  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  
  2. #include  
  3. #include  

這里其實(shí)有一點(diǎn)點(diǎn)問(wèn)題,頭文件引用順序不滿足編碼規(guī)范,這里其實(shí)clang-format都可以檢測(cè)出來(lái),但clang-tidy也可以檢測(cè)出來(lái),通過(guò)-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  
  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 

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

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

這是使用clang-tidy的檢測(cè)結(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( ) 
  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還有很多高端功能,大概可以檢測(cè)出250種問(wèn)題,大體主要分為幾大類:

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

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

參考資料:

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

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


名稱欄目:如何寫(xiě)出優(yōu)雅的C++代碼
分享地址:http://www.dlmjj.cn/article/cccshcs.html