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

Chris Lattner clattner at apple.com
Wed Oct 29 21:22:14 PDT 2008


On Oct 29, 2008, at 11:41 AM, Ted Kremenek wrote:

> 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.

Cool, thanks for working on this Ted.  There was a very similar case  
for C++.  I thought the fix there was to actually create the parameter  
decls in cases like this, I think we should do everything possible to  
keep C++ and C consistent here.

-Chris

>
>
> 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;
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits




More information about the cfe-commits mailing list