2017-04-01 12 views
2

私はLibToolingを使って分析を行っています。私はASTをどのようにトラバースしてどこかにテキストを挿入する方法を知っています。たとえば、LibToolingリライタを使って新しいファイルを生成するClang?

コードを保存する方法があるのでしょうか? (天気を元のファイルに保存するか、新しいファイルを作成する)

解析後、結果を端末でしか読み取ることができず、十分ではありません。

答えて

3
それはあなたのASTFrontEndActionオブジェクトに行われるべき

、EndSourceFileAction内の()関数

// For each source file provided to the tool, a new FrontendAction is created. 
class MyFrontendAction : public ASTFrontendAction { 
public: 
    MyFrontendAction() {} 
    void EndSourceFileAction() override { 
    SourceManager &SM = TheRewriter.getSourceMgr(); 
    llvm::errs() << "** EndSourceFileAction for: " 
       << SM.getFileEntryForID(SM.getMainFileID())->getName() << "\n"; 

    // Now emit the rewritten buffer. 
    // TheRewriter.getEditBuffer(SM.getMainFileID()).write(llvm::outs()); --> this will output to screen as what you got. 
    std::error_code error_code; 
    llvm::raw_fd_ostream outFile("output.txt", error_code, llvm::sys::fs::F_None); 
    TheRewriter.getEditBuffer(SM.getMainFileID()).write(outFile); // --> this will write the result to outFile 
    outFile.close(); 
    } 
//as normal, make sure it matches your ASTConsumer constructor 
    std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, 
               StringRef file) override { 

    llvm::errs() << "** Creating AST consumer for: " << file << "\n"; 
    TheRewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts()); 
    return llvm::make_unique<MyASTConsumer>(TheRewriter,&CI.getASTContext()); 
    } 

あなたが望んでいた出力ファイルですoutput.txtと。

1

mywriter.overwriteChangedFiles();その場合

関連する問題