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