[cfe-dev] clang_findIncludesInFile issue (did not invoke visitor)

Anton Smirnov dev at antonsmirnov.name
Wed Oct 2 00:57:01 PDT 2013


For some reason clang_findIncludesInFile did not find inclusion in file (1
inclusion actually). I've checked test file with clang and no diagnostics
are provided (seems to be correct).

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

static CXVisitorResult include_visit(void *context, CXCursor cursor,
CXSourceRange range) {

  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, "filename=[%s] size=[%u]\n", 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, false);

    // command-line tu
    fprintf(stderr, "command-line file =============\n");
    char *filename = argv[1];
    unsigned filesize = getfilesize(filename);
    CXTranslationUnit commandLineTu = clang_parseTranslationUnit(index,
filename, params, 3, 0, 0, CXTranslationUnit_None);

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

    showInclusions(commandLineTu, filename, filesize);

    clang_disposeTranslationUnit(commandLineTu);
    clang_disposeIndex(index);

    return 0;
}
---

Test files:
1. "clang_include.cpp":
---
#include "a.h"

int a = 10;
---
2. "a.h" exists and is empty.


Build is done in "build folder", sources are in "jni" folder, test files
are in "test" folder
Execution:

mba-anton:build asmirnov$ ./clang_include ../test/test_include.cpp
Starting ----
command-line file =============
filename=[../test/test_include.cpp] size=[27]
result = CXResult_Success

Any thoughts?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20131002/f10c7120/attachment.html>


More information about the cfe-dev mailing list