Need some brief explanation about llvm::MemoryBuffer and llvm::SourceMgr

Hello list,

I learned that under LLVM, #including of <iostream> is forbidden.
Instead LLVM provides llvm::raw_ostream(for std::cout, std::cerr) and
llvm::MemoryBuffer(for input stream).

And using of llvm::raw_ostream is pretty easy but for me learning of
how to use llvm::MemoryBuffer is pretty much difficult.

I found a good sample code; utils/yaml2obj/yaml2obj.cpp.
The function, main() in this file provides a hint which is not enough
for me though.

I still cannot figure out how to use llvm::MemoryBuffer.
What I need is just a means of doing std::cin.

Could someone explain about this?

Thank you in advance.

Is this for user prompts, or just reading data from stdin? You can use MemoryBuffer::getSTDIN to read the contents of stdin into a memory buffer. Then you can get the data pointer and size and read it in. As far as I know, there is not a good way to implement user prompts with the LLVM APIs.

Hi Justin Holewinski,

As far as I know, there is not a good way to implement user prompts with the LLVM APIs.

This info helps me a lot.
I tried to do a thing that is not possible.

Thank you very much.

Sincerely
Journeyer

And for this kind of question I think I would better hold in.
There would be more important issue worth on this list.

For this reason I am sorry.

Sincerely
Journeyer

Hello Justin Holewinski,

I tested MemoryBuffer class.
And this test gives understanding of how to use it.
Though this is simple issue, I think I'd better post it here for
someone who might appear after me.

~/Projects/build/Debug+Asserts/bin/clang++ MemoryBufferTest.cpp
`~/Projects/build/Debug+Asserts/bin/llvm-config --cppflags --ldflags
--libs core` -o memorybuffertest

./memorybuffertest <any .txt file input>
--> tests input of a file

./memorybuffertest -
--> tests input of STDIN

MemoryBufferTest.cpp follows below.

#include "llvm/ADT/OwningPtr.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/system_error.h"

using namespace llvm;

int main(int argc, char **argv)
{
  if (argc != 2) return 1;
  OwningPtr<MemoryBuffer> Buf;
  if (MemoryBuffer::getFileOrSTDIN(argv[1], Buf))
    return 1;

  llvm::errs() << "size : " << Buf->getBufferSize() << "\n";
  llvm::errs() << "start : " << Buf->getBufferStart() << "\n";
  llvm::errs() << "end : " << Buf->getBufferEnd() << "\n";
  llvm::errs() << "buffer : " << Buf->getBuffer() << "\n";

  return 0;
}

Thank you.
Sincerely
Journeyer