本篇內容主要講解“怎么寫出優雅的C++代碼”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么寫出優雅的C++代碼”吧!
工欲善其事必先利其器,優雅的代碼離不開靜態代碼檢查工具,大家可能平時使用較多的是cppcheck,但今天我想跟大家分享另一個靜態代碼檢查工具clang-tidy。
不同于cppcheck使用正則表達式進行靜態代碼分析,clang-tidy是基于語法分析樹的靜態代碼檢查工具,雖然它的速度比正則表達式慢一些,但是它檢查的更準確、全面,而且不僅可以做靜態檢查,還可以做一些修復工作,自行添加一些自定義檢查規則。
話不多說,上代碼:
#include <iostream> int main() { int a = 1.2; return 0; }
這里有隱式類型轉換,可以使用clang-tidy來檢測:
~/test$ clang-tidy -checks=* test_lint.cpp -- 7748 warnings generated. /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] int a = 1.2; ^ Suppressed 7747 warnings (7747 in non-user code). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
這里也許你有疑問了,這不就是一個普通的編譯警告嘛,正常使用編譯器也可以檢查出來,那再看一段代碼:
#include <iostream> int main() { char* d = NULL; return 0; }
我們都知道在C++中應該更多的使用nullptr而不是NULL,這里使用了NULL而不是使用nullptr,可能我們在開發過程中沒有注意到這種用法,所以clang-tidy派上了用場:
~/test$ clang-tidy -checks=* test_lint.cpp -- 7748 warnings generated. /home/wangzhiqiang/test/test_lint.cpp:20:15: warning: use nullptr [modernize-use-nullptr] char* d = NULL; ^~~~~ nullptr Suppressed 7747 warnings (7747 in non-user code). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
再舉一個例子:
struct Base { virtual void func() { } }; struct Derive : Base { virtual void func() { } };
這里可能我們乍一看沒有任何問題,其實在C++11里派生類繼承父類,重寫了某些函數時最好加上override關鍵字,通過clang-tidy還是可以檢測出來:
~/test$ clang-tidy -checks=* test_lint.cpp -- 7749 warnings generated. /home/wangzhiqiang/test/test_lint.cpp:14:18: warning: prefer using 'override' or (rarely) 'final' instead of 'virtual' [hicpp-use-override] virtual void func() { ~~~~~~~~~~~~~^ override Suppressed 7747 warnings (7747 in non-user code). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
該工具還可以檢查代碼是否符合編碼規范,例如Google編碼規范等,看這段頭文件相關代碼:
#include <iostream> #include <string> #include <memory>
這里其實有一點點問題,頭文件引用順序不滿足編碼規范,這里其實clang-format都可以檢測出來,但clang-tidy也可以檢測出來,通過-fix還可以進行自動修復:
~/test$ clang-tidy -checks=* test_lint.cpp -- 8961 warnings generated. /home/wangzhiqiang/test/test_lint.cpp:2:1: warning: #includes are not sorted properly [llvm-include-order] #include <string> ^ ~~~~~~~~ Suppressed 8960 warnings (8960 in non-user code). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well
它還可以檢測隱藏的內存泄漏:
int main() { char* ct = (char*)malloc(323); return 0; }
這是使用clang-tidy的檢測結果:
~/test$ clang-tidy -checks=* test_lint.cpp -- 7756 warnings generated. /home/wangzhiqiang/test/test_lint.cpp:20:5: warning: initializing non-owner 'char *' with a newly created 'gsl::owner<>' [cppcoreguidelines-owning-memory] char* ct = (char*)malloc(323); ^ /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] char* ct = (char*)malloc(323); ^~~~~ auto /home/wangzhiqiang/test/test_lint.cpp:20:11: warning: Value stored to 'ct' during its initialization is never read [clang-analyzer-deadcode.DeadStores] char* ct = (char*)malloc(323); ^ /home/wangzhiqiang/test/test_lint.cpp:20:11: note: Value stored to 'ct' during its initialization is never read /home/wangzhiqiang/test/test_lint.cpp:20:16: warning: C-style casts are discouraged; use static_cast [google-readability-casting] char* ct = (char*)malloc(323); ^~~~~~~~~~~~~ ~ static_cast<char*>( ) /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] /home/wangzhiqiang/test/test_lint.cpp:20:23: warning: do not manage memory manually; consider a container or a smart pointer [cppcoreguidelines-no-malloc] char* ct = (char*)malloc(323); ^ /home/wangzhiqiang/test/test_lint.cpp:21:5: warning: Potential leak of memory pointed to by 'ct' [clang-analyzer-unix.Malloc] return 0; ^ /home/wangzhiqiang/test/test_lint.cpp:20:23: note: Memory is allocated char* ct = (char*)malloc(323); ^ /home/wangzhiqiang/test/test_lint.cpp:21:5: note: Potential leak of memory pointed to by 'ct' return 0; ^ Suppressed 7747 warnings (7747 in non-user code). 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庫的相關問題
android:檢測Android相關問題
boost:檢測boost庫的相關問題
cert:檢測CERT的代碼規范
cpp-core-guidelines:檢測是否違反cpp-core-guidelines
google:檢測是否違反google編碼規范
llvm:檢測是否違反llvm編碼規范
performance:檢測性能相關的問題
readability:檢測與可讀性相關,但又不屬于某些編碼規范的問題
modernize:檢測是否使用現代C++11相關的代碼問題
而且適用于Windows/Linux/MacOS多平臺,還支持命令行,CLion/VSCode/VSStudio插件等,檢測規則還可以定制,重要的是免費開源,快去用起來吧,寫出優雅的C++代碼~
到此,相信大家對“怎么寫出優雅的C++代碼”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。