void IndexerJob::handleInclude(const CXCursor &cursor, CXCursorKind kind, const Location &location) { assert(kind == CXCursor_InclusionDirective); (void)kind; CXFile includedFile = clang_getIncludedFile(cursor); if (includedFile) { const Location refLoc(includedFile, 0); if (!refLoc.isNull()) { { String include = "#include "; const Path path = refLoc.path(); mData->symbolNames[(include + path)].insert(location); mData->symbolNames[(include + path.fileName())].insert(location); } CursorInfo &info = mData->symbols[location]; info.targets.insert(refLoc); info.kind = cursor.kind; info.definition = false; info.symbolName = "#include " + RTags::eatString(clang_getCursorDisplayName(cursor)); info.symbolLength = info.symbolName.size() + 2; // this fails for things like: // # include <foobar.h> } } }
void ClangIndexer::handleInclude(const CXCursor &cursor, CXCursorKind kind, const Location &location) { assert(kind == CXCursor_InclusionDirective); (void)kind; CXFile includedFile = clang_getIncludedFile(cursor); if (includedFile) { const Location refLoc = createLocation(includedFile, 1, 1); if (!refLoc.isNull()) { { String include = "#include "; const Path path = refLoc.path(); assert(mSource.fileId); mData->dependencies[refLoc.fileId()].insert(mSource.fileId); mData->symbolNames[(include + path)].insert(location); mData->symbolNames[(include + path.fileName())].insert(location); } std::shared_ptr<CursorInfo> &info = mData->symbols[location]; if (!info) info = std::make_shared<CursorInfo>(); info->targets.insert(refLoc); info->kind = cursor.kind; info->definition = false; info->symbolName = "#include " + RTags::eatString(clang_getCursorDisplayName(cursor)); info->symbolLength = info->symbolName.size() + 2; // this fails for things like: // # include <foobar.h> } } }
std::string libclang_vim::stringize_included_file(CXCursor const& cursor) { CXFile included_file = clang_getIncludedFile(cursor); if (included_file == nullptr) { return ""; } cxstring_ptr included_file_name = clang_getFileName(included_file); return std::string("'included_file':'") + to_c_str(included_file_name) + "',"; }
SourceRangeContainer FollowSymbol::followSymbol(CXTranslationUnit tu, const Cursor &fullCursor, uint line, uint column) { std::unique_ptr<Tokens> tokens(new Tokens(fullCursor)); if (!tokens->tokenCount) tokens.reset(new Tokens(tu)); if (!tokens->tokenCount) return SourceRangeContainer(); QVector<CXCursor> cursors(static_cast<int>(tokens->tokenCount)); clang_annotateTokens(tu, tokens->data, tokens->tokenCount, cursors.data()); int tokenIndex = getTokenIndex(tu, *tokens, line, column); QTC_ASSERT(tokenIndex >= 0, return SourceRangeContainer()); const Utf8String tokenSpelling = ClangString( clang_getTokenSpelling(tu, tokens->data[tokenIndex])); if (tokenSpelling.isEmpty()) return SourceRangeContainer(); Cursor cursor{cursors[tokenIndex]}; if (cursor.kind() == CXCursor_InclusionDirective) { CXFile file = clang_getIncludedFile(cursors[tokenIndex]); const ClangString filename(clang_getFileName(file)); const SourceLocation loc(tu, filename, 1, 1); return SourceRange(loc, loc); } // For definitions we can always find a declaration in current TU if (cursor.isDefinition()) return extractMatchingTokenRange(cursor.canonical(), tokenSpelling); if (!cursor.isDeclaration()) { // This is the symbol usage // We want to return definition cursor = cursor.referenced(); if (cursor.isNull() || !cursor.isDefinition()) { // We can't find definition in this TU return SourceRangeContainer(); } return extractMatchingTokenRange(cursor, tokenSpelling); } cursor = cursor.definition(); // If we are able to find a definition in current TU if (!cursor.isNull()) return extractMatchingTokenRange(cursor, tokenSpelling); return SourceRangeContainer(); }
SEXP R_clang_getIncludedFile(SEXP r_cursor) { SEXP r_ans = R_NilValue; CXCursor cursor = * GET_REF(r_cursor, CXCursor); CXFile ans; ans = clang_getIncludedFile(cursor); r_ans = R_createRef(ans, "CXFile") ; return(r_ans); }
void DumpThread::handleInclude(const Location &loc, const CXCursor &cursor) { CXFile includedFile = clang_getIncludedFile(cursor); if (includedFile) { CXStringScope fn = clang_getFileName(includedFile); const char *cstr = clang_getCString(fn); if (!cstr) { clang_disposeString(fn); return; } const Path p = Path::resolved(cstr); clang_disposeString(fn); const uint32_t fileId = Location::insertFile(p); Dep *&source = mDependencies[loc.fileId()]; if (!source) source = new Dep(loc.fileId()); Dep *&include = mDependencies[fileId]; if (!include) include = new Dep(fileId); source->include(include); } }
file cursor::includedFile() { return { clang_getIncludedFile(cur) }; }
// Desired functions: // Go to definition of variable/function // Go to declaration of function/class bool Tokenizer::findToken(eFindTokenTypes ft, size_t origOffset, std::string &fn, size_t &line) { CLangAutoLock lock(mCLangLock, __LINE__, this); CXCursor startCursor = getCursorAtOffset(mTransUnit, mSourceFile, origOffset); DUMP_PARSE("find:start cursor", startCursor); // Instantiating type - <class> <type> - CXCursor_TypeRef // Method declaration - <class> { <method>(); }; CXCursor_NoDeclFound // Method definition - <class>::<method>(){} - CXCursor_CXXMethod // Class/method usage - <class>.<method>() // Instance usage - method(){ int v = typename[4]; } - CXCursor_DeclStmt // clang_getCursorSemanticParent returns method // clang_getCursorDefinition returns invalid if(startCursor.kind == CXCursor_InclusionDirective) { CXFile file = clang_getIncludedFile(startCursor); if(file) { fn = getDisposedString(clang_getFileName(file)); line = 1; } else { /// @todo - need to get the full path. // CXStringDisposer cfn = clang_getCursorSpelling(cursor); // fn = cfn; line = 1; } } else { CXCursor cursor = startCursor; switch(ft) { case FT_FindDecl: cursor = clang_getCursorReferenced(startCursor); DUMP_PARSE("find:decl", cursor); break; case FT_FindDef: // If instantiating a type (CXCursor_TypeRef), this goes to the type declaration. cursor = clang_getCursorDefinition(startCursor); // Method call can return invalid file when the definition is not in this // translation unit. if(clang_getCursorKind(cursor) == CXCursor_InvalidFile) { DUMP_PARSE("find:def-invalid", cursor); cursor = clang_getCursorReferenced(startCursor); } DUMP_PARSE("find:def", cursor); // cursor = clang_getCursor(mTransUnit, clang_getCursorLocation(cursor)); // cursor = clang_getCursorDefinition(cursor); break; // cursor = clang_getCursorReferenced(cursor); // cursor = clang_getCanonicalCursor(cursor); // cursor = clang_getCursorSemanticParent(cursor); // cursor = clang_getCursorLexicalParent(cursor); } if(!clang_Cursor_isNull(cursor)) { CXSourceLocation loc = clang_getCursorLocation(cursor); CXFile file; unsigned int uline; clang_getFileLocation(loc, &file, &uline, nullptr, nullptr); if(file) { line = uline; fn = getDisposedString(clang_getFileName(file)); } } } return(fn.size() > 0); }