Skip to content

[SYCL] Do not allow template instantiation to create null attributes. #3575

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

Merged
merged 1 commit into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions clang/lib/Sema/SemaStmtAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,14 @@ template <typename T>
static void FilterAttributeList(ArrayRef<const Attr *> Attrs,
SmallVectorImpl<const T *> &FilteredAttrs) {

llvm::transform(Attrs, std::back_inserter(FilteredAttrs), [](const Attr *A) {
if (const auto *Cast = dyn_cast_or_null<const T>(A))
return Cast->isDependent() ? nullptr : Cast;
return static_cast<const T*>(nullptr);
});
llvm::transform(Attrs, std::back_inserter(FilteredAttrs),
[](const Attr *A) -> const T * {
if (const auto *Cast = dyn_cast<T>(A))
return Cast->isDependent() ? nullptr : Cast;
return nullptr;
});
FilteredAttrs.erase(
std::remove(FilteredAttrs.begin(), FilteredAttrs.end(),
static_cast<const T*>(nullptr)),
std::remove(FilteredAttrs.begin(), FilteredAttrs.end(), nullptr),
FilteredAttrs.end());
}

Expand Down
8 changes: 7 additions & 1 deletion clang/lib/Sema/TreeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -7321,7 +7321,8 @@ TreeTransform<Derived>::TransformAttributedStmt(AttributedStmt *S,
for (const auto *I : S->getAttrs()) {
const Attr *R = getDerived().TransformAttr(I);
AttrsChanged |= (I != R);
Attrs.push_back(R);
if (R)
Attrs.push_back(R);
}

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

// If transforming the attributes failed for all of the attributes in the
// statement, don't make an AttributedStmt without attributes.
if (Attrs.empty())
return SubStmt;

return getDerived().RebuildAttributedStmt(S->getAttrLoc(), Attrs,
SubStmt.get());
}
Expand Down
13 changes: 13 additions & 0 deletions clang/test/SemaSYCL/attr-instantiate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %clang_cc1 -fsycl-is-device -verify -fsyntax-only %s

// Test to ensure that template instantiation of an invalid attribute argument
// does not result in a null pointer crash when rebuilding the attributed
// statement.
template <int A> void bar() {
// expected-error@+1 {{'loop_unroll' attribute requires a positive integral compile time constant expression}}
[[clang::loop_unroll(A)]] for (int i = 0; i < 10; ++i);
}

void foo() {
bar<-1>(); // expected-note {{in instantiation of function template specialization 'bar<-1>' requested here}}
}