-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang-tidy] Add a fully custom message to bugprone-unsafe-functions
#162443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,6 +69,11 @@ Potentially Breaking Changes | |
| - `CharTypdefsToIgnore` to `CharTypedefsToIgnore` in | ||
| :doc:`bugprone-signed-char-misuse | ||
| <clang-tidy/checks/bugprone/signed-char-misuse>` | ||
|
|
||
| - Modified the custom message format of :doc:`bugprone-unsafe-functions | ||
| <clang-tidy/checks/bugprone/unsafe-functions>` by hiding the default suffix | ||
| when the reason starts with the character `>` in the `CustomFunctions` option. | ||
| The warning locations are not changed, but the message is different. | ||
|
Comment on lines
+73
to
+76
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps highlight that this only changes the behavior in the unlikely case when the configured reason already started with |
||
|
|
||
| Improvements to clangd | ||
| ---------------------- | ||
|
|
@@ -314,6 +319,11 @@ Changes in existing checks | |
| <clang-tidy/checks/bugprone/unhandled-self-assignment>` check by adding | ||
| an additional matcher that generalizes the copy-and-swap idiom pattern | ||
| detection. | ||
|
|
||
| - Improved :doc:`bugprone-unsafe-functions | ||
Discookie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <clang-tidy/checks/bugprone/unsafe-functions>` check by hiding the default | ||
| suffix when the reason starts with the character `>` in the `CustomFunctions` | ||
| option. | ||
|
|
||
| - Improved :doc:`cppcoreguidelines-init-variables | ||
| <clang-tidy/checks/cppcoreguidelines/init-variables>` check by fixing the | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,8 +80,8 @@ including any system headers. | |
| Custom functions | ||
| ---------------- | ||
|
|
||
| The option :option:`CustomFunctions` allows the user to define custom functions to be | ||
| checked. The format is the following, without newlines: | ||
| The option :option:`CustomFunctions` allows the user to define custom functions | ||
| to be checked. The format is the following, without newlines: | ||
|
|
||
| .. code:: | ||
|
|
||
|
|
@@ -97,33 +97,61 @@ The functions are matched using POSIX extended regular expressions. | |
| The `reason` is optional and is used to provide additional information about the | ||
| reasoning behind the replacement. The default reason is `is marked as unsafe`. | ||
|
|
||
| If `replacement` is empty, the text `it should not be used` will be shown | ||
| instead of the suggestion for a replacement. | ||
| If `replacement` is empty, the default text `it should not be used` will be | ||
| shown instead of the suggestion for a replacement. | ||
|
|
||
| As an example, the configuration `^original$, replacement, is deprecated;` | ||
| will produce the following diagnostic message. | ||
| If the `reason` starts with the character `>`, the reason becomes fully custom. | ||
| The default suffix is disabled even if a `replacement` is present, and only the | ||
| reason message is shown after the matched function, to allow better control over | ||
| the suggestions. The starting `>` character and the preceding spaces are trimmed | ||
| from the message. | ||
|
Comment on lines
+106
to
+107
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you mean by "preceding spaces" here? Spaces preceding the ">" mark or spaces between the ">" and the message? |
||
|
|
||
| As an example, the following configuration matches only the function `original` | ||
| in the default namespace. A similar diagnostic can also be printed using a fully | ||
| custom reason. | ||
|
|
||
| .. code:: c | ||
|
|
||
| // bugprone-unsafe-functions.CustomFunctions: | ||
| // ^original$, replacement, is deprecated; | ||
| // Using the fully custom message syntax: | ||
| // ^original$,,> is deprecated, 'replacement' should be used instead; | ||
|
|
||
| original(); // warning: function 'original' is deprecated; 'replacement' should be used instead. | ||
| original(); // warning: function 'original' is deprecated; 'replacement' should be used instead | ||
| ::std::original(); // no-warning | ||
| original_function(); // no-warning | ||
|
|
||
| If the regular expression contains the character `:`, it is matched against the | ||
| qualified name (i.e. ``std::original``), otherwise the regex is matched against the unqualified name (``original``). | ||
| If the regular expression starts with `::` (or `^::`), it is matched against the | ||
| fully qualified name (``::std::original``). | ||
| qualified name (i.e. ``std::original``), otherwise the regex is matched against | ||
| the unqualified name (``original``). If the regular expression starts with `::` | ||
| (or `^::`), it is matched against the fully qualified name | ||
| (``::std::original``). | ||
|
|
||
| One of the use cases for fully custom messages is suggesting compiler options | ||
| and warning flags: | ||
|
|
||
| .. code:: c | ||
|
|
||
| // bugprone-unsafe-functions.CustomFunctions: | ||
| // ^memcpy$,,>is recommended to have compiler hardening using '_FORTIFY_SOURCE'; | ||
| // ^printf$,,>is recommended to have the '-Werror=format-security' compiler warning flag; | ||
|
|
||
| memcpy(dest, src, 999'999); // warning: function 'memcpy' is recommended to have compiler hardening using '_FORTIFY_SOURCE' | ||
| printf(raw_str); // warning: function 'printf' is recommended to have the '-Werror=format-security' compiler warning flag | ||
|
|
||
| The | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be removed? |
||
|
|
||
| .. note:: | ||
|
|
||
| Fully qualified names can contain template parameters on certain C++ classes, but not on C++ functions. | ||
| Type aliases are resolved before matching. | ||
| Fully qualified names can contain template parameters on certain C++ classes, | ||
| but not on C++ functions. Type aliases are resolved before matching. | ||
|
|
||
| As an example, the member function ``open`` in the class ``std::ifstream`` | ||
| has a fully qualified name of ``::std::basic_ifstream<char>::open``. | ||
|
|
||
| The example could also be matched with the regex ``::std::basic_ifstream<[^>]*>::open``, which matches all potential | ||
| template parameters, but does not match nested template classes. | ||
| The example could also be matched with the regex | ||
| ``::std::basic_ifstream<[^>]*>::open``, which matches all potential template | ||
| parameters, but does not match nested template classes. | ||
|
|
||
| Options | ||
| ------- | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here and elsewhere ensure that code fragments are surrounded by doubled backtick characters (two before the code fragment + two after it) because in RST single backticks creates links and you need doubled backticks to use monospace font.
Note that this differs from markdown (e.g. github comments) where monospace (code) text is marked by single backticks (while links use a completely different format:
[text](url)).