Skip to content

Commit cec6469

Browse files
authored
[SYCL] Do not allow template instantiation to create null attributes. (#3575)
Consumers of the attributes to be added on the attributed statement do not typically expect to get a null attribute, so make that impossible.
1 parent fa382d6 commit cec6469

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

clang/lib/Sema/SemaStmtAttr.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,14 @@ template <typename T>
166166
static void FilterAttributeList(ArrayRef<const Attr *> Attrs,
167167
SmallVectorImpl<const T *> &FilteredAttrs) {
168168

169-
llvm::transform(Attrs, std::back_inserter(FilteredAttrs), [](const Attr *A) {
170-
if (const auto *Cast = dyn_cast_or_null<const T>(A))
171-
return Cast->isDependent() ? nullptr : Cast;
172-
return static_cast<const T*>(nullptr);
173-
});
169+
llvm::transform(Attrs, std::back_inserter(FilteredAttrs),
170+
[](const Attr *A) -> const T * {
171+
if (const auto *Cast = dyn_cast<T>(A))
172+
return Cast->isDependent() ? nullptr : Cast;
173+
return nullptr;
174+
});
174175
FilteredAttrs.erase(
175-
std::remove(FilteredAttrs.begin(), FilteredAttrs.end(),
176-
static_cast<const T*>(nullptr)),
176+
std::remove(FilteredAttrs.begin(), FilteredAttrs.end(), nullptr),
177177
FilteredAttrs.end());
178178
}
179179

clang/lib/Sema/TreeTransform.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7321,7 +7321,8 @@ TreeTransform<Derived>::TransformAttributedStmt(AttributedStmt *S,
73217321
for (const auto *I : S->getAttrs()) {
73227322
const Attr *R = getDerived().TransformAttr(I);
73237323
AttrsChanged |= (I != R);
7324-
Attrs.push_back(R);
7324+
if (R)
7325+
Attrs.push_back(R);
73257326
}
73267327

73277328
StmtResult SubStmt = getDerived().TransformStmt(S->getSubStmt(), SDK);
@@ -7331,6 +7332,11 @@ TreeTransform<Derived>::TransformAttributedStmt(AttributedStmt *S,
73317332
if (SubStmt.get() == S->getSubStmt() && !AttrsChanged)
73327333
return S;
73337334

7335+
// If transforming the attributes failed for all of the attributes in the
7336+
// statement, don't make an AttributedStmt without attributes.
7337+
if (Attrs.empty())
7338+
return SubStmt;
7339+
73347340
return getDerived().RebuildAttributedStmt(S->getAttrLoc(), Attrs,
73357341
SubStmt.get());
73367342
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %clang_cc1 -fsycl-is-device -verify -fsyntax-only %s
2+
3+
// Test to ensure that template instantiation of an invalid attribute argument
4+
// does not result in a null pointer crash when rebuilding the attributed
5+
// statement.
6+
template <int A> void bar() {
7+
// expected-error@+1 {{'loop_unroll' attribute requires a positive integral compile time constant expression}}
8+
[[clang::loop_unroll(A)]] for (int i = 0; i < 10; ++i);
9+
}
10+
11+
void foo() {
12+
bar<-1>(); // expected-note {{in instantiation of function template specialization 'bar<-1>' requested here}}
13+
}

0 commit comments

Comments
 (0)