Last active
August 29, 2015 14:12
-
-
Save catchouli/f6fb0e1f6378b380e974 to your computer and use it in GitHub Desktop.
libclang c++ draw tree
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
namespace coment | |
{ | |
class Component | |
{ | |
}; | |
} | |
struct Stuff | |
: public coment::Component | |
{ | |
int i; | |
float x; | |
struct vec { float[3] data; }; | |
}; |
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
input.cpp | |
|-+ coment | |
|-+ Component | |
|-+ Stuff | |
|-+ coment::Component | |
|-+ coment | |
|-+ class coment::Component | |
|-+ i | |
|-+ x | |
|-+ vec | |
|-+ data |
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 <iostream> | |
#include <functional> | |
#include <clang-c/Index.h> | |
template <typename T> | |
T&& check(T&& x) | |
{ | |
if (!x) | |
{ | |
fprintf(stderr, "Error\n"); | |
exit(1); | |
} | |
return std::move(x); | |
} | |
CXChildVisitResult drawtree(CXCursor cursor, CXCursor parent, CXClientData client_data) | |
{ | |
int depth = *(int*)client_data; | |
int childDepth = depth + 1; | |
// Draw tree stuff | |
// Add spacing | |
for (int i = 0; i < depth-1; ++i) | |
{ | |
printf(" "); | |
} | |
// Add |-+ thing if this isn't the root node | |
if (depth != 0) | |
{ | |
printf("|-+ "); | |
} | |
// Write this component's name | |
printf("%s\n", clang_getCString(clang_getCursorSpelling(cursor))); | |
// Draw children | |
clang_visitChildren(cursor, drawtree, &childDepth); | |
// Continue traversal | |
return CXChildVisit_Continue; | |
}; | |
int main(int argc, char** argv) | |
{ | |
// Create index | |
CXIndex idx = check(clang_createIndex(1, 1)); | |
// Create index action | |
CXIndexAction idxAction = clang_IndexAction_create(idx); | |
// Parse | |
CXTranslationUnit tu; | |
CXErrorCode ec = clang_parseTranslationUnit2(idx, "input.cpp", argv, argc, | |
nullptr, 0, 0, &tu); | |
// Traverse | |
// Draw tree | |
int depth = 0; | |
drawtree(clang_getTranslationUnitCursor(tu), clang_getNullCursor(), &depth); | |
CXType type = clang_getCursorType(clang_getTranslationUnitCursor(tu)); | |
system("pause"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment