1 //===- CIndexer.h - Clang-C Source Indexing Library -------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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"
23 class CrashRecoveryContext
;
29 class MacroDefinitionRecord
;
36 bool DisplayDiagnostics
;
37 bool StorePreamblesInMemory
= false;
38 unsigned Options
; // CXGlobalOptFlags.
40 std::string ResourcesPath
;
41 std::shared_ptr
<PCHContainerOperations
> PCHContainerOps
;
43 std::string ToolchainPath
;
45 std::string PreambleStoragePath
;
46 std::string InvocationEmissionPath
;
49 CIndexer(std::shared_ptr
<PCHContainerOperations
> PCHContainerOps
=
50 std::make_shared
<PCHContainerOperations
>())
51 : OnlyLocalDecls(false), DisplayDiagnostics(false),
52 Options(CXGlobalOpt_None
), PCHContainerOps(std::move(PCHContainerOps
)) {
55 /// Whether we only want to see "local" declarations (that did not
56 /// come from a previous precompiled header). If false, we want to see all
58 bool getOnlyLocalDecls() const { return OnlyLocalDecls
; }
59 void setOnlyLocalDecls(bool Local
= true) { OnlyLocalDecls
= Local
; }
61 bool getDisplayDiagnostics() const { return DisplayDiagnostics
; }
62 void setDisplayDiagnostics(bool Display
= true) {
63 DisplayDiagnostics
= Display
;
66 std::shared_ptr
<PCHContainerOperations
> getPCHContainerOperations() const {
67 return PCHContainerOps
;
70 unsigned getCXGlobalOptFlags() const { return Options
; }
71 void setCXGlobalOptFlags(unsigned options
) { Options
= options
; }
73 bool isOptEnabled(CXGlobalOptFlags opt
) const {
77 /// Get the path of the clang resource files.
78 const std::string
&getClangResourcesPath();
80 StringRef
getClangToolchainPath();
82 void setStorePreamblesInMemory(bool StoreInMemory
) {
83 StorePreamblesInMemory
= StoreInMemory
;
85 bool getStorePreamblesInMemory() const { return StorePreamblesInMemory
; }
87 void setPreambleStoragePath(StringRef Str
) {
88 PreambleStoragePath
= Str
.str();
91 StringRef
getPreambleStoragePath() const { return PreambleStoragePath
; }
93 void setInvocationEmissionPath(StringRef Str
) {
94 InvocationEmissionPath
= std::string(Str
);
97 StringRef
getInvocationEmissionPath() const { return InvocationEmissionPath
; }
100 /// Logs information about a particular libclang operation like parsing to
101 /// a new file in the invocation emission path.
102 class LibclangInvocationReporter
{
104 enum class OperationKind
{ ParseOperation
, CompletionOperation
};
106 LibclangInvocationReporter(CIndexer
&Idx
, OperationKind Op
,
107 unsigned ParseOptions
,
108 llvm::ArrayRef
<const char *> Args
,
109 llvm::ArrayRef
<std::string
> InvocationArgs
,
110 llvm::ArrayRef
<CXUnsavedFile
> UnsavedFiles
);
111 ~LibclangInvocationReporter();
117 /// Return the current size to request for "safety".
118 unsigned GetSafetyThreadStackSize();
120 /// Set the current size to request for "safety" (or 0, if safety
121 /// threads should not be used).
122 void SetSafetyThreadStackSize(unsigned Value
);
124 /// Execution the given code "safely", using crash recovery or safety
125 /// threads when possible.
127 /// \return False if a crash was detected.
128 bool RunSafely(llvm::CrashRecoveryContext
&CRC
, llvm::function_ref
<void()> Fn
,
131 /// Set the thread priority to background.
132 /// FIXME: Move to llvm/Support.
133 void setThreadBackgroundPriority();
135 /// Print libclang's resource usage to standard error.
136 void PrintLibclangResourceUsage(CXTranslationUnit TU
);
139 void printDiagsToStderr(ASTUnit
*Unit
);
141 /// If \c MacroDefLoc points at a macro definition with \c II as
142 /// its name, this retrieves its MacroInfo.
143 MacroInfo
*getMacroInfo(const IdentifierInfo
&II
,
144 SourceLocation MacroDefLoc
, CXTranslationUnit TU
);
146 /// Retrieves the corresponding MacroInfo of a MacroDefinitionRecord.
147 const MacroInfo
*getMacroInfo(const MacroDefinitionRecord
*MacroDef
,
148 CXTranslationUnit TU
);
150 /// If \c Loc resides inside the definition of \c MI and it points at
151 /// an identifier that has ever been a macro name, this returns the latest
152 /// MacroDefinitionRecord for that name, otherwise it returns NULL.
153 MacroDefinitionRecord
*checkForMacroInMacroDefinition(const MacroInfo
*MI
,
155 CXTranslationUnit TU
);
157 /// If \c Tok resides inside the definition of \c MI and it points at
158 /// an identifier that has ever been a macro name, this returns the latest
159 /// MacroDefinitionRecord for that name, otherwise it returns NULL.
160 MacroDefinitionRecord
*checkForMacroInMacroDefinition(const MacroInfo
*MI
,
162 CXTranslationUnit TU
);