15e306b12SDouglas Gregor //===--- GlobalModuleIndex.cpp - Global Module Index ------------*- C++ -*-===// 25e306b12SDouglas Gregor // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 65e306b12SDouglas Gregor // 75e306b12SDouglas Gregor //===----------------------------------------------------------------------===// 85e306b12SDouglas Gregor // 95e306b12SDouglas Gregor // This file implements the GlobalModuleIndex class. 105e306b12SDouglas Gregor // 115e306b12SDouglas Gregor //===----------------------------------------------------------------------===// 125e306b12SDouglas Gregor 13*d880de2dSAnton Afanasyev 145e306b12SDouglas Gregor #include "ASTReaderInternals.h" 155e306b12SDouglas Gregor #include "clang/Basic/FileManager.h" 16beee15e7SBen Langmuir #include "clang/Lex/HeaderSearch.h" 175e306b12SDouglas Gregor #include "clang/Serialization/ASTBitCodes.h" 185e306b12SDouglas Gregor #include "clang/Serialization/GlobalModuleIndex.h" 19603cd869SDouglas Gregor #include "clang/Serialization/Module.h" 20f3b0046bSRichard Trieu #include "clang/Serialization/PCHContainerOperations.h" 215e306b12SDouglas Gregor #include "llvm/ADT/DenseMap.h" 225e306b12SDouglas Gregor #include "llvm/ADT/MapVector.h" 235e306b12SDouglas Gregor #include "llvm/ADT/SmallString.h" 245e306b12SDouglas Gregor #include "llvm/Bitcode/BitstreamReader.h" 255e306b12SDouglas Gregor #include "llvm/Bitcode/BitstreamWriter.h" 26560ce2c7SJonas Devlieghere #include "llvm/Support/DJB.h" 278ec343ccSDouglas Gregor #include "llvm/Support/FileSystem.h" 285e306b12SDouglas Gregor #include "llvm/Support/LockFileManager.h" 295e306b12SDouglas Gregor #include "llvm/Support/MemoryBuffer.h" 30bb094f06SJustin Bogner #include "llvm/Support/OnDiskHashTable.h" 31552c169eSRafael Espindola #include "llvm/Support/Path.h" 32*d880de2dSAnton Afanasyev #include "llvm/Support/TimeProfiler.h" 33f0add23aSNAKAMURA Takumi #include <cstdio> 345e306b12SDouglas Gregor using namespace clang; 355e306b12SDouglas Gregor using namespace serialization; 365e306b12SDouglas Gregor 375e306b12SDouglas Gregor //----------------------------------------------------------------------------// 385e306b12SDouglas Gregor // Shared constants 395e306b12SDouglas Gregor //----------------------------------------------------------------------------// 405e306b12SDouglas Gregor namespace { 415e306b12SDouglas Gregor enum { 429fc8faf9SAdrian Prantl /// The block containing the index. 435e306b12SDouglas Gregor GLOBAL_INDEX_BLOCK_ID = llvm::bitc::FIRST_APPLICATION_BLOCKID 445e306b12SDouglas Gregor }; 455e306b12SDouglas Gregor 469fc8faf9SAdrian Prantl /// Describes the record types in the index. 475e306b12SDouglas Gregor enum IndexRecordTypes { 489fc8faf9SAdrian Prantl /// Contains version information and potentially other metadata, 495e306b12SDouglas Gregor /// used to determine if we can read this global index file. 50e060e57bSDouglas Gregor INDEX_METADATA, 519fc8faf9SAdrian Prantl /// Describes a module, including its file name and dependencies. 525e306b12SDouglas Gregor MODULE, 539fc8faf9SAdrian Prantl /// The index for identifiers. 545e306b12SDouglas Gregor IDENTIFIER_INDEX 555e306b12SDouglas Gregor }; 56ab9db510SAlexander Kornienko } 575e306b12SDouglas Gregor 589fc8faf9SAdrian Prantl /// The name of the global index file. 595e306b12SDouglas Gregor static const char * const IndexFileName = "modules.idx"; 605e306b12SDouglas Gregor 619fc8faf9SAdrian Prantl /// The global index file version. 625e306b12SDouglas Gregor static const unsigned CurrentVersion = 1; 635e306b12SDouglas Gregor 645e306b12SDouglas Gregor //----------------------------------------------------------------------------// 65e060e57bSDouglas Gregor // Global module index reader. 66e060e57bSDouglas Gregor //----------------------------------------------------------------------------// 67e060e57bSDouglas Gregor 68e060e57bSDouglas Gregor namespace { 69e060e57bSDouglas Gregor 709fc8faf9SAdrian Prantl /// Trait used to read the identifier index from the on-disk hash 71e060e57bSDouglas Gregor /// table. 72e060e57bSDouglas Gregor class IdentifierIndexReaderTrait { 73e060e57bSDouglas Gregor public: 74e060e57bSDouglas Gregor typedef StringRef external_key_type; 75e060e57bSDouglas Gregor typedef StringRef internal_key_type; 76e060e57bSDouglas Gregor typedef SmallVector<unsigned, 2> data_type; 7725463f15SJustin Bogner typedef unsigned hash_value_type; 7825463f15SJustin Bogner typedef unsigned offset_type; 79e060e57bSDouglas Gregor 80e060e57bSDouglas Gregor static bool EqualKey(const internal_key_type& a, const internal_key_type& b) { 81e060e57bSDouglas Gregor return a == b; 82e060e57bSDouglas Gregor } 83e060e57bSDouglas Gregor 8425463f15SJustin Bogner static hash_value_type ComputeHash(const internal_key_type& a) { 85560ce2c7SJonas Devlieghere return llvm::djbHash(a); 86e060e57bSDouglas Gregor } 87e060e57bSDouglas Gregor 88e060e57bSDouglas Gregor static std::pair<unsigned, unsigned> 89e060e57bSDouglas Gregor ReadKeyDataLength(const unsigned char*& d) { 9057ba0b22SJustin Bogner using namespace llvm::support; 9157ba0b22SJustin Bogner unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 9257ba0b22SJustin Bogner unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 93e060e57bSDouglas Gregor return std::make_pair(KeyLen, DataLen); 94e060e57bSDouglas Gregor } 95e060e57bSDouglas Gregor 96e060e57bSDouglas Gregor static const internal_key_type& 97e060e57bSDouglas Gregor GetInternalKey(const external_key_type& x) { return x; } 98e060e57bSDouglas Gregor 99e060e57bSDouglas Gregor static const external_key_type& 100e060e57bSDouglas Gregor GetExternalKey(const internal_key_type& x) { return x; } 101e060e57bSDouglas Gregor 102e060e57bSDouglas Gregor static internal_key_type ReadKey(const unsigned char* d, unsigned n) { 103e060e57bSDouglas Gregor return StringRef((const char *)d, n); 104e060e57bSDouglas Gregor } 105e060e57bSDouglas Gregor 106e060e57bSDouglas Gregor static data_type ReadData(const internal_key_type& k, 107e060e57bSDouglas Gregor const unsigned char* d, 108e060e57bSDouglas Gregor unsigned DataLen) { 10957ba0b22SJustin Bogner using namespace llvm::support; 110e060e57bSDouglas Gregor 111e060e57bSDouglas Gregor data_type Result; 112e060e57bSDouglas Gregor while (DataLen > 0) { 11357ba0b22SJustin Bogner unsigned ID = endian::readNext<uint32_t, little, unaligned>(d); 114e060e57bSDouglas Gregor Result.push_back(ID); 115e060e57bSDouglas Gregor DataLen -= 4; 116e060e57bSDouglas Gregor } 117e060e57bSDouglas Gregor 118e060e57bSDouglas Gregor return Result; 119e060e57bSDouglas Gregor } 120e060e57bSDouglas Gregor }; 121e060e57bSDouglas Gregor 122bb094f06SJustin Bogner typedef llvm::OnDiskIterableChainedHashTable<IdentifierIndexReaderTrait> 123da4e650eSJustin Bogner IdentifierIndexTable; 124e060e57bSDouglas Gregor 125ab9db510SAlexander Kornienko } 126e060e57bSDouglas Gregor 127afa10d3eSDavid Blaikie GlobalModuleIndex::GlobalModuleIndex(std::unique_ptr<llvm::MemoryBuffer> Buffer, 128e060e57bSDouglas Gregor llvm::BitstreamCursor Cursor) 129afa10d3eSDavid Blaikie : Buffer(std::move(Buffer)), IdentifierIndex(), NumIdentifierLookups(), 130afa10d3eSDavid Blaikie NumIdentifierLookupHits() { 131*d880de2dSAnton Afanasyev llvm::TimeTraceScope TimeScope("Module LoadIndex", StringRef("")); 132e060e57bSDouglas Gregor // Read the global index. 133e060e57bSDouglas Gregor bool InGlobalIndexBlock = false; 134e060e57bSDouglas Gregor bool Done = false; 135e060e57bSDouglas Gregor while (!Done) { 136e060e57bSDouglas Gregor llvm::BitstreamEntry Entry = Cursor.advance(); 137e060e57bSDouglas Gregor 138e060e57bSDouglas Gregor switch (Entry.Kind) { 139e060e57bSDouglas Gregor case llvm::BitstreamEntry::Error: 140e060e57bSDouglas Gregor return; 141e060e57bSDouglas Gregor 142e060e57bSDouglas Gregor case llvm::BitstreamEntry::EndBlock: 143e060e57bSDouglas Gregor if (InGlobalIndexBlock) { 144e060e57bSDouglas Gregor InGlobalIndexBlock = false; 145e060e57bSDouglas Gregor Done = true; 146e060e57bSDouglas Gregor continue; 147e060e57bSDouglas Gregor } 148e060e57bSDouglas Gregor return; 149e060e57bSDouglas Gregor 150e060e57bSDouglas Gregor 151e060e57bSDouglas Gregor case llvm::BitstreamEntry::Record: 152e060e57bSDouglas Gregor // Entries in the global index block are handled below. 153e060e57bSDouglas Gregor if (InGlobalIndexBlock) 154e060e57bSDouglas Gregor break; 155e060e57bSDouglas Gregor 156e060e57bSDouglas Gregor return; 157e060e57bSDouglas Gregor 158e060e57bSDouglas Gregor case llvm::BitstreamEntry::SubBlock: 159e060e57bSDouglas Gregor if (!InGlobalIndexBlock && Entry.ID == GLOBAL_INDEX_BLOCK_ID) { 160e060e57bSDouglas Gregor if (Cursor.EnterSubBlock(GLOBAL_INDEX_BLOCK_ID)) 161e060e57bSDouglas Gregor return; 162e060e57bSDouglas Gregor 163e060e57bSDouglas Gregor InGlobalIndexBlock = true; 164e060e57bSDouglas Gregor } else if (Cursor.SkipBlock()) { 165e060e57bSDouglas Gregor return; 166e060e57bSDouglas Gregor } 167e060e57bSDouglas Gregor continue; 168e060e57bSDouglas Gregor } 169e060e57bSDouglas Gregor 170e060e57bSDouglas Gregor SmallVector<uint64_t, 64> Record; 171e060e57bSDouglas Gregor StringRef Blob; 172e060e57bSDouglas Gregor switch ((IndexRecordTypes)Cursor.readRecord(Entry.ID, Record, &Blob)) { 173e060e57bSDouglas Gregor case INDEX_METADATA: 174e060e57bSDouglas Gregor // Make sure that the version matches. 175e060e57bSDouglas Gregor if (Record.size() < 1 || Record[0] != CurrentVersion) 176e060e57bSDouglas Gregor return; 177e060e57bSDouglas Gregor break; 178e060e57bSDouglas Gregor 179e060e57bSDouglas Gregor case MODULE: { 180e060e57bSDouglas Gregor unsigned Idx = 0; 181e060e57bSDouglas Gregor unsigned ID = Record[Idx++]; 182e060e57bSDouglas Gregor 1837029ce1aSDouglas Gregor // Make room for this module's information. 1847029ce1aSDouglas Gregor if (ID == Modules.size()) 1857029ce1aSDouglas Gregor Modules.push_back(ModuleInfo()); 1867029ce1aSDouglas Gregor else 1877029ce1aSDouglas Gregor Modules.resize(ID + 1); 1887029ce1aSDouglas Gregor 1897029ce1aSDouglas Gregor // Size/modification time for this module file at the time the 1907029ce1aSDouglas Gregor // global index was built. 1917029ce1aSDouglas Gregor Modules[ID].Size = Record[Idx++]; 1927029ce1aSDouglas Gregor Modules[ID].ModTime = Record[Idx++]; 193e060e57bSDouglas Gregor 194e060e57bSDouglas Gregor // File name. 195e060e57bSDouglas Gregor unsigned NameLen = Record[Idx++]; 1967029ce1aSDouglas Gregor Modules[ID].FileName.assign(Record.begin() + Idx, 197e060e57bSDouglas Gregor Record.begin() + Idx + NameLen); 198e060e57bSDouglas Gregor Idx += NameLen; 199e060e57bSDouglas Gregor 200e060e57bSDouglas Gregor // Dependencies 201e060e57bSDouglas Gregor unsigned NumDeps = Record[Idx++]; 2027029ce1aSDouglas Gregor Modules[ID].Dependencies.insert(Modules[ID].Dependencies.end(), 2037029ce1aSDouglas Gregor Record.begin() + Idx, 2047029ce1aSDouglas Gregor Record.begin() + Idx + NumDeps); 2057029ce1aSDouglas Gregor Idx += NumDeps; 206e060e57bSDouglas Gregor 2077029ce1aSDouglas Gregor // Make sure we're at the end of the record. 2087029ce1aSDouglas Gregor assert(Idx == Record.size() && "More module info?"); 209603cd869SDouglas Gregor 210603cd869SDouglas Gregor // Record this module as an unresolved module. 211beee15e7SBen Langmuir // FIXME: this doesn't work correctly for module names containing path 212beee15e7SBen Langmuir // separators. 213beee15e7SBen Langmuir StringRef ModuleName = llvm::sys::path::stem(Modules[ID].FileName); 214beee15e7SBen Langmuir // Remove the -<hash of ModuleMapPath> 215beee15e7SBen Langmuir ModuleName = ModuleName.rsplit('-').first; 216beee15e7SBen Langmuir UnresolvedModules[ModuleName] = ID; 217e060e57bSDouglas Gregor break; 218e060e57bSDouglas Gregor } 219e060e57bSDouglas Gregor 220e060e57bSDouglas Gregor case IDENTIFIER_INDEX: 221e060e57bSDouglas Gregor // Wire up the identifier index. 222e060e57bSDouglas Gregor if (Record[0]) { 223e060e57bSDouglas Gregor IdentifierIndex = IdentifierIndexTable::Create( 224e060e57bSDouglas Gregor (const unsigned char *)Blob.data() + Record[0], 225da4e650eSJustin Bogner (const unsigned char *)Blob.data() + sizeof(uint32_t), 226da4e650eSJustin Bogner (const unsigned char *)Blob.data(), IdentifierIndexReaderTrait()); 227e060e57bSDouglas Gregor } 228e060e57bSDouglas Gregor break; 229e060e57bSDouglas Gregor } 230e060e57bSDouglas Gregor } 231e060e57bSDouglas Gregor } 232e060e57bSDouglas Gregor 233e68b847fSNico Weber GlobalModuleIndex::~GlobalModuleIndex() { 234e68b847fSNico Weber delete static_cast<IdentifierIndexTable *>(IdentifierIndex); 235e68b847fSNico Weber } 236e060e57bSDouglas Gregor 237e060e57bSDouglas Gregor std::pair<GlobalModuleIndex *, GlobalModuleIndex::ErrorCode> 2387029ce1aSDouglas Gregor GlobalModuleIndex::readIndex(StringRef Path) { 239e060e57bSDouglas Gregor // Load the index file, if it's there. 240e060e57bSDouglas Gregor llvm::SmallString<128> IndexPath; 241e060e57bSDouglas Gregor IndexPath += Path; 242e060e57bSDouglas Gregor llvm::sys::path::append(IndexPath, IndexFileName); 243e060e57bSDouglas Gregor 2442d2b420aSRafael Espindola llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> BufferOrErr = 2452d2b420aSRafael Espindola llvm::MemoryBuffer::getFile(IndexPath.c_str()); 2462d2b420aSRafael Espindola if (!BufferOrErr) 247a13603a2SCraig Topper return std::make_pair(nullptr, EC_NotFound); 2482d2b420aSRafael Espindola std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(BufferOrErr.get()); 249e060e57bSDouglas Gregor 2509fc8faf9SAdrian Prantl /// The main bitstream cursor for the main block. 25177c89b69SPeter Collingbourne llvm::BitstreamCursor Cursor(*Buffer); 252e060e57bSDouglas Gregor 253e060e57bSDouglas Gregor // Sniff for the signature. 254e060e57bSDouglas Gregor if (Cursor.Read(8) != 'B' || 255e060e57bSDouglas Gregor Cursor.Read(8) != 'C' || 256e060e57bSDouglas Gregor Cursor.Read(8) != 'G' || 257e060e57bSDouglas Gregor Cursor.Read(8) != 'I') { 258a13603a2SCraig Topper return std::make_pair(nullptr, EC_IOError); 259e060e57bSDouglas Gregor } 260e060e57bSDouglas Gregor 261afa10d3eSDavid Blaikie return std::make_pair(new GlobalModuleIndex(std::move(Buffer), Cursor), 2629a16beb8SAhmed Charles EC_None); 263e060e57bSDouglas Gregor } 264e060e57bSDouglas Gregor 2657029ce1aSDouglas Gregor void 2667029ce1aSDouglas Gregor GlobalModuleIndex::getKnownModules(SmallVectorImpl<ModuleFile *> &ModuleFiles) { 267e060e57bSDouglas Gregor ModuleFiles.clear(); 268e060e57bSDouglas Gregor for (unsigned I = 0, N = Modules.size(); I != N; ++I) { 269603cd869SDouglas Gregor if (ModuleFile *MF = Modules[I].File) 270603cd869SDouglas Gregor ModuleFiles.push_back(MF); 271e060e57bSDouglas Gregor } 272e060e57bSDouglas Gregor } 273e060e57bSDouglas Gregor 274e060e57bSDouglas Gregor void GlobalModuleIndex::getModuleDependencies( 2757029ce1aSDouglas Gregor ModuleFile *File, 2767029ce1aSDouglas Gregor SmallVectorImpl<ModuleFile *> &Dependencies) { 277e060e57bSDouglas Gregor // Look for information about this module file. 2787029ce1aSDouglas Gregor llvm::DenseMap<ModuleFile *, unsigned>::iterator Known 2797029ce1aSDouglas Gregor = ModulesByFile.find(File); 280e060e57bSDouglas Gregor if (Known == ModulesByFile.end()) 281e060e57bSDouglas Gregor return; 282e060e57bSDouglas Gregor 283e060e57bSDouglas Gregor // Record dependencies. 2847029ce1aSDouglas Gregor Dependencies.clear(); 2857029ce1aSDouglas Gregor ArrayRef<unsigned> StoredDependencies = Modules[Known->second].Dependencies; 2867029ce1aSDouglas Gregor for (unsigned I = 0, N = StoredDependencies.size(); I != N; ++I) { 287603cd869SDouglas Gregor if (ModuleFile *MF = Modules[I].File) 2887029ce1aSDouglas Gregor Dependencies.push_back(MF); 2897029ce1aSDouglas Gregor } 290e060e57bSDouglas Gregor } 291e060e57bSDouglas Gregor 2927211ac15SDouglas Gregor bool GlobalModuleIndex::lookupIdentifier(StringRef Name, HitSet &Hits) { 2937211ac15SDouglas Gregor Hits.clear(); 294e060e57bSDouglas Gregor 295e060e57bSDouglas Gregor // If there's no identifier index, there is nothing we can do. 296e060e57bSDouglas Gregor if (!IdentifierIndex) 297e060e57bSDouglas Gregor return false; 298e060e57bSDouglas Gregor 299e060e57bSDouglas Gregor // Look into the identifier index. 300e060e57bSDouglas Gregor ++NumIdentifierLookups; 301e060e57bSDouglas Gregor IdentifierIndexTable &Table 302e060e57bSDouglas Gregor = *static_cast<IdentifierIndexTable *>(IdentifierIndex); 303e060e57bSDouglas Gregor IdentifierIndexTable::iterator Known = Table.find(Name); 304e060e57bSDouglas Gregor if (Known == Table.end()) { 305e060e57bSDouglas Gregor return true; 306e060e57bSDouglas Gregor } 307e060e57bSDouglas Gregor 308e060e57bSDouglas Gregor SmallVector<unsigned, 2> ModuleIDs = *Known; 309e060e57bSDouglas Gregor for (unsigned I = 0, N = ModuleIDs.size(); I != N; ++I) { 310603cd869SDouglas Gregor if (ModuleFile *MF = Modules[ModuleIDs[I]].File) 311603cd869SDouglas Gregor Hits.insert(MF); 312e060e57bSDouglas Gregor } 313e060e57bSDouglas Gregor 314e060e57bSDouglas Gregor ++NumIdentifierLookupHits; 315e060e57bSDouglas Gregor return true; 316e060e57bSDouglas Gregor } 317e060e57bSDouglas Gregor 318603cd869SDouglas Gregor bool GlobalModuleIndex::loadedModuleFile(ModuleFile *File) { 319603cd869SDouglas Gregor // Look for the module in the global module index based on the module name. 320beee15e7SBen Langmuir StringRef Name = File->ModuleName; 321603cd869SDouglas Gregor llvm::StringMap<unsigned>::iterator Known = UnresolvedModules.find(Name); 322603cd869SDouglas Gregor if (Known == UnresolvedModules.end()) { 323603cd869SDouglas Gregor return true; 3247029ce1aSDouglas Gregor } 3257029ce1aSDouglas Gregor 326603cd869SDouglas Gregor // Rectify this module with the global module index. 327603cd869SDouglas Gregor ModuleInfo &Info = Modules[Known->second]; 328603cd869SDouglas Gregor 329603cd869SDouglas Gregor // If the size and modification time match what we expected, record this 330603cd869SDouglas Gregor // module file. 331603cd869SDouglas Gregor bool Failed = true; 332603cd869SDouglas Gregor if (File->File->getSize() == Info.Size && 333603cd869SDouglas Gregor File->File->getModificationTime() == Info.ModTime) { 334603cd869SDouglas Gregor Info.File = File; 335603cd869SDouglas Gregor ModulesByFile[File] = Known->second; 336603cd869SDouglas Gregor 337603cd869SDouglas Gregor Failed = false; 3387029ce1aSDouglas Gregor } 3397029ce1aSDouglas Gregor 340603cd869SDouglas Gregor // One way or another, we have resolved this module file. 341603cd869SDouglas Gregor UnresolvedModules.erase(Known); 342603cd869SDouglas Gregor return Failed; 3437029ce1aSDouglas Gregor } 3447029ce1aSDouglas Gregor 345e060e57bSDouglas Gregor void GlobalModuleIndex::printStats() { 346e060e57bSDouglas Gregor std::fprintf(stderr, "*** Global Module Index Statistics:\n"); 347e060e57bSDouglas Gregor if (NumIdentifierLookups) { 348e060e57bSDouglas Gregor fprintf(stderr, " %u / %u identifier lookups succeeded (%f%%)\n", 349e060e57bSDouglas Gregor NumIdentifierLookupHits, NumIdentifierLookups, 350e060e57bSDouglas Gregor (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 351e060e57bSDouglas Gregor } 352e060e57bSDouglas Gregor std::fprintf(stderr, "\n"); 353e060e57bSDouglas Gregor } 354e060e57bSDouglas Gregor 355cdae941eSYaron Keren LLVM_DUMP_METHOD void GlobalModuleIndex::dump() { 356a39baf1aSJohn Thompson llvm::errs() << "*** Global Module Index Dump:\n"; 357a39baf1aSJohn Thompson llvm::errs() << "Module files:\n"; 3584f52d44dSJohn Thompson for (auto &MI : Modules) { 359a39baf1aSJohn Thompson llvm::errs() << "** " << MI.FileName << "\n"; 360a39baf1aSJohn Thompson if (MI.File) 361a39baf1aSJohn Thompson MI.File->dump(); 362bcdcc92eSJohn Thompson else 363a39baf1aSJohn Thompson llvm::errs() << "\n"; 364bcdcc92eSJohn Thompson } 365a39baf1aSJohn Thompson llvm::errs() << "\n"; 366bcdcc92eSJohn Thompson } 367bcdcc92eSJohn Thompson 368e060e57bSDouglas Gregor //----------------------------------------------------------------------------// 3695e306b12SDouglas Gregor // Global module index writer. 3705e306b12SDouglas Gregor //----------------------------------------------------------------------------// 3715e306b12SDouglas Gregor 3725e306b12SDouglas Gregor namespace { 3739fc8faf9SAdrian Prantl /// Provides information about a specific module file. 3745e306b12SDouglas Gregor struct ModuleFileInfo { 3759fc8faf9SAdrian Prantl /// The numberic ID for this module file. 3765e306b12SDouglas Gregor unsigned ID; 3775e306b12SDouglas Gregor 3789fc8faf9SAdrian Prantl /// The set of modules on which this module depends. Each entry is 3795e306b12SDouglas Gregor /// a module ID. 3805e306b12SDouglas Gregor SmallVector<unsigned, 4> Dependencies; 38160fa2888SDuncan P. N. Exon Smith ASTFileSignature Signature; 38260fa2888SDuncan P. N. Exon Smith }; 38360fa2888SDuncan P. N. Exon Smith 38460fa2888SDuncan P. N. Exon Smith struct ImportedModuleFileInfo { 38560fa2888SDuncan P. N. Exon Smith off_t StoredSize; 38660fa2888SDuncan P. N. Exon Smith time_t StoredModTime; 38760fa2888SDuncan P. N. Exon Smith ASTFileSignature StoredSignature; 38860fa2888SDuncan P. N. Exon Smith ImportedModuleFileInfo(off_t Size, time_t ModTime, ASTFileSignature Sig) 38960fa2888SDuncan P. N. Exon Smith : StoredSize(Size), StoredModTime(ModTime), StoredSignature(Sig) {} 3905e306b12SDouglas Gregor }; 3915e306b12SDouglas Gregor 3929fc8faf9SAdrian Prantl /// Builder that generates the global module index file. 3935e306b12SDouglas Gregor class GlobalModuleIndexBuilder { 3945e306b12SDouglas Gregor FileManager &FileMgr; 395fb2398d0SAdrian Prantl const PCHContainerReader &PCHContainerRdr; 3965e306b12SDouglas Gregor 39760fa2888SDuncan P. N. Exon Smith /// Mapping from files to module file information. 3985e306b12SDouglas Gregor typedef llvm::MapVector<const FileEntry *, ModuleFileInfo> ModuleFilesMap; 3995e306b12SDouglas Gregor 40060fa2888SDuncan P. N. Exon Smith /// Information about each of the known module files. 4015e306b12SDouglas Gregor ModuleFilesMap ModuleFiles; 4025e306b12SDouglas Gregor 4039fc8faf9SAdrian Prantl /// Mapping from the imported module file to the imported 40460fa2888SDuncan P. N. Exon Smith /// information. 40560fa2888SDuncan P. N. Exon Smith typedef std::multimap<const FileEntry *, ImportedModuleFileInfo> 40660fa2888SDuncan P. N. Exon Smith ImportedModuleFilesMap; 40760fa2888SDuncan P. N. Exon Smith 4089fc8faf9SAdrian Prantl /// Information about each importing of a module file. 40960fa2888SDuncan P. N. Exon Smith ImportedModuleFilesMap ImportedModuleFiles; 41060fa2888SDuncan P. N. Exon Smith 4119fc8faf9SAdrian Prantl /// Mapping from identifiers to the list of module file IDs that 4125e306b12SDouglas Gregor /// consider this identifier to be interesting. 4135e306b12SDouglas Gregor typedef llvm::StringMap<SmallVector<unsigned, 2> > InterestingIdentifierMap; 4145e306b12SDouglas Gregor 4159fc8faf9SAdrian Prantl /// A mapping from all interesting identifiers to the set of module 4165e306b12SDouglas Gregor /// files in which those identifiers are considered interesting. 4175e306b12SDouglas Gregor InterestingIdentifierMap InterestingIdentifiers; 4185e306b12SDouglas Gregor 4199fc8faf9SAdrian Prantl /// Write the block-info block for the global module index file. 4205e306b12SDouglas Gregor void emitBlockInfoBlock(llvm::BitstreamWriter &Stream); 4215e306b12SDouglas Gregor 4229fc8faf9SAdrian Prantl /// Retrieve the module file information for the given file. 4235e306b12SDouglas Gregor ModuleFileInfo &getModuleFileInfo(const FileEntry *File) { 4245e306b12SDouglas Gregor llvm::MapVector<const FileEntry *, ModuleFileInfo>::iterator Known 4255e306b12SDouglas Gregor = ModuleFiles.find(File); 4265e306b12SDouglas Gregor if (Known != ModuleFiles.end()) 4275e306b12SDouglas Gregor return Known->second; 4285e306b12SDouglas Gregor 4295e306b12SDouglas Gregor unsigned NewID = ModuleFiles.size(); 4305e306b12SDouglas Gregor ModuleFileInfo &Info = ModuleFiles[File]; 4315e306b12SDouglas Gregor Info.ID = NewID; 4325e306b12SDouglas Gregor return Info; 4335e306b12SDouglas Gregor } 4345e306b12SDouglas Gregor 4355e306b12SDouglas Gregor public: 436bb165fb0SAdrian Prantl explicit GlobalModuleIndexBuilder( 437fb2398d0SAdrian Prantl FileManager &FileMgr, const PCHContainerReader &PCHContainerRdr) 438fb2398d0SAdrian Prantl : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr) {} 4395e306b12SDouglas Gregor 4409fc8faf9SAdrian Prantl /// Load the contents of the given module file into the builder. 4415e306b12SDouglas Gregor /// 4425e306b12SDouglas Gregor /// \returns true if an error occurred, false otherwise. 4435e306b12SDouglas Gregor bool loadModuleFile(const FileEntry *File); 4445e306b12SDouglas Gregor 4459fc8faf9SAdrian Prantl /// Write the index to the given bitstream. 44660fa2888SDuncan P. N. Exon Smith /// \returns true if an error occurred, false otherwise. 44760fa2888SDuncan P. N. Exon Smith bool writeIndex(llvm::BitstreamWriter &Stream); 4485e306b12SDouglas Gregor }; 449ab9db510SAlexander Kornienko } 4505e306b12SDouglas Gregor 4515e306b12SDouglas Gregor static void emitBlockID(unsigned ID, const char *Name, 4525e306b12SDouglas Gregor llvm::BitstreamWriter &Stream, 4535e306b12SDouglas Gregor SmallVectorImpl<uint64_t> &Record) { 4545e306b12SDouglas Gregor Record.clear(); 4555e306b12SDouglas Gregor Record.push_back(ID); 4565e306b12SDouglas Gregor Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record); 4575e306b12SDouglas Gregor 4585e306b12SDouglas Gregor // Emit the block name if present. 459a13603a2SCraig Topper if (!Name || Name[0] == 0) return; 4605e306b12SDouglas Gregor Record.clear(); 4615e306b12SDouglas Gregor while (*Name) 4625e306b12SDouglas Gregor Record.push_back(*Name++); 4635e306b12SDouglas Gregor Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record); 4645e306b12SDouglas Gregor } 4655e306b12SDouglas Gregor 4665e306b12SDouglas Gregor static void emitRecordID(unsigned ID, const char *Name, 4675e306b12SDouglas Gregor llvm::BitstreamWriter &Stream, 4685e306b12SDouglas Gregor SmallVectorImpl<uint64_t> &Record) { 4695e306b12SDouglas Gregor Record.clear(); 4705e306b12SDouglas Gregor Record.push_back(ID); 4715e306b12SDouglas Gregor while (*Name) 4725e306b12SDouglas Gregor Record.push_back(*Name++); 4735e306b12SDouglas Gregor Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record); 4745e306b12SDouglas Gregor } 4755e306b12SDouglas Gregor 4765e306b12SDouglas Gregor void 4775e306b12SDouglas Gregor GlobalModuleIndexBuilder::emitBlockInfoBlock(llvm::BitstreamWriter &Stream) { 4785e306b12SDouglas Gregor SmallVector<uint64_t, 64> Record; 479d3a6c70bSPeter Collingbourne Stream.EnterBlockInfoBlock(); 4805e306b12SDouglas Gregor 4815e306b12SDouglas Gregor #define BLOCK(X) emitBlockID(X ## _ID, #X, Stream, Record) 4825e306b12SDouglas Gregor #define RECORD(X) emitRecordID(X, #X, Stream, Record) 4835e306b12SDouglas Gregor BLOCK(GLOBAL_INDEX_BLOCK); 484e060e57bSDouglas Gregor RECORD(INDEX_METADATA); 4855e306b12SDouglas Gregor RECORD(MODULE); 4865e306b12SDouglas Gregor RECORD(IDENTIFIER_INDEX); 4875e306b12SDouglas Gregor #undef RECORD 4885e306b12SDouglas Gregor #undef BLOCK 4895e306b12SDouglas Gregor 4905e306b12SDouglas Gregor Stream.ExitBlock(); 4915e306b12SDouglas Gregor } 4925e306b12SDouglas Gregor 493e060e57bSDouglas Gregor namespace { 4945e306b12SDouglas Gregor class InterestingASTIdentifierLookupTrait 4955e306b12SDouglas Gregor : public serialization::reader::ASTIdentifierLookupTraitBase { 4965e306b12SDouglas Gregor 4975e306b12SDouglas Gregor public: 4989fc8faf9SAdrian Prantl /// The identifier and whether it is "interesting". 4995e306b12SDouglas Gregor typedef std::pair<StringRef, bool> data_type; 5005e306b12SDouglas Gregor 5015e306b12SDouglas Gregor data_type ReadData(const internal_key_type& k, 5025e306b12SDouglas Gregor const unsigned char* d, 5035e306b12SDouglas Gregor unsigned DataLen) { 5045e306b12SDouglas Gregor // The first bit indicates whether this identifier is interesting. 5055e306b12SDouglas Gregor // That's all we care about. 50657ba0b22SJustin Bogner using namespace llvm::support; 50757ba0b22SJustin Bogner unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 5085e306b12SDouglas Gregor bool IsInteresting = RawID & 0x01; 5095e306b12SDouglas Gregor return std::make_pair(k, IsInteresting); 5105e306b12SDouglas Gregor } 5115e306b12SDouglas Gregor }; 512ab9db510SAlexander Kornienko } 5135e306b12SDouglas Gregor 5145e306b12SDouglas Gregor bool GlobalModuleIndexBuilder::loadModuleFile(const FileEntry *File) { 5155e306b12SDouglas Gregor // Open the module file. 5166406f7b8SRafael Espindola 517a885796dSBenjamin Kramer auto Buffer = FileMgr.getBufferForFile(File, /*isVolatile=*/true); 5185e306b12SDouglas Gregor if (!Buffer) { 5195e306b12SDouglas Gregor return true; 5205e306b12SDouglas Gregor } 5215e306b12SDouglas Gregor 5225e306b12SDouglas Gregor // Initialize the input stream 52377c89b69SPeter Collingbourne llvm::BitstreamCursor InStream(PCHContainerRdr.ExtractPCH(**Buffer)); 5245e306b12SDouglas Gregor 5255e306b12SDouglas Gregor // Sniff for the signature. 5265e306b12SDouglas Gregor if (InStream.Read(8) != 'C' || 5275e306b12SDouglas Gregor InStream.Read(8) != 'P' || 5285e306b12SDouglas Gregor InStream.Read(8) != 'C' || 5295e306b12SDouglas Gregor InStream.Read(8) != 'H') { 5305e306b12SDouglas Gregor return true; 5315e306b12SDouglas Gregor } 5325e306b12SDouglas Gregor 5335e306b12SDouglas Gregor // Record this module file and assign it a unique ID (if it doesn't have 5345e306b12SDouglas Gregor // one already). 5355e306b12SDouglas Gregor unsigned ID = getModuleFileInfo(File).ID; 5365e306b12SDouglas Gregor 5375e306b12SDouglas Gregor // Search for the blocks and records we care about. 53860fa2888SDuncan P. N. Exon Smith enum { Other, ControlBlock, ASTBlock, DiagnosticOptionsBlock } State = Other; 5395e306b12SDouglas Gregor bool Done = false; 5405e306b12SDouglas Gregor while (!Done) { 541e060e57bSDouglas Gregor llvm::BitstreamEntry Entry = InStream.advance(); 5425e306b12SDouglas Gregor switch (Entry.Kind) { 5435e306b12SDouglas Gregor case llvm::BitstreamEntry::Error: 544e060e57bSDouglas Gregor Done = true; 545e060e57bSDouglas Gregor continue; 5465e306b12SDouglas Gregor 5475e306b12SDouglas Gregor case llvm::BitstreamEntry::Record: 548e060e57bSDouglas Gregor // In the 'other' state, just skip the record. We don't care. 549e060e57bSDouglas Gregor if (State == Other) { 5505e306b12SDouglas Gregor InStream.skipRecord(Entry.ID); 5515e306b12SDouglas Gregor continue; 5525e306b12SDouglas Gregor } 5535e306b12SDouglas Gregor 5545e306b12SDouglas Gregor // Handle potentially-interesting records below. 5555e306b12SDouglas Gregor break; 5565e306b12SDouglas Gregor 5575e306b12SDouglas Gregor case llvm::BitstreamEntry::SubBlock: 558e060e57bSDouglas Gregor if (Entry.ID == CONTROL_BLOCK_ID) { 5595e306b12SDouglas Gregor if (InStream.EnterSubBlock(CONTROL_BLOCK_ID)) 5605e306b12SDouglas Gregor return true; 5615e306b12SDouglas Gregor 5625e306b12SDouglas Gregor // Found the control block. 5635e306b12SDouglas Gregor State = ControlBlock; 5645e306b12SDouglas Gregor continue; 5655e306b12SDouglas Gregor } 5665e306b12SDouglas Gregor 567e060e57bSDouglas Gregor if (Entry.ID == AST_BLOCK_ID) { 5685e306b12SDouglas Gregor if (InStream.EnterSubBlock(AST_BLOCK_ID)) 5695e306b12SDouglas Gregor return true; 5705e306b12SDouglas Gregor 5715e306b12SDouglas Gregor // Found the AST block. 5725e306b12SDouglas Gregor State = ASTBlock; 5735e306b12SDouglas Gregor continue; 5745e306b12SDouglas Gregor } 5755e306b12SDouglas Gregor 57660fa2888SDuncan P. N. Exon Smith if (Entry.ID == UNHASHED_CONTROL_BLOCK_ID) { 57760fa2888SDuncan P. N. Exon Smith if (InStream.EnterSubBlock(UNHASHED_CONTROL_BLOCK_ID)) 57860fa2888SDuncan P. N. Exon Smith return true; 57960fa2888SDuncan P. N. Exon Smith 58060fa2888SDuncan P. N. Exon Smith // Found the Diagnostic Options block. 58160fa2888SDuncan P. N. Exon Smith State = DiagnosticOptionsBlock; 58260fa2888SDuncan P. N. Exon Smith continue; 58360fa2888SDuncan P. N. Exon Smith } 58460fa2888SDuncan P. N. Exon Smith 5855e306b12SDouglas Gregor if (InStream.SkipBlock()) 5865e306b12SDouglas Gregor return true; 5875e306b12SDouglas Gregor 5885e306b12SDouglas Gregor continue; 5895e306b12SDouglas Gregor 5905e306b12SDouglas Gregor case llvm::BitstreamEntry::EndBlock: 591e060e57bSDouglas Gregor State = Other; 5925e306b12SDouglas Gregor continue; 5935e306b12SDouglas Gregor } 5945e306b12SDouglas Gregor 5955e306b12SDouglas Gregor // Read the given record. 5965e306b12SDouglas Gregor SmallVector<uint64_t, 64> Record; 5975e306b12SDouglas Gregor StringRef Blob; 5985e306b12SDouglas Gregor unsigned Code = InStream.readRecord(Entry.ID, Record, &Blob); 5995e306b12SDouglas Gregor 6005e306b12SDouglas Gregor // Handle module dependencies. 6015e306b12SDouglas Gregor if (State == ControlBlock && Code == IMPORTS) { 6025e306b12SDouglas Gregor // Load each of the imported PCH files. 6035e306b12SDouglas Gregor unsigned Idx = 0, N = Record.size(); 6045e306b12SDouglas Gregor while (Idx < N) { 6055e306b12SDouglas Gregor // Read information about the AST file. 6065e306b12SDouglas Gregor 6075e306b12SDouglas Gregor // Skip the imported kind 6085e306b12SDouglas Gregor ++Idx; 6095e306b12SDouglas Gregor 6105e306b12SDouglas Gregor // Skip the import location 6115e306b12SDouglas Gregor ++Idx; 6125e306b12SDouglas Gregor 6137029ce1aSDouglas Gregor // Load stored size/modification time. 6147029ce1aSDouglas Gregor off_t StoredSize = (off_t)Record[Idx++]; 6157029ce1aSDouglas Gregor time_t StoredModTime = (time_t)Record[Idx++]; 6167029ce1aSDouglas Gregor 617487ea14aSBen Langmuir // Skip the stored signature. 618487ea14aSBen Langmuir // FIXME: we could read the signature out of the import and validate it. 61960fa2888SDuncan P. N. Exon Smith ASTFileSignature StoredSignature = { 62060fa2888SDuncan P. N. Exon Smith {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++], 62160fa2888SDuncan P. N. Exon Smith (uint32_t)Record[Idx++], (uint32_t)Record[Idx++], 62260fa2888SDuncan P. N. Exon Smith (uint32_t)Record[Idx++]}}}; 623487ea14aSBen Langmuir 624d30446fdSBoris Kolpackov // Skip the module name (currently this is only used for prebuilt 625d30446fdSBoris Kolpackov // modules while here we are only dealing with cached). 626d30446fdSBoris Kolpackov Idx += Record[Idx] + 1; 627d30446fdSBoris Kolpackov 6285e306b12SDouglas Gregor // Retrieve the imported file name. 6295e306b12SDouglas Gregor unsigned Length = Record[Idx++]; 6305e306b12SDouglas Gregor SmallString<128> ImportedFile(Record.begin() + Idx, 6315e306b12SDouglas Gregor Record.begin() + Idx + Length); 6325e306b12SDouglas Gregor Idx += Length; 6335e306b12SDouglas Gregor 6345e306b12SDouglas Gregor // Find the imported module file. 635dadd85dcSDouglas Gregor const FileEntry *DependsOnFile 636dadd85dcSDouglas Gregor = FileMgr.getFile(ImportedFile, /*openFile=*/false, 637dadd85dcSDouglas Gregor /*cacheFailure=*/false); 63860fa2888SDuncan P. N. Exon Smith 63960fa2888SDuncan P. N. Exon Smith if (!DependsOnFile) 6405e306b12SDouglas Gregor return true; 6415e306b12SDouglas Gregor 64260fa2888SDuncan P. N. Exon Smith // Save the information in ImportedModuleFileInfo so we can verify after 64360fa2888SDuncan P. N. Exon Smith // loading all pcms. 64460fa2888SDuncan P. N. Exon Smith ImportedModuleFiles.insert(std::make_pair( 64560fa2888SDuncan P. N. Exon Smith DependsOnFile, ImportedModuleFileInfo(StoredSize, StoredModTime, 64660fa2888SDuncan P. N. Exon Smith StoredSignature))); 64760fa2888SDuncan P. N. Exon Smith 6485e306b12SDouglas Gregor // Record the dependency. 6495e306b12SDouglas Gregor unsigned DependsOnID = getModuleFileInfo(DependsOnFile).ID; 6505e306b12SDouglas Gregor getModuleFileInfo(File).Dependencies.push_back(DependsOnID); 6515e306b12SDouglas Gregor } 6525e306b12SDouglas Gregor 6535e306b12SDouglas Gregor continue; 6545e306b12SDouglas Gregor } 6555e306b12SDouglas Gregor 6565e306b12SDouglas Gregor // Handle the identifier table 6575e306b12SDouglas Gregor if (State == ASTBlock && Code == IDENTIFIER_TABLE && Record[0] > 0) { 658bb094f06SJustin Bogner typedef llvm::OnDiskIterableChainedHashTable< 659bb094f06SJustin Bogner InterestingASTIdentifierLookupTrait> InterestingIdentifierTable; 660b8984329SAhmed Charles std::unique_ptr<InterestingIdentifierTable> Table( 661b8984329SAhmed Charles InterestingIdentifierTable::Create( 6625e306b12SDouglas Gregor (const unsigned char *)Blob.data() + Record[0], 663da4e650eSJustin Bogner (const unsigned char *)Blob.data() + sizeof(uint32_t), 6645e306b12SDouglas Gregor (const unsigned char *)Blob.data())); 6655e306b12SDouglas Gregor for (InterestingIdentifierTable::data_iterator D = Table->data_begin(), 6665e306b12SDouglas Gregor DEnd = Table->data_end(); 6675e306b12SDouglas Gregor D != DEnd; ++D) { 6685e306b12SDouglas Gregor std::pair<StringRef, bool> Ident = *D; 6695e306b12SDouglas Gregor if (Ident.second) 6705e306b12SDouglas Gregor InterestingIdentifiers[Ident.first].push_back(ID); 671e060e57bSDouglas Gregor else 672e060e57bSDouglas Gregor (void)InterestingIdentifiers[Ident.first]; 6735e306b12SDouglas Gregor } 6745e306b12SDouglas Gregor } 6755e306b12SDouglas Gregor 67660fa2888SDuncan P. N. Exon Smith // Get Signature. 67760fa2888SDuncan P. N. Exon Smith if (State == DiagnosticOptionsBlock && Code == SIGNATURE) 67860fa2888SDuncan P. N. Exon Smith getModuleFileInfo(File).Signature = { 67960fa2888SDuncan P. N. Exon Smith {{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2], 68060fa2888SDuncan P. N. Exon Smith (uint32_t)Record[3], (uint32_t)Record[4]}}}; 68160fa2888SDuncan P. N. Exon Smith 6825e306b12SDouglas Gregor // We don't care about this record. 6835e306b12SDouglas Gregor } 6845e306b12SDouglas Gregor 6855e306b12SDouglas Gregor return false; 6865e306b12SDouglas Gregor } 6875e306b12SDouglas Gregor 6885e306b12SDouglas Gregor namespace { 6895e306b12SDouglas Gregor 6909fc8faf9SAdrian Prantl /// Trait used to generate the identifier index as an on-disk hash 6915e306b12SDouglas Gregor /// table. 6925e306b12SDouglas Gregor class IdentifierIndexWriterTrait { 6935e306b12SDouglas Gregor public: 6945e306b12SDouglas Gregor typedef StringRef key_type; 6955e306b12SDouglas Gregor typedef StringRef key_type_ref; 6965e306b12SDouglas Gregor typedef SmallVector<unsigned, 2> data_type; 6975e306b12SDouglas Gregor typedef const SmallVector<unsigned, 2> &data_type_ref; 69825463f15SJustin Bogner typedef unsigned hash_value_type; 69925463f15SJustin Bogner typedef unsigned offset_type; 7005e306b12SDouglas Gregor 70125463f15SJustin Bogner static hash_value_type ComputeHash(key_type_ref Key) { 702560ce2c7SJonas Devlieghere return llvm::djbHash(Key); 7035e306b12SDouglas Gregor } 7045e306b12SDouglas Gregor 7055e306b12SDouglas Gregor std::pair<unsigned,unsigned> 7065e306b12SDouglas Gregor EmitKeyDataLength(raw_ostream& Out, key_type_ref Key, data_type_ref Data) { 707e1c147c3SJustin Bogner using namespace llvm::support; 708e3f65297SPeter Collingbourne endian::Writer LE(Out, little); 7095e306b12SDouglas Gregor unsigned KeyLen = Key.size(); 7105e306b12SDouglas Gregor unsigned DataLen = Data.size() * 4; 711e1c147c3SJustin Bogner LE.write<uint16_t>(KeyLen); 712e1c147c3SJustin Bogner LE.write<uint16_t>(DataLen); 7135e306b12SDouglas Gregor return std::make_pair(KeyLen, DataLen); 7145e306b12SDouglas Gregor } 7155e306b12SDouglas Gregor 7165e306b12SDouglas Gregor void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) { 7175e306b12SDouglas Gregor Out.write(Key.data(), KeyLen); 7185e306b12SDouglas Gregor } 7195e306b12SDouglas Gregor 7205e306b12SDouglas Gregor void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data, 7215e306b12SDouglas Gregor unsigned DataLen) { 722e1c147c3SJustin Bogner using namespace llvm::support; 7235e306b12SDouglas Gregor for (unsigned I = 0, N = Data.size(); I != N; ++I) 724e3f65297SPeter Collingbourne endian::write<uint32_t>(Out, Data[I], little); 7255e306b12SDouglas Gregor } 7265e306b12SDouglas Gregor }; 7275e306b12SDouglas Gregor 728ab9db510SAlexander Kornienko } 7295e306b12SDouglas Gregor 73060fa2888SDuncan P. N. Exon Smith bool GlobalModuleIndexBuilder::writeIndex(llvm::BitstreamWriter &Stream) { 73160fa2888SDuncan P. N. Exon Smith for (auto MapEntry : ImportedModuleFiles) { 73260fa2888SDuncan P. N. Exon Smith auto *File = MapEntry.first; 73360fa2888SDuncan P. N. Exon Smith ImportedModuleFileInfo &Info = MapEntry.second; 73460fa2888SDuncan P. N. Exon Smith if (getModuleFileInfo(File).Signature) { 73560fa2888SDuncan P. N. Exon Smith if (getModuleFileInfo(File).Signature != Info.StoredSignature) 73660fa2888SDuncan P. N. Exon Smith // Verify Signature. 73760fa2888SDuncan P. N. Exon Smith return true; 73860fa2888SDuncan P. N. Exon Smith } else if (Info.StoredSize != File->getSize() || 73960fa2888SDuncan P. N. Exon Smith Info.StoredModTime != File->getModificationTime()) 74060fa2888SDuncan P. N. Exon Smith // Verify Size and ModTime. 74160fa2888SDuncan P. N. Exon Smith return true; 74260fa2888SDuncan P. N. Exon Smith } 74360fa2888SDuncan P. N. Exon Smith 7445e306b12SDouglas Gregor using namespace llvm; 745*d880de2dSAnton Afanasyev llvm::TimeTraceScope TimeScope("Module WriteIndex", StringRef("")); 7465e306b12SDouglas Gregor 7475e306b12SDouglas Gregor // Emit the file header. 7485e306b12SDouglas Gregor Stream.Emit((unsigned)'B', 8); 7495e306b12SDouglas Gregor Stream.Emit((unsigned)'C', 8); 7505e306b12SDouglas Gregor Stream.Emit((unsigned)'G', 8); 7515e306b12SDouglas Gregor Stream.Emit((unsigned)'I', 8); 7525e306b12SDouglas Gregor 7535e306b12SDouglas Gregor // Write the block-info block, which describes the records in this bitcode 7545e306b12SDouglas Gregor // file. 7555e306b12SDouglas Gregor emitBlockInfoBlock(Stream); 7565e306b12SDouglas Gregor 7575e306b12SDouglas Gregor Stream.EnterSubblock(GLOBAL_INDEX_BLOCK_ID, 3); 7585e306b12SDouglas Gregor 7595e306b12SDouglas Gregor // Write the metadata. 7605e306b12SDouglas Gregor SmallVector<uint64_t, 2> Record; 7615e306b12SDouglas Gregor Record.push_back(CurrentVersion); 762e060e57bSDouglas Gregor Stream.EmitRecord(INDEX_METADATA, Record); 7635e306b12SDouglas Gregor 7645e306b12SDouglas Gregor // Write the set of known module files. 7655e306b12SDouglas Gregor for (ModuleFilesMap::iterator M = ModuleFiles.begin(), 7665e306b12SDouglas Gregor MEnd = ModuleFiles.end(); 7675e306b12SDouglas Gregor M != MEnd; ++M) { 7685e306b12SDouglas Gregor Record.clear(); 7695e306b12SDouglas Gregor Record.push_back(M->second.ID); 7705e306b12SDouglas Gregor Record.push_back(M->first->getSize()); 7715e306b12SDouglas Gregor Record.push_back(M->first->getModificationTime()); 7725e306b12SDouglas Gregor 7735e306b12SDouglas Gregor // File name 7745e306b12SDouglas Gregor StringRef Name(M->first->getName()); 7755e306b12SDouglas Gregor Record.push_back(Name.size()); 7765e306b12SDouglas Gregor Record.append(Name.begin(), Name.end()); 7775e306b12SDouglas Gregor 7785e306b12SDouglas Gregor // Dependencies 7795e306b12SDouglas Gregor Record.push_back(M->second.Dependencies.size()); 7805e306b12SDouglas Gregor Record.append(M->second.Dependencies.begin(), M->second.Dependencies.end()); 7815e306b12SDouglas Gregor Stream.EmitRecord(MODULE, Record); 7825e306b12SDouglas Gregor } 7835e306b12SDouglas Gregor 7845e306b12SDouglas Gregor // Write the identifier -> module file mapping. 7855e306b12SDouglas Gregor { 786bb094f06SJustin Bogner llvm::OnDiskChainedHashTableGenerator<IdentifierIndexWriterTrait> Generator; 7875e306b12SDouglas Gregor IdentifierIndexWriterTrait Trait; 7885e306b12SDouglas Gregor 7895e306b12SDouglas Gregor // Populate the hash table. 7905e306b12SDouglas Gregor for (InterestingIdentifierMap::iterator I = InterestingIdentifiers.begin(), 7915e306b12SDouglas Gregor IEnd = InterestingIdentifiers.end(); 7925e306b12SDouglas Gregor I != IEnd; ++I) { 7935e306b12SDouglas Gregor Generator.insert(I->first(), I->second, Trait); 7945e306b12SDouglas Gregor } 7955e306b12SDouglas Gregor 7965e306b12SDouglas Gregor // Create the on-disk hash table in a buffer. 7975e306b12SDouglas Gregor SmallString<4096> IdentifierTable; 7985e306b12SDouglas Gregor uint32_t BucketOffset; 7995e306b12SDouglas Gregor { 800e1c147c3SJustin Bogner using namespace llvm::support; 8015e306b12SDouglas Gregor llvm::raw_svector_ostream Out(IdentifierTable); 8025e306b12SDouglas Gregor // Make sure that no bucket is at offset 0 803e3f65297SPeter Collingbourne endian::write<uint32_t>(Out, 0, little); 8045e306b12SDouglas Gregor BucketOffset = Generator.Emit(Out, Trait); 8055e306b12SDouglas Gregor } 8065e306b12SDouglas Gregor 8075e306b12SDouglas Gregor // Create a blob abbreviation 808b44f0bfbSDavid Blaikie auto Abbrev = std::make_shared<BitCodeAbbrev>(); 8095e306b12SDouglas Gregor Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_INDEX)); 8105e306b12SDouglas Gregor Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 8115e306b12SDouglas Gregor Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 812b44f0bfbSDavid Blaikie unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev)); 8135e306b12SDouglas Gregor 8145e306b12SDouglas Gregor // Write the identifier table 81557a41913SMehdi Amini uint64_t Record[] = {IDENTIFIER_INDEX, BucketOffset}; 81692e1b62dSYaron Keren Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable); 8175e306b12SDouglas Gregor } 8185e306b12SDouglas Gregor 8195e306b12SDouglas Gregor Stream.ExitBlock(); 82060fa2888SDuncan P. N. Exon Smith return false; 8215e306b12SDouglas Gregor } 8225e306b12SDouglas Gregor 8235e306b12SDouglas Gregor GlobalModuleIndex::ErrorCode 824bb165fb0SAdrian Prantl GlobalModuleIndex::writeIndex(FileManager &FileMgr, 825fb2398d0SAdrian Prantl const PCHContainerReader &PCHContainerRdr, 826bb165fb0SAdrian Prantl StringRef Path) { 8275e306b12SDouglas Gregor llvm::SmallString<128> IndexPath; 8285e306b12SDouglas Gregor IndexPath += Path; 8295e306b12SDouglas Gregor llvm::sys::path::append(IndexPath, IndexFileName); 8305e306b12SDouglas Gregor 8315e306b12SDouglas Gregor // Coordinate building the global index file with other processes that might 8325e306b12SDouglas Gregor // try to do the same. 8335e306b12SDouglas Gregor llvm::LockFileManager Locked(IndexPath); 8345e306b12SDouglas Gregor switch (Locked) { 8355e306b12SDouglas Gregor case llvm::LockFileManager::LFS_Error: 8365e306b12SDouglas Gregor return EC_IOError; 8375e306b12SDouglas Gregor 8385e306b12SDouglas Gregor case llvm::LockFileManager::LFS_Owned: 8395e306b12SDouglas Gregor // We're responsible for building the index ourselves. Do so below. 8405e306b12SDouglas Gregor break; 8415e306b12SDouglas Gregor 8425e306b12SDouglas Gregor case llvm::LockFileManager::LFS_Shared: 8435e306b12SDouglas Gregor // Someone else is responsible for building the index. We don't care 8445e306b12SDouglas Gregor // when they finish, so we're done. 8455e306b12SDouglas Gregor return EC_Building; 8465e306b12SDouglas Gregor } 8475e306b12SDouglas Gregor 8485e306b12SDouglas Gregor // The module index builder. 849fb2398d0SAdrian Prantl GlobalModuleIndexBuilder Builder(FileMgr, PCHContainerRdr); 8505e306b12SDouglas Gregor 8515e306b12SDouglas Gregor // Load each of the module files. 852c080917eSRafael Espindola std::error_code EC; 8535e306b12SDouglas Gregor for (llvm::sys::fs::directory_iterator D(Path, EC), DEnd; 8545e306b12SDouglas Gregor D != DEnd && !EC; 8555e306b12SDouglas Gregor D.increment(EC)) { 8565e306b12SDouglas Gregor // If this isn't a module file, we don't care. 8575e306b12SDouglas Gregor if (llvm::sys::path::extension(D->path()) != ".pcm") { 8585e306b12SDouglas Gregor // ... unless it's a .pcm.lock file, which indicates that someone is 8595e306b12SDouglas Gregor // in the process of rebuilding a module. They'll rebuild the index 8605e306b12SDouglas Gregor // at the end of that translation unit, so we don't have to. 8615e306b12SDouglas Gregor if (llvm::sys::path::extension(D->path()) == ".pcm.lock") 8625e306b12SDouglas Gregor return EC_Building; 8635e306b12SDouglas Gregor 8645e306b12SDouglas Gregor continue; 8655e306b12SDouglas Gregor } 8665e306b12SDouglas Gregor 8675e306b12SDouglas Gregor // If we can't find the module file, skip it. 8685e306b12SDouglas Gregor const FileEntry *ModuleFile = FileMgr.getFile(D->path()); 8695e306b12SDouglas Gregor if (!ModuleFile) 8705e306b12SDouglas Gregor continue; 8715e306b12SDouglas Gregor 8725e306b12SDouglas Gregor // Load this module file. 8735e306b12SDouglas Gregor if (Builder.loadModuleFile(ModuleFile)) 8745e306b12SDouglas Gregor return EC_IOError; 8755e306b12SDouglas Gregor } 8765e306b12SDouglas Gregor 8775e306b12SDouglas Gregor // The output buffer, into which the global index will be written. 8785e306b12SDouglas Gregor SmallVector<char, 16> OutputBuffer; 8795e306b12SDouglas Gregor { 8805e306b12SDouglas Gregor llvm::BitstreamWriter OutputStream(OutputBuffer); 88160fa2888SDuncan P. N. Exon Smith if (Builder.writeIndex(OutputStream)) 88260fa2888SDuncan P. N. Exon Smith return EC_IOError; 8835e306b12SDouglas Gregor } 8845e306b12SDouglas Gregor 8855e306b12SDouglas Gregor // Write the global index file to a temporary file. 8865e306b12SDouglas Gregor llvm::SmallString<128> IndexTmpPath; 8875e306b12SDouglas Gregor int TmpFD; 88818627115SRafael Espindola if (llvm::sys::fs::createUniqueFile(IndexPath + "-%%%%%%%%", TmpFD, 88918627115SRafael Espindola IndexTmpPath)) 8905e306b12SDouglas Gregor return EC_IOError; 8915e306b12SDouglas Gregor 8925e306b12SDouglas Gregor // Open the temporary global index file for output. 893e00c9868SNAKAMURA Takumi llvm::raw_fd_ostream Out(TmpFD, true); 8945e306b12SDouglas Gregor if (Out.has_error()) 8955e306b12SDouglas Gregor return EC_IOError; 8965e306b12SDouglas Gregor 8975e306b12SDouglas Gregor // Write the index. 8985e306b12SDouglas Gregor Out.write(OutputBuffer.data(), OutputBuffer.size()); 8995e306b12SDouglas Gregor Out.close(); 9005e306b12SDouglas Gregor if (Out.has_error()) 9015e306b12SDouglas Gregor return EC_IOError; 9025e306b12SDouglas Gregor 9035e306b12SDouglas Gregor // Remove the old index file. It isn't relevant any more. 90492e1b62dSYaron Keren llvm::sys::fs::remove(IndexPath); 9055e306b12SDouglas Gregor 9065e306b12SDouglas Gregor // Rename the newly-written index file to the proper name. 90792e1b62dSYaron Keren if (llvm::sys::fs::rename(IndexTmpPath, IndexPath)) { 9085e306b12SDouglas Gregor // Rename failed; just remove the 90992e1b62dSYaron Keren llvm::sys::fs::remove(IndexTmpPath); 9105e306b12SDouglas Gregor return EC_IOError; 9115e306b12SDouglas Gregor } 9125e306b12SDouglas Gregor 9135e306b12SDouglas Gregor // We're done. 9145e306b12SDouglas Gregor return EC_None; 9155e306b12SDouglas Gregor } 9169aca3c61SArgyrios Kyrtzidis 9179aca3c61SArgyrios Kyrtzidis namespace { 9189aca3c61SArgyrios Kyrtzidis class GlobalIndexIdentifierIterator : public IdentifierIterator { 9199fc8faf9SAdrian Prantl /// The current position within the identifier lookup table. 9209aca3c61SArgyrios Kyrtzidis IdentifierIndexTable::key_iterator Current; 9219aca3c61SArgyrios Kyrtzidis 9229fc8faf9SAdrian Prantl /// The end position within the identifier lookup table. 9239aca3c61SArgyrios Kyrtzidis IdentifierIndexTable::key_iterator End; 9249aca3c61SArgyrios Kyrtzidis 9259aca3c61SArgyrios Kyrtzidis public: 9269aca3c61SArgyrios Kyrtzidis explicit GlobalIndexIdentifierIterator(IdentifierIndexTable &Idx) { 9279aca3c61SArgyrios Kyrtzidis Current = Idx.key_begin(); 9289aca3c61SArgyrios Kyrtzidis End = Idx.key_end(); 9299aca3c61SArgyrios Kyrtzidis } 9309aca3c61SArgyrios Kyrtzidis 9313e89dfeeSCraig Topper StringRef Next() override { 9329aca3c61SArgyrios Kyrtzidis if (Current == End) 9339aca3c61SArgyrios Kyrtzidis return StringRef(); 9349aca3c61SArgyrios Kyrtzidis 9359aca3c61SArgyrios Kyrtzidis StringRef Result = *Current; 9369aca3c61SArgyrios Kyrtzidis ++Current; 9379aca3c61SArgyrios Kyrtzidis return Result; 9389aca3c61SArgyrios Kyrtzidis } 9399aca3c61SArgyrios Kyrtzidis }; 940ab9db510SAlexander Kornienko } 9419aca3c61SArgyrios Kyrtzidis 9429aca3c61SArgyrios Kyrtzidis IdentifierIterator *GlobalModuleIndex::createIdentifierIterator() const { 9439aca3c61SArgyrios Kyrtzidis IdentifierIndexTable &Table = 9449aca3c61SArgyrios Kyrtzidis *static_cast<IdentifierIndexTable *>(IdentifierIndex); 9459aca3c61SArgyrios Kyrtzidis return new GlobalIndexIdentifierIterator(Table); 9469aca3c61SArgyrios Kyrtzidis } 947