aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Sema/SemaChecking.cpp
diff options
context:
space:
mode:
authorAaron Ballman <aaron@aaronballman.com>2025-05-07 10:05:00 -0400
committerGitHub <noreply@github.com>2025-05-07 10:05:00 -0400
commitb59ab701e94cce455a53358cbe5082a3efb58fbf (patch)
tree05841dd036906b834ed3ac01ccd1d869bf2bd748 /clang/lib/Sema/SemaChecking.cpp
parenta061998a14a0ff16b633d6cf48c250d50c6acad2 (diff)
downloadllvm-b59ab701e94cce455a53358cbe5082a3efb58fbf.zip
llvm-b59ab701e94cce455a53358cbe5082a3efb58fbf.tar.gz
llvm-b59ab701e94cce455a53358cbe5082a3efb58fbf.tar.bz2
[C] Handle comma operator for implicit int->enum conversions (#138752)
In C++, the type of an enumerator is the type of the enumeration, whereas in C, the type of the enumerator is 'int'. The type of a comma operator is the type of the right-hand operand, which means you can get an implicit conversion with this code in C but not in C++: ``` enum E { Zero }; enum E foo() { return ((void)0, Zero); } ``` We were previously incorrectly diagnosing this code as being incompatible with C++ because the type of the paren expression would be 'int' there, whereas in C++ the type is 'E'. So now we handle the comma operator with special logic when analyzing implicit conversions in C. When analyzing the left-hand operand of a comma operator, we do not need to check for that operand causing an implicit conversion for the entire comma expression. So we only check for that case with the right-hand operand. This addresses a concern brought up post-commit: https://github.com/llvm/llvm-project/pull/137658#issuecomment-2854525259
Diffstat (limited to 'clang/lib/Sema/SemaChecking.cpp')
-rw-r--r--clang/lib/Sema/SemaChecking.cpp24
1 files changed, 23 insertions, 1 deletions
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 7f45533..a5e0094 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -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