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

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

系統(tǒng) OpenHarmony
Clang-Tidy是一個(gè)基于Clang的C++ “l(fā)inter” 工具。絕大部分lint工具只能在出現(xiàn)問(wèn)題的代碼地方給出提示,之后需要人為修改,而clang-tidy則能夠自動(dòng)修復(fù)功能。

??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??

??51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)??

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

前文介紹CodeChecker時(shí),使用到了Clang-Tidy,我們來(lái)看看這個(gè)工具是什么,如何使用。主要是為了了解下背后的知識(shí)點(diǎn),使用CodeChecker已經(jīng)很好用了。

1、Clang-Tidy介紹

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

2、Clang-Tidy使用入門

clang-tidy是一個(gè)基于LibTooling的工具,如果為項(xiàng)目設(shè)置編譯命令數(shù)據(jù)庫(kù),clang-tidy更容易工作。如何設(shè)置編譯命令數(shù)據(jù)的例子,請(qǐng)參閱??如何設(shè)置 LLVM 的工具??。您還可以在命令行??--??符號(hào)之后指定編譯選項(xiàng)

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

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

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

命令行選項(xiàng)-list-checks會(huì)列出所有已啟用的檢查。當(dāng)不帶選項(xiàng)-checks=時(shí),它會(huì)顯示默認(rèn)啟用的檢查器。使用-checks=*時(shí),會(huì)查看所有可用的檢查器;指定具體值-checks=XXX時(shí),會(huì)查看匹配該模式值的檢查器??捎米约后w驗(yàn)下。

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

目前有以下檢查組:

(1)具體示例

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

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

執(zhí)行如下命令:

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

選擇一條命令執(zhí)行,輸出類似下文的輸出??梢钥吹捷敵隽吮桓鞣N檢查器診斷出來(lái)的缺陷或者告警信息。

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、檢查器分組

目前有以下檢查器分組。不同的分組針對(duì)不同的檢查對(duì)象,不同的開(kāi)源項(xiàng)目Android、Fuchsia等,不同的編碼規(guī)范等,可以針對(duì)性啟用。

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語(yǔ)言的靜態(tài)分析和clang-tidy檢查器的靜態(tài)分析類似。Clang的靜態(tài)分析會(huì)被clang-tidy展示,也會(huì)通過(guò)選項(xiàng)-checks=被過(guò)濾掉。然而,這些檢查器的過(guò)濾不會(huì)影響編譯參考,因此它不能打開(kāi)已經(jīng)在構(gòu)建配置中關(guān)閉的Clang告警開(kāi)關(guān)。-warnings-as-errors= 選項(xiàng)會(huì)把-checks=選項(xiàng)的檢查器檢測(cè)出的告警信息升級(jí)為錯(cuò)誤信息。

Clang靜態(tài)分析診斷的檢查器名稱以clang-diagnostic-開(kāi)頭。對(duì)應(yīng)每一個(gè)告警選項(xiàng)的分析診斷,其名稱格式為are named clang-diagnostic-<warning-option>。例如,被編譯選項(xiàng)-Wliteral-conversion控制的Clang告警,會(huì)被名為clang-diagnostic-literal-conversion的檢查器來(lái)分析并報(bào)告。

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

有個(gè)比較重要的選項(xiàng),--fix,開(kāi)啟這個(gè)選項(xiàng)clang-tidy會(huì)修復(fù)發(fā)現(xiàn)的錯(cuò)誤,在對(duì)應(yīng)的檢查器支持的情況下。哪些檢查器支持自動(dòng)修復(fù),可以參考下文中檢查器列表中的Offers fixes字段。使用clang-tidy --help可以查看幫助信息,我們這里主要看下--fix相關(guān)的幫助信息。開(kāi)啟這個(gè)選項(xiàng)--fix,clang-tidy會(huì)修復(fù)發(fā)現(xiàn)的錯(cuò)誤。沒(méi)有指定--fix-errors選項(xiàng)時(shí),如果發(fā)現(xiàn)編譯錯(cuò)誤,clang-tidy會(huì)跳過(guò)修復(fù)。在指定--fix-errors選項(xiàng)時(shí),即使發(fā)現(xiàn)編譯錯(cuò)誤,也會(huì)繼續(xù)修復(fù)。

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

