1 //===- PTHManager.h - Manager object for PTH processing ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PTHManager interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LEX_PTHMANAGER_H
15 #define LLVM_CLANG_LEX_PTHMANAGER_H
17 #include "clang/Basic/IdentifierTable.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/Allocator.h"
22 #include "llvm/Support/OnDiskHashTable.h"
33 class DiagnosticsEngine;
34 class FileSystemStatCache;
38 class PTHManager : public IdentifierInfoLookup {
39 friend class PTHLexer;
40 friend class PTHStatCache;
42 class PTHFileLookupTrait;
43 class PTHStringLookupTrait;
45 using PTHStringIdLookup = llvm::OnDiskChainedHashTable<PTHStringLookupTrait>;
46 using PTHFileLookup = llvm::OnDiskChainedHashTable<PTHFileLookupTrait>;
48 /// The memory mapped PTH file.
49 std::unique_ptr<const llvm::MemoryBuffer> Buf;
51 /// Alloc - Allocator used for IdentifierInfo objects.
52 llvm::BumpPtrAllocator Alloc;
54 /// IdMap - A lazily generated cache mapping from persistent identifiers to
56 std::unique_ptr<IdentifierInfo *[], llvm::FreeDeleter> PerIDCache;
58 /// FileLookup - Abstract data structure used for mapping between files
59 /// and token data in the PTH file.
60 std::unique_ptr<PTHFileLookup> FileLookup;
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;
67 /// SortedIdTable - Abstract data structure mapping from strings to
68 /// persistent IDs. This is used by get().
69 std::unique_ptr<PTHStringIdLookup> StringIdLookup;
71 /// NumIds - The number of identifiers in the PTH file.
72 const unsigned NumIds;
74 /// PP - The Preprocessor object that will use this PTHManager to create
76 Preprocessor* PP = nullptr;
78 /// SpellingBase - The base offset within the PTH memory buffer that
79 /// contains the cached spellings for literals.
80 const unsigned char* const SpellingBase;
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;
86 /// This constructor is intended to only be called by the static 'Create'
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);
95 /// getSpellingAtPTHOffset - Used by PTHLexer classes to get the cached
96 /// spelling for a token.
97 unsigned getSpellingAtPTHOffset(unsigned PTHOffset, const char*& Buffer);
99 /// GetIdentifierInfo - Used to reconstruct IdentifierInfo objects from the
101 IdentifierInfo *GetIdentifierInfo(unsigned PersistentID) {
102 // Check if the IdentifierInfo has already been resolved.
103 if (IdentifierInfo* II = PerIDCache[PersistentID])
105 return LazilyCreateIdentifierInfo(PersistentID);
107 IdentifierInfo* LazilyCreateIdentifierInfo(unsigned PersistentID);
110 // The current PTH version.
111 enum { Version = 10 };
113 PTHManager(const PTHManager &) = delete;
114 PTHManager &operator=(const PTHManager &) = delete;
115 ~PTHManager() override;
117 /// getOriginalSourceFile - Return the full path to the original header
118 /// file name that was used to generate the PTH cache.
119 const char* getOriginalSourceFile() const {
120 return OriginalSourceFile;
123 /// get - Return the identifier token info for the specified named identifier.
124 /// Unlike the version in IdentifierTable, this returns a pointer instead
125 /// of a reference. If the pointer is nullptr then the IdentifierInfo cannot
127 IdentifierInfo *get(StringRef Name) override;
129 /// Create - This method creates PTHManager objects. The 'file' argument
130 /// is the name of the PTH file. This method returns nullptr upon failure.
131 static PTHManager *Create(StringRef file, DiagnosticsEngine &Diags);
133 void setPreprocessor(Preprocessor *pp) { PP = pp; }
135 /// CreateLexer - Return a PTHLexer that "lexes" the cached tokens for the
136 /// specified file. This method returns nullptr if no cached tokens exist.
137 /// It is the responsibility of the caller to 'delete' the returned object.
138 PTHLexer *CreateLexer(FileID FID);
140 /// createStatCache - Returns a FileSystemStatCache object for use with
141 /// FileManager objects. These objects use the PTH data to speed up
142 /// calls to stat by memoizing their results from when the PTH file
144 std::unique_ptr<FileSystemStatCache> createStatCache();
149 #endif // LLVM_CLANG_LEX_PTHMANAGER_H