enum CXChildVisitResult foundChild(CXCursor cursor, CXCursor parent, CXClientData client_data) { if(clang_getCursorKind(cursor) == CXCursor_CallExpr) { printf("-%s\n", clang_getCString(clang_getCursorSpelling(cursor))); } if(clang_isDeclaration(clang_getCursorKind(cursor)) && (clang_getCursorLinkage(cursor) == CXLinkage_External || clang_getCursorLinkage(cursor) == CXLinkage_Internal)) { CXFile file; const char * filename, * wantFilename; CXSourceLocation cloc; cloc = clang_getCursorLocation(cursor); clang_getInstantiationLocation(cloc, &file, NULL, NULL, NULL); filename = clang_getCString(clang_getFileName(file)); wantFilename = (const char *)client_data; if(!filename || strcmp(wantFilename, filename)) return CXChildVisit_Recurse; if(clang_getCursorLinkage(cursor) == CXLinkage_External) printf("+%s\n", clang_getCString(clang_getCursorSpelling(cursor))); else printf("?%s\n", clang_getCString(clang_getCursorSpelling(cursor))); } return CXChildVisit_Recurse; }
static void PrintCursor(CXCursor Cursor) { if (clang_isInvalid(Cursor.kind)) { CXString ks = clang_getCursorKindSpelling(Cursor.kind); printf("Invalid Cursor => %s", clang_getCString(ks)); clang_disposeString(ks); } else { CXString string, ks; CXCursor Referenced; unsigned line, column; ks = clang_getCursorKindSpelling(Cursor.kind); string = clang_getCursorSpelling(Cursor); printf("%s=%s", clang_getCString(ks), clang_getCString(string)); clang_disposeString(ks); clang_disposeString(string); Referenced = clang_getCursorReferenced(Cursor); if (!clang_equalCursors(Referenced, clang_getNullCursor())) { CXSourceLocation Loc = clang_getCursorLocation(Referenced); clang_getInstantiationLocation(Loc, 0, &line, &column, 0); printf(":%d:%d", line, column); } if (clang_isCursorDefinition(Cursor)) printf(" (Definition)"); } }
virtual enum CXChildVisitResult visit(CXCursor cursor, CXCursor parent) { CXFile file; unsigned int line, column, offset; clang_getInstantiationLocation( clang_getCursorLocation(cursor), &file, &line, &column, &offset); CXCursorKind kind = clang_getCursorKind(cursor); const char* cursorFilename = clang_getCString(clang_getFileName(file)); if (!clang_getFileName(file).data || strcmp(cursorFilename, translationUnitFilename) != 0) { return CXChildVisit_Continue; } CXCursor refCursor = clang_getCursorReferenced(cursor); if (!clang_equalCursors(refCursor, clang_getNullCursor())) { CXFile refFile; unsigned int refLine, refColumn, refOffset; clang_getInstantiationLocation( clang_getCursorLocation(refCursor), &refFile, &refLine, &refColumn, &refOffset); if (clang_getFileName(refFile).data) { std::string referencedUsr(clang_getCString(clang_getCursorUSR(refCursor))); if (!referencedUsr.empty()) { std::stringstream ss; ss << cursorFilename << ":" << line << ":" << column << ":" << kind; std::string location(ss.str()); usrToReferences[referencedUsr].insert(location); } } } return CXChildVisit_Recurse; }
enum CXChildVisitResult visitor(CXCursor cursor, CXCursor parent, CXClientData data) { enum CXCursorKind cKind = clang_getCursorKind(cursor); CXString nameString = clang_getCursorDisplayName(cursor); CXString typeString = clang_getCursorKindSpelling(cKind); printf("Name:%s, Kind:%s\n", clang_getCString(nameString), clang_getCString(typeString)); clang_disposeString(nameString); clang_disposeString(typeString); return CXChildVisit_Continue; }
void PrintDiagnostic(CXDiagnostic Diagnostic) { FILE *out = stderr; CXFile file; CXString Msg; unsigned display_opts = CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges; unsigned i, num_fixits; if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored) return; Msg = clang_formatDiagnostic(Diagnostic, display_opts); fprintf(stderr, "%s\n", clang_getCString(Msg)); clang_disposeString(Msg); clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic), &file, 0, 0, 0); if (!file) return; num_fixits = clang_getDiagnosticNumFixIts(Diagnostic); for (i = 0; i != num_fixits; ++i) { CXSourceRange range; CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range); CXSourceLocation start = clang_getRangeStart(range); CXSourceLocation end = clang_getRangeEnd(range); unsigned start_line, start_column, end_line, end_column; CXFile start_file, end_file; clang_getInstantiationLocation(start, &start_file, &start_line, &start_column, 0); clang_getInstantiationLocation(end, &end_file, &end_line, &end_column, 0); if (clang_equalLocations(start, end)) { /* Insertion. */ if (start_file == file) fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n", clang_getCString(insertion_text), start_line, start_column); } else if (strcmp(clang_getCString(insertion_text), "") == 0) { /* Removal. */ if (start_file == file && end_file == file) { fprintf(out, "FIX-IT: Remove "); PrintExtent(out, start_line, start_column, end_line, end_column); fprintf(out, "\n"); } } else { /* Replacement. */ if (start_file == end_file) { fprintf(out, "FIX-IT: Replace "); PrintExtent(out, start_line, start_column, end_line, end_column); fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text)); } break; } clang_disposeString(insertion_text); } }
QASTField::QASTField( QAnnotatedTokenSet *tokenSet, QSourceLocation* cursorLocation, QSourceLocation* rangeStartLocation, QSourceLocation* rangeEndLocation, QASTNode* parent) : QASTNode("field", tokenSet, cursorLocation, rangeStartLocation, rangeEndLocation, parent){ // Get Identifier // -------------- CXString id = clang_getCursorSpelling(tokenSet->cursor()); setIdentifier(clang_getCString(id)); // Get Field Type // --------------- CXType type = clang_getCursorType(tokenSet->cursor()); CXString typeSpelling = clang_getTypeSpelling(type); m_fieldType = clang_getCString(typeSpelling); clang_disposeString(typeSpelling); // Determine field type // -------------------- if ( type.kind == CXType_Unexposed || type.kind == CXType_Invalid || m_fieldType.contains("int") ){ m_fieldType = ""; bool doubleColonFlag = false; for ( QAnnotatedTokenSet::Iterator it = tokenSet->begin(); it != tokenSet->end(); ++it ){ CXToken t = (*it)->token().token; CXString tSpelling = clang_getTokenSpelling(tokenSet->translationUnit(), t); const char* tCSpelling = clang_getCString(tSpelling); CXTokenKind tKind = clang_getTokenKind(t); if ( tKind == CXToken_Identifier && std::string(clang_getCString(id)) == tCSpelling ){ break; } else if ( tKind == CXToken_Punctuation && std::string("::") == tCSpelling ){ doubleColonFlag = true; m_fieldType += tCSpelling; } else if ( ( tKind == CXToken_Identifier || tKind == CXToken_Keyword ) && !m_fieldType.isEmpty() && !doubleColonFlag ){ m_fieldType += QString(" ") + tCSpelling; } else { doubleColonFlag = false; m_fieldType += tCSpelling; } } } clang_disposeString(id); }
enum CXChildVisitResult property_list_builder(CXCursor cursor, CXCursor cursor_parent, CXClientData client_data) { ScintillaObject *current_doc_sci = (ScintillaObject *)client_data; enum CXCursorKind code_cursor_kind; CXSourceLocation code_source_location; code_source_location = clang_getCursorLocation(cursor); code_cursor_kind = clang_getCursorKind(cursor); if (code_cursor_kind == 1) { property_kind = property_helper_get_kind(current_doc_sci,code_source_location); } else if (code_cursor_kind == 6) { gchar *type = NULL; gchar *name = NULL; type = property_helper_get_type(current_doc_sci,code_source_location); name = (gchar *)clang_getCString(clang_getCursorSpelling(cursor)); chunked_property_add(&property_list, type, name, sg_do_getters, sg_do_setters, sg_placement_inner, property_kind); free(type); free(name); } return CXChildVisit_Continue; }
void print_completion_string(CXCompletionString completion_string, FILE *file) { int I, N; N = clang_getNumCompletionChunks(completion_string); for (I = 0; I != N; ++I) { CXString text; const char *cstr; enum CXCompletionChunkKind Kind = clang_getCompletionChunkKind(completion_string, I); if (Kind == CXCompletionChunk_Optional) { fprintf(file, "{Optional "); print_completion_string( clang_getCompletionChunkCompletionString(completion_string, I), file); fprintf(file, "}"); continue; } text = clang_getCompletionChunkText(completion_string, I); cstr = clang_getCString(text); fprintf(file, "{%s %s}", clang_getCompletionChunkKindSpelling(Kind), cstr ? cstr : ""); clang_disposeString(text); } }
QString getQString(const CXString &cxString, bool disposeCXString) { QString s = QString::fromUtf8(clang_getCString(cxString)); if (disposeCXString) clang_disposeString(cxString); return s; }
/* Handle syntax checking request, message format: source_length: [#src_length#] <# SOURCE CODE #> */ void completion_doSyntaxCheck(completion_Session *session, FILE *fp) { unsigned int i_diag = 0, n_diag; CXDiagnostic diag; CXString dmsg; /* get a copy of fresh source file */ completion_readSourcefile(session, fp); /* reparse the source to retrieve diagnostic message */ completion_reparseTranslationUnit(session); /* dump all diagnostic messages to fp */ n_diag = clang_getNumDiagnostics(session->cx_tu); for ( ; i_diag < n_diag; i_diag++) { diag = clang_getDiagnostic(session->cx_tu, i_diag); dmsg = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions()); fprintf(stdout, "%s\n", clang_getCString(dmsg)); clang_disposeString(dmsg); clang_disposeDiagnostic(diag); } fprintf(stdout, "$"); fflush(stdout); /* end of output */ }
std::ostream& operator<<( std::ostream& os, CXType type ) { auto typestr = clang_getTypeSpelling( type ); os << "Type: " << clang_getCString( typestr ) << "\n"; clang_disposeString( typestr ); return os; }
static int code_completion_results_cmp(CXCompletionResult *r1, CXCompletionResult *r2) { int prio1 = clang_getCompletionPriority(r1->CompletionString); int prio2 = clang_getCompletionPriority(r2->CompletionString); if (prio1 != prio2) return prio1 - prio2; CXString r1t = get_result_typed_text(r1); CXString r2t = get_result_typed_text(r2); int cmp = strcmp(clang_getCString(r1t), clang_getCString(r2t)); clang_disposeString(r1t); clang_disposeString(r2t); return cmp; }
static enum CXChildVisitResult visitor_enum_cb(CXCursor cursor, CXCursor parent, CXClientData client_data) { EnumData* data = (EnumData*)client_data; ErlNifEnv *env = data->env; ERL_NIF_TERM name; ERL_NIF_TERM value; long long cvalue; CXString tmp; char* cstr; switch (clang_getCursorKind(cursor)) { case CXCursor_EnumConstantDecl: { tmp = clang_getCursorSpelling(cursor); cstr = (char*)clang_getCString(tmp); cvalue = clang_getEnumConstantDeclValue(cursor); name = enif_make_string(env, cstr, ERL_NIF_LATIN1); value = enif_make_int64(env, cvalue); data->enum_values = enif_make_list_cell(env, enif_make_tuple2(env, name, value), data->enum_values); } default: { return CXChildVisit_Continue; } } }
void ast_json::record_token( json_t& obj, CXToken token, CXTranslationUnit translation_unit) { // Record token kind switch (clang_getTokenKind(token)) { case CXToken_Punctuation: obj[lex_token::KIND] = "punctuation"; break; case CXToken_Keyword: obj[lex_token::KIND] = "keyword"; break; case CXToken_Identifier: obj[lex_token::KIND] = "identifier"; break; case CXToken_Literal: obj[lex_token::KIND] = "literal"; break; case CXToken_Comment: obj[lex_token::KIND] = "comment"; break; } // Get attributes CXString spelling = clang_getTokenSpelling(translation_unit, token); CXSourceRange extent = clang_getTokenExtent(translation_unit, token); obj[lex_token::DATA] = std::string(clang_getCString(spelling)); record_extent(obj, extent); // Reclaim memory clang_disposeString(spelling); }
static const char* GetCursorSource(CXCursor Cursor) { CXSourceLocation Loc = clang_getCursorLocation(Cursor); CXString source; CXFile file; clang_getInstantiationLocation(Loc, &file, 0, 0, 0); source = clang_getFileName(file); if (!clang_getCString(source)) { clang_disposeString(source); return "<invalid loc>"; } else { const char *b = basename(clang_getCString(source)); clang_disposeString(source); return b; } }
static void printString(const char *name, CXString string) { const char *cstr = clang_getCString(string); if (cstr && *cstr) { printf("%s: %s ", name, cstr); } clang_disposeString(string); }
std::string libclang_vim::stringize_key_value(const char* key_name, const cxstring_ptr& p) { const auto* cstring = clang_getCString(p); if (!cstring || std::strcmp(cstring, "") == 0) return ""; else return "'" + std::string{key_name} + "':'" + cstring + "',"; }
json_t* render_string_or_null(CXString s) { const char* str = clang_getCString(s); json_t* js; if (str && strlen(str) > 0) js = json_string(str); else js = json_null(); clang_disposeString(s); return js; }
void ClangUtils::printDiagnosticsToLog(CXTranslationUnit& TU) { //// Report diagnostics to the log file const unsigned diagCount = clang_getNumDiagnostics(TU); for(unsigned i=0; i<diagCount; i++) { CXDiagnostic diag = clang_getDiagnostic(TU, i); CXString diagStr = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions()); wxString wxDiagString = wxString(clang_getCString(diagStr), wxConvUTF8); if(!wxDiagString.Contains(wxT("'dllimport' attribute"))) { fprintf(stdout, "Diagnostic: %s\n", clang_getCString(diagStr)); } clang_disposeString(diagStr); clang_disposeDiagnostic(diag); } }
std::string Cursor::raw_comment() const { UniqueCXString cx_string(clang_Cursor_getRawCommentText(m_cx_cursor)); if ( !cx_string ) { CLANGXX_THROW_LogicError("Error retrieving the raw comment text associated with this cursor."); } return clang_getCString(cx_string.get()); }
int main(int argc, const char* argv[]) { if (argc < 4) { std::cerr << "Usage:\n" << " " << argv[0] << " <dbFilename> <indexFilename> [<options>] <sourceFilename>\n"; return 1; } const char* dbFilename = argv[1]; const char* indexFilename = argv[2]; const char* sourceFilename = argv[argc-1]; // Set up the clang translation unit CXIndex cxindex = clang_createIndex(0, 0); CXTranslationUnit tu = clang_parseTranslationUnit( cxindex, 0, argv + 3, argc - 3, // Skip over dbFilename and indexFilename 0, 0, CXTranslationUnit_None); // Print any errors or warnings int n = clang_getNumDiagnostics(tu); if (n > 0) { int nErrors = 0; for (unsigned i = 0; i != n; ++i) { CXDiagnostic diag = clang_getDiagnostic(tu, i); CXString string = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions()); fprintf(stderr, "%s\n", clang_getCString(string)); if (clang_getDiagnosticSeverity(diag) == CXDiagnostic_Error || clang_getDiagnosticSeverity(diag) == CXDiagnostic_Fatal) nErrors++; } } // Create the index EverythingIndexer visitor(sourceFilename); clang_visitChildren( clang_getTranslationUnitCursor(tu), &visitorFunction, &visitor); ClicIndex& index = visitor.usrToReferences; // OK, now write the index to a compressed file std::ofstream fout(indexFilename); boost::iostreams::filtering_stream<boost::iostreams::output> zout; zout.push(boost::iostreams::gzip_compressor()); zout.push(fout); printIndex(zout, index); // Now open the database and add the index to it ClicDb db(dbFilename); BOOST_FOREACH(const ClicIndex::value_type& it, index) { const std::string& usr = it.first; db.addMultiple(usr, it.second); } return 0; }
std::ostream& operator<<( std::ostream &os, CXCursor cursor ) { auto spelling = clang_getCursorKindSpelling( cursor.kind ); auto morespell = clang_getCursorSpelling( cursor ); os << clang_getCString( spelling ) << ":" << clang_getCString( morespell ) << " "; clang_disposeString( spelling ); clang_disposeString( morespell ); auto refcursor = clang_getCursorReferenced( cursor ); if ( clang_equalCursors( refcursor, clang_getNullCursor() )|| clang_equalCursors( refcursor, cursor ) ) return os; return os << " [ " << refcursor << " ] "; }
string CursorHelper::getFileName(CXCursor cursor) { CXFile file; clang_getSpellingLocation(clang_getCursorLocation(cursor), &file, 0, 0, 0); CXString fileStr = clang_getFileName(file); string fileName(clang_getCString(fileStr)); clang_disposeString(fileStr); return fileName; }
void DisplayDiagnostics() { unsigned NumDiagnostics = clang_getNumDiagnostics(ClangTU); for (unsigned i = 0; i < NumDiagnostics; ++i) { auto Diag = clang_getDiagnostic(ClangTU, i); DEBUG(llvm::dbgs() << clang_getCString(clang_formatDiagnostic( Diag, clang_defaultDiagnosticDisplayOptions())) << "\n"); clang_disposeDiagnostic(Diag); } }
const char* getArgNameCString(char* buff, const CXString& name, int idx) { const char* nameStr = clang_getCString(name); if (strlen(nameStr) == 0) { sprintf(buff, "arg%d", idx); nameStr = buff; } return nameStr; }
std::string Cursor::get_usr() const { UniqueCXString cx_string(clang_getCursorUSR(m_cx_cursor)); if ( !cx_string ) { CLANGXX_THROW_LogicError("Error retrieving a Unified Symbol Resolution (USR) for the entity referenced by this cursor."); } return clang_getCString(cx_string.get()); }
/** Callback function called for each include in a translation unit */ static void inclusionVisitor(CXFile includedFile, CXSourceLocation * , unsigned, CXClientData clientData) { QSet<QString> *fileDict = reinterpret_cast<QSet<QString> *>(clientData); CXString incFileName = clang_getFileName(includedFile); fileDict->insert(clang_getCString(incFileName)); clang_disposeString(incFileName); }
static void printVersion() { // do not change the format for the first line, external programs should be // able to rely on it std::cout << "irony-server version " IRONY_PACKAGE_VERSION "\n"; CXString cxVersionString = clang_getClangVersion(); std::cout << clang_getCString(cxVersionString) << "\n"; clang_disposeString(cxVersionString); }
static enum CXChildVisitResult ide_clang_service_build_index_visitor (CXCursor cursor, CXCursor parent, CXClientData user_data) { IndexRequest *request = user_data; enum CXCursorKind kind; const gchar *style_name = NULL; g_assert (request != NULL); kind = clang_getCursorKind (cursor); switch ((int)kind) { case CXCursor_TypedefDecl: case CXCursor_TypeAliasDecl: style_name = IDE_CLANG_HIGHLIGHTER_TYPE; break; case CXCursor_FunctionDecl: style_name = IDE_CLANG_HIGHLIGHTER_FUNCTION_NAME; break; case CXCursor_EnumDecl: style_name = IDE_CLANG_HIGHLIGHTER_ENUM_NAME; clang_visitChildren (cursor, ide_clang_service_build_index_visitor, user_data); break; case CXCursor_EnumConstantDecl: style_name = IDE_CLANG_HIGHLIGHTER_ENUM_NAME; break; case CXCursor_MacroDefinition: style_name = IDE_CLANG_HIGHLIGHTER_MACRO_NAME; break; default: break; } if (style_name != NULL) { CXString cxstr; const gchar *word; cxstr = clang_getCursorSpelling (cursor); word = clang_getCString (cxstr); ide_highlight_index_insert (request->index, word, (gpointer)style_name); clang_disposeString (cxstr); } return CXChildVisit_Continue; }
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; }