1 //===- CIndexer.h - Clang-C Source Indexing Library -------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines CIndexer, a subclass of Indexer that provides extra 11 // functionality needed by the CIndex library. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CINDEXER_H 16 #define LLVM_CLANG_TOOLS_LIBCLANG_CINDEXER_H 17 18 #include "clang-c/Index.h" 19 #include "clang/Frontend/PCHContainerOperations.h" 20 #include "clang/Lex/ModuleLoader.h" 21 #include "llvm/ADT/StringRef.h" 22 #include "llvm/Support/Path.h" 23 #include <vector> 24 25 namespace llvm { 26 class CrashRecoveryContext; 27 } 28 29 namespace clang { 30 class ASTUnit; 31 class MacroInfo; 32 class MacroDefinitionRecord; 33 class SourceLocation; 34 class Token; 35 class IdentifierInfo; 36 37 class CIndexer { 38 bool OnlyLocalDecls; 39 bool DisplayDiagnostics; 40 unsigned Options; // CXGlobalOptFlags. 41 42 std::string ResourcesPath; 43 std::shared_ptr<PCHContainerOperations> PCHContainerOps; 44 45 public: 46 CIndexer(std::shared_ptr<PCHContainerOperations> PCHContainerOps = 47 std::make_shared<PCHContainerOperations>()) 48 : OnlyLocalDecls(false), DisplayDiagnostics(false), 49 Options(CXGlobalOpt_None), PCHContainerOps(PCHContainerOps) {} 50 51 /// \brief Whether we only want to see "local" declarations (that did not 52 /// come from a previous precompiled header). If false, we want to see all 53 /// declarations. 54 bool getOnlyLocalDecls() const { return OnlyLocalDecls; } 55 void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; } 56 57 bool getDisplayDiagnostics() const { return DisplayDiagnostics; } 58 void setDisplayDiagnostics(bool Display = true) { 59 DisplayDiagnostics = Display; 60 } 61 62 std::shared_ptr<PCHContainerOperations> getPCHContainerOperations() const { 63 return PCHContainerOps; 64 } 65 66 unsigned getCXGlobalOptFlags() const { return Options; } 67 void setCXGlobalOptFlags(unsigned options) { Options = options; } 68 69 bool isOptEnabled(CXGlobalOptFlags opt) const { 70 return Options & opt; 71 } 72 73 /// \brief Get the path of the clang resource files. 74 const std::string &getClangResourcesPath(); 75 }; 76 77 /// \brief Return the current size to request for "safety". 78 unsigned GetSafetyThreadStackSize(); 79 80 /// \brief Set the current size to request for "safety" (or 0, if safety 81 /// threads should not be used). 82 void SetSafetyThreadStackSize(unsigned Value); 83 84 /// \brief Execution the given code "safely", using crash recovery or safety 85 /// threads when possible. 86 /// 87 /// \return False if a crash was detected. 88 bool RunSafely(llvm::CrashRecoveryContext &CRC, llvm::function_ref<void()> Fn, 89 unsigned Size = 0); 90 91 /// \brief Set the thread priority to background. 92 /// FIXME: Move to llvm/Support. 93 void setThreadBackgroundPriority(); 94 95 /// \brief Print libclang's resource usage to standard error. 96 void PrintLibclangResourceUsage(CXTranslationUnit TU); 97 98 namespace cxindex { 99 void printDiagsToStderr(ASTUnit *Unit); 100 101 /// \brief If \c MacroDefLoc points at a macro definition with \c II as 102 /// its name, this retrieves its MacroInfo. 103 MacroInfo *getMacroInfo(const IdentifierInfo &II, 104 SourceLocation MacroDefLoc, CXTranslationUnit TU); 105 106 /// \brief Retrieves the corresponding MacroInfo of a MacroDefinitionRecord. 107 const MacroInfo *getMacroInfo(const MacroDefinitionRecord *MacroDef, 108 CXTranslationUnit TU); 109 110 /// \brief If \c Loc resides inside the definition of \c MI and it points at 111 /// an identifier that has ever been a macro name, this returns the latest 112 /// MacroDefinitionRecord for that name, otherwise it returns NULL. 113 MacroDefinitionRecord *checkForMacroInMacroDefinition(const MacroInfo *MI, 114 SourceLocation Loc, 115 CXTranslationUnit TU); 116 117 /// \brief If \c Tok resides inside the definition of \c MI and it points at 118 /// an identifier that has ever been a macro name, this returns the latest 119 /// MacroDefinitionRecord for that name, otherwise it returns NULL. 120 MacroDefinitionRecord *checkForMacroInMacroDefinition(const MacroInfo *MI, 121 const Token &Tok, 122 CXTranslationUnit TU); 123 } 124 } 125 126 #endif 127