Skip to content

Commit 562999a

Browse files
authored
[clang] assume_aligned incorrectly diagnoses a dependent return type (#111573)
Fix `assume_aligned` incorrectly diagnoses a dependent return type Fixes: #111563
1 parent 9093ba9 commit 562999a

File tree

6 files changed

+27
-10
lines changed

6 files changed

+27
-10
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4453,9 +4453,10 @@ class Sema final : public SemaBase {
44534453
SourceLocation *ArgLocation = nullptr);
44544454

44554455
/// Determine if type T is a valid subject for a nonnull and similar
4456-
/// attributes. By default, we look through references (the behavior used by
4457-
/// nonnull), but if the second parameter is true, then we treat a reference
4458-
/// type as valid.
4456+
/// attributes. Dependent types are considered valid so they can be checked
4457+
/// during instantiation time. By default, we look through references (the
4458+
/// behavior used by nonnull), but if the second parameter is true, then we
4459+
/// treat a reference type as valid.
44594460
bool isValidPointerAttrType(QualType T, bool RefOkay = false);
44604461

44614462
/// AddAssumeAlignedAttr - Adds an assume_aligned attribute to a particular

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,8 @@ static void handlePreferredName(Sema &S, Decl *D, const ParsedAttr &AL) {
12161216
}
12171217

12181218
bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) {
1219+
if (T->isDependentType())
1220+
return true;
12191221
if (RefOkay) {
12201222
if (T->isReferenceType())
12211223
return true;
@@ -1284,7 +1286,7 @@ static void handleNonNullAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
12841286
for (unsigned I = 0, E = getFunctionOrMethodNumParams(D);
12851287
I != E && !AnyPointers; ++I) {
12861288
QualType T = getFunctionOrMethodParamType(D, I);
1287-
if (T->isDependentType() || S.isValidPointerAttrType(T))
1289+
if (S.isValidPointerAttrType(T))
12881290
AnyPointers = true;
12891291
}
12901292

@@ -1409,8 +1411,7 @@ void Sema::AddAllocAlignAttr(Decl *D, const AttributeCommonInfo &CI,
14091411
AllocAlignAttr TmpAttr(Context, CI, ParamIdx());
14101412
SourceLocation AttrLoc = CI.getLoc();
14111413

1412-
if (!ResultType->isDependentType() &&
1413-
!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
1414+
if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) {
14141415
Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only)
14151416
<< &TmpAttr << CI.getRange() << getFunctionOrMethodResultSourceRange(D);
14161417
return;

clang/test/SemaCXX/alloc-align-attr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ void* dependent_param_func(T param) __attribute__((alloc_align(1)));// expected-
2323
template <int T>
2424
void* illegal_align_param(int p) __attribute__((alloc_align(T))); // expected-error {{'alloc_align' attribute requires parameter 1 to be an integer constant}}
2525

26+
template <typename T>
27+
T dependent_return_type(int p) __attribute__((alloc_align(1)));
28+
2629
void dependent_impl(int align) {
2730
dependent_ret<int> a; // expected-note {{in instantiation of template class 'dependent_ret<int>' requested here}}
2831
a.Foo(1);

clang/test/SemaCXX/builtin-assume-aligned-tmpl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ T *atest3() __attribute__((assume_aligned(31, o))); // expected-error {{requeste
5252
template <typename T, int o>
5353
T *atest4() __attribute__((assume_aligned(32, o)));
5454

55+
template<typename T>
56+
T atest5(int) __attribute__((assume_aligned(2)));
57+
58+
// expected-warning@+1 {{'assume_aligned' attribute only applies to return values that are pointers or references}}
59+
int atest6(int) __attribute__((assume_aligned(2)));
60+
5561
void test22() {
5662
atest3<int, 5>();
5763
atest4<int, 5>();

clang/test/SemaCXX/noescape-attr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify %s
2+
3+
template<typename T>
4+
void test1(T __attribute__((noescape)) arr, int size);
5+
6+
// expected-warning@+1 {{'noescape' attribute only applies to pointer arguments}}
7+
void test2(int __attribute__((noescape)) arr, int size);

clang/test/SemaObjCXX/noescape.mm

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
void noescapeFunc2(int *); // expected-error {{conflicting types for 'noescapeFunc2'}}
1818

1919
template <class T>
20-
void noescapeFunc5(__attribute__((noescape)) T); // expected-warning {{'noescape' attribute only applies to pointer arguments}}
20+
void noescapeFunc5(__attribute__((noescape)) T);
2121
template <class T>
2222
void noescapeFunc6(__attribute__((noescape)) const T &);
2323
template <class T>
@@ -144,7 +144,7 @@ -(void) m1:(int*) p {
144144

145145
struct S6 {
146146
S6();
147-
S6(const S6 &) = delete; // expected-note 12 {{'S6' has been explicitly marked deleted here}}
147+
S6(const S6 &) = delete; // expected-note 11 {{'S6' has been explicitly marked deleted here}}
148148
int f;
149149
};
150150

@@ -161,7 +161,7 @@ void test1(C0 *c0) {
161161
__block S6 b6; // expected-error {{call to deleted constructor of 'S6'}}
162162
__block S6 b7; // expected-error {{call to deleted constructor of 'S6'}}
163163
__block S6 b8; // expected-error {{call to deleted constructor of 'S6'}}
164-
__block S6 b9; // expected-error {{call to deleted constructor of 'S6'}}
164+
__block S6 b9;
165165
__block S6 b10; // expected-error {{call to deleted constructor of 'S6'}}
166166
__block S6 b11; // expected-error {{call to deleted constructor of 'S6'}}
167167
__block S6 b12;
@@ -199,7 +199,6 @@ void test1(C0 *c0) {
199199
(void)b8;
200200
});
201201

202-
// FIXME: clang shouldn't reject this.
203202
noescapeFunc5(^{
204203
(void)b9;
205204
});

0 commit comments

Comments
 (0)