[cfe-dev] fatal error: error opening file '<invalid loc>'

Nikola Smiljanic popizdeh at gmail.com
Fri Jun 26 04:03:17 PDT 2015


You probably want to follow this tutorial instead
http://clang.llvm.org/docs/RAVFrontendAction.html

On Fri, Jun 26, 2015 at 6:26 PM, S, Manohar <Manohar.S at netapp.com> wrote:

>  Hi Klimek,
>
>
>
> Thanks for the quick reply. I was through with that LibTooling example and
> trying new things as given in the somewhat old tutorial at
> http://eli.thegreenplace.net/2012/06/08/basic-source-to-source-transformation-with-clang
> <https://urldefense.proofpoint.com/v2/url?u=http-3A__eli.thegreenplace.net_2012_06_08_basic-2Dsource-2Dto-2Dsource-2Dtransformation-2Dwith-2Dclang&d=AwMGaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=CnzuN65ENJ1H9py9XLiRvC_UQz6u3oG6GUNn7_wosSM&m=MEDKfUx4oj-GyjecE3c9YcCuM0kD5JO9xL1sykiDvAU&s=grpMrzNCkPB_eGWMsSbkdWWzMhL2YxH4z-gHKd7B8fs&e=>
>
>
>
> It would be of great help if you could point out the root cause.
>
>
>
> -Manohar
>
>
>
> *From:* Manuel Klimek [mailto:klimek at google.com]
> *Sent:* Thursday, June 25, 2015 9:40 PM
> *To:* S, Manohar; cfe-dev at cs.uiuc.edu
> *Subject:* Re: [cfe-dev] fatal error: error opening file '<invalid loc>'
>
>
>
> This is what we created libTooling for (
> http://clang.llvm.org/docs/LibTooling.html
> <https://urldefense.proofpoint.com/v2/url?u=http-3A__clang.llvm.org_docs_LibTooling.html&d=AwMGaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=CnzuN65ENJ1H9py9XLiRvC_UQz6u3oG6GUNn7_wosSM&m=MEDKfUx4oj-GyjecE3c9YcCuM0kD5JO9xL1sykiDvAU&s=yRCtEtcAyCQqwl9KQS2T4Fqdp4BOualWOxymF6Qn_hU&e=>
> ).
>
>
>
> On Thu, Jun 25, 2015 at 9:06 AM S, Manohar <Manohar.S at netapp.com> wrote:
>
>  Hi,
>
>
>
> I just started with Clang and already quite impressed with its
> capabilities J.
>
>
>
> While trying to run the below program on .cpp file (say
> PrintFunctions.cpp) it is throwing up:
>
>
>
> fatal error: error opening file '<invalid loc>'
>
>
>
> while ParseAST() is called. When I debugged, it is failing because of
> FID.ID
> <https://urldefense.proofpoint.com/v2/url?u=http-3A__FID.ID&d=AwMGaQ&c=8hUWFZcy2Z-Za5rBPlktOQ&r=CnzuN65ENJ1H9py9XLiRvC_UQz6u3oG6GUNn7_wosSM&m=MEDKfUx4oj-GyjecE3c9YcCuM0kD5JO9xL1sykiDvAU&s=eWjIzC6rLli-6Ysbk3IOz-_CU263FgV7ED_TWUEuaC0&e=>
> == 0, in Preprocessor::EnterSourceFile().
>
>
>
> I couldn’t get the root cause of the problem as the passing .cpp source
> file as argument seems to be fine.
>
>
>
> Can somebody please help me fix the problem?
>
>
>
> Thanks in advance,
>
>
>
> Regards,
>
> Manohar
>
>
>
>
>
> The Program:
>
> #include <cstdio>
>
> #include <string>
>
> #include <iostream>
>
> #include <sstream>
>
> #include <map>
>
> #include <utility>
>
> #include "clang/AST/ASTConsumer.h"
>
> #include "clang/AST/RecursiveASTVisitor.h"
>
> #include "clang/Basic/Diagnostic.h"
>
> #include "clang/Basic/FileManager.h"
>
> #include "clang/Basic/SourceManager.h"
>
> #include "clang/Basic/TargetOptions.h"
>
> #include "clang/Basic/TargetInfo.h"
>
> #include "clang/Frontend/CompilerInstance.h"
>
> #include "clang/Lex/Preprocessor.h"
>
> #include "clang/Parse/ParseAST.h"
>
> #include "clang/Rewrite/Core/Rewriter.h"
>
> #include "clang/Rewrite/Frontend/Rewriters.h"
>
> #include "llvm/Support/Host.h"
>
> #include "llvm/Support/raw_ostream.h"
>
>
>
> #include "clang/AST/Stmt.h"
>
>
>
> using namespace clang;
>
> using namespace std;
>
> class MyASTVisitor : public RecursiveASTVisitor<MyASTVisitor>
>
> {
>
>       public:
>
>             bool VisitStmt(Stmt *s) {
>
>                   // Print name of sub‐class of s
>
>                   printf("\t%s\n", s->getStmtClassName());
>
>                   return true;
>
>             }
>
>             bool VisitFunctionDecl(FunctionDecl *f) {
>
>                   // Print function name
>
>                   printf("%s\n", f->getName());
>
>                   return true;
>
>             }
>
> };
>
>
>
> class MyASTConsumer : public ASTConsumer
>
> {
>
>       public:
>
>             MyASTConsumer()
>
>                   : Visitor() //initialize MyASTVisitor
>
>             {}
>
>             virtual bool HandleTopLevelDecl(DeclGroupRef DR) {
>
>                   for (DeclGroupRef::iterator b = DR.begin(), e =
> DR.end(); b != e; ++b) {
>
>                         // Travel each function declaration using
> MyASTVisitor
>
>                         Visitor.TraverseDecl(*b);
>
>                   }
>
>                   return true;
>
>             }
>
>       private:
>
>             MyASTVisitor Visitor;
>
> };
>
>
>
> int main(int argc, char *argv[])
>
> {
>
>       if (argc != 2) {
>
>             llvm::errs() << "Usage: PrintFunctions <filename>\n";
>
>             return 1;
>
>       }
>
>       // CompilerInstance will hold the instance of the Clang compiler for
> us,
>
>       // managing the various objects needed to run the compiler.
>
>       CompilerInstance TheCompInst;
>
>       // Diagnostics manage problems and issues in compile
>
>       TheCompInst.createDiagnostics(NULL, false);
>
>       // Set target platform options
>
>       // Initialize target info with the default triple for our platform.
>
>       shared_ptr<TargetOptions> TO = make_shared<TargetOptions>();
>
>       //TargetOptions *TO = new TargetOptions();
>
>       TO->Triple = llvm::sys::getDefaultTargetTriple();
>
>       TargetInfo *TI =
> TargetInfo::CreateTargetInfo(TheCompInst.getDiagnostics(), TO);
>
>       TheCompInst.setTarget(TI);
>
>
>
>       // FileManager supports for file system lookup, file system caching,
> and directory search management.
>
>       TheCompInst.createFileManager();
>
>       FileManager &FileMgr = TheCompInst.getFileManager();
>
>
>
>       // SourceManager handles loading and caching of source files into
> memory.
>
>       TheCompInst.createSourceManager(FileMgr);
>
>       SourceManager &SourceMgr = TheCompInst.getSourceManager();
>
>
>
>       // Prreprocessor runs within a single source file
>
>       TheCompInst.createPreprocessor(TU_Complete);
>
>
>
>       // ASTContext holds long‐lived AST nodes (such as types and decls) .
>
>       TheCompInst.createASTContext();
>
>
>
>       // A Rewriter helps us manage the code rewriting task.
>
>       Rewriter TheRewriter;
>
>       TheRewriter.setSourceMgr(SourceMgr, TheCompInst.getLangOpts());
>
>
>
>       // Set the main file handled by the source manager to the input file.
>
>       const FileEntry *FileIn = FileMgr.getFile(argv[1]);
>
>       SourceMgr.createFileID(FileIn, SourceLocation(), SrcMgr::C_User);
> //SM: Changed from CreateMainFileID()
>
>
>
>       // Inform Diagnostics that processing of a source file is beginning.
>
>
> TheCompInst.getDiagnosticClient().BeginSourceFile(TheCompInst.getLangOpts(),&TheCompInst.getPreprocessor());
>
>
>
>       // Create an AST consumer instance which is going to get called by
> ParseAST.
>
>       MyASTConsumer TheConsumer;
>
>       // Parse the file to AST, registering our consumer as the AST
> consumer.
>
>
>
>       ParseAST(TheCompInst.getPreprocessor(), &TheConsumer,
> TheCompInst.getASTContext());
>
>       return 0;
>
> }
>
>
>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150626/7ae2336e/attachment.html>


More information about the cfe-dev mailing list