fork download
  1. #include <iostream>
  2. #include <clang-c/Index.h>
  3.  
  4. template<typename T, void F(T)> struct manager
  5. {
  6. T t;
  7. manager(T t) : t(t) {}
  8. operator T () {return t;}
  9. ~manager() {F(t);}
  10. };
  11.  
  12. int main()
  13. {
  14. manager<CXIndex, clang_disposeIndex> managed_index(clang_createIndex(0, 0));
  15. CXIndex index = managed_index;
  16. if (!index)
  17. {
  18. std::cerr << "failed creating index" << std::endl;
  19. return 1;
  20. }
  21.  
  22. manager<CXTranslationUnit, clang_disposeTranslationUnit> managed_mickey(
  23. clang_parseTranslationUnit(
  24. index,
  25. "mickey.cpp",
  26. nullptr,
  27. 0,
  28. 0,
  29. 0,
  30. CXTranslationUnit_PrecompiledPreamble));
  31. CXTranslationUnit mickey = managed_mickey;
  32. if (!mickey)
  33. {
  34. std::cerr << "mickey parsing failed" << std::endl;
  35. return 1;
  36. }
  37.  
  38. clang_reparseTranslationUnit(mickey, 0, nullptr, 0);
  39.  
  40. manager<CXTranslationUnit, clang_disposeTranslationUnit> managed_minnie(
  41. clang_parseTranslationUnit(
  42. index,
  43. "minnie.cpp",
  44. nullptr,
  45. 0,
  46. 0,
  47. 0,
  48. CXTranslationUnit_PrecompiledPreamble));
  49. CXTranslationUnit minnie = managed_minnie;
  50. if (!minnie)
  51. {
  52. std::cerr << "minnie parsing failed" << std::endl;
  53. return 1;
  54. }
  55.  
  56. clang_reparseTranslationUnit(minnie, 0, nullptr, 0);
  57.  
  58. CXFile file;
  59. unsigned line;
  60. unsigned column;
  61. unsigned offset;
  62.  
  63. CXFile mickey_file = clang_getFile(mickey, "mickey.cpp");
  64. CXSourceLocation mickey_location = clang_getLocation(mickey, mickey_file, 5, 5);
  65. CXCursor mickey_cursor = clang_getCursor(mickey, mickey_location);
  66. CXType mickey_type = clang_getCursorType(mickey_cursor);
  67. CXCursor mickey_decl_cursor = clang_getTypeDeclaration(mickey_type);
  68. CXSourceLocation mickey_decl_location = clang_getCursorLocation(mickey_decl_cursor);
  69. clang_getExpansionLocation(mickey_decl_location, &file, &line, &column, &offset);
  70. CXString mickey_decl_file_name = clang_getFileName(file);
  71. const char *mickey_decl_cstr_file_name = clang_getCString(mickey_decl_file_name);
  72.  
  73. std::cout << "beginning" << std::endl;
  74. std::cout << mickey_decl_cstr_file_name << " " << line << " " << column << " " << offset << std::endl;
  75.  
  76. return 0;
  77. }
  78.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty