[OPENMP] Initial parsing and sema analysis for 'read' clause in 'atomic' directive.

llvm-svn: 213717
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index c7c7654..7ec0d2b 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -92,15 +92,16 @@
     Scope *CurScope;
     SourceLocation ConstructLoc;
     bool OrderedRegion;
+    SourceLocation AtomicClauseLoc;
     SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
                  Scope *CurScope, SourceLocation Loc)
         : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
           Directive(DKind), DirectiveName(std::move(Name)), CurScope(CurScope),
-          ConstructLoc(Loc), OrderedRegion(false) {}
+          ConstructLoc(Loc), OrderedRegion(false), AtomicClauseLoc() {}
     SharingMapTy()
         : SharingMap(), AlignedMap(), DefaultAttr(DSA_unspecified),
           Directive(OMPD_unknown), DirectiveName(), CurScope(nullptr),
-          ConstructLoc(), OrderedRegion(false) {}
+          ConstructLoc(), OrderedRegion(false), AtomicClauseLoc() {}
   };
 
   typedef SmallVector<SharingMapTy, 64> StackTy;
@@ -207,6 +208,22 @@
     return false;
   }
 
+  /// \brief Checks if the 'atomic' construct has explicitly specified 'read',
+  /// 'update', 'write' or 'capture' clause.
+  bool hasAtomicClause() const {
+    return Stack.back().AtomicClauseLoc.isValid();
+  }
+  /// \brief Gets location of explicitly specified clause for 'atomic'
+  /// construct.
+  SourceLocation getAtomicClauseLoc() const {
+    return Stack.back().AtomicClauseLoc;
+  }
+  /// \brief Sets location of explicitly specified clause for 'atomic'
+  /// directive.
+  void setAtomicClauseLoc(SourceLocation Loc) {
+    Stack.back().AtomicClauseLoc = Loc;
+  }
+
   Scope *getCurScope() const { return Stack.back().CurScope; }
   Scope *getCurScope() { return Stack.back().CurScope; }
   SourceLocation getConstructLoc() { return Stack.back().ConstructLoc; }
@@ -2379,12 +2396,22 @@
                                             SourceLocation StartLoc,
                                             SourceLocation EndLoc) {
   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
+  auto CS = cast<CapturedStmt>(AStmt);
   // 1.2.2 OpenMP Language Terminology
   // Structured block - An executable statement with a single entry at the
   // top and a single exit at the bottom.
   // The point of exit cannot be a branch out of the structured block.
   // longjmp() and throw() must not violate the entry/exit criteria.
   // TODO further analysis of associated statements and clauses.
+  for (auto *C : Clauses) {
+    if (C->getClauseKind() == OMPC_read) {
+      if (!isa<Expr>(CS->getCapturedStmt())) {
+          Diag(CS->getCapturedStmt()->getLocStart(),
+               diag::err_omp_atomic_read_not_expression_statement);
+        return StmtError();
+      }
+    }
+  }
 
   getCurFunction()->setHasBranchProtectedScope();
 
@@ -2430,6 +2457,7 @@
   case OMPC_mergeable:
   case OMPC_threadprivate:
   case OMPC_flush:
+  case OMPC_read:
   case OMPC_unknown:
     llvm_unreachable("Clause is not allowed.");
   }
@@ -2633,6 +2661,7 @@
   case OMPC_mergeable:
   case OMPC_threadprivate:
   case OMPC_flush:
+  case OMPC_read:
   case OMPC_unknown:
     llvm_unreachable("Clause is not allowed.");
   }
@@ -2748,6 +2777,7 @@
   case OMPC_mergeable:
   case OMPC_threadprivate:
   case OMPC_flush:
+  case OMPC_read:
   case OMPC_unknown:
     llvm_unreachable("Clause is not allowed.");
   }
@@ -2827,6 +2857,9 @@
   case OMPC_mergeable:
     Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
     break;
+  case OMPC_read:
+    Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
+    break;
   case OMPC_if:
   case OMPC_final:
   case OMPC_num_threads:
@@ -2873,6 +2906,12 @@
   return new (Context) OMPMergeableClause(StartLoc, EndLoc);
 }
 
+OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
+                                       SourceLocation EndLoc) {
+  DSAStack->setAtomicClauseLoc(StartLoc);
+  return new (Context) OMPReadClause(StartLoc, EndLoc);
+}
+
 OMPClause *Sema::ActOnOpenMPVarListClause(
     OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
     SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
@@ -2926,6 +2965,7 @@
   case OMPC_untied:
   case OMPC_mergeable:
   case OMPC_threadprivate:
+  case OMPC_read:
   case OMPC_unknown:
     llvm_unreachable("Clause is not allowed.");
   }
@@ -4226,5 +4266,3 @@
 
   return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
 }
-
-#undef DSAStack