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