How do I pass GslHeader argument to clang-tidy while using -fix option?
Clash Royale CLAN TAG#URR8PPP
How do I pass GslHeader argument to clang-tidy while using -fix option?
I couldn't find sample code for a clang-tidy command line for fixing the following error:
[archlinux@thinkpad fizzbuzz]$ clang-tidy fizzbuzz2.cpp -checks=cppcoreguidelines-pro-bounds-constant-array-index
5 warnings generated.
fizzbuzz2.cpp:21:18: warning: do not use array subscript when the index is not an integer constant expression; use gsl::at() instead [cppcoreguidelines-pro-bounds-constant-array-index]
std::cout << arr[index] << std::endl;
^
Suppressed 4 warnings (4 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.
[archlinux@thinkpad fizzbuzz]$
I just want to find out how I can get clang-tidy to fix this automatically. If not, how should I use gsl::at()
to fix this? The clang-tidy documentation says the following:
gsl::at()
cppcoreguidelines-pro-bounds-constant-array-index
This check flags all array subscript expressions on static arrays and std::arrays that either do not have a constant integer expression index or are out of bounds (for std::array). For out-of-bounds checking of static arrays, see the -Warray-bounds Clang diagnostic.
This rule is part of the “Bounds safety” profile of the C++ Core Guidelines, see https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Pro-bounds-arrayindex.
Options
GslHeader
The check can generate fixes after this option has been set to the name of the include file that contains gsl::at(), e.g. “gsl/gsl.h”.
IncludeStyle
A string specifying which include-style is used, llvm or google. Default is llvm.
I found the gsl::at()
subroutine in <gsl/gsl_util>
. How do I tell clang-tidy to use it to fix my warning?
gsl::at()
<gsl/gsl_util>
Edit: I looked at the config string and found the solution to this:
On running:
clang-tidy fizzbuzz2.cpp -checks=cppcoreguidelines-pro-bounds-constant-array-index -dump-config
clang-tidy fizzbuzz2.cpp -checks=cppcoreguidelines-pro-bounds-constant-array-index -dump-config
I got some hints towards the solution
clang-tidy fizzbuzz2.cpp -checks=cppcoreguidelines-pro-bounds-constant-array-index -config="CheckOptions: [key: cppcoreguidelines-pro-bounds-constant-array-index.GslHeader, value: gsl/gsl_util]" -fix
clang-tidy fizzbuzz2.cpp -checks=cppcoreguidelines-pro-bounds-constant-array-index -config="CheckOptions: [key: cppcoreguidelines-pro-bounds-constant-array-index.GslHeader, value: gsl/gsl_util]" -fix
1 Answer
1
Edit: I looked at the config string and found the solution to this:
On running:
clang-tidy fizzbuzz2.cpp -checks=cppcoreguidelines-pro-bounds-constant-array-index -dump-config
I got some hints towards the solution
clang-tidy fizzbuzz2.cpp -checks=cppcoreguidelines-pro-bounds-constant-array-index -config="CheckOptions: [key: cppcoreguidelines-pro-bounds-constant-array-index.GslHeader, value: gsl/gsl_util]" -fix
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.