xref: /minix3/external/bsd/llvm/dist/clang/lib/Serialization/GlobalModuleIndex.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- GlobalModuleIndex.cpp - Global Module Index ------------*- C++ -*-===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements the GlobalModuleIndex class.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "ASTReaderInternals.h"
15f4a2713aSLionel Sambuc #include "clang/Basic/FileManager.h"
16*0a6a1f1dSLionel Sambuc #include "clang/Lex/HeaderSearch.h"
17f4a2713aSLionel Sambuc #include "clang/Serialization/ASTBitCodes.h"
18f4a2713aSLionel Sambuc #include "clang/Serialization/GlobalModuleIndex.h"
19f4a2713aSLionel Sambuc #include "clang/Serialization/Module.h"
20f4a2713aSLionel Sambuc #include "llvm/ADT/DenseMap.h"
21f4a2713aSLionel Sambuc #include "llvm/ADT/MapVector.h"
22f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
23f4a2713aSLionel Sambuc #include "llvm/ADT/StringExtras.h"
24f4a2713aSLionel Sambuc #include "llvm/Bitcode/BitstreamReader.h"
25f4a2713aSLionel Sambuc #include "llvm/Bitcode/BitstreamWriter.h"
26f4a2713aSLionel Sambuc #include "llvm/Support/FileSystem.h"
27f4a2713aSLionel Sambuc #include "llvm/Support/LockFileManager.h"
28f4a2713aSLionel Sambuc #include "llvm/Support/MemoryBuffer.h"
29*0a6a1f1dSLionel Sambuc #include "llvm/Support/OnDiskHashTable.h"
30f4a2713aSLionel Sambuc #include "llvm/Support/Path.h"
31f4a2713aSLionel Sambuc #include <cstdio>
32f4a2713aSLionel Sambuc using namespace clang;
33f4a2713aSLionel Sambuc using namespace serialization;
34f4a2713aSLionel Sambuc 
35f4a2713aSLionel Sambuc //----------------------------------------------------------------------------//
36f4a2713aSLionel Sambuc // Shared constants
37f4a2713aSLionel Sambuc //----------------------------------------------------------------------------//
38f4a2713aSLionel Sambuc namespace {
39f4a2713aSLionel Sambuc   enum {
40f4a2713aSLionel Sambuc     /// \brief The block containing the index.
41f4a2713aSLionel Sambuc     GLOBAL_INDEX_BLOCK_ID = llvm::bitc::FIRST_APPLICATION_BLOCKID
42f4a2713aSLionel Sambuc   };
43f4a2713aSLionel Sambuc 
44f4a2713aSLionel Sambuc   /// \brief Describes the record types in the index.
45f4a2713aSLionel Sambuc   enum IndexRecordTypes {
46f4a2713aSLionel Sambuc     /// \brief Contains version information and potentially other metadata,
47f4a2713aSLionel Sambuc     /// used to determine if we can read this global index file.
48f4a2713aSLionel Sambuc     INDEX_METADATA,
49f4a2713aSLionel Sambuc     /// \brief Describes a module, including its file name and dependencies.
50f4a2713aSLionel Sambuc     MODULE,
51f4a2713aSLionel Sambuc     /// \brief The index for identifiers.
52f4a2713aSLionel Sambuc     IDENTIFIER_INDEX
53f4a2713aSLionel Sambuc   };
54f4a2713aSLionel Sambuc }
55f4a2713aSLionel Sambuc 
56f4a2713aSLionel Sambuc /// \brief The name of the global index file.
57f4a2713aSLionel Sambuc static const char * const IndexFileName = "modules.idx";
58f4a2713aSLionel Sambuc 
59f4a2713aSLionel Sambuc /// \brief The global index file version.
60f4a2713aSLionel Sambuc static const unsigned CurrentVersion = 1;
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc //----------------------------------------------------------------------------//
63f4a2713aSLionel Sambuc // Global module index reader.
64f4a2713aSLionel Sambuc //----------------------------------------------------------------------------//
65f4a2713aSLionel Sambuc 
66f4a2713aSLionel Sambuc namespace {
67f4a2713aSLionel Sambuc 
68f4a2713aSLionel Sambuc /// \brief Trait used to read the identifier index from the on-disk hash
69f4a2713aSLionel Sambuc /// table.
70f4a2713aSLionel Sambuc class IdentifierIndexReaderTrait {
71f4a2713aSLionel Sambuc public:
72f4a2713aSLionel Sambuc   typedef StringRef external_key_type;
73f4a2713aSLionel Sambuc   typedef StringRef internal_key_type;
74f4a2713aSLionel Sambuc   typedef SmallVector<unsigned, 2> data_type;
75*0a6a1f1dSLionel Sambuc   typedef unsigned hash_value_type;
76*0a6a1f1dSLionel Sambuc   typedef unsigned offset_type;
77f4a2713aSLionel Sambuc 
EqualKey(const internal_key_type & a,const internal_key_type & b)78f4a2713aSLionel Sambuc   static bool EqualKey(const internal_key_type& a, const internal_key_type& b) {
79f4a2713aSLionel Sambuc     return a == b;
80f4a2713aSLionel Sambuc   }
81f4a2713aSLionel Sambuc 
ComputeHash(const internal_key_type & a)82*0a6a1f1dSLionel Sambuc   static hash_value_type ComputeHash(const internal_key_type& a) {
83f4a2713aSLionel Sambuc     return llvm::HashString(a);
84f4a2713aSLionel Sambuc   }
85f4a2713aSLionel Sambuc 
86f4a2713aSLionel Sambuc   static std::pair<unsigned, unsigned>
ReadKeyDataLength(const unsigned char * & d)87f4a2713aSLionel Sambuc   ReadKeyDataLength(const unsigned char*& d) {
88*0a6a1f1dSLionel Sambuc     using namespace llvm::support;
89*0a6a1f1dSLionel Sambuc     unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
90*0a6a1f1dSLionel Sambuc     unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
91f4a2713aSLionel Sambuc     return std::make_pair(KeyLen, DataLen);
92f4a2713aSLionel Sambuc   }
93f4a2713aSLionel Sambuc 
94f4a2713aSLionel Sambuc   static const internal_key_type&
GetInternalKey(const external_key_type & x)95f4a2713aSLionel Sambuc   GetInternalKey(const external_key_type& x) { return x; }
96f4a2713aSLionel Sambuc 
97f4a2713aSLionel Sambuc   static const external_key_type&
GetExternalKey(const internal_key_type & x)98f4a2713aSLionel Sambuc   GetExternalKey(const internal_key_type& x) { return x; }
99f4a2713aSLionel Sambuc 
ReadKey(const unsigned char * d,unsigned n)100f4a2713aSLionel Sambuc   static internal_key_type ReadKey(const unsigned char* d, unsigned n) {
101f4a2713aSLionel Sambuc     return StringRef((const char *)d, n);
102f4a2713aSLionel Sambuc   }
103f4a2713aSLionel Sambuc 
ReadData(const internal_key_type & k,const unsigned char * d,unsigned DataLen)104f4a2713aSLionel Sambuc   static data_type ReadData(const internal_key_type& k,
105f4a2713aSLionel Sambuc                             const unsigned char* d,
106f4a2713aSLionel Sambuc                             unsigned DataLen) {
107*0a6a1f1dSLionel Sambuc     using namespace llvm::support;
108f4a2713aSLionel Sambuc 
109f4a2713aSLionel Sambuc     data_type Result;
110f4a2713aSLionel Sambuc     while (DataLen > 0) {
111*0a6a1f1dSLionel Sambuc       unsigned ID = endian::readNext<uint32_t, little, unaligned>(d);
112f4a2713aSLionel Sambuc       Result.push_back(ID);
113f4a2713aSLionel Sambuc       DataLen -= 4;
114f4a2713aSLionel Sambuc     }
115f4a2713aSLionel Sambuc 
116f4a2713aSLionel Sambuc     return Result;
117f4a2713aSLionel Sambuc   }
118f4a2713aSLionel Sambuc };
119f4a2713aSLionel Sambuc 
120*0a6a1f1dSLionel Sambuc typedef llvm::OnDiskIterableChainedHashTable<IdentifierIndexReaderTrait>
121*0a6a1f1dSLionel Sambuc     IdentifierIndexTable;
122f4a2713aSLionel Sambuc 
123f4a2713aSLionel Sambuc }
124f4a2713aSLionel Sambuc 
GlobalModuleIndex(std::unique_ptr<llvm::MemoryBuffer> Buffer,llvm::BitstreamCursor Cursor)125*0a6a1f1dSLionel Sambuc GlobalModuleIndex::GlobalModuleIndex(std::unique_ptr<llvm::MemoryBuffer> Buffer,
126f4a2713aSLionel Sambuc                                      llvm::BitstreamCursor Cursor)
127*0a6a1f1dSLionel Sambuc     : Buffer(std::move(Buffer)), IdentifierIndex(), NumIdentifierLookups(),
128*0a6a1f1dSLionel Sambuc       NumIdentifierLookupHits() {
129f4a2713aSLionel Sambuc   // Read the global index.
130f4a2713aSLionel Sambuc   bool InGlobalIndexBlock = false;
131f4a2713aSLionel Sambuc   bool Done = false;
132f4a2713aSLionel Sambuc   while (!Done) {
133f4a2713aSLionel Sambuc     llvm::BitstreamEntry Entry = Cursor.advance();
134f4a2713aSLionel Sambuc 
135f4a2713aSLionel Sambuc     switch (Entry.Kind) {
136f4a2713aSLionel Sambuc     case llvm::BitstreamEntry::Error:
137f4a2713aSLionel Sambuc       return;
138f4a2713aSLionel Sambuc 
139f4a2713aSLionel Sambuc     case llvm::BitstreamEntry::EndBlock:
140f4a2713aSLionel Sambuc       if (InGlobalIndexBlock) {
141f4a2713aSLionel Sambuc         InGlobalIndexBlock = false;
142f4a2713aSLionel Sambuc         Done = true;
143f4a2713aSLionel Sambuc         continue;
144f4a2713aSLionel Sambuc       }
145f4a2713aSLionel Sambuc       return;
146f4a2713aSLionel Sambuc 
147f4a2713aSLionel Sambuc 
148f4a2713aSLionel Sambuc     case llvm::BitstreamEntry::Record:
149f4a2713aSLionel Sambuc       // Entries in the global index block are handled below.
150f4a2713aSLionel Sambuc       if (InGlobalIndexBlock)
151f4a2713aSLionel Sambuc         break;
152f4a2713aSLionel Sambuc 
153f4a2713aSLionel Sambuc       return;
154f4a2713aSLionel Sambuc 
155f4a2713aSLionel Sambuc     case llvm::BitstreamEntry::SubBlock:
156f4a2713aSLionel Sambuc       if (!InGlobalIndexBlock && Entry.ID == GLOBAL_INDEX_BLOCK_ID) {
157f4a2713aSLionel Sambuc         if (Cursor.EnterSubBlock(GLOBAL_INDEX_BLOCK_ID))
158f4a2713aSLionel Sambuc           return;
159f4a2713aSLionel Sambuc 
160f4a2713aSLionel Sambuc         InGlobalIndexBlock = true;
161f4a2713aSLionel Sambuc       } else if (Cursor.SkipBlock()) {
162f4a2713aSLionel Sambuc         return;
163f4a2713aSLionel Sambuc       }
164f4a2713aSLionel Sambuc       continue;
165f4a2713aSLionel Sambuc     }
166f4a2713aSLionel Sambuc 
167f4a2713aSLionel Sambuc     SmallVector<uint64_t, 64> Record;
168f4a2713aSLionel Sambuc     StringRef Blob;
169f4a2713aSLionel Sambuc     switch ((IndexRecordTypes)Cursor.readRecord(Entry.ID, Record, &Blob)) {
170f4a2713aSLionel Sambuc     case INDEX_METADATA:
171f4a2713aSLionel Sambuc       // Make sure that the version matches.
172f4a2713aSLionel Sambuc       if (Record.size() < 1 || Record[0] != CurrentVersion)
173f4a2713aSLionel Sambuc         return;
174f4a2713aSLionel Sambuc       break;
175f4a2713aSLionel Sambuc 
176f4a2713aSLionel Sambuc     case MODULE: {
177f4a2713aSLionel Sambuc       unsigned Idx = 0;
178f4a2713aSLionel Sambuc       unsigned ID = Record[Idx++];
179f4a2713aSLionel Sambuc 
180f4a2713aSLionel Sambuc       // Make room for this module's information.
181f4a2713aSLionel Sambuc       if (ID == Modules.size())
182f4a2713aSLionel Sambuc         Modules.push_back(ModuleInfo());
183f4a2713aSLionel Sambuc       else
184f4a2713aSLionel Sambuc         Modules.resize(ID + 1);
185f4a2713aSLionel Sambuc 
186f4a2713aSLionel Sambuc       // Size/modification time for this module file at the time the
187f4a2713aSLionel Sambuc       // global index was built.
188f4a2713aSLionel Sambuc       Modules[ID].Size = Record[Idx++];
189f4a2713aSLionel Sambuc       Modules[ID].ModTime = Record[Idx++];
190f4a2713aSLionel Sambuc 
191f4a2713aSLionel Sambuc       // File name.
192f4a2713aSLionel Sambuc       unsigned NameLen = Record[Idx++];
193f4a2713aSLionel Sambuc       Modules[ID].FileName.assign(Record.begin() + Idx,
194f4a2713aSLionel Sambuc                                   Record.begin() + Idx + NameLen);
195f4a2713aSLionel Sambuc       Idx += NameLen;
196f4a2713aSLionel Sambuc 
197f4a2713aSLionel Sambuc       // Dependencies
198f4a2713aSLionel Sambuc       unsigned NumDeps = Record[Idx++];
199f4a2713aSLionel Sambuc       Modules[ID].Dependencies.insert(Modules[ID].Dependencies.end(),
200f4a2713aSLionel Sambuc                                       Record.begin() + Idx,
201f4a2713aSLionel Sambuc                                       Record.begin() + Idx + NumDeps);
202f4a2713aSLionel Sambuc       Idx += NumDeps;
203f4a2713aSLionel Sambuc 
204f4a2713aSLionel Sambuc       // Make sure we're at the end of the record.
205f4a2713aSLionel Sambuc       assert(Idx == Record.size() && "More module info?");
206f4a2713aSLionel Sambuc 
207f4a2713aSLionel Sambuc       // Record this module as an unresolved module.
208*0a6a1f1dSLionel Sambuc       // FIXME: this doesn't work correctly for module names containing path
209*0a6a1f1dSLionel Sambuc       // separators.
210*0a6a1f1dSLionel Sambuc       StringRef ModuleName = llvm::sys::path::stem(Modules[ID].FileName);
211*0a6a1f1dSLionel Sambuc       // Remove the -<hash of ModuleMapPath>
212*0a6a1f1dSLionel Sambuc       ModuleName = ModuleName.rsplit('-').first;
213*0a6a1f1dSLionel Sambuc       UnresolvedModules[ModuleName] = ID;
214f4a2713aSLionel Sambuc       break;
215f4a2713aSLionel Sambuc     }
216f4a2713aSLionel Sambuc 
217f4a2713aSLionel Sambuc     case IDENTIFIER_INDEX:
218f4a2713aSLionel Sambuc       // Wire up the identifier index.
219f4a2713aSLionel Sambuc       if (Record[0]) {
220f4a2713aSLionel Sambuc         IdentifierIndex = IdentifierIndexTable::Create(
221f4a2713aSLionel Sambuc             (const unsigned char *)Blob.data() + Record[0],
222*0a6a1f1dSLionel Sambuc             (const unsigned char *)Blob.data() + sizeof(uint32_t),
223*0a6a1f1dSLionel Sambuc             (const unsigned char *)Blob.data(), IdentifierIndexReaderTrait());
224f4a2713aSLionel Sambuc       }
225f4a2713aSLionel Sambuc       break;
226f4a2713aSLionel Sambuc     }
227f4a2713aSLionel Sambuc   }
228f4a2713aSLionel Sambuc }
229f4a2713aSLionel Sambuc 
~GlobalModuleIndex()230*0a6a1f1dSLionel Sambuc GlobalModuleIndex::~GlobalModuleIndex() {
231*0a6a1f1dSLionel Sambuc   delete static_cast<IdentifierIndexTable *>(IdentifierIndex);
232*0a6a1f1dSLionel Sambuc }
233f4a2713aSLionel Sambuc 
234f4a2713aSLionel Sambuc std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode>
readIndex(StringRef Path)235f4a2713aSLionel Sambuc GlobalModuleIndex::readIndex(StringRef Path) {
236f4a2713aSLionel Sambuc   // Load the index file, if it's there.
237f4a2713aSLionel Sambuc   llvm::SmallString<128> IndexPath;
238f4a2713aSLionel Sambuc   IndexPath += Path;
239f4a2713aSLionel Sambuc   llvm::sys::path::append(IndexPath, IndexFileName);
240f4a2713aSLionel Sambuc 
241*0a6a1f1dSLionel Sambuc   llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> BufferOrErr =
242*0a6a1f1dSLionel Sambuc       llvm::MemoryBuffer::getFile(IndexPath.c_str());
243*0a6a1f1dSLionel Sambuc   if (!BufferOrErr)
244*0a6a1f1dSLionel Sambuc     return std::make_pair(nullptr, EC_NotFound);
245*0a6a1f1dSLionel Sambuc   std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(BufferOrErr.get());
246f4a2713aSLionel Sambuc 
247f4a2713aSLionel Sambuc   /// \brief The bitstream reader from which we'll read the AST file.
248f4a2713aSLionel Sambuc   llvm::BitstreamReader Reader((const unsigned char *)Buffer->getBufferStart(),
249f4a2713aSLionel Sambuc                                (const unsigned char *)Buffer->getBufferEnd());
250f4a2713aSLionel Sambuc 
251f4a2713aSLionel Sambuc   /// \brief The main bitstream cursor for the main block.
252f4a2713aSLionel Sambuc   llvm::BitstreamCursor Cursor(Reader);
253f4a2713aSLionel Sambuc 
254f4a2713aSLionel Sambuc   // Sniff for the signature.
255f4a2713aSLionel Sambuc   if (Cursor.Read(8) != 'B' ||
256f4a2713aSLionel Sambuc       Cursor.Read(8) != 'C' ||
257f4a2713aSLionel Sambuc       Cursor.Read(8) != 'G' ||
258f4a2713aSLionel Sambuc       Cursor.Read(8) != 'I') {
259*0a6a1f1dSLionel Sambuc     return std::make_pair(nullptr, EC_IOError);
260f4a2713aSLionel Sambuc   }
261f4a2713aSLionel Sambuc 
262*0a6a1f1dSLionel Sambuc   return std::make_pair(new GlobalModuleIndex(std::move(Buffer), Cursor),
263*0a6a1f1dSLionel Sambuc                         EC_None);
264f4a2713aSLionel Sambuc }
265f4a2713aSLionel Sambuc 
266f4a2713aSLionel Sambuc void
getKnownModules(SmallVectorImpl<ModuleFile * > & ModuleFiles)267f4a2713aSLionel Sambuc GlobalModuleIndex::getKnownModules(SmallVectorImpl<ModuleFile *> &ModuleFiles) {
268f4a2713aSLionel Sambuc   ModuleFiles.clear();
269f4a2713aSLionel Sambuc   for (unsigned I = 0, N = Modules.size(); I != N; ++I) {
270f4a2713aSLionel Sambuc     if (ModuleFile *MF = Modules[I].File)
271f4a2713aSLionel Sambuc       ModuleFiles.push_back(MF);
272f4a2713aSLionel Sambuc   }
273f4a2713aSLionel Sambuc }
274f4a2713aSLionel Sambuc 
getModuleDependencies(ModuleFile * File,SmallVectorImpl<ModuleFile * > & Dependencies)275f4a2713aSLionel Sambuc void GlobalModuleIndex::getModuleDependencies(
276f4a2713aSLionel Sambuc        ModuleFile *File,
277f4a2713aSLionel Sambuc        SmallVectorImpl<ModuleFile *> &Dependencies) {
278f4a2713aSLionel Sambuc   // Look for information about this module file.
279f4a2713aSLionel Sambuc   llvm::DenseMap<ModuleFile *, unsigned>::iterator Known
280f4a2713aSLionel Sambuc     = ModulesByFile.find(File);
281f4a2713aSLionel Sambuc   if (Known == ModulesByFile.end())
282f4a2713aSLionel Sambuc     return;
283f4a2713aSLionel Sambuc 
284f4a2713aSLionel Sambuc   // Record dependencies.
285f4a2713aSLionel Sambuc   Dependencies.clear();
286f4a2713aSLionel Sambuc   ArrayRef<unsigned> StoredDependencies = Modules[Known->second].Dependencies;
287f4a2713aSLionel Sambuc   for (unsigned I = 0, N = StoredDependencies.size(); I != N; ++I) {
288f4a2713aSLionel Sambuc     if (ModuleFile *MF = Modules[I].File)
289f4a2713aSLionel Sambuc       Dependencies.push_back(MF);
290f4a2713aSLionel Sambuc   }
291f4a2713aSLionel Sambuc }
292f4a2713aSLionel Sambuc 
lookupIdentifier(StringRef Name,HitSet & Hits)293f4a2713aSLionel Sambuc bool GlobalModuleIndex::lookupIdentifier(StringRef Name, HitSet &Hits) {
294f4a2713aSLionel Sambuc   Hits.clear();
295f4a2713aSLionel Sambuc 
296f4a2713aSLionel Sambuc   // If there's no identifier index, there is nothing we can do.
297f4a2713aSLionel Sambuc   if (!IdentifierIndex)
298f4a2713aSLionel Sambuc     return false;
299f4a2713aSLionel Sambuc 
300f4a2713aSLionel Sambuc   // Look into the identifier index.
301f4a2713aSLionel Sambuc   ++NumIdentifierLookups;
302f4a2713aSLionel Sambuc   IdentifierIndexTable &Table
303f4a2713aSLionel Sambuc     = *static_cast<IdentifierIndexTable *>(IdentifierIndex);
304f4a2713aSLionel Sambuc   IdentifierIndexTable::iterator Known = Table.find(Name);
305f4a2713aSLionel Sambuc   if (Known == Table.end()) {
306f4a2713aSLionel Sambuc     return true;
307f4a2713aSLionel Sambuc   }
308f4a2713aSLionel Sambuc 
309f4a2713aSLionel Sambuc   SmallVector<unsigned, 2> ModuleIDs = *Known;
310f4a2713aSLionel Sambuc   for (unsigned I = 0, N = ModuleIDs.size(); I != N; ++I) {
311f4a2713aSLionel Sambuc     if (ModuleFile *MF = Modules[ModuleIDs[I]].File)
312f4a2713aSLionel Sambuc       Hits.insert(MF);
313f4a2713aSLionel Sambuc   }
314f4a2713aSLionel Sambuc 
315f4a2713aSLionel Sambuc   ++NumIdentifierLookupHits;
316f4a2713aSLionel Sambuc   return true;
317f4a2713aSLionel Sambuc }
318f4a2713aSLionel Sambuc 
loadedModuleFile(ModuleFile * File)319f4a2713aSLionel Sambuc bool GlobalModuleIndex::loadedModuleFile(ModuleFile *File) {
320f4a2713aSLionel Sambuc   // Look for the module in the global module index based on the module name.
321*0a6a1f1dSLionel Sambuc   StringRef Name = File->ModuleName;
322f4a2713aSLionel Sambuc   llvm::StringMap<unsigned>::iterator Known = UnresolvedModules.find(Name);
323f4a2713aSLionel Sambuc   if (Known == UnresolvedModules.end()) {
324f4a2713aSLionel Sambuc     return true;
325f4a2713aSLionel Sambuc   }
326f4a2713aSLionel Sambuc 
327f4a2713aSLionel Sambuc   // Rectify this module with the global module index.
328f4a2713aSLionel Sambuc   ModuleInfo &Info = Modules[Known->second];
329f4a2713aSLionel Sambuc 
330f4a2713aSLionel Sambuc   //  If the size and modification time match what we expected, record this
331f4a2713aSLionel Sambuc   // module file.
332f4a2713aSLionel Sambuc   bool Failed = true;
333f4a2713aSLionel Sambuc   if (File->File->getSize() == Info.Size &&
334f4a2713aSLionel Sambuc       File->File->getModificationTime() == Info.ModTime) {
335f4a2713aSLionel Sambuc     Info.File = File;
336f4a2713aSLionel Sambuc     ModulesByFile[File] = Known->second;
337f4a2713aSLionel Sambuc 
338f4a2713aSLionel Sambuc     Failed = false;
339f4a2713aSLionel Sambuc   }
340f4a2713aSLionel Sambuc 
341f4a2713aSLionel Sambuc   // One way or another, we have resolved this module file.
342f4a2713aSLionel Sambuc   UnresolvedModules.erase(Known);
343f4a2713aSLionel Sambuc   return Failed;
344f4a2713aSLionel Sambuc }
345f4a2713aSLionel Sambuc 
printStats()346f4a2713aSLionel Sambuc void GlobalModuleIndex::printStats() {
347f4a2713aSLionel Sambuc   std::fprintf(stderr, "*** Global Module Index Statistics:\n");
348f4a2713aSLionel Sambuc   if (NumIdentifierLookups) {
349f4a2713aSLionel Sambuc     fprintf(stderr, "  %u / %u identifier lookups succeeded (%f%%)\n",
350f4a2713aSLionel Sambuc             NumIdentifierLookupHits, NumIdentifierLookups,
351f4a2713aSLionel Sambuc             (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
352f4a2713aSLionel Sambuc   }
353f4a2713aSLionel Sambuc   std::fprintf(stderr, "\n");
354f4a2713aSLionel Sambuc }
355f4a2713aSLionel Sambuc 
dump()356*0a6a1f1dSLionel Sambuc void GlobalModuleIndex::dump() {
357*0a6a1f1dSLionel Sambuc   llvm::errs() << "*** Global Module Index Dump:\n";
358*0a6a1f1dSLionel Sambuc   llvm::errs() << "Module files:\n";
359*0a6a1f1dSLionel Sambuc   for (auto &MI : Modules) {
360*0a6a1f1dSLionel Sambuc     llvm::errs() << "** " << MI.FileName << "\n";
361*0a6a1f1dSLionel Sambuc     if (MI.File)
362*0a6a1f1dSLionel Sambuc       MI.File->dump();
363*0a6a1f1dSLionel Sambuc     else
364*0a6a1f1dSLionel Sambuc       llvm::errs() << "\n";
365*0a6a1f1dSLionel Sambuc   }
366*0a6a1f1dSLionel Sambuc   llvm::errs() << "\n";
367*0a6a1f1dSLionel Sambuc }
368*0a6a1f1dSLionel Sambuc 
369f4a2713aSLionel Sambuc //----------------------------------------------------------------------------//
370f4a2713aSLionel Sambuc // Global module index writer.
371f4a2713aSLionel Sambuc //----------------------------------------------------------------------------//
372f4a2713aSLionel Sambuc 
373f4a2713aSLionel Sambuc namespace {
374f4a2713aSLionel Sambuc   /// \brief Provides information about a specific module file.
375f4a2713aSLionel Sambuc   struct ModuleFileInfo {
376f4a2713aSLionel Sambuc     /// \brief The numberic ID for this module file.
377f4a2713aSLionel Sambuc     unsigned ID;
378f4a2713aSLionel Sambuc 
379f4a2713aSLionel Sambuc     /// \brief The set of modules on which this module depends. Each entry is
380f4a2713aSLionel Sambuc     /// a module ID.
381f4a2713aSLionel Sambuc     SmallVector<unsigned, 4> Dependencies;
382f4a2713aSLionel Sambuc   };
383f4a2713aSLionel Sambuc 
384f4a2713aSLionel Sambuc   /// \brief Builder that generates the global module index file.
385f4a2713aSLionel Sambuc   class GlobalModuleIndexBuilder {
386f4a2713aSLionel Sambuc     FileManager &FileMgr;
387f4a2713aSLionel Sambuc 
388f4a2713aSLionel Sambuc     /// \brief Mapping from files to module file information.
389f4a2713aSLionel Sambuc     typedef llvm::MapVector<const FileEntry *, ModuleFileInfo> ModuleFilesMap;
390f4a2713aSLionel Sambuc 
391f4a2713aSLionel Sambuc     /// \brief Information about each of the known module files.
392f4a2713aSLionel Sambuc     ModuleFilesMap ModuleFiles;
393f4a2713aSLionel Sambuc 
394f4a2713aSLionel Sambuc     /// \brief Mapping from identifiers to the list of module file IDs that
395f4a2713aSLionel Sambuc     /// consider this identifier to be interesting.
396f4a2713aSLionel Sambuc     typedef llvm::StringMap<SmallVector<unsigned, 2> > InterestingIdentifierMap;
397f4a2713aSLionel Sambuc 
398f4a2713aSLionel Sambuc     /// \brief A mapping from all interesting identifiers to the set of module
399f4a2713aSLionel Sambuc     /// files in which those identifiers are considered interesting.
400f4a2713aSLionel Sambuc     InterestingIdentifierMap InterestingIdentifiers;
401f4a2713aSLionel Sambuc 
402f4a2713aSLionel Sambuc     /// \brief Write the block-info block for the global module index file.
403f4a2713aSLionel Sambuc     void emitBlockInfoBlock(llvm::BitstreamWriter &Stream);
404f4a2713aSLionel Sambuc 
405f4a2713aSLionel Sambuc     /// \brief Retrieve the module file information for the given file.
getModuleFileInfo(const FileEntry * File)406f4a2713aSLionel Sambuc     ModuleFileInfo &getModuleFileInfo(const FileEntry *File) {
407f4a2713aSLionel Sambuc       llvm::MapVector<const FileEntry *, ModuleFileInfo>::iterator Known
408f4a2713aSLionel Sambuc         = ModuleFiles.find(File);
409f4a2713aSLionel Sambuc       if (Known != ModuleFiles.end())
410f4a2713aSLionel Sambuc         return Known->second;
411f4a2713aSLionel Sambuc 
412f4a2713aSLionel Sambuc       unsigned NewID = ModuleFiles.size();
413f4a2713aSLionel Sambuc       ModuleFileInfo &Info = ModuleFiles[File];
414f4a2713aSLionel Sambuc       Info.ID = NewID;
415f4a2713aSLionel Sambuc       return Info;
416f4a2713aSLionel Sambuc     }
417f4a2713aSLionel Sambuc 
418f4a2713aSLionel Sambuc   public:
GlobalModuleIndexBuilder(FileManager & FileMgr)419f4a2713aSLionel Sambuc     explicit GlobalModuleIndexBuilder(FileManager &FileMgr) : FileMgr(FileMgr){}
420f4a2713aSLionel Sambuc 
421f4a2713aSLionel Sambuc     /// \brief Load the contents of the given module file into the builder.
422f4a2713aSLionel Sambuc     ///
423f4a2713aSLionel Sambuc     /// \returns true if an error occurred, false otherwise.
424f4a2713aSLionel Sambuc     bool loadModuleFile(const FileEntry *File);
425f4a2713aSLionel Sambuc 
426f4a2713aSLionel Sambuc     /// \brief Write the index to the given bitstream.
427f4a2713aSLionel Sambuc     void writeIndex(llvm::BitstreamWriter &Stream);
428f4a2713aSLionel Sambuc   };
429f4a2713aSLionel Sambuc }
430f4a2713aSLionel Sambuc 
emitBlockID(unsigned ID,const char * Name,llvm::BitstreamWriter & Stream,SmallVectorImpl<uint64_t> & Record)431f4a2713aSLionel Sambuc static void emitBlockID(unsigned ID, const char *Name,
432f4a2713aSLionel Sambuc                         llvm::BitstreamWriter &Stream,
433f4a2713aSLionel Sambuc                         SmallVectorImpl<uint64_t> &Record) {
434f4a2713aSLionel Sambuc   Record.clear();
435f4a2713aSLionel Sambuc   Record.push_back(ID);
436f4a2713aSLionel Sambuc   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
437f4a2713aSLionel Sambuc 
438f4a2713aSLionel Sambuc   // Emit the block name if present.
439*0a6a1f1dSLionel Sambuc   if (!Name || Name[0] == 0) return;
440f4a2713aSLionel Sambuc   Record.clear();
441f4a2713aSLionel Sambuc   while (*Name)
442f4a2713aSLionel Sambuc     Record.push_back(*Name++);
443f4a2713aSLionel Sambuc   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
444f4a2713aSLionel Sambuc }
445f4a2713aSLionel Sambuc 
emitRecordID(unsigned ID,const char * Name,llvm::BitstreamWriter & Stream,SmallVectorImpl<uint64_t> & Record)446f4a2713aSLionel Sambuc static void emitRecordID(unsigned ID, const char *Name,
447f4a2713aSLionel Sambuc                          llvm::BitstreamWriter &Stream,
448f4a2713aSLionel Sambuc                          SmallVectorImpl<uint64_t> &Record) {
449f4a2713aSLionel Sambuc   Record.clear();
450f4a2713aSLionel Sambuc   Record.push_back(ID);
451f4a2713aSLionel Sambuc   while (*Name)
452f4a2713aSLionel Sambuc     Record.push_back(*Name++);
453f4a2713aSLionel Sambuc   Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
454f4a2713aSLionel Sambuc }
455f4a2713aSLionel Sambuc 
456f4a2713aSLionel Sambuc void
emitBlockInfoBlock(llvm::BitstreamWriter & Stream)457f4a2713aSLionel Sambuc GlobalModuleIndexBuilder::emitBlockInfoBlock(llvm::BitstreamWriter &Stream) {
458f4a2713aSLionel Sambuc   SmallVector<uint64_t, 64> Record;
459f4a2713aSLionel Sambuc   Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
460f4a2713aSLionel Sambuc 
461f4a2713aSLionel Sambuc #define BLOCK(X) emitBlockID(X ## _ID, #X, Stream, Record)
462f4a2713aSLionel Sambuc #define RECORD(X) emitRecordID(X, #X, Stream, Record)
463f4a2713aSLionel Sambuc   BLOCK(GLOBAL_INDEX_BLOCK);
464f4a2713aSLionel Sambuc   RECORD(INDEX_METADATA);
465f4a2713aSLionel Sambuc   RECORD(MODULE);
466f4a2713aSLionel Sambuc   RECORD(IDENTIFIER_INDEX);
467f4a2713aSLionel Sambuc #undef RECORD
468f4a2713aSLionel Sambuc #undef BLOCK
469f4a2713aSLionel Sambuc 
470f4a2713aSLionel Sambuc   Stream.ExitBlock();
471f4a2713aSLionel Sambuc }
472f4a2713aSLionel Sambuc 
473f4a2713aSLionel Sambuc namespace {
474f4a2713aSLionel Sambuc   class InterestingASTIdentifierLookupTrait
475f4a2713aSLionel Sambuc     : public serialization::reader::ASTIdentifierLookupTraitBase {
476f4a2713aSLionel Sambuc 
477f4a2713aSLionel Sambuc   public:
478f4a2713aSLionel Sambuc     /// \brief The identifier and whether it is "interesting".
479f4a2713aSLionel Sambuc     typedef std::pair<StringRef, bool> data_type;
480f4a2713aSLionel Sambuc 
ReadData(const internal_key_type & k,const unsigned char * d,unsigned DataLen)481f4a2713aSLionel Sambuc     data_type ReadData(const internal_key_type& k,
482f4a2713aSLionel Sambuc                        const unsigned char* d,
483f4a2713aSLionel Sambuc                        unsigned DataLen) {
484f4a2713aSLionel Sambuc       // The first bit indicates whether this identifier is interesting.
485f4a2713aSLionel Sambuc       // That's all we care about.
486*0a6a1f1dSLionel Sambuc       using namespace llvm::support;
487*0a6a1f1dSLionel Sambuc       unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
488f4a2713aSLionel Sambuc       bool IsInteresting = RawID & 0x01;
489f4a2713aSLionel Sambuc       return std::make_pair(k, IsInteresting);
490f4a2713aSLionel Sambuc     }
491f4a2713aSLionel Sambuc   };
492f4a2713aSLionel Sambuc }
493f4a2713aSLionel Sambuc 
loadModuleFile(const FileEntry * File)494f4a2713aSLionel Sambuc bool GlobalModuleIndexBuilder::loadModuleFile(const FileEntry *File) {
495f4a2713aSLionel Sambuc   // Open the module file.
496*0a6a1f1dSLionel Sambuc 
497*0a6a1f1dSLionel Sambuc   auto Buffer = FileMgr.getBufferForFile(File, /*isVolatile=*/true);
498f4a2713aSLionel Sambuc   if (!Buffer) {
499f4a2713aSLionel Sambuc     return true;
500f4a2713aSLionel Sambuc   }
501f4a2713aSLionel Sambuc 
502f4a2713aSLionel Sambuc   // Initialize the input stream
503f4a2713aSLionel Sambuc   llvm::BitstreamReader InStreamFile;
504*0a6a1f1dSLionel Sambuc   InStreamFile.init((const unsigned char *)(*Buffer)->getBufferStart(),
505*0a6a1f1dSLionel Sambuc                     (const unsigned char *)(*Buffer)->getBufferEnd());
506*0a6a1f1dSLionel Sambuc   llvm::BitstreamCursor InStream(InStreamFile);
507f4a2713aSLionel Sambuc 
508f4a2713aSLionel Sambuc   // Sniff for the signature.
509f4a2713aSLionel Sambuc   if (InStream.Read(8) != 'C' ||
510f4a2713aSLionel Sambuc       InStream.Read(8) != 'P' ||
511f4a2713aSLionel Sambuc       InStream.Read(8) != 'C' ||
512f4a2713aSLionel Sambuc       InStream.Read(8) != 'H') {
513f4a2713aSLionel Sambuc     return true;
514f4a2713aSLionel Sambuc   }
515f4a2713aSLionel Sambuc 
516f4a2713aSLionel Sambuc   // Record this module file and assign it a unique ID (if it doesn't have
517f4a2713aSLionel Sambuc   // one already).
518f4a2713aSLionel Sambuc   unsigned ID = getModuleFileInfo(File).ID;
519f4a2713aSLionel Sambuc 
520f4a2713aSLionel Sambuc   // Search for the blocks and records we care about.
521f4a2713aSLionel Sambuc   enum { Other, ControlBlock, ASTBlock } State = Other;
522f4a2713aSLionel Sambuc   bool Done = false;
523f4a2713aSLionel Sambuc   while (!Done) {
524f4a2713aSLionel Sambuc     llvm::BitstreamEntry Entry = InStream.advance();
525f4a2713aSLionel Sambuc     switch (Entry.Kind) {
526f4a2713aSLionel Sambuc     case llvm::BitstreamEntry::Error:
527f4a2713aSLionel Sambuc       Done = true;
528f4a2713aSLionel Sambuc       continue;
529f4a2713aSLionel Sambuc 
530f4a2713aSLionel Sambuc     case llvm::BitstreamEntry::Record:
531f4a2713aSLionel Sambuc       // In the 'other' state, just skip the record. We don't care.
532f4a2713aSLionel Sambuc       if (State == Other) {
533f4a2713aSLionel Sambuc         InStream.skipRecord(Entry.ID);
534f4a2713aSLionel Sambuc         continue;
535f4a2713aSLionel Sambuc       }
536f4a2713aSLionel Sambuc 
537f4a2713aSLionel Sambuc       // Handle potentially-interesting records below.
538f4a2713aSLionel Sambuc       break;
539f4a2713aSLionel Sambuc 
540f4a2713aSLionel Sambuc     case llvm::BitstreamEntry::SubBlock:
541f4a2713aSLionel Sambuc       if (Entry.ID == CONTROL_BLOCK_ID) {
542f4a2713aSLionel Sambuc         if (InStream.EnterSubBlock(CONTROL_BLOCK_ID))
543f4a2713aSLionel Sambuc           return true;
544f4a2713aSLionel Sambuc 
545f4a2713aSLionel Sambuc         // Found the control block.
546f4a2713aSLionel Sambuc         State = ControlBlock;
547f4a2713aSLionel Sambuc         continue;
548f4a2713aSLionel Sambuc       }
549f4a2713aSLionel Sambuc 
550f4a2713aSLionel Sambuc       if (Entry.ID == AST_BLOCK_ID) {
551f4a2713aSLionel Sambuc         if (InStream.EnterSubBlock(AST_BLOCK_ID))
552f4a2713aSLionel Sambuc           return true;
553f4a2713aSLionel Sambuc 
554f4a2713aSLionel Sambuc         // Found the AST block.
555f4a2713aSLionel Sambuc         State = ASTBlock;
556f4a2713aSLionel Sambuc         continue;
557f4a2713aSLionel Sambuc       }
558f4a2713aSLionel Sambuc 
559f4a2713aSLionel Sambuc       if (InStream.SkipBlock())
560f4a2713aSLionel Sambuc         return true;
561f4a2713aSLionel Sambuc 
562f4a2713aSLionel Sambuc       continue;
563f4a2713aSLionel Sambuc 
564f4a2713aSLionel Sambuc     case llvm::BitstreamEntry::EndBlock:
565f4a2713aSLionel Sambuc       State = Other;
566f4a2713aSLionel Sambuc       continue;
567f4a2713aSLionel Sambuc     }
568f4a2713aSLionel Sambuc 
569f4a2713aSLionel Sambuc     // Read the given record.
570f4a2713aSLionel Sambuc     SmallVector<uint64_t, 64> Record;
571f4a2713aSLionel Sambuc     StringRef Blob;
572f4a2713aSLionel Sambuc     unsigned Code = InStream.readRecord(Entry.ID, Record, &Blob);
573f4a2713aSLionel Sambuc 
574f4a2713aSLionel Sambuc     // Handle module dependencies.
575f4a2713aSLionel Sambuc     if (State == ControlBlock && Code == IMPORTS) {
576f4a2713aSLionel Sambuc       // Load each of the imported PCH files.
577f4a2713aSLionel Sambuc       unsigned Idx = 0, N = Record.size();
578f4a2713aSLionel Sambuc       while (Idx < N) {
579f4a2713aSLionel Sambuc         // Read information about the AST file.
580f4a2713aSLionel Sambuc 
581f4a2713aSLionel Sambuc         // Skip the imported kind
582f4a2713aSLionel Sambuc         ++Idx;
583f4a2713aSLionel Sambuc 
584f4a2713aSLionel Sambuc         // Skip the import location
585f4a2713aSLionel Sambuc         ++Idx;
586f4a2713aSLionel Sambuc 
587f4a2713aSLionel Sambuc         // Load stored size/modification time.
588f4a2713aSLionel Sambuc         off_t StoredSize = (off_t)Record[Idx++];
589f4a2713aSLionel Sambuc         time_t StoredModTime = (time_t)Record[Idx++];
590f4a2713aSLionel Sambuc 
591*0a6a1f1dSLionel Sambuc         // Skip the stored signature.
592*0a6a1f1dSLionel Sambuc         // FIXME: we could read the signature out of the import and validate it.
593*0a6a1f1dSLionel Sambuc         Idx++;
594*0a6a1f1dSLionel Sambuc 
595f4a2713aSLionel Sambuc         // Retrieve the imported file name.
596f4a2713aSLionel Sambuc         unsigned Length = Record[Idx++];
597f4a2713aSLionel Sambuc         SmallString<128> ImportedFile(Record.begin() + Idx,
598f4a2713aSLionel Sambuc                                       Record.begin() + Idx + Length);
599f4a2713aSLionel Sambuc         Idx += Length;
600f4a2713aSLionel Sambuc 
601f4a2713aSLionel Sambuc         // Find the imported module file.
602f4a2713aSLionel Sambuc         const FileEntry *DependsOnFile
603f4a2713aSLionel Sambuc           = FileMgr.getFile(ImportedFile, /*openFile=*/false,
604f4a2713aSLionel Sambuc                             /*cacheFailure=*/false);
605f4a2713aSLionel Sambuc         if (!DependsOnFile ||
606f4a2713aSLionel Sambuc             (StoredSize != DependsOnFile->getSize()) ||
607f4a2713aSLionel Sambuc             (StoredModTime != DependsOnFile->getModificationTime()))
608f4a2713aSLionel Sambuc           return true;
609f4a2713aSLionel Sambuc 
610f4a2713aSLionel Sambuc         // Record the dependency.
611f4a2713aSLionel Sambuc         unsigned DependsOnID = getModuleFileInfo(DependsOnFile).ID;
612f4a2713aSLionel Sambuc         getModuleFileInfo(File).Dependencies.push_back(DependsOnID);
613f4a2713aSLionel Sambuc       }
614f4a2713aSLionel Sambuc 
615f4a2713aSLionel Sambuc       continue;
616f4a2713aSLionel Sambuc     }
617f4a2713aSLionel Sambuc 
618f4a2713aSLionel Sambuc     // Handle the identifier table
619f4a2713aSLionel Sambuc     if (State == ASTBlock && Code == IDENTIFIER_TABLE && Record[0] > 0) {
620*0a6a1f1dSLionel Sambuc       typedef llvm::OnDiskIterableChainedHashTable<
621*0a6a1f1dSLionel Sambuc           InterestingASTIdentifierLookupTrait> InterestingIdentifierTable;
622*0a6a1f1dSLionel Sambuc       std::unique_ptr<InterestingIdentifierTable> Table(
623*0a6a1f1dSLionel Sambuc           InterestingIdentifierTable::Create(
624f4a2713aSLionel Sambuc               (const unsigned char *)Blob.data() + Record[0],
625*0a6a1f1dSLionel Sambuc               (const unsigned char *)Blob.data() + sizeof(uint32_t),
626f4a2713aSLionel Sambuc               (const unsigned char *)Blob.data()));
627f4a2713aSLionel Sambuc       for (InterestingIdentifierTable::data_iterator D = Table->data_begin(),
628f4a2713aSLionel Sambuc                                                      DEnd = Table->data_end();
629f4a2713aSLionel Sambuc            D != DEnd; ++D) {
630f4a2713aSLionel Sambuc         std::pair<StringRef, bool> Ident = *D;
631f4a2713aSLionel Sambuc         if (Ident.second)
632f4a2713aSLionel Sambuc           InterestingIdentifiers[Ident.first].push_back(ID);
633f4a2713aSLionel Sambuc         else
634f4a2713aSLionel Sambuc           (void)InterestingIdentifiers[Ident.first];
635f4a2713aSLionel Sambuc       }
636f4a2713aSLionel Sambuc     }
637f4a2713aSLionel Sambuc 
638f4a2713aSLionel Sambuc     // We don't care about this record.
639f4a2713aSLionel Sambuc   }
640f4a2713aSLionel Sambuc 
641f4a2713aSLionel Sambuc   return false;
642f4a2713aSLionel Sambuc }
643f4a2713aSLionel Sambuc 
644f4a2713aSLionel Sambuc namespace {
645f4a2713aSLionel Sambuc 
646f4a2713aSLionel Sambuc /// \brief Trait used to generate the identifier index as an on-disk hash
647f4a2713aSLionel Sambuc /// table.
648f4a2713aSLionel Sambuc class IdentifierIndexWriterTrait {
649f4a2713aSLionel Sambuc public:
650f4a2713aSLionel Sambuc   typedef StringRef key_type;
651f4a2713aSLionel Sambuc   typedef StringRef key_type_ref;
652f4a2713aSLionel Sambuc   typedef SmallVector<unsigned, 2> data_type;
653f4a2713aSLionel Sambuc   typedef const SmallVector<unsigned, 2> &data_type_ref;
654*0a6a1f1dSLionel Sambuc   typedef unsigned hash_value_type;
655*0a6a1f1dSLionel Sambuc   typedef unsigned offset_type;
656f4a2713aSLionel Sambuc 
ComputeHash(key_type_ref Key)657*0a6a1f1dSLionel Sambuc   static hash_value_type ComputeHash(key_type_ref Key) {
658f4a2713aSLionel Sambuc     return llvm::HashString(Key);
659f4a2713aSLionel Sambuc   }
660f4a2713aSLionel Sambuc 
661f4a2713aSLionel Sambuc   std::pair<unsigned,unsigned>
EmitKeyDataLength(raw_ostream & Out,key_type_ref Key,data_type_ref Data)662f4a2713aSLionel Sambuc   EmitKeyDataLength(raw_ostream& Out, key_type_ref Key, data_type_ref Data) {
663*0a6a1f1dSLionel Sambuc     using namespace llvm::support;
664*0a6a1f1dSLionel Sambuc     endian::Writer<little> LE(Out);
665f4a2713aSLionel Sambuc     unsigned KeyLen = Key.size();
666f4a2713aSLionel Sambuc     unsigned DataLen = Data.size() * 4;
667*0a6a1f1dSLionel Sambuc     LE.write<uint16_t>(KeyLen);
668*0a6a1f1dSLionel Sambuc     LE.write<uint16_t>(DataLen);
669f4a2713aSLionel Sambuc     return std::make_pair(KeyLen, DataLen);
670f4a2713aSLionel Sambuc   }
671f4a2713aSLionel Sambuc 
EmitKey(raw_ostream & Out,key_type_ref Key,unsigned KeyLen)672f4a2713aSLionel Sambuc   void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
673f4a2713aSLionel Sambuc     Out.write(Key.data(), KeyLen);
674f4a2713aSLionel Sambuc   }
675f4a2713aSLionel Sambuc 
EmitData(raw_ostream & Out,key_type_ref Key,data_type_ref Data,unsigned DataLen)676f4a2713aSLionel Sambuc   void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
677f4a2713aSLionel Sambuc                 unsigned DataLen) {
678*0a6a1f1dSLionel Sambuc     using namespace llvm::support;
679f4a2713aSLionel Sambuc     for (unsigned I = 0, N = Data.size(); I != N; ++I)
680*0a6a1f1dSLionel Sambuc       endian::Writer<little>(Out).write<uint32_t>(Data[I]);
681f4a2713aSLionel Sambuc   }
682f4a2713aSLionel Sambuc };
683f4a2713aSLionel Sambuc 
684f4a2713aSLionel Sambuc }
685f4a2713aSLionel Sambuc 
writeIndex(llvm::BitstreamWriter & Stream)686f4a2713aSLionel Sambuc void GlobalModuleIndexBuilder::writeIndex(llvm::BitstreamWriter &Stream) {
687f4a2713aSLionel Sambuc   using namespace llvm;
688f4a2713aSLionel Sambuc 
689f4a2713aSLionel Sambuc   // Emit the file header.
690f4a2713aSLionel Sambuc   Stream.Emit((unsigned)'B', 8);
691f4a2713aSLionel Sambuc   Stream.Emit((unsigned)'C', 8);
692f4a2713aSLionel Sambuc   Stream.Emit((unsigned)'G', 8);
693f4a2713aSLionel Sambuc   Stream.Emit((unsigned)'I', 8);
694f4a2713aSLionel Sambuc 
695f4a2713aSLionel Sambuc   // Write the block-info block, which describes the records in this bitcode
696f4a2713aSLionel Sambuc   // file.
697f4a2713aSLionel Sambuc   emitBlockInfoBlock(Stream);
698f4a2713aSLionel Sambuc 
699f4a2713aSLionel Sambuc   Stream.EnterSubblock(GLOBAL_INDEX_BLOCK_ID, 3);
700f4a2713aSLionel Sambuc 
701f4a2713aSLionel Sambuc   // Write the metadata.
702f4a2713aSLionel Sambuc   SmallVector<uint64_t, 2> Record;
703f4a2713aSLionel Sambuc   Record.push_back(CurrentVersion);
704f4a2713aSLionel Sambuc   Stream.EmitRecord(INDEX_METADATA, Record);
705f4a2713aSLionel Sambuc 
706f4a2713aSLionel Sambuc   // Write the set of known module files.
707f4a2713aSLionel Sambuc   for (ModuleFilesMap::iterator M = ModuleFiles.begin(),
708f4a2713aSLionel Sambuc                                 MEnd = ModuleFiles.end();
709f4a2713aSLionel Sambuc        M != MEnd; ++M) {
710f4a2713aSLionel Sambuc     Record.clear();
711f4a2713aSLionel Sambuc     Record.push_back(M->second.ID);
712f4a2713aSLionel Sambuc     Record.push_back(M->first->getSize());
713f4a2713aSLionel Sambuc     Record.push_back(M->first->getModificationTime());
714f4a2713aSLionel Sambuc 
715f4a2713aSLionel Sambuc     // File name
716f4a2713aSLionel Sambuc     StringRef Name(M->first->getName());
717f4a2713aSLionel Sambuc     Record.push_back(Name.size());
718f4a2713aSLionel Sambuc     Record.append(Name.begin(), Name.end());
719f4a2713aSLionel Sambuc 
720f4a2713aSLionel Sambuc     // Dependencies
721f4a2713aSLionel Sambuc     Record.push_back(M->second.Dependencies.size());
722f4a2713aSLionel Sambuc     Record.append(M->second.Dependencies.begin(), M->second.Dependencies.end());
723f4a2713aSLionel Sambuc     Stream.EmitRecord(MODULE, Record);
724f4a2713aSLionel Sambuc   }
725f4a2713aSLionel Sambuc 
726f4a2713aSLionel Sambuc   // Write the identifier -> module file mapping.
727f4a2713aSLionel Sambuc   {
728*0a6a1f1dSLionel Sambuc     llvm::OnDiskChainedHashTableGenerator<IdentifierIndexWriterTrait> Generator;
729f4a2713aSLionel Sambuc     IdentifierIndexWriterTrait Trait;
730f4a2713aSLionel Sambuc 
731f4a2713aSLionel Sambuc     // Populate the hash table.
732f4a2713aSLionel Sambuc     for (InterestingIdentifierMap::iterator I = InterestingIdentifiers.begin(),
733f4a2713aSLionel Sambuc                                             IEnd = InterestingIdentifiers.end();
734f4a2713aSLionel Sambuc          I != IEnd; ++I) {
735f4a2713aSLionel Sambuc       Generator.insert(I->first(), I->second, Trait);
736f4a2713aSLionel Sambuc     }
737f4a2713aSLionel Sambuc 
738f4a2713aSLionel Sambuc     // Create the on-disk hash table in a buffer.
739f4a2713aSLionel Sambuc     SmallString<4096> IdentifierTable;
740f4a2713aSLionel Sambuc     uint32_t BucketOffset;
741f4a2713aSLionel Sambuc     {
742*0a6a1f1dSLionel Sambuc       using namespace llvm::support;
743f4a2713aSLionel Sambuc       llvm::raw_svector_ostream Out(IdentifierTable);
744f4a2713aSLionel Sambuc       // Make sure that no bucket is at offset 0
745*0a6a1f1dSLionel Sambuc       endian::Writer<little>(Out).write<uint32_t>(0);
746f4a2713aSLionel Sambuc       BucketOffset = Generator.Emit(Out, Trait);
747f4a2713aSLionel Sambuc     }
748f4a2713aSLionel Sambuc 
749f4a2713aSLionel Sambuc     // Create a blob abbreviation
750f4a2713aSLionel Sambuc     BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
751f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_INDEX));
752f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
753f4a2713aSLionel Sambuc     Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
754f4a2713aSLionel Sambuc     unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
755f4a2713aSLionel Sambuc 
756f4a2713aSLionel Sambuc     // Write the identifier table
757f4a2713aSLionel Sambuc     Record.clear();
758f4a2713aSLionel Sambuc     Record.push_back(IDENTIFIER_INDEX);
759f4a2713aSLionel Sambuc     Record.push_back(BucketOffset);
760f4a2713aSLionel Sambuc     Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
761f4a2713aSLionel Sambuc   }
762f4a2713aSLionel Sambuc 
763f4a2713aSLionel Sambuc   Stream.ExitBlock();
764f4a2713aSLionel Sambuc }
765f4a2713aSLionel Sambuc 
766f4a2713aSLionel Sambuc GlobalModuleIndex::ErrorCode
writeIndex(FileManager & FileMgr,StringRef Path)767f4a2713aSLionel Sambuc GlobalModuleIndex::writeIndex(FileManager &FileMgr, StringRef Path) {
768f4a2713aSLionel Sambuc   llvm::SmallString<128> IndexPath;
769f4a2713aSLionel Sambuc   IndexPath += Path;
770f4a2713aSLionel Sambuc   llvm::sys::path::append(IndexPath, IndexFileName);
771f4a2713aSLionel Sambuc 
772f4a2713aSLionel Sambuc   // Coordinate building the global index file with other processes that might
773f4a2713aSLionel Sambuc   // try to do the same.
774f4a2713aSLionel Sambuc   llvm::LockFileManager Locked(IndexPath);
775f4a2713aSLionel Sambuc   switch (Locked) {
776f4a2713aSLionel Sambuc   case llvm::LockFileManager::LFS_Error:
777f4a2713aSLionel Sambuc     return EC_IOError;
778f4a2713aSLionel Sambuc 
779f4a2713aSLionel Sambuc   case llvm::LockFileManager::LFS_Owned:
780f4a2713aSLionel Sambuc     // We're responsible for building the index ourselves. Do so below.
781f4a2713aSLionel Sambuc     break;
782f4a2713aSLionel Sambuc 
783f4a2713aSLionel Sambuc   case llvm::LockFileManager::LFS_Shared:
784f4a2713aSLionel Sambuc     // Someone else is responsible for building the index. We don't care
785f4a2713aSLionel Sambuc     // when they finish, so we're done.
786f4a2713aSLionel Sambuc     return EC_Building;
787f4a2713aSLionel Sambuc   }
788f4a2713aSLionel Sambuc 
789f4a2713aSLionel Sambuc   // The module index builder.
790f4a2713aSLionel Sambuc   GlobalModuleIndexBuilder Builder(FileMgr);
791f4a2713aSLionel Sambuc 
792f4a2713aSLionel Sambuc   // Load each of the module files.
793*0a6a1f1dSLionel Sambuc   std::error_code EC;
794f4a2713aSLionel Sambuc   for (llvm::sys::fs::directory_iterator D(Path, EC), DEnd;
795f4a2713aSLionel Sambuc        D != DEnd && !EC;
796f4a2713aSLionel Sambuc        D.increment(EC)) {
797f4a2713aSLionel Sambuc     // If this isn't a module file, we don't care.
798f4a2713aSLionel Sambuc     if (llvm::sys::path::extension(D->path()) != ".pcm") {
799f4a2713aSLionel Sambuc       // ... unless it's a .pcm.lock file, which indicates that someone is
800f4a2713aSLionel Sambuc       // in the process of rebuilding a module. They'll rebuild the index
801f4a2713aSLionel Sambuc       // at the end of that translation unit, so we don't have to.
802f4a2713aSLionel Sambuc       if (llvm::sys::path::extension(D->path()) == ".pcm.lock")
803f4a2713aSLionel Sambuc         return EC_Building;
804f4a2713aSLionel Sambuc 
805f4a2713aSLionel Sambuc       continue;
806f4a2713aSLionel Sambuc     }
807f4a2713aSLionel Sambuc 
808f4a2713aSLionel Sambuc     // If we can't find the module file, skip it.
809f4a2713aSLionel Sambuc     const FileEntry *ModuleFile = FileMgr.getFile(D->path());
810f4a2713aSLionel Sambuc     if (!ModuleFile)
811f4a2713aSLionel Sambuc       continue;
812f4a2713aSLionel Sambuc 
813f4a2713aSLionel Sambuc     // Load this module file.
814f4a2713aSLionel Sambuc     if (Builder.loadModuleFile(ModuleFile))
815f4a2713aSLionel Sambuc       return EC_IOError;
816f4a2713aSLionel Sambuc   }
817f4a2713aSLionel Sambuc 
818f4a2713aSLionel Sambuc   // The output buffer, into which the global index will be written.
819f4a2713aSLionel Sambuc   SmallVector<char, 16> OutputBuffer;
820f4a2713aSLionel Sambuc   {
821f4a2713aSLionel Sambuc     llvm::BitstreamWriter OutputStream(OutputBuffer);
822f4a2713aSLionel Sambuc     Builder.writeIndex(OutputStream);
823f4a2713aSLionel Sambuc   }
824f4a2713aSLionel Sambuc 
825f4a2713aSLionel Sambuc   // Write the global index file to a temporary file.
826f4a2713aSLionel Sambuc   llvm::SmallString<128> IndexTmpPath;
827f4a2713aSLionel Sambuc   int TmpFD;
828f4a2713aSLionel Sambuc   if (llvm::sys::fs::createUniqueFile(IndexPath + "-%%%%%%%%", TmpFD,
829f4a2713aSLionel Sambuc                                       IndexTmpPath))
830f4a2713aSLionel Sambuc     return EC_IOError;
831f4a2713aSLionel Sambuc 
832f4a2713aSLionel Sambuc   // Open the temporary global index file for output.
833f4a2713aSLionel Sambuc   llvm::raw_fd_ostream Out(TmpFD, true);
834f4a2713aSLionel Sambuc   if (Out.has_error())
835f4a2713aSLionel Sambuc     return EC_IOError;
836f4a2713aSLionel Sambuc 
837f4a2713aSLionel Sambuc   // Write the index.
838f4a2713aSLionel Sambuc   Out.write(OutputBuffer.data(), OutputBuffer.size());
839f4a2713aSLionel Sambuc   Out.close();
840f4a2713aSLionel Sambuc   if (Out.has_error())
841f4a2713aSLionel Sambuc     return EC_IOError;
842f4a2713aSLionel Sambuc 
843f4a2713aSLionel Sambuc   // Remove the old index file. It isn't relevant any more.
844*0a6a1f1dSLionel Sambuc   llvm::sys::fs::remove(IndexPath.str());
845f4a2713aSLionel Sambuc 
846f4a2713aSLionel Sambuc   // Rename the newly-written index file to the proper name.
847f4a2713aSLionel Sambuc   if (llvm::sys::fs::rename(IndexTmpPath.str(), IndexPath.str())) {
848f4a2713aSLionel Sambuc     // Rename failed; just remove the
849*0a6a1f1dSLionel Sambuc     llvm::sys::fs::remove(IndexTmpPath.str());
850f4a2713aSLionel Sambuc     return EC_IOError;
851f4a2713aSLionel Sambuc   }
852f4a2713aSLionel Sambuc 
853f4a2713aSLionel Sambuc   // We're done.
854f4a2713aSLionel Sambuc   return EC_None;
855f4a2713aSLionel Sambuc }
856f4a2713aSLionel Sambuc 
857f4a2713aSLionel Sambuc namespace {
858f4a2713aSLionel Sambuc   class GlobalIndexIdentifierIterator : public IdentifierIterator {
859f4a2713aSLionel Sambuc     /// \brief The current position within the identifier lookup table.
860f4a2713aSLionel Sambuc     IdentifierIndexTable::key_iterator Current;
861f4a2713aSLionel Sambuc 
862f4a2713aSLionel Sambuc     /// \brief The end position within the identifier lookup table.
863f4a2713aSLionel Sambuc     IdentifierIndexTable::key_iterator End;
864f4a2713aSLionel Sambuc 
865f4a2713aSLionel Sambuc   public:
GlobalIndexIdentifierIterator(IdentifierIndexTable & Idx)866f4a2713aSLionel Sambuc     explicit GlobalIndexIdentifierIterator(IdentifierIndexTable &Idx) {
867f4a2713aSLionel Sambuc       Current = Idx.key_begin();
868f4a2713aSLionel Sambuc       End = Idx.key_end();
869f4a2713aSLionel Sambuc     }
870f4a2713aSLionel Sambuc 
Next()871*0a6a1f1dSLionel Sambuc     StringRef Next() override {
872f4a2713aSLionel Sambuc       if (Current == End)
873f4a2713aSLionel Sambuc         return StringRef();
874f4a2713aSLionel Sambuc 
875f4a2713aSLionel Sambuc       StringRef Result = *Current;
876f4a2713aSLionel Sambuc       ++Current;
877f4a2713aSLionel Sambuc       return Result;
878f4a2713aSLionel Sambuc     }
879f4a2713aSLionel Sambuc   };
880f4a2713aSLionel Sambuc }
881f4a2713aSLionel Sambuc 
createIdentifierIterator() const882f4a2713aSLionel Sambuc IdentifierIterator *GlobalModuleIndex::createIdentifierIterator() const {
883f4a2713aSLionel Sambuc   IdentifierIndexTable &Table =
884f4a2713aSLionel Sambuc     *static_cast<IdentifierIndexTable *>(IdentifierIndex);
885f4a2713aSLionel Sambuc   return new GlobalIndexIdentifierIterator(Table);
886f4a2713aSLionel Sambuc }
887