Skip to content

Commit 1096c52

Browse files
authored
merge main into amd-staging (#616)
2 parents e9b3d79 + 2ee4a8f commit 1096c52

File tree

130 files changed

+4939
-997
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+4939
-997
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();

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ Bug Fixes to Attribute Support
743743
- Fixes crashes or missing diagnostics with the `device_kernel` attribute. (#GH161905)
744744
- Fix handling of parameter indexes when an attribute is applied to a C++23 explicit object member function.
745745
- Fixed several false positives and false negatives in function effect (`nonblocking`) analysis. (#GH166078) (#GH166101) (#GH166110)
746+
- Fix ``cleanup`` attribute by delaying type checks until after the type is deduced. (#GH129631)
746747

747748
Bug Fixes to C++ Support
748749
^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/Basic/Attr.td

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,17 @@ class Attr {
741741
// our existing general parsing we need to have a separate flag that
742742
// opts an attribute into strict parsing of attribute parameters
743743
bit StrictEnumParameters = 0;
744+
// Set to true for attributes which have Sema checks which requires the type
745+
// to be deduced.
746+
// When `IsTypeDependent` is set to true, you should add an `ActOn*Attr`
747+
// function to `Sema.h`. The signature of the function must be:
748+
// `void ActOn*Attr(Decl *, const Attr *);` where the `Decl *` is the
749+
// declaration the attribute will be attached to; its type will have already
750+
// been deduced, and the `Attr *` is the attribute being applied to that
751+
// declaration. This function should handle all type-sensitive semantics for
752+
// the attribute. This function will be automatically called by
753+
// `Sema::CheckAttributesOnDeducedType()`.
754+
bit IsTypeDependent = 0;
744755
// Lists language options, one of which is required to be true for the
745756
// attribute to be applicable. If empty, no language options are required.
746757
list<LangOpt> LangOpts = [];
@@ -1400,6 +1411,7 @@ def Cleanup : InheritableAttr {
14001411
let Args = [DeclArgument<Function, "FunctionDecl">];
14011412
let Subjects = SubjectList<[LocalVar]>;
14021413
let Documentation = [CleanupDocs];
1414+
let IsTypeDependent = 1;
14031415
// FIXME: DeclArgument should be reworked to also store the
14041416
// Expr instead of adding attr specific hacks like the following.
14051417
// See the discussion in https://github.com/llvm/llvm-project/pull/14023.

clang/include/clang/Basic/Builtins.td

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5253,6 +5253,18 @@ def HLSLF16ToF32 : LangBuiltin<"HLSL_LANG"> {
52535253
let Prototype = "void(...)";
52545254
}
52555255

5256+
def HLSLDdxCoarse : LangBuiltin<"HLSL_LANG"> {
5257+
let Spellings = ["__builtin_hlsl_elementwise_ddx_coarse"];
5258+
let Attributes = [NoThrow, Const, CustomTypeChecking];
5259+
let Prototype = "void(...)";
5260+
}
5261+
5262+
def HLSLDdyCoarse : LangBuiltin<"HLSL_LANG"> {
5263+
let Spellings = ["__builtin_hlsl_elementwise_ddy_coarse"];
5264+
let Attributes = [NoThrow, Const, CustomTypeChecking];
5265+
let Prototype = "void(...)";
5266+
}
5267+
52565268
// Builtins for XRay.
52575269
def XRayCustomEvent : Builtin {
52585270
let Spellings = ["__xray_customevent"];

clang/include/clang/Sema/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ clang_tablegen(AttrParsedAttrKinds.inc -gen-clang-attr-parsed-attr-kinds
88
SOURCE ../Basic/Attr.td
99
TARGET ClangAttrParsedAttrKinds)
1010

11+
clang_tablegen(AttrIsTypeDependent.inc -gen-clang-attr-is-type-dependent
12+
-I ${CMAKE_CURRENT_SOURCE_DIR}/../../
13+
SOURCE ../Basic/Attr.td
14+
TARGET ClangAttrIsTypeDependent)
15+
1116
clang_tablegen(AttrSpellingListIndex.inc -gen-clang-attr-spelling-index
1217
-I ${CMAKE_CURRENT_SOURCE_DIR}/../../
1318
SOURCE ../Basic/Attr.td

clang/include/clang/Sema/Sema.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4456,6 +4456,10 @@ class Sema final : public SemaBase {
44564456
NamedDecl *New, Decl *Old,
44574457
AvailabilityMergeKind AMK = AvailabilityMergeKind::Redeclaration);
44584458

4459+
/// CheckAttributesOnDeducedType - Calls Sema functions for attributes that
4460+
/// requires the type to be deduced.
4461+
void CheckAttributesOnDeducedType(Decl *D);
4462+
44594463
/// MergeTypedefNameDecl - We just parsed a typedef 'New' which has the
44604464
/// same name and scope as a previous declaration 'Old'. Figure out
44614465
/// how to resolve this situation, merging decls or emitting
@@ -4760,6 +4764,8 @@ class Sema final : public SemaBase {
47604764
// linkage or not.
47614765
static bool mightHaveNonExternalLinkage(const DeclaratorDecl *FD);
47624766

4767+
#include "clang/Sema/AttrIsTypeDependent.inc"
4768+
47634769
///@}
47644770

47654771
//
@@ -15469,6 +15475,8 @@ class Sema final : public SemaBase {
1546915475
std::optional<FunctionEffectMode>
1547015476
ActOnEffectExpression(Expr *CondExpr, StringRef AttributeName);
1547115477

15478+
void ActOnCleanupAttr(Decl *D, const Attr *A);
15479+
1547215480
private:
1547315481
/// The implementation of RequireCompleteType
1547415482
bool RequireCompleteTypeImpl(SourceLocation Loc, QualType T,

clang/lib/CodeGen/CGHLSLBuiltins.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,24 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID,
924924
return EmitRuntimeCall(
925925
Intrinsic::getOrInsertDeclaration(&CGM.getModule(), ID));
926926
}
927+
case Builtin::BI__builtin_hlsl_elementwise_ddx_coarse: {
928+
Value *Op0 = EmitScalarExpr(E->getArg(0));
929+
if (!E->getArg(0)->getType()->hasFloatingRepresentation())
930+
llvm_unreachable("ddx_coarse operand must have a float representation");
931+
Intrinsic::ID ID = CGM.getHLSLRuntime().getDdxCoarseIntrinsic();
932+
return Builder.CreateIntrinsic(/*ReturnType=*/Op0->getType(), ID,
933+
ArrayRef<Value *>{Op0}, nullptr,
934+
"hlsl.ddx.coarse");
935+
}
936+
case Builtin::BI__builtin_hlsl_elementwise_ddy_coarse: {
937+
Value *Op0 = EmitScalarExpr(E->getArg(0));
938+
if (!E->getArg(0)->getType()->hasFloatingRepresentation())
939+
llvm_unreachable("ddy_coarse operand must have a float representation");
940+
Intrinsic::ID ID = CGM.getHLSLRuntime().getDdyCoarseIntrinsic();
941+
return Builder.CreateIntrinsic(/*ReturnType=*/Op0->getType(), ID,
942+
ArrayRef<Value *>{Op0}, nullptr,
943+
"hlsl.ddy.coarse");
944+
}
927945
case Builtin::BI__builtin_get_spirv_spec_constant_bool:
928946
case Builtin::BI__builtin_get_spirv_spec_constant_short:
929947
case Builtin::BI__builtin_get_spirv_spec_constant_ushort:

0 commit comments

Comments
 (0)