Skip to content

Commit 40645ed

Browse files
DiscookieNagyDonat
andauthored
[clang-tidy] Add a fully custom message to bugprone-unsafe-functions (#162443)
In some cases, such as when recommending the compiler option _FORTIFY_SOURCE, the current custom message format is clunky. Now, when the reason starts with `>`, the replacement string is omitted., so only the Reason is shown. `^function$,,has a custom message;` - function 'function' has a custom message; it should not be used `^function$,,>has a custom message and no replacement suggestion;` - function 'function' has a custom message and no replacement suggestion --------- Co-authored-by: Donát Nagy <donat.nagy@ericsson.com>
1 parent 2ede6af commit 40645ed

File tree

4 files changed

+63
-20
lines changed

4 files changed

+63
-20
lines changed

clang-tools-extra/clang-tidy/bugprone/UnsafeFunctionsCheck.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,20 @@ void UnsafeFunctionsCheck::check(const MatchFinder::MatchResult &Result) {
301301
if (Custom) {
302302
for (const auto &Entry : CustomFunctions) {
303303
if (Entry.Pattern.match(*FuncDecl)) {
304-
const StringRef Reason =
304+
StringRef Reason =
305305
Entry.Reason.empty() ? "is marked as unsafe" : Entry.Reason.c_str();
306306

307-
if (Entry.Replacement.empty()) {
307+
// Omit the replacement, when a fully-custom reason is given.
308+
if (Reason.consume_front(">")) {
309+
diag(SourceExpr->getExprLoc(), "function %0 %1")
310+
<< FuncDecl << Reason.trim() << SourceExpr->getSourceRange();
311+
// Do not recommend a replacement when it is not present.
312+
} else if (Entry.Replacement.empty()) {
308313
diag(SourceExpr->getExprLoc(),
309314
"function %0 %1; it should not be used")
310315
<< FuncDecl << Reason << Entry.Replacement
311316
<< SourceExpr->getSourceRange();
317+
// Otherwise, emit the replacement.
312318
} else {
313319
diag(SourceExpr->getExprLoc(),
314320
"function %0 %1; '%2' should be used instead")

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ Potentially Breaking Changes
6969
- `CharTypdefsToIgnore` to `CharTypedefsToIgnore` in
7070
:doc:`bugprone-signed-char-misuse
7171
<clang-tidy/checks/bugprone/signed-char-misuse>`
72+
73+
- Modified the custom message format of :doc:`bugprone-unsafe-functions
74+
<clang-tidy/checks/bugprone/unsafe-functions>` by assigning a special meaning
75+
to the character ``>`` at the start of the value of the option
76+
``CustomFunctions``. If the option value starts with ``>``, then the
77+
replacement suggestion part of the message (which would be included by
78+
default) is omitted. (This does not change the warning locations.)
7279

7380
- :program:`clang-tidy` now displays warnings from all non-system headers by
7481
default. Previously, users had to explicitly opt-in to header warnings using
@@ -387,6 +394,11 @@ Changes in existing checks
387394
<clang-tidy/checks/bugprone/unhandled-self-assignment>` check by adding
388395
an additional matcher that generalizes the copy-and-swap idiom pattern
389396
detection.
397+
398+
- Improved :doc:`bugprone-unsafe-functions
399+
<clang-tidy/checks/bugprone/unsafe-functions>` check by hiding the default
400+
suffix when the reason starts with the character `>` in the `CustomFunctions`
401+
option.
390402

391403
- Improved :doc:`cppcoreguidelines-avoid-non-const-global-variables
392404
<clang-tidy/checks/cppcoreguidelines/avoid-non-const-global-variables>` check

clang-tools-extra/docs/clang-tidy/checks/bugprone/unsafe-functions.rst

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -96,37 +96,62 @@ to be checked. The format is the following, without newlines:
9696
The functions are matched using POSIX extended regular expressions.
9797
*(Note: The regular expressions do not support negative* ``(?!)`` *matches.)*
9898

99-
The `reason` is optional and is used to provide additional information
100-
about the reasoning behind the replacement. The default reason is
101-
`is marked as unsafe`.
99+
The ``reason`` is optional and is used to provide additional information about the
100+
reasoning behind the replacement. The default reason is ``is marked as unsafe``.
102101

103-
If `replacement` is empty, the text `it should not be used` will be shown
104-
instead of the suggestion for a replacement.
102+
If ``replacement`` is empty, the default text ``it should not be used`` will be
103+
shown instead of the suggestion for a replacement.
105104

106-
As an example, the configuration `^original$, replacement, is deprecated;`
107-
will produce the following diagnostic message.
105+
If the ``reason`` starts with the character ``>``, the reason becomes fully custom.
106+
The default suffix is disabled even if a ``replacement`` is present, and only the
107+
reason message is shown after the matched function, to allow better control over
108+
the suggestions. (The starting ``>`` and whitespace directly after it are
109+
trimmed from the message.)
110+
111+
As an example, the following configuration matches only the function ``original``
112+
in the default namespace. A similar diagnostic can also be printed using a fully
113+
custom reason.
108114

109115
.. code:: c
110116
117+
// bugprone-unsafe-functions.CustomFunctions:
118+
// ^original$, replacement, is deprecated;
119+
// Using the fully custom message syntax:
120+
// ^suspicious$,,> should be avoided if possible.
111121
original(); // warning: function 'original' is deprecated; 'replacement' should be used instead.
122+
suspicious(); // warning: function 'suspicious' should be avoided if possible.
112123
::std::original(); // no-warning
113124
original_function(); // no-warning
114125
115-
If the regular expression contains the character `:`, it is matched against the
116-
qualified name (i.e. ``std::original``), otherwise the regex is matched against the unqualified name (``original``).
117-
If the regular expression starts with `::` (or `^::`), it is matched against the
118-
fully qualified name (``::std::original``).
126+
If the regular expression contains the character ``:``, it is matched against the
127+
qualified name (i.e. ``std::original``), otherwise the regex is matched against
128+
the unqualified name (``original``). If the regular expression starts with ``::``
129+
(or ``^::``), it is matched against the fully qualified name
130+
(``::std::original``).
131+
132+
One of the use cases for fully custom messages is suggesting compiler options
133+
and warning flags:
134+
135+
.. code:: c
136+
137+
// bugprone-unsafe-functions.CustomFunctions:
138+
// ^memcpy$,,>is recommended to have compiler hardening using '_FORTIFY_SOURCE';
139+
// ^printf$,,>is recommended to have the '-Werror=format-security' compiler warning flag;
140+
141+
memcpy(dest, src, 999'999); // warning: function 'memcpy' is recommended to have compiler hardening using '_FORTIFY_SOURCE'
142+
printf(raw_str); // warning: function 'printf' is recommended to have the '-Werror=format-security' compiler warning flag
119143
120144
.. note::
121145

122-
Fully qualified names can contain template parameters on certain C++ classes, but not on C++ functions.
123-
Type aliases are resolved before matching.
146+
Fully qualified names can contain template parameters on certain C++ classes,
147+
but not on C++ functions. Type aliases are resolved before matching.
124148

125149
As an example, the member function ``open`` in the class ``std::ifstream``
126150
has a fully qualified name of ``::std::basic_ifstream<char>::open``.
127151

128-
The example could also be matched with the regex ``::std::basic_ifstream<[^>]*>::open``, which matches all potential
129-
template parameters, but does not match nested template classes.
152+
The example could also be matched with the regex
153+
``::std::basic_ifstream<[^>]*>::open``, which matches all potential template
154+
parameters, but does not match nested template classes.
130155

131156
Options
132157
-------

clang-tools-extra/test/clang-tidy/checkers/bugprone/unsafe-functions-custom.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %check_clang_tidy -check-suffix=NON-STRICT-REGEX %s bugprone-unsafe-functions %t --\
2-
// RUN: -config="{CheckOptions: {bugprone-unsafe-functions.CustomFunctions: '::name_match,replacement,is a qualname match;^::prefix_match,,is matched on qualname prefix'}}"
2+
// RUN: -config="{CheckOptions: {bugprone-unsafe-functions.CustomFunctions: \"::name_match,,>is a qualname match, but with a fully 'custom' message;^::prefix_match,,is matched on qualname prefix\"}}"
33
// RUN: %check_clang_tidy -check-suffix=STRICT-REGEX %s bugprone-unsafe-functions %t --\
44
// RUN: -config="{CheckOptions: {bugprone-unsafe-functions.CustomFunctions: '^name_match$,replacement,is matched on function name only;^::prefix_match$,,is a full qualname match'}}"
55

@@ -11,14 +11,14 @@ void prefix_match_regex();
1111

1212
void f1() {
1313
name_match();
14-
// CHECK-MESSAGES-NON-STRICT-REGEX: :[[@LINE-1]]:3: warning: function 'name_match' is a qualname match; 'replacement' should be used instead
14+
// CHECK-MESSAGES-NON-STRICT-REGEX: :[[@LINE-1]]:3: warning: function 'name_match' is a qualname match, but with a fully 'custom' message
1515
// CHECK-MESSAGES-STRICT-REGEX: :[[@LINE-2]]:3: warning: function 'name_match' is matched on function name only; 'replacement' should be used instead
1616
prefix_match();
1717
// CHECK-MESSAGES-NON-STRICT-REGEX: :[[@LINE-1]]:3: warning: function 'prefix_match' is matched on qualname prefix; it should not be used
1818
// CHECK-MESSAGES-STRICT-REGEX: :[[@LINE-2]]:3: warning: function 'prefix_match' is a full qualname match; it should not be used
1919

2020
name_match_regex();
21-
// CHECK-MESSAGES-NON-STRICT-REGEX: :[[@LINE-1]]:3: warning: function 'name_match_regex' is a qualname match; 'replacement' should be used instead
21+
// CHECK-MESSAGES-NON-STRICT-REGEX: :[[@LINE-1]]:3: warning: function 'name_match_regex' is a qualname match, but with a fully 'custom' message
2222
// no-warning STRICT-REGEX
2323

2424
prefix_match_regex();

0 commit comments

Comments
 (0)