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; }
Location TranslationUnit::GetDeclarationLocation( int line, int column, const std::vector< UnsavedFile > &unsaved_files, bool reparse ) { if ( reparse ) Reparse( unsaved_files ); unique_lock< mutex > lock( clang_access_mutex_ ); if ( !clang_translation_unit_ ) return Location(); CXCursor cursor = GetCursor( line, column ); if ( !CursorIsValid( cursor ) ) return Location(); CXCursor referenced_cursor = clang_getCursorReferenced( cursor ); if ( !CursorIsValid( referenced_cursor ) ) return Location(); CXCursor canonical_cursor = clang_getCanonicalCursor( referenced_cursor ); if ( !CursorIsValid( canonical_cursor ) ) return Location( clang_getCursorLocation( referenced_cursor ) ); return Location( clang_getCursorLocation( canonical_cursor ) ); }
Location TranslationUnit::GetDeclarationLocationForCursor( CXCursor cursor ) { CXCursor referenced_cursor = clang_getCursorReferenced( cursor ); if ( !CursorIsValid( referenced_cursor ) ) { return Location(); } CXCursor canonical_cursor = clang_getCanonicalCursor( referenced_cursor ); if ( !CursorIsValid( canonical_cursor ) ) { return Location( clang_getCursorLocation( referenced_cursor ) ); } return Location( clang_getCursorLocation( canonical_cursor ) ); }
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)"); } }
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; }
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; }
CXChildVisitResult visitor( CXCursor cursor, CXCursor parent, CXClientData client_data) { ClangTools::TranslationUnit::AstWalker * astWalker = reinterpret_cast<ClangTools::TranslationUnit::AstWalker *>(client_data); std::vector<std::string> & funcNames = *(reinterpret_cast<std::vector<std::string> *>(astWalker->getClientData())); auto sourceLocation = clang_getCursorLocation(cursor); CXFile file; clang_getFileLocation(sourceLocation, &file, 0, 0, 0); auto fileName = clang_getFileName(file); auto fileNameStr = ClangTools::String(fileName); if (fileNameStr != astWalker->getFileName()) { return CXChildVisit_Recurse; } auto func = std::string{}; auto def = bool{false}; if (clang_isCursorDefinition(cursor)) { def = true; } switch (clang_getCursorKind(cursor)) { case CXCursor_FunctionDecl: func = "Function "; break; case CXCursor_FunctionTemplate: func = "FunctionTemplate "; break; case CXCursor_CXXMethod: func = "CXXMethod "; break; default: return CXChildVisit_Recurse; break; } func += clang_isCursorDefinition(cursor) ? "definition: " : "declaration: "; auto semanticParent = clang_getCursorSemanticParent(cursor); auto semanticParentSpelling = clang_getCursorSpelling(semanticParent); func += ClangTools::String(semanticParentSpelling) + "::"; auto cursorSpelling = clang_getCursorSpelling(cursor); func += ClangTools::String(cursorSpelling); auto lexicalParent = clang_getCursorLexicalParent(cursor); auto lexicalParentSpelling = clang_getCursorSpelling(lexicalParent); func += " found in: " + ClangTools::String(lexicalParentSpelling); funcNames.push_back(func); return CXChildVisit_Recurse; }
void wci_getCursorFile(char* cuin, char* cxsout) { CXCursor cu = wci_get_CXCursor(cuin); CXSourceLocation loc = clang_getCursorLocation( cu ); CXFile cxfile; clang_getExpansionLocation(loc, &cxfile, 0, 0, 0); wci_save_CXString(clang_getFileName(cxfile), cxsout); }
CXChildVisitResult DumpThread::visitor(CXCursor cursor, CXCursor, CXClientData userData) { DumpThread *that = reinterpret_cast<DumpThread*>(userData); assert(that); CXSourceLocation location = clang_getCursorLocation(cursor); if (!clang_equalLocations(location, nullLocation)) { CXString file; unsigned line, col; clang_getPresumedLocation(location, &file, &line, &col); Path path = RTags::eatString(file); if (!path.isEmpty()) { uint32_t &fileId = that->mFiles[path]; if (!fileId) { const Path resolved = path.resolved(); fileId = Location::insertFile(resolved); that->mFiles[path] = that->mFiles[resolved] = fileId; } if (that->mQueryFlags & QueryMessage::DumpIncludeHeaders || fileId == that->mSource.fileId) { const Location loc(fileId, line, col); String message; message.reserve(256); if (!(that->mQueryFlags & QueryMessage::NoContext)) message += loc.context(); CXSourceRange range = clang_getCursorExtent(cursor); CXSourceLocation rangeEnd = clang_getRangeEnd(range); unsigned endLine, endColumn; clang_getPresumedLocation(rangeEnd, 0, &endLine, &endColumn); if (endLine == line) { message += String::format<32>(" // %d-%d, %d: ", col, endColumn, that->mIndentLevel); } else { message += String::format<32>(" // %d-%d:%d, %d: ", col, endLine, endColumn, that->mIndentLevel); } message += RTags::cursorToString(cursor, RTags::AllCursorToStringFlags); message.append(" " + RTags::typeName(cursor) + " "); CXCursor ref = clang_getCursorReferenced(cursor); if (clang_equalCursors(ref, cursor)) { message.append("refs self"); } else if (!clang_equalCursors(ref, nullCursor)) { message.append("refs "); message.append(RTags::cursorToString(ref, RTags::AllCursorToStringFlags)); } CXCursor canonical = clang_getCanonicalCursor(cursor); if (!clang_equalCursors(canonical, cursor) && !clang_equalCursors(canonical, nullCursor)) { message.append("canonical "); message.append(RTags::cursorToString(canonical, RTags::AllCursorToStringFlags)); } that->writeToConnetion(message); } } } ++that->mIndentLevel; clang_visitChildren(cursor, DumpThread::visitor, userData); --that->mIndentLevel; return CXChildVisit_Continue; }
Location TranslationUnit::GetDefinitionLocationForCursor( CXCursor cursor ) { CXCursor definition_cursor = clang_getCursorDefinition( cursor ); if ( !CursorIsValid( definition_cursor ) ) { return Location(); } return Location( clang_getCursorLocation( definition_cursor ) ); }
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; }
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; }
SEXP R_clang_getCursorLocation(SEXP r_arg1) { SEXP r_ans = R_NilValue; CXCursor arg1 = * GET_REF(r_arg1, CXCursor); CXSourceLocation ans; ans = clang_getCursorLocation(arg1); r_ans = R_makeCXSourceLocation(ans) ; return(r_ans); }
Location IndexerJob::createLocation(const CXCursor &cursor) { CXSourceLocation location = clang_getCursorLocation(cursor); if (!clang_equalLocations(location, nullLocation)) { CXFile file; unsigned start; clang_getSpellingLocation(location, &file, 0, 0, &start); if (file) { return Location(file, start); } } return Location(); }
void ClangUtils::GetCursorLocation(CXCursor cursor, wxString& filename, unsigned& line, unsigned& col) { CXSourceLocation loc = clang_getCursorLocation(cursor); CXFile file; unsigned off; line = 1, col = 1; clang_getSpellingLocation(loc, &file, &line, &col, &off); CXString strFileName = clang_getFileName(file); filename = wxString(clang_getCString(strFileName), wxConvUTF8); clang_disposeString(strFileName); }
location translation_unit::definition_location_at(uint32_t row, uint32_t col) { CXCursor cursor = get_cursor_at(row, col); CXCursor ref = clang_getCursorDefinition( cursor ); if (clang_Cursor_isNull(ref) || clang_isInvalid(clang_getCursorKind(ref))) return {"", 0, 0}; CXSourceLocation loc = clang_getCursorLocation(ref); CXFile file; uint32_t nrow, ncol, offset = 0; clang_getExpansionLocation( loc, &file, &nrow, &ncol, &offset ); return { cx2std(clang_getFileName(file)), nrow, ncol }; }
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; } }
enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor, CXCursor Parent, CXClientData ClientData) { VisitorData *Data = (VisitorData *)ClientData; if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) { CXSourceLocation Loc = clang_getCursorLocation(Cursor); unsigned line, column; clang_getInstantiationLocation(Loc, 0, &line, &column, 0); printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Cursor), line, column); PrintCursor(Cursor); PrintCursorExtent(Cursor); printf("\n"); return CXChildVisit_Recurse; } return CXChildVisit_Continue; }
bool filterByFilename(CXCursor cu) { // hardcoded to exclude anything from '/usr/include' - todo: take a list of paths to accept/ reject bool ret=true; CXSourceLocation srcLoc = clang_getCursorLocation(cu); CXFile file; clang_getSpellingLocation(srcLoc,&file,0,0,0); CXString filename = clang_getFileName(file); const char* szfilename=clang_getCString(filename); const char* exclude_path="/usr/include"; if (szfilename) { const char* s1=szfilename,*s2=exclude_path; while (*s1 && *s2) { if (*s1!=*s2) break; s1++,s2++; } if (!*s2) ret=false; if (!ret) {printf("filtered out %s\n", szfilename);} } clang_disposeString(filename); return ret; }
CXChildVisitResult visitor( CXCursor cursor, CXCursor parent, CXClientData d ) { auto range = clang_getCursorExtent( cursor ); auto space = reinterpret_cast<int>( d ); if ( space > 0 ) std::cout << std::setw( space ) << " "; std::cout << cursor << " " << range << "\n"; space += 2; auto location = clang_getCursorLocation( cursor ); CXFile thisfile; clang_getSpellingLocation( location, &thisfile, NULL, NULL, NULL ); if ( cursor.kind != CXCursor_InclusionDirective && clang_getFile( tu, filename.c_str() ) == thisfile ) clang_visitChildren( cursor, visitor, reinterpret_cast<CXClientData>(space) ); return CXChildVisit_Continue; }
static void printCursor(CXCursor cursor) { CXFile file; unsigned int off, line, col; CXSourceLocation location = clang_getCursorLocation(cursor); clang_getSpellingLocation(location, &file, &line, &col, &off); CXString fileName = clang_getFileName(file); const char *fileNameCStr = clang_getCString(fileName); if (fileNameCStr) { CXSourceRange range = clang_getCursorExtent(cursor); unsigned int start, end; clang_getSpellingLocation(clang_getRangeStart(range), 0, 0, 0, &start); clang_getSpellingLocation(clang_getRangeEnd(range), 0, 0, 0, &end); printf("%s:%d:%d (%d, %d-%d) ", fileNameCStr, line, col, off, start, end); } clang_disposeString(fileName); printString("kind", clang_getCursorKindSpelling(clang_getCursorKind(cursor))); printString("display name", clang_getCursorDisplayName(cursor)); printString("usr", clang_getCursorUSR(cursor)); if (clang_isCursorDefinition(cursor)) printf("definition "); printf("\n"); }
std::string libclang_vim::stringize_cursor_location(CXCursor const& cursor) { CXSourceLocation const location = clang_getCursorLocation(cursor); return stringize_location(location); }
bool Cursor::IsFromMainFile() const { auto location = clang_getCursorLocation(cursor_); return clang_Location_isFromMainFile(location) != 0; }
SourceLocation Cursor::GetSourceLocation() const { return clang_getCursorLocation(cursor_); }
static inline bool isImplicit(const CXCursor &cursor) { return clang_equalLocations(clang_getCursorLocation(cursor), clang_getCursorLocation(clang_getCursorSemanticParent(cursor))); }
void Html_File::write_token(FILE* f, CXFile file, CXToken tok, const char* str, unsigned line, unsigned column) { static bool preprocessor = false; static bool include = false; CXSourceLocation tloc = clang_getTokenLocation(tu_file_->tu(), tok); CXCursor c = clang_getCursor(tu_file_->tu(), tloc); if (cur_line_ <= line) cur_column_ = 1; for (; cur_line_ <= line; ++cur_line_) fprintf (f, "\n<a name=\"l%05i\"></a>%05i", cur_line_, cur_line_); for (; cur_column_ <= column; ++cur_column_) fprintf (f , " "); switch (clang_getTokenKind(tok)) { case (CXToken_Punctuation): if (str[0] == '#') preprocessor = true; fprintf(f, "%s", str); break; case (CXToken_Keyword): fprintf(f, "<span class=\"keyword\">%s</span>", str); break; case (CXToken_Comment): fprintf(f, "<span class=\"comment\">%s</span>", str); break; case (CXToken_Literal): { //include = false; // disable include links for now if (include) { include = false; // found an include file std::string t; const char* p = str; while (*p) { if (*p != '"') t += *p; ++p; } // first, use this file's path, then all the include paths bool found_include = false; char path[PATH_MAX]; std::string includefile = realpath(dirname(tu_file_->source_filename()), path); includefile += "/" + t; struct stat st; if (stat(includefile.c_str(), &st) == 0) { found_include = true; } else { for (std::vector<std::string>::const_iterator i = includes_.begin(), e = includes_.end(); i != e; ++i) { includefile = realpath((*i).c_str(), path); includefile += "/" + t; if (stat(includefile.c_str(), &st) == 0) { found_include = true; break; } } } if (found_include) { if (files_.find(includefile) != files_.end()) { t = make_filename(includefile, html_dir_, prefix_, ".html", false); fprintf(f, "<a class=\"code\" href=\"%s\" title="">%s</a>", t.c_str(), str); break; } std::map<std::string, Definition>::iterator i = defmap_.find(includefile); if (i != defmap_.end()) { t = i->second.file.c_str(); fprintf(f, "<a class=\"code\" href=\"%s\" title="">%s</a>", t.c_str(), str); break; } } } // not an include or include not found std::string s = fix(str); fprintf(f, "%s", s.c_str() ); break; } case (CXToken_Identifier): { if (preprocessor) { preprocessor = false; if (strcmp(str, "include") == 0) include = true; fprintf(f, "<span class=\"code\">%s</span>", str); break; } if (clang_isUnexposed(c.kind)) { fprintf(f, "<span class=\"code\">%s</span>", str); fprintf(f, "<!-- origin line: %i : %s : kind = %i -->", __LINE__, str, c.kind); break; } // Calling clang_getCursorDefinition() does not work properly // for template classes, i.e., it will find the method // declaration, not the definition, if they differ. However, // once you have the declaration's location, you can use it // get that cursor, and find the definition that way. CXSourceLocation decloc = clang_getCursorLocation(clang_getCursorDefinition(c)); CXCursor cref = clang_getCursorDefinition(clang_getCursor(tu_file_->tu(), decloc)); if (clang_isUnexposed(cref.kind)) { fprintf(f, "<span class=\"code\">%s</span>", str); fprintf(f, "<!-- origin line: %i : (ref) %s : kind = %i -->", __LINE__, str, cref.kind); break; } std::string rfile; std::string html_dir; unsigned refl = line; bool found = false; if (!clang_Cursor_isNull(cref) && cref.kind != CXCursor_Namespace) { CXSourceLocation refloc = clang_getCursorLocation(cref); if (!clang_equalLocations(tloc, refloc)) { CXFile cxfile; unsigned col; unsigned off; clang_getExpansionLocation(refloc, &cxfile, &refl, &col, &off); if (cxfile == file) { found = true; fprintf(f, "<!-- origin line: %i : (ref) %s : kind = %i -->", __LINE__, str, cref.kind); } else { CXString cxfn = clang_getFileName(cxfile); const char* fn = clang_getCString(cxfn); if (fn) { if (files_.find(fn) != files_.end()) { rfile = fn; found = true; fprintf(f, "<!-- origin line: %i : (ref) %s : kind = %i -->", __LINE__, str, cref.kind); } } clang_disposeString(cxfn); } } } else if (!clang_isDeclaration(c.kind) && c.kind != CXCursor_Namespace) { CXCursor ref = clang_getCursorReferenced(c); if (ref.kind != CXCursor_Namespace) { std::string fsn = munge_fullyscopedname(fullyScopedName(ref)); if (fsn.empty()) { fprintf(f, "<!-- origin line: %i : (fsn empty) %s : kind = %i -->", __LINE__, str, c.kind); } else { std::map<std::string, Definition>::iterator r = defmap_.find(fsn); if (r != defmap_.end()) { found = true; fprintf(f, "<!-- origin line: %i : %s : kind = %i -->", __LINE__, fsn.c_str(), c.kind); rfile = r->second.file.c_str(); html_dir = r->second.html_path.c_str(); refl = r->second.line; } } } } // since we are linking to lines, no need to link to same line if (found && (!rfile.empty() || refl != line)) { if (!rfile.empty()) rfile = make_filename(rfile, html_dir, prefix_, ".html", !html_dir.empty()); fprintf(f, "<a class=\"code\" href=\"%s#l%05i\" title="">%s</a>", rfile.c_str(), refl , str); break; } fprintf(f, "<span class=\"code\">%s</span>", str); break; } } cur_column_ += strlen(str); }
SourceLocation::SourceLocation(Cursor& cursor) { location_ = clang_getCursorLocation(cursor.cursor_); }
source_location cursor::location() { return { clang_getCursorLocation(cur) }; }
string CursorHelper::getColumnNumber(CXCursor cursor) { unsigned column; clang_getSpellingLocation(clang_getCursorLocation(cursor), 0, 0, &column, 0); return itoa(column); }
string CursorHelper::getLineNumber(CXCursor cursor) { unsigned line; clang_getSpellingLocation(clang_getCursorLocation(cursor), 0, &line, 0, 0); return itoa(line); }