2

I'm having a problem with libclang.

I'm currently writing a C++ parser in C++ using libclang, and apparently, clang does not know where my headers are, despite having specified the include directory. The file I want to parse includes other headers located inside the same project, but clang does not seem to be able to find them. Basically, inside the cursor visiting, nothing prints out, except I should have thousands of lines on my screen. Changing the include paths to use an absolute path works, but that's not what I want. I'd like to parse a project regularly without having to touch anything.

I have tried every flag possible, checking the spelling, using forward slashes, back slashes, moving my project to a directory without spaces in its path, changing the include path to absolute, to relative, creating a copy of the project I want to parse right next to my parser... but nothing works. I have also tried to parse a simple main file with very basic code, and my program does print out something, so the cursor visiting is not the problem here...

I'd also like to mention that I've tried searching for a similar issue on Google hundreds of times before posting this, but again I've found absolutely nothing. Seems like Google doesn't want you to learn things...

Here's my current code :

CXIndex index = clang_createIndex( 0, 0 );

// Include paths.
int argc = 1;
const char* argv[] = { "-I\"D:/Projets/Visual Studio/UGE/ugBase/\"" };

// Parse the header.
CXTranslationUnit translationUnit;
clang_parseTranslationUnit2(
    index,
    "D:/Projets/Visual Studio/UGE/ugBase/Common/Base/ugBase.h",
    argv, argc,
    0, 0,
    CXTranslationUnit_None, &translationUnit );

// What did we find?
CXCursor cursor = clang_getTranslationUnitCursor( translationUnit );
clang_visitChildren(
    cursor,
    [](CXCursor c, CXCursor parent, CXClientData client_data)
    {
        cout << "Cursor kind: " << (char*) clang_getCursorKindSpelling(clang_getCursorKind( c )).data << ' ' << (char*) clang_getCursorSpelling( c ).data << endl;
        return CXChildVisit_Recurse;
    },
    0);

system( "pause" );

clang_disposeTranslationUnit( translationUnit );
clang_disposeIndex( index );
Dave
  • 41
  • 8
  • Have you tried the "normal" `argc`/`argv` concept, where `argv[0]` is the "command" and `argv[argc]` is a null pointer? Meaning you should have an initializer like `{ "clang", "-I...", nullptr }` (and of course initialize `argc` to `2`). – Some programmer dude Nov 05 '21 at 09:01
  • 1
    I just tried it, and I still have no output... – Dave Nov 05 '21 at 09:05

1 Answers1

1

I was having trouble figuring this out as well. Turns out you don't need the quotes. This worked on Windows with llvm/clang 12.0.0:

"-IC:\\Program Files\\LLVM\\include"

Using '/' also worked.

boocs
  • 456
  • 1
  • 3
  • 5