diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index cfd273fe6f07..ee0c447390ee 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -11564,6 +11564,8 @@ def err_cilk_spawn_invalid_func_context : Error< "|a varargs function}0">; def err_pragma_cilk_precedes_noncilk : Error< "expected a Cilk keyword to follow '%0'">; +def err_cilk_in_statement_expression : Error< + "'%0' cannot be used inside a statement expression">; // cilk_scope def note_protected_by_cilk_scope: Note< diff --git a/clang/lib/Sema/SemaCilk.cpp b/clang/lib/Sema/SemaCilk.cpp index fb8505528930..9080549e7e6d 100644 --- a/clang/lib/Sema/SemaCilk.cpp +++ b/clang/lib/Sema/SemaCilk.cpp @@ -13,6 +13,7 @@ #include "clang/AST/ExprCilk.h" #include "clang/AST/StmtCilk.h" #include "clang/Sema/SemaInternal.h" +#include "clang/Sema/ScopeInfo.h" using namespace clang; using namespace sema; @@ -51,6 +52,15 @@ static FunctionScopeInfo *checkCilkContext(Sema &S, SourceLocation Loc, FunctionScopeInfo *ScopeInfo = S.getCurFunction(); assert(ScopeInfo && "missing function scope for function"); + // Cilk keywords inside statement expressions cause problems + // generating IR. + for (CompoundScopeInfo &Scope : ScopeInfo->CompoundScopes) { + if (Scope.IsStmtExpr) { + S.Diag(Loc, diag::err_cilk_in_statement_expression) << Keyword; + return nullptr; + } + } + return ScopeInfo; } diff --git a/clang/test/Cilk/stmt-expr.c b/clang/test/Cilk/stmt-expr.c new file mode 100644 index 000000000000..cc6324eb3a30 --- /dev/null +++ b/clang/test/Cilk/stmt-expr.c @@ -0,0 +1,13 @@ +// RUN: %clang_cc1 %s -triple x86_64-unknown-linux-gnu -fopencilk -fsyntax-only -verify + +extern int f(int); + +// CHECK_LABEL: stmt_expr_fn +void stmt_expr_fn(int arg) +{ + int x = cilk_spawn(f( ({cilk_sync; 1;}) )); + // expected-error@-1{{'cilk_sync' cannot be used inside a statement expression}} + int y = ({cilk_spawn f(2); -1;}); + // expected-error@-1{{'cilk_spawn' cannot be used inside a statement expression}} + int z = cilk_spawn f(3); +}