File indexing completed on 2024-05-12 04:39:19
0001 /* 0002 SPDX-FileCopyrightText: 2014 Kevin Funk <kfunk@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #ifndef CLANGUTILS_H 0008 #define CLANGUTILS_H 0009 0010 #include <util/path.h> 0011 0012 #include <clang-c/Index.h> 0013 0014 #include "clangprivateexport.h" 0015 #include "../duchain/unsavedfile.h" 0016 0017 #include <language/duchain/classfunctiondeclaration.h> 0018 0019 #include <functional> 0020 0021 namespace ClangUtils 0022 { 0023 /** 0024 * Finds the most specific CXCursor which applies to the specified line and column 0025 * in the given translation unit and file. 0026 * 0027 * @param line The 0-indexed line number at which to search. 0028 * @param column The 0-indexed column number at which to search. 0029 * @param unit The translation unit to examine. 0030 * @param file The file in the translation unit to examine. 0031 * 0032 * @return The cursor at the specified location 0033 */ 0034 CXCursor getCXCursor(int line, int column, const CXTranslationUnit& unit, const CXFile& file); 0035 0036 enum DefaultArgumentsMode 0037 { 0038 FixedSize, ///< The vector will have length equal to the number of arguments to the function 0039 /// and any arguments without a default parameter will be represented with an empty string. 0040 MinimumSize ///< The vector will have a length equal to the number of default values 0041 }; 0042 0043 /** 0044 * Given a cursor representing a function, returns a vector containing the string 0045 * representations of the default arguments of the function which are defined at 0046 * the occurrence of the cursor. Note that this is not necessarily all of the default 0047 * arguments of the function. 0048 * 0049 * @param cursor The cursor to examine 0050 * @return a vector of QStrings representing the default arguments, or an empty 0051 * vector if cursor does not represent a function 0052 */ 0053 QVector<QString> getDefaultArguments(CXCursor cursor, DefaultArgumentsMode mode = FixedSize); 0054 0055 /** 0056 * @return true when the cursor kind references a named scope. 0057 */ 0058 bool isScopeKind(CXCursorKind kind); 0059 0060 /** 0061 * @brief Retrieve a list of all unsaved files. 0062 * 0063 * @note Since this reads text from the editor widget, it must be called from the 0064 * GUI thread or with the foreground lock held. 0065 * 0066 * @return vector of all unsaved files and their current contents 0067 */ 0068 KDEVCLANGPRIVATE_EXPORT QVector<UnsavedFile> unsavedFiles(); 0069 0070 /** 0071 * Given a cursor and destination context, returns the string representing the 0072 * cursor's scope at its current location. 0073 * 0074 * @param cursor The cursor to examine 0075 * @param context The destination context from which the cursor should be referenced. 0076 * By default this will be set to the cursors lexical parent. 0077 * @return the cursor's scope as a string 0078 */ 0079 KDEVCLANGPRIVATE_EXPORT QString getScope(CXCursor cursor, CXCursor context = clang_getNullCursor()); 0080 0081 /** 0082 * Given a cursor representing some sort of function, returns its signature. The 0083 * effect of this function when passed a non-function cursor is undefined. 0084 * 0085 * @param cursor The cursor to work with 0086 * @param scope The scope of the cursor (e.g. "SomeNS::SomeClass") 0087 * @return A QString of the function's signature 0088 */ 0089 KDEVCLANGPRIVATE_EXPORT QString getCursorSignature(CXCursor cursor, const QString& scope, const QVector<QString>& defaultArgs = QVector<QString>()); 0090 0091 /** 0092 * Given a cursor representing the template argument list, return a 0093 * list of the argument types. 0094 * 0095 * @param cursor The cursor to work with 0096 * @return A QStringList of the template's arguments 0097 */ 0098 KDEVCLANGPRIVATE_EXPORT QStringList templateArgumentTypes(CXCursor cursor); 0099 0100 /** 0101 * Extract the raw contents of the range @p range 0102 * 0103 * @note This will return the exact textual representation of the code, 0104 * no whitespace stripped, etc. 0105 * 0106 * @param unit Translation unit this range is part of 0107 */ 0108 KDEVCLANGPRIVATE_EXPORT QString getRawContents(CXTranslationUnit unit, CXSourceRange range); 0109 0110 /** 0111 * @brief Return true if file @p file1 and file @p file2 are equal 0112 * 0113 * @see clang_File_isEqual 0114 */ 0115 inline bool isFileEqual(CXFile file1, CXFile file2) 0116 { 0117 return clang_File_isEqual(file1, file2); 0118 } 0119 0120 /** 0121 * @brief Return true if the cursor @p cursor refers to an explicitly deleted/defaulted function 0122 * such as the default constructor in "struct Foo { Foo() = delete; }" 0123 * 0124 * TODO: do we need isExplicitlyDefaulted() + isExplicitlyDeleted()? 0125 * Currently this is only used by the implements completion to hide deleted+defaulted functions so 0126 * we don't need to know the difference. 0127 */ 0128 bool isExplicitlyDefaultedOrDeleted(CXCursor cursor); 0129 0130 0131 /** 0132 * @brief run clang_visitChildren using given visitor function 0133 */ 0134 KDEVCLANGPRIVATE_EXPORT void visitChildren(CXCursor parent, std::function<CXChildVisitResult(CXCursor cursor, CXCursor parent)> visitor); 0135 0136 /** 0137 * Extract the range of the path-spec inside the include-directive in line @p line 0138 * 0139 * Example: line = "#include <vector>" => returns {0, 10, 0, 16} 0140 * 0141 * @param originalRange This is the range that the resulting range will be based on 0142 * 0143 * @return Range pointing to the path-spec of the include or invalid range if there is no #include directive on the line. 0144 */ 0145 KDEVCLANGPRIVATE_EXPORT KTextEditor::Range rangeForIncludePathSpec(const QString& line, const KTextEditor::Range& originalRange = KTextEditor::Range()); 0146 0147 /** 0148 * Returns special attributes (isFinal, isQtSlot, ...) given a @p cursor representing a CXXmethod 0149 */ 0150 KDevelop::ClassFunctionFlags specialAttributes(CXCursor cursor); 0151 0152 /** 0153 * @return the top most line in a file skipping any comment block 0154 */ 0155 unsigned int skipTopCommentBlock(CXTranslationUnit unit, CXFile file); 0156 } 0157 0158 #endif // CLANGUTILS_H