r206133 - Fix diagnostics for C-style cast to function type.

Logan Chien tzuhsiang.chien at gmail.com
Sun Apr 13 09:08:26 PDT 2014


Author: logan
Date: Sun Apr 13 11:08:24 2014
New Revision: 206133

URL: http://llvm.org/viewvc/llvm-project?rev=206133&view=rev
Log:
Fix diagnostics for C-style cast to function type.

If the C-style type cast is applied to the overloaded
function and the destination type is function type,
then Clang will crash with assertion failure.  For example,

    void foo(int);
    void foo(int, int);
    void bar() {
        typedef void (ft)(int);
        ft p = (ft)foo;
    }

In this case, the overloaded function foo will be cast to
a function type, which should be considered as an error.
But, unfortunately, since the function resolution is using
canonical type, the matched function will be returned, and
result in SEGV.

This patch fixes this issue by removing the assertion and
add some error diagnostics as the one in static_cast.

Modified:
    cfe/trunk/lib/Sema/SemaCast.cpp
    cfe/trunk/test/SemaCXX/addr-of-overloaded-function-casting.cpp

Modified: cfe/trunk/lib/Sema/SemaCast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCast.cpp?rev=206133&r1=206132&r2=206133&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaCast.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCast.cpp Sun Apr 13 11:08:24 2014
@@ -2097,10 +2097,16 @@ void CastOperation::CheckCXXCStyleCast(b
                                 DestType,
                                 /*Complain*/ true,
                                 Found);
-      
-      assert(!Fn && "cast failed but able to resolve overload expression!!");
-      (void)Fn;
-
+      if (Fn) {
+        // If DestType is a function type (not to be confused with the function
+        // pointer type), it will be possible to resolve the function address,
+        // but the type cast should be considered as failure.
+        OverloadExpr *OE = OverloadExpr::find(SrcExpr.get()).Expression;
+        Self.Diag(OpRange.getBegin(), diag::err_bad_cstyle_cast_overload)
+          << OE->getName() << DestType << OpRange
+          << OE->getQualifierLoc().getSourceRange();
+        Self.NoteAllOverloadCandidates(SrcExpr.get());
+      }
     } else {
       diagnoseBadCast(Self, msg, (FunctionalStyle ? CT_Functional : CT_CStyle),
                       OpRange, SrcExpr.get(), DestType, ListInitialization);

Modified: cfe/trunk/test/SemaCXX/addr-of-overloaded-function-casting.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/addr-of-overloaded-function-casting.cpp?rev=206133&r1=206132&r2=206133&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/addr-of-overloaded-function-casting.cpp (original)
+++ cfe/trunk/test/SemaCXX/addr-of-overloaded-function-casting.cpp Sun Apr 13 11:08:24 2014
@@ -1,8 +1,8 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s
 void g();
 
-void f(); // expected-note 9{{candidate function}}
-void f(int); // expected-note 9{{candidate function}}
+void f(); // expected-note 11{{candidate function}}
+void f(int); // expected-note 11{{candidate function}}
 
 template <class T>
 void t(T); // expected-note 3{{candidate function}} \
@@ -58,4 +58,13 @@ int main()
   { bool b = static_cast<int (&)(char)>(t); } // expected-error{{does not match required}}
   
   { bool b = static_cast<void (&)(char)>(f); } // expected-error{{does not match}}
+
+  {
+    // The error should be reported when casting overloaded function to the
+    // compatible function type (not to be confused with function pointer or
+    // function reference type.)
+    typedef void (FnType)(int);
+    FnType a = static_cast<FnType>(f); // expected-error{{address of overloaded function}}
+    FnType b = (FnType)(f); // expected-error{{address of overloaded function}}
+  }
 }





More information about the cfe-commits mailing list