Created
June 23, 2015 02:48
-
-
Save codebrainz/faabd73930c5346f7b50 to your computer and use it in GitHub Desktop.
List include files using libclang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
In the above code what values to be passed in the command line for main().
Regards,
Umesh.