clang或libtooling如何在预处理中扩展宏并传递给解析器 之前之后

问题描述

我正在使用源到源转换工具。

我知道我们可以在预处理器中添加回调,并且在以下方法中,我可以获得宏扩展代码

void HandleMacro::MacroExpands(const Token &MacroNametok,const MacroDeFinition &MD,SourceRange Range,const MacroArgs *ConstArgs) {


    if (info->isFunctionLike()) {

        //get the expanded code 
        const MacroInfo* info = MD.getMacroInfo();
        const auto mapping = _createParameterMap(*info,*ConstArgs);
        std::string text = getExpandMacroText(*info,mapping);

        //rewrite to file
        clang::Rewriter rewriter(sourceManager,compilerInstance->getLangOpts());
        std::string content = rewriter.getRewrittenText(Range);
        rewriter.ReplaceText(Range,text);
        string Filename = sourceManager.getFileEntryForID(sourceManager.getMainFileID())->getName();
        std::error_code error_code;
        llvm::raw_fd_ostream outFile(Filename,error_code,llvm::sys::fs::F_None);
        rewriter.getEditBuffer(sourceManager.getMainFileID()).write(outFile);
    }
}

例如

之前

//macro define
#define CallRquest(NAME) {[self call ## NAME ##Request];}

//invocation
- (void)test {
    CallRquest(POST);
}

//method
- (void)callPOSTRequest {
    
}

之后

- (void)test {
    [self callPOSTRequest];
}


但是,我发现扩展的代码没有传递给解析器,并且我不想再次重新解析整个源代码树。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)