xref: /netbsd-src/external/apache2/llvm/dist/clang/tools/libclang/CIndexer.h (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //===- CIndexer.h - Clang-C Source Indexing Library -------------*- C++ -*-===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file defines CIndexer, a subclass of Indexer that provides extra
107330f729Sjoerg // functionality needed by the CIndex library.
117330f729Sjoerg //
127330f729Sjoerg //===----------------------------------------------------------------------===//
137330f729Sjoerg 
147330f729Sjoerg #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CINDEXER_H
157330f729Sjoerg #define LLVM_CLANG_TOOLS_LIBCLANG_CINDEXER_H
167330f729Sjoerg 
177330f729Sjoerg #include "clang-c/Index.h"
187330f729Sjoerg #include "clang/Frontend/PCHContainerOperations.h"
197330f729Sjoerg #include "llvm/ADT/STLExtras.h"
207330f729Sjoerg #include <utility>
217330f729Sjoerg 
227330f729Sjoerg namespace llvm {
237330f729Sjoerg   class CrashRecoveryContext;
247330f729Sjoerg }
257330f729Sjoerg 
267330f729Sjoerg namespace clang {
277330f729Sjoerg class ASTUnit;
287330f729Sjoerg class MacroInfo;
297330f729Sjoerg class MacroDefinitionRecord;
307330f729Sjoerg class SourceLocation;
317330f729Sjoerg class Token;
327330f729Sjoerg class IdentifierInfo;
337330f729Sjoerg 
347330f729Sjoerg class CIndexer {
357330f729Sjoerg   bool OnlyLocalDecls;
367330f729Sjoerg   bool DisplayDiagnostics;
377330f729Sjoerg   unsigned Options; // CXGlobalOptFlags.
387330f729Sjoerg 
397330f729Sjoerg   std::string ResourcesPath;
407330f729Sjoerg   std::shared_ptr<PCHContainerOperations> PCHContainerOps;
417330f729Sjoerg 
427330f729Sjoerg   std::string ToolchainPath;
437330f729Sjoerg 
447330f729Sjoerg   std::string InvocationEmissionPath;
457330f729Sjoerg 
467330f729Sjoerg public:
477330f729Sjoerg   CIndexer(std::shared_ptr<PCHContainerOperations> PCHContainerOps =
487330f729Sjoerg                std::make_shared<PCHContainerOperations>())
OnlyLocalDecls(false)497330f729Sjoerg       : OnlyLocalDecls(false), DisplayDiagnostics(false),
507330f729Sjoerg         Options(CXGlobalOpt_None), PCHContainerOps(std::move(PCHContainerOps)) {
517330f729Sjoerg   }
527330f729Sjoerg 
537330f729Sjoerg   /// Whether we only want to see "local" declarations (that did not
547330f729Sjoerg   /// come from a previous precompiled header). If false, we want to see all
557330f729Sjoerg   /// declarations.
getOnlyLocalDecls()567330f729Sjoerg   bool getOnlyLocalDecls() const { return OnlyLocalDecls; }
577330f729Sjoerg   void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; }
587330f729Sjoerg 
getDisplayDiagnostics()597330f729Sjoerg   bool getDisplayDiagnostics() const { return DisplayDiagnostics; }
607330f729Sjoerg   void setDisplayDiagnostics(bool Display = true) {
617330f729Sjoerg     DisplayDiagnostics = Display;
627330f729Sjoerg   }
637330f729Sjoerg 
getPCHContainerOperations()647330f729Sjoerg   std::shared_ptr<PCHContainerOperations> getPCHContainerOperations() const {
657330f729Sjoerg     return PCHContainerOps;
667330f729Sjoerg   }
677330f729Sjoerg 
getCXGlobalOptFlags()687330f729Sjoerg   unsigned getCXGlobalOptFlags() const { return Options; }
setCXGlobalOptFlags(unsigned options)697330f729Sjoerg   void setCXGlobalOptFlags(unsigned options) { Options = options; }
707330f729Sjoerg 
isOptEnabled(CXGlobalOptFlags opt)717330f729Sjoerg   bool isOptEnabled(CXGlobalOptFlags opt) const {
727330f729Sjoerg     return Options & opt;
737330f729Sjoerg   }
747330f729Sjoerg 
757330f729Sjoerg   /// Get the path of the clang resource files.
767330f729Sjoerg   const std::string &getClangResourcesPath();
777330f729Sjoerg 
787330f729Sjoerg   StringRef getClangToolchainPath();
797330f729Sjoerg 
setInvocationEmissionPath(StringRef Str)807330f729Sjoerg   void setInvocationEmissionPath(StringRef Str) {
81*e038c9c4Sjoerg     InvocationEmissionPath = std::string(Str);
827330f729Sjoerg   }
837330f729Sjoerg 
getInvocationEmissionPath()847330f729Sjoerg   StringRef getInvocationEmissionPath() const { return InvocationEmissionPath; }
857330f729Sjoerg };
867330f729Sjoerg 
877330f729Sjoerg /// Logs information about a particular libclang operation like parsing to
887330f729Sjoerg /// a new file in the invocation emission path.
897330f729Sjoerg class LibclangInvocationReporter {
907330f729Sjoerg public:
917330f729Sjoerg   enum class OperationKind { ParseOperation, CompletionOperation };
927330f729Sjoerg 
937330f729Sjoerg   LibclangInvocationReporter(CIndexer &Idx, OperationKind Op,
947330f729Sjoerg                              unsigned ParseOptions,
957330f729Sjoerg                              llvm::ArrayRef<const char *> Args,
967330f729Sjoerg                              llvm::ArrayRef<std::string> InvocationArgs,
977330f729Sjoerg                              llvm::ArrayRef<CXUnsavedFile> UnsavedFiles);
987330f729Sjoerg   ~LibclangInvocationReporter();
997330f729Sjoerg 
1007330f729Sjoerg private:
1017330f729Sjoerg   std::string File;
1027330f729Sjoerg };
1037330f729Sjoerg 
1047330f729Sjoerg   /// Return the current size to request for "safety".
1057330f729Sjoerg   unsigned GetSafetyThreadStackSize();
1067330f729Sjoerg 
1077330f729Sjoerg   /// Set the current size to request for "safety" (or 0, if safety
1087330f729Sjoerg   /// threads should not be used).
1097330f729Sjoerg   void SetSafetyThreadStackSize(unsigned Value);
1107330f729Sjoerg 
1117330f729Sjoerg   /// Execution the given code "safely", using crash recovery or safety
1127330f729Sjoerg   /// threads when possible.
1137330f729Sjoerg   ///
1147330f729Sjoerg   /// \return False if a crash was detected.
1157330f729Sjoerg   bool RunSafely(llvm::CrashRecoveryContext &CRC, llvm::function_ref<void()> Fn,
1167330f729Sjoerg                  unsigned Size = 0);
1177330f729Sjoerg 
1187330f729Sjoerg   /// Set the thread priority to background.
1197330f729Sjoerg   /// FIXME: Move to llvm/Support.
1207330f729Sjoerg   void setThreadBackgroundPriority();
1217330f729Sjoerg 
1227330f729Sjoerg   /// Print libclang's resource usage to standard error.
1237330f729Sjoerg   void PrintLibclangResourceUsage(CXTranslationUnit TU);
1247330f729Sjoerg 
1257330f729Sjoerg   namespace cxindex {
1267330f729Sjoerg     void printDiagsToStderr(ASTUnit *Unit);
1277330f729Sjoerg 
1287330f729Sjoerg     /// If \c MacroDefLoc points at a macro definition with \c II as
1297330f729Sjoerg     /// its name, this retrieves its MacroInfo.
1307330f729Sjoerg     MacroInfo *getMacroInfo(const IdentifierInfo &II,
1317330f729Sjoerg                             SourceLocation MacroDefLoc, CXTranslationUnit TU);
1327330f729Sjoerg 
1337330f729Sjoerg     /// Retrieves the corresponding MacroInfo of a MacroDefinitionRecord.
1347330f729Sjoerg     const MacroInfo *getMacroInfo(const MacroDefinitionRecord *MacroDef,
1357330f729Sjoerg                                   CXTranslationUnit TU);
1367330f729Sjoerg 
1377330f729Sjoerg     /// If \c Loc resides inside the definition of \c MI and it points at
1387330f729Sjoerg     /// an identifier that has ever been a macro name, this returns the latest
1397330f729Sjoerg     /// MacroDefinitionRecord for that name, otherwise it returns NULL.
1407330f729Sjoerg     MacroDefinitionRecord *checkForMacroInMacroDefinition(const MacroInfo *MI,
1417330f729Sjoerg                                                           SourceLocation Loc,
1427330f729Sjoerg                                                           CXTranslationUnit TU);
1437330f729Sjoerg 
1447330f729Sjoerg     /// If \c Tok resides inside the definition of \c MI and it points at
1457330f729Sjoerg     /// an identifier that has ever been a macro name, this returns the latest
1467330f729Sjoerg     /// MacroDefinitionRecord for that name, otherwise it returns NULL.
1477330f729Sjoerg     MacroDefinitionRecord *checkForMacroInMacroDefinition(const MacroInfo *MI,
1487330f729Sjoerg                                                           const Token &Tok,
1497330f729Sjoerg                                                           CXTranslationUnit TU);
1507330f729Sjoerg     }
1517330f729Sjoerg     }
1527330f729Sjoerg 
1537330f729Sjoerg #endif
154