clang API Documentation

PTHManager.h
Go to the documentation of this file.
00001 //===--- PTHManager.h - Manager object for PTH processing -------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the PTHManager interface.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CLANG_LEX_PTHMANAGER_H
00015 #define LLVM_CLANG_LEX_PTHMANAGER_H
00016 
00017 #include "clang/Basic/Diagnostic.h"
00018 #include "clang/Basic/IdentifierTable.h"
00019 #include "clang/Basic/LangOptions.h"
00020 #include "clang/Lex/PTHLexer.h"
00021 #include "llvm/ADT/DenseMap.h"
00022 #include "llvm/Support/Allocator.h"
00023 #include "llvm/Support/OnDiskHashTable.h"
00024 #include <string>
00025 
00026 namespace llvm {
00027   class MemoryBuffer;
00028 }
00029 
00030 namespace clang {
00031 
00032 class FileEntry;
00033 class PTHLexer;
00034 class DiagnosticsEngine;
00035 class FileSystemStatCache;
00036 
00037 class PTHManager : public IdentifierInfoLookup {
00038   friend class PTHLexer;
00039 
00040   friend class PTHStatCache;
00041 
00042   class PTHStringLookupTrait;
00043   class PTHFileLookupTrait;
00044   typedef llvm::OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
00045   typedef llvm::OnDiskChainedHashTable<PTHFileLookupTrait> PTHFileLookup;
00046 
00047   /// The memory mapped PTH file.
00048   std::unique_ptr<const llvm::MemoryBuffer> Buf;
00049 
00050   /// Alloc - Allocator used for IdentifierInfo objects.
00051   llvm::BumpPtrAllocator Alloc;
00052 
00053   /// IdMap - A lazily generated cache mapping from persistent identifiers to
00054   ///  IdentifierInfo*.
00055   std::unique_ptr<IdentifierInfo *[], llvm::FreeDeleter> PerIDCache;
00056 
00057   /// FileLookup - Abstract data structure used for mapping between files
00058   ///  and token data in the PTH file.
00059   std::unique_ptr<PTHFileLookup> FileLookup;
00060 
00061   /// IdDataTable - Array representing the mapping from persistent IDs to the
00062   ///  data offset within the PTH file containing the information to
00063   ///  reconsitute an IdentifierInfo.
00064   const unsigned char* const IdDataTable;
00065 
00066   /// SortedIdTable - Abstract data structure mapping from strings to
00067   ///  persistent IDs.  This is used by get().
00068   std::unique_ptr<PTHStringIdLookup> StringIdLookup;
00069 
00070   /// NumIds - The number of identifiers in the PTH file.
00071   const unsigned NumIds;
00072 
00073   /// PP - The Preprocessor object that will use this PTHManager to create
00074   ///  PTHLexer objects.
00075   Preprocessor* PP;
00076 
00077   /// SpellingBase - The base offset within the PTH memory buffer that
00078   ///  contains the cached spellings for literals.
00079   const unsigned char* const SpellingBase;
00080 
00081   /// OriginalSourceFile - A null-terminated C-string that specifies the name
00082   ///  if the file (if any) that was to used to generate the PTH cache.
00083   const char* OriginalSourceFile;
00084 
00085   /// This constructor is intended to only be called by the static 'Create'
00086   /// method.
00087   PTHManager(std::unique_ptr<const llvm::MemoryBuffer> buf,
00088              std::unique_ptr<PTHFileLookup> fileLookup,
00089              const unsigned char *idDataTable,
00090              std::unique_ptr<IdentifierInfo *[], llvm::FreeDeleter> perIDCache,
00091              std::unique_ptr<PTHStringIdLookup> stringIdLookup, unsigned numIds,
00092              const unsigned char *spellingBase, const char *originalSourceFile);
00093 
00094   PTHManager(const PTHManager &) LLVM_DELETED_FUNCTION;
00095   void operator=(const PTHManager &) LLVM_DELETED_FUNCTION;
00096 
00097   /// getSpellingAtPTHOffset - Used by PTHLexer classes to get the cached
00098   ///  spelling for a token.
00099   unsigned getSpellingAtPTHOffset(unsigned PTHOffset, const char*& Buffer);
00100 
00101   /// GetIdentifierInfo - Used to reconstruct IdentifierInfo objects from the
00102   ///  PTH file.
00103   inline IdentifierInfo* GetIdentifierInfo(unsigned PersistentID) {
00104     // Check if the IdentifierInfo has already been resolved.
00105     if (IdentifierInfo* II = PerIDCache[PersistentID])
00106       return II;
00107     return LazilyCreateIdentifierInfo(PersistentID);
00108   }
00109   IdentifierInfo* LazilyCreateIdentifierInfo(unsigned PersistentID);
00110 
00111 public:
00112   // The current PTH version.
00113   enum { Version = 10 };
00114 
00115   ~PTHManager();
00116 
00117   /// getOriginalSourceFile - Return the full path to the original header
00118   ///  file name that was used to generate the PTH cache.
00119   const char* getOriginalSourceFile() const {
00120     return OriginalSourceFile;
00121   }
00122 
00123   /// get - Return the identifier token info for the specified named identifier.
00124   ///  Unlike the version in IdentifierTable, this returns a pointer instead
00125   ///  of a reference.  If the pointer is NULL then the IdentifierInfo cannot
00126   ///  be found.
00127   IdentifierInfo *get(StringRef Name) override;
00128 
00129   /// Create - This method creates PTHManager objects.  The 'file' argument
00130   ///  is the name of the PTH file.  This method returns NULL upon failure.
00131   static PTHManager *Create(const std::string& file, DiagnosticsEngine &Diags);
00132 
00133   void setPreprocessor(Preprocessor *pp) { PP = pp; }
00134 
00135   /// CreateLexer - Return a PTHLexer that "lexes" the cached tokens for the
00136   ///  specified file.  This method returns NULL if no cached tokens exist.
00137   ///  It is the responsibility of the caller to 'delete' the returned object.
00138   PTHLexer *CreateLexer(FileID FID);
00139 
00140   /// createStatCache - Returns a FileSystemStatCache object for use with
00141   ///  FileManager objects.  These objects use the PTH data to speed up
00142   ///  calls to stat by memoizing their results from when the PTH file
00143   ///  was generated.
00144   std::unique_ptr<FileSystemStatCache> createStatCache();
00145 };
00146 
00147 }  // end namespace clang
00148 
00149 #endif