10b57cec5SDimitry Andric //===--- GlobalModuleIndex.cpp - Global Module Index ------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the GlobalModuleIndex class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
13480093f4SDimitry Andric #include "clang/Serialization/GlobalModuleIndex.h"
140b57cec5SDimitry Andric #include "ASTReaderInternals.h"
150b57cec5SDimitry Andric #include "clang/Basic/FileManager.h"
160b57cec5SDimitry Andric #include "clang/Lex/HeaderSearch.h"
170b57cec5SDimitry Andric #include "clang/Serialization/ASTBitCodes.h"
18480093f4SDimitry Andric #include "clang/Serialization/ModuleFile.h"
190b57cec5SDimitry Andric #include "clang/Serialization/PCHContainerOperations.h"
200b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
210b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h"
220b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
23a7dea167SDimitry Andric #include "llvm/ADT/StringRef.h"
240b57cec5SDimitry Andric #include "llvm/Bitstream/BitstreamReader.h"
250b57cec5SDimitry Andric #include "llvm/Bitstream/BitstreamWriter.h"
260b57cec5SDimitry Andric #include "llvm/Support/DJB.h"
270b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
28a7dea167SDimitry Andric #include "llvm/Support/FileUtilities.h"
290b57cec5SDimitry Andric #include "llvm/Support/LockFileManager.h"
300b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
310b57cec5SDimitry Andric #include "llvm/Support/OnDiskHashTable.h"
320b57cec5SDimitry Andric #include "llvm/Support/Path.h"
330b57cec5SDimitry Andric #include "llvm/Support/TimeProfiler.h"
340b57cec5SDimitry Andric #include <cstdio>
350b57cec5SDimitry Andric using namespace clang;
360b57cec5SDimitry Andric using namespace serialization;
370b57cec5SDimitry Andric
380b57cec5SDimitry Andric //----------------------------------------------------------------------------//
390b57cec5SDimitry Andric // Shared constants
400b57cec5SDimitry Andric //----------------------------------------------------------------------------//
410b57cec5SDimitry Andric namespace {
420b57cec5SDimitry Andric enum {
430b57cec5SDimitry Andric /// The block containing the index.
440b57cec5SDimitry Andric GLOBAL_INDEX_BLOCK_ID = llvm::bitc::FIRST_APPLICATION_BLOCKID
450b57cec5SDimitry Andric };
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric /// Describes the record types in the index.
480b57cec5SDimitry Andric enum IndexRecordTypes {
490b57cec5SDimitry Andric /// Contains version information and potentially other metadata,
500b57cec5SDimitry Andric /// used to determine if we can read this global index file.
510b57cec5SDimitry Andric INDEX_METADATA,
520b57cec5SDimitry Andric /// Describes a module, including its file name and dependencies.
530b57cec5SDimitry Andric MODULE,
540b57cec5SDimitry Andric /// The index for identifiers.
550b57cec5SDimitry Andric IDENTIFIER_INDEX
560b57cec5SDimitry Andric };
570b57cec5SDimitry Andric }
580b57cec5SDimitry Andric
590b57cec5SDimitry Andric /// The name of the global index file.
600b57cec5SDimitry Andric static const char * const IndexFileName = "modules.idx";
610b57cec5SDimitry Andric
620b57cec5SDimitry Andric /// The global index file version.
630b57cec5SDimitry Andric static const unsigned CurrentVersion = 1;
640b57cec5SDimitry Andric
650b57cec5SDimitry Andric //----------------------------------------------------------------------------//
660b57cec5SDimitry Andric // Global module index reader.
670b57cec5SDimitry Andric //----------------------------------------------------------------------------//
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric namespace {
700b57cec5SDimitry Andric
710b57cec5SDimitry Andric /// Trait used to read the identifier index from the on-disk hash
720b57cec5SDimitry Andric /// table.
730b57cec5SDimitry Andric class IdentifierIndexReaderTrait {
740b57cec5SDimitry Andric public:
750b57cec5SDimitry Andric typedef StringRef external_key_type;
760b57cec5SDimitry Andric typedef StringRef internal_key_type;
770b57cec5SDimitry Andric typedef SmallVector<unsigned, 2> data_type;
780b57cec5SDimitry Andric typedef unsigned hash_value_type;
790b57cec5SDimitry Andric typedef unsigned offset_type;
800b57cec5SDimitry Andric
EqualKey(const internal_key_type & a,const internal_key_type & b)810b57cec5SDimitry Andric static bool EqualKey(const internal_key_type& a, const internal_key_type& b) {
820b57cec5SDimitry Andric return a == b;
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric
ComputeHash(const internal_key_type & a)850b57cec5SDimitry Andric static hash_value_type ComputeHash(const internal_key_type& a) {
860b57cec5SDimitry Andric return llvm::djbHash(a);
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric static std::pair<unsigned, unsigned>
ReadKeyDataLength(const unsigned char * & d)900b57cec5SDimitry Andric ReadKeyDataLength(const unsigned char*& d) {
910b57cec5SDimitry Andric using namespace llvm::support;
920b57cec5SDimitry Andric unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
930b57cec5SDimitry Andric unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
940b57cec5SDimitry Andric return std::make_pair(KeyLen, DataLen);
950b57cec5SDimitry Andric }
960b57cec5SDimitry Andric
970b57cec5SDimitry Andric static const internal_key_type&
GetInternalKey(const external_key_type & x)980b57cec5SDimitry Andric GetInternalKey(const external_key_type& x) { return x; }
990b57cec5SDimitry Andric
1000b57cec5SDimitry Andric static const external_key_type&
GetExternalKey(const internal_key_type & x)1010b57cec5SDimitry Andric GetExternalKey(const internal_key_type& x) { return x; }
1020b57cec5SDimitry Andric
ReadKey(const unsigned char * d,unsigned n)1030b57cec5SDimitry Andric static internal_key_type ReadKey(const unsigned char* d, unsigned n) {
1040b57cec5SDimitry Andric return StringRef((const char *)d, n);
1050b57cec5SDimitry Andric }
1060b57cec5SDimitry Andric
ReadData(const internal_key_type & k,const unsigned char * d,unsigned DataLen)1070b57cec5SDimitry Andric static data_type ReadData(const internal_key_type& k,
1080b57cec5SDimitry Andric const unsigned char* d,
1090b57cec5SDimitry Andric unsigned DataLen) {
1100b57cec5SDimitry Andric using namespace llvm::support;
1110b57cec5SDimitry Andric
1120b57cec5SDimitry Andric data_type Result;
1130b57cec5SDimitry Andric while (DataLen > 0) {
1140b57cec5SDimitry Andric unsigned ID = endian::readNext<uint32_t, little, unaligned>(d);
1150b57cec5SDimitry Andric Result.push_back(ID);
1160b57cec5SDimitry Andric DataLen -= 4;
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric
1190b57cec5SDimitry Andric return Result;
1200b57cec5SDimitry Andric }
1210b57cec5SDimitry Andric };
1220b57cec5SDimitry Andric
1230b57cec5SDimitry Andric typedef llvm::OnDiskIterableChainedHashTable<IdentifierIndexReaderTrait>
1240b57cec5SDimitry Andric IdentifierIndexTable;
1250b57cec5SDimitry Andric
1260b57cec5SDimitry Andric }
1270b57cec5SDimitry Andric
GlobalModuleIndex(std::unique_ptr<llvm::MemoryBuffer> IndexBuffer,llvm::BitstreamCursor Cursor)128480093f4SDimitry Andric GlobalModuleIndex::GlobalModuleIndex(
129480093f4SDimitry Andric std::unique_ptr<llvm::MemoryBuffer> IndexBuffer,
1300b57cec5SDimitry Andric llvm::BitstreamCursor Cursor)
131480093f4SDimitry Andric : Buffer(std::move(IndexBuffer)), IdentifierIndex(), NumIdentifierLookups(),
1320b57cec5SDimitry Andric NumIdentifierLookupHits() {
133480093f4SDimitry Andric auto Fail = [&](llvm::Error &&Err) {
1340b57cec5SDimitry Andric report_fatal_error("Module index '" + Buffer->getBufferIdentifier() +
1350b57cec5SDimitry Andric "' failed: " + toString(std::move(Err)));
1360b57cec5SDimitry Andric };
1370b57cec5SDimitry Andric
138480093f4SDimitry Andric llvm::TimeTraceScope TimeScope("Module LoadIndex");
1390b57cec5SDimitry Andric // Read the global index.
1400b57cec5SDimitry Andric bool InGlobalIndexBlock = false;
1410b57cec5SDimitry Andric bool Done = false;
1420b57cec5SDimitry Andric while (!Done) {
1430b57cec5SDimitry Andric llvm::BitstreamEntry Entry;
1440b57cec5SDimitry Andric if (Expected<llvm::BitstreamEntry> Res = Cursor.advance())
1450b57cec5SDimitry Andric Entry = Res.get();
1460b57cec5SDimitry Andric else
1470b57cec5SDimitry Andric Fail(Res.takeError());
1480b57cec5SDimitry Andric
1490b57cec5SDimitry Andric switch (Entry.Kind) {
1500b57cec5SDimitry Andric case llvm::BitstreamEntry::Error:
1510b57cec5SDimitry Andric return;
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andric case llvm::BitstreamEntry::EndBlock:
1540b57cec5SDimitry Andric if (InGlobalIndexBlock) {
1550b57cec5SDimitry Andric InGlobalIndexBlock = false;
1560b57cec5SDimitry Andric Done = true;
1570b57cec5SDimitry Andric continue;
1580b57cec5SDimitry Andric }
1590b57cec5SDimitry Andric return;
1600b57cec5SDimitry Andric
1610b57cec5SDimitry Andric
1620b57cec5SDimitry Andric case llvm::BitstreamEntry::Record:
1630b57cec5SDimitry Andric // Entries in the global index block are handled below.
1640b57cec5SDimitry Andric if (InGlobalIndexBlock)
1650b57cec5SDimitry Andric break;
1660b57cec5SDimitry Andric
1670b57cec5SDimitry Andric return;
1680b57cec5SDimitry Andric
1690b57cec5SDimitry Andric case llvm::BitstreamEntry::SubBlock:
1700b57cec5SDimitry Andric if (!InGlobalIndexBlock && Entry.ID == GLOBAL_INDEX_BLOCK_ID) {
1710b57cec5SDimitry Andric if (llvm::Error Err = Cursor.EnterSubBlock(GLOBAL_INDEX_BLOCK_ID))
1720b57cec5SDimitry Andric Fail(std::move(Err));
1730b57cec5SDimitry Andric InGlobalIndexBlock = true;
1740b57cec5SDimitry Andric } else if (llvm::Error Err = Cursor.SkipBlock())
1750b57cec5SDimitry Andric Fail(std::move(Err));
1760b57cec5SDimitry Andric continue;
1770b57cec5SDimitry Andric }
1780b57cec5SDimitry Andric
1790b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record;
1800b57cec5SDimitry Andric StringRef Blob;
1810b57cec5SDimitry Andric Expected<unsigned> MaybeIndexRecord =
1820b57cec5SDimitry Andric Cursor.readRecord(Entry.ID, Record, &Blob);
1830b57cec5SDimitry Andric if (!MaybeIndexRecord)
1840b57cec5SDimitry Andric Fail(MaybeIndexRecord.takeError());
1850b57cec5SDimitry Andric IndexRecordTypes IndexRecord =
1860b57cec5SDimitry Andric static_cast<IndexRecordTypes>(MaybeIndexRecord.get());
1870b57cec5SDimitry Andric switch (IndexRecord) {
1880b57cec5SDimitry Andric case INDEX_METADATA:
1890b57cec5SDimitry Andric // Make sure that the version matches.
1900b57cec5SDimitry Andric if (Record.size() < 1 || Record[0] != CurrentVersion)
1910b57cec5SDimitry Andric return;
1920b57cec5SDimitry Andric break;
1930b57cec5SDimitry Andric
1940b57cec5SDimitry Andric case MODULE: {
1950b57cec5SDimitry Andric unsigned Idx = 0;
1960b57cec5SDimitry Andric unsigned ID = Record[Idx++];
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andric // Make room for this module's information.
1990b57cec5SDimitry Andric if (ID == Modules.size())
2000b57cec5SDimitry Andric Modules.push_back(ModuleInfo());
2010b57cec5SDimitry Andric else
2020b57cec5SDimitry Andric Modules.resize(ID + 1);
2030b57cec5SDimitry Andric
2040b57cec5SDimitry Andric // Size/modification time for this module file at the time the
2050b57cec5SDimitry Andric // global index was built.
2060b57cec5SDimitry Andric Modules[ID].Size = Record[Idx++];
2070b57cec5SDimitry Andric Modules[ID].ModTime = Record[Idx++];
2080b57cec5SDimitry Andric
2090b57cec5SDimitry Andric // File name.
2100b57cec5SDimitry Andric unsigned NameLen = Record[Idx++];
2110b57cec5SDimitry Andric Modules[ID].FileName.assign(Record.begin() + Idx,
2120b57cec5SDimitry Andric Record.begin() + Idx + NameLen);
2130b57cec5SDimitry Andric Idx += NameLen;
2140b57cec5SDimitry Andric
2150b57cec5SDimitry Andric // Dependencies
2160b57cec5SDimitry Andric unsigned NumDeps = Record[Idx++];
2170b57cec5SDimitry Andric Modules[ID].Dependencies.insert(Modules[ID].Dependencies.end(),
2180b57cec5SDimitry Andric Record.begin() + Idx,
2190b57cec5SDimitry Andric Record.begin() + Idx + NumDeps);
2200b57cec5SDimitry Andric Idx += NumDeps;
2210b57cec5SDimitry Andric
2220b57cec5SDimitry Andric // Make sure we're at the end of the record.
2230b57cec5SDimitry Andric assert(Idx == Record.size() && "More module info?");
2240b57cec5SDimitry Andric
2250b57cec5SDimitry Andric // Record this module as an unresolved module.
2260b57cec5SDimitry Andric // FIXME: this doesn't work correctly for module names containing path
2270b57cec5SDimitry Andric // separators.
2280b57cec5SDimitry Andric StringRef ModuleName = llvm::sys::path::stem(Modules[ID].FileName);
2290b57cec5SDimitry Andric // Remove the -<hash of ModuleMapPath>
2300b57cec5SDimitry Andric ModuleName = ModuleName.rsplit('-').first;
2310b57cec5SDimitry Andric UnresolvedModules[ModuleName] = ID;
2320b57cec5SDimitry Andric break;
2330b57cec5SDimitry Andric }
2340b57cec5SDimitry Andric
2350b57cec5SDimitry Andric case IDENTIFIER_INDEX:
2360b57cec5SDimitry Andric // Wire up the identifier index.
2370b57cec5SDimitry Andric if (Record[0]) {
2380b57cec5SDimitry Andric IdentifierIndex = IdentifierIndexTable::Create(
2390b57cec5SDimitry Andric (const unsigned char *)Blob.data() + Record[0],
2400b57cec5SDimitry Andric (const unsigned char *)Blob.data() + sizeof(uint32_t),
2410b57cec5SDimitry Andric (const unsigned char *)Blob.data(), IdentifierIndexReaderTrait());
2420b57cec5SDimitry Andric }
2430b57cec5SDimitry Andric break;
2440b57cec5SDimitry Andric }
2450b57cec5SDimitry Andric }
2460b57cec5SDimitry Andric }
2470b57cec5SDimitry Andric
~GlobalModuleIndex()2480b57cec5SDimitry Andric GlobalModuleIndex::~GlobalModuleIndex() {
2490b57cec5SDimitry Andric delete static_cast<IdentifierIndexTable *>(IdentifierIndex);
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric
2520b57cec5SDimitry Andric std::pair<GlobalModuleIndex *, llvm::Error>
readIndex(StringRef Path)2530b57cec5SDimitry Andric GlobalModuleIndex::readIndex(StringRef Path) {
2540b57cec5SDimitry Andric // Load the index file, if it's there.
2550b57cec5SDimitry Andric llvm::SmallString<128> IndexPath;
2560b57cec5SDimitry Andric IndexPath += Path;
2570b57cec5SDimitry Andric llvm::sys::path::append(IndexPath, IndexFileName);
2580b57cec5SDimitry Andric
2590b57cec5SDimitry Andric llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> BufferOrErr =
2600b57cec5SDimitry Andric llvm::MemoryBuffer::getFile(IndexPath.c_str());
2610b57cec5SDimitry Andric if (!BufferOrErr)
2620b57cec5SDimitry Andric return std::make_pair(nullptr,
2630b57cec5SDimitry Andric llvm::errorCodeToError(BufferOrErr.getError()));
2640b57cec5SDimitry Andric std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(BufferOrErr.get());
2650b57cec5SDimitry Andric
2660b57cec5SDimitry Andric /// The main bitstream cursor for the main block.
2670b57cec5SDimitry Andric llvm::BitstreamCursor Cursor(*Buffer);
2680b57cec5SDimitry Andric
2690b57cec5SDimitry Andric // Sniff for the signature.
2700b57cec5SDimitry Andric for (unsigned char C : {'B', 'C', 'G', 'I'}) {
2710b57cec5SDimitry Andric if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Cursor.Read(8)) {
2720b57cec5SDimitry Andric if (Res.get() != C)
2730b57cec5SDimitry Andric return std::make_pair(
2740b57cec5SDimitry Andric nullptr, llvm::createStringError(std::errc::illegal_byte_sequence,
2750b57cec5SDimitry Andric "expected signature BCGI"));
2760b57cec5SDimitry Andric } else
2770b57cec5SDimitry Andric return std::make_pair(nullptr, Res.takeError());
2780b57cec5SDimitry Andric }
2790b57cec5SDimitry Andric
2800b57cec5SDimitry Andric return std::make_pair(new GlobalModuleIndex(std::move(Buffer), Cursor),
2810b57cec5SDimitry Andric llvm::Error::success());
2820b57cec5SDimitry Andric }
2830b57cec5SDimitry Andric
2840b57cec5SDimitry Andric void
getKnownModules(SmallVectorImpl<ModuleFile * > & ModuleFiles)2850b57cec5SDimitry Andric GlobalModuleIndex::getKnownModules(SmallVectorImpl<ModuleFile *> &ModuleFiles) {
2860b57cec5SDimitry Andric ModuleFiles.clear();
2870b57cec5SDimitry Andric for (unsigned I = 0, N = Modules.size(); I != N; ++I) {
2880b57cec5SDimitry Andric if (ModuleFile *MF = Modules[I].File)
2890b57cec5SDimitry Andric ModuleFiles.push_back(MF);
2900b57cec5SDimitry Andric }
2910b57cec5SDimitry Andric }
2920b57cec5SDimitry Andric
getModuleDependencies(ModuleFile * File,SmallVectorImpl<ModuleFile * > & Dependencies)2930b57cec5SDimitry Andric void GlobalModuleIndex::getModuleDependencies(
2940b57cec5SDimitry Andric ModuleFile *File,
2950b57cec5SDimitry Andric SmallVectorImpl<ModuleFile *> &Dependencies) {
2960b57cec5SDimitry Andric // Look for information about this module file.
2970b57cec5SDimitry Andric llvm::DenseMap<ModuleFile *, unsigned>::iterator Known
2980b57cec5SDimitry Andric = ModulesByFile.find(File);
2990b57cec5SDimitry Andric if (Known == ModulesByFile.end())
3000b57cec5SDimitry Andric return;
3010b57cec5SDimitry Andric
3020b57cec5SDimitry Andric // Record dependencies.
3030b57cec5SDimitry Andric Dependencies.clear();
3040b57cec5SDimitry Andric ArrayRef<unsigned> StoredDependencies = Modules[Known->second].Dependencies;
3050b57cec5SDimitry Andric for (unsigned I = 0, N = StoredDependencies.size(); I != N; ++I) {
3060b57cec5SDimitry Andric if (ModuleFile *MF = Modules[I].File)
3070b57cec5SDimitry Andric Dependencies.push_back(MF);
3080b57cec5SDimitry Andric }
3090b57cec5SDimitry Andric }
3100b57cec5SDimitry Andric
lookupIdentifier(StringRef Name,HitSet & Hits)3110b57cec5SDimitry Andric bool GlobalModuleIndex::lookupIdentifier(StringRef Name, HitSet &Hits) {
3120b57cec5SDimitry Andric Hits.clear();
3130b57cec5SDimitry Andric
3140b57cec5SDimitry Andric // If there's no identifier index, there is nothing we can do.
3150b57cec5SDimitry Andric if (!IdentifierIndex)
3160b57cec5SDimitry Andric return false;
3170b57cec5SDimitry Andric
3180b57cec5SDimitry Andric // Look into the identifier index.
3190b57cec5SDimitry Andric ++NumIdentifierLookups;
3200b57cec5SDimitry Andric IdentifierIndexTable &Table
3210b57cec5SDimitry Andric = *static_cast<IdentifierIndexTable *>(IdentifierIndex);
3220b57cec5SDimitry Andric IdentifierIndexTable::iterator Known = Table.find(Name);
3230b57cec5SDimitry Andric if (Known == Table.end()) {
3245ffd83dbSDimitry Andric return false;
3250b57cec5SDimitry Andric }
3260b57cec5SDimitry Andric
3270b57cec5SDimitry Andric SmallVector<unsigned, 2> ModuleIDs = *Known;
3280b57cec5SDimitry Andric for (unsigned I = 0, N = ModuleIDs.size(); I != N; ++I) {
3290b57cec5SDimitry Andric if (ModuleFile *MF = Modules[ModuleIDs[I]].File)
3300b57cec5SDimitry Andric Hits.insert(MF);
3310b57cec5SDimitry Andric }
3320b57cec5SDimitry Andric
3330b57cec5SDimitry Andric ++NumIdentifierLookupHits;
3340b57cec5SDimitry Andric return true;
3350b57cec5SDimitry Andric }
3360b57cec5SDimitry Andric
loadedModuleFile(ModuleFile * File)3370b57cec5SDimitry Andric bool GlobalModuleIndex::loadedModuleFile(ModuleFile *File) {
3380b57cec5SDimitry Andric // Look for the module in the global module index based on the module name.
3390b57cec5SDimitry Andric StringRef Name = File->ModuleName;
3400b57cec5SDimitry Andric llvm::StringMap<unsigned>::iterator Known = UnresolvedModules.find(Name);
3410b57cec5SDimitry Andric if (Known == UnresolvedModules.end()) {
3420b57cec5SDimitry Andric return true;
3430b57cec5SDimitry Andric }
3440b57cec5SDimitry Andric
3450b57cec5SDimitry Andric // Rectify this module with the global module index.
3460b57cec5SDimitry Andric ModuleInfo &Info = Modules[Known->second];
3470b57cec5SDimitry Andric
3480b57cec5SDimitry Andric // If the size and modification time match what we expected, record this
3490b57cec5SDimitry Andric // module file.
3500b57cec5SDimitry Andric bool Failed = true;
3510b57cec5SDimitry Andric if (File->File->getSize() == Info.Size &&
3520b57cec5SDimitry Andric File->File->getModificationTime() == Info.ModTime) {
3530b57cec5SDimitry Andric Info.File = File;
3540b57cec5SDimitry Andric ModulesByFile[File] = Known->second;
3550b57cec5SDimitry Andric
3560b57cec5SDimitry Andric Failed = false;
3570b57cec5SDimitry Andric }
3580b57cec5SDimitry Andric
3590b57cec5SDimitry Andric // One way or another, we have resolved this module file.
3600b57cec5SDimitry Andric UnresolvedModules.erase(Known);
3610b57cec5SDimitry Andric return Failed;
3620b57cec5SDimitry Andric }
3630b57cec5SDimitry Andric
printStats()3640b57cec5SDimitry Andric void GlobalModuleIndex::printStats() {
3650b57cec5SDimitry Andric std::fprintf(stderr, "*** Global Module Index Statistics:\n");
3660b57cec5SDimitry Andric if (NumIdentifierLookups) {
3670b57cec5SDimitry Andric fprintf(stderr, " %u / %u identifier lookups succeeded (%f%%)\n",
3680b57cec5SDimitry Andric NumIdentifierLookupHits, NumIdentifierLookups,
3690b57cec5SDimitry Andric (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
3700b57cec5SDimitry Andric }
3710b57cec5SDimitry Andric std::fprintf(stderr, "\n");
3720b57cec5SDimitry Andric }
3730b57cec5SDimitry Andric
dump()3740b57cec5SDimitry Andric LLVM_DUMP_METHOD void GlobalModuleIndex::dump() {
3750b57cec5SDimitry Andric llvm::errs() << "*** Global Module Index Dump:\n";
3760b57cec5SDimitry Andric llvm::errs() << "Module files:\n";
3770b57cec5SDimitry Andric for (auto &MI : Modules) {
3780b57cec5SDimitry Andric llvm::errs() << "** " << MI.FileName << "\n";
3790b57cec5SDimitry Andric if (MI.File)
3800b57cec5SDimitry Andric MI.File->dump();
3810b57cec5SDimitry Andric else
3820b57cec5SDimitry Andric llvm::errs() << "\n";
3830b57cec5SDimitry Andric }
3840b57cec5SDimitry Andric llvm::errs() << "\n";
3850b57cec5SDimitry Andric }
3860b57cec5SDimitry Andric
3870b57cec5SDimitry Andric //----------------------------------------------------------------------------//
3880b57cec5SDimitry Andric // Global module index writer.
3890b57cec5SDimitry Andric //----------------------------------------------------------------------------//
3900b57cec5SDimitry Andric
3910b57cec5SDimitry Andric namespace {
3920b57cec5SDimitry Andric /// Provides information about a specific module file.
3930b57cec5SDimitry Andric struct ModuleFileInfo {
3940b57cec5SDimitry Andric /// The numberic ID for this module file.
3950b57cec5SDimitry Andric unsigned ID;
3960b57cec5SDimitry Andric
3970b57cec5SDimitry Andric /// The set of modules on which this module depends. Each entry is
3980b57cec5SDimitry Andric /// a module ID.
3990b57cec5SDimitry Andric SmallVector<unsigned, 4> Dependencies;
4000b57cec5SDimitry Andric ASTFileSignature Signature;
4010b57cec5SDimitry Andric };
4020b57cec5SDimitry Andric
4030b57cec5SDimitry Andric struct ImportedModuleFileInfo {
4040b57cec5SDimitry Andric off_t StoredSize;
4050b57cec5SDimitry Andric time_t StoredModTime;
4060b57cec5SDimitry Andric ASTFileSignature StoredSignature;
ImportedModuleFileInfo__anon989f7eea0511::ImportedModuleFileInfo4070b57cec5SDimitry Andric ImportedModuleFileInfo(off_t Size, time_t ModTime, ASTFileSignature Sig)
4080b57cec5SDimitry Andric : StoredSize(Size), StoredModTime(ModTime), StoredSignature(Sig) {}
4090b57cec5SDimitry Andric };
4100b57cec5SDimitry Andric
4110b57cec5SDimitry Andric /// Builder that generates the global module index file.
4120b57cec5SDimitry Andric class GlobalModuleIndexBuilder {
4130b57cec5SDimitry Andric FileManager &FileMgr;
4140b57cec5SDimitry Andric const PCHContainerReader &PCHContainerRdr;
4150b57cec5SDimitry Andric
4160b57cec5SDimitry Andric /// Mapping from files to module file information.
4170b57cec5SDimitry Andric typedef llvm::MapVector<const FileEntry *, ModuleFileInfo> ModuleFilesMap;
4180b57cec5SDimitry Andric
4190b57cec5SDimitry Andric /// Information about each of the known module files.
4200b57cec5SDimitry Andric ModuleFilesMap ModuleFiles;
4210b57cec5SDimitry Andric
4220b57cec5SDimitry Andric /// Mapping from the imported module file to the imported
4230b57cec5SDimitry Andric /// information.
4240b57cec5SDimitry Andric typedef std::multimap<const FileEntry *, ImportedModuleFileInfo>
4250b57cec5SDimitry Andric ImportedModuleFilesMap;
4260b57cec5SDimitry Andric
4270b57cec5SDimitry Andric /// Information about each importing of a module file.
4280b57cec5SDimitry Andric ImportedModuleFilesMap ImportedModuleFiles;
4290b57cec5SDimitry Andric
4300b57cec5SDimitry Andric /// Mapping from identifiers to the list of module file IDs that
4310b57cec5SDimitry Andric /// consider this identifier to be interesting.
4320b57cec5SDimitry Andric typedef llvm::StringMap<SmallVector<unsigned, 2> > InterestingIdentifierMap;
4330b57cec5SDimitry Andric
4340b57cec5SDimitry Andric /// A mapping from all interesting identifiers to the set of module
4350b57cec5SDimitry Andric /// files in which those identifiers are considered interesting.
4360b57cec5SDimitry Andric InterestingIdentifierMap InterestingIdentifiers;
4370b57cec5SDimitry Andric
4380b57cec5SDimitry Andric /// Write the block-info block for the global module index file.
4390b57cec5SDimitry Andric void emitBlockInfoBlock(llvm::BitstreamWriter &Stream);
4400b57cec5SDimitry Andric
4410b57cec5SDimitry Andric /// Retrieve the module file information for the given file.
getModuleFileInfo(const FileEntry * File)4420b57cec5SDimitry Andric ModuleFileInfo &getModuleFileInfo(const FileEntry *File) {
4430b57cec5SDimitry Andric llvm::MapVector<const FileEntry *, ModuleFileInfo>::iterator Known
4440b57cec5SDimitry Andric = ModuleFiles.find(File);
4450b57cec5SDimitry Andric if (Known != ModuleFiles.end())
4460b57cec5SDimitry Andric return Known->second;
4470b57cec5SDimitry Andric
4480b57cec5SDimitry Andric unsigned NewID = ModuleFiles.size();
4490b57cec5SDimitry Andric ModuleFileInfo &Info = ModuleFiles[File];
4500b57cec5SDimitry Andric Info.ID = NewID;
4510b57cec5SDimitry Andric return Info;
4520b57cec5SDimitry Andric }
4530b57cec5SDimitry Andric
4540b57cec5SDimitry Andric public:
GlobalModuleIndexBuilder(FileManager & FileMgr,const PCHContainerReader & PCHContainerRdr)4550b57cec5SDimitry Andric explicit GlobalModuleIndexBuilder(
4560b57cec5SDimitry Andric FileManager &FileMgr, const PCHContainerReader &PCHContainerRdr)
4570b57cec5SDimitry Andric : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr) {}
4580b57cec5SDimitry Andric
4590b57cec5SDimitry Andric /// Load the contents of the given module file into the builder.
4600b57cec5SDimitry Andric llvm::Error loadModuleFile(const FileEntry *File);
4610b57cec5SDimitry Andric
4620b57cec5SDimitry Andric /// Write the index to the given bitstream.
4630b57cec5SDimitry Andric /// \returns true if an error occurred, false otherwise.
4640b57cec5SDimitry Andric bool writeIndex(llvm::BitstreamWriter &Stream);
4650b57cec5SDimitry Andric };
4660b57cec5SDimitry Andric }
4670b57cec5SDimitry Andric
emitBlockID(unsigned ID,const char * Name,llvm::BitstreamWriter & Stream,SmallVectorImpl<uint64_t> & Record)4680b57cec5SDimitry Andric static void emitBlockID(unsigned ID, const char *Name,
4690b57cec5SDimitry Andric llvm::BitstreamWriter &Stream,
4700b57cec5SDimitry Andric SmallVectorImpl<uint64_t> &Record) {
4710b57cec5SDimitry Andric Record.clear();
4720b57cec5SDimitry Andric Record.push_back(ID);
4730b57cec5SDimitry Andric Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
4740b57cec5SDimitry Andric
4750b57cec5SDimitry Andric // Emit the block name if present.
4760b57cec5SDimitry Andric if (!Name || Name[0] == 0) return;
4770b57cec5SDimitry Andric Record.clear();
4780b57cec5SDimitry Andric while (*Name)
4790b57cec5SDimitry Andric Record.push_back(*Name++);
4800b57cec5SDimitry Andric Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
4810b57cec5SDimitry Andric }
4820b57cec5SDimitry Andric
emitRecordID(unsigned ID,const char * Name,llvm::BitstreamWriter & Stream,SmallVectorImpl<uint64_t> & Record)4830b57cec5SDimitry Andric static void emitRecordID(unsigned ID, const char *Name,
4840b57cec5SDimitry Andric llvm::BitstreamWriter &Stream,
4850b57cec5SDimitry Andric SmallVectorImpl<uint64_t> &Record) {
4860b57cec5SDimitry Andric Record.clear();
4870b57cec5SDimitry Andric Record.push_back(ID);
4880b57cec5SDimitry Andric while (*Name)
4890b57cec5SDimitry Andric Record.push_back(*Name++);
4900b57cec5SDimitry Andric Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
4910b57cec5SDimitry Andric }
4920b57cec5SDimitry Andric
4930b57cec5SDimitry Andric void
emitBlockInfoBlock(llvm::BitstreamWriter & Stream)4940b57cec5SDimitry Andric GlobalModuleIndexBuilder::emitBlockInfoBlock(llvm::BitstreamWriter &Stream) {
4950b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record;
4960b57cec5SDimitry Andric Stream.EnterBlockInfoBlock();
4970b57cec5SDimitry Andric
4980b57cec5SDimitry Andric #define BLOCK(X) emitBlockID(X ## _ID, #X, Stream, Record)
4990b57cec5SDimitry Andric #define RECORD(X) emitRecordID(X, #X, Stream, Record)
5000b57cec5SDimitry Andric BLOCK(GLOBAL_INDEX_BLOCK);
5010b57cec5SDimitry Andric RECORD(INDEX_METADATA);
5020b57cec5SDimitry Andric RECORD(MODULE);
5030b57cec5SDimitry Andric RECORD(IDENTIFIER_INDEX);
5040b57cec5SDimitry Andric #undef RECORD
5050b57cec5SDimitry Andric #undef BLOCK
5060b57cec5SDimitry Andric
5070b57cec5SDimitry Andric Stream.ExitBlock();
5080b57cec5SDimitry Andric }
5090b57cec5SDimitry Andric
5100b57cec5SDimitry Andric namespace {
5110b57cec5SDimitry Andric class InterestingASTIdentifierLookupTrait
5120b57cec5SDimitry Andric : public serialization::reader::ASTIdentifierLookupTraitBase {
5130b57cec5SDimitry Andric
5140b57cec5SDimitry Andric public:
5150b57cec5SDimitry Andric /// The identifier and whether it is "interesting".
5160b57cec5SDimitry Andric typedef std::pair<StringRef, bool> data_type;
5170b57cec5SDimitry Andric
ReadData(const internal_key_type & k,const unsigned char * d,unsigned DataLen)5180b57cec5SDimitry Andric data_type ReadData(const internal_key_type& k,
5190b57cec5SDimitry Andric const unsigned char* d,
5200b57cec5SDimitry Andric unsigned DataLen) {
5210b57cec5SDimitry Andric // The first bit indicates whether this identifier is interesting.
5220b57cec5SDimitry Andric // That's all we care about.
5230b57cec5SDimitry Andric using namespace llvm::support;
5240b57cec5SDimitry Andric unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
5250b57cec5SDimitry Andric bool IsInteresting = RawID & 0x01;
5260b57cec5SDimitry Andric return std::make_pair(k, IsInteresting);
5270b57cec5SDimitry Andric }
5280b57cec5SDimitry Andric };
5290b57cec5SDimitry Andric }
5300b57cec5SDimitry Andric
loadModuleFile(const FileEntry * File)5310b57cec5SDimitry Andric llvm::Error GlobalModuleIndexBuilder::loadModuleFile(const FileEntry *File) {
5320b57cec5SDimitry Andric // Open the module file.
5330b57cec5SDimitry Andric
5340b57cec5SDimitry Andric auto Buffer = FileMgr.getBufferForFile(File, /*isVolatile=*/true);
5350b57cec5SDimitry Andric if (!Buffer)
5360b57cec5SDimitry Andric return llvm::createStringError(Buffer.getError(),
5370b57cec5SDimitry Andric "failed getting buffer for module file");
5380b57cec5SDimitry Andric
5390b57cec5SDimitry Andric // Initialize the input stream
5400b57cec5SDimitry Andric llvm::BitstreamCursor InStream(PCHContainerRdr.ExtractPCH(**Buffer));
5410b57cec5SDimitry Andric
5420b57cec5SDimitry Andric // Sniff for the signature.
5430b57cec5SDimitry Andric for (unsigned char C : {'C', 'P', 'C', 'H'})
5440b57cec5SDimitry Andric if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = InStream.Read(8)) {
5450b57cec5SDimitry Andric if (Res.get() != C)
5460b57cec5SDimitry Andric return llvm::createStringError(std::errc::illegal_byte_sequence,
5470b57cec5SDimitry Andric "expected signature CPCH");
5480b57cec5SDimitry Andric } else
5490b57cec5SDimitry Andric return Res.takeError();
5500b57cec5SDimitry Andric
5510b57cec5SDimitry Andric // Record this module file and assign it a unique ID (if it doesn't have
5520b57cec5SDimitry Andric // one already).
5530b57cec5SDimitry Andric unsigned ID = getModuleFileInfo(File).ID;
5540b57cec5SDimitry Andric
5550b57cec5SDimitry Andric // Search for the blocks and records we care about.
5560b57cec5SDimitry Andric enum { Other, ControlBlock, ASTBlock, DiagnosticOptionsBlock } State = Other;
5570b57cec5SDimitry Andric bool Done = false;
5580b57cec5SDimitry Andric while (!Done) {
5590b57cec5SDimitry Andric Expected<llvm::BitstreamEntry> MaybeEntry = InStream.advance();
5600b57cec5SDimitry Andric if (!MaybeEntry)
5610b57cec5SDimitry Andric return MaybeEntry.takeError();
5620b57cec5SDimitry Andric llvm::BitstreamEntry Entry = MaybeEntry.get();
5630b57cec5SDimitry Andric
5640b57cec5SDimitry Andric switch (Entry.Kind) {
5650b57cec5SDimitry Andric case llvm::BitstreamEntry::Error:
5660b57cec5SDimitry Andric Done = true;
5670b57cec5SDimitry Andric continue;
5680b57cec5SDimitry Andric
5690b57cec5SDimitry Andric case llvm::BitstreamEntry::Record:
5700b57cec5SDimitry Andric // In the 'other' state, just skip the record. We don't care.
5710b57cec5SDimitry Andric if (State == Other) {
5720b57cec5SDimitry Andric if (llvm::Expected<unsigned> Skipped = InStream.skipRecord(Entry.ID))
5730b57cec5SDimitry Andric continue;
5740b57cec5SDimitry Andric else
5750b57cec5SDimitry Andric return Skipped.takeError();
5760b57cec5SDimitry Andric }
5770b57cec5SDimitry Andric
5780b57cec5SDimitry Andric // Handle potentially-interesting records below.
5790b57cec5SDimitry Andric break;
5800b57cec5SDimitry Andric
5810b57cec5SDimitry Andric case llvm::BitstreamEntry::SubBlock:
5820b57cec5SDimitry Andric if (Entry.ID == CONTROL_BLOCK_ID) {
5830b57cec5SDimitry Andric if (llvm::Error Err = InStream.EnterSubBlock(CONTROL_BLOCK_ID))
5840b57cec5SDimitry Andric return Err;
5850b57cec5SDimitry Andric
5860b57cec5SDimitry Andric // Found the control block.
5870b57cec5SDimitry Andric State = ControlBlock;
5880b57cec5SDimitry Andric continue;
5890b57cec5SDimitry Andric }
5900b57cec5SDimitry Andric
5910b57cec5SDimitry Andric if (Entry.ID == AST_BLOCK_ID) {
5920b57cec5SDimitry Andric if (llvm::Error Err = InStream.EnterSubBlock(AST_BLOCK_ID))
5930b57cec5SDimitry Andric return Err;
5940b57cec5SDimitry Andric
5950b57cec5SDimitry Andric // Found the AST block.
5960b57cec5SDimitry Andric State = ASTBlock;
5970b57cec5SDimitry Andric continue;
5980b57cec5SDimitry Andric }
5990b57cec5SDimitry Andric
6000b57cec5SDimitry Andric if (Entry.ID == UNHASHED_CONTROL_BLOCK_ID) {
6010b57cec5SDimitry Andric if (llvm::Error Err = InStream.EnterSubBlock(UNHASHED_CONTROL_BLOCK_ID))
6020b57cec5SDimitry Andric return Err;
6030b57cec5SDimitry Andric
6040b57cec5SDimitry Andric // Found the Diagnostic Options block.
6050b57cec5SDimitry Andric State = DiagnosticOptionsBlock;
6060b57cec5SDimitry Andric continue;
6070b57cec5SDimitry Andric }
6080b57cec5SDimitry Andric
6090b57cec5SDimitry Andric if (llvm::Error Err = InStream.SkipBlock())
6100b57cec5SDimitry Andric return Err;
6110b57cec5SDimitry Andric
6120b57cec5SDimitry Andric continue;
6130b57cec5SDimitry Andric
6140b57cec5SDimitry Andric case llvm::BitstreamEntry::EndBlock:
6150b57cec5SDimitry Andric State = Other;
6160b57cec5SDimitry Andric continue;
6170b57cec5SDimitry Andric }
6180b57cec5SDimitry Andric
6190b57cec5SDimitry Andric // Read the given record.
6200b57cec5SDimitry Andric SmallVector<uint64_t, 64> Record;
6210b57cec5SDimitry Andric StringRef Blob;
6220b57cec5SDimitry Andric Expected<unsigned> MaybeCode = InStream.readRecord(Entry.ID, Record, &Blob);
6230b57cec5SDimitry Andric if (!MaybeCode)
6240b57cec5SDimitry Andric return MaybeCode.takeError();
6250b57cec5SDimitry Andric unsigned Code = MaybeCode.get();
6260b57cec5SDimitry Andric
6270b57cec5SDimitry Andric // Handle module dependencies.
6280b57cec5SDimitry Andric if (State == ControlBlock && Code == IMPORTS) {
6290b57cec5SDimitry Andric // Load each of the imported PCH files.
6300b57cec5SDimitry Andric unsigned Idx = 0, N = Record.size();
6310b57cec5SDimitry Andric while (Idx < N) {
6320b57cec5SDimitry Andric // Read information about the AST file.
6330b57cec5SDimitry Andric
6340b57cec5SDimitry Andric // Skip the imported kind
6350b57cec5SDimitry Andric ++Idx;
6360b57cec5SDimitry Andric
6370b57cec5SDimitry Andric // Skip the import location
6380b57cec5SDimitry Andric ++Idx;
6390b57cec5SDimitry Andric
6400b57cec5SDimitry Andric // Load stored size/modification time.
6410b57cec5SDimitry Andric off_t StoredSize = (off_t)Record[Idx++];
6420b57cec5SDimitry Andric time_t StoredModTime = (time_t)Record[Idx++];
6430b57cec5SDimitry Andric
6440b57cec5SDimitry Andric // Skip the stored signature.
6450b57cec5SDimitry Andric // FIXME: we could read the signature out of the import and validate it.
6465ffd83dbSDimitry Andric auto FirstSignatureByte = Record.begin() + Idx;
6475ffd83dbSDimitry Andric ASTFileSignature StoredSignature = ASTFileSignature::create(
6485ffd83dbSDimitry Andric FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
6495ffd83dbSDimitry Andric Idx += ASTFileSignature::size;
6500b57cec5SDimitry Andric
6510b57cec5SDimitry Andric // Skip the module name (currently this is only used for prebuilt
6520b57cec5SDimitry Andric // modules while here we are only dealing with cached).
6530b57cec5SDimitry Andric Idx += Record[Idx] + 1;
6540b57cec5SDimitry Andric
6550b57cec5SDimitry Andric // Retrieve the imported file name.
6560b57cec5SDimitry Andric unsigned Length = Record[Idx++];
6570b57cec5SDimitry Andric SmallString<128> ImportedFile(Record.begin() + Idx,
6580b57cec5SDimitry Andric Record.begin() + Idx + Length);
6590b57cec5SDimitry Andric Idx += Length;
6600b57cec5SDimitry Andric
6610b57cec5SDimitry Andric // Find the imported module file.
662a7dea167SDimitry Andric auto DependsOnFile
6630b57cec5SDimitry Andric = FileMgr.getFile(ImportedFile, /*OpenFile=*/false,
6640b57cec5SDimitry Andric /*CacheFailure=*/false);
6650b57cec5SDimitry Andric
6660b57cec5SDimitry Andric if (!DependsOnFile)
6670b57cec5SDimitry Andric return llvm::createStringError(std::errc::bad_file_descriptor,
6680b57cec5SDimitry Andric "imported file \"%s\" not found",
6690b57cec5SDimitry Andric ImportedFile.c_str());
6700b57cec5SDimitry Andric
6710b57cec5SDimitry Andric // Save the information in ImportedModuleFileInfo so we can verify after
6720b57cec5SDimitry Andric // loading all pcms.
6730b57cec5SDimitry Andric ImportedModuleFiles.insert(std::make_pair(
674a7dea167SDimitry Andric *DependsOnFile, ImportedModuleFileInfo(StoredSize, StoredModTime,
6750b57cec5SDimitry Andric StoredSignature)));
6760b57cec5SDimitry Andric
6770b57cec5SDimitry Andric // Record the dependency.
678a7dea167SDimitry Andric unsigned DependsOnID = getModuleFileInfo(*DependsOnFile).ID;
6790b57cec5SDimitry Andric getModuleFileInfo(File).Dependencies.push_back(DependsOnID);
6800b57cec5SDimitry Andric }
6810b57cec5SDimitry Andric
6820b57cec5SDimitry Andric continue;
6830b57cec5SDimitry Andric }
6840b57cec5SDimitry Andric
6850b57cec5SDimitry Andric // Handle the identifier table
6860b57cec5SDimitry Andric if (State == ASTBlock && Code == IDENTIFIER_TABLE && Record[0] > 0) {
6870b57cec5SDimitry Andric typedef llvm::OnDiskIterableChainedHashTable<
6880b57cec5SDimitry Andric InterestingASTIdentifierLookupTrait> InterestingIdentifierTable;
6890b57cec5SDimitry Andric std::unique_ptr<InterestingIdentifierTable> Table(
6900b57cec5SDimitry Andric InterestingIdentifierTable::Create(
6910b57cec5SDimitry Andric (const unsigned char *)Blob.data() + Record[0],
6920b57cec5SDimitry Andric (const unsigned char *)Blob.data() + sizeof(uint32_t),
6930b57cec5SDimitry Andric (const unsigned char *)Blob.data()));
6940b57cec5SDimitry Andric for (InterestingIdentifierTable::data_iterator D = Table->data_begin(),
6950b57cec5SDimitry Andric DEnd = Table->data_end();
6960b57cec5SDimitry Andric D != DEnd; ++D) {
6970b57cec5SDimitry Andric std::pair<StringRef, bool> Ident = *D;
6980b57cec5SDimitry Andric if (Ident.second)
6990b57cec5SDimitry Andric InterestingIdentifiers[Ident.first].push_back(ID);
7000b57cec5SDimitry Andric else
7010b57cec5SDimitry Andric (void)InterestingIdentifiers[Ident.first];
7020b57cec5SDimitry Andric }
7030b57cec5SDimitry Andric }
7040b57cec5SDimitry Andric
7050b57cec5SDimitry Andric // Get Signature.
7060b57cec5SDimitry Andric if (State == DiagnosticOptionsBlock && Code == SIGNATURE)
7075ffd83dbSDimitry Andric getModuleFileInfo(File).Signature = ASTFileSignature::create(
7085ffd83dbSDimitry Andric Record.begin(), Record.begin() + ASTFileSignature::size);
7090b57cec5SDimitry Andric
7100b57cec5SDimitry Andric // We don't care about this record.
7110b57cec5SDimitry Andric }
7120b57cec5SDimitry Andric
7130b57cec5SDimitry Andric return llvm::Error::success();
7140b57cec5SDimitry Andric }
7150b57cec5SDimitry Andric
7160b57cec5SDimitry Andric namespace {
7170b57cec5SDimitry Andric
7180b57cec5SDimitry Andric /// Trait used to generate the identifier index as an on-disk hash
7190b57cec5SDimitry Andric /// table.
7200b57cec5SDimitry Andric class IdentifierIndexWriterTrait {
7210b57cec5SDimitry Andric public:
7220b57cec5SDimitry Andric typedef StringRef key_type;
7230b57cec5SDimitry Andric typedef StringRef key_type_ref;
7240b57cec5SDimitry Andric typedef SmallVector<unsigned, 2> data_type;
7250b57cec5SDimitry Andric typedef const SmallVector<unsigned, 2> &data_type_ref;
7260b57cec5SDimitry Andric typedef unsigned hash_value_type;
7270b57cec5SDimitry Andric typedef unsigned offset_type;
7280b57cec5SDimitry Andric
ComputeHash(key_type_ref Key)7290b57cec5SDimitry Andric static hash_value_type ComputeHash(key_type_ref Key) {
7300b57cec5SDimitry Andric return llvm::djbHash(Key);
7310b57cec5SDimitry Andric }
7320b57cec5SDimitry Andric
7330b57cec5SDimitry Andric std::pair<unsigned,unsigned>
EmitKeyDataLength(raw_ostream & Out,key_type_ref Key,data_type_ref Data)7340b57cec5SDimitry Andric EmitKeyDataLength(raw_ostream& Out, key_type_ref Key, data_type_ref Data) {
7350b57cec5SDimitry Andric using namespace llvm::support;
7360b57cec5SDimitry Andric endian::Writer LE(Out, little);
7370b57cec5SDimitry Andric unsigned KeyLen = Key.size();
7380b57cec5SDimitry Andric unsigned DataLen = Data.size() * 4;
7390b57cec5SDimitry Andric LE.write<uint16_t>(KeyLen);
7400b57cec5SDimitry Andric LE.write<uint16_t>(DataLen);
7410b57cec5SDimitry Andric return std::make_pair(KeyLen, DataLen);
7420b57cec5SDimitry Andric }
7430b57cec5SDimitry Andric
EmitKey(raw_ostream & Out,key_type_ref Key,unsigned KeyLen)7440b57cec5SDimitry Andric void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
7450b57cec5SDimitry Andric Out.write(Key.data(), KeyLen);
7460b57cec5SDimitry Andric }
7470b57cec5SDimitry Andric
EmitData(raw_ostream & Out,key_type_ref Key,data_type_ref Data,unsigned DataLen)7480b57cec5SDimitry Andric void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
7490b57cec5SDimitry Andric unsigned DataLen) {
7500b57cec5SDimitry Andric using namespace llvm::support;
7510b57cec5SDimitry Andric for (unsigned I = 0, N = Data.size(); I != N; ++I)
7520b57cec5SDimitry Andric endian::write<uint32_t>(Out, Data[I], little);
7530b57cec5SDimitry Andric }
7540b57cec5SDimitry Andric };
7550b57cec5SDimitry Andric
7560b57cec5SDimitry Andric }
7570b57cec5SDimitry Andric
writeIndex(llvm::BitstreamWriter & Stream)7580b57cec5SDimitry Andric bool GlobalModuleIndexBuilder::writeIndex(llvm::BitstreamWriter &Stream) {
7590b57cec5SDimitry Andric for (auto MapEntry : ImportedModuleFiles) {
7600b57cec5SDimitry Andric auto *File = MapEntry.first;
7610b57cec5SDimitry Andric ImportedModuleFileInfo &Info = MapEntry.second;
7620b57cec5SDimitry Andric if (getModuleFileInfo(File).Signature) {
7630b57cec5SDimitry Andric if (getModuleFileInfo(File).Signature != Info.StoredSignature)
7640b57cec5SDimitry Andric // Verify Signature.
7650b57cec5SDimitry Andric return true;
7660b57cec5SDimitry Andric } else if (Info.StoredSize != File->getSize() ||
7670b57cec5SDimitry Andric Info.StoredModTime != File->getModificationTime())
7680b57cec5SDimitry Andric // Verify Size and ModTime.
7690b57cec5SDimitry Andric return true;
7700b57cec5SDimitry Andric }
7710b57cec5SDimitry Andric
7720b57cec5SDimitry Andric using namespace llvm;
773480093f4SDimitry Andric llvm::TimeTraceScope TimeScope("Module WriteIndex");
7740b57cec5SDimitry Andric
7750b57cec5SDimitry Andric // Emit the file header.
7760b57cec5SDimitry Andric Stream.Emit((unsigned)'B', 8);
7770b57cec5SDimitry Andric Stream.Emit((unsigned)'C', 8);
7780b57cec5SDimitry Andric Stream.Emit((unsigned)'G', 8);
7790b57cec5SDimitry Andric Stream.Emit((unsigned)'I', 8);
7800b57cec5SDimitry Andric
7810b57cec5SDimitry Andric // Write the block-info block, which describes the records in this bitcode
7820b57cec5SDimitry Andric // file.
7830b57cec5SDimitry Andric emitBlockInfoBlock(Stream);
7840b57cec5SDimitry Andric
7850b57cec5SDimitry Andric Stream.EnterSubblock(GLOBAL_INDEX_BLOCK_ID, 3);
7860b57cec5SDimitry Andric
7870b57cec5SDimitry Andric // Write the metadata.
7880b57cec5SDimitry Andric SmallVector<uint64_t, 2> Record;
7890b57cec5SDimitry Andric Record.push_back(CurrentVersion);
7900b57cec5SDimitry Andric Stream.EmitRecord(INDEX_METADATA, Record);
7910b57cec5SDimitry Andric
7920b57cec5SDimitry Andric // Write the set of known module files.
7930b57cec5SDimitry Andric for (ModuleFilesMap::iterator M = ModuleFiles.begin(),
7940b57cec5SDimitry Andric MEnd = ModuleFiles.end();
7950b57cec5SDimitry Andric M != MEnd; ++M) {
7960b57cec5SDimitry Andric Record.clear();
7970b57cec5SDimitry Andric Record.push_back(M->second.ID);
7980b57cec5SDimitry Andric Record.push_back(M->first->getSize());
7990b57cec5SDimitry Andric Record.push_back(M->first->getModificationTime());
8000b57cec5SDimitry Andric
8010b57cec5SDimitry Andric // File name
8020b57cec5SDimitry Andric StringRef Name(M->first->getName());
8030b57cec5SDimitry Andric Record.push_back(Name.size());
8040b57cec5SDimitry Andric Record.append(Name.begin(), Name.end());
8050b57cec5SDimitry Andric
8060b57cec5SDimitry Andric // Dependencies
8070b57cec5SDimitry Andric Record.push_back(M->second.Dependencies.size());
8080b57cec5SDimitry Andric Record.append(M->second.Dependencies.begin(), M->second.Dependencies.end());
8090b57cec5SDimitry Andric Stream.EmitRecord(MODULE, Record);
8100b57cec5SDimitry Andric }
8110b57cec5SDimitry Andric
8120b57cec5SDimitry Andric // Write the identifier -> module file mapping.
8130b57cec5SDimitry Andric {
8140b57cec5SDimitry Andric llvm::OnDiskChainedHashTableGenerator<IdentifierIndexWriterTrait> Generator;
8150b57cec5SDimitry Andric IdentifierIndexWriterTrait Trait;
8160b57cec5SDimitry Andric
8170b57cec5SDimitry Andric // Populate the hash table.
8180b57cec5SDimitry Andric for (InterestingIdentifierMap::iterator I = InterestingIdentifiers.begin(),
8190b57cec5SDimitry Andric IEnd = InterestingIdentifiers.end();
8200b57cec5SDimitry Andric I != IEnd; ++I) {
8210b57cec5SDimitry Andric Generator.insert(I->first(), I->second, Trait);
8220b57cec5SDimitry Andric }
8230b57cec5SDimitry Andric
8240b57cec5SDimitry Andric // Create the on-disk hash table in a buffer.
8250b57cec5SDimitry Andric SmallString<4096> IdentifierTable;
8260b57cec5SDimitry Andric uint32_t BucketOffset;
8270b57cec5SDimitry Andric {
8280b57cec5SDimitry Andric using namespace llvm::support;
8290b57cec5SDimitry Andric llvm::raw_svector_ostream Out(IdentifierTable);
8300b57cec5SDimitry Andric // Make sure that no bucket is at offset 0
8310b57cec5SDimitry Andric endian::write<uint32_t>(Out, 0, little);
8320b57cec5SDimitry Andric BucketOffset = Generator.Emit(Out, Trait);
8330b57cec5SDimitry Andric }
8340b57cec5SDimitry Andric
8350b57cec5SDimitry Andric // Create a blob abbreviation
8360b57cec5SDimitry Andric auto Abbrev = std::make_shared<BitCodeAbbrev>();
8370b57cec5SDimitry Andric Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_INDEX));
8380b57cec5SDimitry Andric Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
8390b57cec5SDimitry Andric Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
8400b57cec5SDimitry Andric unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
8410b57cec5SDimitry Andric
8420b57cec5SDimitry Andric // Write the identifier table
8430b57cec5SDimitry Andric uint64_t Record[] = {IDENTIFIER_INDEX, BucketOffset};
8440b57cec5SDimitry Andric Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
8450b57cec5SDimitry Andric }
8460b57cec5SDimitry Andric
8470b57cec5SDimitry Andric Stream.ExitBlock();
8480b57cec5SDimitry Andric return false;
8490b57cec5SDimitry Andric }
8500b57cec5SDimitry Andric
8510b57cec5SDimitry Andric llvm::Error
writeIndex(FileManager & FileMgr,const PCHContainerReader & PCHContainerRdr,StringRef Path)8520b57cec5SDimitry Andric GlobalModuleIndex::writeIndex(FileManager &FileMgr,
8530b57cec5SDimitry Andric const PCHContainerReader &PCHContainerRdr,
8540b57cec5SDimitry Andric StringRef Path) {
8550b57cec5SDimitry Andric llvm::SmallString<128> IndexPath;
8560b57cec5SDimitry Andric IndexPath += Path;
8570b57cec5SDimitry Andric llvm::sys::path::append(IndexPath, IndexFileName);
8580b57cec5SDimitry Andric
8590b57cec5SDimitry Andric // Coordinate building the global index file with other processes that might
8600b57cec5SDimitry Andric // try to do the same.
8610b57cec5SDimitry Andric llvm::LockFileManager Locked(IndexPath);
8620b57cec5SDimitry Andric switch (Locked) {
8630b57cec5SDimitry Andric case llvm::LockFileManager::LFS_Error:
8640b57cec5SDimitry Andric return llvm::createStringError(std::errc::io_error, "LFS error");
8650b57cec5SDimitry Andric
8660b57cec5SDimitry Andric case llvm::LockFileManager::LFS_Owned:
8670b57cec5SDimitry Andric // We're responsible for building the index ourselves. Do so below.
8680b57cec5SDimitry Andric break;
8690b57cec5SDimitry Andric
8700b57cec5SDimitry Andric case llvm::LockFileManager::LFS_Shared:
8710b57cec5SDimitry Andric // Someone else is responsible for building the index. We don't care
8720b57cec5SDimitry Andric // when they finish, so we're done.
8730b57cec5SDimitry Andric return llvm::createStringError(std::errc::device_or_resource_busy,
8740b57cec5SDimitry Andric "someone else is building the index");
8750b57cec5SDimitry Andric }
8760b57cec5SDimitry Andric
8770b57cec5SDimitry Andric // The module index builder.
8780b57cec5SDimitry Andric GlobalModuleIndexBuilder Builder(FileMgr, PCHContainerRdr);
8790b57cec5SDimitry Andric
8800b57cec5SDimitry Andric // Load each of the module files.
8810b57cec5SDimitry Andric std::error_code EC;
8820b57cec5SDimitry Andric for (llvm::sys::fs::directory_iterator D(Path, EC), DEnd;
8830b57cec5SDimitry Andric D != DEnd && !EC;
8840b57cec5SDimitry Andric D.increment(EC)) {
8850b57cec5SDimitry Andric // If this isn't a module file, we don't care.
8860b57cec5SDimitry Andric if (llvm::sys::path::extension(D->path()) != ".pcm") {
8870b57cec5SDimitry Andric // ... unless it's a .pcm.lock file, which indicates that someone is
8880b57cec5SDimitry Andric // in the process of rebuilding a module. They'll rebuild the index
8890b57cec5SDimitry Andric // at the end of that translation unit, so we don't have to.
8900b57cec5SDimitry Andric if (llvm::sys::path::extension(D->path()) == ".pcm.lock")
8910b57cec5SDimitry Andric return llvm::createStringError(std::errc::device_or_resource_busy,
8920b57cec5SDimitry Andric "someone else is building the index");
8930b57cec5SDimitry Andric
8940b57cec5SDimitry Andric continue;
8950b57cec5SDimitry Andric }
8960b57cec5SDimitry Andric
8970b57cec5SDimitry Andric // If we can't find the module file, skip it.
898a7dea167SDimitry Andric auto ModuleFile = FileMgr.getFile(D->path());
8990b57cec5SDimitry Andric if (!ModuleFile)
9000b57cec5SDimitry Andric continue;
9010b57cec5SDimitry Andric
9020b57cec5SDimitry Andric // Load this module file.
903a7dea167SDimitry Andric if (llvm::Error Err = Builder.loadModuleFile(*ModuleFile))
9040b57cec5SDimitry Andric return Err;
9050b57cec5SDimitry Andric }
9060b57cec5SDimitry Andric
9070b57cec5SDimitry Andric // The output buffer, into which the global index will be written.
908*af732203SDimitry Andric SmallString<16> OutputBuffer;
9090b57cec5SDimitry Andric {
9100b57cec5SDimitry Andric llvm::BitstreamWriter OutputStream(OutputBuffer);
9110b57cec5SDimitry Andric if (Builder.writeIndex(OutputStream))
9120b57cec5SDimitry Andric return llvm::createStringError(std::errc::io_error,
9130b57cec5SDimitry Andric "failed writing index");
9140b57cec5SDimitry Andric }
9150b57cec5SDimitry Andric
916*af732203SDimitry Andric return llvm::writeFileAtomically((IndexPath + "-%%%%%%%%").str(), IndexPath,
917*af732203SDimitry Andric OutputBuffer);
9180b57cec5SDimitry Andric }
9190b57cec5SDimitry Andric
9200b57cec5SDimitry Andric namespace {
9210b57cec5SDimitry Andric class GlobalIndexIdentifierIterator : public IdentifierIterator {
9220b57cec5SDimitry Andric /// The current position within the identifier lookup table.
9230b57cec5SDimitry Andric IdentifierIndexTable::key_iterator Current;
9240b57cec5SDimitry Andric
9250b57cec5SDimitry Andric /// The end position within the identifier lookup table.
9260b57cec5SDimitry Andric IdentifierIndexTable::key_iterator End;
9270b57cec5SDimitry Andric
9280b57cec5SDimitry Andric public:
GlobalIndexIdentifierIterator(IdentifierIndexTable & Idx)9290b57cec5SDimitry Andric explicit GlobalIndexIdentifierIterator(IdentifierIndexTable &Idx) {
9300b57cec5SDimitry Andric Current = Idx.key_begin();
9310b57cec5SDimitry Andric End = Idx.key_end();
9320b57cec5SDimitry Andric }
9330b57cec5SDimitry Andric
Next()9340b57cec5SDimitry Andric StringRef Next() override {
9350b57cec5SDimitry Andric if (Current == End)
9360b57cec5SDimitry Andric return StringRef();
9370b57cec5SDimitry Andric
9380b57cec5SDimitry Andric StringRef Result = *Current;
9390b57cec5SDimitry Andric ++Current;
9400b57cec5SDimitry Andric return Result;
9410b57cec5SDimitry Andric }
9420b57cec5SDimitry Andric };
9430b57cec5SDimitry Andric }
9440b57cec5SDimitry Andric
createIdentifierIterator() const9450b57cec5SDimitry Andric IdentifierIterator *GlobalModuleIndex::createIdentifierIterator() const {
9460b57cec5SDimitry Andric IdentifierIndexTable &Table =
9470b57cec5SDimitry Andric *static_cast<IdentifierIndexTable *>(IdentifierIndex);
9480b57cec5SDimitry Andric return new GlobalIndexIdentifierIterator(Table);
9490b57cec5SDimitry Andric }
950