blob: fb647b0be9bcc4e864fbb27e9722ddd275495aef [file] [log] [blame]
Douglas Gregora6b00fc2013-01-23 22:38:11 +00001//===--- 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"
Douglas Gregorfa69fc12013-03-22 18:50:14 +000019#include "clang/Serialization/Module.h"
Douglas Gregora6b00fc2013-01-23 22:38:11 +000020#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"
Douglas Gregor51001352013-01-23 22:45:24 +000026#include "llvm/Support/FileSystem.h"
Douglas Gregora6b00fc2013-01-23 22:38:11 +000027#include "llvm/Support/LockFileManager.h"
28#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola8229d222013-06-11 22:15:02 +000029#include "llvm/Support/Path.h"
NAKAMURA Takumi1d321332013-01-25 01:47:07 +000030#include <cstdio>
Douglas Gregora6b00fc2013-01-23 22:38:11 +000031using namespace clang;
32using namespace serialization;
33
34//----------------------------------------------------------------------------//
35// Shared constants
36//----------------------------------------------------------------------------//
37namespace {
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.
Douglas Gregor1a49d972013-01-25 01:03:03 +000047 INDEX_METADATA,
Douglas Gregora6b00fc2013-01-23 22:38:11 +000048 /// \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.
56static const char * const IndexFileName = "modules.idx";
57
58/// \brief The global index file version.
59static const unsigned CurrentVersion = 1;
60
61//----------------------------------------------------------------------------//
Douglas Gregor1a49d972013-01-25 01:03:03 +000062// Global module index reader.
63//----------------------------------------------------------------------------//
64
65namespace {
66
67/// \brief Trait used to read the identifier index from the on-disk hash
68/// table.
69class IdentifierIndexReaderTrait {
70public:
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
117typedef OnDiskChainedHashTable<IdentifierIndexReaderTrait> IdentifierIndexTable;
118
Douglas Gregor1a49d972013-01-25 01:03:03 +0000119}
120
Douglas Gregor677e15f2013-03-19 00:28:20 +0000121GlobalModuleIndex::GlobalModuleIndex(llvm::MemoryBuffer *Buffer,
Douglas Gregor1a49d972013-01-25 01:03:03 +0000122 llvm::BitstreamCursor Cursor)
Douglas Gregorfa69fc12013-03-22 18:50:14 +0000123 : Buffer(Buffer), IdentifierIndex(),
Douglas Gregor188bdcd2013-01-25 23:32:03 +0000124 NumIdentifierLookups(), NumIdentifierLookupHits()
Douglas Gregor1a49d972013-01-25 01:03:03 +0000125{
Douglas Gregor1a49d972013-01-25 01:03:03 +0000126 // Read the global index.
Douglas Gregor1a49d972013-01-25 01:03:03 +0000127 bool InGlobalIndexBlock = false;
128 bool Done = false;
Douglas Gregor1a49d972013-01-25 01:03:03 +0000129 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++];
Douglas Gregor677e15f2013-03-19 00:28:20 +0000176
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++];
Douglas Gregor1a49d972013-01-25 01:03:03 +0000187
188 // File name.
189 unsigned NameLen = Record[Idx++];
Douglas Gregor677e15f2013-03-19 00:28:20 +0000190 Modules[ID].FileName.assign(Record.begin() + Idx,
191 Record.begin() + Idx + NameLen);
Douglas Gregor1a49d972013-01-25 01:03:03 +0000192 Idx += NameLen;
193
194 // Dependencies
195 unsigned NumDeps = Record[Idx++];
Douglas Gregor677e15f2013-03-19 00:28:20 +0000196 Modules[ID].Dependencies.insert(Modules[ID].Dependencies.end(),
197 Record.begin() + Idx,
198 Record.begin() + Idx + NumDeps);
199 Idx += NumDeps;
Douglas Gregor1a49d972013-01-25 01:03:03 +0000200
Douglas Gregor677e15f2013-03-19 00:28:20 +0000201 // Make sure we're at the end of the record.
202 assert(Idx == Record.size() && "More module info?");
Douglas Gregorfa69fc12013-03-22 18:50:14 +0000203
204 // Record this module as an unresolved module.
205 UnresolvedModules[llvm::sys::path::stem(Modules[ID].FileName)] = ID;
Douglas Gregor1a49d972013-01-25 01:03:03 +0000206 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 }
Douglas Gregor1a49d972013-01-25 01:03:03 +0000220}
221
222GlobalModuleIndex::~GlobalModuleIndex() { }
223
224std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode>
Douglas Gregor677e15f2013-03-19 00:28:20 +0000225GlobalModuleIndex::readIndex(StringRef Path) {
Douglas Gregor1a49d972013-01-25 01:03:03 +0000226 // Load the index file, if it's there.
227 llvm::SmallString<128> IndexPath;
228 IndexPath += Path;
229 llvm::sys::path::append(IndexPath, IndexFileName);
230
Douglas Gregor677e15f2013-03-19 00:28:20 +0000231 llvm::OwningPtr<llvm::MemoryBuffer> Buffer;
Rafael Espindola28ce23a2013-10-25 19:00:49 +0000232 if (llvm::MemoryBuffer::getFile(IndexPath.c_str(), Buffer) !=
233 llvm::errc::success)
Douglas Gregor1a49d972013-01-25 01:03:03 +0000234 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
Douglas Gregor677e15f2013-03-19 00:28:20 +0000251 return std::make_pair(new GlobalModuleIndex(Buffer.take(), Cursor), EC_None);
Douglas Gregor1a49d972013-01-25 01:03:03 +0000252}
253
Douglas Gregor677e15f2013-03-19 00:28:20 +0000254void
255GlobalModuleIndex::getKnownModules(SmallVectorImpl<ModuleFile *> &ModuleFiles) {
Douglas Gregor1a49d972013-01-25 01:03:03 +0000256 ModuleFiles.clear();
257 for (unsigned I = 0, N = Modules.size(); I != N; ++I) {
Douglas Gregorfa69fc12013-03-22 18:50:14 +0000258 if (ModuleFile *MF = Modules[I].File)
259 ModuleFiles.push_back(MF);
Douglas Gregor1a49d972013-01-25 01:03:03 +0000260 }
261}
262
263void GlobalModuleIndex::getModuleDependencies(
Douglas Gregor677e15f2013-03-19 00:28:20 +0000264 ModuleFile *File,
265 SmallVectorImpl<ModuleFile *> &Dependencies) {
Douglas Gregor1a49d972013-01-25 01:03:03 +0000266 // Look for information about this module file.
Douglas Gregor677e15f2013-03-19 00:28:20 +0000267 llvm::DenseMap<ModuleFile *, unsigned>::iterator Known
268 = ModulesByFile.find(File);
Douglas Gregor1a49d972013-01-25 01:03:03 +0000269 if (Known == ModulesByFile.end())
270 return;
271
272 // Record dependencies.
Douglas Gregor677e15f2013-03-19 00:28:20 +0000273 Dependencies.clear();
274 ArrayRef<unsigned> StoredDependencies = Modules[Known->second].Dependencies;
275 for (unsigned I = 0, N = StoredDependencies.size(); I != N; ++I) {
Douglas Gregorfa69fc12013-03-22 18:50:14 +0000276 if (ModuleFile *MF = Modules[I].File)
Douglas Gregor677e15f2013-03-19 00:28:20 +0000277 Dependencies.push_back(MF);
278 }
Douglas Gregor1a49d972013-01-25 01:03:03 +0000279}
280
Douglas Gregor188bdcd2013-01-25 23:32:03 +0000281bool GlobalModuleIndex::lookupIdentifier(StringRef Name, HitSet &Hits) {
282 Hits.clear();
Douglas Gregor1a49d972013-01-25 01:03:03 +0000283
284 // If there's no identifier index, there is nothing we can do.
285 if (!IdentifierIndex)
286 return false;
287
288 // Look into the identifier index.
289 ++NumIdentifierLookups;
290 IdentifierIndexTable &Table
291 = *static_cast<IdentifierIndexTable *>(IdentifierIndex);
292 IdentifierIndexTable::iterator Known = Table.find(Name);
293 if (Known == Table.end()) {
294 return true;
295 }
296
297 SmallVector<unsigned, 2> ModuleIDs = *Known;
298 for (unsigned I = 0, N = ModuleIDs.size(); I != N; ++I) {
Douglas Gregorfa69fc12013-03-22 18:50:14 +0000299 if (ModuleFile *MF = Modules[ModuleIDs[I]].File)
300 Hits.insert(MF);
Douglas Gregor1a49d972013-01-25 01:03:03 +0000301 }
302
303 ++NumIdentifierLookupHits;
304 return true;
305}
306
Douglas Gregorfa69fc12013-03-22 18:50:14 +0000307bool GlobalModuleIndex::loadedModuleFile(ModuleFile *File) {
308 // Look for the module in the global module index based on the module name.
309 StringRef Name = llvm::sys::path::stem(File->FileName);
310 llvm::StringMap<unsigned>::iterator Known = UnresolvedModules.find(Name);
311 if (Known == UnresolvedModules.end()) {
312 return true;
Douglas Gregor677e15f2013-03-19 00:28:20 +0000313 }
314
Douglas Gregorfa69fc12013-03-22 18:50:14 +0000315 // Rectify this module with the global module index.
316 ModuleInfo &Info = Modules[Known->second];
317
318 // If the size and modification time match what we expected, record this
319 // module file.
320 bool Failed = true;
321 if (File->File->getSize() == Info.Size &&
322 File->File->getModificationTime() == Info.ModTime) {
323 Info.File = File;
324 ModulesByFile[File] = Known->second;
325
326 Failed = false;
Douglas Gregor677e15f2013-03-19 00:28:20 +0000327 }
328
Douglas Gregorfa69fc12013-03-22 18:50:14 +0000329 // One way or another, we have resolved this module file.
330 UnresolvedModules.erase(Known);
331 return Failed;
Douglas Gregor677e15f2013-03-19 00:28:20 +0000332}
333
Douglas Gregor1a49d972013-01-25 01:03:03 +0000334void GlobalModuleIndex::printStats() {
335 std::fprintf(stderr, "*** Global Module Index Statistics:\n");
336 if (NumIdentifierLookups) {
337 fprintf(stderr, " %u / %u identifier lookups succeeded (%f%%)\n",
338 NumIdentifierLookupHits, NumIdentifierLookups,
339 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
340 }
Douglas Gregor1a49d972013-01-25 01:03:03 +0000341 std::fprintf(stderr, "\n");
342}
343
344//----------------------------------------------------------------------------//
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000345// Global module index writer.
346//----------------------------------------------------------------------------//
347
348namespace {
349 /// \brief Provides information about a specific module file.
350 struct ModuleFileInfo {
351 /// \brief The numberic ID for this module file.
352 unsigned ID;
353
354 /// \brief The set of modules on which this module depends. Each entry is
355 /// a module ID.
356 SmallVector<unsigned, 4> Dependencies;
357 };
358
359 /// \brief Builder that generates the global module index file.
360 class GlobalModuleIndexBuilder {
361 FileManager &FileMgr;
362
363 /// \brief Mapping from files to module file information.
364 typedef llvm::MapVector<const FileEntry *, ModuleFileInfo> ModuleFilesMap;
365
366 /// \brief Information about each of the known module files.
367 ModuleFilesMap ModuleFiles;
368
369 /// \brief Mapping from identifiers to the list of module file IDs that
370 /// consider this identifier to be interesting.
371 typedef llvm::StringMap<SmallVector<unsigned, 2> > InterestingIdentifierMap;
372
373 /// \brief A mapping from all interesting identifiers to the set of module
374 /// files in which those identifiers are considered interesting.
375 InterestingIdentifierMap InterestingIdentifiers;
376
377 /// \brief Write the block-info block for the global module index file.
378 void emitBlockInfoBlock(llvm::BitstreamWriter &Stream);
379
380 /// \brief Retrieve the module file information for the given file.
381 ModuleFileInfo &getModuleFileInfo(const FileEntry *File) {
382 llvm::MapVector<const FileEntry *, ModuleFileInfo>::iterator Known
383 = ModuleFiles.find(File);
384 if (Known != ModuleFiles.end())
385 return Known->second;
386
387 unsigned NewID = ModuleFiles.size();
388 ModuleFileInfo &Info = ModuleFiles[File];
389 Info.ID = NewID;
390 return Info;
391 }
392
393 public:
394 explicit GlobalModuleIndexBuilder(FileManager &FileMgr) : FileMgr(FileMgr){}
395
396 /// \brief Load the contents of the given module file into the builder.
397 ///
398 /// \returns true if an error occurred, false otherwise.
399 bool loadModuleFile(const FileEntry *File);
400
401 /// \brief Write the index to the given bitstream.
402 void writeIndex(llvm::BitstreamWriter &Stream);
403 };
404}
405
406static void emitBlockID(unsigned ID, const char *Name,
407 llvm::BitstreamWriter &Stream,
408 SmallVectorImpl<uint64_t> &Record) {
409 Record.clear();
410 Record.push_back(ID);
411 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
412
413 // Emit the block name if present.
414 if (Name == 0 || Name[0] == 0) return;
415 Record.clear();
416 while (*Name)
417 Record.push_back(*Name++);
418 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
419}
420
421static void emitRecordID(unsigned ID, const char *Name,
422 llvm::BitstreamWriter &Stream,
423 SmallVectorImpl<uint64_t> &Record) {
424 Record.clear();
425 Record.push_back(ID);
426 while (*Name)
427 Record.push_back(*Name++);
428 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
429}
430
431void
432GlobalModuleIndexBuilder::emitBlockInfoBlock(llvm::BitstreamWriter &Stream) {
433 SmallVector<uint64_t, 64> Record;
434 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
435
436#define BLOCK(X) emitBlockID(X ## _ID, #X, Stream, Record)
437#define RECORD(X) emitRecordID(X, #X, Stream, Record)
438 BLOCK(GLOBAL_INDEX_BLOCK);
Douglas Gregor1a49d972013-01-25 01:03:03 +0000439 RECORD(INDEX_METADATA);
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000440 RECORD(MODULE);
441 RECORD(IDENTIFIER_INDEX);
442#undef RECORD
443#undef BLOCK
444
445 Stream.ExitBlock();
446}
447
Douglas Gregor1a49d972013-01-25 01:03:03 +0000448namespace {
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000449 class InterestingASTIdentifierLookupTrait
450 : public serialization::reader::ASTIdentifierLookupTraitBase {
451
452 public:
453 /// \brief The identifier and whether it is "interesting".
454 typedef std::pair<StringRef, bool> data_type;
455
456 data_type ReadData(const internal_key_type& k,
457 const unsigned char* d,
458 unsigned DataLen) {
459 // The first bit indicates whether this identifier is interesting.
460 // That's all we care about.
461 using namespace clang::io;
462 unsigned RawID = ReadUnalignedLE32(d);
463 bool IsInteresting = RawID & 0x01;
464 return std::make_pair(k, IsInteresting);
465 }
466 };
467}
468
469bool GlobalModuleIndexBuilder::loadModuleFile(const FileEntry *File) {
470 // Open the module file.
471 OwningPtr<llvm::MemoryBuffer> Buffer;
Douglas Gregor8e31d062013-02-06 18:08:37 +0000472 std::string ErrorStr;
473 Buffer.reset(FileMgr.getBufferForFile(File, &ErrorStr, /*isVolatile=*/true));
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000474 if (!Buffer) {
475 return true;
476 }
477
478 // Initialize the input stream
479 llvm::BitstreamReader InStreamFile;
480 llvm::BitstreamCursor InStream;
481 InStreamFile.init((const unsigned char *)Buffer->getBufferStart(),
482 (const unsigned char *)Buffer->getBufferEnd());
483 InStream.init(InStreamFile);
484
485 // Sniff for the signature.
486 if (InStream.Read(8) != 'C' ||
487 InStream.Read(8) != 'P' ||
488 InStream.Read(8) != 'C' ||
489 InStream.Read(8) != 'H') {
490 return true;
491 }
492
493 // Record this module file and assign it a unique ID (if it doesn't have
494 // one already).
495 unsigned ID = getModuleFileInfo(File).ID;
496
497 // Search for the blocks and records we care about.
Douglas Gregor1a49d972013-01-25 01:03:03 +0000498 enum { Other, ControlBlock, ASTBlock } State = Other;
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000499 bool Done = false;
500 while (!Done) {
Douglas Gregor1a49d972013-01-25 01:03:03 +0000501 llvm::BitstreamEntry Entry = InStream.advance();
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000502 switch (Entry.Kind) {
503 case llvm::BitstreamEntry::Error:
Douglas Gregor1a49d972013-01-25 01:03:03 +0000504 Done = true;
505 continue;
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000506
507 case llvm::BitstreamEntry::Record:
Douglas Gregor1a49d972013-01-25 01:03:03 +0000508 // In the 'other' state, just skip the record. We don't care.
509 if (State == Other) {
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000510 InStream.skipRecord(Entry.ID);
511 continue;
512 }
513
514 // Handle potentially-interesting records below.
515 break;
516
517 case llvm::BitstreamEntry::SubBlock:
Douglas Gregor1a49d972013-01-25 01:03:03 +0000518 if (Entry.ID == CONTROL_BLOCK_ID) {
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000519 if (InStream.EnterSubBlock(CONTROL_BLOCK_ID))
520 return true;
521
522 // Found the control block.
523 State = ControlBlock;
524 continue;
525 }
526
Douglas Gregor1a49d972013-01-25 01:03:03 +0000527 if (Entry.ID == AST_BLOCK_ID) {
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000528 if (InStream.EnterSubBlock(AST_BLOCK_ID))
529 return true;
530
531 // Found the AST block.
532 State = ASTBlock;
533 continue;
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000534 }
535
536 if (InStream.SkipBlock())
537 return true;
538
539 continue;
540
541 case llvm::BitstreamEntry::EndBlock:
Douglas Gregor1a49d972013-01-25 01:03:03 +0000542 State = Other;
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000543 continue;
544 }
545
546 // Read the given record.
547 SmallVector<uint64_t, 64> Record;
548 StringRef Blob;
549 unsigned Code = InStream.readRecord(Entry.ID, Record, &Blob);
550
551 // Handle module dependencies.
552 if (State == ControlBlock && Code == IMPORTS) {
553 // Load each of the imported PCH files.
554 unsigned Idx = 0, N = Record.size();
555 while (Idx < N) {
556 // Read information about the AST file.
557
558 // Skip the imported kind
559 ++Idx;
560
561 // Skip the import location
562 ++Idx;
563
Douglas Gregor677e15f2013-03-19 00:28:20 +0000564 // Load stored size/modification time.
565 off_t StoredSize = (off_t)Record[Idx++];
566 time_t StoredModTime = (time_t)Record[Idx++];
567
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000568 // Retrieve the imported file name.
569 unsigned Length = Record[Idx++];
570 SmallString<128> ImportedFile(Record.begin() + Idx,
571 Record.begin() + Idx + Length);
572 Idx += Length;
573
574 // Find the imported module file.
Douglas Gregorea14a872013-02-08 21:27:45 +0000575 const FileEntry *DependsOnFile
576 = FileMgr.getFile(ImportedFile, /*openFile=*/false,
577 /*cacheFailure=*/false);
Douglas Gregor677e15f2013-03-19 00:28:20 +0000578 if (!DependsOnFile ||
579 (StoredSize != DependsOnFile->getSize()) ||
580 (StoredModTime != DependsOnFile->getModificationTime()))
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000581 return true;
582
583 // Record the dependency.
584 unsigned DependsOnID = getModuleFileInfo(DependsOnFile).ID;
585 getModuleFileInfo(File).Dependencies.push_back(DependsOnID);
586 }
587
588 continue;
589 }
590
591 // Handle the identifier table
592 if (State == ASTBlock && Code == IDENTIFIER_TABLE && Record[0] > 0) {
593 typedef OnDiskChainedHashTable<InterestingASTIdentifierLookupTrait>
594 InterestingIdentifierTable;
595 llvm::OwningPtr<InterestingIdentifierTable>
596 Table(InterestingIdentifierTable::Create(
597 (const unsigned char *)Blob.data() + Record[0],
598 (const unsigned char *)Blob.data()));
599 for (InterestingIdentifierTable::data_iterator D = Table->data_begin(),
600 DEnd = Table->data_end();
601 D != DEnd; ++D) {
602 std::pair<StringRef, bool> Ident = *D;
603 if (Ident.second)
604 InterestingIdentifiers[Ident.first].push_back(ID);
Douglas Gregor1a49d972013-01-25 01:03:03 +0000605 else
606 (void)InterestingIdentifiers[Ident.first];
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000607 }
608 }
609
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000610 // We don't care about this record.
611 }
612
613 return false;
614}
615
616namespace {
617
618/// \brief Trait used to generate the identifier index as an on-disk hash
619/// table.
620class IdentifierIndexWriterTrait {
621public:
622 typedef StringRef key_type;
623 typedef StringRef key_type_ref;
624 typedef SmallVector<unsigned, 2> data_type;
625 typedef const SmallVector<unsigned, 2> &data_type_ref;
626
627 static unsigned ComputeHash(key_type_ref Key) {
628 return llvm::HashString(Key);
629 }
630
631 std::pair<unsigned,unsigned>
632 EmitKeyDataLength(raw_ostream& Out, key_type_ref Key, data_type_ref Data) {
633 unsigned KeyLen = Key.size();
634 unsigned DataLen = Data.size() * 4;
635 clang::io::Emit16(Out, KeyLen);
636 clang::io::Emit16(Out, DataLen);
637 return std::make_pair(KeyLen, DataLen);
638 }
639
640 void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
641 Out.write(Key.data(), KeyLen);
642 }
643
644 void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
645 unsigned DataLen) {
646 for (unsigned I = 0, N = Data.size(); I != N; ++I)
647 clang::io::Emit32(Out, Data[I]);
648 }
649};
650
651}
652
653void GlobalModuleIndexBuilder::writeIndex(llvm::BitstreamWriter &Stream) {
654 using namespace llvm;
655
656 // Emit the file header.
657 Stream.Emit((unsigned)'B', 8);
658 Stream.Emit((unsigned)'C', 8);
659 Stream.Emit((unsigned)'G', 8);
660 Stream.Emit((unsigned)'I', 8);
661
662 // Write the block-info block, which describes the records in this bitcode
663 // file.
664 emitBlockInfoBlock(Stream);
665
666 Stream.EnterSubblock(GLOBAL_INDEX_BLOCK_ID, 3);
667
668 // Write the metadata.
669 SmallVector<uint64_t, 2> Record;
670 Record.push_back(CurrentVersion);
Douglas Gregor1a49d972013-01-25 01:03:03 +0000671 Stream.EmitRecord(INDEX_METADATA, Record);
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000672
673 // Write the set of known module files.
674 for (ModuleFilesMap::iterator M = ModuleFiles.begin(),
675 MEnd = ModuleFiles.end();
676 M != MEnd; ++M) {
677 Record.clear();
678 Record.push_back(M->second.ID);
679 Record.push_back(M->first->getSize());
680 Record.push_back(M->first->getModificationTime());
681
682 // File name
683 StringRef Name(M->first->getName());
684 Record.push_back(Name.size());
685 Record.append(Name.begin(), Name.end());
686
687 // Dependencies
688 Record.push_back(M->second.Dependencies.size());
689 Record.append(M->second.Dependencies.begin(), M->second.Dependencies.end());
690 Stream.EmitRecord(MODULE, Record);
691 }
692
693 // Write the identifier -> module file mapping.
694 {
695 OnDiskChainedHashTableGenerator<IdentifierIndexWriterTrait> Generator;
696 IdentifierIndexWriterTrait Trait;
697
698 // Populate the hash table.
699 for (InterestingIdentifierMap::iterator I = InterestingIdentifiers.begin(),
700 IEnd = InterestingIdentifiers.end();
701 I != IEnd; ++I) {
702 Generator.insert(I->first(), I->second, Trait);
703 }
704
705 // Create the on-disk hash table in a buffer.
706 SmallString<4096> IdentifierTable;
707 uint32_t BucketOffset;
708 {
709 llvm::raw_svector_ostream Out(IdentifierTable);
710 // Make sure that no bucket is at offset 0
711 clang::io::Emit32(Out, 0);
712 BucketOffset = Generator.Emit(Out, Trait);
713 }
714
715 // Create a blob abbreviation
716 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
717 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_INDEX));
718 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
719 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
720 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
721
722 // Write the identifier table
723 Record.clear();
724 Record.push_back(IDENTIFIER_INDEX);
725 Record.push_back(BucketOffset);
726 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
727 }
728
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000729 Stream.ExitBlock();
730}
731
732GlobalModuleIndex::ErrorCode
733GlobalModuleIndex::writeIndex(FileManager &FileMgr, StringRef Path) {
734 llvm::SmallString<128> IndexPath;
735 IndexPath += Path;
736 llvm::sys::path::append(IndexPath, IndexFileName);
737
738 // Coordinate building the global index file with other processes that might
739 // try to do the same.
740 llvm::LockFileManager Locked(IndexPath);
741 switch (Locked) {
742 case llvm::LockFileManager::LFS_Error:
743 return EC_IOError;
744
745 case llvm::LockFileManager::LFS_Owned:
746 // We're responsible for building the index ourselves. Do so below.
747 break;
748
749 case llvm::LockFileManager::LFS_Shared:
750 // Someone else is responsible for building the index. We don't care
751 // when they finish, so we're done.
752 return EC_Building;
753 }
754
755 // The module index builder.
756 GlobalModuleIndexBuilder Builder(FileMgr);
757
758 // Load each of the module files.
759 llvm::error_code EC;
760 for (llvm::sys::fs::directory_iterator D(Path, EC), DEnd;
761 D != DEnd && !EC;
762 D.increment(EC)) {
763 // If this isn't a module file, we don't care.
764 if (llvm::sys::path::extension(D->path()) != ".pcm") {
765 // ... unless it's a .pcm.lock file, which indicates that someone is
766 // in the process of rebuilding a module. They'll rebuild the index
767 // at the end of that translation unit, so we don't have to.
768 if (llvm::sys::path::extension(D->path()) == ".pcm.lock")
769 return EC_Building;
770
771 continue;
772 }
773
774 // If we can't find the module file, skip it.
775 const FileEntry *ModuleFile = FileMgr.getFile(D->path());
776 if (!ModuleFile)
777 continue;
778
779 // Load this module file.
780 if (Builder.loadModuleFile(ModuleFile))
781 return EC_IOError;
782 }
783
784 // The output buffer, into which the global index will be written.
785 SmallVector<char, 16> OutputBuffer;
786 {
787 llvm::BitstreamWriter OutputStream(OutputBuffer);
788 Builder.writeIndex(OutputStream);
789 }
790
791 // Write the global index file to a temporary file.
792 llvm::SmallString<128> IndexTmpPath;
793 int TmpFD;
Rafael Espindola70e7aec2013-07-05 21:13:58 +0000794 if (llvm::sys::fs::createUniqueFile(IndexPath + "-%%%%%%%%", TmpFD,
795 IndexTmpPath))
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000796 return EC_IOError;
797
798 // Open the temporary global index file for output.
NAKAMURA Takumid2db16f2013-01-24 08:20:11 +0000799 llvm::raw_fd_ostream Out(TmpFD, true);
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000800 if (Out.has_error())
801 return EC_IOError;
802
803 // Write the index.
804 Out.write(OutputBuffer.data(), OutputBuffer.size());
805 Out.close();
806 if (Out.has_error())
807 return EC_IOError;
808
809 // Remove the old index file. It isn't relevant any more.
810 bool OldIndexExisted;
811 llvm::sys::fs::remove(IndexPath.str(), OldIndexExisted);
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(), OldIndexExisted);
817 return EC_IOError;
818 }
819
820 // We're done.
821 return EC_None;
822}
Argyrios Kyrtzidis87f9d812013-04-17 22:10:55 +0000823
824namespace {
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 virtual StringRef Next() {
839 if (Current == End)
840 return StringRef();
841
842 StringRef Result = *Current;
843 ++Current;
844 return Result;
845 }
846 };
847}
848
849IdentifierIterator *GlobalModuleIndex::createIdentifierIterator() const {
850 IdentifierIndexTable &Table =
851 *static_cast<IdentifierIndexTable *>(IdentifierIndex);
852 return new GlobalIndexIdentifierIterator(Table);
853}