[cfe-commits] r133628 - in /cfe/trunk: lib/Sema/SemaTemplateInstantiateDecl.cpp test/SemaTemplate/instantiate-function-2.cpp

Douglas Gregor dgregor at apple.com
Wed Jun 22 11:16:25 PDT 2011


Author: dgregor
Date: Wed Jun 22 13:16:25 2011
New Revision: 133628

URL: http://llvm.org/viewvc/llvm-project?rev=133628&view=rev
Log:
When instantiating a function template declaration that was expressed
via a typedef of a function, make sure to synthesize parameter
declarations. Fixes PR9654 / <rdar://problem/9257497>.

Modified:
    cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
    cfe/trunk/test/SemaTemplate/instantiate-function-2.cpp

Modified: cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp?rev=133628&r1=133627&r2=133628&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Wed Jun 22 13:16:25 2011
@@ -1090,9 +1090,26 @@
   Function->setLexicalDeclContext(LexicalDC);
 
   // Attach the parameters
-  for (unsigned P = 0; P < Params.size(); ++P)
-    if (Params[P])
-      Params[P]->setOwningFunction(Function);
+  if (isa<FunctionProtoType>(Function->getType())) {
+    // Adopt the already-instantiated parameters into our own context.
+    for (unsigned P = 0; P < Params.size(); ++P)
+      if (Params[P])
+        Params[P]->setOwningFunction(Function);
+  } else {
+    // Since we were instantiated via a typedef of a function type, create
+    // new parameters.
+    const FunctionProtoType *Proto
+      = Function->getType()->getAs<FunctionProtoType>();
+    assert(Proto && "No function prototype in template instantiation?");
+    for (FunctionProtoType::arg_type_iterator AI = Proto->arg_type_begin(),
+         AE = Proto->arg_type_end(); AI != AE; ++AI) {
+      ParmVarDecl *Param
+        = SemaRef.BuildParmVarDeclForTypedef(Function, Function->getLocation(),
+                                             *AI);
+      Param->setScopeInfo(0, Params.size());
+      Params.push_back(Param);
+    }
+  }
   Function->setParams(Params.data(), Params.size());
 
   SourceLocation InstantiateAtPOI;

Modified: cfe/trunk/test/SemaTemplate/instantiate-function-2.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaTemplate/instantiate-function-2.cpp?rev=133628&r1=133627&r2=133628&view=diff
==============================================================================
--- cfe/trunk/test/SemaTemplate/instantiate-function-2.cpp (original)
+++ cfe/trunk/test/SemaTemplate/instantiate-function-2.cpp Wed Jun 22 13:16:25 2011
@@ -31,3 +31,14 @@
     foo<int>(); // expected-note{{instantiation of}}
   }
 }
+
+namespace PR9654 {
+  typedef void ftype(int);
+
+  template<typename T>
+  ftype f;
+
+  void g() {
+    f<int>(0);
+  }
+}





More information about the cfe-commits mailing list