[libcxx-commits] [clang] [libcxx] [llvm] [Clang] Add warnings when mixing different charN_t types (PR #138708)

Tom Honermann via libcxx-commits libcxx-commits at lists.llvm.org
Thu May 8 13:37:02 PDT 2025


================
@@ -1567,15 +1568,81 @@ void Sema::checkEnumArithmeticConversions(Expr *LHS, Expr *RHS,
   }
 }
 
+static void CheckUnicodeArithmeticConversions(Sema &SemaRef, Expr *LHS,
+                                              Expr *RHS, SourceLocation Loc,
+                                              ArithConvKind ACK) {
+  QualType LHSType = LHS->getType().getUnqualifiedType();
+  QualType RHSType = RHS->getType().getUnqualifiedType();
+
+  if (!SemaRef.getLangOpts().CPlusPlus || !LHSType->isUnicodeCharacterType() ||
+      !RHSType->isUnicodeCharacterType())
+    return;
+
+  if (ACK == ArithConvKind::Comparison) {
+    if (SemaRef.getASTContext().hasSameType(LHSType, RHSType))
+      return;
+
+    Expr::EvalResult LHSRes, RHSRes;
+    bool Success = LHS->EvaluateAsInt(LHSRes, SemaRef.getASTContext(),
+                                      Expr::SE_AllowSideEffects,
+                                      SemaRef.isConstantEvaluatedContext());
+    if (Success)
+      Success = RHS->EvaluateAsInt(RHSRes, SemaRef.getASTContext(),
+                                   Expr::SE_AllowSideEffects,
+                                   SemaRef.isConstantEvaluatedContext());
+    if (Success) {
+      llvm::APSInt LHSValue(32);
+      LHSValue = LHSRes.Val.getInt();
+      llvm::APSInt RHSValue(32);
+      RHSValue = RHSRes.Val.getInt();
+
+      auto IsSingleCodeUnitCP = [](const QualType &T,
+                                   const llvm::APSInt &Value) {
+        if (T->isChar8Type())
+          return llvm::IsSingleCodeUnitUTF8Codepoint(Value.getExtValue());
+        if (T->isChar16Type())
+          return llvm::IsSingleCodeUnitUTF16Codepoint(Value.getExtValue());
+        return llvm::IsSingleCodeUnitUTF32Codepoint(Value.getExtValue());
+      };
----------------
tahonermann wrote:

This lambda appears in several places. Consider factoring it out.

https://github.com/llvm/llvm-project/pull/138708


More information about the libcxx-commits mailing list