成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

靜態分析工具Clang Static Analyzer (4) Clang-Tidy

系統 OpenHarmony
Clang-Tidy是一個基于Clang的C++ “linter” 工具。絕大部分lint工具只能在出現問題的代碼地方給出提示,之后需要人為修改,而clang-tidy則能夠自動修復功能。

??想了解更多關于開源的內容,請訪問:??

??51CTO 開源基礎軟件社區??

??https://ost.51cto.com??

前文介紹CodeChecker時,使用到了Clang-Tidy,我們來看看這個工具是什么,如何使用。主要是為了了解下背后的知識點,使用CodeChecker已經很好用了。

1、Clang-Tidy介紹

Clang-Tidy是一個基于Clang的C++ “linter” 工具。絕大部分lint工具只能在出現問題的代碼地方給出提示,之后需要人為修改,而clang-tidy則能夠自動修復功能。當然這個如何修復,需要該check作者提供。clang-tidy 的目的是為診斷和修復典型編程錯誤提供一個可擴展的框架,如樣式違規、接口濫用或可以通過靜態分析推斷的缺陷。clang-tidy 是模塊化的,提供了便利的接口來增加新的check檢查器。如果用戶想往clang-tidy添加一個新的檢測功能,只需要編寫一個clang-tidy check實現。每一個check檢測一種問題,例如檢測某個違反Code style的模式,檢測某些API不正確使用的方法等。

2、Clang-Tidy使用入門

clang-tidy是一個基于LibTooling的工具,如果為項目設置編譯命令數據庫,clang-tidy更容易工作。如何設置編譯命令數據的例子,請參閱??如何設置 LLVM 的工具??。您還可以在命令行??--??符號之后指定編譯選項

clang-tidy test.cpp -- -Imy_project/include -DMY_DEFINES ...

clang-tidy有自己的checks檢查器,也可以運行Clang Static Analyzer的checks檢查器。每個check檢查器都有一個名稱,可以使用選項-checks=選擇要運行的檢查,該選項指定了以逗號分隔的正和 負(前綴為-)的globs模式。正模式為要添加的檢查器集合,負的模式會刪除檢查器集合。例如,下面的例子將禁用所有的檢查(-*),并且啟用除 clang-analyzer-cplusplus* 之外的所有匹配clang-analyzer-*模式的檢查器。

$ clang-tidy test.cpp -checks=-*,clang-analyzer-*,-clang-analyzer-cplusplus*

命令行選項-list-checks會列出所有已啟用的檢查。當不帶選項-checks=時,它會顯示默認啟用的檢查器。使用-checks=*時,會查看所有可用的檢查器;指定具體值-checks=XXX時,會查看匹配該模式值的檢查器。可用自己體驗下。

clang-tidy -list-checks
clang-tidy -list-checks -checks=*
clang-tidy -list-checks -checks=-*,clang-analyzer-*,-clang-analyzer-cplusplus*

目前有以下檢查組:

(1)具體示例

可以使用之前的hello.c,看下怎么使用。如上文所說,一般不會直接使用clang-tidy,使用CodeChecker更好一些,需要了解下即可。

int main() {
int x = 7 / 0; // bug here
return 0;
}

執行如下命令:

clang-tidy hello.c
clang-tidy --checks=* hello.c

選擇一條命令執行,輸出類似下文的輸出。可以看到輸出了被各種檢查器診斷出來的缺陷或者告警信息。

zhushangyuan@DESKTOP-RPE9R4O:~/CSA$ clang-tidy --checks=* hello.c
5 warnings generated.
/home/zhushangyuan/CSA/hello.c:2:7: warning: Value stored to 'x' during its initialization is never read [clang-analyzer-deadcode.DeadStores]
int x = 7 / 0; // bug here
^
/home/zhushangyuan/CSA/hello.c:2:7: note: Value stored to 'x' during its initialization is never read
/home/zhushangyuan/CSA/hello.c:2:11: warning: 7 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
int x = 7 / 0; // bug here
^
/home/zhushangyuan/CSA/hello.c:2:13: warning: Division by zero [clang-analyzer-core.DivideZero]
int x = 7 / 0; // bug here
^
/home/zhushangyuan/CSA/hello.c:2:13: note: Division by zero
/home/zhushangyuan/CSA/hello.c:2:13: warning: division by zero is undefined [clang-diagnostic-division-by-zero]
int x = 7 / 0; // bug here
^

3、檢查器分組

目前有以下檢查器分組。不同的分組針對不同的檢查對象,不同的開源項目Android、Fuchsia等,不同的編碼規范等,可以針對性啟用。

