[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


================
@@ -11810,6 +11811,46 @@ static void DiagnoseIntInBoolContext(Sema &S, Expr *E) {
   }
 }
 
+static void DiagnoseMixedUnicodeImplicitConversion(Sema &S, const Type *Source,
+                                                   const Type *Target, Expr *E,
+                                                   QualType T,
+                                                   SourceLocation CC) {
+  assert(Source->isUnicodeCharacterType() && Target->isUnicodeCharacterType() &&
+         Source != Target);
+  Expr::EvalResult Result;
+  if (E->EvaluateAsInt(Result, S.getASTContext(), Expr::SE_AllowSideEffects,
+                       S.isConstantEvaluatedContext())) {
+    llvm::APSInt Value(32);
+    Value = Result.Val.getInt();
+    bool IsASCII = Value <= 0x7F;
+    bool IsBMP = Value <= 0xD7FF || (Value >= 0xE000 && Value <= 0xFFFF);
+    bool ConversionPreservesSemantics =
+        IsASCII || (!Source->isChar8Type() && !Target->isChar8Type() && IsBMP);
----------------
tahonermann wrote:

I don't think it is necessary to check that the source type isn't `char8_t` here.
```suggestion
    bool ConversionPreservesSemantics =
        IsASCII || (!Target->isChar8Type() && IsBMP);
```

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


More information about the libcxx-commits mailing list