Fix crash reported in PR2923 where a function declared using typeof(another_function) would have FunctionDecl::getNumParams() return the number of parameters in the original function type and not the number of parameters in the actual FunctionDecl.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@58392 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp
index 3713776..dbc92f4 100644
--- a/lib/AST/Decl.cpp
+++ b/lib/AST/Decl.cpp
@@ -176,16 +176,26 @@
   return 0;
 }
 
-unsigned FunctionDecl::getNumParams() const {
-  const FunctionType *FT = getType()->getAsFunctionType();
+// Helper function for FunctionDecl::getNumParams and FunctionDecl::setParams()
+static unsigned getNumTypeParams(QualType T) {
+  const FunctionType *FT = T->getAsFunctionType();
   if (isa<FunctionTypeNoProto>(FT))
     return 0;
   return cast<FunctionTypeProto>(FT)->getNumArgs();
 }
 
+unsigned FunctionDecl::getNumParams() const {
+  // Can happen if a FunctionDecl is declared using typeof(some_other_func) bar;
+  if (!ParamInfo)
+    return 0;
+  
+  return getNumTypeParams(getType());
+}
+
 void FunctionDecl::setParams(ParmVarDecl **NewParamInfo, unsigned NumParams) {
   assert(ParamInfo == 0 && "Already has param info!");
-  assert(NumParams == getNumParams() && "Parameter count mismatch!");
+  assert(NumParams == getNumTypeParams(getType()) &&
+         "Parameter count mismatch!");
   
   // Zero params -> null pointer.
   if (NumParams) {