bool QueryJob::locationToString(Location location, const std::function<void(LocationPiece, const String &)> &cb, Flags<WriteFlag> writeFlags) { if (location.isNull()) return false; Flags<Location::ToStringFlag> kf = locationToStringFlags(); kf &= ~Location::ShowContext; cb(Piece_Location, location.toString(kf, &mContextCache)); if (!(writeFlags & NoContext) && !(queryFlags() & QueryMessage::NoContext)) cb(Piece_Context, location.context(kf, &mContextCache)); const bool containingFunction = queryFlags() & QueryMessage::ContainingFunction; const bool containingFunctionLocation = queryFlags() & QueryMessage::ContainingFunctionLocation; const bool cursorKind = queryFlags() & QueryMessage::CursorKind; const bool displayName = queryFlags() & QueryMessage::DisplayName; if (containingFunction || containingFunctionLocation || cursorKind || displayName || !mKindFilters.isEmpty()) { int idx; Symbol symbol = project()->findSymbol(location, &idx); if (symbol.isNull()) { error() << "Somehow can't find" << location << "in symbols"; } else { if (!mKindFilters.filter(symbol)) return false; if (displayName) cb(Piece_SymbolName, symbol.displayName()); if (cursorKind) cb(Piece_Kind, symbol.kindSpelling()); if (containingFunction || containingFunctionLocation) { const uint32_t fileId = location.fileId(); const unsigned int line = location.line(); const unsigned int column = location.column(); auto fileMap = project()->openSymbols(location.fileId()); if (fileMap) { while (idx > 0) { symbol = fileMap->valueAt(--idx); if (symbol.location.fileId() != fileId) break; if (symbol.isDefinition() && RTags::isContainer(symbol.kind) && comparePosition(line, column, symbol.startLine, symbol.startColumn) >= 0 && comparePosition(line, column, symbol.endLine, symbol.endColumn) <= 0) { if (containingFunction) cb(Piece_ContainingFunctionName, symbol.symbolName); if (containingFunctionLocation) cb(Piece_ContainingFunctionLocation, symbol.location.toString(locationToStringFlags() & ~Location::ShowContext)); break; } } } } } } return true; }
CXChildVisitResult IndexerJob::dumpVisitor(CXCursor cursor, CXCursor, CXClientData userData) { DumpUserData *dump = reinterpret_cast<DumpUserData*>(userData); assert(dump); assert(dump->job); Location loc = dump->job->createLocation(cursor); if (loc.fileId()) { CXCursor ref = clang_getCursorReferenced(cursor); String out; out.reserve(256); int col = -1; if (dump->showContext) { out.append(loc.context(&col)); if (col != -1) { out.append(String::format<32>(" // %d, %d: ", col, dump->indentLevel)); } else { out.append(String::format<32>(" // %d: ", dump->indentLevel)); } } else { out.append(String(dump->indentLevel * 2, ' ')); } out.append(RTags::cursorToString(cursor, RTags::AllCursorToStringFlags)); out.append(" " + typeName(cursor) + " "); if (clang_equalCursors(ref, cursor)) { out.append("refs self"); } else if (!clang_equalCursors(ref, nullCursor)) { out.append("refs "); out.append(RTags::cursorToString(ref, RTags::AllCursorToStringFlags)); } dump->job->write(out); } ++dump->indentLevel; clang_visitChildren(cursor, IndexerJob::dumpVisitor, userData); --dump->indentLevel; return CXChildVisit_Continue; }
CXChildVisitResult DumpThread::visit(const CXCursor &cursor) { if (isAborted()) return CXChildVisit_Break; const Location location = createLocation(cursor); if (!location.isNull()) { if (mQueryFlags & QueryMessage::DumpCheckIncludes) { checkIncludes(location, cursor); return CXChildVisit_Recurse; } else { Flags<Location::ToStringFlag> locationFlags; if (mQueryFlags & QueryMessage::NoColor) locationFlags |= Location::NoColor; CXSourceRange range = clang_getCursorExtent(cursor); CXSourceLocation rangeEnd = clang_getRangeEnd(range); unsigned int endLine, endColumn; clang_getPresumedLocation(rangeEnd, 0, &endLine, &endColumn); if (!(mQueryFlags & QueryMessage::DumpIncludeHeaders) && location.fileId() != mSource.fileId) { return CXChildVisit_Continue; } String message; message.reserve(256); if (!(mQueryFlags & QueryMessage::NoContext)) { message = location.context(locationFlags); } if (endLine == location.line()) { message += String::format<32>(" // %d-%d, %d: ", location.column(), endColumn, mIndentLevel); } else { message += String::format<32>(" // %d-%d:%d, %d: ", location.column(), endLine, endColumn, mIndentLevel); } message += RTags::cursorToString(cursor, RTags::AllCursorToStringFlags); message.append(" " + RTags::typeName(cursor));; if (clang_getCursorKind(cursor) == CXCursor_VarDecl) { const std::shared_ptr<RTags::Auto> autoResolved = RTags::resolveAuto(cursor); if (autoResolved && !clang_equalCursors(autoResolved->cursor, nullCursor)) { message += "auto resolves to " + RTags::cursorToString(autoResolved->cursor, RTags::AllCursorToStringFlags); } } 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)); } CXCursor specialized = clang_getSpecializedCursorTemplate(cursor); if (!clang_equalCursors(specialized, cursor) && !clang_equalCursors(specialized, nullCursor)) { message.append("specialized "); message.append(RTags::cursorToString(specialized, RTags::AllCursorToStringFlags)); } writeToConnetion(message); } } ++mIndentLevel; clang_visitChildren(cursor, DumpThread::visitor, this); if (isAborted()) return CXChildVisit_Break; --mIndentLevel; return CXChildVisit_Continue; }