CodeCompletion for C++ Code in Buffers

Hello,

I'm trying to perform code completion on code which is only in memory
buffer(s).
Is there a way to do this? I played around with the remapped file option but
that does not work
since a real file seems always be required. I debugged thru the clang code
but i did not find
any obvious way to do so.
Any hints appreciated.

Cheers

A file name is always required for things like the __FILE__ builtin macro to work, but it doesn't have to be a real file. You can just specify a name ("<unsaved file>" or similar) and it will work fine.

David

David Chisnall-4 wrote

You can just specify a name ("
<unsaved file>
" or similar) and it will work fine.

Hi David,

thanks for your reply.
I already tried that with no success.
My test is a simple sequence of
clang_createIndex, clang_parseTranslationUnit and clang_codeCompleteAt.

David Chisnall-4 wrote

> You can just specify a name ("
> <unsaved file>
> " or similar) and it will work fine.

Hi David,

thanks for your reply.
I already tried that with no success.
My test is a simple sequence of
clang_createIndex, clang_parseTranslationUnit and clang_codeCompleteAt.

-----------------------------------------------

const char* tmpCpp =
"struct MyStruct { void myFunc(int i); };\r\n"
"int main()\r\n"
"{\r\n"
" MyStruct s;\r\n"
" s.\r\n";

#define SRC_FILE "<unsaved_file>"

int main()
{
    CXIndex idx = clang_createIndex(1, 0);
    CXUnsavedFile* uf = new CXUnsavedFile[1];
    uf[0].Filename = SRC_FILE;
    uf[0].Contents = tmpCpp;
    uf[0].Length = strlen(tmpCpp);

    CXTranslationUnit u = clang_parseTranslationUnit(idx, SRC_FILE, nullptr,
0, uf, 1, 0);
    CXCodeCompleteResults* res = clang_codeCompleteAt(u, SRC_FILE, 5, 6, uf,
1, clang_defaultCodeCompleteOptions());
}

-----------------------------------------------

If i define SRC_FILE to a real file of whatever content, everything works
fine.
But if the file does not exist, clang crashes in

clang_parseTranslationUnit_Impl()

at this line:

if (isASTReadError(Unit ? Unit.get() : ErrUnit.get())) <--------------

The problem is that both Unit and ErrUnit are nulltr and there is no
further check in isASTReadError() for nullptr. Is this a bug i did i make
something wrong?

Your code looks fine to me.

I've reported a bug report about this issue a while ago already:
  https://llvm.org/bugs/show_bug.cgi?id=22282

We've seen the same issue in KDevelop.