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