[cfe-commits] r137201 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExpr.cpp test/SemaCXX/unknown-anytype.cpp

John McCall rjmccall at apple.com
Tue Aug 9 21:12:23 PDT 2011


Author: rjmccall
Date: Tue Aug  9 23:12:23 2011
New Revision: 137201

URL: http://llvm.org/viewvc/llvm-project?rev=137201&view=rev
Log:
Change an assert into a check.  I'm pretty sure there was a point
in time when this assert was valid, but it's not valid now.
Also teach this code to correctly introduce function-to-pointer
decay.


Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/SemaCXX/unknown-anytype.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=137201&r1=137200&r2=137201&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Aug  9 23:12:23 2011
@@ -4500,6 +4500,8 @@
   "can only be cast to a pointer type">;
 def err_unknown_any_var_function_type : Error<
   "variable %0 with unknown type cannot be given a function type">;
+def err_unknown_any_function : Error<
+  "function %0 with unknown type must be given a function type">;
 
 def err_filter_expression_integral : Error<
   "filter expression type should be an integral value not %0">;

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=137201&r1=137200&r2=137201&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Tue Aug  9 23:12:23 2011
@@ -9819,9 +9819,19 @@
 
   //  - functions
   if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) {
-    // This is true because FunctionDecls must always have function
-    // type, so we can't be resolving the entire thing at once.
-    assert(type->isFunctionType());
+    if (const PointerType *ptr = type->getAs<PointerType>()) {
+      DestType = ptr->getPointeeType();
+      ExprResult result = resolveDecl(expr, decl);
+      if (result.isInvalid()) return ExprError();
+      return S.ImpCastExprToType(result.take(), type,
+                                 CK_FunctionToPointerDecay, VK_RValue);
+    }
+
+    if (!type->isFunctionType()) {
+      S.Diag(expr->getExprLoc(), diag::err_unknown_any_function)
+        << decl << expr->getSourceRange();
+      return ExprError();
+    }
 
     if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(fn))
       if (method->isInstance()) {

Modified: cfe/trunk/test/SemaCXX/unknown-anytype.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/unknown-anytype.cpp?rev=137201&r1=137200&r2=137201&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/unknown-anytype.cpp (original)
+++ cfe/trunk/test/SemaCXX/unknown-anytype.cpp Tue Aug  9 23:12:23 2011
@@ -34,3 +34,14 @@
     ((void(void)) foo)(); // expected-error {{variable 'foo' with unknown type cannot be given a function type}}
   }
 }
+
+// rdar://problem/9899447
+namespace test4 {
+  extern __unknown_anytype test0(...);
+  extern __unknown_anytype test1(...);
+
+  void test() {
+    void (*fn)(int) = (void(*)(int)) test0;
+    int x = (int) test1; // expected-error {{function 'test1' with unknown type must be given a function type}}
+  }
+}





More information about the cfe-commits mailing list