CIndexer.h 5.4 KB

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