summaryrefslogtreecommitdiffstats
path: root/include/clang/Lex/PTHManager.h
blob: f4e4774429f940afc092cd05905946b187bb1452 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//===--- PTHManager.h - Manager object for PTH processing -------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This file defines the PTHManager interface.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_LEX_PTHMANAGER_H
#define LLVM_CLANG_LEX_PTHMANAGER_H

#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/SourceLocation.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/OnDiskHashTable.h"

namespace llvm {
  class MemoryBuffer;
}

namespace clang {

class FileEntry;
class Preprocessor;
class PTHLexer;
class DiagnosticsEngine;
class FileSystemStatCache;

class PTHManager : public IdentifierInfoLookup {
  friend class PTHLexer;

  friend class PTHStatCache;

  class PTHStringLookupTrait;
  class PTHFileLookupTrait;
  typedef llvm::OnDiskChainedHashTable<PTHStringLookupTrait> PTHStringIdLookup;
  typedef llvm::OnDiskChainedHashTable<PTHFileLookupTrait> PTHFileLookup;

  /// The memory mapped PTH file.
  std::unique_ptr<const llvm::MemoryBuffer> Buf;

  /// Alloc - Allocator used for IdentifierInfo objects.
  llvm::BumpPtrAllocator Alloc;

  /// IdMap - A lazily generated cache mapping from persistent identifiers to
  ///  IdentifierInfo*.
  std::unique_ptr<IdentifierInfo *[], llvm::FreeDeleter> PerIDCache;

  /// FileLookup - Abstract data structure used for mapping between files
  ///  and token data in the PTH file.
  std::unique_ptr<PTHFileLookup> FileLookup;

  /// IdDataTable - Array representing the mapping from persistent IDs to the
  ///  data offset within the PTH file containing the information to
  ///  reconsitute an IdentifierInfo.
  const unsigned char* const IdDataTable;

  /// SortedIdTable - Abstract data structure mapping from strings to
  ///  persistent IDs.  This is used by get().
  std::unique_ptr<PTHStringIdLookup> StringIdLookup;

  /// NumIds - The number of identifiers in the PTH file.
  const unsigned NumIds;

  /// PP - The Preprocessor object that will use this PTHManager to create
  ///  PTHLexer objects.
  Preprocessor* PP;

  /// SpellingBase - The base offset within the PTH memory buffer that
  ///  contains the cached spellings for literals.
  const unsigned char* const SpellingBase;

  /// OriginalSourceFile - A null-terminated C-string that specifies the name
  ///  if the file (if any) that was to used to generate the PTH cache.
  const char* OriginalSourceFile;

  /// This constructor is intended to only be called by the static 'Create'
  /// method.
  PTHManager(std::unique_ptr<const llvm::MemoryBuffer> buf,
             std::unique_ptr<PTHFileLookup> fileLookup,
             const unsigned char *idDataTable,
             std::unique_ptr<IdentifierInfo *[], llvm::FreeDeleter> perIDCache,
             std::unique_ptr<PTHStringIdLookup> stringIdLookup, unsigned numIds,
             const unsigned char *spellingBase, const char *originalSourceFile);

  PTHManager(const PTHManager &) = delete;
  void operator=(const PTHManager &) = delete;

  /// getSpellingAtPTHOffset - Used by PTHLexer classes to get the cached
  ///  spelling for a token.
  unsigned getSpellingAtPTHOffset(unsigned PTHOffset, const char*& Buffer);

  /// GetIdentifierInfo - Used to reconstruct IdentifierInfo objects from the
  ///  PTH file.
  inline IdentifierInfo* GetIdentifierInfo(unsigned PersistentID) {
    // Check if the IdentifierInfo has already been resolved.
    if (IdentifierInfo* II = PerIDCache[PersistentID])
      return II;
    return LazilyCreateIdentifierInfo(PersistentID);
  }
  IdentifierInfo* LazilyCreateIdentifierInfo(unsigned PersistentID);

public:
  // The current PTH version.
  enum { Version = 10 };

  ~PTHManager() override;

  /// getOriginalSourceFile - Return the full path to the original header
  ///  file name that was used to generate the PTH cache.
  const char* getOriginalSourceFile() const {
    return OriginalSourceFile;
  }

  /// get - Return the identifier token info for the specified named identifier.
  ///  Unlike the version in IdentifierTable, this returns a pointer instead
  ///  of a reference.  If the pointer is NULL then the IdentifierInfo cannot
  ///  be found.
  IdentifierInfo *get(StringRef Name) override;

  /// Create - This method creates PTHManager objects.  The 'file' argument
  ///  is the name of the PTH file.  This method returns NULL upon failure.
  static PTHManager *Create(StringRef file, DiagnosticsEngine &Diags);

  void setPreprocessor(Preprocessor *pp) { PP = pp; }

  /// CreateLexer - Return a PTHLexer that "lexes" the cached tokens for the
  ///  specified file.  This method returns NULL if no cached tokens exist.
  ///  It is the responsibility of the caller to 'delete' the returned object.
  PTHLexer *CreateLexer(FileID FID);

  /// createStatCache - Returns a FileSystemStatCache object for use with
  ///  FileManager objects.  These objects use the PTH data to speed up
  ///  calls to stat by memoizing their results from when the PTH file
  ///  was generated.
  std::unique_ptr<FileSystemStatCache> createStatCache();
};

}  // end namespace clang

#endif