[cfe-dev] prepare for calling ParseAST. Fatal error

Ivan Kush via cfe-dev cfe-dev at lists.llvm.org
Fri Nov 4 11:44:00 PDT 2016


the problem is solved. Need to set MainFileID for CompilerInstence's SourceManager

   clang::FileID mainFileID = CI.getSourceManager().createFileID(file, clang::SourceLocation(), clang::SrcMgr::C_User);
    CI.getSourceManager().setMainFileID(mainFileID);



corrected full code is:


#include "llvm/Support/CommandLine.h"

#include "clang/Basic/Diagnostic.h"

#include "clang/Frontend/CompilerInstance.h"

#include "clang/Basic/TargetInfo.h"
#include "clang/Basic/TargetOptions.h"

#include "llvm/Support/Host.h"
#include <memory>
#include <string>

#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Lex/Preprocessor.h"

#include "clang/AST/ASTConsumer.h"
#include "clang/Frontend/ASTConsumers.h"

#include "llvm/Support/raw_ostream.h"

#include "clang/Parse/ParseAST.h"

#include "clang/AST/ASTContext.h"

static llvm::cl::opt<std::string> fileName(llvm::cl::Positional, llvm::cl::desc("Input file"), llvm::cl::Required);

int main(int argc, char **argv)
{   
     
    llvm::cl::ParseCommandLineOptions(argc, argv, "My simple front end\n");

    clang::CompilerInstance CI;
    clang::DiagnosticOptions diagnosticOptions;
    CI.createDiagnostics();
    
    std::shared_ptr<clang::TargetOptions> PTO = std::make_shared<clang::TargetOptions>();
    PTO->Triple = llvm::sys::getDefaultTargetTriple();
    clang::TargetInfo *PTI = clang::TargetInfo::CreateTargetInfo(CI.getDiagnostics(), PTO);
    CI.setTarget(PTI);

    //clang::FileSystemOptions fileSystemOptions;
    //clang::FileManager fileManager(fileSystemOptions);
    //CI.createFileManager(fileManager);
    
    CI.createFileManager();//References getFileSystemOpts(), clang::vfs::getRealFileSystem(), hasVirtualFileSystem(), and setVirtualFileSystem().
    CI.createSourceManager( CI.getFileManager());//References getDiagnostics().
    CI.createPreprocessor(clang::TU_Complete);
    CI.getPreprocessorOpts().UsePredefines = false;

    std::unique_ptr< clang::ASTConsumer > astConsumer = clang::CreateASTPrinter(NULL, "");
    CI.setASTConsumer(std::move(astConsumer));
    
    CI.createASTContext();
    CI.createSema(clang::TU_Complete, NULL);//after getASTConsumer(), getASTContext(), and getPreprocessor().
    const clang::FileEntry *file = CI.getFileManager().getFile(fileName);
    if (!file) {
            llvm::errs() << "File not found: " << fileName;
            return 1;
    }
    clang::FileID mainFileID = CI.getSourceManager().createFileID(file, clang::SourceLocation(), clang::SrcMgr::C_User);
    CI.getSourceManager().setMainFileID(mainFileID);


    CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
    clang::ParseAST(CI.getSema());
    // Print AST statistics
    CI.getASTContext().PrintStats();
    CI.getASTContext().Idents.PrintStats();

    return 0;
}


-- 
Best wishes,
Ivan Kush



02.11.2016, 19:17, "Manuel Klimek" <klimek at google.com>:
> What's your overall goal? If you want to write a clang AST based too, I'd propose to look at libtooling (http://clang.llvm.org/docs/LibTooling.html) which handles all this setup for you.
>
> On Tue, Nov 1, 2016 at 3:19 PM Ivan Kush via cfe-dev <cfe-dev at lists.llvm.org> wrote:
>> 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());`:
>>
>>     [bash at bash book]$ ./book_clang test.c
>>     fatal error: error opening file '<invalid loc>':
>>
>> What should I add to prepare for `ParseAST`?
>>
>> code:
>>
>>     #include "llvm/ADT/IntrusiveRefCntPtr.h"
>>     #include "llvm/Support/CommandLine.h"
>>
>>     #include "clang/Basic/Diagnostic.h"
>>
>>     #include "clang/Frontend/CompilerInstance.h"
>>
>>     #include "clang/Basic/TargetInfo.h"
>>     #include "clang/Basic/TargetOptions.h"
>>
>>     #include "llvm/Support/Host.h"
>>     #include <memory>
>>     #include <string>
>>
>>     #include "clang/Basic/FileManager.h"
>>     #include "clang/Basic/SourceManager.h"
>>     #include "clang/Lex/PreprocessorOptions.h"
>>     #include "clang/Lex/Preprocessor.h"
>>
>>     #include "clang/AST/ASTConsumer.h"
>>     #include "clang/Frontend/ASTConsumers.h"
>>
>>     #include "llvm/Support/raw_ostream.h"
>>
>>     #include "clang/Parse/ParseAST.h"
>>
>>     #include "clang/AST/ASTContext.h"
>>
>>     static llvm::cl::opt<std::string> fileName(llvm::cl::Positional, llvm::cl::desc("Input file"), llvm::cl::Required);
>>
>>     int main(int argc, char **argv)
>>     {
>>         llvm::cl::ParseCommandLineOptions(argc, argv, "My simple front end\n");
>>         clang::CompilerInstance CI;
>>         clang::DiagnosticOptions diagnosticOptions;
>>         CI.createDiagnostics();
>>
>>         std::shared_ptr<clang::TargetOptions> PTO = std::make_shared<clang::TargetOptions>();
>>         PTO->Triple = llvm::sys::getDefaultTargetTriple();
>>         clang::TargetInfo *PTI = clang::TargetInfo::CreateTargetInfo(CI.getDiagnostics(), PTO);
>>         CI.setTarget(PTI);
>>
>>         CI.createFileManager();//References getFileSystemOpts(), clang::vfs::getRealFileSystem(), hasVirtualFileSystem(), and setVirtualFileSystem().
>>         CI.createSourceManager( CI.getFileManager());//References getDiagnostics().
>>         CI.createPreprocessor(clang::TU_Complete);
>>         CI.getPreprocessorOpts().UsePredefines = false;
>>
>>         std::unique_ptr< clang::ASTConsumer > astConsumer = clang::CreateASTPrinter(NULL, "");
>>         CI.setASTConsumer(std::move(astConsumer));
>>
>>         CI.createASTContext();
>>         CI.createSema(clang::TU_Complete, NULL);//after getASTConsumer(), getASTContext(), and getPreprocessor().
>>         const clang::FileEntry *file =    CI.getFileManager().getFile(fileName);
>>         if (!file) {
>>             llvm::errs() << "File not found: " << fileName;
>>             return 1;
>>         }
>>         CI.getSourceManager().createFileID(file, clang::SourceLocation(), clang::SrcMgr::C_User);
>>
>>         CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
>>         clang::ParseAST(CI.getSema());
>>         // Print AST statistics
>>     //   CI.getASTContext().PrintStats();
>>     //  CI.getASTContext().Idents.PrintStats();
>>
>>         return 0;
>>     }
>>
>> for executable need to pass c-file, for example:
>> **test.c**
>>
>>     int main() {
>>     char *msg = "Hello, world!\n";
>>     write(1, msg, 14);
>>     return 0;
>>     }
>>
>> --
>> Best wishes,
>> Ivan Kush
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



More information about the cfe-dev mailing list