Skip to content

[C] Handle comma operator for implicit int->enum conversions #138752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
@@ -11647,6 +11647,15 @@ static void DiagnoseFloatingImpCast(Sema &S, Expr *E, QualType T,
}
}

static void CheckCommaOperand(Sema &S, Expr *E, QualType T, SourceLocation CC,
bool ExtraCheckForImplicitConversion) {
E = E->IgnoreParenImpCasts();
AnalyzeImplicitConversions(S, E, CC);

if (ExtraCheckForImplicitConversion && E->getType() != T)
S.CheckImplicitConversion(E, T, CC);
}

/// Analyze the given compound assignment for the possible losing of
/// floating-point precision.
static void AnalyzeCompoundAssignment(Sema &S, BinaryOperator *E) {
@@ -12464,7 +12473,7 @@ static void AnalyzeImplicitConversions(
<< OrigE->getSourceRange() << T->isBooleanType()
<< FixItHint::CreateReplacement(UO->getBeginLoc(), "!");

if (const auto *BO = dyn_cast<BinaryOperator>(SourceExpr))
if (auto *BO = dyn_cast<BinaryOperator>(SourceExpr))
if ((BO->getOpcode() == BO_And || BO->getOpcode() == BO_Or) &&
BO->getLHS()->isKnownToHaveBooleanValue() &&
BO->getRHS()->isKnownToHaveBooleanValue() &&
@@ -12490,6 +12499,19 @@ static void AnalyzeImplicitConversions(
(BO->getOpcode() == BO_And ? "&&" : "||"));
S.Diag(BO->getBeginLoc(), diag::note_cast_operand_to_int);
}
} else if (BO->isCommaOp() && !S.getLangOpts().CPlusPlus) {
/// Analyze the given comma operator. The basic idea behind the analysis
/// is to analyze the left and right operands slightly differently. The
/// left operand needs to check whether the operand itself has an implicit
/// conversion, but not whether the left operand induces an implicit
/// conversion for the entire comma expression itself. This is similar to
/// how CheckConditionalOperand behaves; it's as-if the correct operand
/// were directly used for the implicit conversion check.
CheckCommaOperand(S, BO->getLHS(), T, BO->getOperatorLoc(),
/*ExtraCheckForImplicitConversion=*/false);
CheckCommaOperand(S, BO->getRHS(), T, BO->getOperatorLoc(),
/*ExtraCheckForImplicitConversion=*/true);
return;
}

// For conditional operators, we analyze the arguments as if they
7 changes: 6 additions & 1 deletion clang/test/Sema/implicit-cast.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// RUN: %clang_cc1 -fsyntax-only %s
// RUN: %clang_cc1 -fsyntax-only -verify %s

static char *test1(int cf) {
return cf ? "abc" : 0;
}
static char *test2(int cf) {
return cf ? 0 : "abc";
}

int baz(void) {
int f;
return ((void)0, f = 1.4f); // expected-warning {{implicit conversion from 'float' to 'int' changes value from 1.4 to 1}}
}
22 changes: 22 additions & 0 deletions clang/test/Sema/implicit-int-enum-conversion.c
Original file line number Diff line number Diff line change
@@ -50,3 +50,25 @@ enum E1 quux(void) {
return E2_Zero; // expected-warning {{implicit conversion from enumeration type 'enum E2' to different enumeration type 'enum E1'}} \
cxx-error {{cannot initialize return object of type 'enum E1' with an rvalue of type 'E2'}}
}

enum E1 comma1(void) {
return ((void)0, E1_One);
}

enum E1 comma2(void) {
enum E1 x;
return
(x = 12, // expected-warning {{implicit conversion from 'int' to enumeration type 'enum E1' is invalid in C++}} \
cxx-error {{assigning to 'enum E1' from incompatible type 'int'}}
E1_One);
}

enum E1 comma3(void) {
enum E1 x;
return ((void)0, foo()); // Okay, no conversion in C++
}

enum E1 comma4(void) {
return ((void)1, 2); // expected-warning {{implicit conversion from 'int' to enumeration type 'enum E1' is invalid in C++}} \
cxx-error {{cannot initialize return object of type 'enum E1' with an rvalue of type 'int'}}
}
Loading