<div dir="ltr">What's your overall goal? If you want to write a clang AST based too, I'd propose to look at libtooling (<a href="http://clang.llvm.org/docs/LibTooling.html">http://clang.llvm.org/docs/LibTooling.html</a>) which handles all this setup for you.</div><br><div class="gmail_quote"><div dir="ltr">On Tue, Nov 1, 2016 at 3:19 PM Ivan Kush via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I'm reading the book `Getting Started with LLVM Core Libraries` and trying to implement an example from page 100 `Chapter 4: Frontend`. But got an error on the last line of code `clang::ParseAST(CI.getSema());`:<br class="gmail_msg">
<br class="gmail_msg">
    [bash@bash book]$ ./book_clang test.c<br class="gmail_msg">
    fatal error: error opening file '<invalid loc>':<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
What should I add to prepare for `ParseAST`?<br class="gmail_msg">
<br class="gmail_msg">
code:<br class="gmail_msg">
<br class="gmail_msg">
    #include "llvm/ADT/IntrusiveRefCntPtr.h"<br class="gmail_msg">
    #include "llvm/Support/CommandLine.h"<br class="gmail_msg">
<br class="gmail_msg">
    #include "clang/Basic/Diagnostic.h"<br class="gmail_msg">
<br class="gmail_msg">
    #include "clang/Frontend/CompilerInstance.h"<br class="gmail_msg">
<br class="gmail_msg">
    #include "clang/Basic/TargetInfo.h"<br class="gmail_msg">
    #include "clang/Basic/TargetOptions.h"<br class="gmail_msg">
<br class="gmail_msg">
    #include "llvm/Support/Host.h"<br class="gmail_msg">
    #include <memory><br class="gmail_msg">
    #include <string><br class="gmail_msg">
<br class="gmail_msg">
    #include "clang/Basic/FileManager.h"<br class="gmail_msg">
    #include "clang/Basic/SourceManager.h"<br class="gmail_msg">
    #include "clang/Lex/PreprocessorOptions.h"<br class="gmail_msg">
    #include "clang/Lex/Preprocessor.h"<br class="gmail_msg">
<br class="gmail_msg">
    #include "clang/AST/ASTConsumer.h"<br class="gmail_msg">
    #include "clang/Frontend/ASTConsumers.h"<br class="gmail_msg">
<br class="gmail_msg">
    #include "llvm/Support/raw_ostream.h"<br class="gmail_msg">
<br class="gmail_msg">
    #include "clang/Parse/ParseAST.h"<br class="gmail_msg">
<br class="gmail_msg">
    #include "clang/AST/ASTContext.h"<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
    static llvm::cl::opt<std::string> fileName(llvm::cl::Positional, llvm::cl::desc("Input file"), llvm::cl::Required);<br class="gmail_msg">
<br class="gmail_msg">
    int main(int argc, char **argv)<br class="gmail_msg">
    {<br class="gmail_msg">
        llvm::cl::ParseCommandLineOptions(argc, argv, "My simple front end\n");<br class="gmail_msg">
        clang::CompilerInstance CI;<br class="gmail_msg">
        clang::DiagnosticOptions diagnosticOptions;<br class="gmail_msg">
        CI.createDiagnostics();<br class="gmail_msg">
<br class="gmail_msg">
        std::shared_ptr<clang::TargetOptions> PTO = std::make_shared<clang::TargetOptions>();<br class="gmail_msg">
        PTO->Triple = llvm::sys::getDefaultTargetTriple();<br class="gmail_msg">
        clang::TargetInfo *PTI = clang::TargetInfo::CreateTargetInfo(CI.getDiagnostics(), PTO);<br class="gmail_msg">
        CI.setTarget(PTI);<br class="gmail_msg">
<br class="gmail_msg">
        CI.createFileManager();//References getFileSystemOpts(), clang::vfs::getRealFileSystem(), hasVirtualFileSystem(), and setVirtualFileSystem().<br class="gmail_msg">
        CI.createSourceManager( CI.getFileManager());//References getDiagnostics().<br class="gmail_msg">
        CI.createPreprocessor(clang::TU_Complete);<br class="gmail_msg">
        CI.getPreprocessorOpts().UsePredefines = false;<br class="gmail_msg">
<br class="gmail_msg">
        std::unique_ptr< clang::ASTConsumer > astConsumer = clang::CreateASTPrinter(NULL, "");<br class="gmail_msg">
        CI.setASTConsumer(std::move(astConsumer));<br class="gmail_msg">
<br class="gmail_msg">
        CI.createASTContext();<br class="gmail_msg">
        CI.createSema(clang::TU_Complete, NULL);//after getASTConsumer(), getASTContext(), and getPreprocessor().<br class="gmail_msg">
        const clang::FileEntry *file =    CI.getFileManager().getFile(fileName);<br class="gmail_msg">
        if (!file) {<br class="gmail_msg">
            llvm::errs() << "File not found: " << fileName;<br class="gmail_msg">
            return 1;<br class="gmail_msg">
        }<br class="gmail_msg">
        CI.getSourceManager().createFileID(file, clang::SourceLocation(), clang::SrcMgr::C_User);<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
        CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);<br class="gmail_msg">
        clang::ParseAST(CI.getSema());<br class="gmail_msg">
        // Print AST statistics<br class="gmail_msg">
    //   CI.getASTContext().PrintStats();<br class="gmail_msg">
    //  CI.getASTContext().Idents.PrintStats();<br class="gmail_msg">
<br class="gmail_msg">
        return 0;<br class="gmail_msg">
    }<br class="gmail_msg">
<br class="gmail_msg">
for executable need to pass c-file, for example:<br class="gmail_msg">
**test.c**<br class="gmail_msg">
<br class="gmail_msg">
    int main() {<br class="gmail_msg">
    char *msg = "Hello, world!\n";<br class="gmail_msg">
    write(1, msg, 14);<br class="gmail_msg">
    return 0;<br class="gmail_msg">
    }<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
-- <br class="gmail_msg">
Best wishes,<br class="gmail_msg">
Ivan Kush<br class="gmail_msg">
_______________________________________________<br class="gmail_msg">
cfe-dev mailing list<br class="gmail_msg">
<a href="mailto:cfe-dev@lists.llvm.org" class="gmail_msg" target="_blank">cfe-dev@lists.llvm.org</a><br class="gmail_msg">
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" class="gmail_msg" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br class="gmail_msg">
</blockquote></div>