[clang] 6da6064 - [Clang][Sema] Check unexpected else statement in cond-update-stmt

Shilei Tian via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 21 05:20:38 PST 2022


Author: Shilei Tian
Date: 2022-02-21T08:20:34-05:00
New Revision: 6da60647cda2fad4d93e359ef3afaea18599b2ba

URL: https://github.com/llvm/llvm-project/commit/6da60647cda2fad4d93e359ef3afaea18599b2ba
DIFF: https://github.com/llvm/llvm-project/commit/6da60647cda2fad4d93e359ef3afaea18599b2ba.diff

LOG: [Clang][Sema] Check unexpected else statement in cond-update-stmt

In 'cond-update-stmt', `else` statement is not expected. This patch adds
the check in Sema.

Reviewed By: ABataev

Differential Revision: https://reviews.llvm.org/D120225

Added: 
    

Modified: 
    clang/include/clang/Basic/DiagnosticSemaKinds.td
    clang/lib/Sema/SemaOpenMP.cpp
    clang/test/OpenMP/atomic_messages.c

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 1719db4871ff3..0d301e76c92d7 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10526,7 +10526,7 @@ def err_omp_atomic_compare : Error<
 def note_omp_atomic_compare: Note<
   "%select{expected compound statement|expected exactly one expression statement|expected assignment statement|expected conditional operator|expect result value to be at false expression|"
   "expect binary operator in conditional expression|expect '<', '>' or '==' as order operator|expect comparison in a form of 'x == e', 'e == x', 'x ordop expr', or 'expr ordop x'|"
-  "expect lvalue for result value|expect scalar value|expect integer value}0">;
+  "expect lvalue for result value|expect scalar value|expect integer value|unexpected 'else' statement}0">;
 def err_omp_atomic_several_clauses : Error<
   "directive '#pragma omp atomic' cannot contain more than one 'read', 'write', 'update', 'capture', or 'compare' clause">;
 def err_omp_several_mem_order_clauses : Error<

diff  --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index ec0d095e89950..c32609e4e32e3 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -10974,6 +10974,8 @@ class OpenMPAtomicCompareChecker {
     NotScalar,
     /// Not an integer.
     NotInteger,
+    /// 'else' statement is not expected.
+    UnexpectedElse,
     /// No error.
     NoError,
   };
@@ -11111,6 +11113,13 @@ bool OpenMPAtomicCompareChecker::checkCondUpdateStmt(IfStmt *S,
     return false;
   }
 
+  if (S->getElse()) {
+    ErrorInfo.Error = ErrorTy::UnexpectedElse;
+    ErrorInfo.ErrorLoc = ErrorInfo.NoteLoc = S->getElse()->getBeginLoc();
+    ErrorInfo.ErrorRange = ErrorInfo.NoteRange = S->getElse()->getSourceRange();
+    return false;
+  }
+
   return true;
 }
 

diff  --git a/clang/test/OpenMP/atomic_messages.c b/clang/test/OpenMP/atomic_messages.c
index 22f7be91662b5..c66cd19b5aca3 100644
--- a/clang/test/OpenMP/atomic_messages.c
+++ b/clang/test/OpenMP/atomic_messages.c
@@ -473,6 +473,15 @@ void compare(void) {
       x = e;
     d = e;
   }
+// omp51-error at +7 {{the statement for 'atomic compare' must be a compound statement of form '{x = expr ordop x ? expr : x;}', '{x = x ordop expr? expr : x;}', '{x = x == e ? d : x;}', '{x = e == x ? d : x;}', or 'if(expr ordop x) {x = expr;}', 'if(x ordop expr) {x = expr;}', 'if(x == e) {x = d;}', 'if(e == x) {x = d;}' where 'x' is an lvalue expression with scalar type, 'expr', 'e', and 'd' are expressions with scalar type, and 'ordop' is one of '<' or '>'.}}
+// omp51-note at +6 {{unexpected 'else' statement}}
+#pragma omp atomic compare
+  {
+    if (x > e)
+      x = e;
+    else
+      d = e;
+  }
   float fx = 0.0f;
   float fd = 0.0f;
   float fe = 0.0f;


        


More information about the cfe-commits mailing list