Executing a FrontendAction on a MemoryBuffer

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Executing a FrontendAction on a MemoryBuffer

Mike Miller
Hi,

I'm currently using the following code(excerpted) to execute a FrontendAction(an EmitObjAction specifically) on source code contained in a MemoryBuffer:

llvm::MemoryBuffer* Buffer = llvm::MemoryBuffer::getMemBuffer(Source);
SourceManager& SM = Clang.getSourceManager();
FileID MainFileID = SM.createMainFileIDForMemBuffer(Buffer);

EmitObjAction E;
E.setCompilerInstance(&Clang);
E.Execute();

The code fails at "E.Execute()" with:

Assertion failed: (!CurrentFile.empty() && "No current file!"), function isCurrentFileAST, file /Volumes/Data/Users/mike/llvm/tools/clang/lib/Frontend/../../include/clang/Frontend/FrontendAction.h, line 95.


I looked up the FrontendAction::setCurrentFile method, but it takes in a StringRef to a path. Since I don't have a path to a file, as I'm only operating on source code in memory, I'm not able to set the current file.

How can I set the current file to point to a MemoryBuffer?

Thanks!
Mike

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Executing a FrontendAction on a MemoryBuffer

Mike Miller
Some folks on #llvm helped me with this - the solution is to create a virtual file using FileManager::getVirtualFile, and populate it with the buffer's contents using SourceManager::overrideFileContents, then pass the virtual file to the FrontendAction.

Hope that helps,
Mike

On Fri, May 7, 2010 at 5:40 PM, Mike Miller <[hidden email]> wrote:
Hi,

I'm currently using the following code(excerpted) to execute a FrontendAction(an EmitObjAction specifically) on source code contained in a MemoryBuffer:

llvm::MemoryBuffer* Buffer = llvm::MemoryBuffer::getMemBuffer(Source);
SourceManager& SM = Clang.getSourceManager();
FileID MainFileID = SM.createMainFileIDForMemBuffer(Buffer);

EmitObjAction E;
E.setCompilerInstance(&Clang);
E.Execute();

The code fails at "E.Execute()" with:

Assertion failed: (!CurrentFile.empty() && "No current file!"), function isCurrentFileAST, file /Volumes/Data/Users/mike/llvm/tools/clang/lib/Frontend/../../include/clang/Frontend/FrontendAction.h, line 95.


I looked up the FrontendAction::setCurrentFile method, but it takes in a StringRef to a path. Since I don't have a path to a file, as I'm only operating on source code in memory, I'm not able to set the current file.

How can I set the current file to point to a MemoryBuffer?

Thanks!
Mike


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev