[cfe-commits] r62849 - in /cfe/trunk: include/clang/Parse/Action.h lib/Parse/Parser.cpp lib/Sema/Sema.h lib/Sema/SemaDecl.cpp test/Sema/function.c test/Sema/redefinition.c

Douglas Gregor dgregor at apple.com
Fri Jan 23 08:23:14 PST 2009


Author: dgregor
Date: Fri Jan 23 10:23:13 2009
New Revision: 62849

URL: http://llvm.org/viewvc/llvm-project?rev=62849&view=rev
Log:
Handle any undeclared parameters in a K&R-style function with a
special action, inside function prototype scope. This avoids confusion
when we try to inject these parameters into the scope of the function
body before the function itself has been added to the surrounding
scope. Fixes <rdar://problem/6097326>.

Modified:
    cfe/trunk/include/clang/Parse/Action.h
    cfe/trunk/lib/Parse/Parser.cpp
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/Sema/function.c
    cfe/trunk/test/Sema/redefinition.c

Modified: cfe/trunk/include/clang/Parse/Action.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Action.h?rev=62849&r1=62848&r2=62849&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Fri Jan 23 10:23:13 2009
@@ -223,6 +223,13 @@
     return Group;
   }
 
+  /// @brief Indicates that all K&R-style parameter declarations have
+  /// been parsed prior to a function definition.
+  /// @param S  The function prototype scope.
+  /// @param D  The function declarator.
+  virtual void ActOnFinishKNRParamDeclarations(Scope *S, Declarator &D) {
+  }
+
   /// ActOnStartOfFunctionDef - This is called at the start of a function
   /// definition, instead of calling ActOnDeclarator.  The Declarator includes
   /// information about formal arguments that are part of this function.

Modified: cfe/trunk/lib/Parse/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/Parser.cpp?rev=62849&r1=62848&r2=62849&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/Parser.cpp (original)
+++ cfe/trunk/lib/Parse/Parser.cpp Fri Jan 23 10:23:13 2009
@@ -674,6 +674,7 @@
   }
 
   // The actions module must verify that all arguments were declared.
+  Actions.ActOnFinishKNRParamDeclarations(CurScope, D);
 }
 
 

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=62849&r1=62848&r2=62849&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Fri Jan 23 10:23:13 2009
@@ -311,6 +311,7 @@
   void ActOnUninitializedDecl(DeclTy *dcl);
   virtual DeclTy *FinalizeDeclaratorGroup(Scope *S, DeclTy *Group);
 
+  virtual void ActOnFinishKNRParamDeclarations(Scope *S, Declarator &D);
   virtual DeclTy *ActOnStartOfFunctionDef(Scope *S, Declarator &D);
   virtual DeclTy *ActOnStartOfFunctionDef(Scope *S, DeclTy *D);
   virtual void ObjCActOnStartOfMethodDef(Scope *S, DeclTy *D);

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=62849&r1=62848&r2=62849&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Fri Jan 23 10:23:13 2009
@@ -2633,8 +2633,7 @@
 
 }
 
-Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
-  assert(getCurFunctionDecl() == 0 && "Function parsing confused");
+void Sema::ActOnFinishKNRParamDeclarations(Scope *S, Declarator &D) {
   assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
          "Not a function declarator!");
   DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
@@ -2654,10 +2653,19 @@
                            PrevSpec);
         Declarator ParamD(DS, Declarator::KNRTypeListContext);
         ParamD.SetIdentifier(FTI.ArgInfo[i].Ident, FTI.ArgInfo[i].IdentLoc);
-        FTI.ArgInfo[i].Param = ActOnParamDeclarator(FnBodyScope, ParamD);
+        FTI.ArgInfo[i].Param = ActOnParamDeclarator(S, ParamD);
       }
     }
-  } else {
+  } 
+}
+
+Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
+  assert(getCurFunctionDecl() == 0 && "Function parsing confused");
+  assert(D.getTypeObject(0).Kind == DeclaratorChunk::Function &&
+         "Not a function declarator!");
+  DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
+
+  if (FTI.hasPrototype) {
     // FIXME: Diagnose arguments without names in C. 
   }
   

Modified: cfe/trunk/test/Sema/function.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/function.c?rev=62849&r1=62848&r2=62849&view=diff

==============================================================================
--- cfe/trunk/test/Sema/function.c (original)
+++ cfe/trunk/test/Sema/function.c Fri Jan 23 10:23:13 2009
@@ -39,3 +39,7 @@
 int t14() {
   return; // expected-warning {{non-void function 't14' should return a value}}
 }
+
+// <rdar://problem/6097326>
+y(y) { return y; } // expected-warning{{parameter 'y' was not declared, defaulting to type 'int'}} \
+                   // expected-warning{{type specifier missing, defaults to 'int'}}

Modified: cfe/trunk/test/Sema/redefinition.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/redefinition.c?rev=62849&r1=62848&r2=62849&view=diff

==============================================================================
--- cfe/trunk/test/Sema/redefinition.c (original)
+++ cfe/trunk/test/Sema/redefinition.c Fri Jan 23 10:23:13 2009
@@ -3,3 +3,8 @@
 int f(int);
 int f(int a) { } // expected-error {{redefinition of 'f'}}
 
+// <rdar://problem/6097326>
+int foo(x) {
+  return 0;
+}
+int x = 1;





More information about the cfe-commits mailing list