[PATCH] D35817: Ban implicit _Complex to scalar conversions in C++

Tim Northover via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 24 14:42:06 PDT 2017


t.p.northover created this revision.
Herald added a subscriber: mcrosier.

As part of preparing Clang for a C++14 default we noticed that code like this was accepted and did horrible things in C++11 or earlier modes (i.e. where 1.0if is a `_Complex float` rather than a UDL):

  std::complex<float> var = 1.0if;

This is because 1.0if is implicitly cast to a plain `float`, discarding the imaginary part. Then the constructor is called with just this real part and a default 0.0f for the imaginary part.

To avoid this surprising behaviour (and to follow GCC's behaviour) this patch bans such conversions in C++ mode. We can't do it in C99 because it's blessed by the standard.

As part of this some OpenMP and warning tests became irrelevant (again, in line with GCC) so I removed them.

My biggest annoyance is the new diagnostic; I couldn't see a way to promote the existing warning to an error in C++ mode so I duplicated the warning and chose at runtime. I'd be happy to rework if there is a better alternative.

Does it look reasonable?


Repository:
  rL LLVM

https://reviews.llvm.org/D35817

Files:
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaChecking.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/test/CodeGenCXX/stmtexpr.cpp
  clang/test/OpenMP/atomic_capture_codegen.cpp
  clang/test/OpenMP/atomic_update_codegen.cpp
  clang/test/SemaCXX/complex-conversion.cpp
  clang/test/SemaCXX/complex-overload.cpp
  clang/test/SemaCXX/integer-overflow.cpp
  clang/test/SemaCXX/warn-absolute-value.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D35817.107964.patch
Type: text/x-patch
Size: 23194 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170724/01b7acd9/attachment-0001.bin>


More information about the cfe-commits mailing list