{"id":5748,"date":"2022-05-02T14:25:12","date_gmt":"2022-05-02T06:25:12","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=5748"},"modified":"2022-05-02T14:25:12","modified_gmt":"2022-05-02T06:25:12","slug":"cicd%e4%b8%adclang-tidy%e9%9d%99%e6%80%81%e8%af%ad%e4%b9%89%e6%a3%80%e6%9f%a5","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/5748","title":{"rendered":"CICD\u4e2dclang-tidy\u9759\u6001\u8bed\u4e49\u68c0\u67e5"},"content":{"rendered":"<p><a href=\"https:\/\/blog.csdn.net\/weixin_43360707\/article\/details\/122602239\">\u539f\u6587\u5730\u5740<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>C++\u57fa\u7840\u77e5\u8bc6<br \/>\n\u4e13\u680f\u6536\u5f55\u8be5\u5185\u5bb9<br \/>\n29 \u7bc7\u6587\u7ae00 \u8ba2\u9605<br \/>\n\u8ba2\u9605\u4e13\u680f<br \/>\n\u6559\u7a0b\uff1b<br \/>\nhttps:\/\/hokein.github.io\/clang-tools-tutorial\/<br \/>\n\u8981\u7528clang-tidy\u9996\u5148\u8981\u5728\u7535\u8111\u4e0a\u5b89\u88c5clang-tools<br \/>\nLinux Ubuntu\u7cfb\u7edf<\/p>\n<p>sudo apt install clang-tools<br \/>\n1<br \/>\n\u88c5\u597d\u540e\u5c31\u53ef\u4ee5\u770b\u5230clang-tidy\u4ee5\u53ca\u5176\u5b83\u76f8\u5173\u5de5\u5177\u4e86\u3002<\/p>\n<p>clang-tidy \u5b98\u65b9\u6587\u6863<br \/>\n\u63a5\u4e0b\u6765\u6211\u4eec\u8981\u4f7f\u7528clang-tidy-8 &#8211;help,\u6ce8\u610f\u6700\u540e\u9762<\/p>\n<p>Configuration files:<br \/>\nclang-tidy attempts to read configuration for each source file from a<br \/>\n.clang-tidy file located in the closest parent directory of the source<br \/>\nfile. If any configuration options have a corresponding command-line<br \/>\noption, command-line option takes precedence. The effective<br \/>\nconfiguration can be inspected using -dump-config:<\/p>\n<p>$ clang-tidy -dump-config<br \/>\n&#8212;<br \/>\nChecks: &#8216;-*,some-check&#8217;<br \/>\nWarningsAsErrors: &#8221;<br \/>\nHeaderFilterRegex: &#8221;<br \/>\nFormatStyle: none<br \/>\nUser: user<br \/>\nCheckOptions:<br \/>\n&#8211; key: some-check.SomeOption<br \/>\nvalue: &#8216;some value&#8217;<br \/>\n&#8230;<br \/>\n\u5c31\u662f\u8bf4\u4f60\u53ef\u4ee5\u901a\u8fc7\u5efa\u7acb\u4e00\u4e2a\u6587\u4ef6.clang-tidy\u6765\u51cf\u5c11\u547d\u4ee4\u884c\u4e2d-checks= option\u7684\u547d\u4ee4\u3002<\/p>\n<p>\u5728\u4f60\u9700\u8981\u68c0\u67e5\u7684\u6587\u4ef6\u5939\u4e0b\u5efa\u7acb\u6587\u4ef6.clang-tidy<br \/>\n\u5728.clang-tidy\u4e2d<\/p>\n<p>{<br \/>\nChecks: &#8220;-*,bugprone-*,cert-*,clang-analyzer-*,cppcoreguidelines-avoid-goto,cppcoreguidelines-c-copy-assignment-signature,cppcoreguidelines-interfaces-global-init,cppcoreguidelines-narrowing-conversions,cppcoreguidelines-no-malloc,cppcoreguidelines-pro-bounds-constant-array-index,cppcoreguidelines-pro-type-const-cast,cppcoreguidelines-pro-type-cstyle-cast,cppcoreguidelines-pro-type-member-init,cppcoreguidelines-pro-type-reinterpret-cast,cppcoreguidelines-pro-type-static-cast-downcast,cppcoreguidelines-pro-type-union-access,cppcoreguidelines-slicing,cppcoreguidelines-special-member-functions,google-build-*,google-default-arguments,google-explicit-constructor,google-global-names-in-headers,google-objc-*,google-readability-casting,google-readability-function-size,google-readability-namespace-comments,google-readability-todo,google-runtime-operator,hicpp-avoid-goto,hicpp-deprecated-headers,hicpp-exception-baseclass,hicpp-explicit-conversions,hicpp-function-size,hicpp-invalid-access-moved,hicpp-member-init,hicpp-move-const-arg,hicpp-multiway-paths-covered,hicpp-named-parameter,hicpp-new-delete-operators,hicpp-no-assembler,hicpp-no-malloc,hicpp-noexcept-move,hicpp-special-member-functions,hicpp-static-assert,hicpp-undelegated-constructor,hicpp-use-*,misc-*,modernize-*,-modernize-use-trailing-return-type,performance-*,readability-avoid-const-params-in-decls,readability-container-size-empty,readability-delete-null-pointer,readability-deleted-default,readability-else-after-return,readability-function-size,readability-identifier-naming,readability-inconsistent-declaration-parameter-name,readability-misleading-indentation,readability-misplaced-array-index,readability-named-parameter,readability-non-const-parameter,readability-redundant-*,readability-simplify-*,readability-static-*,readability-string-compare,readability-uniqueptr-delete-release,readability-rary-objects&#8221;<br \/>\n}<br \/>\n1<br \/>\n2<br \/>\n3<br \/>\n\u8fd9\u4e9b\u5c31\u662f\u68c0\u6d4b\u89c4\u5219<br \/>\n\u7136\u540e\u6267\u884c\uff1a<\/p>\n<p>clang-tidy-8 -p ${PWD} -dump-config<br \/>\n1<br \/>\n\u7136\u540e\u8fd0\u884cclang-tidy<\/p>\n<p>clant-tidy-8 &#8211;quite -p .\/ src.cpp<br \/>\n1<br \/>\n\u8fd9\u91cc\u6ce8\u610f-p \u53c2\u6570\uff0c\u5728help\u91cc\u662f\u8fd9\u6837\u8bf4\u7684<\/p>\n<p>-p &lt;build-path&gt; is used to read a compile command database.<\/p>\n<p>For example, it can be a CMake build directory in which a file named<br \/>\ncompile_commands.json exists (use -DCMAKE_EXPORT_COMPILE_COMMANDS=ON<br \/>\nCMake option to get this output). When no build path is specified,<br \/>\na search for compile_commands.json will be attempted through all<br \/>\nparent paths of the first input file . See:<br \/>\nhttp:\/\/clang.llvm.org\/docs\/HowToSetupToolingForLLVM.html for an<br \/>\nexample of setting up Clang Tooling on a source tree.<\/p>\n<p>\u5f88\u591a\u7ae5\u978b\u9047\u5230\u4e00\u4e2a\u95ee\u9898\uff1a<\/p>\n<p>rror while trying to load a compilation database:<br \/>\nCould not auto-detect compilation database from directory &#8220;.\/&#8221;<br \/>\nNo compilation database found in \/builds\/crystal\/crystal-media\/ or any parent directory<\/p>\n<p>\u5c31\u662f\u56e0\u4e3a\u4e0a\u8ff0\u539f\u56e0\u5f15\u8d77\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u901a\u8fc7-p\u6765\u6307\u5b9acompile_commands.json\u7684\u4f4d\u7f6e\uff0c\u5b98\u65b9\u6587\u6863\u8bf4\u660e<\/p>\n<p>z\u8fd9\u91cc\u8fd8\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662fqmake\uff0c\u90a3\u4e48\u5982\u4f55\u4f7f\u7528qmake\u6765clang-tidy\u5462\uff1f<br \/>\n\u95ee\u9898\u89e3\u51b3\u5165\u53e3<br \/>\nhowtouseclangtidyinQt<\/p>\n<p>\u5177\u4f53\u68c0\u67e5\u89c4\u5219\uff1a<br \/>\n1\u3001readability-identifier-naming \uff08\u89c4\u52192.1.1\uff09<\/p>\n<p>\u8fd9\u6761\u89c4\u5219\u662f\u7528\u6765\u68c0\u67e5\u540d\u547d\u540d\u89c4\u5219\u7684\u3002\u5728\u8fd9\u6761\u603b\u7684\u89c4\u5219\u4e4b\u4e0b\uff0c\u8fd8\u6709\u5173\u4e8e\u5177\u4f53\u8981\u68c0\u67e5\u9879\u7684key\u548cvalue\u3002\u6bd4\u5982\uff1a<\/p>\n<p>key: readability-identifier-naming.ClassCase<\/p>\n<p>value: CamelCase<\/p>\n<p>\u8fd9\u91ccClassCase\u8bf4\u660e\u68c0\u67e5\u7684\u662f\u7c7b\u7684\u547d\u540d\uff0cCamelCase\u8bf4\u660e\u8981\u6c42\u7684\u98ce\u683c\u662f\u5927\u9a7c\u5cf0\u547d\u540d\u6cd5\u3002\u65b9\u821f\u91cc\u6d89\u53ca\u5230\u547d\u540d\u89c4\u5219\uff0c\u76ee\u524d\u6709CamelCase\uff08\u5927\u9a7c\u5cf0\u547d\u540d\u6cd5\uff09\u3001camelBack\uff08\u5c0f\u9a7c\u5cf0\u547d\u540d\u6cd5\uff09\u548clower_case\uff08\u5c0f\u5199\u547d\u540d\u6cd5\uff09\u3002\u6d89\u53ca\u5230\u7684\u68c0\u67e5\u9879\u4e3b\u8981\u6709ClassCase\uff08\u7c7b\uff09\u3001StructCase\uff08\u7ed3\u6784\u4f53\uff09\u3001TypedefCase\uff08Typedef\uff09\u3001EnumCase\uff08\u679a\u4e3e\uff09\u3001EnumConstantCase\uff08\u679a\u4e3e\u5e38\u91cf\uff09\u3001UnionCase(\u8054\u5408)\u3001NamespaceCase\uff08\u547d\u540d\u7a7a\u95f4\uff09\u3001FunctionCase\uff08\u51fd\u6570\uff09\u3001VariableCase\uff08\u53d8\u91cf\uff09\u3001ConstantCase\uff08\u5e38\u91cf\uff09\u3002<\/p>\n<p>2\u3001readability-function-size<\/p>\n<p>\u8fd9\u6761\u89c4\u5219\u662f\u7528\u6765\u68c0\u67e5\u51fd\u6570\u7684\u5927\u5c0f\u7684\u3002\u603b\u89c4\u5219\u4e4b\u4e0b\uff0c\u4e5f\u5305\u542b\u4e86\u4e24\u6761\u5177\u4f53\u7684\u68c0\u67e5\u9879\u3002<\/p>\n<p>key: readability-function-size.StatementThreshold<\/p>\n<p>value: 50<\/p>\n<p>\u68c0\u67e5\u51fd\u6570\u5185\u7684\u8bed\u53e5\u4e0d\u8d85\u8fc750\u6761\u3002\uff08\u5efa\u8bae8.1.1\uff09<\/p>\n<p>key: readability-function-size.ParameterThreshold<\/p>\n<p>value: 5<\/p>\n<p>\u68c0\u67e5\u51fd\u6570\u7684\u53c2\u6570\u4e0d\u8d85\u8fc75\u4e2a\u3002\uff08\u5efa\u8bae8.3.3\uff09<\/p>\n<p>3\u3001readability-braces-around-statements<\/p>\n<p>\u8bed\u53e5\u5fc5\u987b\u5728\u5927\u62ec\u53f7\u4e4b\u5185\u3002\u8fd9\u91cc\u4e3b\u8981\u662f\u9488\u5bf9\u6761\u4ef6\u5224\u65ad\u4e4b\u540e\u8fdb\u884c\u6267\u884c\u7684\u8bed\u53e5\uff0c\u6bd4\u5982if-else\uff0c\u54ea\u6015\u662f\u53ea\u6709\u4e00\u6761\u8bed\u53e5\uff0c\u4e5f\u5e94\u8be5\u5728\u62ec\u53f7\u4e4b\u5185\u3002\uff08\u89c4\u52193.6.1\uff0c\u89c4\u52193.7.1\uff09<\/p>\n<p>4\u3001readability-magic-numbers<\/p>\n<p>\u9b54\u9b3c\u6570\u5b57\u68c0\u67e5\u3002\uff08\u5efa\u8bae9.1.3\uff09<\/p>\n<p>5\u3001misc-unused-parameters<\/p>\n<p>\u672a\u4f7f\u7528\u51fd\u6570\u53c2\u6570\u68c0\u67e5\u3002<\/p>\n<p>6\u3001modernize-use-nullptr<\/p>\n<p>\u68c0\u67e5\u4f7f\u7528nullptr\uff0c\u800c\u4e0d\u662fNULL\u62160\u3002\uff08\u89c4\u521910.1.3\uff09<\/p>\n<p>7\u3001modernize-replace-auto-ptr<\/p>\n<p>\u68c0\u67e5\u7981\u6b62\u4f7f\u7528auto_ptr\u3002(\u89c4\u52199.5.2)<\/p>\n<p>8\u3001modernize-use-noexcept<\/p>\n<p>\u68c0\u67e5\u4e0d\u4f7f\u7528\u5f02\u5e38\u673a\u5236\u3002\uff08\u89c4\u521911.4.1\uff09<\/p>\n<p>9\u3001modernize-use-override<\/p>\n<p>\u68c0\u67e5\u91cd\u5199\u865a\u51fd\u6570\u65f6\u4f7f\u7528override\u5173\u952e\u5b57\u3002(\u89c4\u521910.1.1)<\/p>\n<p>10\u3001performance-move-const-arg<\/p>\n<p>\u68c0\u67e5\u7981\u6b62\u4f7f\u7528std::move\u64cd\u4f5cconst\u5bf9\u8c61\u3002\uff08\u89c4\u521910.1.4\uff09<\/p>\n<p>11\u3001cppcoreguidelines-pro-type-cstyle-cast<\/p>\n<p>\u4e0d\u4f7f\u7528C\u98ce\u683c\u8f6c\u6362\u68c0\u67e5\u3002(\u89c4\u52199.3.1)<\/p>\n<p>12\u3001cppcoreguidelines-pro-type-reinterpret-cast<\/p>\n<p>\u4e0d\u4f7f\u7528reinterpret_cast \u68c0\u67e5\u3002\uff08\u5efa\u8bae9.3.2\uff09<\/p>\n<p>13\u3001cppcoreguidelines-pro-type-const-cast<\/p>\n<p>\u4e0d\u4f7f\u7528const_cast\u68c0\u67e5\u3002\uff08\u5efa\u8bae9.3.3\uff09 \u4f5c\u8005\uff1a\u5c0f\u4e56\u4ed6\u7239-\u77e5\u4e4e https:\/\/www.bilibili.com\/read\/cv5153605 \u51fa\u5904\uff1abilibili<br \/>\n\u5177\u4f53\u811a\u672c\u5982\u4e0b\uff1a<\/p>\n<p>#\/bin\/bash<br \/>\n# check code quality with clang-tidy<br \/>\n#crystalmedia<\/p>\n<p>VERSION=debug<br \/>\ncd .\/${CI_PROJECT_NAME}_${VERSION}<br \/>\nDIR_BUILD=${PWD}<br \/>\necho PWD:${PWD}<br \/>\nls<\/p>\n<p>echo &#8220;${PROMPT}================= Check with Clang-tidy ======================&#8221;<br \/>\nchk_report=&#8221;${DIR_BUILD}\/clang-tidy-check.txt&#8221;<br \/>\nrm -f $chk_report<br \/>\ntouch $chk_report<br \/>\necho &#8220;${PROMPT}Report filename: $chk_report&#8221;<br \/>\necho &#8220;${PROMPT}Current directory: ${PWD}&#8221;<br \/>\necho &#8220;${PROMPT}Clang-tidy configuration:&#8221;<br \/>\nclang-tidy-8 -p ${DIR_BUILD} -dump-config<\/p>\n<p>fifo=&#8221;\/tmp\/$$.fifo&#8221; # a file for pipe<br \/>\nmkfifo $fifo # a pipe as the process queue<br \/>\nexec 6&lt;&gt;$fifo # attach fd #6 to the pipe<br \/>\nthread_num=8 # max processes number<br \/>\ncount=0;<\/p>\n<p>while [[ $count -lt $thread_num ]]; do<br \/>\necho &gt;&amp;6<br \/>\n#let count=count+1<br \/>\ncount=$((count + 1 ))<br \/>\ndone<\/p>\n<p>fail_flag_file=&#8221;.\/quality-failed.txt&#8221;<br \/>\nfile_list=$(find -name &#8216;*.cpp&#8217; )<br \/>\n#file_list=$(find \\( -path &#8216;.\/ptz&#8217; -o -path &#8216;.\/vcnkit&#8217; \\) -prune -o -name &#8216;*.cpp&#8217; -print)<br \/>\necho -e ${file_list}<br \/>\nfor src_file in $file_list; do<br \/>\nread -u6<br \/>\n{<br \/>\nif [[ $(basename $src_file) != moc_* &amp;&amp; $(basename $src_file) != test_* &amp;&amp; $(basename $src_file) != qrc_* &amp;&amp; $(basename $src_file) != AES* ]]; then<br \/>\ntime_start=$(date +%s)<br \/>\necho &#8220;${PROMPT}Analyzing: $src_file&#8221;<br \/>\neval &#8220;clang-tidy-8 &#8211;quiet -p .\/ $src_file &gt;&gt;$chk_report&#8221;<\/p>\n<p>[ $? -ne 0 ] &amp;&amp; echo &#8220;FAIL&#8221; &gt; $fail_flag_file<\/p>\n<p>time_end=$(date +%s)<br \/>\nduration=$[${time_end}-${time_start}]<br \/>\nif [[ ${print_job_duration} == &#8220;on&#8221; ]]; then<br \/>\necho ${PROMPT}&#8221;Task duration time: ${duration}&#8221;<br \/>\nfi<br \/>\nif [[ ${duration} -gt ${task_duration_limit} ]]; then<br \/>\necho ${PROMPT}&#8221;Warning: This task takes time more than ${task_duration_limit} seconds.&#8221;<br \/>\nfi<br \/>\nfi<br \/>\n# produce a cook<br \/>\necho &gt;&amp;6<br \/>\n}&amp;<br \/>\ndone<br \/>\nwait<br \/>\nexec 6&gt;&amp;-<br \/>\nrm $fifo<\/p>\n<p>if [ -f $fail_flag_file ];then<br \/>\nCHECK_PASS=NO<br \/>\nelse<br \/>\nCHECK_PASS=YES<br \/>\nfi<\/p>\n<p># check report<br \/>\n[ $(cat $chk_report | wc -l) -gt 0 ] &amp;&amp; CHECK_PASS=NO<\/p>\n<p>echo &#8220;${PROMPT}=================== Clang-tidy Report ========================&#8221;<br \/>\ncat $chk_report<br \/>\necho &#8220;${PROMPT}==============================================================&#8221;<\/p>\n<p>#cp $chk_report ${DIR_BUILD}\/*check.txt ${DIR_BUILD}\/<br \/>\n#cp ${DIR_BUILD}\/compile_commands.json ..\/compile_commands.json<\/p>\n<p># exit<br \/>\nif [ &#8220;#$CHECK_PASS&#8221; = &#8220;#NO&#8221; ]; then<br \/>\nexit 1;<br \/>\necho ${PROMPT}Cod Quality check failed.<br \/>\nfi<br \/>\nexit 0<\/p>\n<p>clang-tidy\u9759\u6001\u8bed\u4e49\u68c0\u67e5\uff0c\u5b89\u88c5\u3001\u4f7f\u7528\u3001\u68c0\u67e5\u9879\u6ce8\u89e3<\/p>\n<p>\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e0bclang\u4e0eLLVM\u4e4b\u95f4\u7684\u533a\u522b\u4e0e\u8054\u7cfb<br \/>\nLLVM\u4e0eClang\u7684\u6982\u8ff0\u53ca\u5173\u7cfb<\/p>\n<p>\u6df1\u5165\u6d45\u51fa\u8ba9\u4f60\u7406\u89e3\u4ec0\u4e48\u662fLLVM<\/p>\n<p>\u8fd8\u6709\u4e00\u4e2aclang-format<br \/>\n.clang-format<\/p>\n<p># Installation:<br \/>\n# sudo apt install clang-format-8<br \/>\n# Reference:<br \/>\n# http:\/\/clang.llvm.org\/docs\/ClangFormatStyleOptions.html<br \/>\n#<br \/>\n# Clang-format understands also special comments that switch formatting in a<br \/>\n# delimited range. The code between a comment:<br \/>\n# &#8216;\/\/ clang-format off&#8217; or &#8216;\/* clang-format off *\/&#8217; up to a comment<br \/>\n# &#8216;\/\/ clang-format on&#8217; or &#8216;\/* clang-format on *\/&#8217; will not be formatted.<br \/>\n# The comments themselves will be formatted (aligned) normally.<\/p>\n<p>BasedOnStyle: LLVM<\/p>\n<p>Cpp11BracedListStyle: true<\/p>\n<p>Language: Cpp<\/p>\n<p># Cpp03, Cpp11, Auto<br \/>\nStandard: Cpp14<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 &nbsp; C++\u57fa\u7840\u77e5\u8bc6 <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[24],"tags":[121],"class_list":["post-5748","post","type-post","status-publish","format-standard","hentry","category-value_docs","tag-05-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5748","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=5748"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5748\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=5748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=5748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=5748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}