[cfe-dev] Enumeration on included files not working?

Anton Smirnov dev at antonsmirnov.name
Tue Nov 12 03:14:55 PST 2013


Hello, i've trying to get included files list using Clang C
API clang_findIncludesInFile() and it seems to be not working or i'm doing
smth wrong.

The source code is pretty straight-forward - 1 index, 2 translation units
for source and header files and header is included in source. But visitor
is not invoked.

Full utility source code:
----------------------------------

#include <iostream>
#include <clang-c/Index.h>

static CXVisitorResult include_visit(void *context, CXCursor cursor,
CXSourceRange range) {
  fprintf(stderr, "include_visit()\n");

  CXFile file = clang_getIncludedFile(cursor);
  CXString filename = clang_getFileName(file);

  fprintf(stderr, "    include [%s]\n", clang_getCString(filename));

  clang_disposeString(filename);

  // continue
  return CXVisit_Continue;
}

CXCursorAndRangeVisitor visitor = {
  .context = 0,
  .visit = include_visit
};

char * cx_results[] = {
  "CXResult_Success",
  /**
   * \brief One of the parameters was invalid for the function.
   */
  "CXResult_Invalid",
  /**
   * \brief The function was terminated by a callback (e.g. it returned
   * CXVisit_Break)
   */
  "CXResult_VisitBreak"
};

void showInclusions(CXTranslationUnit TU, char *src_filename, unsigned
filesize) {

    CXFile file = clang_getFile(TU, src_filename);
    fprintf(stderr, "src file=%p filename=[%s] size=[%u]\n", &file,
src_filename, filesize);

    CXResult result = clang_findIncludesInFile(TU, file, visitor);
    fprintf(stderr, "result = %s\n", cx_results[result]);
}

unsigned getfilesize(char* filename)
{
FILE *fp = fopen(filename, "r");
fseek(fp, 0L, SEEK_END);
unsigned sz = ftell(fp);
fclose(fp);
return sz;
}

const char * const params[] = {
    "-c",
    "-x",
    "c++"
};

int main(int argc, char** argv)
{
    std::cout << "Starting ----" << std::endl;

    CXIndex index = clang_createIndex(false, true);

    // command-line tu
    //fprintf(stderr, "command-line file =============\n");
    //char *filename = argv[1];
    //unsigned filesize = getfilesize(filename);

    CXUnsavedFile unsavedFile[2];

    // header
    CXUnsavedFile *header = &unsavedFile[0];
    header->Filename = "./header.h";
    header->Contents = "int a = 10;";
    header->Length = strlen(header->Contents);

    // cpp
    CXUnsavedFile *source = &unsavedFile[1];
    source->Filename = "./source.cpp";
    source->Contents = "#include \"header.h\" ";
    source->Length = strlen(source->Contents);

    // parse header
    CXTranslationUnit headerTU =
      clang_parseTranslationUnit(index, header->Filename, NULL, 0, header,
1, CXTranslationUnit_None);

    // parse source
    CXTranslationUnit sourceTu =
      clang_parseTranslationUnit(index, source->Filename, NULL, 0,
unsavedFile, 2, CXTranslationUnit_None);

    if (!sourceTu) {
      fprintf(stderr, "failed to parse TU\n");
      return 1;
    }

    showInclusions(sourceTu, (char *)source->Filename, source->Length);

    clang_disposeTranslationUnit(sourceTu);
    clang_disposeTranslationUnit(headerTU);
    clang_disposeIndex(index);

    return 0;
}


Output:
----------

Starting ----

command-line file =============

src file=0x7fff514f8b20 filename=[./source.cpp] size=[20]

result = CXResult_Success
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20131112/fa3b8866/attachment.html>


More information about the cfe-dev mailing list