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