https://github.com/AmrDeveloper updated https://github.com/llvm/llvm-project/pull/187954
>From 4fabe10727f9bd7e34658f5f4f2ca8fe1329482d Mon Sep 17 00:00:00 2001 From: Amr Hesham <[email protected]> Date: Sun, 22 Mar 2026 20:09:20 +0100 Subject: [PATCH 1/3] [Clang][Sema] Prevent implicit casting Complex type to Vector --- clang/docs/ReleaseNotes.rst | 2 ++ clang/include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ clang/lib/Sema/SemaChecking.cpp | 3 +++ clang/test/Sema/implicit-cast-complex-to-vector.c | 11 +++++++++++ 4 files changed, 18 insertions(+) create mode 100644 clang/test/Sema/implicit-cast-complex-to-vector.c diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 442ec58adc25a..8f8d606b36cf4 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -358,6 +358,8 @@ Improvements to Clang's diagnostics - Improved ``-Wgnu-zero-variadic-macro-arguments`` to suggest using ``__VA_OPT__`` if the current language version supports it(#GH188624) +- Clang now emits an error when implicitly casting a complex type to a built-in vector type. (#GH186805) + Improvements to Clang's time-trace ---------------------------------- diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 7c65fc15f2836..71d504c659cc2 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -4501,6 +4501,8 @@ def warn_impcast_float_result_precision : Warning< def warn_impcast_double_promotion : Warning< "implicit conversion increases floating-point precision: %0 to %1">, InGroup<DoublePromotion>, DefaultIgnore; +def err_impcast_incompatible_type : Error< + "implicit conversion from %0 to incompatible type %1">; def warn_impcast_integer_sign : Warning< "implicit conversion changes signedness: %0 to %1">, InGroup<SignConversion>, DefaultIgnore; diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index de8b965144971..f4cc5ef73961b 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -13071,9 +13071,12 @@ void Sema::CheckImplicitConversion(Expr *E, QualType T, SourceLocation CC, if (SourceMgr.isInSystemMacro(CC) || Target->isBooleanType()) return; + const Type *OriginalTargetTy = Context.getCanonicalType(T).getTypePtr(); return DiagnoseImpCast(*this, E, T, CC, getLangOpts().CPlusPlus ? diag::err_impcast_complex_scalar + : OriginalTargetTy->isVectorType() + ? diag::err_impcast_incompatible_type : diag::warn_impcast_complex_scalar); } diff --git a/clang/test/Sema/implicit-cast-complex-to-vector.c b/clang/test/Sema/implicit-cast-complex-to-vector.c new file mode 100644 index 0000000000000..a30bfb2d9dbd8 --- /dev/null +++ b/clang/test/Sema/implicit-cast-complex-to-vector.c @@ -0,0 +1,11 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +typedef char __attribute__((__vector_size__(64))) V; + +void implicit_cast_complex_to_vector() { + _Complex double x; + V y; + // expected-error@+1 {{implicit conversion from '_Complex double' to incompatible type 'V' (vector of 64 'char' values)}} + V z = x + y; +} + >From 8312241cb9ef2d56ff918b6b78a935079ce49335 Mon Sep 17 00:00:00 2001 From: Amr Hesham <[email protected]> Date: Sun, 22 Mar 2026 22:23:39 +0100 Subject: [PATCH 2/3] Address code review comments --- clang/lib/Sema/SemaChecking.cpp | 8 +++++--- clang/test/Sema/implicit-cast-complex-to-vector.c | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index f4cc5ef73961b..832666a5e481d 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -13071,12 +13071,14 @@ void Sema::CheckImplicitConversion(Expr *E, QualType T, SourceLocation CC, if (SourceMgr.isInSystemMacro(CC) || Target->isBooleanType()) return; - const Type *OriginalTargetTy = Context.getCanonicalType(T).getTypePtr(); + if (!getLangOpts().CPlusPlus && T->isVectorType()) { + return DiagnoseImpCast(*this, E, T, CC, + diag::err_impcast_incompatible_type); + } + return DiagnoseImpCast(*this, E, T, CC, getLangOpts().CPlusPlus ? diag::err_impcast_complex_scalar - : OriginalTargetTy->isVectorType() - ? diag::err_impcast_incompatible_type : diag::warn_impcast_complex_scalar); } diff --git a/clang/test/Sema/implicit-cast-complex-to-vector.c b/clang/test/Sema/implicit-cast-complex-to-vector.c index a30bfb2d9dbd8..4b91c96c6f34e 100644 --- a/clang/test/Sema/implicit-cast-complex-to-vector.c +++ b/clang/test/Sema/implicit-cast-complex-to-vector.c @@ -1,11 +1,13 @@ -// RUN: %clang_cc1 -fsyntax-only -verify %s +// RUN: %clang_cc1 -fsyntax-only -verify=c %s +// RUN: %clang_cc1 -x c++ -fsyntax-only -verify=cxx %s typedef char __attribute__((__vector_size__(64))) V; void implicit_cast_complex_to_vector() { _Complex double x; V y; - // expected-error@+1 {{implicit conversion from '_Complex double' to incompatible type 'V' (vector of 64 'char' values)}} + // c-error@+2 {{implicit conversion from '_Complex double' to incompatible type 'V' (vector of 64 'char' values)}} + // cxx-error@+1 {{implicit conversion from '_Complex double' to 'V' (vector of 64 'char' values) is not permitted in C++}} V z = x + y; } >From 12de2f9acdbfab7d80b20eeaa9214aa451ae6c61 Mon Sep 17 00:00:00 2001 From: Amr Hesham <[email protected]> Date: Fri, 3 Apr 2026 13:57:56 +0200 Subject: [PATCH 3/3] Strip and check for ComplexType first to have unstripped target ty --- clang/lib/Sema/SemaChecking.cpp | 42 ++++++++++++++++----------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 832666a5e481d..1c1cd0f292753 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -13003,6 +13003,27 @@ void Sema::CheckImplicitConversion(Expr *E, QualType T, SourceLocation CC, else if (auto *DictionaryLiteral = dyn_cast<ObjCDictionaryLiteral>(E)) ObjC().checkDictionaryLiteral(QualType(Target, 0), DictionaryLiteral); + // Strip complex types. + if (isa<ComplexType>(Source)) { + if (!isa<ComplexType>(Target)) { + if (SourceMgr.isInSystemMacro(CC) || Target->isBooleanType()) + return; + + if (!getLangOpts().CPlusPlus && Target->isVectorType()) { + return DiagnoseImpCast(*this, E, T, CC, + diag::err_impcast_incompatible_type); + } + + return DiagnoseImpCast(*this, E, T, CC, + getLangOpts().CPlusPlus + ? diag::err_impcast_complex_scalar + : diag::warn_impcast_complex_scalar); + } + + Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); + Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); + } + // Strip vector types. if (isa<VectorType>(Source)) { if (Target->isSveVLSBuiltinType() && @@ -13065,27 +13086,6 @@ void Sema::CheckImplicitConversion(Expr *E, QualType T, SourceLocation CC, if (const auto *MatTy = dyn_cast<ConstantMatrixType>(Target)) Target = MatTy->getElementType().getTypePtr(); - // Strip complex types. - if (isa<ComplexType>(Source)) { - if (!isa<ComplexType>(Target)) { - if (SourceMgr.isInSystemMacro(CC) || Target->isBooleanType()) - return; - - if (!getLangOpts().CPlusPlus && T->isVectorType()) { - return DiagnoseImpCast(*this, E, T, CC, - diag::err_impcast_incompatible_type); - } - - return DiagnoseImpCast(*this, E, T, CC, - getLangOpts().CPlusPlus - ? diag::err_impcast_complex_scalar - : diag::warn_impcast_complex_scalar); - } - - Source = cast<ComplexType>(Source)->getElementType().getTypePtr(); - Target = cast<ComplexType>(Target)->getElementType().getTypePtr(); - } - const BuiltinType *SourceBT = dyn_cast<BuiltinType>(Source); const BuiltinType *TargetBT = dyn_cast<BuiltinType>(Target); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
