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

S, Manohar Manohar.S at netapp.com
Fri Jun 26 01:26:47 PDT 2015

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

It would be of great help if you could point out the root cause.


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).

On Thu, Jun 25, 2015 at 9:06 AM S, Manohar <Manohar.S at netapp.com<mailto:Manohar.S at netapp.com>> wrote:

I just started with Clang and already quite impressed with its capabilities ☺.

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<http://FID.ID> == 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,


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>
            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
                  : 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
                  return true;
            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);

      // FileManager supports for file system lookup, file system caching, and directory search management.
      FileManager &FileMgr = TheCompInst.getFileManager();

      // SourceManager handles loading and caching of source files into memory.
      SourceManager &SourceMgr = TheCompInst.getSourceManager();

      // Prreprocessor runs within a single source file

      // ASTContext holds long‐lived AST nodes (such as types and decls) .

      // 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.

      // 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<mailto:cfe-dev at cs.uiuc.edu>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150626/72976f9b/attachment.html>

More information about the cfe-dev mailing list