Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern "C" {
- #include "clang-c/Index.h"
- }
- #include "llvm/Support/CommandLine.h"
- #include "llvm/Support/raw_ostream.h"
- using namespace llvm;
- static cl::opt<std::string> FileName(cl::Positional, cl::desc("Input File"), cl::Required);
- /* Function callback for the AST */
- enum CXChildVisitResult visitNode (CXCursor cursor, CXCursor parent, CXClientData client_data);
- int
- main(int argc, char** argv)
- {
- cl::ParseCommandLineOptions(argc, argv, "AST Traversal Name Extractor");
- CXIndex index = clang_createIndex(0, 0);
- const char *args[] = {
- "-I/usr/include",
- "-I."
- };
- CXTranslationUnit translationUnit = clang_parseTranslationUnit(index, FileName.c_str(), args, 2, NULL, 0, CXTranslationUnit_None);
- // get cursor for visiting AST tree
- CXCursor cur = clang_getTranslationUnitCursor(translationUnit);
- // Function to go through the whole
- // AST tree, we have to give the callback
- clang_visitChildren(cur, visitNode, NULL);
- clang_disposeTranslationUnit(translationUnit);
- clang_disposeIndex(index);
- return 0;
- }
- enum CXChildVisitResult
- visitNode (CXCursor cursor, CXCursor parent, CXClientData client_data)
- /*
- * Function to print every function declared
- * and method name.
- */
- {
- if (clang_getCursorKind(cursor) == CXCursor_CXXMethod ||
- clang_getCursorKind(cursor) == CXCursor_FunctionDecl)
- {
- CXString name = clang_getCursorSpelling(cursor);
- CXSourceLocation loc = clang_getCursorLocation(cursor);
- CXString fName;
- unsigned line = 0, col = 0;
- clang_getPresumedLocation(loc, &fName, &line, &col);
- outs() << clang_getCString(fName) << ":"
- << line << ":" << col << " declares "
- << clang_getCString(name) << "\n";
- clang_disposeString(name);
- clang_disposeString(fName);
- return CXChildVisit_Continue; // skip child nodes
- }
- return CXChildVisit_Recurse; // go to children node
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement