]> granicus.if.org Git - clang/commitdiff
Do typechecking and codegen for K&R-style function declarations
authorEli Friedman <eli.friedman@gmail.com>
Mon, 25 Aug 2008 21:31:01 +0000 (21:31 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Mon, 25 Aug 2008 21:31:01 +0000 (21:31 +0000)
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

lib/CodeGen/CodeGenFunction.cpp
lib/Sema/SemaDecl.cpp
lib/Sema/SemaType.cpp
test/CodeGen/kr-func-promote.c [new file with mode: 0644]

index af63be73fe9d07ce2e977ca605eb0b660e872c01..11fde87302891f5a461c82713e55d6bc0dda70f2 100644 (file)
@@ -138,10 +138,23 @@ void CodeGenFunction::GenerateCode(const FunctionDecl *FD,
     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());
 }
index 066969ce9e81687a0bdbc79c97d768f9cc4cb812..ad83908e4512944e7d72a9fdaa66687d461667b8 100644 (file)
@@ -690,8 +690,7 @@ Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) {
 
     // 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 @@ Sema::ActOnDeclarator(Scope *S, Declarator &D, DeclTy *lastDecl) {
           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 @@ Sema::DeclTy *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Declarator &D) {
         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. 
   }
index 820a1c85f06ab5478fa834c7267a708034ae8aeb..c7876b1151a9c2e816248c4186b2aadf20fd2989 100644 (file)
@@ -397,14 +397,12 @@ QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
         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 @@ QualType Sema::GetTypeForDeclarator(Declarator &D, Scope *S) {
               // 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);
diff --git a/test/CodeGen/kr-func-promote.c b/test/CodeGen/kr-func-promote.c
new file mode 100644 (file)
index 0000000..a20532a
--- /dev/null
@@ -0,0 +1,5 @@
+// RUN: clang %s -emit-llvm -o - | grep "i32 @a(i32)"
+
+int a();
+int a(x) short x; {return x;}
+