[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / tools / libclang / CIndexer.h
blobde15cc34b5e51a67c6ad51077d38e5e1a01873c0
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.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CINDEXER_H
15 #define LLVM_CLANG_TOOLS_LIBCLANG_CINDEXER_H
17 #include "clang-c/Index.h"
18 #include "clang/Frontend/PCHContainerOperations.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include <utility>
22 namespace llvm {
23 class CrashRecoveryContext;
26 namespace clang {
27 class ASTUnit;
28 class MacroInfo;
29 class MacroDefinitionRecord;
30 class SourceLocation;
31 class Token;
32 class IdentifierInfo;
34 class CIndexer {
35 bool OnlyLocalDecls;
36 bool DisplayDiagnostics;
37 unsigned Options; // CXGlobalOptFlags.
39 std::string ResourcesPath;
40 std::shared_ptr<PCHContainerOperations> PCHContainerOps;
42 std::string ToolchainPath;
44 std::string InvocationEmissionPath;
46 public:
47 CIndexer(std::shared_ptr<PCHContainerOperations> PCHContainerOps =
48 std::make_shared<PCHContainerOperations>())
49 : OnlyLocalDecls(false), DisplayDiagnostics(false),
50 Options(CXGlobalOpt_None), PCHContainerOps(std::move(PCHContainerOps)) {
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.
56 bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
57 void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; }
59 bool getDisplayDiagnostics() const { return DisplayDiagnostics; }
60 void setDisplayDiagnostics(bool Display = true) {
61 DisplayDiagnostics = Display;
64 std::shared_ptr<PCHContainerOperations> getPCHContainerOperations() const {
65 return PCHContainerOps;
68 unsigned getCXGlobalOptFlags() const { return Options; }
69 void setCXGlobalOptFlags(unsigned options) { Options = options; }
71 bool isOptEnabled(CXGlobalOptFlags opt) const {
72 return Options & opt;
75 /// Get the path of the clang resource files.
76 const std::string &getClangResourcesPath();
78 StringRef getClangToolchainPath();
80 void setInvocationEmissionPath(StringRef Str) {
81 InvocationEmissionPath = std::string(Str);
84 StringRef getInvocationEmissionPath() const { return InvocationEmissionPath; }
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 };
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();
100 private:
101 std::string File;
104 /// Return the current size to request for "safety".
105 unsigned GetSafetyThreadStackSize();
107 /// Set the current size to request for "safety" (or 0, if safety
108 /// threads should not be used).
109 void SetSafetyThreadStackSize(unsigned Value);
111 /// Execution the given code "safely", using crash recovery or safety
112 /// threads when possible.
114 /// \return False if a crash was detected.
115 bool RunSafely(llvm::CrashRecoveryContext &CRC, llvm::function_ref<void()> Fn,
116 unsigned Size = 0);
118 /// Set the thread priority to background.
119 /// FIXME: Move to llvm/Support.
120 void setThreadBackgroundPriority();
122 /// Print libclang's resource usage to standard error.
123 void PrintLibclangResourceUsage(CXTranslationUnit TU);
125 namespace cxindex {
126 void printDiagsToStderr(ASTUnit *Unit);
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);
133 /// Retrieves the corresponding MacroInfo of a MacroDefinitionRecord.
134 const MacroInfo *getMacroInfo(const MacroDefinitionRecord *MacroDef,
135 CXTranslationUnit TU);
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);
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);
153 #endif