Guest User

Untitled

a guest
Jan 29th, 2014
97
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Copyright (C) 2011, 2012, 2013 Jan Erik Hanssen and Anders Bakken
  3.  
  4.     This program is free software: you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation, either version 3 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16.  
  17.     Source: https://github.com/Andersbakken/rtags/blob/master/src/clangtest.c
  18. */
  19.  
  20. #include <clang-c/Index.h>
  21.  
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <assert.h>
  26.  
  27. static void printString(const char *name, CXString string)
  28. {
  29.     const char *cstr = clang_getCString(string);
  30.     if (cstr && *cstr) {
  31.         printf("%s: %s ", name, cstr);
  32.     }
  33.     clang_disposeString(string);
  34. }
  35.  
  36. static void printCString(const char *name, const char* cstr)
  37. {
  38.     if (cstr && *cstr) {
  39.         printf("%s: %s ", name, cstr);
  40.     }
  41. }
  42.  
  43. static const char* getTypeSpelling (CXType type)
  44. {
  45.     switch (type.kind)
  46.     {
  47.         case CXType_Invalid: return "<Invalid>";
  48.         case CXType_Unexposed: return getTypeSpelling(clang_getCanonicalType(type));
  49.         case CXType_Int: return "int";
  50.         case CXType_Long: return "long";
  51.         default: return "<Unhandled type>";
  52.     }
  53. }
  54.  
  55. static void printCursor(CXCursor cursor)
  56. {
  57.     CXFile file;
  58.     unsigned off, line, col;
  59.     CXSourceLocation location = clang_getCursorLocation(cursor);
  60.     clang_getSpellingLocation(location, &file, &line, &col, &off);
  61.     CXString fileName = clang_getFileName(file);
  62.     const char *fileNameCStr = clang_getCString(fileName);
  63.     if (fileNameCStr) {
  64.         CXSourceRange range = clang_getCursorExtent(cursor);
  65.         unsigned start, end;
  66.         clang_getSpellingLocation(clang_getRangeStart(range), 0, 0, 0, &start);
  67.         clang_getSpellingLocation(clang_getRangeEnd(range), 0, 0, 0, &end);
  68.         printf("%s:%d:%d (%d, %d-%d) ", fileNameCStr, line, col, off, start, end);
  69.     }
  70.     clang_disposeString(fileName);
  71.     printString("kind", clang_getCursorKindSpelling(clang_getCursorKind(cursor)));
  72.     printCString("type", getTypeSpelling(clang_getCursorType(cursor)));
  73.     printString("display name", clang_getCursorDisplayName(cursor));
  74.     printString("usr", clang_getCursorUSR(cursor));
  75.     if (clang_isCursorDefinition(cursor))
  76.         printf("definition ");
  77.     printf("\n");
  78. }
  79.  
  80.  
  81.  
  82. static enum CXChildVisitResult visit(CXCursor cursor, CXCursor parent, CXClientData userData)
  83. {
  84.     (void)parent;
  85.     int indent = *(int*)userData;
  86.     int i;
  87.     for (i=0; i<indent; ++i) {
  88.         printf("  ");
  89.     }
  90.     printCursor(cursor);
  91.     CXCursor ref = clang_getCursorReferenced(cursor);
  92.     if (!clang_isInvalid(clang_getCursorKind(ref)) && !clang_equalCursors(ref, cursor)) {
  93.         for (i=0; i<indent; ++i) {
  94.             printf("  ");
  95.         }
  96.         printf("-> ");
  97.         printCursor(ref);
  98.     }
  99.     ++indent;
  100.     clang_visitChildren(cursor, visit, &indent);
  101.     return CXChildVisit_Continue;
  102. }
  103.  
  104. int main(int argc, char **argv)
  105. {
  106.     if (argc < 2)
  107.         return 1;
  108.     CXIndex index = clang_createIndex(1, 1);
  109.     const char * const *args = 0;
  110.     if (argc > 2)
  111.         args = (const char *const *)&argv[2];
  112.  
  113.     CXTranslationUnit unit = clang_parseTranslationUnit(index, argv[1], args, argc - 2,
  114.                                                         0, 0, clang_defaultEditingTranslationUnitOptions());
  115.     if (unit) {
  116.         int indent = 0;
  117.         clang_visitChildren(clang_getTranslationUnitCursor(unit), visit, &indent);
  118.  
  119.         const unsigned diagnosticCount = clang_getNumDiagnostics(unit);
  120.         unsigned i;
  121.         for (i=0; i<diagnosticCount; ++i) {
  122.             CXDiagnostic diagnostic = clang_getDiagnostic(unit, i);
  123.             const unsigned diagnosticOptions = (CXDiagnostic_DisplaySourceLocation|
  124.                                                 CXDiagnostic_DisplayColumn|
  125.                                                 CXDiagnostic_DisplaySourceRanges|
  126.                                                 CXDiagnostic_DisplayOption|
  127.                                                 CXDiagnostic_DisplayCategoryId|
  128.                                                 CXDiagnostic_DisplayCategoryName);
  129.             CXString diagnosticText = clang_formatDiagnostic(diagnostic, diagnosticOptions);
  130.             const char *cstr = clang_getCString(diagnosticText);
  131.             if (cstr)
  132.                 printf("%s\n", cstr);
  133.             clang_disposeString(diagnosticText);
  134.         }
  135.         clang_disposeTranslationUnit(unit);
  136.     }
  137.     clang_disposeIndex(index);
  138.     return 0;
  139. }
RAW Paste Data