Clang Project

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