Name prefix

Description

??abseil-??

Checks related to Abseil library.

??altera-??

Checks related to OpenCL programming for FPGAs.

??android-??

Checks related to Android.

??boost-??

Checks related to Boost library.

??bugprone-??

Checks that target bug-prone code constructs.

??cert-??

Checks related to CERT Secure Coding Guidelines.

??clang-analyzer-??

Clang Static Analyzer checks.

??concurrency-??

Checks related to concurrent programming (including threads, fibers, coroutines, etc.).

??cppcoreguidelines-??

Checks related to C++ Core Guidelines.

??darwin-??

Checks related to Darwin coding conventions.

??fuchsia-??

Checks related to Fuchsia coding conventions.

??google-??

Checks related to Google coding conventions.

??hicpp-??

Checks related to High Integrity C++ Coding Standard.

??linuxkernel-??

Checks related to the Linux Kernel coding conventions.

??llvm-??

Checks related to the LLVM coding conventions.

??llvmlibc-??

Checks related to the LLVM-libc coding standards.

??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.

??openmp-??

Checks related to OpenMP API.

??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.

Clang語言的靜態分析和clang-tidy檢查器的靜態分析類似。Clang的靜態分析會被clang-tidy展示,也會通過選項-checks=被過濾掉。然而,這些檢查器的過濾不會影響編譯參考,因此它不能打開已經在構建配置中關閉的Clang告警開關。-warnings-as-errors= 選項會把-checks=選項的檢查器檢測出的告警信息升級為錯誤信息。

Clang靜態分析診斷的檢查器名稱以clang-diagnostic-開頭。對應每一個告警選項的分析診斷,其名稱格式為are named clang-diagnostic-<warning-option>。例如,被編譯選項-Wliteral-conversion控制的Clang告警,會被名為clang-diagnostic-literal-conversion的檢查器來分析并報告。

The -fix flag instructs clang-tidy to fix found errors if supported by corresponding checks.

有個比較重要的選項,--fix,開啟這個選項clang-tidy會修復發現的錯誤,在對應的檢查器支持的情況下。哪些檢查器支持自動修復,可以參考下文中檢查器列表中的Offers fixes字段。使用clang-tidy --help可以查看幫助信息,我們這里主要看下--fix相關的幫助信息。開啟這個選項--fix,clang-tidy會修復發現的錯誤。沒有指定--fix-errors選項時,如果發現編譯錯誤,clang-tidy會跳過修復。在指定--fix-errors選項時,即使發現編譯錯誤,也會繼續修復。

--fix                          -
Apply suggested fixes. Without -fix-errors
clang-tidy will bail out if any compilation
errors were found.
--fix-errors -
Apply suggested fixes even if compilation
errors were found. If compiler errors have
attached fix-its, clang-tidy will apply them as
well.

沒有親自體驗過,執行下面的命令,如果發現示例文件中的未使用的聲明??using declarations??的告警信息,就會自動修復刪除掉。

// 找出simple.cc中所有沒有用到的using declarations并自動fix(刪除掉)
$ clang-tidy -checks="-*,misc-unused-using-decls" -fix path/to/simple.cc --

4、檢查器列表

Clang-Tidy 現在支持<mark>四五百個</mark>Checks檢查器,詳細列表可以訪問??clang-tidy - Clang-Tidy Checks — Extra Clang Tools 16.0.0git documentation??獲取。可以看到對于這些檢查器,是否支持自動修復錯誤。對于這些檢查器,也是很好的學習資源,可以看看這些檢查器的會修復什么類型的缺陷,以后寫代碼的時候,避免編寫這些的缺陷,提升編程能力和素養。

  • Clang-Tidy Checks列表片段

Name

Offers fixes

??abseil-cleanup-ctad??

Yes

??android-cloexec-creat??

Yes

??android-comparison-in-temp-failure-retry??

??boost-use-to-string??

Yes

??bugprone-argument-comment??

Yes

??cert-dcl21-cpp??

Yes

??clang-analyzer-core.DynamicTypePropagation??

5、學習些檢查器

在使用clang-tidy檢查報告,可以詳細了解下檢查器的知識,可以有助于我們理解這些檢查規則,有助于如何修復。挑選幾個。

(1)readability-duplicate-include

詳細鏈接在https://clang.llvm.org/extra/clang-tidy/checks/readability/duplicate-include.html。

Looks for duplicate includes and removes them. The check maintains a list of included files and looks for duplicates. If a macro is defined or undefined then the list of included files is cleared.

