diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 3f28890a03d4..2fe76e60946f 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 eddf9c50033e..d5904bd1d6f2 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, 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, DefaultIgnore; diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index de8b96514497..1c1cd0f29275 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(E)) ObjC().checkDictionaryLiteral(QualType(Target, 0), DictionaryLiteral); + // Strip complex types. + if (isa(Source)) { + if (!isa(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(Source)->getElementType().getTypePtr(); + Target = cast(Target)->getElementType().getTypePtr(); + } + // Strip vector types. if (isa(Source)) { if (Target->isSveVLSBuiltinType() && @@ -13065,22 +13086,6 @@ void Sema::CheckImplicitConversion(Expr *E, QualType T, SourceLocation CC, if (const auto *MatTy = dyn_cast(Target)) Target = MatTy->getElementType().getTypePtr(); - // Strip complex types. - if (isa(Source)) { - if (!isa(Target)) { - if (SourceMgr.isInSystemMacro(CC) || Target->isBooleanType()) - return; - - return DiagnoseImpCast(*this, E, T, CC, - getLangOpts().CPlusPlus - ? diag::err_impcast_complex_scalar - : diag::warn_impcast_complex_scalar); - } - - Source = cast(Source)->getElementType().getTypePtr(); - Target = cast(Target)->getElementType().getTypePtr(); - } - const BuiltinType *SourceBT = dyn_cast(Source); const BuiltinType *TargetBT = dyn_cast(Target); 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 000000000000..4b91c96c6f34 --- /dev/null +++ b/clang/test/Sema/implicit-cast-complex-to-vector.c @@ -0,0 +1,13 @@ +// 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; + // 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; +} +