[PATCH] D39859: [OpenMP] diagnose assign to firstprivate const

Joel E. Denny via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 9 12:05:26 PST 2017


jdenny created this revision.

[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).


https://reviews.llvm.org/D39859

Files:
  lib/Sema/SemaExpr.cpp
  test/OpenMP/parallel_firstprivate_messages.cpp


Index: test/OpenMP/parallel_firstprivate_messages.cpp
===================================================================
--- test/OpenMP/parallel_firstprivate_messages.cpp
+++ test/OpenMP/parallel_firstprivate_messages.cpp
@@ -56,7 +56,7 @@
 }
 
 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,7 @@
   #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'}}
+    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)
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -14352,8 +14352,13 @@
   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 = QualType(DeclRefType.getTypePtr(), Qualifiers::Const);
+    }
     ByRef = S.IsOpenMPCapturedByRef(Var, RSI->OpenMPLevel);
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39859.122292.patch
Type: text/x-patch
Size: 1736 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171109/02e6d182/attachment.bin>


More information about the cfe-commits mailing list