{"id":5240,"date":"2021-05-16T12:56:02","date_gmt":"2021-05-16T04:56:02","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=5240"},"modified":"2021-05-16T12:56:02","modified_gmt":"2021-05-16T04:56:02","slug":"clang-tidy-%e9%9d%99%e6%80%81%e4%bb%a3%e7%a0%81%e5%88%86%e6%9e%90%e6%a1%86%e6%9e%b6","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/5240","title":{"rendered":"clang-tidy\u2014\u2014\u9759\u6001\u4ee3\u7801\u5206\u6790\u6846\u67b6"},"content":{"rendered":"<p class=\"h2 mb-3\"><a href=\"https:\/\/segmentfault.com\/a\/1190000007015981\">\u539f\u6587\u5730\u5740<\/a><\/p>\n<p>&nbsp;<\/p>\n<div class=\"d-flex align-items-center justify-content-between flex-wrap mb-4\">\n<div class=\"d-flex align-items-center\"><a class=\"d-flex align-items-center\" href=\"https:\/\/segmentfault.com\/u\/hokein\"><picture class=\"mr-2 d-sm-block d-none\"><img decoding=\"async\" class=\"d-inline-block rounded-circle\" src=\"https:\/\/avatar-static.segmentfault.com\/250\/343\/2503439943-1030000000639011_huge128\" alt=\"\" width=\"32\" \/><\/picture><strong class=\"align-self-center font-size-14\">hokein<\/strong><\/a><time class=\"text-secondary font-size-14\" datetime=\"2016-09-26T20:46:05.000Z\">\u53d1\u5e03\u4e8e\u00a02016-09-27<\/time><\/div>\n<\/div>\n<div id=\"OA_holder_25\" class=\"ad-container\"><\/div>\n<article class=\"article fmt article-content \"><strong>\u539f\u6587\u94fe\u63a5<\/strong>\uff1a<a href=\"https:\/\/hokein.github.io\/clang-tools-tutorial\" rel=\"nofollow\">https:\/\/hokein.github.io\/clan&#8230;<\/a><\/p>\n<h2 id=\"item-1\">clang-tidy\u4ecb\u7ecd<\/h2>\n<p><a href=\"http:\/\/clang.llvm.org\/extra\/clang-tidy\/\" rel=\"nofollow\">clang-tidy<\/a>\u662f\u4e00\u4e2a\u57fa\u4e8eclang\u7684\u9759\u6001\u4ee3\u7801\u5206\u6790\u6846\u67b6\uff0c\u652f\u6301C++\/C\/Objective-C\u3002<\/p>\n<p>\u5b83\u662f\u4e00\u4e2a\u529f\u80fd\u66f4\u5f3a\u5927\u7684lint\u5de5\u5177\u3002\u7edd\u5927\u90e8\u5206lint\u5de5\u5177\u53ea\u80fd\u5728\u51fa\u73b0\u95ee\u9898\u7684\u4ee3\u7801\u5730\u65b9\u7ed9\u51fa\u63d0\u793a\uff0c\u4e4b\u540e\u9700\u8981\u4eba\u4e3a\u4fee\u6539\uff0c\u800cclang-tidy\u5219\u80fd\u591f\u81ea\u52a8\u4fee\u590d\u529f\u80fd\uff08\u5f53\u7136\u8fd9\u4e2a\u5982\u4f55\u4fee\u590d\u9700\u8981\u8be5check\u4f5c\u8005\u63d0\u4f9b\uff09\uff1b\u5e76\u4e14clang-tidy\u91c7\u7528\u6a21\u5757\u5316\u8bbe\u8ba1\uff0c\u975e\u5e38\u5bb9\u6613\u6269\u5c55\u3002\u5982\u679c\u7528\u6237\u60f3\u5f80clang-tidy\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u68c0\u6d4b\u529f\u80fd\uff0c\u53ea\u9700\u8981\u7f16\u5199\u4e00\u4e2aclang-tidy check\u5b9e\u73b0\uff08\u5982\u4f55\u7f16\u5199\u4e00\u4e2aclang-tidy check\u4ee5\u540e\u5c06\u4f1a\u6709\u4e00\u7ae0\u8be6\u7ec6\u4ecb\u7ecd\uff09\uff0c\u6bcf\u4e00\u4e2acheck\u68c0\u6d4b\u4e00\u79cd\u95ee\u9898\uff0c\u4f8b\u5982\u68c0\u6d4b\u67d0\u4e2a\u8fdd\u53cdCode style\u7684\u6a21\u5f0f\uff0c\u68c0\u6d4b\u67d0\u4e9bAPI\u4e0d\u6b63\u786e\u4f7f\u7528\u7684\u65b9\u6cd5\u7b49\u7b49\u3002<\/p>\n<p>clang-tidy check\u53ef\u4ee5\u68c0\u6d4b\u5404\u5f0f\u5404\u6837\u7684\u95ee\u9898\uff1a<\/p>\n<ul>\n<li>\u68c0\u6d4b\u8fdd\u53cd\u4ee3\u7801\u89c4\u8303\u7684\u4ee3\u7801\u6a21\u5f0f\uff08header guard\u4e0d\u6ee1\u8db3\uff0cinclude\u5934\u6587\u4ef6\u987a\u5e8f\u9519\u8bef)\uff1b<\/li>\n<li>\u627e\u51fa\u4e0d\u5bb9\u6613\u5728\u7f16\u8bd1\u65f6\u53d1\u73b0\u7684\u4ee3\u7801\u9519\u8bef\uff08\u628aint\u8d4b\u503c\u7ed9std::string, \u53d8\u91cf\u5b9a\u4e49\u5728\u5934\u6587\u4ef6\uff09\uff1b<\/li>\n<li>\u628adeprecated\u7684API\u66ff\u6362\u6210\u65b0\u7684API\uff0cmodernize\u6a21\u5757\u5178\u578b\u4f8b\u5b50\uff0c\u628aC++03\u7684\u4ee3\u7801\u81ea\u52a8\u8f6c\u6362\u6210C++11\u7684\u4ee3\u7801\uff08for-range-loop, auto, nullptr, overriede, default\uff09\uff1b<\/li>\n<\/ul>\n<p>Google code style\u63d0\u4f9b\u4e86<a href=\"https:\/\/github.com\/google\/styleguide\/tree\/gh-pages\/cpplint\" rel=\"nofollow\">cpplint<\/a>\u811a\u672c\uff0c\u7528\u4e8e\u68c0\u6d4b\u4ee3\u7801\u4e2d\u8fdd\u53cdcode style\u7684\u5730\u65b9\u3002clang-tidy\u4e5f\u652f\u6301\u68c0\u6d4b\u4ee3\u7801\u4e2d\u8fdd\u53cdGoogle code style\u7684\u5730\u65b9(<code>google-*<\/code>check), \u5b83\u4eec\u4e4b\u95f4\u6709\u4ec0\u4e48\u533a\u522b\uff1fcpplint\u662f\u4e00\u4e2apython\u7684\u811a\u672c\uff0c\u5b83\u662f\u91c7\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u51fa\u8fdd\u53cdcode style\u7684\u4ee3\u7801, \u6240\u4ee5\u5b83\u80fd\u68c0\u6d4b\u7684\u529f\u80fd\u4f1a\u53d7\u9650\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u5b83\u4e0d\u80fd\u591f\u68c0\u6d4b\u6240\u6709\u7684\u8fdd\u53cdcode style\u7684\u5730\u65b9\uff0c\u5e76\u4e14\u8fd8\u4f1a\u6709False positive\u548cTrue positive\uff1b\u800cclang-tidy\u5219\u662f\u57fa\u4e8e\u62bd\u8c61\u8bed\u6cd5\u6811(AST)\u5bf9\u6e90\u6587\u4ef6\u8fdb\u884c\u5206\u6790\uff0c\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u662f\u5728\u5206\u6790\u7684\u7ed3\u679c\u66f4\u52a0\u51c6\u786e\uff0c\u80fd\u68c0\u6d4b\u7684\u95ee\u9898\u4e5f\u66f4\u591a\u3002<\/p>\n<p>\u8fd9\u4e24\u8005\u5404\u6709\u4f18\u7f3a\u70b9\uff0cclang-tidy\u9700\u8981\u5bf9\u6e90\u4ee3\u7801\u8fdb\u884c\u8bed\u6cd5\u5206\u6790\uff08\u7f16\u8bd1\u6e90\u6587\u4ef6\uff09\uff0c\u6240\u4ee5\u5b83\u9700\u8981\u77e5\u9053\u6e90\u6587\u4ef6\u7684\u7f16\u8bd1\u547d\u4ee4\uff0c\u5bf9\u4e8e\u4f9d\u8d56\u8f83\u591a\u8f83\u5927\u7684\u6587\u4ef6\uff0c\u82b1\u8d39\u65f6\u95f4\u4f1a\u8f83\u957f\uff1bcpplint\u5219\u4e0d\u9700\u8981\u6e90\u6587\u4ef6\u7f16\u8bd1\uff0c\u5bf9\u6587\u4ef6\u5185\u5bb9\u8fdb\u884c\u6b63\u5219\u5339\u914d\uff0c\u8fd0\u884c\u4f1a\u66f4\u5feb\u3002<\/p>\n<p>clang-tidy\u6bcf\u6b21\u53ea\u9488\u5bf9\u4e00\u4e2a\u7f16\u8bd1\u5355\u5143(TranslationUnit, \u53ef\u7b80\u5355\u7406\u89e3\u62101\u4e2a<code>.cpp<\/code>\u6587\u4ef6)\u8fdb\u884c\u9759\u6001\u5206\u6790\uff0c\u56e0\u6b64\uff0c\u5b83\u53ea\u80fd\u67e5\u627e\u51fa\u4e00\u4e2a\u7f16\u8bd1\u5355\u5143\u91cc\u9762\u7684\u4ee3\u7801\u95ee\u9898\uff0c\u5bf9\u4e8e\u90a3\u79cd\u53ea\u5728\u8de8\u7f16\u8bd1\u5355\u5143\u51fa\u73b0\u7684\u95ee\u9898\uff0c\u5c31\u65e0\u80fd\u4e3a\u529b\u4e86\u3002<\/p>\n<p>\u73b0\u5728clang-tidy\u5b9e\u73b0\u6709100+\u4e2acheck\uff0c\u8bf7\u67e5\u770blist\u5217\u8868\u3002\u6839\u636echeck\u4e0d\u540c\u79cd\u7c7b(\u4ececheck\u540d\u5b57\u7684\u524d\u7f00\u5c31\u80fd\u77e5\u9053\u54ea\u4e00\u7c7b)\uff0c\u5206\u4e3a\u5982\u4e0b\u51e0\u5927\u7c7b\uff1a<\/p>\n<ul>\n<li>boost \u68c0\u6d4bboost\u5e93API\u4f7f\u7528\u95ee\u9898<\/li>\n<li>cert \u68c0\u6d4b<a href=\"https:\/\/www.securecoding.cert.org\/confluence\/display\/seccode\/SEI+CERT+Coding+Standards\" rel=\"nofollow\">CERT<\/a>\u7684\u4ee3\u7801\u89c4\u8303<\/li>\n<li>cpp-core-guidelines \u68c0\u6d4b\u662f\u5426\u8fdd\u53cdcpp-core-guidelines<\/li>\n<li>google \u68c0\u6d4b\u662f\u5426\u8fdd\u53cdgoogle code style<\/li>\n<li>llvm \u68c0\u6d4b\u662f\u5426\u8fdd\u53cdllvm code style<\/li>\n<li>readability \u68c0\u6d4b\u4ee3\u7801\u4e0a\u76f8\u5173\u95ee\u9898\uff0c\u4f46\u53c8\u4e0d\u660e\u786e\u5c5e\u4e8e\u4efb\u4f55\u4ee3\u7801\u89c4\u8303\u7684<\/li>\n<li>misc \u5176\u5b83\u4e00\u4e9b\u96f6\u788e\u7684check<\/li>\n<li>mpi \u68c0\u6d4bMPI API\u95ee\u9898<\/li>\n<li>modernize \u628aC++03\u4ee3\u7801\u8f6c\u6362\u6210C++11\u4ee3\u7801\uff0c\u4f7f\u7528C++11\u65b0\u7279\u6027<\/li>\n<li>performance \u68c0\u6d4bperformance\u76f8\u5173\u95ee\u9898<\/li>\n<\/ul>\n<h2 id=\"item-2\">clang-tidy\u4f7f\u7528<\/h2>\n<p>clang-tidy\u662f\u4e00\u4e2a\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u5b98\u7f51\u4e0b\u8f7d\u7f16\u8bd1\u597d\u7684\u4e8c\u8fdb\u5236\u5305\u3002<br \/>\n\u6211\u5199\u4e86\u4e00\u4e2a<a href=\"https:\/\/www.npmjs.com\/package\/clang-tools-prebuilt\" rel=\"nofollow\">clang-tools-prebuilt<\/a>\u7684npm\u5305\uff0c\u53ef\u4ee5\u901a\u8fc7<code>npm install clang-tools-prebuilt<\/code>\u7684\u547d\u4ee4\u4e0b\u8f7d\uff08\u4ec5\u652f\u6301MacOS, Linux\uff09\u3002<\/p>\n<pre class=\"bash hljs\"><code class=\"bash\">\/\/ \u5217\u51fa\u6240\u6709\u7684check\r\n$ clang-tidy -list-checks\r\n\/\/ \u627e\u51fasimple.cc\u4e2d\u6240\u6709\u6ca1\u6709\u7528\u5230\u7684using declarations. \u540e\u9762\u7684`--`\u8868\u793a\u8fd9\u4e2a\u6587\u4ef6\u4e0d\u5728compilation database\u91cc\u9762\uff0c\u53ef\u4ee5\u76f4\u63a5\u5355\u72ec\u7f16\u8bd1\uff1b\r\n$ clang-tidy -checks=<span class=\"hljs-string\">\"-*,misc-unused-using-decls\"<\/span> path\/to\/simple.cc --\r\n\r\n\/\/ \u627e\u51fasimple.cc\u4e2d\u6240\u6709\u6ca1\u6709\u7528\u5230\u7684using declarations\u5e76\u81ea\u52a8fix(\u5220\u9664\u6389)\r\n$ clang-tidy -checks=<span class=\"hljs-string\">\"-*,misc-unused-using-decls\"<\/span> -fix path\/to\/simple.cc --\r\n\r\n\/\/ \u627e\u51faa.c\u4e2d\u6ca1\u6709\u7528\u5230\u7684using declarations. \u8fd9\u91cc\u9700\u8981path\/to\/project\/compile_commands.json\u5b58\u5728\r\n$ clang-tidy -checks=<span class=\"hljs-string\">\"-*,misc-unused-using-decls\"<\/span> path\/to\/project\/a.cc<\/code><\/pre>\n<p>\u5206\u6790\u9879\u76ee\u4e2d\u7684y\u7f16\u8bd1\u5355\u5143\uff0cclang-tidy\u9996\u5148\u8981\u77e5\u9053\u5982\u4f55\u7f16\u8bd1\u5355\u5143(\u8be5\u7f16\u8bd1\u5355\u5143\u7684\u7f16\u8bd1\u547d\u4ee4)\uff0c\u5b83\u4ece\u76ee\u5f55\u4e0b\u67e5\u627ecompliation database\uff0c\u8fd9\u4e2adatabase\u5c31\u662f<code>compile_commands.json<\/code>\u6587\u4ef6\uff0c\u91cc\u9762\u5305\u542b\u8be5\u9879\u76ee\u4e2d\u6240\u6709\u7684\u7f16\u8bd1\u5355\u5143\u7684\u7f16\u8bd1\u547d\u4ee4\u3002\u5728\u4f7f\u7528\u4e4b\u524d\u8981\u5bfc\u51fa\u8fd9\u4e2a\u6587\u4ef6\u3002\u76ee\u524d\u5df2\u7ecf\u6709\u5de5\u5177\u5e2e\u6211\u4eec\u505a\u4e86\u8fd9\u9879\u5de5\u4f5c\u3002<\/p>\n<ul>\n<li>\u5982\u679c\u662fcmake\u7684\u9879\u76ee\uff0c\u901a\u8fc7<code>cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON path\/to\/llvm\/sources<\/code>\u547d\u4ee4\u5bfc\u51fa;<\/li>\n<li>\u5982\u679c\u662fGYP\u9879\u76ee\uff0c\u901a\u8fc7<code>ninja -C out\/D -t compdb cc cxx objc objcxx &gt; compile_commands.json<\/code>\uff1b<\/li>\n<li>\u5982\u679c\u662fmake\u9879\u76ee\uff0c\u4f7f\u7528<a href=\"https:\/\/github.com\/rizsotto\/Bear\" rel=\"nofollow\">Bear<\/a>\u5de5\u5177\uff1b<\/li>\n<\/ul>\n<p>\u4e00\u6b21\u547d\u4ee4\u53ea\u80fd\u5206\u6790\u4e00\u4e2a\u6587\u4ef6\u592a\u9ebb\u70e6\uff0c\u8981\u5206\u6790\u6574\u4e2a\u9879\u76ee\u7684\u6240\u6709\u6587\u4ef6\uff1f<a href=\"https:\/\/github.com\/llvm-mirror\/clang-tools-extra\/blob\/master\/clang-tidy\/tool\/run-clang-tidy.py\" rel=\"nofollow\">run_clang_tidy.py<\/a>\u811a\u672c\u6b63\u662f\u4f60\u60f3\u8981\u7684\uff0c\u901a\u8fc7\u591a\u8fdb\u7a0b\u7684\u65b9\u6cd5\u5bf9\u6574\u4e2a\u9879\u76ee\u6587\u4ef6\u8fdb\u884c\u5206\u6790\u3002<\/p>\n<\/article>\n<div class=\"d-flex flex-wrap align-items-center \">\n<div class=\"m-n1\"><a class=\"m-1 badge-tag\" href=\"https:\/\/segmentfault.com\/t\/clang\">clang<\/a><a class=\"m-1 badge-tag\" href=\"https:\/\/segmentfault.com\/t\/c%2B%2B\">c++<\/a><\/div>\n<\/div>\n<div class=\"my-4\">\n<div id=\"OA_holder_3\" class=\"ad-container\"><\/div>\n<\/div>\n<div class=\"text-secondary font-size-14 mt-3 mb-5 d-flex justify-content-between row\">\n<div class=\"col\">\u9605\u8bfb\u00a017.4k<time datetime=\"2016-09-26T20:46:05.000Z\">\u66f4\u65b0\u4e8e\u00a02016-09-28<\/time><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 &nbsp; hokein\u53d1\u5e03<\/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":[],"tags":[118],"class_list":["post-5240","post","type-post","status-publish","format-standard","hentry","tag-02-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5240","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=5240"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5240\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=5240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=5240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=5240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}