[cfe-commits] r55339 - in /cfe/trunk: lib/CodeGen/CodeGenFunction.cpp lib/Sema/SemaDecl.cpp lib/Sema/SemaType.cpp test/CodeGen/kr-func-promote.c

Eli Friedman eli.friedman at gmail.com
Mon Aug 25 14:31:01 PDT 2008


Author: efriedma
Date: Mon Aug 25 16:31:01 2008
New Revision: 55339

URL: http://llvm.org/viewvc/llvm-project?rev=55339&view=rev
Log:
Do typechecking and codegen for K&R-style function declarations 
correctly.  Not a regression, but made more obvious by my recent fix 
which made function type compatibility checking a bit more strict.


Added:
    cfe/trunk/test/CodeGen/kr-func-promote.c
Modified:
    cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaType.cpp

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.cpp?rev=55339&r1=55338&r2=55339&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.cpp Mon Aug 25 16:31:01 2008
@@ -138,10 +138,23 @@
     AI->setName("agg.result");
     ++AI;
   }
-  
-  for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
-    assert(AI != CurFn->arg_end() && "Argument mismatch!");
-    EmitParmDecl(*FD->getParamDecl(i), AI);
+
+  if (FD->getNumParams()) {
+    const FunctionTypeProto* FProto = FD->getType()->getAsFunctionTypeProto();
+    assert(FProto && "Function def must have prototype!");
+    for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i, ++AI) {
+      assert(AI != CurFn->arg_end() && "Argument mismatch!");
+      const ParmVarDecl* CurParam = FD->getParamDecl(i);
+      llvm::Value* V = AI;
+      if (!getContext().typesAreCompatible(FProto->getArgType(i),
+                                           CurParam->getType())) {
+        // This must be a promotion, for something like
+        // "void a(x) short x; {..."
+        V = EmitScalarConversion(V, FProto->getArgType(i),
+                                 CurParam->getType());
+      }
+      EmitParmDecl(*CurParam, V);
+    }
   }
   GenerateFunction(FD->getBody());
 }

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Mon Aug 25 16:31:01 2008
@@ -690,8 +690,7 @@
 
     // Copy the parameter declarations from the declarator D to
     // the function declaration NewFD, if they are available.
-    if (D.getNumTypeObjects() > 0 &&
-        D.getTypeObject(0).Fun.hasPrototype) {
+    if (D.getNumTypeObjects() > 0) {
       DeclaratorChunk::FunctionTypeInfo &FTI = D.getTypeObject(0).Fun;
 
       // Create Decl objects for each parameter, adding them to the
@@ -716,7 +715,7 @@
           Diag(Param->getLocation(), diag::ext_param_typedef_of_void);
         }
 
-      } else {
+      } else if (FTI.NumArgs > 0 && FTI.ArgInfo[0].Param != 0) {
         for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i)
           Params.push_back((ParmVarDecl *)FTI.ArgInfo[i].Param);
       }
@@ -1540,11 +1539,6 @@
         FTI.ArgInfo[i].Param = ActOnParamDeclarator(FnBodyScope, ParamD);
       }
     }
-
-    // Since this is a function definition, act as though we have information
-    // about the arguments.
-    if (FTI.NumArgs)
-      FTI.hasPrototype = true;
   } else {
     // FIXME: Diagnose arguments without names in C. 
   }

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Mon Aug 25 16:31:01 2008
@@ -397,14 +397,12 @@
         D.setInvalidType(true);
       }
         
-      if (!FTI.hasPrototype) {
+      if (FTI.NumArgs == 0) {
         // Simple void foo(), where the incoming T is the result type.
         T = Context.getFunctionTypeNoProto(T);
-
+      } else if (FTI.ArgInfo[0].Param == 0) {
         // C99 6.7.5.3p3: Reject int(x,y,z) when it's not a function definition.
-        if (FTI.NumArgs != 0)
-          Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);
-        
+        Diag(FTI.ArgInfo[0].IdentLoc, diag::err_ident_list_in_fn_declaration);        
       } else {
         // Otherwise, we have a function with an argument list that is
         // potentially variadic.
@@ -458,6 +456,13 @@
               // Do not add 'void' to the ArgTys list.
               break;
             }
+          } else if (!FTI.hasPrototype) {
+            if (ArgTy->isPromotableIntegerType()) {
+              ArgTy = Context.IntTy;
+            } else if (const BuiltinType* BTy = ArgTy->getAsBuiltinType()) {
+              if (BTy->getKind() == BuiltinType::Float)
+                ArgTy = Context.DoubleTy;
+            }
           }
           
           ArgTys.push_back(ArgTy);

Added: cfe/trunk/test/CodeGen/kr-func-promote.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/kr-func-promote.c?rev=55339&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/kr-func-promote.c (added)
+++ cfe/trunk/test/CodeGen/kr-func-promote.c Mon Aug 25 16:31:01 2008
@@ -0,0 +1,5 @@
+// RUN: clang %s -emit-llvm -o - | grep "i32 @a(i32)"
+
+int a();
+int a(x) short x; {return x;}
+





More information about the cfe-commits mailing list