GlobalModuleIndex.cpp revision 188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15
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>
30using namespace clang;
31using namespace serialization;
32
33//----------------------------------------------------------------------------//
34// Shared constants
35//----------------------------------------------------------------------------//
36namespace {
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.
55static const char * const IndexFileName = "modules.idx";
56
57/// \brief The global index file version.
58static const unsigned CurrentVersion = 1;
59
60//----------------------------------------------------------------------------//
61// Global module index reader.
62//----------------------------------------------------------------------------//
63
64namespace {
65
66/// \brief Trait used to read the identifier index from the on-disk hash
67/// table.
68class IdentifierIndexReaderTrait {
69public:
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
116typedef OnDiskChainedHashTable<IdentifierIndexReaderTrait> IdentifierIndexTable;
117
118/// \brief Module information as it was loaded from the index file.
119struct LoadedModuleInfo {
120  const FileEntry *File;
121  SmallVector<unsigned, 2> Dependencies;
122  SmallVector<unsigned, 2> ImportedBy;
123};
124
125}
126
127GlobalModuleIndex::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
309GlobalModuleIndex::~GlobalModuleIndex() { }
310
311std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode>
312GlobalModuleIndex::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
342void 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
351void 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
372bool 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
401void 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
415namespace {
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
473static 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
488static 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
498void
499GlobalModuleIndexBuilder::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
515namespace {
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
536bool 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    // FIXME: Handle the selector table.
669
670    // We don't care about this record.
671  }
672
673  return false;
674}
675
676namespace {
677
678/// \brief Trait used to generate the identifier index as an on-disk hash
679/// table.
680class IdentifierIndexWriterTrait {
681public:
682  typedef StringRef key_type;
683  typedef StringRef key_type_ref;
684  typedef SmallVector<unsigned, 2> data_type;
685  typedef const SmallVector<unsigned, 2> &data_type_ref;
686
687  static unsigned ComputeHash(key_type_ref Key) {
688    return llvm::HashString(Key);
689  }
690
691  std::pair<unsigned,unsigned>
692  EmitKeyDataLength(raw_ostream& Out, key_type_ref Key, data_type_ref Data) {
693    unsigned KeyLen = Key.size();
694    unsigned DataLen = Data.size() * 4;
695    clang::io::Emit16(Out, KeyLen);
696    clang::io::Emit16(Out, DataLen);
697    return std::make_pair(KeyLen, DataLen);
698  }
699
700  void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
701    Out.write(Key.data(), KeyLen);
702  }
703
704  void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
705                unsigned DataLen) {
706    for (unsigned I = 0, N = Data.size(); I != N; ++I)
707      clang::io::Emit32(Out, Data[I]);
708  }
709};
710
711}
712
713void GlobalModuleIndexBuilder::writeIndex(llvm::BitstreamWriter &Stream) {
714  using namespace llvm;
715
716  // Emit the file header.
717  Stream.Emit((unsigned)'B', 8);
718  Stream.Emit((unsigned)'C', 8);
719  Stream.Emit((unsigned)'G', 8);
720  Stream.Emit((unsigned)'I', 8);
721
722  // Write the block-info block, which describes the records in this bitcode
723  // file.
724  emitBlockInfoBlock(Stream);
725
726  Stream.EnterSubblock(GLOBAL_INDEX_BLOCK_ID, 3);
727
728  // Write the metadata.
729  SmallVector<uint64_t, 2> Record;
730  Record.push_back(CurrentVersion);
731  Stream.EmitRecord(INDEX_METADATA, Record);
732
733  // Write the set of known module files.
734  for (ModuleFilesMap::iterator M = ModuleFiles.begin(),
735                                MEnd = ModuleFiles.end();
736       M != MEnd; ++M) {
737    Record.clear();
738    Record.push_back(M->second.ID);
739    Record.push_back(M->first->getSize());
740    Record.push_back(M->first->getModificationTime());
741
742    // File name
743    StringRef Name(M->first->getName());
744    Record.push_back(Name.size());
745    Record.append(Name.begin(), Name.end());
746
747    // Dependencies
748    Record.push_back(M->second.Dependencies.size());
749    Record.append(M->second.Dependencies.begin(), M->second.Dependencies.end());
750    Stream.EmitRecord(MODULE, Record);
751  }
752
753  // Write the identifier -> module file mapping.
754  {
755    OnDiskChainedHashTableGenerator<IdentifierIndexWriterTrait> Generator;
756    IdentifierIndexWriterTrait Trait;
757
758    // Populate the hash table.
759    for (InterestingIdentifierMap::iterator I = InterestingIdentifiers.begin(),
760                                            IEnd = InterestingIdentifiers.end();
761         I != IEnd; ++I) {
762      Generator.insert(I->first(), I->second, Trait);
763    }
764
765    // Create the on-disk hash table in a buffer.
766    SmallString<4096> IdentifierTable;
767    uint32_t BucketOffset;
768    {
769      llvm::raw_svector_ostream Out(IdentifierTable);
770      // Make sure that no bucket is at offset 0
771      clang::io::Emit32(Out, 0);
772      BucketOffset = Generator.Emit(Out, Trait);
773    }
774
775    // Create a blob abbreviation
776    BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
777    Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_INDEX));
778    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
779    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
780    unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
781
782    // Write the identifier table
783    Record.clear();
784    Record.push_back(IDENTIFIER_INDEX);
785    Record.push_back(BucketOffset);
786    Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
787  }
788
789  // FIXME: Selectors.
790
791  Stream.ExitBlock();
792}
793
794GlobalModuleIndex::ErrorCode
795GlobalModuleIndex::writeIndex(FileManager &FileMgr, StringRef Path) {
796  llvm::SmallString<128> IndexPath;
797  IndexPath += Path;
798  llvm::sys::path::append(IndexPath, IndexFileName);
799
800  // Coordinate building the global index file with other processes that might
801  // try to do the same.
802  llvm::LockFileManager Locked(IndexPath);
803  switch (Locked) {
804  case llvm::LockFileManager::LFS_Error:
805    return EC_IOError;
806
807  case llvm::LockFileManager::LFS_Owned:
808    // We're responsible for building the index ourselves. Do so below.
809    break;
810
811  case llvm::LockFileManager::LFS_Shared:
812    // Someone else is responsible for building the index. We don't care
813    // when they finish, so we're done.
814    return EC_Building;
815  }
816
817  // The module index builder.
818  GlobalModuleIndexBuilder Builder(FileMgr);
819
820  // Load each of the module files.
821  llvm::error_code EC;
822  for (llvm::sys::fs::directory_iterator D(Path, EC), DEnd;
823       D != DEnd && !EC;
824       D.increment(EC)) {
825    // If this isn't a module file, we don't care.
826    if (llvm::sys::path::extension(D->path()) != ".pcm") {
827      // ... unless it's a .pcm.lock file, which indicates that someone is
828      // in the process of rebuilding a module. They'll rebuild the index
829      // at the end of that translation unit, so we don't have to.
830      if (llvm::sys::path::extension(D->path()) == ".pcm.lock")
831        return EC_Building;
832
833      continue;
834    }
835
836    // If we can't find the module file, skip it.
837    const FileEntry *ModuleFile = FileMgr.getFile(D->path());
838    if (!ModuleFile)
839      continue;
840
841    // Load this module file.
842    if (Builder.loadModuleFile(ModuleFile))
843      return EC_IOError;
844  }
845
846  // The output buffer, into which the global index will be written.
847  SmallVector<char, 16> OutputBuffer;
848  {
849    llvm::BitstreamWriter OutputStream(OutputBuffer);
850    Builder.writeIndex(OutputStream);
851  }
852
853  // Write the global index file to a temporary file.
854  llvm::SmallString<128> IndexTmpPath;
855  int TmpFD;
856  if (llvm::sys::fs::unique_file(IndexPath + "-%%%%%%%%", TmpFD, IndexTmpPath))
857    return EC_IOError;
858
859  // Open the temporary global index file for output.
860  llvm::raw_fd_ostream Out(TmpFD, true);
861  if (Out.has_error())
862    return EC_IOError;
863
864  // Write the index.
865  Out.write(OutputBuffer.data(), OutputBuffer.size());
866  Out.close();
867  if (Out.has_error())
868    return EC_IOError;
869
870  // Remove the old index file. It isn't relevant any more.
871  bool OldIndexExisted;
872  llvm::sys::fs::remove(IndexPath.str(), OldIndexExisted);
873
874  // Rename the newly-written index file to the proper name.
875  if (llvm::sys::fs::rename(IndexTmpPath.str(), IndexPath.str())) {
876    // Rename failed; just remove the
877    llvm::sys::fs::remove(IndexTmpPath.str(), OldIndexExisted);
878    return EC_IOError;
879  }
880
881  // We're done.
882  return EC_None;
883}
884