Skip to content

Instantly share code, notes, and snippets.

@codebrainz
Created June 23, 2015 02:48
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codebrainz/faabd73930c5346f7b50 to your computer and use it in GitHub Desktop.
Save codebrainz/faabd73930c5346f7b50 to your computer and use it in GitHub Desktop.
List include files using libclang
#include <stdio.h>
#include <stdlib.h>
#include <clang-c/Index.h>
enum CXChildVisitResult
onVisitCursor(CXCursor cursor, CXCursor parent, CXClientData client_data)
{
if (clang_getCursorKind(cursor) == CXCursor_InclusionDirective) {
CXFile file = clang_getIncludedFile(cursor);
CXString filename = clang_getFileName(file);
const char *fn = clang_getCString(filename);
fprintf(stdout, "%s\n", fn);
clang_disposeString(filename);
}
return CXChildVisit_Continue;
}
int main(int argc, char **argv)
{
if (argc < 2) {
fputs("error: missing source file argument\n", stderr);
exit(EXIT_FAILURE);
}
int clang_argc = argc - 1;
const char *const *clang_argv = (const char *const *) argv + 1;
CXIndex index = clang_createIndex(0, 1);
CXTranslationUnit tu =
clang_createTranslationUnitFromSourceFile(index, NULL, clang_argc, clang_argv, 0, NULL);
if (! tu) {
fputs("error: failed to compile input\n", stderr);
exit(EXIT_FAILURE);
}
clang_visitChildren(clang_getTranslationUnitCursor(tu), onVisitCursor, NULL);
clang_disposeTranslationUnit(tu);
clang_disposeIndex(index);
return 0;
}
@umesh4b4
Copy link

umesh4b4 commented Nov 7, 2016

Hi,

In the above code what values to be passed in the command line for main().

Regards,
Umesh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment