In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to write elegant C++ code", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to write elegant C++ code.
Elegant code is inseparable from static code review tools. You may use cppcheck more often, but today I would like to share with you another static code review tool, clang-tidy.
Different from cppcheck using regular expressions for static code analysis, clang-tidy is a static code checking tool based on the parsing tree. Although its speed is slower than regular expressions, it checks more accurately and comprehensively, and it can not only do static checking, but also do some repair work to add some custom checking rules.
Don't say much, go to the code:
# include int main () {int a = 1.2; return 0;}
There is implicit type conversion, which can be detected using 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.
Here you may have a question, this is just a common compilation warning, normal use of the compiler can also be checked out, then look at another piece of code:
# include int main () {char* d = NULL; return 0;}
We all know that we should use nullptr rather than NULL in C++. Here we use NULL instead of nullptr. Maybe we didn't notice this usage during development, so clang-tidy came in handy:
~ / 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; ^ ~ ~ nullptr 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.
Let me give you another example:
Struct Base {virtual void func () {}}; struct Derive: Base {virtual void func () {}}
At first glance, we may not have any problem. In fact, the derived class inherits the parent class in Category 11. When some functions are rewritten, it is best to add the override keyword, which can still be detected by 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 () {~ ^ override 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.
The tool can also check whether the code conforms to the coding specification, such as Google coding specification, etc., and look at the relevant code of this header file:
# include # include # include
Actually, there is a little problem here. The referencing order of header files does not meet the coding specification. In fact, clang-format can detect it here, but clang-tidy can also detect it, and it can also be repaired automatically through-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 ^ ~ 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
It can also detect hidden memory leaks:
Int main () {char* ct = (char*) malloc (323); return 0;}
This is the test result using 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; ^ / 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 ^ ~ 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; ^ / 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; ^ ~ static_cast () / 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; ^ / 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 also has a lot of high-end features, which can detect about 250 problems, which are mainly divided into several categories:
Abseil: problems related to detecting abseil libraries
Android: detect problems related to Android
Boost: problems related to detecting boost libraries
Cert: code specification for detecting CERT
Cpp-core-guidelines: detect if cpp-core-guidelines is violated
Google: detection of violations of google coding specifications
Llvm: detection of violations of llvm coding specifications
Performance: detect performance-related issu
Readability: detection of issues related to readability but not to some coding specifications
Modernize: detect whether or not to use code issues related to modern Category 11
And suitable for Windows/Linux/MacOS multi-platform, but also supports the command line, CLion/VSCode/VSStudio plug-ins, etc., detection rules can also be customized, the important thing is free and open source, quickly use it, write elegant C++ code ~
At this point, I believe you have a deeper understanding of "how to write elegant C++ code". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.