[cfe-dev] CodeGen

郭志全 gzq_study at 163.com
Wed Jul 30 18:44:18 PDT 2014


I'm learning the clang source code,try to understand how llvm translate .cpp to .ll
when I read the CodeGenModule.cpp

02271   if (GV->getType()->getElementType() != Ty) {
02272     // If the types mismatch then we have to rewrite the definition.
02273     assert(GV->isDeclaration() && "Shouldn't replace non-declaration");
02274
02275     // F is the Function* for the one with the wrong type, we must make a new
02276     // Function* and update everything that used F (a declaration) with the new
02277     // Function* (which will be a definition).
02278     //
02279     // This happens if there is a prototype for a function
02280     // (e.g. "int f()") and then a definition of a different type
02281     // (e.g. "int f(int x)").  Move the old function aside so that it
02282     // doesn't interfere with GetAddrOfFunction.
02283     GV->setName(StringRef());
02284     auto *NewFn = cast<llvm::Function>(GetAddrOfFunction(GD, Ty));
02285
02286     // This might be an implementation of a function without a
02287     // prototype, in which case, try to do special replacement of
02288     // calls which match the new prototype.  The really key thing here
02289     // is that we also potentially drop arguments from the call site
02290     // so as to make a direct call, which makes the inliner happier
02291     // and suppresses a number of optimizer warnings (!) about
02292     // dropping arguments.
02293     if (!GV->use_empty()) {
02294       ReplaceUsesOfNonProtoTypeWithRealFunction(GV, NewFn);
02295       GV->removeDeadConstantUsers();
02296     }
02297
02298     // Replace uses of F with the Function we will endow with a body.
02299     if (!GV->use_empty()) {
02300       llvm::Constant *NewPtrForOldDecl =
02301           llvm::ConstantExpr::getBitCast(NewFn, GV->getType());
02302       GV->replaceAllUsesWith(NewPtrForOldDecl);
02303     }
02304
02305     // Ok, delete the old function now, which is dead.
02306     GV->eraseFromParent();
02307
02308     GV = NewFn;
02309   }


I have a test


//aa.c


#include <stdio.h>

void fun(int a,int b);

void fun()
{
        printf("no parameters\n");
}
int main()
{
        fun();
        return 0;
}


compiler using clang


aa.c:11:6: error: too few arguments to function call, expected 2, have 0
        fun();
        ~~~ ^
aa.c:5:1: note: 'fun' declared here
void fun()
^
1 error generated.

This Is it right?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140731/2dd0f694/attachment.html>


More information about the cfe-dev mailing list