[PATCH] D33333: Emit warning when throw exception in destruct or dealloc functions which has a (possible implicit) noexcept specifier

Jennifer Yu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu May 18 12:14:22 PDT 2017


jyu2 created this revision.

Throwing in the destructor is not good (C++11 change try to not allow see below).  But in reality, those codes are exist.  
C++11 [class.dtor]p3:

  A declaration of a destructor that does not have an exception-specification is implicitly considered to have the same exception specification as an implicit declaration.

With this change, the application worked before may now run into runtime termination.  May gold here is to emit a warning to provide only possible info to where the code may need to be changed.

First there is no way, in compile time to identify the “throw” really throw out of the function. Things like the call which throw out…  To keep this simple, when “throw” is seen, checking its enclosing function(only destructor and dealloc functions) with noexcept(true) specifier emit warning.

Here is implementation detail:
A new member function CheckCXXThrowInNonThrowingFunc is added for class Sema in Sema.h.  It is used in the call to both BuildCXXThrow and TransformCXXThrowExpr.

The function basic check if the enclosing function with non-throwing noexcept specifer, if so emit warning for it.

The example of warning message like:
k1.cpp:18:3: warning: ''~dependent_warn'' has a (possible implicit) non-throwing

      noexcept specifier. Throwing exception may cause termination.
      [-Wthrow-in-dtor]
  throw 1;
  ^

k1.cpp:43:30: note: in instantiation of member function

  'dependent_warn<noexcept_fun>::~dependent_warn' requested here

dependent_warn<noexcept_fun> f; // cause warning

Let me know if more information is needed.

Thanks.

Jennifer


https://reviews.llvm.org/D33333

Files:
  include/clang/Basic/DiagnosticGroups.td
  include/clang/Basic/DiagnosticSemaKinds.td
  include/clang/Sema/Sema.h
  lib/Sema/SemaExprCXX.cpp
  lib/Sema/TreeTransform.h
  test/SemaCXX/warn-throw-out-dtor.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33333.99458.patch
Type: text/x-patch
Size: 10696 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170518/7394ab4d/attachment.bin>


More information about the cfe-commits mailing list