From: Chandler Carruth Date: Tue, 29 Dec 2009 06:17:27 +0000 (+0000) Subject: Handle using declarations in overloaded and template functions during ADL and X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bd64729ac6de8fed320e7a722597cc5444709c63;p=clang Handle using declarations in overloaded and template functions during ADL and address resolution. This fixes PR5751. Also, while we're here, remove logic from ADL which mistakenly included the definition namespaces of overloaded and/or templated functions whose name or address is used as an argument. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@92245 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaLookup.cpp b/lib/Sema/SemaLookup.cpp index aac3ffe6dd..289c81da7a 100644 --- a/lib/Sema/SemaLookup.cpp +++ b/lib/Sema/SemaLookup.cpp @@ -1528,15 +1528,12 @@ Sema::FindAssociatedClassesAndNamespaces(Expr **Args, unsigned NumArgs, for (llvm::SmallVectorImpl::iterator I = Functions.begin(), E = Functions.end(); I != E; ++I) { - FunctionDecl *FDecl = dyn_cast(*I); - if (!FDecl) - FDecl = cast(*I)->getTemplatedDecl(); + // Look through any using declarations to find the underlying function. + NamedDecl *Fn = (*I)->getUnderlyingDecl(); - // Add the namespace in which this function was defined. Note - // that, if this is a member function, we do *not* consider the - // enclosing namespace of its class. - DeclContext *Ctx = FDecl->getDeclContext(); - CollectNamespace(AssociatedNamespaces, Ctx); + FunctionDecl *FDecl = dyn_cast(Fn); + if (!FDecl) + FDecl = cast(Fn)->getTemplatedDecl(); // Add the classes and namespaces associated with the parameter // types and return type of this function. diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index 9f175f962b..72a85cc968 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -4465,6 +4465,9 @@ Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, bool FoundNonTemplateFunction = false; for (llvm::SmallVectorImpl::iterator I = Fns.begin(), E = Fns.end(); I != E; ++I) { + // Look through any using declarations to find the underlying function. + NamedDecl *Fn = (*I)->getUnderlyingDecl(); + // C++ [over.over]p3: // Non-member functions and static member functions match // targets of type "pointer-to-function" or "reference-to-function." @@ -4473,7 +4476,7 @@ Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, // Note that according to DR 247, the containing class does not matter. if (FunctionTemplateDecl *FunctionTemplate - = dyn_cast(*I)) { + = dyn_cast(Fn)) { if (CXXMethodDecl *Method = dyn_cast(FunctionTemplate->getTemplatedDecl())) { // Skip non-static function templates when converting to pointer, and @@ -4510,7 +4513,7 @@ Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, continue; } - if (CXXMethodDecl *Method = dyn_cast(*I)) { + if (CXXMethodDecl *Method = dyn_cast(Fn)) { // Skip non-static functions when converting to pointer, and static // when converting to member pointer. if (Method->isStatic() == IsMember) @@ -4522,7 +4525,7 @@ Sema::ResolveAddressOfOverloadedFunction(Expr *From, QualType ToType, } else if (IsMember) continue; - if (FunctionDecl *FunDecl = dyn_cast(*I)) { + if (FunctionDecl *FunDecl = dyn_cast(Fn)) { QualType ResultTy; if (Context.hasSameUnqualifiedType(FunctionType, FunDecl->getType()) || IsNoReturnConversion(Context, FunDecl->getType(), FunctionType, diff --git a/test/SemaCXX/using-decl-1.cpp b/test/SemaCXX/using-decl-1.cpp index fe4d167fe2..e8a7d70976 100644 --- a/test/SemaCXX/using-decl-1.cpp +++ b/test/SemaCXX/using-decl-1.cpp @@ -42,3 +42,21 @@ struct X1 : X0 { struct A { void f(); }; struct B : A { }; class C : B { using B::f; }; + +// PR5751: Resolve overloaded functions through using decls. +namespace O { + void f(int i); + void f(double d); +} +namespace P { + void f(); + void g(void (*ptr)(int)); + using O::f; + void test() { + f(); + f(1); + void (*f_ptr1)(double) = f; + void (*f_ptr2)() = f; + g(f); + } +}