File indexing completed on 2024-05-12 04:39:11

0001 /*
0002     SPDX-FileCopyrightText: 2014 Olivier de Gaalon <olivier.jg@gmail.com>
0003     SPDX-FileCopyrightText: 2014 Milian Wolff <mail@milianw.de>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #ifndef CLANGHELPERS_H
0009 #define CLANGHELPERS_H
0010 
0011 #include "clangprivateexport.h"
0012 
0013 #include <language/duchain/duchainpointer.h>
0014 #include <language/duchain/topducontext.h>
0015 
0016 #include <clang-c/Index.h>
0017 
0018 #include <functional>
0019 
0020 class ParseSession;
0021 class ClangIndex;
0022 
0023 namespace KDevelop
0024 {
0025 class ModificationRevision;
0026 }
0027 
0028 struct Import
0029 {
0030     CXFile file;
0031     KDevelop::CursorInRevision location;
0032 };
0033 
0034 Q_DECLARE_TYPEINFO(Import, Q_MOVABLE_TYPE);
0035 
0036 using Imports = QMultiHash<CXFile, Import>;
0037 using IncludeFileContexts = QHash<CXFile, KDevelop::ReferencedTopDUContext>;
0038 using UnsavedRevisions = QHash<KDevelop::IndexedString, KDevelop::ModificationRevision>;
0039 
0040 namespace ClangHelpers {
0041 
0042 KDevelop::DeclarationPointer findDeclaration(CXSourceLocation cursor, const KDevelop::QualifiedIdentifier& id, const KDevelop::ReferencedTopDUContext& top);
0043 KDevelop::DeclarationPointer findDeclaration(CXCursor cursor, const IncludeFileContexts& includes);
0044 KDevelop::DeclarationPointer findDeclaration(CXType type, const IncludeFileContexts& includes);
0045 
0046 /**
0047  * Try to look up the first reachable forward declaration for type @a type
0048  *
0049  * @param context The context where this search is happening
0050  * @param cursor The location from which we're searching
0051  */
0052 KDevelop::DeclarationPointer findForwardDeclaration(CXType type, KDevelop::DUContext* context, CXCursor cursor);
0053 
0054 /**
0055  * Wrapper for @ref clang_Cursor_getSpellingNameRange which sometimes reports invalid ranges
0056  */
0057 KDevelop::RangeInRevision cursorSpellingNameRange(CXCursor cursor, const KDevelop::Identifier& id);
0058 
0059 /**
0060  * @returns all the Imports for each file in the @a tu
0061  */
0062 KDEVCLANGPRIVATE_EXPORT Imports tuImports(CXTranslationUnit tu);
0063 
0064 /**
0065  * Recursively builds a duchain with the specified @a features for the
0066  * @a file and each of its @a imports using the TU from @a session.
0067  * The resulting contexts are placed in @a includedFiles.
0068  * @returns the context created for @a file
0069  */
0070 KDEVCLANGPRIVATE_EXPORT KDevelop::ReferencedTopDUContext
0071 buildDUChain(CXFile file, const Imports& imports, const ParseSession& session,
0072              KDevelop::TopDUContext::Features features, IncludeFileContexts& includedFiles,
0073              const UnsavedRevisions& unsavedRevisions, const KDevelop::IndexedString& parseDocument,
0074              ClangIndex* index = nullptr, const std::function<bool()>& abortFunction = {});
0075 
0076 /**
0077  * @return List of possible header extensions used for definition/declaration fallback switching
0078  */
0079 QStringList headerExtensions();
0080 
0081 /**
0082  * @return List of possible source extensions used for definition/declaration fallback switching
0083  */
0084 QStringList sourceExtensions();
0085 
0086 /**
0087  * @return True if the given file @a path has the extension of a C++ source file
0088  */
0089 KDEVCLANGPRIVATE_EXPORT bool isSource(const QString& path);
0090 
0091 /**
0092  * @return True if the given file @a path has the extension of a C++ header file
0093  */
0094 KDEVCLANGPRIVATE_EXPORT bool isHeader(const QString& path);
0095 
0096 KDEVCLANGPRIVATE_EXPORT QString clangVersion();
0097 
0098 /**
0099  * @return The path containing Clang built includes (e.g. stddef.h, stdarg.h, cpuid.h)
0100  * The returned path is the env. var KDEV_CLANG_BUILTIN_DIR when set otherwise the path
0101  * to the headers used when kdev-clang was built, possibly updated for  upgrades to
0102  * the library (e.g. 7.0.0 -> 7.0.1).
0103  * Returns an empty string if none of the checked locations contain the file cpuid.h .
0104  *
0105  * Also see: https://clang.llvm.org/docs/FAQ.html
0106  */
0107 KDEVCLANGPRIVATE_EXPORT QString clangBuiltinIncludePath();
0108 
0109 /**
0110  * @return True if the given @a path is a valid clang builtin directory.
0111  */
0112 KDEVCLANGPRIVATE_EXPORT bool isValidClangBuiltingIncludePath(const QString& path);
0113 
0114 }
0115 
0116 #endif //CLANGHELPERS_H