<div dir="ltr">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.<div><br></div><div>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.<br>
<div><br></div><div>Full utility source code:</div><div>----------------------------------</div><div><br></div><div><div>#include <iostream></div><div>#include <clang-c/Index.h></div><div><br></div><div>static CXVisitorResult include_visit(void *context, CXCursor cursor, CXSourceRange range) {</div>
<div> fprintf(stderr, "include_visit()\n");</div><div><br></div><div> CXFile file = clang_getIncludedFile(cursor);</div><div> CXString filename = clang_getFileName(file);</div><div><br></div><div> fprintf(stderr, " include [%s]\n", clang_getCString(filename));</div>
<div><br></div><div> clang_disposeString(filename);</div><div><br></div><div> // continue</div><div> return CXVisit_Continue;</div><div>}</div><div><br></div><div>CXCursorAndRangeVisitor visitor = {</div><div> .context = 0,</div>
<div> .visit = include_visit</div><div>};</div><div><br></div><div>char * cx_results[] = {</div><div> "CXResult_Success",</div><div> /**</div><div> * \brief One of the parameters was invalid for the function.</div>
<div> */</div><div> "CXResult_Invalid",</div><div> /**</div><div> * \brief The function was terminated by a callback (e.g. it returned</div><div> * CXVisit_Break)</div><div> */</div><div> "CXResult_VisitBreak"</div>
<div>};</div><div><br></div><div>void showInclusions(CXTranslationUnit TU, char *src_filename, unsigned filesize) {</div><div> </div><div> CXFile file = clang_getFile(TU, src_filename);</div><div> fprintf(stderr, "src file=%p filename=[%s] size=[%u]\n", &file, src_filename, filesize);</div>
<div><br></div><div> CXResult result = clang_findIncludesInFile(TU, file, visitor);</div><div> fprintf(stderr, "result = %s\n", cx_results[result]);</div><div>}</div><div><br></div><div>unsigned getfilesize(char* filename)</div>
<div>{</div><div><span class="" style="white-space:pre"> </span>FILE *fp = fopen(filename, "r");</div><div><span class="" style="white-space:pre"> </span>fseek(fp, 0L, SEEK_END);</div><div><span class="" style="white-space:pre"> </span>unsigned sz = ftell(fp);</div>
<div><span class="" style="white-space:pre"> </span>fclose(fp);</div><div><span class="" style="white-space:pre"> </span>return sz;</div><div>}</div><div><br></div><div>const char * const params[] = {</div><div> "-c",</div>
<div> "-x",</div><div> "c++"</div><div>};</div><div><br></div><div>int main(int argc, char** argv)</div><div>{</div><div> std::cout << "Starting ----" << std::endl;</div>
<div><br></div><div> CXIndex index = clang_createIndex(false, true);</div><div> </div><div> // command-line tu</div><div> //fprintf(stderr, "command-line file =============\n");</div><div> //char *filename = argv[1];</div>
<div> //unsigned filesize = getfilesize(filename);</div><div><br></div><div> CXUnsavedFile unsavedFile[2];</div><div> </div><div> // header</div><div> CXUnsavedFile *header = &unsavedFile[0];</div><div>
header->Filename = "./header.h";</div><div> header->Contents = "int a = 10;";</div><div> header->Length = strlen(header->Contents);</div><div><br></div><div> // cpp</div><div> CXUnsavedFile *source = &unsavedFile[1];</div>
<div> source->Filename = "./source.cpp";</div><div> source->Contents = "#include \"header.h\" ";</div><div> source->Length = strlen(source->Contents);</div><div><br></div>
<div> // parse header</div><div> CXTranslationUnit headerTU =</div><div> clang_parseTranslationUnit(index, header->Filename, NULL, 0, header, 1, CXTranslationUnit_None);</div><div> </div><div> // parse source</div>
<div> CXTranslationUnit sourceTu =</div><div> clang_parseTranslationUnit(index, source->Filename, NULL, 0, unsavedFile, 2, CXTranslationUnit_None);</div><div> </div><div> if (!sourceTu) {</div><div> fprintf(stderr, "failed to parse TU\n");</div>
<div> return 1;</div><div> }</div><div> </div><div> showInclusions(sourceTu, (char *)source->Filename, source->Length);</div><div> </div><div> clang_disposeTranslationUnit(sourceTu);</div><div> clang_disposeTranslationUnit(headerTU);</div>
<div> clang_disposeIndex(index);</div><div><br></div><div> return 0;</div><div>}</div></div><div><br></div><div><br></div><div>Output:</div><div>----------</div><div>
<p class="">Starting ----</p>
<p class="">command-line file =============</p>
<p class="">src file=0x7fff514f8b20 filename=[./source.cpp] size=[20]</p>
<p class="">result = CXResult_Success</p></div></div></div>