<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

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

          共 5292字,需瀏覽 11分鐘

           ·

          2022-11-21 10:21

          # 干了這碗雞湯

          不要否定自己的未來,未來會很晴朗,好運也在路上。



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


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


          話不多說,上代碼:

          #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,可能我們在開發(fā)過程中沒有注意到這種用法,所以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;              ^~~~~              nullptrSuppressed 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里派生類繼承父類,重寫了某些函數(shù)時最好加上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() {    ~~~~~~~~~~~~~^                        overrideSuppressed 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.


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

          #include <iostream>#include <string>#include <memory>


          這里其實有一點點問題,頭文件引用順序不滿足編碼規(guī)范,這里其實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的代碼規(guī)范
          • cpp-core-guidelines:檢測是否違反cpp-core-guidelines
          • google:檢測是否違反google編碼規(guī)范
          • llvm:檢測是否違反llvm編碼規(guī)范
          • performance:檢測性能相關的問題
          • readability:檢測與可讀性相關,但又不屬于某些編碼規(guī)范的問題
          • modernize:檢測是否使用現(xiàn)代C++11相關的代碼問題


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


          瀏覽 88
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  男人的天堂青青草视频 | 天堂视频资源8 | 国产精品无码久久久久久免费 | 影音先锋成人网 | 伊人在线久久 |