Author: abataev Date: Fri Nov 10 07:39:50 2017 New Revision: 317891 URL: http://llvm.org/viewvc/llvm-project?rev=317891&view=rev Log: [OpenMP] diagnose assign to firstprivate const, patch by Joel E. Denny
Summary: [OpenMP] diagnose assign to firstprivate const Clang does not diagnose assignments to const variables declared firstprivate. Furthermore, codegen is broken such that, at run time, such assignments simply have no effect. For example, the following prints 0 not 1: int main() { const int i = 0; #pragma omp parallel firstprivate(i) { i=1; printf("%d\n", i); } return 0; } This commit makes these assignments a compile error, which is consistent with other OpenMP compilers I've tried (pgcc 17.4-0, gcc 6.3.0). Reviewers: ABataev Reviewed By: ABataev Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D39859 Modified: cfe/trunk/lib/Sema/SemaExpr.cpp cfe/trunk/test/OpenMP/parallel_firstprivate_messages.cpp Modified: cfe/trunk/lib/Sema/SemaExpr.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=317891&r1=317890&r2=317891&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaExpr.cpp (original) +++ cfe/trunk/lib/Sema/SemaExpr.cpp Fri Nov 10 07:39:50 2017 @@ -14352,8 +14352,13 @@ static bool captureInCapturedRegion(Capt bool ByRef = true; // Using an LValue reference type is consistent with Lambdas (see below). if (S.getLangOpts().OpenMP && RSI->CapRegionKind == CR_OpenMP) { - if (S.IsOpenMPCapturedDecl(Var)) + if (S.IsOpenMPCapturedDecl(Var)) { + bool HasConst = DeclRefType.isConstQualified(); DeclRefType = DeclRefType.getUnqualifiedType(); + // Don't lose diagnostics about assignments to const. + if (HasConst) + DeclRefType.addConst(); + } ByRef = S.IsOpenMPCapturedByRef(Var, RSI->OpenMPLevel); } Modified: cfe/trunk/test/OpenMP/parallel_firstprivate_messages.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/OpenMP/parallel_firstprivate_messages.cpp?rev=317891&r1=317890&r2=317891&view=diff ============================================================================== --- cfe/trunk/test/OpenMP/parallel_firstprivate_messages.cpp (original) +++ cfe/trunk/test/OpenMP/parallel_firstprivate_messages.cpp Fri Nov 10 07:39:50 2017 @@ -56,7 +56,7 @@ using A::x; } int main(int argc, char **argv) { - const int d = 5; + const int d = 5; // expected-note {{variable 'd' declared const here}} const int da[5] = { 0 }; S4 e(4); S5 g(5); @@ -72,6 +72,8 @@ int main(int argc, char **argv) { #pragma omp parallel firstprivate (argc) #pragma omp parallel firstprivate (S1) // expected-error {{'S1' does not refer to a value}} #pragma omp parallel firstprivate (a, b, c, d, f) // expected-error {{firstprivate variable with incomplete type 'S1'}} + #pragma omp parallel firstprivate (d) + d = 5; // expected-error {{cannot assign to variable 'd' with const-qualified type}} #pragma omp parallel firstprivate (argv[1]) // expected-error {{expected variable name}} #pragma omp parallel firstprivate(ba) #pragma omp parallel firstprivate(ca) _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits