1 //===--- PTHManager.h - Manager object for PTH processing -------*- 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 the PTHManager interface. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CLANG_LEX_PTHMANAGER_H 15 #define LLVM_CLANG_LEX_PTHMANAGER_H 16 17 #include "clang/Basic/Diagnostic.h" 18 #include "clang/Basic/IdentifierTable.h" 19 #include "clang/Basic/LangOptions.h" 20 #include "clang/Lex/PTHLexer.h" 21 #include "llvm/ADT/DenseMap.h" 22 #include "llvm/ADT/STLExtras.h" 23 #include "llvm/Support/Allocator.h" 24 #include "llvm/Support/OnDiskHashTable.h" 25 #include <string> 26 27 namespace llvm { 28 class MemoryBuffer; 29 } 30 31 namespace clang { 32 33 class FileEntry; 34 class PTHLexer; 35 class DiagnosticsEngine; 36 class FileSystemStatCache; 37 38 class PTHManager : public IdentifierInfoLookup { 39 friend class PTHLexer; 40 41 friend class PTHStatCache; 42 43 class PTHStringLookupTrait; 44 class PTHFileLookupTrait; 45 typedef llvm::OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup; 46 typedef llvm::OnDiskChainedHashTable<PTHFileLookupTrait> PTHFileLookup; 47 48 /// The memory mapped PTH file. 49 std::unique_ptr<const llvm::MemoryBuffer> Buf; 50 51 /// Alloc - Allocator used for IdentifierInfo objects. 52 llvm::BumpPtrAllocator Alloc; 53 54 /// IdMap - A lazily generated cache mapping from persistent identifiers to 55 /// IdentifierInfo*. 56 std::unique_ptr<IdentifierInfo *[], llvm::FreeDeleter> PerIDCache; 57 58 /// FileLookup - Abstract data structure used for mapping between files 59 /// and token data in the PTH file. 60 std::unique_ptr<PTHFileLookup> FileLookup; 61 62 /// IdDataTable - Array representing the mapping from persistent IDs to the 63 /// data offset within the PTH file containing the information to 64 /// reconsitute an IdentifierInfo. 65 const unsigned char* const IdDataTable; 66 67 /// SortedIdTable - Abstract data structure mapping from strings to 68 /// persistent IDs. This is used by get(). 69 std::unique_ptr<PTHStringIdLookup> StringIdLookup; 70 71 /// NumIds - The number of identifiers in the PTH file. 72 const unsigned NumIds; 73 74 /// PP - The Preprocessor object that will use this PTHManager to create 75 /// PTHLexer objects. 76 Preprocessor* PP; 77 78 /// SpellingBase - The base offset within the PTH memory buffer that 79 /// contains the cached spellings for literals. 80 const unsigned char* const SpellingBase; 81 82 /// OriginalSourceFile - A null-terminated C-string that specifies the name 83 /// if the file (if any) that was to used to generate the PTH cache. 84 const char* OriginalSourceFile; 85 86 /// This constructor is intended to only be called by the static 'Create' 87 /// method. 88 PTHManager(std::unique_ptr<const llvm::MemoryBuffer> buf, 89 std::unique_ptr<PTHFileLookup> fileLookup, 90 const unsigned char *idDataTable, 91 std::unique_ptr<IdentifierInfo *[], llvm::FreeDeleter> perIDCache, 92 std::unique_ptr<PTHStringIdLookup> stringIdLookup, unsigned numIds, 93 const unsigned char *spellingBase, const char *originalSourceFile); 94 95 PTHManager(const PTHManager &) = delete; 96 void operator=(const PTHManager &) = delete; 97 98 /// getSpellingAtPTHOffset - Used by PTHLexer classes to get the cached 99 /// spelling for a token. 100 unsigned getSpellingAtPTHOffset(unsigned PTHOffset, const char*& Buffer); 101 102 /// GetIdentifierInfo - Used to reconstruct IdentifierInfo objects from the 103 /// PTH file. GetIdentifierInfo(unsigned PersistentID)104 inline IdentifierInfo* GetIdentifierInfo(unsigned PersistentID) { 105 // Check if the IdentifierInfo has already been resolved. 106 if (IdentifierInfo* II = PerIDCache[PersistentID]) 107 return II; 108 return LazilyCreateIdentifierInfo(PersistentID); 109 } 110 IdentifierInfo* LazilyCreateIdentifierInfo(unsigned PersistentID); 111 112 public: 113 // The current PTH version. 114 enum { Version = 10 }; 115 116 ~PTHManager() override; 117 118 /// getOriginalSourceFile - Return the full path to the original header 119 /// file name that was used to generate the PTH cache. getOriginalSourceFile()120 const char* getOriginalSourceFile() const { 121 return OriginalSourceFile; 122 } 123 124 /// get - Return the identifier token info for the specified named identifier. 125 /// Unlike the version in IdentifierTable, this returns a pointer instead 126 /// of a reference. If the pointer is NULL then the IdentifierInfo cannot 127 /// be found. 128 IdentifierInfo *get(StringRef Name) override; 129 130 /// Create - This method creates PTHManager objects. The 'file' argument 131 /// is the name of the PTH file. This method returns NULL upon failure. 132 static PTHManager *Create(StringRef file, DiagnosticsEngine &Diags); 133 setPreprocessor(Preprocessor * pp)134 void setPreprocessor(Preprocessor *pp) { PP = pp; } 135 136 /// CreateLexer - Return a PTHLexer that "lexes" the cached tokens for the 137 /// specified file. This method returns NULL if no cached tokens exist. 138 /// It is the responsibility of the caller to 'delete' the returned object. 139 PTHLexer *CreateLexer(FileID FID); 140 141 /// createStatCache - Returns a FileSystemStatCache object for use with 142 /// FileManager objects. These objects use the PTH data to speed up 143 /// calls to stat by memoizing their results from when the PTH file 144 /// was generated. 145 std::unique_ptr<FileSystemStatCache> createStatCache(); 146 }; 147 148 } // end namespace clang 149 150 #endif 151