Home | History | Annotate | Download | only in libclang
      1 //===- CIndexer.h - Clang-C Source Indexing Library -------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file defines CIndexer, a subclass of Indexer that provides extra
     11 // functionality needed by the CIndex library.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CINDEXER_H
     16 #define LLVM_CLANG_TOOLS_LIBCLANG_CINDEXER_H
     17 
     18 #include "clang-c/Index.h"
     19 #include "clang/Frontend/PCHContainerOperations.h"
     20 #include "clang/Lex/ModuleLoader.h"
     21 #include "llvm/ADT/StringRef.h"
     22 #include "llvm/Support/Path.h"
     23 #include <utility>
     24 #include <vector>
     25 
     26 namespace llvm {
     27   class CrashRecoveryContext;
     28 }
     29 
     30 namespace clang {
     31 class ASTUnit;
     32 class MacroInfo;
     33 class MacroDefinitionRecord;
     34 class SourceLocation;
     35 class Token;
     36 class IdentifierInfo;
     37 
     38 class CIndexer {
     39   bool OnlyLocalDecls;
     40   bool DisplayDiagnostics;
     41   unsigned Options; // CXGlobalOptFlags.
     42 
     43   std::string ResourcesPath;
     44   std::shared_ptr<PCHContainerOperations> PCHContainerOps;
     45 
     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)) {
     51   }
     52 
     53   /// \brief 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; }
     58 
     59   bool getDisplayDiagnostics() const { return DisplayDiagnostics; }
     60   void setDisplayDiagnostics(bool Display = true) {
     61     DisplayDiagnostics = Display;
     62   }
     63 
     64   std::shared_ptr<PCHContainerOperations> getPCHContainerOperations() const {
     65     return PCHContainerOps;
     66   }
     67 
     68   unsigned getCXGlobalOptFlags() const { return Options; }
     69   void setCXGlobalOptFlags(unsigned options) { Options = options; }
     70 
     71   bool isOptEnabled(CXGlobalOptFlags opt) const {
     72     return Options & opt;
     73   }
     74 
     75   /// \brief Get the path of the clang resource files.
     76   const std::string &getClangResourcesPath();
     77 };
     78 
     79   /// \brief Return the current size to request for "safety".
     80   unsigned GetSafetyThreadStackSize();
     81 
     82   /// \brief Set the current size to request for "safety" (or 0, if safety
     83   /// threads should not be used).
     84   void SetSafetyThreadStackSize(unsigned Value);
     85 
     86   /// \brief Execution the given code "safely", using crash recovery or safety
     87   /// threads when possible.
     88   ///
     89   /// \return False if a crash was detected.
     90   bool RunSafely(llvm::CrashRecoveryContext &CRC, llvm::function_ref<void()> Fn,
     91                  unsigned Size = 0);
     92 
     93   /// \brief Set the thread priority to background.
     94   /// FIXME: Move to llvm/Support.
     95   void setThreadBackgroundPriority();
     96 
     97   /// \brief Print libclang's resource usage to standard error.
     98   void PrintLibclangResourceUsage(CXTranslationUnit TU);
     99 
    100   namespace cxindex {
    101     void printDiagsToStderr(ASTUnit *Unit);
    102 
    103     /// \brief If \c MacroDefLoc points at a macro definition with \c II as
    104     /// its name, this retrieves its MacroInfo.
    105     MacroInfo *getMacroInfo(const IdentifierInfo &II,
    106                             SourceLocation MacroDefLoc, CXTranslationUnit TU);
    107 
    108     /// \brief Retrieves the corresponding MacroInfo of a MacroDefinitionRecord.
    109     const MacroInfo *getMacroInfo(const MacroDefinitionRecord *MacroDef,
    110                                   CXTranslationUnit TU);
    111 
    112     /// \brief If \c Loc resides inside the definition of \c MI and it points at
    113     /// an identifier that has ever been a macro name, this returns the latest
    114     /// MacroDefinitionRecord for that name, otherwise it returns NULL.
    115     MacroDefinitionRecord *checkForMacroInMacroDefinition(const MacroInfo *MI,
    116                                                           SourceLocation Loc,
    117                                                           CXTranslationUnit TU);
    118 
    119     /// \brief If \c Tok resides inside the definition of \c MI and it points at
    120     /// an identifier that has ever been a macro name, this returns the latest
    121     /// MacroDefinitionRecord for that name, otherwise it returns NULL.
    122     MacroDefinitionRecord *checkForMacroInMacroDefinition(const MacroInfo *MI,
    123                                                           const Token &Tok,
    124                                                           CXTranslationUnit TU);
    125     }
    126     }
    127 
    128 #endif
    129