Domanda

I am using the latest LibClang to parse some C header files. The code I process comes from CXUnsavedFile's (it is all generated dynamically and nothing lives on disk). For Example:

FileA.h contains:

struct STRUCT_A {
  int a;
  struct STRUCT_B foo;
};

FileB.h contains:

struct STRUCT_B {
  int b;
};

When parsing fileA.h with the following code snippet:

CXUnsavedFile unsaved_files[2];

unsaved_files[0].Filename = "fileA.h";
unsaved_files[0].Contents = fileA_contents;
unsaved_files[0].Length   = strlen( fileA_contents );

unsaved_files[1].Filename = "fileB.h";
unsaved_files[1].Contents = fileB_contents;
unsaved_files[1].Length   = strlen( fileB_contents );

tu = clang_parseTranslationUnit(
    index,
    "fileA.h",
    argv, // "-x c-header -target i386-pc-win32"
    argc,
    (CXUnsavedFile *)&unsaved_files,
    2,
    CXTranslationUnit_None
);

CXCursor cur = clang_getTranslationUnitCursor( tu );

clang_visitChildren( cur, visitor, NULL );

I get the error "field has incomplete type 'struct STRUCT_B'" which makes sense as I have not included fileB.h in order to define struct STRUCT_B.

Adding an "#include <fileB.h>" does not work (fatal error: 'fileB.h' file not found).

How do I get parsing fileA.h to work when one or more needed definitions are present in another CXUnsavedFile fileB.h?

È stato utile?

Soluzione

Not sure this will help you, but here are two remarks:

  1. Although it isn't explicitly mentioned in the documentation, I think that the Filename field should contain a full path to the file (which could be important for inclusions, especially when there are "-I" switches in the command-line)

  2. from libclang's documentation (emphasis mine):

    const char* CXUnsavedFile::Filename


    The file whose contents have not yet been saved. This file must already exist in the file system.

    I suspect libclang relies on the filesystem for almost everything (finding the correct file to include, checking it exists, ...) and only account for CXUnsavedFiles at the last step, when actual content must be read.

If you can, I would suggest creating empty files in a memory filesystem. This would not incur much resource usage, and could help libclang find the correct include files.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top