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; }
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; }
std::vector<diagnostic> translation_unit::diagnose() { // Get all the diagnostics uint32_t n = clang_getNumDiagnostics(mUnit); if (n == 0) return {}; std::vector<diagnostic> ret; ret.reserve(n); for (uint32_t i = 0; i < n; ++i) { CXFile file; uint32_t row = 0, col = 0, offset = 0; CXDiagnostic diag = clang_getDiagnostic(mUnit, i); CXSourceLocation loc = clang_getDiagnosticLocation(diag); clang_getExpansionLocation( loc, &file, &row, &col, &offset ); ret.push_back({ { cx2std(clang_getFileName(file)), row, col }, clang_getDiagnosticSeverity(diag), diagnostic_text(diag), diagnostic_summary(diag) }); clang_disposeDiagnostic(diag); } return ret; }
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; }
void CppHelperPluginView::inclusionVisitor( details::InclusionVisitorData* const data , CXFile file , CXSourceLocation* const stack , const unsigned stack_size ) { const auto header_name = clang::toString(clang::DCXString{clang_getFileName(file)}); // Obtain (or assign a new) an unique header ID const auto header_id = m_plugin->headersCache()[header_name]; auto included_from_id = DocumentInfo::IncludeLocationData::ROOT; clang::location loc; if (stack_size) // Is there anything on stack { loc = {stack[0]}; // NOTE Take filename from top of stack only! // Obtain a filename and its ID in headers cache /// \todo Normalize relative paths? included_from_id = m_plugin->headersCache()[loc.file().toLocalFile()]; } // NOTE Kate has zero based cursor position, so -1 is here data->m_di->addInclusionEntry({header_id, included_from_id, loc.line() - 1, loc.column() - 1}); auto* parent = static_cast<QTreeWidgetItem*>(nullptr); if (data->m_last_stack_size < stack_size) // Is current stack grew relative to the last call { assert("Sanity check!" && (stack_size - data->m_last_stack_size == 1)); // We have to add one more parent data->m_parents.push(data->m_last_added_item); parent = data->m_last_added_item; } else if (stack_size < data->m_last_stack_size) { // Stack size reduced since tha last call: remove our top for (auto i = data->m_last_stack_size; i > stack_size; --i) data->m_parents.pop(); assert("Stack expected to be non empty!" && !data->m_parents.empty()); parent = data->m_parents.top(); } else { assert("Sanity check!" && stack_size == data->m_last_stack_size); parent = data->m_parents.top(); } assert("Sanity check!" && parent); data->m_last_added_item = new QTreeWidgetItem(parent); data->m_last_added_item->setText(0, header_name); auto it = data->m_visited_ids.find(header_id); if (it != end(data->m_visited_ids)) { KColorScheme scheme(QPalette::Normal, KColorScheme::Selection); data->m_last_added_item->setForeground( 0 , scheme.foreground(KColorScheme::NeutralText).color() ); } else data->m_visited_ids.insert(header_id); data->m_last_stack_size = stack_size; }
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); }
/** * CXSourceLocation - 4-byte number, we need to call clang_getSpellingLocation * or clang_getExpansionLocation to retrieve line, column and file name. */ void Generator::getLocationParts(const CXSourceLocation &location, std::string &fileName, unsigned &line, unsigned &column) const { CXFile file; unsigned offset = 0; clang_getSpellingLocation(location, &file, &line, &column, &offset); (void)offset; fileName = stringize(clang_getFileName(file)); }
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; }
/** 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); }
void TranslationUnitUpdater::includeCallback(CXFile included_file, CXSourceLocation *, unsigned, CXClientData clientData) { ClangString includeFilePath(clang_getFileName(included_file)); TranslationUnitUpdater *updater = static_cast<TranslationUnitUpdater *>(clientData); updater->m_out.dependedOnFilePaths.insert(FilePath::fromNativeSeparators(includeFilePath)); }
std::ostream& operator<<( std::ostream& os, CXSourceLocation loc ) { CXFile cxfile; unsigned line, col, off; clang_getFileLocation( loc, &cxfile, &line, &col, &off ); auto filestr = clang_getFileName( cxfile ); os << clang_getCString( filestr ) << ":" << line << ":" << col; clang_disposeString( filestr ); return os; }
std::string libclang_vim::stringize_location(CXSourceLocation const& location) { CXFile file; unsigned int line, column, offset; clang_getSpellingLocation(location, &file, &line, &column, &offset); cxstring_ptr file_name = clang_getFileName(file); return "'line':" + std::to_string(line) + ",'column':" + std::to_string(column) + ",'offset':" + std::to_string(offset) + ',' + stringize_key_value("file", file_name); }
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) + "',"; }
/** Callback function called for each include in a translation unit */ static void inclusionVisitor(CXFile includedFile, CXSourceLocation* /*inclusionStack*/, unsigned /*includeLen*/, CXClientData clientData) { QDict<void> *fileDict = (QDict<void> *)clientData; CXString incFileName = clang_getFileName(includedFile); //printf("--- file %s includes %s\n",fileName,clang_getCString(incFileName)); fileDict->insert(clang_getCString(incFileName),(void*)0x8); clang_disposeString(incFileName); }
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; }
void TranslationUnit::includeCallback(CXFile included_file, CXSourceLocation * /*inclusion_stack*/, unsigned /*include_len*/, CXClientData clientData) { ClangString includeFilePath(clang_getFileName(included_file)); TranslationUnit *translationUnit = static_cast<TranslationUnit*>(clientData); translationUnit->d->dependedFilePaths.insert(includeFilePath); }
SEXP R_clang_getFileName(SEXP r_SFile) { SEXP r_ans = R_NilValue; CXFile SFile = GET_REF(r_SFile, CXFile); CXString ans; ans = clang_getFileName(SFile); //fprintf(stderr, "CXFile = %p, %s\n", SFile, clang_getCString(ans)); r_ans = CXStringToSEXP(ans) ; return(r_ans); }
void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack, unsigned includeStackLen, CXClientData data) { unsigned i; CXString fname; fname = clang_getFileName(includedFile); printf("file: %s\nincluded by:\n", clang_getCString(fname)); clang_disposeString(fname); for (i = 0; i < includeStackLen; ++i) { CXFile includingFile; unsigned line, column; clang_getInstantiationLocation(includeStack[i], &includingFile, &line, &column, 0); fname = clang_getFileName(includingFile); printf(" %s:%d:%d\n", clang_getCString(fname), line, column); clang_disposeString(fname); } printf("\n"); }
SourceLocation::SourceLocation(CXSourceLocation cxSourceLocation) { CXFile cxFile; clang_getFileLocation(cxSourceLocation, &cxFile, &line_, &column_, &offset_); filePath_ = ClangString(clang_getFileName(cxFile)); }
static void dumpDiagnostics(const CXTranslationUnit &tu) { std::cout << "(\n"; std::string file; for (unsigned i = 0, diagnosticCount = clang_getNumDiagnostics(tu); i < diagnosticCount; ++i) { CXDiagnostic diagnostic = clang_getDiagnostic(tu, i); CXSourceLocation location = clang_getDiagnosticLocation(diagnostic); unsigned line, column, offset; if (clang_equalLocations(location, clang_getNullLocation())) { file.clear(); line = 0; column = 0; offset = 0; } else { CXFile cxFile; // clang_getInstantiationLocation() has been marked deprecated and // is aimed to be replaced by clang_getExpansionLocation(). #if CINDEX_VERSION >= 6 clang_getExpansionLocation(location, &cxFile, &line, &column, &offset); #else clang_getInstantiationLocation(location, &cxFile, &line, &column, &offset); #endif file = cxStringToStd(clang_getFileName(cxFile)); } const char *severity = diagnosticSeverity(diagnostic); std::string message = cxStringToStd(clang_getDiagnosticSpelling(diagnostic)); std::cout << '(' << support::quoted(file) // << ' ' << line // << ' ' << column // << ' ' << offset // << ' ' << severity // << ' ' << support::quoted(message) // << ")\n"; clang_disposeDiagnostic(diagnostic); } std::cout << ")\n"; }
/** * CXSourceLocation - 4-байтное число, для извлечения имени файла и позиции в * файле потребуется один из вызовов clang_getSpellingLocation или * clang_getExpansionLocation */ void decodeLocation(const CXSourceLocation &location, std::string &fileName, unsigned &line, unsigned &column) { CXFile file; unsigned offset; clang_getSpellingLocation(location, &file, &line, &column, &offset); (void)offset; CXString cxName = clang_getFileName(file); fileName = clang_getCString(cxName); clang_disposeString(cxName); }
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; } }
location::location(const CXSourceLocation loc) { CXFile file; unsigned line; unsigned column; unsigned offset; clang_getSpellingLocation(loc, &file, &line, &column, &offset); if (file == nullptr) throw exception::invalid("No file has attached to a source location"); DCXString filename = {clang_getFileName(static_cast<CXFile>(file))}; m_file = clang_getCString(filename); assert("Sanity check" && m_file.isValid() && !m_file.isEmpty()); m_file.cleanPath(); m_line = line; m_column = column; m_offset = offset; }
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; }
static CXChildVisitResult visitTranslationUnit(CXCursor cursor, CXCursor parent, CXClientData client_data) { visitTranslationUnitData *data = static_cast<visitTranslationUnitData*>(client_data); CXSourceRange range = clang_getCursorExtent(cursor); CXSourceLocation startLoc = clang_getRangeStart(range); CXSourceLocation endLoc = clang_getRangeEnd(range); CXFile file; unsigned startOffset; unsigned endOffset; clang_getSpellingLocation(startLoc, &file, nullptr, nullptr, &startOffset); clang_getSpellingLocation(endLoc, &file, nullptr, nullptr, &endOffset); // Use the smallest cursor that surrounds the desired location. unsigned size = endOffset - startOffset; if(clangFilesEqual(file, data->mFile) && startOffset < data->mDesiredOffset && endOffset > data->mDesiredOffset) { if(size < data->mSize) { data->mCursor = cursor; data->mSize = size; fprintf(sLog.mFp, "GOOD:\n "); } } CXStringDisposer sp = clang_getCursorSpelling(cursor); CXStringDisposer kind = clang_getCursorKindSpelling(cursor.kind); std::string fn; if(file) { CXStringDisposer s = clang_getFileName(file); fn = s; } fprintf(sLog.mFp, "%s %s off %d size %d des offset %d file %s\n", kind.c_str(), sp.c_str(), startOffset, size, data->mDesiredOffset, fn.c_str()); DUMP_PARSE("visitTU", cursor); // clang_visitChildren(cursor, ::visitTranslationUnit, client_data); // return CXChildVisit_Continue; return CXChildVisit_Recurse; }
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); } }
std::string make_vimson_from_tokens(std::vector<Token> tokens) const { return "[" + std::accumulate(std::begin(tokens), std::end(tokens), std::string{}, [&](std::string const& acc, Token const& token){ auto const kind = clang_getTokenKind(token); auto const spell = owned(clang_getTokenSpelling(translation_unit, token)); auto const location = clang_getTokenLocation(translation_unit, token); CXFile file; unsigned int line, column, offset; clang_getFileLocation(location, &file, &line, &column, &offset); auto const source_name = owned(clang_getFileName(file)); return acc + "{'spell':'" + clang_getCString(*spell) + "','kind':'" + get_kind_spelling(kind) + "','file':'" + clang_getCString(*source_name) + "','line':" + std::to_string(line) + ",'column':" + std::to_string(column) + ",'offset':" + std::to_string(offset) + "},"; }) + "]"; }
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"); }