void filter_already_existing_methods(CXTranslationUnit code_translation_unit,gchar *filename, gchar *class_name) { size_t i; CXFile code_file; CXCursor code_cursor; CXSourceLocation code_source_location; gchar *settername; gchar *gettername; struct GcharTuple tuple; code_file = clang_getFile(code_translation_unit,filename); code_source_location = clang_getLocation(code_translation_unit,code_file,1,1); code_cursor = clang_getTranslationUnitCursor(code_translation_unit); for (i=0; i < property_list.used; i++) { gettername = malloc((strlen(sg_method_name_getter) + 1) * sizeof(gchar)); strncpy(gettername,sg_method_name_getter,strlen(sg_method_name_getter) + 1); gettername = chunked_string_replace(gettername,"$NAME",property_list.data[i].name); settername = malloc((strlen(sg_method_name_setter) + 1) * sizeof(gchar)); strncpy(settername,sg_method_name_setter,strlen(sg_method_name_setter) + 1); settername = chunked_string_replace(settername,"$NAME",property_list.data[i].name); tuple.first = gettername; tuple.second = settername; tuple.class_name = class_name; tuple.number = i; clang_visitChildren(code_cursor,filterer,&tuple); free(gettername); gettername = NULL; free(settername); settername = NULL; } }
bool ClangUtils::GetCursorAt(CXTranslationUnit &unit, const wxString &filename, int line, int col, CXCursor &cur) { CXFile file1; CXCursor cursor; CXSourceLocation loc; file1 = clang_getFile (unit, filename.mb_str(wxConvUTF8).data()); loc = clang_getLocation(unit, file1, line, col); cursor = clang_getCursor (unit, loc); if(clang_isInvalid(cursor.kind)) return false; if(cursor.kind == CXCursor_MemberRef || cursor.kind == CXCursor_MemberRefExpr || cursor.kind == CXCursor_TypeRef || cursor.kind == CXCursor_DeclRefExpr || cursor.kind == CXCursor_TemplateRef || cursor.kind == CXCursor_NamespaceRef ) { cursor = clang_getCursorReferenced(cursor); } cur = cursor; return true; }
const char* libclang_vim::at_specific_location( const location_tuple& location_tuple, const std::function<std::string(CXCursor const&)>& predicate) { static std::string vimson; char const* file_name = location_tuple.file.c_str(); auto const args_ptrs = get_args_ptrs(location_tuple.args); cxindex_ptr index = clang_createIndex(/*excludeDeclsFromPCH*/ 1, /*displayDiagnostics*/ 0); std::vector<CXUnsavedFile> unsaved_files = create_unsaved_files(location_tuple); cxtranslation_unit_ptr translation_unit(clang_parseTranslationUnit( index, file_name, args_ptrs.data(), args_ptrs.size(), unsaved_files.data(), unsaved_files.size(), CXTranslationUnit_Incomplete)); if (!translation_unit) return "{}"; CXFile file = clang_getFile(translation_unit, file_name); auto const location = clang_getLocation( translation_unit, file, location_tuple.line, location_tuple.col); CXCursor const cursor = clang_getCursor(translation_unit, location); vimson = predicate(cursor); return vimson.c_str(); }
CXSourceLocation TranslationUnit::GetSourceLocation( const std::string &filename, int line, int column ) { // ASSUMES A LOCK IS ALREADY HELD ON clang_access_mutex_ AND THE TU IS VALID! CXFile file = clang_getFile( clang_translation_unit_, filename.c_str() ); return clang_getLocation( clang_translation_unit_, file, line, column ); }
int main(int argc, char *argv[]) { auto index = clang_createIndex(0, 0); auto options = clang_defaultEditingTranslationUnitOptions(); char const *args[] = { "-x", "c++", "-std=c++11" }; auto arg_count = sizeof( args ) / sizeof( *args ); filename = argv[1]; CXUnsavedFile *unsaved_files = NULL; auto unsaved_file_count = 0; tu = clang_parseTranslationUnit(index, filename.c_str(), args, arg_count, unsaved_files, unsaved_file_count, options ); if ( !tu ) { std::cout << "Translation Unit Parse Failed!\n"; return -1; } std::stringstream ss( argv[2] ); int line, col; ss >> line; ss.get(); ss >> col; std::cout << "Hello " << line << ":" << col << "\n"; auto file = clang_getFile( tu, filename.c_str() ); auto location = clang_getLocation( tu, file, line, col ); clang_visitChildren( clang_getTranslationUnitCursor( tu ), visitor, reinterpret_cast<CXClientData>(0) ); auto cursor = clang_getCursor( tu, location ); auto refcursor = clang_getCursorReferenced( cursor ); auto rrefcursor = clang_getCursorReferenced( refcursor ); auto arf = clang_getTypeKindSpelling( clang_getCursorType( cursor ).kind ); auto foo = clang_getCanonicalCursor( cursor ); auto semparent = clang_getCursorSemanticParent( cursor ); auto lexparent = clang_getCursorLexicalParent( cursor ); std::cout << cursor << "\n"; std::cout << refcursor << "\n"; std::cout << rrefcursor << "\n"; std::cout << clang_getCString(arf) << "\n"; std::cout << foo << "\n"; std::cout << "Parent: " << semparent << "\n"; std::cout << "LexParent: " << lexparent << "\n"; //clang_visitChildren( semparent, visitor, reinterpret_cast<CXClientData>(0) ); clang_disposeString( arf ); return 0; }
static int perform_file_scan(const char *ast_file, const char *source_file, const char *prefix) { CXIndex Idx; CXTranslationUnit TU; FILE *fp; CXCursor prevCursor = clang_getNullCursor(); CXFile file; unsigned line = 1, col = 1; unsigned start_line = 1, start_col = 1; if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, /* displayDiagnosics=*/1))) { fprintf(stderr, "Could not create Index\n"); return 1; } if (!CreateTranslationUnit(Idx, ast_file, &TU)) return 1; if ((fp = fopen(source_file, "r")) == NULL) { fprintf(stderr, "Could not open '%s'\n", source_file); return 1; } file = clang_getFile(TU, source_file); for (;;) { CXCursor cursor; int c = fgetc(fp); if (c == '\n') { ++line; col = 1; } else ++col; /* Check the cursor at this position, and dump the previous one if we have * found something new. */ cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col)); if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) && prevCursor.kind != CXCursor_InvalidFile) { print_cursor_file_scan(prevCursor, start_line, start_col, line, col, prefix); start_line = line; start_col = col; } if (c == EOF) break; prevCursor = cursor; } fclose(fp); return 0; }
static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor, CXCursor Parent, CXClientData ClientData) { const char *startBuf, *endBuf; unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn; CXCursor Ref; VisitorData *Data = (VisitorData *)ClientData; if (Cursor.kind != CXCursor_FunctionDecl || !clang_isCursorDefinition(Cursor)) return CXChildVisit_Continue; clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf, &startLine, &startColumn, &endLine, &endColumn); /* Probe the entire body, looking for both decls and refs. */ curLine = startLine; curColumn = startColumn; while (startBuf < endBuf) { CXSourceLocation Loc; CXFile file; CXString source; if (*startBuf == '\n') { startBuf++; curLine++; curColumn = 1; } else if (*startBuf != '\t') curColumn++; Loc = clang_getCursorLocation(Cursor); clang_getInstantiationLocation(Loc, &file, 0, 0, 0); source = clang_getFileName(file); if (clang_getCString(source)) { CXSourceLocation RefLoc = clang_getLocation(Data->TU, file, curLine, curColumn); Ref = clang_getCursor(Data->TU, RefLoc); if (Ref.kind == CXCursor_NoDeclFound) { /* Nothing found here; that's fine. */ } else if (Ref.kind != CXCursor_FunctionDecl) { printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref), curLine, curColumn); PrintCursor(Ref); printf("\n"); } } clang_disposeString(source); startBuf++; } return CXChildVisit_Continue; }
SourceLocation::SourceLocation(CXTranslationUnit cxTranslationUnit, const Utf8String &filePath, uint line, uint column) : cxSourceLocation(clang_getLocation(cxTranslationUnit, clang_getFile(cxTranslationUnit, filePath.constData()), line, column)), filePath_(filePath), line_(line), column_(column) { }
CXCursor TranslationUnit::GetCursor( int line, int column ) { // ASSUMES A LOCK IS ALREADY HELD ON clang_access_mutex_! if ( !clang_translation_unit_ ) return clang_getNullCursor(); CXFile file = clang_getFile( clang_translation_unit_, filename_.c_str() ); CXSourceLocation source_location = clang_getLocation( clang_translation_unit_, file, line, column ); return clang_getCursor( clang_translation_unit_, source_location ); }
void Irony::getType(unsigned line, unsigned col) const { if (activeTu_ == nullptr) { std::clog << "W: get-type - parse wasn't called\n"; std::cout << "nil\n"; return; } CXFile cxFile = clang_getFile(activeTu_, file_.c_str()); CXSourceLocation sourceLoc = clang_getLocation(activeTu_, cxFile, line, col); CXCursor cursor = clang_getCursor(activeTu_, sourceLoc); if (clang_Cursor_isNull(cursor)) { // TODO: "error: no type at point"? std::cout << "nil"; return; } CXType cxTypes[2]; cxTypes[0] = clang_getCursorType(cursor); cxTypes[1] = clang_getCanonicalType(cxTypes[0]); std::cout << "("; for (const CXType &cxType : cxTypes) { CXString typeDescr = clang_getTypeSpelling(cxType); std::string typeStr = clang_getCString(typeDescr); clang_disposeString(typeDescr); if (typeStr.empty()) break; std::cout << support::quoted(typeStr) << " "; } std::cout << ")\n"; }
SourceLocation::SourceLocation(TranslationUnit& unit, File& file, unsigned line, unsigned column) { location_ = clang_getLocation(unit.unit_, file.file_, line, column); }
source_location translation_unit::get_location(file f, unsigned line, unsigned column) { return { clang_getLocation(tu, f.file, line, column) }; }
int perform_token_annotation(int argc, const char **argv) { const char *input = argv[1]; char *filename = 0; unsigned line, second_line; unsigned column, second_column; CXIndex CIdx; CXTranslationUnit TU = 0; int errorCode; struct CXUnsavedFile *unsaved_files = 0; int num_unsaved_files = 0; CXToken *tokens; unsigned num_tokens; CXSourceRange range; CXSourceLocation startLoc, endLoc; CXFile file = 0; CXCursor *cursors = 0; unsigned i; input += strlen("-test-annotate-tokens="); if ((errorCode = parse_file_line_column(input, &filename, &line, &column, &second_line, &second_column))) return errorCode; if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) return -1; CIdx = clang_createIndex(0, 1); TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1], argc - num_unsaved_files - 3, argv + num_unsaved_files + 2, num_unsaved_files, unsaved_files); if (!TU) { fprintf(stderr, "unable to parse input\n"); clang_disposeIndex(CIdx); free(filename); free_remapped_files(unsaved_files, num_unsaved_files); return -1; } errorCode = 0; file = clang_getFile(TU, filename); if (!file) { fprintf(stderr, "file %s is not in this translation unit\n", filename); errorCode = -1; goto teardown; } startLoc = clang_getLocation(TU, file, line, column); if (clang_equalLocations(clang_getNullLocation(), startLoc)) { fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line, column); errorCode = -1; goto teardown; } endLoc = clang_getLocation(TU, file, second_line, second_column); if (clang_equalLocations(clang_getNullLocation(), endLoc)) { fprintf(stderr, "invalid source location %s:%d:%d\n", filename, second_line, second_column); errorCode = -1; goto teardown; } range = clang_getRange(startLoc, endLoc); clang_tokenize(TU, range, &tokens, &num_tokens); cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor)); clang_annotateTokens(TU, tokens, num_tokens, cursors); for (i = 0; i != num_tokens; ++i) { const char *kind = "<unknown>"; CXString spelling = clang_getTokenSpelling(TU, tokens[i]); CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]); unsigned start_line, start_column, end_line, end_column; switch (clang_getTokenKind(tokens[i])) { case CXToken_Punctuation: kind = "Punctuation"; break; case CXToken_Keyword: kind = "Keyword"; break; case CXToken_Identifier: kind = "Identifier"; break; case CXToken_Literal: kind = "Literal"; break; case CXToken_Comment: kind = "Comment"; break; } clang_getInstantiationLocation(clang_getRangeStart(extent), 0, &start_line, &start_column, 0); clang_getInstantiationLocation(clang_getRangeEnd(extent), 0, &end_line, &end_column, 0); printf("%s: \"%s\" ", kind, clang_getCString(spelling)); PrintExtent(stdout, start_line, start_column, end_line, end_column); if (!clang_isInvalid(cursors[i].kind)) { printf(" "); PrintCursor(cursors[i]); } printf("\n"); } free(cursors); teardown: PrintDiagnostics(TU); clang_disposeTranslationUnit(TU); clang_disposeIndex(CIdx); free(filename); free_remapped_files(unsaved_files, num_unsaved_files); return errorCode; }
int inspect_cursor_at(int argc, const char **argv) { CXIndex CIdx; int errorCode; struct CXUnsavedFile *unsaved_files = 0; int num_unsaved_files = 0; CXTranslationUnit TU; CXCursor Cursor; CursorSourceLocation *Locations = 0; unsigned NumLocations = 0, Loc; /* Count the number of locations. */ while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1]) ++NumLocations; /* Parse the locations. */ assert(NumLocations > 0 && "Unable to count locations?"); Locations = (CursorSourceLocation *)malloc( NumLocations * sizeof(CursorSourceLocation)); for (Loc = 0; Loc < NumLocations; ++Loc) { const char *input = argv[Loc + 1] + strlen("-cursor-at="); if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename, &Locations[Loc].line, &Locations[Loc].column, 0, 0))) return errorCode; } if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files, &num_unsaved_files)) return -1; CIdx = clang_createIndex(0, 1); TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1], argc - num_unsaved_files - 2 - NumLocations, argv + num_unsaved_files + 1 + NumLocations, num_unsaved_files, unsaved_files); if (!TU) { fprintf(stderr, "unable to parse input\n"); return -1; } for (Loc = 0; Loc < NumLocations; ++Loc) { CXFile file = clang_getFile(TU, Locations[Loc].filename); if (!file) continue; Cursor = clang_getCursor(TU, clang_getLocation(TU, file, Locations[Loc].line, Locations[Loc].column)); PrintCursor(Cursor); printf("\n"); free(Locations[Loc].filename); } PrintDiagnostics(TU); clang_disposeTranslationUnit(TU); clang_disposeIndex(CIdx); free(Locations); free_remapped_files(unsaved_files, num_unsaved_files); return 0; }