Created
March 19, 2013 15:09
-
-
Save d0k/5196919 to your computer and use it in GitHub Desktop.
libclang program to print the header files that are referenced by a call in a TU.
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
/* clang depgraph.c -o depgraph -I tools/clang/include -lclang */ | |
#include "clang-c/Index.h" | |
#include <stdio.h> | |
static enum CXChildVisitResult | |
visitor(CXCursor cursor, CXCursor parent, CXClientData client_data) { | |
enum CXCursorKind kind = clang_getCursorKind(cursor); | |
CXFile tu_file = (CXFile *)client_data; | |
CXFile in_file, from_file; | |
unsigned line, column, offset; | |
CXSourceLocation loc; | |
CXString tu_spelling, from_spelling; | |
if (clang_isInvalid(kind)) | |
return CXChildVisit_Recurse; | |
/* Just look for call expressions. */ | |
if (kind != CXCursor_CallExpr) | |
return CXChildVisit_Recurse; | |
/* Now check if the call is coming from the current file and calling into a | |
different file (a header). */ | |
loc = clang_getCursorLocation(cursor); | |
clang_getExpansionLocation(loc, &in_file, &line, &column, &offset); | |
cursor = clang_getCursorReferenced(cursor); | |
loc = clang_getCursorLocation(cursor); | |
clang_getExpansionLocation(loc, &from_file, &line, &column, &offset); | |
if (in_file != tu_file || from_file == tu_file) | |
return CXChildVisit_Recurse; | |
/* Print in graphviz format. */ | |
tu_spelling = clang_getFileName(tu_file); | |
from_spelling = clang_getFileName(from_file); | |
printf("\"%s\" -> \"%s\"\n", clang_getCString(tu_spelling), | |
clang_getCString(from_spelling)); | |
clang_disposeString(tu_spelling); | |
clang_disposeString(from_spelling); | |
return CXChildVisit_Recurse; | |
} | |
int main(int argc, const char *argv[]) { | |
CXIndex idx = clang_createIndex(1, 1); | |
if (argc < 2) | |
return 1; | |
CXTranslationUnit tu = clang_createTranslationUnitFromSourceFile( | |
idx, argv[1], argc - 2, argv + 2, 0, NULL); | |
CXFile tu_file = clang_getFile(tu, argv[1]); | |
clang_visitChildren(clang_getTranslationUnitCursor(tu), visitor, tu_file); | |
clang_disposeTranslationUnit(tu); | |
clang_disposeIndex(idx); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment