| 1 | //===- CIndexer.h - Clang-C Source Indexing Library -------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines CIndexer, a subclass of Indexer that provides extra |
| 10 | // functionality needed by the CIndex library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CINDEXER_H |
| 15 | #define LLVM_CLANG_TOOLS_LIBCLANG_CINDEXER_H |
| 16 | |
| 17 | #include "clang-c/Index.h" |
| 18 | #include "clang/Frontend/PCHContainerOperations.h" |
| 19 | #include "llvm/ADT/STLExtras.h" |
| 20 | #include <utility> |
| 21 | |
| 22 | namespace llvm { |
| 23 | class CrashRecoveryContext; |
| 24 | } |
| 25 | |
| 26 | namespace clang { |
| 27 | class ASTUnit; |
| 28 | class MacroInfo; |
| 29 | class MacroDefinitionRecord; |
| 30 | class SourceLocation; |
| 31 | class Token; |
| 32 | class IdentifierInfo; |
| 33 | |
| 34 | class CIndexer { |
| 35 | bool OnlyLocalDecls; |
| 36 | bool DisplayDiagnostics; |
| 37 | bool StorePreamblesInMemory = false; |
| 38 | unsigned Options; // CXGlobalOptFlags. |
| 39 | |
| 40 | std::string ResourcesPath; |
| 41 | std::shared_ptr<PCHContainerOperations> PCHContainerOps; |
| 42 | |
| 43 | std::string ToolchainPath; |
| 44 | |
| 45 | std::string PreambleStoragePath; |
| 46 | std::string InvocationEmissionPath; |
| 47 | |
| 48 | public: |
| 49 | CIndexer(std::shared_ptr<PCHContainerOperations> PCHContainerOps = |
| 50 | std::make_shared<PCHContainerOperations>()) |
| 51 | : OnlyLocalDecls(false), DisplayDiagnostics(false), |
| 52 | Options(CXGlobalOpt_None), PCHContainerOps(std::move(PCHContainerOps)) { |
| 53 | } |
| 54 | |
| 55 | /// Whether we only want to see "local" declarations (that did not |
| 56 | /// come from a previous precompiled header). If false, we want to see all |
| 57 | /// declarations. |
| 58 | bool getOnlyLocalDecls() const { return OnlyLocalDecls; } |
| 59 | void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; } |
| 60 | |
| 61 | bool getDisplayDiagnostics() const { return DisplayDiagnostics; } |
| 62 | void setDisplayDiagnostics(bool Display = true) { |
| 63 | DisplayDiagnostics = Display; |
| 64 | } |
| 65 | |
| 66 | std::shared_ptr<PCHContainerOperations> getPCHContainerOperations() const { |
| 67 | return PCHContainerOps; |
| 68 | } |
| 69 | |
| 70 | unsigned getCXGlobalOptFlags() const { return Options; } |
| 71 | void setCXGlobalOptFlags(unsigned options) { Options = options; } |
| 72 | |
| 73 | bool isOptEnabled(CXGlobalOptFlags opt) const { |
| 74 | return Options & opt; |
| 75 | } |
| 76 | |
| 77 | /// Get the path of the clang resource files. |
| 78 | const std::string &getClangResourcesPath(); |
| 79 | |
| 80 | StringRef getClangToolchainPath(); |
| 81 | |
| 82 | void setStorePreamblesInMemory(bool StoreInMemory) { |
| 83 | StorePreamblesInMemory = StoreInMemory; |
| 84 | } |
| 85 | bool getStorePreamblesInMemory() const { return StorePreamblesInMemory; } |
| 86 | |
| 87 | void setPreambleStoragePath(StringRef Str) { |
| 88 | PreambleStoragePath = Str.str(); |
| 89 | } |
| 90 | |
| 91 | StringRef getPreambleStoragePath() const { return PreambleStoragePath; } |
| 92 | |
| 93 | void setInvocationEmissionPath(StringRef Str) { |
| 94 | InvocationEmissionPath = std::string(Str); |
| 95 | } |
| 96 | |
| 97 | StringRef getInvocationEmissionPath() const { return InvocationEmissionPath; } |
| 98 | }; |
| 99 | |
| 100 | /// Logs information about a particular libclang operation like parsing to |
| 101 | /// a new file in the invocation emission path. |
| 102 | class LibclangInvocationReporter { |
| 103 | public: |
| 104 | enum class OperationKind { ParseOperation, CompletionOperation }; |
| 105 | |
| 106 | LibclangInvocationReporter(CIndexer &Idx, OperationKind Op, |
| 107 | unsigned ParseOptions, |
| 108 | llvm::ArrayRef<const char *> Args, |
| 109 | llvm::ArrayRef<std::string> InvocationArgs, |
| 110 | llvm::ArrayRef<CXUnsavedFile> UnsavedFiles); |
| 111 | ~LibclangInvocationReporter(); |
| 112 | |
| 113 | private: |
| 114 | std::string File; |
| 115 | }; |
| 116 | |
| 117 | /// Return the current size to request for "safety". |
| 118 | unsigned GetSafetyThreadStackSize(); |
| 119 | |
| 120 | /// Set the current size to request for "safety" (or 0, if safety |
| 121 | /// threads should not be used). |
| 122 | void SetSafetyThreadStackSize(unsigned Value); |
| 123 | |
| 124 | /// Execution the given code "safely", using crash recovery or safety |
| 125 | /// threads when possible. |
| 126 | /// |
| 127 | /// \return False if a crash was detected. |
| 128 | bool RunSafely(llvm::CrashRecoveryContext &CRC, llvm::function_ref<void()> Fn, |
| 129 | unsigned Size = 0); |
| 130 | |
| 131 | /// Set the thread priority to background. |
| 132 | /// FIXME: Move to llvm/Support. |
| 133 | void setThreadBackgroundPriority(); |
| 134 | |
| 135 | /// Print libclang's resource usage to standard error. |
| 136 | void PrintLibclangResourceUsage(CXTranslationUnit TU); |
| 137 | |
| 138 | namespace cxindex { |
| 139 | void printDiagsToStderr(ASTUnit *Unit); |
| 140 | |
| 141 | /// If \c MacroDefLoc points at a macro definition with \c II as |
| 142 | /// its name, this retrieves its MacroInfo. |
| 143 | MacroInfo *getMacroInfo(const IdentifierInfo &II, |
| 144 | SourceLocation MacroDefLoc, CXTranslationUnit TU); |
| 145 | |
| 146 | /// Retrieves the corresponding MacroInfo of a MacroDefinitionRecord. |
| 147 | const MacroInfo *getMacroInfo(const MacroDefinitionRecord *MacroDef, |
| 148 | CXTranslationUnit TU); |
| 149 | |
| 150 | /// If \c Loc resides inside the definition of \c MI and it points at |
| 151 | /// an identifier that has ever been a macro name, this returns the latest |
| 152 | /// MacroDefinitionRecord for that name, otherwise it returns NULL. |
| 153 | MacroDefinitionRecord *checkForMacroInMacroDefinition(const MacroInfo *MI, |
| 154 | SourceLocation Loc, |
| 155 | CXTranslationUnit TU); |
| 156 | |
| 157 | /// If \c Tok resides inside the definition of \c MI and it points at |
| 158 | /// an identifier that has ever been a macro name, this returns the latest |
| 159 | /// MacroDefinitionRecord for that name, otherwise it returns NULL. |
| 160 | MacroDefinitionRecord *checkForMacroInMacroDefinition(const MacroInfo *MI, |
| 161 | const Token &Tok, |
| 162 | CXTranslationUnit TU); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | #endif |
| 167 | |