查找重復的include語句并刪除它們。該檢查器維護一個include文件列表,然后查找重復項。如果已定義或取消定義宏,include文件列表會被清理。

示例如下:

#include <memory>
#include <vector>
#include <memory>

修復方法:

#include <memory>
#include <vector>

如下示例,因為中間出現宏定義,不會識別出重復include,代碼不會被自動修復。

#undef NDEBUG
#include "assertion.h"
// ...code with assertions enabled

#define NDEBUG
#include "assertion.h"
// ...code with assertions disabled

(2)readability-delete-null-pointer

詳細鏈接在https://clang.llvm.org/extra/clang-tidy/checks/readability/delete-null-pointer.html。

在if語句里,如果檢測指針是否存在,然后刪除。這樣的檢查是沒有必要的,因為刪除空指針沒有任何左右,屬于可以刪除的冗余代碼。

int *p;
if (p)
delete p;

(3)misc-unused-parameters

詳細鏈接在https://clang.llvm.org/extra/clang-tidy/checks/misc/unused-parameters.html。

查找未使用的函數參數。未使用的參數可能意味著代碼缺陷,例如,當使用不同的參數代替時。建議的修復要么是注釋參數名稱或完全刪除參數,只要函數的調用方位于同一翻譯單元中,并且可以修改。

該檢查器類似于編譯器診斷選項-Wunused-parameter,可以用于準備代碼庫以啟用該診斷。默認情況下,該檢查器比較寬松。

示例1:

void a(int i) { /*some code that doesn't use `i`*/ }

// becomes

void a(int /*i*/) { /*some code that doesn't use `i`*/ }

示例2:

static void staticFunctionA(int i);
static void staticFunctionA(int i) { /*some code that doesn't use `i`*/ }

// becomes

static void staticFunctionA()
static void staticFunctionA() { /*some code that doesn't use `i`*/ }

??想了解更多關于開源的內容,請訪問:??

??51CTO 開源基礎軟件社區??

??https://ost.51cto.com??。

責任編輯:jianghua 來源: 51CTO 開源基礎軟件社區
相關推薦

2022-12-13 15:42:56

Clang-Tidy靜態分析工具

2022-12-09 15:38:54

Cppcheck靜態分析工具

2022-12-08 15:25:10

Clang分析工具CSA

2021-12-27 11:11:30

LLVMSPIR-V后端

2016-12-12 14:19:59

LLVMClangApple

2022-11-24 13:05:27

ClangiOS

2021-01-14 15:49:10

Linux 5.12GCC編譯器

2018-04-13 10:56:14

編譯器工具開發者

2015-07-30 11:36:48

Xcode7ClangAddress San

2012-05-22 00:28:21

JavaJava開源開源工具

2011-04-11 13:58:09

TCP

2018-03-06 14:33:21

Windows微軟編譯器

2021-04-25 18:09:53

Fedora 35編譯器開發

2020-11-03 10:32:22

編譯器工具代碼

2018-12-17 16:54:50

開發者技能 Firefox

2018-02-25 10:33:46

LLVMSwiftRust

2021-06-08 13:56:34

工具靜態代碼

2017-04-19 12:05:59

2021-07-29 06:37:55

KubernetesKubeLinter工具

2016-05-17 13:54:05

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 四虎午夜剧场 | 国产二区三区 | 国产精品一区二区av | 国产精品伦理一区二区三区 | 欧美久久一区二区三区 | 久久精品国产一区二区三区不卡 | 涩涩99 | 欧美一区日韩一区 | 亚洲协和影视 | 国产 日韩 欧美 在线 | 婷婷久久网 | 婷婷久久精品一区二区 | 99久视频| 午夜无码国产理论在线 | 中文字幕免费中文 | 激情亚洲 | 精品国产一区二区三区成人影院 | 免费在线观看黄色av | 日韩在线免费视频 | 日日夜精品视频 | 三级在线免费 | 亚洲一区二区三区福利 | 中文字幕av在线 | 91精品久久久久久久久 | 欧美成人一区二区三区 | 亚洲精品在线视频 | 成人av免费在线观看 | 久久精品无码一区二区三区 | 日韩二| 人人看人人搞 | 99久久久久久 | 日本福利在线观看 | 国产精品1区2区3区 欧美 中文字幕 | 99热在这里只有精品 | 国产高清免费在线 | 国产日韩欧美一区二区 | 国产亚洲精品精品国产亚洲综合 | 日韩欧美国产一区二区三区 | 国产一区二区三区免费观看在线 | 久久高清 | 日韩精品久久久久 |