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 unsigned Options; // CXGlobalOptFlags. 38 39 std::string ResourcesPath; 40 std::shared_ptr<PCHContainerOperations> PCHContainerOps; 41 42 std::string ToolchainPath; 43 44 std::string InvocationEmissionPath; 45 46 public: 47 CIndexer(std::shared_ptr<PCHContainerOperations> PCHContainerOps = 48 std::make_shared<PCHContainerOperations>()) OnlyLocalDecls(false)49 : OnlyLocalDecls(false), DisplayDiagnostics(false), 50 Options(CXGlobalOpt_None), PCHContainerOps(std::move(PCHContainerOps)) { 51 } 52 53 /// Whether we only want to see "local" declarations (that did not 54 /// come from a previous precompiled header). If false, we want to see all 55 /// declarations. getOnlyLocalDecls()56 bool getOnlyLocalDecls() const { return OnlyLocalDecls; } 57 void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; } 58 getDisplayDiagnostics()59 bool getDisplayDiagnostics() const { return DisplayDiagnostics; } 60 void setDisplayDiagnostics(bool Display = true) { 61 DisplayDiagnostics = Display; 62 } 63 getPCHContainerOperations()64 std::shared_ptr<PCHContainerOperations> getPCHContainerOperations() const { 65 return PCHContainerOps; 66 } 67 getCXGlobalOptFlags()68 unsigned getCXGlobalOptFlags() const { return Options; } setCXGlobalOptFlags(unsigned options)69 void setCXGlobalOptFlags(unsigned options) { Options = options; } 70 isOptEnabled(CXGlobalOptFlags opt)71 bool isOptEnabled(CXGlobalOptFlags opt) const { 72 return Options & opt; 73 } 74 75 /// Get the path of the clang resource files. 76 const std::string &getClangResourcesPath(); 77 78 StringRef getClangToolchainPath(); 79 setInvocationEmissionPath(StringRef Str)80 void setInvocationEmissionPath(StringRef Str) { 81 InvocationEmissionPath = std::string(Str); 82 } 83 getInvocationEmissionPath()84 StringRef getInvocationEmissionPath() const { return InvocationEmissionPath; } 85 }; 86 87 /// Logs information about a particular libclang operation like parsing to 88 /// a new file in the invocation emission path. 89 class LibclangInvocationReporter { 90 public: 91 enum class OperationKind { ParseOperation, CompletionOperation }; 92 93 LibclangInvocationReporter(CIndexer &Idx, OperationKind Op, 94 unsigned ParseOptions, 95 llvm::ArrayRef<const char *> Args, 96 llvm::ArrayRef<std::string> InvocationArgs, 97 llvm::ArrayRef<CXUnsavedFile> UnsavedFiles); 98 ~LibclangInvocationReporter(); 99 100 private: 101 std::string File; 102 }; 103 104 /// Return the current size to request for "safety". 105 unsigned GetSafetyThreadStackSize(); 106 107 /// Set the current size to request for "safety" (or 0, if safety 108 /// threads should not be used). 109 void SetSafetyThreadStackSize(unsigned Value); 110 111 /// Execution the given code "safely", using crash recovery or safety 112 /// threads when possible. 113 /// 114 /// \return False if a crash was detected. 115 bool RunSafely(llvm::CrashRecoveryContext &CRC, llvm::function_ref<void()> Fn, 116 unsigned Size = 0); 117 118 /// Set the thread priority to background. 119 /// FIXME: Move to llvm/Support. 120 void setThreadBackgroundPriority(); 121 122 /// Print libclang's resource usage to standard error. 123 void PrintLibclangResourceUsage(CXTranslationUnit TU); 124 125 namespace cxindex { 126 void printDiagsToStderr(ASTUnit *Unit); 127 128 /// If \c MacroDefLoc points at a macro definition with \c II as 129 /// its name, this retrieves its MacroInfo. 130 MacroInfo *getMacroInfo(const IdentifierInfo &II, 131 SourceLocation MacroDefLoc, CXTranslationUnit TU); 132 133 /// Retrieves the corresponding MacroInfo of a MacroDefinitionRecord. 134 const MacroInfo *getMacroInfo(const MacroDefinitionRecord *MacroDef, 135 CXTranslationUnit TU); 136 137 /// If \c Loc resides inside the definition of \c MI and it points at 138 /// an identifier that has ever been a macro name, this returns the latest 139 /// MacroDefinitionRecord for that name, otherwise it returns NULL. 140 MacroDefinitionRecord *checkForMacroInMacroDefinition(const MacroInfo *MI, 141 SourceLocation Loc, 142 CXTranslationUnit TU); 143 144 /// If \c Tok resides inside the definition of \c MI and it points at 145 /// an identifier that has ever been a macro name, this returns the latest 146 /// MacroDefinitionRecord for that name, otherwise it returns NULL. 147 MacroDefinitionRecord *checkForMacroInMacroDefinition(const MacroInfo *MI, 148 const Token &Tok, 149 CXTranslationUnit TU); 150 } 151 } 152 153 #endif 154