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 13f7170d17SDuncan P. N. Exon Smith #include "clang/Serialization/GlobalModuleIndex.h" 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" 18f7170d17SDuncan P. N. Exon Smith #include "clang/Serialization/ModuleFile.h" 19f3b0046bSRichard Trieu #include "clang/Serialization/PCHContainerOperations.h" 205e306b12SDouglas Gregor #include "llvm/ADT/DenseMap.h" 215e306b12SDouglas Gregor #include "llvm/ADT/MapVector.h" 225e306b12SDouglas Gregor #include "llvm/ADT/SmallString.h" 23f69c9178SJan Korous #include "llvm/ADT/StringRef.h" 24e0308279SFrancis Visoiu Mistrih #include "llvm/Bitstream/BitstreamReader.h" 25e0308279SFrancis Visoiu Mistrih #include "llvm/Bitstream/BitstreamWriter.h" 26560ce2c7SJonas Devlieghere #include "llvm/Support/DJB.h" 278ec343ccSDouglas Gregor #include "llvm/Support/FileSystem.h" 28f69c9178SJan Korous #include "llvm/Support/FileUtilities.h" 295e306b12SDouglas Gregor #include "llvm/Support/LockFileManager.h" 305e306b12SDouglas Gregor #include "llvm/Support/MemoryBuffer.h" 31bb094f06SJustin Bogner #include "llvm/Support/OnDiskHashTable.h" 32552c169eSRafael Espindola #include "llvm/Support/Path.h" 33d880de2dSAnton Afanasyev #include "llvm/Support/TimeProfiler.h" 34f0add23aSNAKAMURA Takumi #include <cstdio> 355e306b12SDouglas Gregor using namespace clang; 365e306b12SDouglas Gregor using namespace serialization; 375e306b12SDouglas Gregor 385e306b12SDouglas Gregor //----------------------------------------------------------------------------// 395e306b12SDouglas Gregor // Shared constants 405e306b12SDouglas Gregor //----------------------------------------------------------------------------// 415e306b12SDouglas Gregor namespace { 425e306b12SDouglas Gregor enum { 439fc8faf9SAdrian Prantl /// The block containing the index. 445e306b12SDouglas Gregor GLOBAL_INDEX_BLOCK_ID = llvm::bitc::FIRST_APPLICATION_BLOCKID 455e306b12SDouglas Gregor }; 465e306b12SDouglas Gregor 479fc8faf9SAdrian Prantl /// Describes the record types in the index. 485e306b12SDouglas Gregor enum IndexRecordTypes { 499fc8faf9SAdrian Prantl /// Contains version information and potentially other metadata, 505e306b12SDouglas Gregor /// used to determine if we can read this global index file. 51e060e57bSDouglas Gregor INDEX_METADATA, 529fc8faf9SAdrian Prantl /// Describes a module, including its file name and dependencies. 535e306b12SDouglas Gregor MODULE, 549fc8faf9SAdrian Prantl /// The index for identifiers. 555e306b12SDouglas Gregor IDENTIFIER_INDEX 565e306b12SDouglas Gregor }; 57ab9db510SAlexander Kornienko } 585e306b12SDouglas Gregor 599fc8faf9SAdrian Prantl /// The name of the global index file. 605e306b12SDouglas Gregor static const char * const IndexFileName = "modules.idx"; 615e306b12SDouglas Gregor 629fc8faf9SAdrian Prantl /// The global index file version. 635e306b12SDouglas Gregor static const unsigned CurrentVersion = 1; 645e306b12SDouglas Gregor 655e306b12SDouglas Gregor //----------------------------------------------------------------------------// 66e060e57bSDouglas Gregor // Global module index reader. 67e060e57bSDouglas Gregor //----------------------------------------------------------------------------// 68e060e57bSDouglas Gregor 69e060e57bSDouglas Gregor namespace { 70e060e57bSDouglas Gregor 719fc8faf9SAdrian Prantl /// Trait used to read the identifier index from the on-disk hash 72e060e57bSDouglas Gregor /// table. 73e060e57bSDouglas Gregor class IdentifierIndexReaderTrait { 74e060e57bSDouglas Gregor public: 75e060e57bSDouglas Gregor typedef StringRef external_key_type; 76e060e57bSDouglas Gregor typedef StringRef internal_key_type; 77e060e57bSDouglas Gregor typedef SmallVector<unsigned, 2> data_type; 7825463f15SJustin Bogner typedef unsigned hash_value_type; 7925463f15SJustin Bogner typedef unsigned offset_type; 80e060e57bSDouglas Gregor 81e060e57bSDouglas Gregor static bool EqualKey(const internal_key_type& a, const internal_key_type& b) { 82e060e57bSDouglas Gregor return a == b; 83e060e57bSDouglas Gregor } 84e060e57bSDouglas Gregor 8525463f15SJustin Bogner static hash_value_type ComputeHash(const internal_key_type& a) { 86560ce2c7SJonas Devlieghere return llvm::djbHash(a); 87e060e57bSDouglas Gregor } 88e060e57bSDouglas Gregor 89e060e57bSDouglas Gregor static std::pair<unsigned, unsigned> 90e060e57bSDouglas Gregor ReadKeyDataLength(const unsigned char*& d) { 9157ba0b22SJustin Bogner using namespace llvm::support; 9257ba0b22SJustin Bogner unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); 9357ba0b22SJustin Bogner unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); 94e060e57bSDouglas Gregor return std::make_pair(KeyLen, DataLen); 95e060e57bSDouglas Gregor } 96e060e57bSDouglas Gregor 97e060e57bSDouglas Gregor static const internal_key_type& 98e060e57bSDouglas Gregor GetInternalKey(const external_key_type& x) { return x; } 99e060e57bSDouglas Gregor 100e060e57bSDouglas Gregor static const external_key_type& 101e060e57bSDouglas Gregor GetExternalKey(const internal_key_type& x) { return x; } 102e060e57bSDouglas Gregor 103e060e57bSDouglas Gregor static internal_key_type ReadKey(const unsigned char* d, unsigned n) { 104e060e57bSDouglas Gregor return StringRef((const char *)d, n); 105e060e57bSDouglas Gregor } 106e060e57bSDouglas Gregor 107e060e57bSDouglas Gregor static data_type ReadData(const internal_key_type& k, 108e060e57bSDouglas Gregor const unsigned char* d, 109e060e57bSDouglas Gregor unsigned DataLen) { 11057ba0b22SJustin Bogner using namespace llvm::support; 111e060e57bSDouglas Gregor 112e060e57bSDouglas Gregor data_type Result; 113e060e57bSDouglas Gregor while (DataLen > 0) { 11457ba0b22SJustin Bogner unsigned ID = endian::readNext<uint32_t, little, unaligned>(d); 115e060e57bSDouglas Gregor Result.push_back(ID); 116e060e57bSDouglas Gregor DataLen -= 4; 117e060e57bSDouglas Gregor } 118e060e57bSDouglas Gregor 119e060e57bSDouglas Gregor return Result; 120e060e57bSDouglas Gregor } 121e060e57bSDouglas Gregor }; 122e060e57bSDouglas Gregor 123bb094f06SJustin Bogner typedef llvm::OnDiskIterableChainedHashTable<IdentifierIndexReaderTrait> 124da4e650eSJustin Bogner IdentifierIndexTable; 125e060e57bSDouglas Gregor 126ab9db510SAlexander Kornienko } 127e060e57bSDouglas Gregor 128fce887beSSimon Pilgrim GlobalModuleIndex::GlobalModuleIndex( 129fce887beSSimon Pilgrim std::unique_ptr<llvm::MemoryBuffer> IndexBuffer, 130e060e57bSDouglas Gregor llvm::BitstreamCursor Cursor) 131fce887beSSimon Pilgrim : Buffer(std::move(IndexBuffer)), IdentifierIndex(), NumIdentifierLookups(), 132afa10d3eSDavid Blaikie NumIdentifierLookupHits() { 133fce887beSSimon Pilgrim auto Fail = [&](llvm::Error &&Err) { 1340e828958SJF Bastien report_fatal_error("Module index '" + Buffer->getBufferIdentifier() + 1350e828958SJF Bastien "' failed: " + toString(std::move(Err))); 1360e828958SJF Bastien }; 1370e828958SJF Bastien 138df494f75SRussell Gallop llvm::TimeTraceScope TimeScope("Module LoadIndex"); 139e060e57bSDouglas Gregor // Read the global index. 140e060e57bSDouglas Gregor bool InGlobalIndexBlock = false; 141e060e57bSDouglas Gregor bool Done = false; 142e060e57bSDouglas Gregor while (!Done) { 1430e828958SJF Bastien llvm::BitstreamEntry Entry; 1440e828958SJF Bastien if (Expected<llvm::BitstreamEntry> Res = Cursor.advance()) 1450e828958SJF Bastien Entry = Res.get(); 1460e828958SJF Bastien else 1470e828958SJF Bastien Fail(Res.takeError()); 148e060e57bSDouglas Gregor 149e060e57bSDouglas Gregor switch (Entry.Kind) { 150e060e57bSDouglas Gregor case llvm::BitstreamEntry::Error: 151e060e57bSDouglas Gregor return; 152e060e57bSDouglas Gregor 153e060e57bSDouglas Gregor case llvm::BitstreamEntry::EndBlock: 154e060e57bSDouglas Gregor if (InGlobalIndexBlock) { 155e060e57bSDouglas Gregor InGlobalIndexBlock = false; 156e060e57bSDouglas Gregor Done = true; 157e060e57bSDouglas Gregor continue; 158e060e57bSDouglas Gregor } 159e060e57bSDouglas Gregor return; 160e060e57bSDouglas Gregor 161e060e57bSDouglas Gregor 162e060e57bSDouglas Gregor case llvm::BitstreamEntry::Record: 163e060e57bSDouglas Gregor // Entries in the global index block are handled below. 164e060e57bSDouglas Gregor if (InGlobalIndexBlock) 165e060e57bSDouglas Gregor break; 166e060e57bSDouglas Gregor 167e060e57bSDouglas Gregor return; 168e060e57bSDouglas Gregor 169e060e57bSDouglas Gregor case llvm::BitstreamEntry::SubBlock: 170e060e57bSDouglas Gregor if (!InGlobalIndexBlock && Entry.ID == GLOBAL_INDEX_BLOCK_ID) { 1710e828958SJF Bastien if (llvm::Error Err = Cursor.EnterSubBlock(GLOBAL_INDEX_BLOCK_ID)) 1720e828958SJF Bastien Fail(std::move(Err)); 173e060e57bSDouglas Gregor InGlobalIndexBlock = true; 1740e828958SJF Bastien } else if (llvm::Error Err = Cursor.SkipBlock()) 1750e828958SJF Bastien Fail(std::move(Err)); 176e060e57bSDouglas Gregor continue; 177e060e57bSDouglas Gregor } 178e060e57bSDouglas Gregor 179e060e57bSDouglas Gregor SmallVector<uint64_t, 64> Record; 180e060e57bSDouglas Gregor StringRef Blob; 1810e828958SJF Bastien Expected<unsigned> MaybeIndexRecord = 1820e828958SJF Bastien Cursor.readRecord(Entry.ID, Record, &Blob); 1830e828958SJF Bastien if (!MaybeIndexRecord) 1840e828958SJF Bastien Fail(MaybeIndexRecord.takeError()); 1850e828958SJF Bastien IndexRecordTypes IndexRecord = 1860e828958SJF Bastien static_cast<IndexRecordTypes>(MaybeIndexRecord.get()); 1870e828958SJF Bastien switch (IndexRecord) { 188e060e57bSDouglas Gregor case INDEX_METADATA: 189e060e57bSDouglas Gregor // Make sure that the version matches. 190e060e57bSDouglas Gregor if (Record.size() < 1 || Record[0] != CurrentVersion) 191e060e57bSDouglas Gregor return; 192e060e57bSDouglas Gregor break; 193e060e57bSDouglas Gregor 194e060e57bSDouglas Gregor case MODULE: { 195e060e57bSDouglas Gregor unsigned Idx = 0; 196e060e57bSDouglas Gregor unsigned ID = Record[Idx++]; 197e060e57bSDouglas Gregor 1987029ce1aSDouglas Gregor // Make room for this module's information. 1997029ce1aSDouglas Gregor if (ID == Modules.size()) 2007029ce1aSDouglas Gregor Modules.push_back(ModuleInfo()); 2017029ce1aSDouglas Gregor else 2027029ce1aSDouglas Gregor Modules.resize(ID + 1); 2037029ce1aSDouglas Gregor 2047029ce1aSDouglas Gregor // Size/modification time for this module file at the time the 2057029ce1aSDouglas Gregor // global index was built. 2067029ce1aSDouglas Gregor Modules[ID].Size = Record[Idx++]; 2077029ce1aSDouglas Gregor Modules[ID].ModTime = Record[Idx++]; 208e060e57bSDouglas Gregor 209e060e57bSDouglas Gregor // File name. 210e060e57bSDouglas Gregor unsigned NameLen = Record[Idx++]; 2117029ce1aSDouglas Gregor Modules[ID].FileName.assign(Record.begin() + Idx, 212e060e57bSDouglas Gregor Record.begin() + Idx + NameLen); 213e060e57bSDouglas Gregor Idx += NameLen; 214e060e57bSDouglas Gregor 215e060e57bSDouglas Gregor // Dependencies 216e060e57bSDouglas Gregor unsigned NumDeps = Record[Idx++]; 2177029ce1aSDouglas Gregor Modules[ID].Dependencies.insert(Modules[ID].Dependencies.end(), 2187029ce1aSDouglas Gregor Record.begin() + Idx, 2197029ce1aSDouglas Gregor Record.begin() + Idx + NumDeps); 2207029ce1aSDouglas Gregor Idx += NumDeps; 221e060e57bSDouglas Gregor 2227029ce1aSDouglas Gregor // Make sure we're at the end of the record. 2237029ce1aSDouglas Gregor assert(Idx == Record.size() && "More module info?"); 224603cd869SDouglas Gregor 225603cd869SDouglas Gregor // Record this module as an unresolved module. 226beee15e7SBen Langmuir // FIXME: this doesn't work correctly for module names containing path 227beee15e7SBen Langmuir // separators. 228beee15e7SBen Langmuir StringRef ModuleName = llvm::sys::path::stem(Modules[ID].FileName); 229beee15e7SBen Langmuir // Remove the -<hash of ModuleMapPath> 230beee15e7SBen Langmuir ModuleName = ModuleName.rsplit('-').first; 231beee15e7SBen Langmuir UnresolvedModules[ModuleName] = ID; 232e060e57bSDouglas Gregor break; 233e060e57bSDouglas Gregor } 234e060e57bSDouglas Gregor 235e060e57bSDouglas Gregor case IDENTIFIER_INDEX: 236e060e57bSDouglas Gregor // Wire up the identifier index. 237e060e57bSDouglas Gregor if (Record[0]) { 238e060e57bSDouglas Gregor IdentifierIndex = IdentifierIndexTable::Create( 239e060e57bSDouglas Gregor (const unsigned char *)Blob.data() + Record[0], 240da4e650eSJustin Bogner (const unsigned char *)Blob.data() + sizeof(uint32_t), 241da4e650eSJustin Bogner (const unsigned char *)Blob.data(), IdentifierIndexReaderTrait()); 242e060e57bSDouglas Gregor } 243e060e57bSDouglas Gregor break; 244e060e57bSDouglas Gregor } 245e060e57bSDouglas Gregor } 246e060e57bSDouglas Gregor } 247e060e57bSDouglas Gregor 248e68b847fSNico Weber GlobalModuleIndex::~GlobalModuleIndex() { 249e68b847fSNico Weber delete static_cast<IdentifierIndexTable *>(IdentifierIndex); 250e68b847fSNico Weber } 251e060e57bSDouglas Gregor 2520e828958SJF Bastien std::pair<GlobalModuleIndex *, llvm::Error> 2537029ce1aSDouglas Gregor GlobalModuleIndex::readIndex(StringRef Path) { 254e060e57bSDouglas Gregor // Load the index file, if it's there. 255e060e57bSDouglas Gregor llvm::SmallString<128> IndexPath; 256e060e57bSDouglas Gregor IndexPath += Path; 257e060e57bSDouglas Gregor llvm::sys::path::append(IndexPath, IndexFileName); 258e060e57bSDouglas Gregor 2592d2b420aSRafael Espindola llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> BufferOrErr = 2602d2b420aSRafael Espindola llvm::MemoryBuffer::getFile(IndexPath.c_str()); 2612d2b420aSRafael Espindola if (!BufferOrErr) 2620e828958SJF Bastien return std::make_pair(nullptr, 2630e828958SJF Bastien llvm::errorCodeToError(BufferOrErr.getError())); 2642d2b420aSRafael Espindola std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(BufferOrErr.get()); 265e060e57bSDouglas Gregor 2669fc8faf9SAdrian Prantl /// The main bitstream cursor for the main block. 26777c89b69SPeter Collingbourne llvm::BitstreamCursor Cursor(*Buffer); 268e060e57bSDouglas Gregor 269e060e57bSDouglas Gregor // Sniff for the signature. 2700e828958SJF Bastien for (unsigned char C : {'B', 'C', 'G', 'I'}) { 2710e828958SJF Bastien if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Cursor.Read(8)) { 2720e828958SJF Bastien if (Res.get() != C) 2730e828958SJF Bastien return std::make_pair( 2740e828958SJF Bastien nullptr, llvm::createStringError(std::errc::illegal_byte_sequence, 2750e828958SJF Bastien "expected signature BCGI")); 2760e828958SJF Bastien } else 2770e828958SJF Bastien return std::make_pair(nullptr, Res.takeError()); 278e060e57bSDouglas Gregor } 279e060e57bSDouglas Gregor 280afa10d3eSDavid Blaikie return std::make_pair(new GlobalModuleIndex(std::move(Buffer), Cursor), 2810e828958SJF Bastien llvm::Error::success()); 282e060e57bSDouglas Gregor } 283e060e57bSDouglas Gregor 2847029ce1aSDouglas Gregor void 2857029ce1aSDouglas Gregor GlobalModuleIndex::getKnownModules(SmallVectorImpl<ModuleFile *> &ModuleFiles) { 286e060e57bSDouglas Gregor ModuleFiles.clear(); 287e060e57bSDouglas Gregor for (unsigned I = 0, N = Modules.size(); I != N; ++I) { 288603cd869SDouglas Gregor if (ModuleFile *MF = Modules[I].File) 289603cd869SDouglas Gregor ModuleFiles.push_back(MF); 290e060e57bSDouglas Gregor } 291e060e57bSDouglas Gregor } 292e060e57bSDouglas Gregor 293e060e57bSDouglas Gregor void GlobalModuleIndex::getModuleDependencies( 2947029ce1aSDouglas Gregor ModuleFile *File, 2957029ce1aSDouglas Gregor SmallVectorImpl<ModuleFile *> &Dependencies) { 296e060e57bSDouglas Gregor // Look for information about this module file. 2977029ce1aSDouglas Gregor llvm::DenseMap<ModuleFile *, unsigned>::iterator Known 2987029ce1aSDouglas Gregor = ModulesByFile.find(File); 299e060e57bSDouglas Gregor if (Known == ModulesByFile.end()) 300e060e57bSDouglas Gregor return; 301e060e57bSDouglas Gregor 302e060e57bSDouglas Gregor // Record dependencies. 3037029ce1aSDouglas Gregor Dependencies.clear(); 3047029ce1aSDouglas Gregor ArrayRef<unsigned> StoredDependencies = Modules[Known->second].Dependencies; 3057029ce1aSDouglas Gregor for (unsigned I = 0, N = StoredDependencies.size(); I != N; ++I) { 306603cd869SDouglas Gregor if (ModuleFile *MF = Modules[I].File) 3077029ce1aSDouglas Gregor Dependencies.push_back(MF); 3087029ce1aSDouglas Gregor } 309e060e57bSDouglas Gregor } 310e060e57bSDouglas Gregor 3117211ac15SDouglas Gregor bool GlobalModuleIndex::lookupIdentifier(StringRef Name, HitSet &Hits) { 3127211ac15SDouglas Gregor Hits.clear(); 313e060e57bSDouglas Gregor 314e060e57bSDouglas Gregor // If there's no identifier index, there is nothing we can do. 315e060e57bSDouglas Gregor if (!IdentifierIndex) 316e060e57bSDouglas Gregor return false; 317e060e57bSDouglas Gregor 318e060e57bSDouglas Gregor // Look into the identifier index. 319e060e57bSDouglas Gregor ++NumIdentifierLookups; 320e060e57bSDouglas Gregor IdentifierIndexTable &Table 321e060e57bSDouglas Gregor = *static_cast<IdentifierIndexTable *>(IdentifierIndex); 322e060e57bSDouglas Gregor IdentifierIndexTable::iterator Known = Table.find(Name); 323e060e57bSDouglas Gregor if (Known == Table.end()) { 324*46ea465bSVassil Vassilev return false; 325e060e57bSDouglas Gregor } 326e060e57bSDouglas Gregor 327e060e57bSDouglas Gregor SmallVector<unsigned, 2> ModuleIDs = *Known; 328e060e57bSDouglas Gregor for (unsigned I = 0, N = ModuleIDs.size(); I != N; ++I) { 329603cd869SDouglas Gregor if (ModuleFile *MF = Modules[ModuleIDs[I]].File) 330603cd869SDouglas Gregor Hits.insert(MF); 331e060e57bSDouglas Gregor } 332e060e57bSDouglas Gregor 333e060e57bSDouglas Gregor ++NumIdentifierLookupHits; 334e060e57bSDouglas Gregor return true; 335e060e57bSDouglas Gregor } 336e060e57bSDouglas Gregor 337603cd869SDouglas Gregor bool GlobalModuleIndex::loadedModuleFile(ModuleFile *File) { 338603cd869SDouglas Gregor // Look for the module in the global module index based on the module name. 339beee15e7SBen Langmuir StringRef Name = File->ModuleName; 340603cd869SDouglas Gregor llvm::StringMap<unsigned>::iterator Known = UnresolvedModules.find(Name); 341603cd869SDouglas Gregor if (Known == UnresolvedModules.end()) { 342603cd869SDouglas Gregor return true; 3437029ce1aSDouglas Gregor } 3447029ce1aSDouglas Gregor 345603cd869SDouglas Gregor // Rectify this module with the global module index. 346603cd869SDouglas Gregor ModuleInfo &Info = Modules[Known->second]; 347603cd869SDouglas Gregor 348603cd869SDouglas Gregor // If the size and modification time match what we expected, record this 349603cd869SDouglas Gregor // module file. 350603cd869SDouglas Gregor bool Failed = true; 351603cd869SDouglas Gregor if (File->File->getSize() == Info.Size && 352603cd869SDouglas Gregor File->File->getModificationTime() == Info.ModTime) { 353603cd869SDouglas Gregor Info.File = File; 354603cd869SDouglas Gregor ModulesByFile[File] = Known->second; 355603cd869SDouglas Gregor 356603cd869SDouglas Gregor Failed = false; 3577029ce1aSDouglas Gregor } 3587029ce1aSDouglas Gregor 359603cd869SDouglas Gregor // One way or another, we have resolved this module file. 360603cd869SDouglas Gregor UnresolvedModules.erase(Known); 361603cd869SDouglas Gregor return Failed; 3627029ce1aSDouglas Gregor } 3637029ce1aSDouglas Gregor 364e060e57bSDouglas Gregor void GlobalModuleIndex::printStats() { 365e060e57bSDouglas Gregor std::fprintf(stderr, "*** Global Module Index Statistics:\n"); 366e060e57bSDouglas Gregor if (NumIdentifierLookups) { 367e060e57bSDouglas Gregor fprintf(stderr, " %u / %u identifier lookups succeeded (%f%%)\n", 368e060e57bSDouglas Gregor NumIdentifierLookupHits, NumIdentifierLookups, 369e060e57bSDouglas Gregor (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); 370e060e57bSDouglas Gregor } 371e060e57bSDouglas Gregor std::fprintf(stderr, "\n"); 372e060e57bSDouglas Gregor } 373e060e57bSDouglas Gregor 374cdae941eSYaron Keren LLVM_DUMP_METHOD void GlobalModuleIndex::dump() { 375a39baf1aSJohn Thompson llvm::errs() << "*** Global Module Index Dump:\n"; 376a39baf1aSJohn Thompson llvm::errs() << "Module files:\n"; 3774f52d44dSJohn Thompson for (auto &MI : Modules) { 378a39baf1aSJohn Thompson llvm::errs() << "** " << MI.FileName << "\n"; 379a39baf1aSJohn Thompson if (MI.File) 380a39baf1aSJohn Thompson MI.File->dump(); 381bcdcc92eSJohn Thompson else 382a39baf1aSJohn Thompson llvm::errs() << "\n"; 383bcdcc92eSJohn Thompson } 384a39baf1aSJohn Thompson llvm::errs() << "\n"; 385bcdcc92eSJohn Thompson } 386bcdcc92eSJohn Thompson 387e060e57bSDouglas Gregor //----------------------------------------------------------------------------// 3885e306b12SDouglas Gregor // Global module index writer. 3895e306b12SDouglas Gregor //----------------------------------------------------------------------------// 3905e306b12SDouglas Gregor 3915e306b12SDouglas Gregor namespace { 3929fc8faf9SAdrian Prantl /// Provides information about a specific module file. 3935e306b12SDouglas Gregor struct ModuleFileInfo { 3949fc8faf9SAdrian Prantl /// The numberic ID for this module file. 3955e306b12SDouglas Gregor unsigned ID; 3965e306b12SDouglas Gregor 3979fc8faf9SAdrian Prantl /// The set of modules on which this module depends. Each entry is 3985e306b12SDouglas Gregor /// a module ID. 3995e306b12SDouglas Gregor SmallVector<unsigned, 4> Dependencies; 40060fa2888SDuncan P. N. Exon Smith ASTFileSignature Signature; 40160fa2888SDuncan P. N. Exon Smith }; 40260fa2888SDuncan P. N. Exon Smith 40360fa2888SDuncan P. N. Exon Smith struct ImportedModuleFileInfo { 40460fa2888SDuncan P. N. Exon Smith off_t StoredSize; 40560fa2888SDuncan P. N. Exon Smith time_t StoredModTime; 40660fa2888SDuncan P. N. Exon Smith ASTFileSignature StoredSignature; 40760fa2888SDuncan P. N. Exon Smith ImportedModuleFileInfo(off_t Size, time_t ModTime, ASTFileSignature Sig) 40860fa2888SDuncan P. N. Exon Smith : StoredSize(Size), StoredModTime(ModTime), StoredSignature(Sig) {} 4095e306b12SDouglas Gregor }; 4105e306b12SDouglas Gregor 4119fc8faf9SAdrian Prantl /// Builder that generates the global module index file. 4125e306b12SDouglas Gregor class GlobalModuleIndexBuilder { 4135e306b12SDouglas Gregor FileManager &FileMgr; 414fb2398d0SAdrian Prantl const PCHContainerReader &PCHContainerRdr; 4155e306b12SDouglas Gregor 41660fa2888SDuncan P. N. Exon Smith /// Mapping from files to module file information. 4175e306b12SDouglas Gregor typedef llvm::MapVector<const FileEntry *, ModuleFileInfo> ModuleFilesMap; 4185e306b12SDouglas Gregor 41960fa2888SDuncan P. N. Exon Smith /// Information about each of the known module files. 4205e306b12SDouglas Gregor ModuleFilesMap ModuleFiles; 4215e306b12SDouglas Gregor 4229fc8faf9SAdrian Prantl /// Mapping from the imported module file to the imported 42360fa2888SDuncan P. N. Exon Smith /// information. 42460fa2888SDuncan P. N. Exon Smith typedef std::multimap<const FileEntry *, ImportedModuleFileInfo> 42560fa2888SDuncan P. N. Exon Smith ImportedModuleFilesMap; 42660fa2888SDuncan P. N. Exon Smith 4279fc8faf9SAdrian Prantl /// Information about each importing of a module file. 42860fa2888SDuncan P. N. Exon Smith ImportedModuleFilesMap ImportedModuleFiles; 42960fa2888SDuncan P. N. Exon Smith 4309fc8faf9SAdrian Prantl /// Mapping from identifiers to the list of module file IDs that 4315e306b12SDouglas Gregor /// consider this identifier to be interesting. 4325e306b12SDouglas Gregor typedef llvm::StringMap<SmallVector<unsigned, 2> > InterestingIdentifierMap; 4335e306b12SDouglas Gregor 4349fc8faf9SAdrian Prantl /// A mapping from all interesting identifiers to the set of module 4355e306b12SDouglas Gregor /// files in which those identifiers are considered interesting. 4365e306b12SDouglas Gregor InterestingIdentifierMap InterestingIdentifiers; 4375e306b12SDouglas Gregor 4389fc8faf9SAdrian Prantl /// Write the block-info block for the global module index file. 4395e306b12SDouglas Gregor void emitBlockInfoBlock(llvm::BitstreamWriter &Stream); 4405e306b12SDouglas Gregor 4419fc8faf9SAdrian Prantl /// Retrieve the module file information for the given file. 4425e306b12SDouglas Gregor ModuleFileInfo &getModuleFileInfo(const FileEntry *File) { 4435e306b12SDouglas Gregor llvm::MapVector<const FileEntry *, ModuleFileInfo>::iterator Known 4445e306b12SDouglas Gregor = ModuleFiles.find(File); 4455e306b12SDouglas Gregor if (Known != ModuleFiles.end()) 4465e306b12SDouglas Gregor return Known->second; 4475e306b12SDouglas Gregor 4485e306b12SDouglas Gregor unsigned NewID = ModuleFiles.size(); 4495e306b12SDouglas Gregor ModuleFileInfo &Info = ModuleFiles[File]; 4505e306b12SDouglas Gregor Info.ID = NewID; 4515e306b12SDouglas Gregor return Info; 4525e306b12SDouglas Gregor } 4535e306b12SDouglas Gregor 4545e306b12SDouglas Gregor public: 455bb165fb0SAdrian Prantl explicit GlobalModuleIndexBuilder( 456fb2398d0SAdrian Prantl FileManager &FileMgr, const PCHContainerReader &PCHContainerRdr) 457fb2398d0SAdrian Prantl : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr) {} 4585e306b12SDouglas Gregor 4599fc8faf9SAdrian Prantl /// Load the contents of the given module file into the builder. 4600e828958SJF Bastien llvm::Error loadModuleFile(const FileEntry *File); 4615e306b12SDouglas Gregor 4629fc8faf9SAdrian Prantl /// Write the index to the given bitstream. 46360fa2888SDuncan P. N. Exon Smith /// \returns true if an error occurred, false otherwise. 46460fa2888SDuncan P. N. Exon Smith bool writeIndex(llvm::BitstreamWriter &Stream); 4655e306b12SDouglas Gregor }; 466ab9db510SAlexander Kornienko } 4675e306b12SDouglas Gregor 4685e306b12SDouglas Gregor static void emitBlockID(unsigned ID, const char *Name, 4695e306b12SDouglas Gregor llvm::BitstreamWriter &Stream, 4705e306b12SDouglas Gregor SmallVectorImpl<uint64_t> &Record) { 4715e306b12SDouglas Gregor Record.clear(); 4725e306b12SDouglas Gregor Record.push_back(ID); 4735e306b12SDouglas Gregor Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record); 4745e306b12SDouglas Gregor 4755e306b12SDouglas Gregor // Emit the block name if present. 476a13603a2SCraig Topper if (!Name || Name[0] == 0) return; 4775e306b12SDouglas Gregor Record.clear(); 4785e306b12SDouglas Gregor while (*Name) 4795e306b12SDouglas Gregor Record.push_back(*Name++); 4805e306b12SDouglas Gregor Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record); 4815e306b12SDouglas Gregor } 4825e306b12SDouglas Gregor 4835e306b12SDouglas Gregor static void emitRecordID(unsigned ID, const char *Name, 4845e306b12SDouglas Gregor llvm::BitstreamWriter &Stream, 4855e306b12SDouglas Gregor SmallVectorImpl<uint64_t> &Record) { 4865e306b12SDouglas Gregor Record.clear(); 4875e306b12SDouglas Gregor Record.push_back(ID); 4885e306b12SDouglas Gregor while (*Name) 4895e306b12SDouglas Gregor Record.push_back(*Name++); 4905e306b12SDouglas Gregor Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record); 4915e306b12SDouglas Gregor } 4925e306b12SDouglas Gregor 4935e306b12SDouglas Gregor void 4945e306b12SDouglas Gregor GlobalModuleIndexBuilder::emitBlockInfoBlock(llvm::BitstreamWriter &Stream) { 4955e306b12SDouglas Gregor SmallVector<uint64_t, 64> Record; 496d3a6c70bSPeter Collingbourne Stream.EnterBlockInfoBlock(); 4975e306b12SDouglas Gregor 4985e306b12SDouglas Gregor #define BLOCK(X) emitBlockID(X ## _ID, #X, Stream, Record) 4995e306b12SDouglas Gregor #define RECORD(X) emitRecordID(X, #X, Stream, Record) 5005e306b12SDouglas Gregor BLOCK(GLOBAL_INDEX_BLOCK); 501e060e57bSDouglas Gregor RECORD(INDEX_METADATA); 5025e306b12SDouglas Gregor RECORD(MODULE); 5035e306b12SDouglas Gregor RECORD(IDENTIFIER_INDEX); 5045e306b12SDouglas Gregor #undef RECORD 5055e306b12SDouglas Gregor #undef BLOCK 5065e306b12SDouglas Gregor 5075e306b12SDouglas Gregor Stream.ExitBlock(); 5085e306b12SDouglas Gregor } 5095e306b12SDouglas Gregor 510e060e57bSDouglas Gregor namespace { 5115e306b12SDouglas Gregor class InterestingASTIdentifierLookupTrait 5125e306b12SDouglas Gregor : public serialization::reader::ASTIdentifierLookupTraitBase { 5135e306b12SDouglas Gregor 5145e306b12SDouglas Gregor public: 5159fc8faf9SAdrian Prantl /// The identifier and whether it is "interesting". 5165e306b12SDouglas Gregor typedef std::pair<StringRef, bool> data_type; 5175e306b12SDouglas Gregor 5185e306b12SDouglas Gregor data_type ReadData(const internal_key_type& k, 5195e306b12SDouglas Gregor const unsigned char* d, 5205e306b12SDouglas Gregor unsigned DataLen) { 5215e306b12SDouglas Gregor // The first bit indicates whether this identifier is interesting. 5225e306b12SDouglas Gregor // That's all we care about. 52357ba0b22SJustin Bogner using namespace llvm::support; 52457ba0b22SJustin Bogner unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); 5255e306b12SDouglas Gregor bool IsInteresting = RawID & 0x01; 5265e306b12SDouglas Gregor return std::make_pair(k, IsInteresting); 5275e306b12SDouglas Gregor } 5285e306b12SDouglas Gregor }; 529ab9db510SAlexander Kornienko } 5305e306b12SDouglas Gregor 5310e828958SJF Bastien llvm::Error GlobalModuleIndexBuilder::loadModuleFile(const FileEntry *File) { 5325e306b12SDouglas Gregor // Open the module file. 5336406f7b8SRafael Espindola 534a885796dSBenjamin Kramer auto Buffer = FileMgr.getBufferForFile(File, /*isVolatile=*/true); 5350e828958SJF Bastien if (!Buffer) 5360e828958SJF Bastien return llvm::createStringError(Buffer.getError(), 5370e828958SJF Bastien "failed getting buffer for module file"); 5385e306b12SDouglas Gregor 5395e306b12SDouglas Gregor // Initialize the input stream 54077c89b69SPeter Collingbourne llvm::BitstreamCursor InStream(PCHContainerRdr.ExtractPCH(**Buffer)); 5415e306b12SDouglas Gregor 5425e306b12SDouglas Gregor // Sniff for the signature. 5430e828958SJF Bastien for (unsigned char C : {'C', 'P', 'C', 'H'}) 5440e828958SJF Bastien if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = InStream.Read(8)) { 5450e828958SJF Bastien if (Res.get() != C) 5460e828958SJF Bastien return llvm::createStringError(std::errc::illegal_byte_sequence, 5470e828958SJF Bastien "expected signature CPCH"); 5480e828958SJF Bastien } else 5490e828958SJF Bastien return Res.takeError(); 5505e306b12SDouglas Gregor 5515e306b12SDouglas Gregor // Record this module file and assign it a unique ID (if it doesn't have 5525e306b12SDouglas Gregor // one already). 5535e306b12SDouglas Gregor unsigned ID = getModuleFileInfo(File).ID; 5545e306b12SDouglas Gregor 5555e306b12SDouglas Gregor // Search for the blocks and records we care about. 55660fa2888SDuncan P. N. Exon Smith enum { Other, ControlBlock, ASTBlock, DiagnosticOptionsBlock } State = Other; 5575e306b12SDouglas Gregor bool Done = false; 5585e306b12SDouglas Gregor while (!Done) { 5590e828958SJF Bastien Expected<llvm::BitstreamEntry> MaybeEntry = InStream.advance(); 5600e828958SJF Bastien if (!MaybeEntry) 5610e828958SJF Bastien return MaybeEntry.takeError(); 5620e828958SJF Bastien llvm::BitstreamEntry Entry = MaybeEntry.get(); 5630e828958SJF Bastien 5645e306b12SDouglas Gregor switch (Entry.Kind) { 5655e306b12SDouglas Gregor case llvm::BitstreamEntry::Error: 566e060e57bSDouglas Gregor Done = true; 567e060e57bSDouglas Gregor continue; 5685e306b12SDouglas Gregor 5695e306b12SDouglas Gregor case llvm::BitstreamEntry::Record: 570e060e57bSDouglas Gregor // In the 'other' state, just skip the record. We don't care. 571e060e57bSDouglas Gregor if (State == Other) { 5720e828958SJF Bastien if (llvm::Expected<unsigned> Skipped = InStream.skipRecord(Entry.ID)) 5735e306b12SDouglas Gregor continue; 5740e828958SJF Bastien else 5750e828958SJF Bastien return Skipped.takeError(); 5765e306b12SDouglas Gregor } 5775e306b12SDouglas Gregor 5785e306b12SDouglas Gregor // Handle potentially-interesting records below. 5795e306b12SDouglas Gregor break; 5805e306b12SDouglas Gregor 5815e306b12SDouglas Gregor case llvm::BitstreamEntry::SubBlock: 582e060e57bSDouglas Gregor if (Entry.ID == CONTROL_BLOCK_ID) { 5830e828958SJF Bastien if (llvm::Error Err = InStream.EnterSubBlock(CONTROL_BLOCK_ID)) 5840e828958SJF Bastien return Err; 5855e306b12SDouglas Gregor 5865e306b12SDouglas Gregor // Found the control block. 5875e306b12SDouglas Gregor State = ControlBlock; 5885e306b12SDouglas Gregor continue; 5895e306b12SDouglas Gregor } 5905e306b12SDouglas Gregor 591e060e57bSDouglas Gregor if (Entry.ID == AST_BLOCK_ID) { 5920e828958SJF Bastien if (llvm::Error Err = InStream.EnterSubBlock(AST_BLOCK_ID)) 5930e828958SJF Bastien return Err; 5945e306b12SDouglas Gregor 5955e306b12SDouglas Gregor // Found the AST block. 5965e306b12SDouglas Gregor State = ASTBlock; 5975e306b12SDouglas Gregor continue; 5985e306b12SDouglas Gregor } 5995e306b12SDouglas Gregor 60060fa2888SDuncan P. N. Exon Smith if (Entry.ID == UNHASHED_CONTROL_BLOCK_ID) { 6010e828958SJF Bastien if (llvm::Error Err = InStream.EnterSubBlock(UNHASHED_CONTROL_BLOCK_ID)) 6020e828958SJF Bastien return Err; 60360fa2888SDuncan P. N. Exon Smith 60460fa2888SDuncan P. N. Exon Smith // Found the Diagnostic Options block. 60560fa2888SDuncan P. N. Exon Smith State = DiagnosticOptionsBlock; 60660fa2888SDuncan P. N. Exon Smith continue; 60760fa2888SDuncan P. N. Exon Smith } 60860fa2888SDuncan P. N. Exon Smith 6090e828958SJF Bastien if (llvm::Error Err = InStream.SkipBlock()) 6100e828958SJF Bastien return Err; 6115e306b12SDouglas Gregor 6125e306b12SDouglas Gregor continue; 6135e306b12SDouglas Gregor 6145e306b12SDouglas Gregor case llvm::BitstreamEntry::EndBlock: 615e060e57bSDouglas Gregor State = Other; 6165e306b12SDouglas Gregor continue; 6175e306b12SDouglas Gregor } 6185e306b12SDouglas Gregor 6195e306b12SDouglas Gregor // Read the given record. 6205e306b12SDouglas Gregor SmallVector<uint64_t, 64> Record; 6215e306b12SDouglas Gregor StringRef Blob; 6220e828958SJF Bastien Expected<unsigned> MaybeCode = InStream.readRecord(Entry.ID, Record, &Blob); 6230e828958SJF Bastien if (!MaybeCode) 6240e828958SJF Bastien return MaybeCode.takeError(); 6250e828958SJF Bastien unsigned Code = MaybeCode.get(); 6265e306b12SDouglas Gregor 6275e306b12SDouglas Gregor // Handle module dependencies. 6285e306b12SDouglas Gregor if (State == ControlBlock && Code == IMPORTS) { 6295e306b12SDouglas Gregor // Load each of the imported PCH files. 6305e306b12SDouglas Gregor unsigned Idx = 0, N = Record.size(); 6315e306b12SDouglas Gregor while (Idx < N) { 6325e306b12SDouglas Gregor // Read information about the AST file. 6335e306b12SDouglas Gregor 6345e306b12SDouglas Gregor // Skip the imported kind 6355e306b12SDouglas Gregor ++Idx; 6365e306b12SDouglas Gregor 6375e306b12SDouglas Gregor // Skip the import location 6385e306b12SDouglas Gregor ++Idx; 6395e306b12SDouglas Gregor 6407029ce1aSDouglas Gregor // Load stored size/modification time. 6417029ce1aSDouglas Gregor off_t StoredSize = (off_t)Record[Idx++]; 6427029ce1aSDouglas Gregor time_t StoredModTime = (time_t)Record[Idx++]; 6437029ce1aSDouglas Gregor 644487ea14aSBen Langmuir // Skip the stored signature. 645487ea14aSBen Langmuir // FIXME: we could read the signature out of the import and validate it. 646e87e55edSDaniel Grumberg auto FirstSignatureByte = Record.begin() + Idx; 647e87e55edSDaniel Grumberg ASTFileSignature StoredSignature = ASTFileSignature::create( 648e87e55edSDaniel Grumberg FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size); 649e87e55edSDaniel Grumberg Idx += ASTFileSignature::size; 650487ea14aSBen Langmuir 651d30446fdSBoris Kolpackov // Skip the module name (currently this is only used for prebuilt 652d30446fdSBoris Kolpackov // modules while here we are only dealing with cached). 653d30446fdSBoris Kolpackov Idx += Record[Idx] + 1; 654d30446fdSBoris Kolpackov 6555e306b12SDouglas Gregor // Retrieve the imported file name. 6565e306b12SDouglas Gregor unsigned Length = Record[Idx++]; 6575e306b12SDouglas Gregor SmallString<128> ImportedFile(Record.begin() + Idx, 6585e306b12SDouglas Gregor Record.begin() + Idx + Length); 6595e306b12SDouglas Gregor Idx += Length; 6605e306b12SDouglas Gregor 6615e306b12SDouglas Gregor // Find the imported module file. 6628d323d15SHarlan Haskins auto DependsOnFile 66349a3ad21SRui Ueyama = FileMgr.getFile(ImportedFile, /*OpenFile=*/false, 66449a3ad21SRui Ueyama /*CacheFailure=*/false); 66560fa2888SDuncan P. N. Exon Smith 66660fa2888SDuncan P. N. Exon Smith if (!DependsOnFile) 6670e828958SJF Bastien return llvm::createStringError(std::errc::bad_file_descriptor, 6680e828958SJF Bastien "imported file \"%s\" not found", 6690e828958SJF Bastien ImportedFile.c_str()); 6705e306b12SDouglas Gregor 67160fa2888SDuncan P. N. Exon Smith // Save the information in ImportedModuleFileInfo so we can verify after 67260fa2888SDuncan P. N. Exon Smith // loading all pcms. 67360fa2888SDuncan P. N. Exon Smith ImportedModuleFiles.insert(std::make_pair( 6748d323d15SHarlan Haskins *DependsOnFile, ImportedModuleFileInfo(StoredSize, StoredModTime, 67560fa2888SDuncan P. N. Exon Smith StoredSignature))); 67660fa2888SDuncan P. N. Exon Smith 6775e306b12SDouglas Gregor // Record the dependency. 6788d323d15SHarlan Haskins unsigned DependsOnID = getModuleFileInfo(*DependsOnFile).ID; 6795e306b12SDouglas Gregor getModuleFileInfo(File).Dependencies.push_back(DependsOnID); 6805e306b12SDouglas Gregor } 6815e306b12SDouglas Gregor 6825e306b12SDouglas Gregor continue; 6835e306b12SDouglas Gregor } 6845e306b12SDouglas Gregor 6855e306b12SDouglas Gregor // Handle the identifier table 6865e306b12SDouglas Gregor if (State == ASTBlock && Code == IDENTIFIER_TABLE && Record[0] > 0) { 687bb094f06SJustin Bogner typedef llvm::OnDiskIterableChainedHashTable< 688bb094f06SJustin Bogner InterestingASTIdentifierLookupTrait> InterestingIdentifierTable; 689b8984329SAhmed Charles std::unique_ptr<InterestingIdentifierTable> Table( 690b8984329SAhmed Charles InterestingIdentifierTable::Create( 6915e306b12SDouglas Gregor (const unsigned char *)Blob.data() + Record[0], 692da4e650eSJustin Bogner (const unsigned char *)Blob.data() + sizeof(uint32_t), 6935e306b12SDouglas Gregor (const unsigned char *)Blob.data())); 6945e306b12SDouglas Gregor for (InterestingIdentifierTable::data_iterator D = Table->data_begin(), 6955e306b12SDouglas Gregor DEnd = Table->data_end(); 6965e306b12SDouglas Gregor D != DEnd; ++D) { 6975e306b12SDouglas Gregor std::pair<StringRef, bool> Ident = *D; 6985e306b12SDouglas Gregor if (Ident.second) 6995e306b12SDouglas Gregor InterestingIdentifiers[Ident.first].push_back(ID); 700e060e57bSDouglas Gregor else 701e060e57bSDouglas Gregor (void)InterestingIdentifiers[Ident.first]; 7025e306b12SDouglas Gregor } 7035e306b12SDouglas Gregor } 7045e306b12SDouglas Gregor 70560fa2888SDuncan P. N. Exon Smith // Get Signature. 70660fa2888SDuncan P. N. Exon Smith if (State == DiagnosticOptionsBlock && Code == SIGNATURE) 707e87e55edSDaniel Grumberg getModuleFileInfo(File).Signature = ASTFileSignature::create( 708e87e55edSDaniel Grumberg Record.begin(), Record.begin() + ASTFileSignature::size); 70960fa2888SDuncan P. N. Exon Smith 7105e306b12SDouglas Gregor // We don't care about this record. 7115e306b12SDouglas Gregor } 7125e306b12SDouglas Gregor 7130e828958SJF Bastien return llvm::Error::success(); 7145e306b12SDouglas Gregor } 7155e306b12SDouglas Gregor 7165e306b12SDouglas Gregor namespace { 7175e306b12SDouglas Gregor 7189fc8faf9SAdrian Prantl /// Trait used to generate the identifier index as an on-disk hash 7195e306b12SDouglas Gregor /// table. 7205e306b12SDouglas Gregor class IdentifierIndexWriterTrait { 7215e306b12SDouglas Gregor public: 7225e306b12SDouglas Gregor typedef StringRef key_type; 7235e306b12SDouglas Gregor typedef StringRef key_type_ref; 7245e306b12SDouglas Gregor typedef SmallVector<unsigned, 2> data_type; 7255e306b12SDouglas Gregor typedef const SmallVector<unsigned, 2> &data_type_ref; 72625463f15SJustin Bogner typedef unsigned hash_value_type; 72725463f15SJustin Bogner typedef unsigned offset_type; 7285e306b12SDouglas Gregor 72925463f15SJustin Bogner static hash_value_type ComputeHash(key_type_ref Key) { 730560ce2c7SJonas Devlieghere return llvm::djbHash(Key); 7315e306b12SDouglas Gregor } 7325e306b12SDouglas Gregor 7335e306b12SDouglas Gregor std::pair<unsigned,unsigned> 7345e306b12SDouglas Gregor EmitKeyDataLength(raw_ostream& Out, key_type_ref Key, data_type_ref Data) { 735e1c147c3SJustin Bogner using namespace llvm::support; 736e3f65297SPeter Collingbourne endian::Writer LE(Out, little); 7375e306b12SDouglas Gregor unsigned KeyLen = Key.size(); 7385e306b12SDouglas Gregor unsigned DataLen = Data.size() * 4; 739e1c147c3SJustin Bogner LE.write<uint16_t>(KeyLen); 740e1c147c3SJustin Bogner LE.write<uint16_t>(DataLen); 7415e306b12SDouglas Gregor return std::make_pair(KeyLen, DataLen); 7425e306b12SDouglas Gregor } 7435e306b12SDouglas Gregor 7445e306b12SDouglas Gregor void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) { 7455e306b12SDouglas Gregor Out.write(Key.data(), KeyLen); 7465e306b12SDouglas Gregor } 7475e306b12SDouglas Gregor 7485e306b12SDouglas Gregor void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data, 7495e306b12SDouglas Gregor unsigned DataLen) { 750e1c147c3SJustin Bogner using namespace llvm::support; 7515e306b12SDouglas Gregor for (unsigned I = 0, N = Data.size(); I != N; ++I) 752e3f65297SPeter Collingbourne endian::write<uint32_t>(Out, Data[I], little); 7535e306b12SDouglas Gregor } 7545e306b12SDouglas Gregor }; 7555e306b12SDouglas Gregor 756ab9db510SAlexander Kornienko } 7575e306b12SDouglas Gregor 75860fa2888SDuncan P. N. Exon Smith bool GlobalModuleIndexBuilder::writeIndex(llvm::BitstreamWriter &Stream) { 75960fa2888SDuncan P. N. Exon Smith for (auto MapEntry : ImportedModuleFiles) { 76060fa2888SDuncan P. N. Exon Smith auto *File = MapEntry.first; 76160fa2888SDuncan P. N. Exon Smith ImportedModuleFileInfo &Info = MapEntry.second; 76260fa2888SDuncan P. N. Exon Smith if (getModuleFileInfo(File).Signature) { 76360fa2888SDuncan P. N. Exon Smith if (getModuleFileInfo(File).Signature != Info.StoredSignature) 76460fa2888SDuncan P. N. Exon Smith // Verify Signature. 76560fa2888SDuncan P. N. Exon Smith return true; 76660fa2888SDuncan P. N. Exon Smith } else if (Info.StoredSize != File->getSize() || 76760fa2888SDuncan P. N. Exon Smith Info.StoredModTime != File->getModificationTime()) 76860fa2888SDuncan P. N. Exon Smith // Verify Size and ModTime. 76960fa2888SDuncan P. N. Exon Smith return true; 77060fa2888SDuncan P. N. Exon Smith } 77160fa2888SDuncan P. N. Exon Smith 7725e306b12SDouglas Gregor using namespace llvm; 773df494f75SRussell Gallop llvm::TimeTraceScope TimeScope("Module WriteIndex"); 7745e306b12SDouglas Gregor 7755e306b12SDouglas Gregor // Emit the file header. 7765e306b12SDouglas Gregor Stream.Emit((unsigned)'B', 8); 7775e306b12SDouglas Gregor Stream.Emit((unsigned)'C', 8); 7785e306b12SDouglas Gregor Stream.Emit((unsigned)'G', 8); 7795e306b12SDouglas Gregor Stream.Emit((unsigned)'I', 8); 7805e306b12SDouglas Gregor 7815e306b12SDouglas Gregor // Write the block-info block, which describes the records in this bitcode 7825e306b12SDouglas Gregor // file. 7835e306b12SDouglas Gregor emitBlockInfoBlock(Stream); 7845e306b12SDouglas Gregor 7855e306b12SDouglas Gregor Stream.EnterSubblock(GLOBAL_INDEX_BLOCK_ID, 3); 7865e306b12SDouglas Gregor 7875e306b12SDouglas Gregor // Write the metadata. 7885e306b12SDouglas Gregor SmallVector<uint64_t, 2> Record; 7895e306b12SDouglas Gregor Record.push_back(CurrentVersion); 790e060e57bSDouglas Gregor Stream.EmitRecord(INDEX_METADATA, Record); 7915e306b12SDouglas Gregor 7925e306b12SDouglas Gregor // Write the set of known module files. 7935e306b12SDouglas Gregor for (ModuleFilesMap::iterator M = ModuleFiles.begin(), 7945e306b12SDouglas Gregor MEnd = ModuleFiles.end(); 7955e306b12SDouglas Gregor M != MEnd; ++M) { 7965e306b12SDouglas Gregor Record.clear(); 7975e306b12SDouglas Gregor Record.push_back(M->second.ID); 7985e306b12SDouglas Gregor Record.push_back(M->first->getSize()); 7995e306b12SDouglas Gregor Record.push_back(M->first->getModificationTime()); 8005e306b12SDouglas Gregor 8015e306b12SDouglas Gregor // File name 8025e306b12SDouglas Gregor StringRef Name(M->first->getName()); 8035e306b12SDouglas Gregor Record.push_back(Name.size()); 8045e306b12SDouglas Gregor Record.append(Name.begin(), Name.end()); 8055e306b12SDouglas Gregor 8065e306b12SDouglas Gregor // Dependencies 8075e306b12SDouglas Gregor Record.push_back(M->second.Dependencies.size()); 8085e306b12SDouglas Gregor Record.append(M->second.Dependencies.begin(), M->second.Dependencies.end()); 8095e306b12SDouglas Gregor Stream.EmitRecord(MODULE, Record); 8105e306b12SDouglas Gregor } 8115e306b12SDouglas Gregor 8125e306b12SDouglas Gregor // Write the identifier -> module file mapping. 8135e306b12SDouglas Gregor { 814bb094f06SJustin Bogner llvm::OnDiskChainedHashTableGenerator<IdentifierIndexWriterTrait> Generator; 8155e306b12SDouglas Gregor IdentifierIndexWriterTrait Trait; 8165e306b12SDouglas Gregor 8175e306b12SDouglas Gregor // Populate the hash table. 8185e306b12SDouglas Gregor for (InterestingIdentifierMap::iterator I = InterestingIdentifiers.begin(), 8195e306b12SDouglas Gregor IEnd = InterestingIdentifiers.end(); 8205e306b12SDouglas Gregor I != IEnd; ++I) { 8215e306b12SDouglas Gregor Generator.insert(I->first(), I->second, Trait); 8225e306b12SDouglas Gregor } 8235e306b12SDouglas Gregor 8245e306b12SDouglas Gregor // Create the on-disk hash table in a buffer. 8255e306b12SDouglas Gregor SmallString<4096> IdentifierTable; 8265e306b12SDouglas Gregor uint32_t BucketOffset; 8275e306b12SDouglas Gregor { 828e1c147c3SJustin Bogner using namespace llvm::support; 8295e306b12SDouglas Gregor llvm::raw_svector_ostream Out(IdentifierTable); 8305e306b12SDouglas Gregor // Make sure that no bucket is at offset 0 831e3f65297SPeter Collingbourne endian::write<uint32_t>(Out, 0, little); 8325e306b12SDouglas Gregor BucketOffset = Generator.Emit(Out, Trait); 8335e306b12SDouglas Gregor } 8345e306b12SDouglas Gregor 8355e306b12SDouglas Gregor // Create a blob abbreviation 836b44f0bfbSDavid Blaikie auto Abbrev = std::make_shared<BitCodeAbbrev>(); 8375e306b12SDouglas Gregor Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_INDEX)); 8385e306b12SDouglas Gregor Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); 8395e306b12SDouglas Gregor Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); 840b44f0bfbSDavid Blaikie unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev)); 8415e306b12SDouglas Gregor 8425e306b12SDouglas Gregor // Write the identifier table 84357a41913SMehdi Amini uint64_t Record[] = {IDENTIFIER_INDEX, BucketOffset}; 84492e1b62dSYaron Keren Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable); 8455e306b12SDouglas Gregor } 8465e306b12SDouglas Gregor 8475e306b12SDouglas Gregor Stream.ExitBlock(); 84860fa2888SDuncan P. N. Exon Smith return false; 8495e306b12SDouglas Gregor } 8505e306b12SDouglas Gregor 8510e828958SJF Bastien llvm::Error 852bb165fb0SAdrian Prantl GlobalModuleIndex::writeIndex(FileManager &FileMgr, 853fb2398d0SAdrian Prantl const PCHContainerReader &PCHContainerRdr, 854bb165fb0SAdrian Prantl StringRef Path) { 8555e306b12SDouglas Gregor llvm::SmallString<128> IndexPath; 8565e306b12SDouglas Gregor IndexPath += Path; 8575e306b12SDouglas Gregor llvm::sys::path::append(IndexPath, IndexFileName); 8585e306b12SDouglas Gregor 8595e306b12SDouglas Gregor // Coordinate building the global index file with other processes that might 8605e306b12SDouglas Gregor // try to do the same. 8615e306b12SDouglas Gregor llvm::LockFileManager Locked(IndexPath); 8625e306b12SDouglas Gregor switch (Locked) { 8635e306b12SDouglas Gregor case llvm::LockFileManager::LFS_Error: 8640e828958SJF Bastien return llvm::createStringError(std::errc::io_error, "LFS error"); 8655e306b12SDouglas Gregor 8665e306b12SDouglas Gregor case llvm::LockFileManager::LFS_Owned: 8675e306b12SDouglas Gregor // We're responsible for building the index ourselves. Do so below. 8685e306b12SDouglas Gregor break; 8695e306b12SDouglas Gregor 8705e306b12SDouglas Gregor case llvm::LockFileManager::LFS_Shared: 8715e306b12SDouglas Gregor // Someone else is responsible for building the index. We don't care 8725e306b12SDouglas Gregor // when they finish, so we're done. 8730e828958SJF Bastien return llvm::createStringError(std::errc::device_or_resource_busy, 8740e828958SJF Bastien "someone else is building the index"); 8755e306b12SDouglas Gregor } 8765e306b12SDouglas Gregor 8775e306b12SDouglas Gregor // The module index builder. 878fb2398d0SAdrian Prantl GlobalModuleIndexBuilder Builder(FileMgr, PCHContainerRdr); 8795e306b12SDouglas Gregor 8805e306b12SDouglas Gregor // Load each of the module files. 881c080917eSRafael Espindola std::error_code EC; 8825e306b12SDouglas Gregor for (llvm::sys::fs::directory_iterator D(Path, EC), DEnd; 8835e306b12SDouglas Gregor D != DEnd && !EC; 8845e306b12SDouglas Gregor D.increment(EC)) { 8855e306b12SDouglas Gregor // If this isn't a module file, we don't care. 8865e306b12SDouglas Gregor if (llvm::sys::path::extension(D->path()) != ".pcm") { 8875e306b12SDouglas Gregor // ... unless it's a .pcm.lock file, which indicates that someone is 8885e306b12SDouglas Gregor // in the process of rebuilding a module. They'll rebuild the index 8895e306b12SDouglas Gregor // at the end of that translation unit, so we don't have to. 8905e306b12SDouglas Gregor if (llvm::sys::path::extension(D->path()) == ".pcm.lock") 8910e828958SJF Bastien return llvm::createStringError(std::errc::device_or_resource_busy, 8920e828958SJF Bastien "someone else is building the index"); 8935e306b12SDouglas Gregor 8945e306b12SDouglas Gregor continue; 8955e306b12SDouglas Gregor } 8965e306b12SDouglas Gregor 8975e306b12SDouglas Gregor // If we can't find the module file, skip it. 8988d323d15SHarlan Haskins auto ModuleFile = FileMgr.getFile(D->path()); 8995e306b12SDouglas Gregor if (!ModuleFile) 9005e306b12SDouglas Gregor continue; 9015e306b12SDouglas Gregor 9025e306b12SDouglas Gregor // Load this module file. 9038d323d15SHarlan Haskins if (llvm::Error Err = Builder.loadModuleFile(*ModuleFile)) 9040e828958SJF Bastien return Err; 9055e306b12SDouglas Gregor } 9065e306b12SDouglas Gregor 9075e306b12SDouglas Gregor // The output buffer, into which the global index will be written. 9085e306b12SDouglas Gregor SmallVector<char, 16> OutputBuffer; 9095e306b12SDouglas Gregor { 9105e306b12SDouglas Gregor llvm::BitstreamWriter OutputStream(OutputBuffer); 91160fa2888SDuncan P. N. Exon Smith if (Builder.writeIndex(OutputStream)) 9120e828958SJF Bastien return llvm::createStringError(std::errc::io_error, 9130e828958SJF Bastien "failed writing index"); 9145e306b12SDouglas Gregor } 9155e306b12SDouglas Gregor 916f69c9178SJan Korous return llvm::writeFileAtomically( 917f69c9178SJan Korous (IndexPath + "-%%%%%%%%").str(), IndexPath, 918f69c9178SJan Korous llvm::StringRef(OutputBuffer.data(), OutputBuffer.size())); 9195e306b12SDouglas Gregor } 9209aca3c61SArgyrios Kyrtzidis 9219aca3c61SArgyrios Kyrtzidis namespace { 9229aca3c61SArgyrios Kyrtzidis class GlobalIndexIdentifierIterator : public IdentifierIterator { 9239fc8faf9SAdrian Prantl /// The current position within the identifier lookup table. 9249aca3c61SArgyrios Kyrtzidis IdentifierIndexTable::key_iterator Current; 9259aca3c61SArgyrios Kyrtzidis 9269fc8faf9SAdrian Prantl /// The end position within the identifier lookup table. 9279aca3c61SArgyrios Kyrtzidis IdentifierIndexTable::key_iterator End; 9289aca3c61SArgyrios Kyrtzidis 9299aca3c61SArgyrios Kyrtzidis public: 9309aca3c61SArgyrios Kyrtzidis explicit GlobalIndexIdentifierIterator(IdentifierIndexTable &Idx) { 9319aca3c61SArgyrios Kyrtzidis Current = Idx.key_begin(); 9329aca3c61SArgyrios Kyrtzidis End = Idx.key_end(); 9339aca3c61SArgyrios Kyrtzidis } 9349aca3c61SArgyrios Kyrtzidis 9353e89dfeeSCraig Topper StringRef Next() override { 9369aca3c61SArgyrios Kyrtzidis if (Current == End) 9379aca3c61SArgyrios Kyrtzidis return StringRef(); 9389aca3c61SArgyrios Kyrtzidis 9399aca3c61SArgyrios Kyrtzidis StringRef Result = *Current; 9409aca3c61SArgyrios Kyrtzidis ++Current; 9419aca3c61SArgyrios Kyrtzidis return Result; 9429aca3c61SArgyrios Kyrtzidis } 9439aca3c61SArgyrios Kyrtzidis }; 944ab9db510SAlexander Kornienko } 9459aca3c61SArgyrios Kyrtzidis 9469aca3c61SArgyrios Kyrtzidis IdentifierIterator *GlobalModuleIndex::createIdentifierIterator() const { 9479aca3c61SArgyrios Kyrtzidis IdentifierIndexTable &Table = 9489aca3c61SArgyrios Kyrtzidis *static_cast<IdentifierIndexTable *>(IdentifierIndex); 9499aca3c61SArgyrios Kyrtzidis return new GlobalIndexIdentifierIterator(Table); 9509aca3c61SArgyrios Kyrtzidis } 951