[cfe-dev] Mocking via Clang
Reid Kleckner via cfe-dev
cfe-dev at lists.llvm.org
Thu Jan 19 11:02:24 PST 2017
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> 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
> 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/20170119/17614bac/attachment.html>
More information about the cfe-dev
mailing list