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