沒(méi)有親自體驗(yàn)過(guò),執(zhí)行下面的命令,如果發(fā)現(xiàn)示例文件中的未使用的聲明??using declarations??的告警信息,就會(huì)自動(dòng)修復(fù)刪除掉。

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

4、檢查器列表

Clang-Tidy 現(xiàn)在支持<mark>四五百個(gè)</mark>Checks檢查器,詳細(xì)列表可以訪問(wèn)??clang-tidy - Clang-Tidy Checks — Extra Clang Tools 16.0.0git documentation??獲取??梢钥吹綄?duì)于這些檢查器,是否支持自動(dòng)修復(fù)錯(cuò)誤。對(duì)于這些檢查器,也是很好的學(xué)習(xí)資源,可以看看這些檢查器的會(huì)修復(fù)什么類型的缺陷,以后寫代碼的時(shí)候,避免編寫這些的缺陷,提升編程能力和素養(yǎng)。

  • 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、學(xué)習(xí)些檢查器

在使用clang-tidy檢查報(bào)告,可以詳細(xì)了解下檢查器的知識(shí),可以有助于我們理解這些檢查規(guī)則,有助于如何修復(fù)。挑選幾個(gè)。

(1)readability-duplicate-include

詳細(xì)鏈接在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.

查找重復(fù)的include語(yǔ)句并刪除它們。該檢查器維護(hù)一個(gè)include文件列表,然后查找重復(fù)項(xiàng)。如果已定義或取消定義宏,include文件列表會(huì)被清理。

示例如下:

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

修復(fù)方法:

#include <memory>
#include <vector>

如下示例,因?yàn)橹虚g出現(xiàn)宏定義,不會(huì)識(shí)別出重復(fù)include,代碼不會(huì)被自動(dòng)修復(fù)。

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

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

(2)readability-delete-null-pointer

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

在if語(yǔ)句里,如果檢測(cè)指針是否存在,然后刪除。這樣的檢查是沒(méi)有必要的,因?yàn)閯h除空指針沒(méi)有任何左右,屬于可以刪除的冗余代碼。

int *p;
if (p)
delete p;

(3)misc-unused-parameters

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

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

該檢查器類似于編譯器診斷選項(xiàng)-Wunused-parameter,可以用于準(zhǔn)備代碼庫(kù)以啟用該診斷。默認(rèn)情況下,該檢查器比較寬松。

示例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`*/ }

??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??

??51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)??

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

責(zé)任編輯:jianghua 來(lái)源: 51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)
相關(guān)推薦

2022-12-13 15:42:56

Clang-Tidy靜態(tài)分析工具

2022-12-09 15:38:54

Cppcheck靜態(tài)分析工具

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

編譯器工具開(kāi)發(fā)者

2015-07-30 11:36:48

Xcode7ClangAddress San

2012-05-22 00:28:21

JavaJava開(kāi)源開(kāi)源工具

2011-04-11 13:58:09

TCP

2018-03-06 14:33:21

Windows微軟編譯器

2021-04-25 18:09:53

Fedora 35編譯器開(kāi)發(fā)

2020-11-03 10:32:22

編譯器工具代碼

2018-12-17 16:54:50

開(kāi)發(fā)者技能 Firefox

2018-02-25 10:33:46

LLVMSwiftRust

2021-06-08 13:56:34

工具靜態(tài)代碼

2017-04-19 12:05:59

2016-05-17 13:54:05

2021-07-29 06:37:55

KubernetesKubeLinter工具
點(diǎn)贊
收藏

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