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.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@55339 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CodeGenFunction.cpp b/lib/CodeGen/CodeGenFunction.cpp
index af63be7..11fde87 100644
--- a/lib/CodeGen/CodeGenFunction.cpp
+++ b/lib/CodeGen/CodeGenFunction.cpp
@@ -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());
 }