[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
This commit is contained in:
Shilei Tian 2022-02-21 08:20:26 -05:00
parent 40d06c4ce9
commit 6da60647cd
3 changed files with 19 additions and 1 deletions

View File

@ -10526,7 +10526,7 @@ def err_omp_atomic_compare : Error<
def note_omp_atomic_compare: Note< 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|" "%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 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< def err_omp_atomic_several_clauses : Error<
"directive '#pragma omp atomic' cannot contain more than one 'read', 'write', 'update', 'capture', or 'compare' clause">; "directive '#pragma omp atomic' cannot contain more than one 'read', 'write', 'update', 'capture', or 'compare' clause">;
def err_omp_several_mem_order_clauses : Error< def err_omp_several_mem_order_clauses : Error<

View File

@ -10974,6 +10974,8 @@ public:
NotScalar, NotScalar,
/// Not an integer. /// Not an integer.
NotInteger, NotInteger,
/// 'else' statement is not expected.
UnexpectedElse,
/// No error. /// No error.
NoError, NoError,
}; };
@ -11111,6 +11113,13 @@ bool OpenMPAtomicCompareChecker::checkCondUpdateStmt(IfStmt *S,
return false; 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; return true;
} }

View File

@ -473,6 +473,15 @@ void compare(void) {
x = e; x = e;
d = e; d = e;
} }
// omp51-error@+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@+6 {{unexpected 'else' statement}}
#pragma omp atomic compare
{
if (x > e)
x = e;
else
d = e;
}
float fx = 0.0f; float fx = 0.0f;
float fd = 0.0f; float fd = 0.0f;
float fe = 0.0f; float fe = 0.0f;