[cfe-dev] Mocking via Clang

Eran Jakoby via cfe-dev cfe-dev at lists.llvm.org
Thu Jan 26 04:05:20 PST 2017


--wrap will not allow me to mock functions in my current translation unit.
Is there something wrong with the way I'm adding a new function here to the new AST?
How should/can I connect a newly created function in a proper way so that in appears in the AST it afterwards in the object file? (Currently it doesn't).
Thanks,
Eran


From: Reid Kleckner [mailto:rnk at google.com]
Sent: Thursday, January 19, 2017 9:02 PM
To: Eran Jakoby <eranj at mellanox.com>
Cc: cfe-dev at lists.llvm.org
Subject: Re: [cfe-dev] Mocking via Clang

It's probably easier to do this with ld --wrap than with clang.

On Wed, Jan 18, 2017 at 7:08 AM, Eran Jakoby via cfe-dev <cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>> wrote:
Hi,
I'm trying to utilize Clang for mocking.
Starting for simplicity from C language – I would like to create:

1.       Identical function for each function found (e.g. foo_wrapper() for foo()).

2.       All calls to original function (e.g. foo()) should instead call the wrapper (e.g. foo_wrapper()).
(the wrappers themselves can be left as undefined symbols to be resolved in link).

I started by using Clang plug-in with a RecursiveASTVisitor that when visiting a function does the following code: (the visitor is called from HandleTopLevelDecl of an ASTConsumer)
                                FunctionDecl* NewFD=FunctionDecl::Create(getContext(), getContext().getTranslationUnitDecl(), f->getSourceRange().getBegin(), f->getSourceRange().getEnd(), DeclarationName(&newNameIdInfo), f->getReturnType(), f->getTypeSourceInfo(), f->getStorageClass());
                                NewFD->setLexicalDeclContext(getContext().getTranslationUnitDecl());
                                SmallVector<ParmVarDecl*, 16> Params;
                                for(auto iter = f->param_begin(); iter!=f->param_end(); ++iter) {
                                                Params.push_back(*iter);
                                }
                                NewFD->setParams(Params);

However this does not work and does not add an identical function to be used as wrapper (using nm I don't see the wrapper function symbol).
Moreover I have no idea how afterwards to set all calls to foo_wrapper instead of foo.

Is my general approach correct? Am I missing something?
Any advice would be greatly appreciated.
Thanks!
Eran


_______________________________________________
cfe-dev mailing list
cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170126/9807b9f1/attachment.html>


More information about the cfe-dev mailing list