[cfe-commits] r58392 - in /cfe/trunk: lib/AST/Decl.cpp test/Sema/PR2923.c

Ted Kremenek kremenek at apple.com
Wed Oct 29 11:41:35 PDT 2008


Author: kremenek
Date: Wed Oct 29 13:41:34 2008
New Revision: 58392

URL: http://llvm.org/viewvc/llvm-project?rev=58392&view=rev
Log:
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.

Added:
    cfe/trunk/test/Sema/PR2923.c
Modified:
    cfe/trunk/lib/AST/Decl.cpp

Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=58392&r1=58391&r2=58392&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Wed Oct 29 13:41:34 2008
@@ -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) {

Added: cfe/trunk/test/Sema/PR2923.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/PR2923.c?rev=58392&view=auto

==============================================================================
--- cfe/trunk/test/Sema/PR2923.c (added)
+++ cfe/trunk/test/Sema/PR2923.c Wed Oct 29 13:41:34 2008
@@ -0,0 +1,12 @@
+// RUN: clang -fsyntax-only -verify
+
+// Test for absence of crash reported in PR 2923:
+//
+//  http://llvm.org/bugs/show_bug.cgi?id=2923
+//
+// Previously we had a crash when deallocating the FunctionDecl for 'bar'
+// because FunctionDecl::getNumParams() just used the type of foo to determine
+// the number of parameters it has.  In the case of 'bar' there are no
+// ParmVarDecls.
+int foo(int x, int y) { return x + y; }
+extern typeof(foo) bar;





More information about the cfe-commits mailing list