2013-01-24 06:38:11 +08:00
|
|
|
//===--- GlobalModuleIndex.cpp - Global Module Index ------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-01-24 06:38:11 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the GlobalModuleIndex class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-11-22 10:49:05 +08:00
|
|
|
#include "clang/Serialization/GlobalModuleIndex.h"
|
2013-01-24 06:38:11 +08:00
|
|
|
#include "ASTReaderInternals.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
2014-04-15 02:00:01 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2013-01-24 06:38:11 +08:00
|
|
|
#include "clang/Serialization/ASTBitCodes.h"
|
2019-11-22 10:49:05 +08:00
|
|
|
#include "clang/Serialization/ModuleFile.h"
|
2018-12-12 10:53:59 +08:00
|
|
|
#include "clang/Serialization/PCHContainerOperations.h"
|
2013-01-24 06:38:11 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/MapVector.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
2019-09-14 04:08:27 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2019-07-04 06:40:07 +08:00
|
|
|
#include "llvm/Bitstream/BitstreamReader.h"
|
|
|
|
#include "llvm/Bitstream/BitstreamWriter.h"
|
2018-02-26 23:16:42 +08:00
|
|
|
#include "llvm/Support/DJB.h"
|
2013-01-24 06:45:24 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2019-09-14 04:08:27 +08:00
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2013-01-24 06:38:11 +08:00
|
|
|
#include "llvm/Support/LockFileManager.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2014-04-19 03:57:06 +08:00
|
|
|
#include "llvm/Support/OnDiskHashTable.h"
|
2013-06-12 06:15:02 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2019-03-30 16:42:48 +08:00
|
|
|
#include "llvm/Support/TimeProfiler.h"
|
2013-01-25 09:47:07 +08:00
|
|
|
#include <cstdio>
|
2013-01-24 06:38:11 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace serialization;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
// Shared constants
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
namespace {
|
|
|
|
enum {
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The block containing the index.
|
2013-01-24 06:38:11 +08:00
|
|
|
GLOBAL_INDEX_BLOCK_ID = llvm::bitc::FIRST_APPLICATION_BLOCKID
|
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Describes the record types in the index.
|
2013-01-24 06:38:11 +08:00
|
|
|
enum IndexRecordTypes {
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Contains version information and potentially other metadata,
|
2013-01-24 06:38:11 +08:00
|
|
|
/// used to determine if we can read this global index file.
|
2013-01-25 09:03:03 +08:00
|
|
|
INDEX_METADATA,
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Describes a module, including its file name and dependencies.
|
2013-01-24 06:38:11 +08:00
|
|
|
MODULE,
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The index for identifiers.
|
2013-01-24 06:38:11 +08:00
|
|
|
IDENTIFIER_INDEX
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2013-01-24 06:38:11 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The name of the global index file.
|
2013-01-24 06:38:11 +08:00
|
|
|
static const char * const IndexFileName = "modules.idx";
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The global index file version.
|
2013-01-24 06:38:11 +08:00
|
|
|
static const unsigned CurrentVersion = 1;
|
|
|
|
|
2013-01-25 09:03:03 +08:00
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
// Global module index reader.
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Trait used to read the identifier index from the on-disk hash
|
2013-01-25 09:03:03 +08:00
|
|
|
/// table.
|
|
|
|
class IdentifierIndexReaderTrait {
|
|
|
|
public:
|
|
|
|
typedef StringRef external_key_type;
|
|
|
|
typedef StringRef internal_key_type;
|
|
|
|
typedef SmallVector<unsigned, 2> data_type;
|
2014-04-19 04:27:24 +08:00
|
|
|
typedef unsigned hash_value_type;
|
|
|
|
typedef unsigned offset_type;
|
2013-01-25 09:03:03 +08:00
|
|
|
|
|
|
|
static bool EqualKey(const internal_key_type& a, const internal_key_type& b) {
|
|
|
|
return a == b;
|
|
|
|
}
|
|
|
|
|
2014-04-19 04:27:24 +08:00
|
|
|
static hash_value_type ComputeHash(const internal_key_type& a) {
|
2018-02-26 23:16:42 +08:00
|
|
|
return llvm::djbHash(a);
|
2013-01-25 09:03:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::pair<unsigned, unsigned>
|
|
|
|
ReadKeyDataLength(const unsigned char*& d) {
|
2014-03-29 06:03:24 +08:00
|
|
|
using namespace llvm::support;
|
|
|
|
unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d);
|
|
|
|
unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d);
|
2013-01-25 09:03:03 +08:00
|
|
|
return std::make_pair(KeyLen, DataLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const internal_key_type&
|
|
|
|
GetInternalKey(const external_key_type& x) { return x; }
|
|
|
|
|
|
|
|
static const external_key_type&
|
|
|
|
GetExternalKey(const internal_key_type& x) { return x; }
|
|
|
|
|
|
|
|
static internal_key_type ReadKey(const unsigned char* d, unsigned n) {
|
|
|
|
return StringRef((const char *)d, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static data_type ReadData(const internal_key_type& k,
|
|
|
|
const unsigned char* d,
|
|
|
|
unsigned DataLen) {
|
2014-03-29 06:03:24 +08:00
|
|
|
using namespace llvm::support;
|
2013-01-25 09:03:03 +08:00
|
|
|
|
|
|
|
data_type Result;
|
|
|
|
while (DataLen > 0) {
|
2014-03-29 06:03:24 +08:00
|
|
|
unsigned ID = endian::readNext<uint32_t, little, unaligned>(d);
|
2013-01-25 09:03:03 +08:00
|
|
|
Result.push_back(ID);
|
|
|
|
DataLen -= 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-19 03:57:06 +08:00
|
|
|
typedef llvm::OnDiskIterableChainedHashTable<IdentifierIndexReaderTrait>
|
2014-04-15 00:34:29 +08:00
|
|
|
IdentifierIndexTable;
|
2013-01-25 09:03:03 +08:00
|
|
|
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2013-01-25 09:03:03 +08:00
|
|
|
|
2020-01-12 00:42:41 +08:00
|
|
|
GlobalModuleIndex::GlobalModuleIndex(
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> IndexBuffer,
|
|
|
|
llvm::BitstreamCursor Cursor)
|
|
|
|
: Buffer(std::move(IndexBuffer)), IdentifierIndex(), NumIdentifierLookups(),
|
2014-08-12 02:47:26 +08:00
|
|
|
NumIdentifierLookupHits() {
|
2020-01-12 00:42:41 +08:00
|
|
|
auto Fail = [&](llvm::Error &&Err) {
|
2019-06-27 03:50:12 +08:00
|
|
|
report_fatal_error("Module index '" + Buffer->getBufferIdentifier() +
|
|
|
|
"' failed: " + toString(std::move(Err)));
|
|
|
|
};
|
|
|
|
|
2019-12-11 19:49:42 +08:00
|
|
|
llvm::TimeTraceScope TimeScope("Module LoadIndex");
|
2013-01-25 09:03:03 +08:00
|
|
|
// Read the global index.
|
|
|
|
bool InGlobalIndexBlock = false;
|
|
|
|
bool Done = false;
|
|
|
|
while (!Done) {
|
2019-06-27 03:50:12 +08:00
|
|
|
llvm::BitstreamEntry Entry;
|
|
|
|
if (Expected<llvm::BitstreamEntry> Res = Cursor.advance())
|
|
|
|
Entry = Res.get();
|
|
|
|
else
|
|
|
|
Fail(Res.takeError());
|
2013-01-25 09:03:03 +08:00
|
|
|
|
|
|
|
switch (Entry.Kind) {
|
|
|
|
case llvm::BitstreamEntry::Error:
|
|
|
|
return;
|
|
|
|
|
|
|
|
case llvm::BitstreamEntry::EndBlock:
|
|
|
|
if (InGlobalIndexBlock) {
|
|
|
|
InGlobalIndexBlock = false;
|
|
|
|
Done = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
case llvm::BitstreamEntry::Record:
|
|
|
|
// Entries in the global index block are handled below.
|
|
|
|
if (InGlobalIndexBlock)
|
|
|
|
break;
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
case llvm::BitstreamEntry::SubBlock:
|
|
|
|
if (!InGlobalIndexBlock && Entry.ID == GLOBAL_INDEX_BLOCK_ID) {
|
2019-06-27 03:50:12 +08:00
|
|
|
if (llvm::Error Err = Cursor.EnterSubBlock(GLOBAL_INDEX_BLOCK_ID))
|
|
|
|
Fail(std::move(Err));
|
2013-01-25 09:03:03 +08:00
|
|
|
InGlobalIndexBlock = true;
|
2019-06-27 03:50:12 +08:00
|
|
|
} else if (llvm::Error Err = Cursor.SkipBlock())
|
|
|
|
Fail(std::move(Err));
|
2013-01-25 09:03:03 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<uint64_t, 64> Record;
|
|
|
|
StringRef Blob;
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<unsigned> MaybeIndexRecord =
|
|
|
|
Cursor.readRecord(Entry.ID, Record, &Blob);
|
|
|
|
if (!MaybeIndexRecord)
|
|
|
|
Fail(MaybeIndexRecord.takeError());
|
|
|
|
IndexRecordTypes IndexRecord =
|
|
|
|
static_cast<IndexRecordTypes>(MaybeIndexRecord.get());
|
|
|
|
switch (IndexRecord) {
|
2013-01-25 09:03:03 +08:00
|
|
|
case INDEX_METADATA:
|
|
|
|
// Make sure that the version matches.
|
|
|
|
if (Record.size() < 1 || Record[0] != CurrentVersion)
|
|
|
|
return;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MODULE: {
|
|
|
|
unsigned Idx = 0;
|
|
|
|
unsigned ID = Record[Idx++];
|
2013-03-19 08:28:20 +08:00
|
|
|
|
|
|
|
// Make room for this module's information.
|
|
|
|
if (ID == Modules.size())
|
|
|
|
Modules.push_back(ModuleInfo());
|
|
|
|
else
|
|
|
|
Modules.resize(ID + 1);
|
|
|
|
|
|
|
|
// Size/modification time for this module file at the time the
|
|
|
|
// global index was built.
|
|
|
|
Modules[ID].Size = Record[Idx++];
|
|
|
|
Modules[ID].ModTime = Record[Idx++];
|
2013-01-25 09:03:03 +08:00
|
|
|
|
|
|
|
// File name.
|
|
|
|
unsigned NameLen = Record[Idx++];
|
2013-03-19 08:28:20 +08:00
|
|
|
Modules[ID].FileName.assign(Record.begin() + Idx,
|
|
|
|
Record.begin() + Idx + NameLen);
|
2013-01-25 09:03:03 +08:00
|
|
|
Idx += NameLen;
|
|
|
|
|
|
|
|
// Dependencies
|
|
|
|
unsigned NumDeps = Record[Idx++];
|
2013-03-19 08:28:20 +08:00
|
|
|
Modules[ID].Dependencies.insert(Modules[ID].Dependencies.end(),
|
|
|
|
Record.begin() + Idx,
|
|
|
|
Record.begin() + Idx + NumDeps);
|
|
|
|
Idx += NumDeps;
|
2013-01-25 09:03:03 +08:00
|
|
|
|
2013-03-19 08:28:20 +08:00
|
|
|
// Make sure we're at the end of the record.
|
|
|
|
assert(Idx == Record.size() && "More module info?");
|
2013-03-23 02:50:14 +08:00
|
|
|
|
|
|
|
// Record this module as an unresolved module.
|
2014-04-15 02:00:01 +08:00
|
|
|
// FIXME: this doesn't work correctly for module names containing path
|
|
|
|
// separators.
|
|
|
|
StringRef ModuleName = llvm::sys::path::stem(Modules[ID].FileName);
|
|
|
|
// Remove the -<hash of ModuleMapPath>
|
|
|
|
ModuleName = ModuleName.rsplit('-').first;
|
|
|
|
UnresolvedModules[ModuleName] = ID;
|
2013-01-25 09:03:03 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case IDENTIFIER_INDEX:
|
|
|
|
// Wire up the identifier index.
|
|
|
|
if (Record[0]) {
|
|
|
|
IdentifierIndex = IdentifierIndexTable::Create(
|
2014-04-15 00:34:29 +08:00
|
|
|
(const unsigned char *)Blob.data() + Record[0],
|
|
|
|
(const unsigned char *)Blob.data() + sizeof(uint32_t),
|
|
|
|
(const unsigned char *)Blob.data(), IdentifierIndexReaderTrait());
|
2013-01-25 09:03:03 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-26 03:45:23 +08:00
|
|
|
GlobalModuleIndex::~GlobalModuleIndex() {
|
|
|
|
delete static_cast<IdentifierIndexTable *>(IdentifierIndex);
|
|
|
|
}
|
2013-01-25 09:03:03 +08:00
|
|
|
|
2019-06-27 03:50:12 +08:00
|
|
|
std::pair<GlobalModuleIndex *, llvm::Error>
|
2013-03-19 08:28:20 +08:00
|
|
|
GlobalModuleIndex::readIndex(StringRef Path) {
|
2013-01-25 09:03:03 +08:00
|
|
|
// Load the index file, if it's there.
|
|
|
|
llvm::SmallString<128> IndexPath;
|
|
|
|
IndexPath += Path;
|
|
|
|
llvm::sys::path::append(IndexPath, IndexFileName);
|
|
|
|
|
2014-07-07 01:43:24 +08:00
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> BufferOrErr =
|
|
|
|
llvm::MemoryBuffer::getFile(IndexPath.c_str());
|
|
|
|
if (!BufferOrErr)
|
2019-06-27 03:50:12 +08:00
|
|
|
return std::make_pair(nullptr,
|
|
|
|
llvm::errorCodeToError(BufferOrErr.getError()));
|
2014-07-07 01:43:24 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(BufferOrErr.get());
|
2013-01-25 09:03:03 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The main bitstream cursor for the main block.
|
2016-11-08 12:17:11 +08:00
|
|
|
llvm::BitstreamCursor Cursor(*Buffer);
|
2013-01-25 09:03:03 +08:00
|
|
|
|
|
|
|
// Sniff for the signature.
|
2019-06-27 03:50:12 +08:00
|
|
|
for (unsigned char C : {'B', 'C', 'G', 'I'}) {
|
|
|
|
if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Cursor.Read(8)) {
|
|
|
|
if (Res.get() != C)
|
|
|
|
return std::make_pair(
|
|
|
|
nullptr, llvm::createStringError(std::errc::illegal_byte_sequence,
|
|
|
|
"expected signature BCGI"));
|
|
|
|
} else
|
|
|
|
return std::make_pair(nullptr, Res.takeError());
|
2013-01-25 09:03:03 +08:00
|
|
|
}
|
2014-03-08 03:33:25 +08:00
|
|
|
|
2014-08-12 02:47:26 +08:00
|
|
|
return std::make_pair(new GlobalModuleIndex(std::move(Buffer), Cursor),
|
2019-06-27 03:50:12 +08:00
|
|
|
llvm::Error::success());
|
2013-01-25 09:03:03 +08:00
|
|
|
}
|
|
|
|
|
2013-03-19 08:28:20 +08:00
|
|
|
void
|
|
|
|
GlobalModuleIndex::getKnownModules(SmallVectorImpl<ModuleFile *> &ModuleFiles) {
|
2013-01-25 09:03:03 +08:00
|
|
|
ModuleFiles.clear();
|
|
|
|
for (unsigned I = 0, N = Modules.size(); I != N; ++I) {
|
2013-03-23 02:50:14 +08:00
|
|
|
if (ModuleFile *MF = Modules[I].File)
|
|
|
|
ModuleFiles.push_back(MF);
|
2013-01-25 09:03:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalModuleIndex::getModuleDependencies(
|
2013-03-19 08:28:20 +08:00
|
|
|
ModuleFile *File,
|
|
|
|
SmallVectorImpl<ModuleFile *> &Dependencies) {
|
2013-01-25 09:03:03 +08:00
|
|
|
// Look for information about this module file.
|
2013-03-19 08:28:20 +08:00
|
|
|
llvm::DenseMap<ModuleFile *, unsigned>::iterator Known
|
|
|
|
= ModulesByFile.find(File);
|
2013-01-25 09:03:03 +08:00
|
|
|
if (Known == ModulesByFile.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Record dependencies.
|
2013-03-19 08:28:20 +08:00
|
|
|
Dependencies.clear();
|
|
|
|
ArrayRef<unsigned> StoredDependencies = Modules[Known->second].Dependencies;
|
|
|
|
for (unsigned I = 0, N = StoredDependencies.size(); I != N; ++I) {
|
2013-03-23 02:50:14 +08:00
|
|
|
if (ModuleFile *MF = Modules[I].File)
|
2013-03-19 08:28:20 +08:00
|
|
|
Dependencies.push_back(MF);
|
|
|
|
}
|
2013-01-25 09:03:03 +08:00
|
|
|
}
|
|
|
|
|
2013-01-26 07:32:03 +08:00
|
|
|
bool GlobalModuleIndex::lookupIdentifier(StringRef Name, HitSet &Hits) {
|
|
|
|
Hits.clear();
|
2018-02-26 23:16:42 +08:00
|
|
|
|
2013-01-25 09:03:03 +08:00
|
|
|
// If there's no identifier index, there is nothing we can do.
|
|
|
|
if (!IdentifierIndex)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Look into the identifier index.
|
|
|
|
++NumIdentifierLookups;
|
|
|
|
IdentifierIndexTable &Table
|
|
|
|
= *static_cast<IdentifierIndexTable *>(IdentifierIndex);
|
|
|
|
IdentifierIndexTable::iterator Known = Table.find(Name);
|
|
|
|
if (Known == Table.end()) {
|
2020-06-19 15:01:02 +08:00
|
|
|
return false;
|
2013-01-25 09:03:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<unsigned, 2> ModuleIDs = *Known;
|
|
|
|
for (unsigned I = 0, N = ModuleIDs.size(); I != N; ++I) {
|
2013-03-23 02:50:14 +08:00
|
|
|
if (ModuleFile *MF = Modules[ModuleIDs[I]].File)
|
|
|
|
Hits.insert(MF);
|
2013-01-25 09:03:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
++NumIdentifierLookupHits;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-23 02:50:14 +08:00
|
|
|
bool GlobalModuleIndex::loadedModuleFile(ModuleFile *File) {
|
|
|
|
// Look for the module in the global module index based on the module name.
|
2014-04-15 02:00:01 +08:00
|
|
|
StringRef Name = File->ModuleName;
|
2013-03-23 02:50:14 +08:00
|
|
|
llvm::StringMap<unsigned>::iterator Known = UnresolvedModules.find(Name);
|
|
|
|
if (Known == UnresolvedModules.end()) {
|
|
|
|
return true;
|
2013-03-19 08:28:20 +08:00
|
|
|
}
|
|
|
|
|
2013-03-23 02:50:14 +08:00
|
|
|
// Rectify this module with the global module index.
|
|
|
|
ModuleInfo &Info = Modules[Known->second];
|
|
|
|
|
|
|
|
// If the size and modification time match what we expected, record this
|
|
|
|
// module file.
|
|
|
|
bool Failed = true;
|
|
|
|
if (File->File->getSize() == Info.Size &&
|
|
|
|
File->File->getModificationTime() == Info.ModTime) {
|
|
|
|
Info.File = File;
|
|
|
|
ModulesByFile[File] = Known->second;
|
|
|
|
|
|
|
|
Failed = false;
|
2013-03-19 08:28:20 +08:00
|
|
|
}
|
|
|
|
|
2013-03-23 02:50:14 +08:00
|
|
|
// One way or another, we have resolved this module file.
|
|
|
|
UnresolvedModules.erase(Known);
|
|
|
|
return Failed;
|
2013-03-19 08:28:20 +08:00
|
|
|
}
|
|
|
|
|
2013-01-25 09:03:03 +08:00
|
|
|
void GlobalModuleIndex::printStats() {
|
|
|
|
std::fprintf(stderr, "*** Global Module Index Statistics:\n");
|
|
|
|
if (NumIdentifierLookups) {
|
|
|
|
fprintf(stderr, " %u / %u identifier lookups succeeded (%f%%)\n",
|
|
|
|
NumIdentifierLookupHits, NumIdentifierLookups,
|
|
|
|
(double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
|
|
|
|
}
|
|
|
|
std::fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
2016-01-30 03:38:18 +08:00
|
|
|
LLVM_DUMP_METHOD void GlobalModuleIndex::dump() {
|
2014-04-18 01:06:13 +08:00
|
|
|
llvm::errs() << "*** Global Module Index Dump:\n";
|
|
|
|
llvm::errs() << "Module files:\n";
|
2014-04-18 02:17:36 +08:00
|
|
|
for (auto &MI : Modules) {
|
2014-04-18 01:06:13 +08:00
|
|
|
llvm::errs() << "** " << MI.FileName << "\n";
|
|
|
|
if (MI.File)
|
|
|
|
MI.File->dump();
|
2014-04-17 05:03:41 +08:00
|
|
|
else
|
2014-04-18 01:06:13 +08:00
|
|
|
llvm::errs() << "\n";
|
2014-04-17 05:03:41 +08:00
|
|
|
}
|
2014-04-18 01:06:13 +08:00
|
|
|
llvm::errs() << "\n";
|
2014-04-17 05:03:41 +08:00
|
|
|
}
|
|
|
|
|
2013-01-24 06:38:11 +08:00
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
// Global module index writer.
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
|
|
|
|
namespace {
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Provides information about a specific module file.
|
2013-01-24 06:38:11 +08:00
|
|
|
struct ModuleFileInfo {
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The numberic ID for this module file.
|
2013-01-24 06:38:11 +08:00
|
|
|
unsigned ID;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The set of modules on which this module depends. Each entry is
|
2013-01-24 06:38:11 +08:00
|
|
|
/// a module ID.
|
|
|
|
SmallVector<unsigned, 4> Dependencies;
|
2017-03-14 02:45:08 +08:00
|
|
|
ASTFileSignature Signature;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ImportedModuleFileInfo {
|
|
|
|
off_t StoredSize;
|
|
|
|
time_t StoredModTime;
|
|
|
|
ASTFileSignature StoredSignature;
|
|
|
|
ImportedModuleFileInfo(off_t Size, time_t ModTime, ASTFileSignature Sig)
|
|
|
|
: StoredSize(Size), StoredModTime(ModTime), StoredSignature(Sig) {}
|
2013-01-24 06:38:11 +08:00
|
|
|
};
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Builder that generates the global module index file.
|
2013-01-24 06:38:11 +08:00
|
|
|
class GlobalModuleIndexBuilder {
|
|
|
|
FileManager &FileMgr;
|
2015-07-17 09:19:54 +08:00
|
|
|
const PCHContainerReader &PCHContainerRdr;
|
2013-01-24 06:38:11 +08:00
|
|
|
|
2017-03-14 02:45:08 +08:00
|
|
|
/// Mapping from files to module file information.
|
2013-01-24 06:38:11 +08:00
|
|
|
typedef llvm::MapVector<const FileEntry *, ModuleFileInfo> ModuleFilesMap;
|
|
|
|
|
2017-03-14 02:45:08 +08:00
|
|
|
/// Information about each of the known module files.
|
2013-01-24 06:38:11 +08:00
|
|
|
ModuleFilesMap ModuleFiles;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Mapping from the imported module file to the imported
|
2017-03-14 02:45:08 +08:00
|
|
|
/// information.
|
|
|
|
typedef std::multimap<const FileEntry *, ImportedModuleFileInfo>
|
|
|
|
ImportedModuleFilesMap;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Information about each importing of a module file.
|
2017-03-14 02:45:08 +08:00
|
|
|
ImportedModuleFilesMap ImportedModuleFiles;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Mapping from identifiers to the list of module file IDs that
|
2013-01-24 06:38:11 +08:00
|
|
|
/// consider this identifier to be interesting.
|
|
|
|
typedef llvm::StringMap<SmallVector<unsigned, 2> > InterestingIdentifierMap;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// A mapping from all interesting identifiers to the set of module
|
2013-01-24 06:38:11 +08:00
|
|
|
/// files in which those identifiers are considered interesting.
|
|
|
|
InterestingIdentifierMap InterestingIdentifiers;
|
2018-02-26 23:16:42 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Write the block-info block for the global module index file.
|
2013-01-24 06:38:11 +08:00
|
|
|
void emitBlockInfoBlock(llvm::BitstreamWriter &Stream);
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Retrieve the module file information for the given file.
|
2013-01-24 06:38:11 +08:00
|
|
|
ModuleFileInfo &getModuleFileInfo(const FileEntry *File) {
|
|
|
|
llvm::MapVector<const FileEntry *, ModuleFileInfo>::iterator Known
|
|
|
|
= ModuleFiles.find(File);
|
|
|
|
if (Known != ModuleFiles.end())
|
|
|
|
return Known->second;
|
|
|
|
|
|
|
|
unsigned NewID = ModuleFiles.size();
|
|
|
|
ModuleFileInfo &Info = ModuleFiles[File];
|
|
|
|
Info.ID = NewID;
|
|
|
|
return Info;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2015-06-21 02:53:08 +08:00
|
|
|
explicit GlobalModuleIndexBuilder(
|
2015-07-17 09:19:54 +08:00
|
|
|
FileManager &FileMgr, const PCHContainerReader &PCHContainerRdr)
|
|
|
|
: FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr) {}
|
2013-01-24 06:38:11 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Load the contents of the given module file into the builder.
|
2019-06-27 03:50:12 +08:00
|
|
|
llvm::Error loadModuleFile(const FileEntry *File);
|
2013-01-24 06:38:11 +08:00
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Write the index to the given bitstream.
|
2017-03-14 02:45:08 +08:00
|
|
|
/// \returns true if an error occurred, false otherwise.
|
|
|
|
bool writeIndex(llvm::BitstreamWriter &Stream);
|
2013-01-24 06:38:11 +08:00
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2013-01-24 06:38:11 +08:00
|
|
|
|
|
|
|
static void emitBlockID(unsigned ID, const char *Name,
|
|
|
|
llvm::BitstreamWriter &Stream,
|
|
|
|
SmallVectorImpl<uint64_t> &Record) {
|
|
|
|
Record.clear();
|
|
|
|
Record.push_back(ID);
|
|
|
|
Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
|
|
|
|
|
|
|
|
// Emit the block name if present.
|
2014-05-22 13:54:18 +08:00
|
|
|
if (!Name || Name[0] == 0) return;
|
2013-01-24 06:38:11 +08:00
|
|
|
Record.clear();
|
|
|
|
while (*Name)
|
|
|
|
Record.push_back(*Name++);
|
|
|
|
Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void emitRecordID(unsigned ID, const char *Name,
|
|
|
|
llvm::BitstreamWriter &Stream,
|
|
|
|
SmallVectorImpl<uint64_t> &Record) {
|
|
|
|
Record.clear();
|
|
|
|
Record.push_back(ID);
|
|
|
|
while (*Name)
|
|
|
|
Record.push_back(*Name++);
|
|
|
|
Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GlobalModuleIndexBuilder::emitBlockInfoBlock(llvm::BitstreamWriter &Stream) {
|
|
|
|
SmallVector<uint64_t, 64> Record;
|
2016-11-01 09:18:57 +08:00
|
|
|
Stream.EnterBlockInfoBlock();
|
2013-01-24 06:38:11 +08:00
|
|
|
|
|
|
|
#define BLOCK(X) emitBlockID(X ## _ID, #X, Stream, Record)
|
|
|
|
#define RECORD(X) emitRecordID(X, #X, Stream, Record)
|
|
|
|
BLOCK(GLOBAL_INDEX_BLOCK);
|
2013-01-25 09:03:03 +08:00
|
|
|
RECORD(INDEX_METADATA);
|
2013-01-24 06:38:11 +08:00
|
|
|
RECORD(MODULE);
|
|
|
|
RECORD(IDENTIFIER_INDEX);
|
|
|
|
#undef RECORD
|
|
|
|
#undef BLOCK
|
|
|
|
|
|
|
|
Stream.ExitBlock();
|
|
|
|
}
|
|
|
|
|
2013-01-25 09:03:03 +08:00
|
|
|
namespace {
|
2013-01-24 06:38:11 +08:00
|
|
|
class InterestingASTIdentifierLookupTrait
|
|
|
|
: public serialization::reader::ASTIdentifierLookupTraitBase {
|
|
|
|
|
|
|
|
public:
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The identifier and whether it is "interesting".
|
2013-01-24 06:38:11 +08:00
|
|
|
typedef std::pair<StringRef, bool> data_type;
|
|
|
|
|
|
|
|
data_type ReadData(const internal_key_type& k,
|
|
|
|
const unsigned char* d,
|
|
|
|
unsigned DataLen) {
|
|
|
|
// The first bit indicates whether this identifier is interesting.
|
|
|
|
// That's all we care about.
|
2014-03-29 06:03:24 +08:00
|
|
|
using namespace llvm::support;
|
|
|
|
unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d);
|
2013-01-24 06:38:11 +08:00
|
|
|
bool IsInteresting = RawID & 0x01;
|
|
|
|
return std::make_pair(k, IsInteresting);
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2013-01-24 06:38:11 +08:00
|
|
|
|
2019-06-27 03:50:12 +08:00
|
|
|
llvm::Error GlobalModuleIndexBuilder::loadModuleFile(const FileEntry *File) {
|
2013-01-24 06:38:11 +08:00
|
|
|
// Open the module file.
|
2014-08-27 03:54:40 +08:00
|
|
|
|
2014-10-27 06:44:13 +08:00
|
|
|
auto Buffer = FileMgr.getBufferForFile(File, /*isVolatile=*/true);
|
2019-06-27 03:50:12 +08:00
|
|
|
if (!Buffer)
|
|
|
|
return llvm::createStringError(Buffer.getError(),
|
|
|
|
"failed getting buffer for module file");
|
2013-01-24 06:38:11 +08:00
|
|
|
|
|
|
|
// Initialize the input stream
|
2016-11-08 12:17:11 +08:00
|
|
|
llvm::BitstreamCursor InStream(PCHContainerRdr.ExtractPCH(**Buffer));
|
2013-01-24 06:38:11 +08:00
|
|
|
|
|
|
|
// Sniff for the signature.
|
2019-06-27 03:50:12 +08:00
|
|
|
for (unsigned char C : {'C', 'P', 'C', 'H'})
|
|
|
|
if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = InStream.Read(8)) {
|
|
|
|
if (Res.get() != C)
|
|
|
|
return llvm::createStringError(std::errc::illegal_byte_sequence,
|
|
|
|
"expected signature CPCH");
|
|
|
|
} else
|
|
|
|
return Res.takeError();
|
2013-01-24 06:38:11 +08:00
|
|
|
|
|
|
|
// Record this module file and assign it a unique ID (if it doesn't have
|
|
|
|
// one already).
|
|
|
|
unsigned ID = getModuleFileInfo(File).ID;
|
|
|
|
|
|
|
|
// Search for the blocks and records we care about.
|
2017-03-14 02:45:08 +08:00
|
|
|
enum { Other, ControlBlock, ASTBlock, DiagnosticOptionsBlock } State = Other;
|
2013-01-24 06:38:11 +08:00
|
|
|
bool Done = false;
|
|
|
|
while (!Done) {
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<llvm::BitstreamEntry> MaybeEntry = InStream.advance();
|
|
|
|
if (!MaybeEntry)
|
|
|
|
return MaybeEntry.takeError();
|
|
|
|
llvm::BitstreamEntry Entry = MaybeEntry.get();
|
|
|
|
|
2013-01-24 06:38:11 +08:00
|
|
|
switch (Entry.Kind) {
|
|
|
|
case llvm::BitstreamEntry::Error:
|
2013-01-25 09:03:03 +08:00
|
|
|
Done = true;
|
|
|
|
continue;
|
2013-01-24 06:38:11 +08:00
|
|
|
|
|
|
|
case llvm::BitstreamEntry::Record:
|
2013-01-25 09:03:03 +08:00
|
|
|
// In the 'other' state, just skip the record. We don't care.
|
|
|
|
if (State == Other) {
|
2019-06-27 03:50:12 +08:00
|
|
|
if (llvm::Expected<unsigned> Skipped = InStream.skipRecord(Entry.ID))
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
return Skipped.takeError();
|
2013-01-24 06:38:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle potentially-interesting records below.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case llvm::BitstreamEntry::SubBlock:
|
2013-01-25 09:03:03 +08:00
|
|
|
if (Entry.ID == CONTROL_BLOCK_ID) {
|
2019-06-27 03:50:12 +08:00
|
|
|
if (llvm::Error Err = InStream.EnterSubBlock(CONTROL_BLOCK_ID))
|
|
|
|
return Err;
|
2013-01-24 06:38:11 +08:00
|
|
|
|
|
|
|
// Found the control block.
|
|
|
|
State = ControlBlock;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-01-25 09:03:03 +08:00
|
|
|
if (Entry.ID == AST_BLOCK_ID) {
|
2019-06-27 03:50:12 +08:00
|
|
|
if (llvm::Error Err = InStream.EnterSubBlock(AST_BLOCK_ID))
|
|
|
|
return Err;
|
2013-01-24 06:38:11 +08:00
|
|
|
|
|
|
|
// Found the AST block.
|
|
|
|
State = ASTBlock;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-14 02:45:08 +08:00
|
|
|
if (Entry.ID == UNHASHED_CONTROL_BLOCK_ID) {
|
2019-06-27 03:50:12 +08:00
|
|
|
if (llvm::Error Err = InStream.EnterSubBlock(UNHASHED_CONTROL_BLOCK_ID))
|
|
|
|
return Err;
|
2017-03-14 02:45:08 +08:00
|
|
|
|
|
|
|
// Found the Diagnostic Options block.
|
|
|
|
State = DiagnosticOptionsBlock;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-06-27 03:50:12 +08:00
|
|
|
if (llvm::Error Err = InStream.SkipBlock())
|
|
|
|
return Err;
|
2013-01-24 06:38:11 +08:00
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case llvm::BitstreamEntry::EndBlock:
|
2013-01-25 09:03:03 +08:00
|
|
|
State = Other;
|
2013-01-24 06:38:11 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the given record.
|
|
|
|
SmallVector<uint64_t, 64> Record;
|
|
|
|
StringRef Blob;
|
2019-06-27 03:50:12 +08:00
|
|
|
Expected<unsigned> MaybeCode = InStream.readRecord(Entry.ID, Record, &Blob);
|
|
|
|
if (!MaybeCode)
|
|
|
|
return MaybeCode.takeError();
|
|
|
|
unsigned Code = MaybeCode.get();
|
2013-01-24 06:38:11 +08:00
|
|
|
|
|
|
|
// Handle module dependencies.
|
|
|
|
if (State == ControlBlock && Code == IMPORTS) {
|
|
|
|
// Load each of the imported PCH files.
|
|
|
|
unsigned Idx = 0, N = Record.size();
|
|
|
|
while (Idx < N) {
|
|
|
|
// Read information about the AST file.
|
|
|
|
|
|
|
|
// Skip the imported kind
|
|
|
|
++Idx;
|
|
|
|
|
|
|
|
// Skip the import location
|
|
|
|
++Idx;
|
|
|
|
|
2018-02-26 23:16:42 +08:00
|
|
|
// Load stored size/modification time.
|
2013-03-19 08:28:20 +08:00
|
|
|
off_t StoredSize = (off_t)Record[Idx++];
|
|
|
|
time_t StoredModTime = (time_t)Record[Idx++];
|
|
|
|
|
2014-10-24 02:05:36 +08:00
|
|
|
// Skip the stored signature.
|
|
|
|
// FIXME: we could read the signature out of the import and validate it.
|
2020-06-08 03:05:25 +08:00
|
|
|
auto FirstSignatureByte = Record.begin() + Idx;
|
|
|
|
ASTFileSignature StoredSignature = ASTFileSignature::create(
|
|
|
|
FirstSignatureByte, FirstSignatureByte + ASTFileSignature::size);
|
|
|
|
Idx += ASTFileSignature::size;
|
2014-10-24 02:05:36 +08:00
|
|
|
|
2017-08-31 14:26:43 +08:00
|
|
|
// Skip the module name (currently this is only used for prebuilt
|
|
|
|
// modules while here we are only dealing with cached).
|
|
|
|
Idx += Record[Idx] + 1;
|
|
|
|
|
2013-01-24 06:38:11 +08:00
|
|
|
// Retrieve the imported file name.
|
|
|
|
unsigned Length = Record[Idx++];
|
|
|
|
SmallString<128> ImportedFile(Record.begin() + Idx,
|
|
|
|
Record.begin() + Idx + Length);
|
|
|
|
Idx += Length;
|
|
|
|
|
|
|
|
// Find the imported module file.
|
2019-08-02 05:31:56 +08:00
|
|
|
auto DependsOnFile
|
2019-07-16 12:46:31 +08:00
|
|
|
= FileMgr.getFile(ImportedFile, /*OpenFile=*/false,
|
|
|
|
/*CacheFailure=*/false);
|
2017-03-14 02:45:08 +08:00
|
|
|
|
|
|
|
if (!DependsOnFile)
|
2019-06-27 03:50:12 +08:00
|
|
|
return llvm::createStringError(std::errc::bad_file_descriptor,
|
|
|
|
"imported file \"%s\" not found",
|
|
|
|
ImportedFile.c_str());
|
2013-01-24 06:38:11 +08:00
|
|
|
|
2017-03-14 02:45:08 +08:00
|
|
|
// Save the information in ImportedModuleFileInfo so we can verify after
|
|
|
|
// loading all pcms.
|
|
|
|
ImportedModuleFiles.insert(std::make_pair(
|
2019-08-02 05:31:56 +08:00
|
|
|
*DependsOnFile, ImportedModuleFileInfo(StoredSize, StoredModTime,
|
|
|
|
StoredSignature)));
|
2017-03-14 02:45:08 +08:00
|
|
|
|
2013-01-24 06:38:11 +08:00
|
|
|
// Record the dependency.
|
2019-08-02 05:31:56 +08:00
|
|
|
unsigned DependsOnID = getModuleFileInfo(*DependsOnFile).ID;
|
2013-01-24 06:38:11 +08:00
|
|
|
getModuleFileInfo(File).Dependencies.push_back(DependsOnID);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the identifier table
|
|
|
|
if (State == ASTBlock && Code == IDENTIFIER_TABLE && Record[0] > 0) {
|
2014-04-19 03:57:06 +08:00
|
|
|
typedef llvm::OnDiskIterableChainedHashTable<
|
|
|
|
InterestingASTIdentifierLookupTrait> InterestingIdentifierTable;
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<InterestingIdentifierTable> Table(
|
|
|
|
InterestingIdentifierTable::Create(
|
|
|
|
(const unsigned char *)Blob.data() + Record[0],
|
2014-04-15 00:34:29 +08:00
|
|
|
(const unsigned char *)Blob.data() + sizeof(uint32_t),
|
2014-03-08 04:03:18 +08:00
|
|
|
(const unsigned char *)Blob.data()));
|
2013-01-24 06:38:11 +08:00
|
|
|
for (InterestingIdentifierTable::data_iterator D = Table->data_begin(),
|
|
|
|
DEnd = Table->data_end();
|
|
|
|
D != DEnd; ++D) {
|
|
|
|
std::pair<StringRef, bool> Ident = *D;
|
|
|
|
if (Ident.second)
|
|
|
|
InterestingIdentifiers[Ident.first].push_back(ID);
|
2013-01-25 09:03:03 +08:00
|
|
|
else
|
|
|
|
(void)InterestingIdentifiers[Ident.first];
|
2013-01-24 06:38:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 02:45:08 +08:00
|
|
|
// Get Signature.
|
|
|
|
if (State == DiagnosticOptionsBlock && Code == SIGNATURE)
|
2020-06-08 03:05:25 +08:00
|
|
|
getModuleFileInfo(File).Signature = ASTFileSignature::create(
|
|
|
|
Record.begin(), Record.begin() + ASTFileSignature::size);
|
2017-03-14 02:45:08 +08:00
|
|
|
|
2013-01-24 06:38:11 +08:00
|
|
|
// We don't care about this record.
|
|
|
|
}
|
|
|
|
|
2019-06-27 03:50:12 +08:00
|
|
|
return llvm::Error::success();
|
2013-01-24 06:38:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Trait used to generate the identifier index as an on-disk hash
|
2013-01-24 06:38:11 +08:00
|
|
|
/// table.
|
|
|
|
class IdentifierIndexWriterTrait {
|
|
|
|
public:
|
|
|
|
typedef StringRef key_type;
|
|
|
|
typedef StringRef key_type_ref;
|
|
|
|
typedef SmallVector<unsigned, 2> data_type;
|
|
|
|
typedef const SmallVector<unsigned, 2> &data_type_ref;
|
2014-04-19 04:27:24 +08:00
|
|
|
typedef unsigned hash_value_type;
|
|
|
|
typedef unsigned offset_type;
|
2013-01-24 06:38:11 +08:00
|
|
|
|
2014-04-19 04:27:24 +08:00
|
|
|
static hash_value_type ComputeHash(key_type_ref Key) {
|
2018-02-26 23:16:42 +08:00
|
|
|
return llvm::djbHash(Key);
|
2013-01-24 06:38:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<unsigned,unsigned>
|
|
|
|
EmitKeyDataLength(raw_ostream& Out, key_type_ref Key, data_type_ref Data) {
|
2014-03-29 06:03:19 +08:00
|
|
|
using namespace llvm::support;
|
2018-05-19 03:46:24 +08:00
|
|
|
endian::Writer LE(Out, little);
|
2013-01-24 06:38:11 +08:00
|
|
|
unsigned KeyLen = Key.size();
|
|
|
|
unsigned DataLen = Data.size() * 4;
|
2014-03-29 06:03:19 +08:00
|
|
|
LE.write<uint16_t>(KeyLen);
|
|
|
|
LE.write<uint16_t>(DataLen);
|
2013-01-24 06:38:11 +08:00
|
|
|
return std::make_pair(KeyLen, DataLen);
|
|
|
|
}
|
2018-02-26 23:16:42 +08:00
|
|
|
|
2013-01-24 06:38:11 +08:00
|
|
|
void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
|
|
|
|
Out.write(Key.data(), KeyLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
|
|
|
|
unsigned DataLen) {
|
2014-03-29 06:03:19 +08:00
|
|
|
using namespace llvm::support;
|
2013-01-24 06:38:11 +08:00
|
|
|
for (unsigned I = 0, N = Data.size(); I != N; ++I)
|
2018-05-19 03:46:24 +08:00
|
|
|
endian::write<uint32_t>(Out, Data[I], little);
|
2013-01-24 06:38:11 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2013-01-24 06:38:11 +08:00
|
|
|
|
2017-03-14 02:45:08 +08:00
|
|
|
bool GlobalModuleIndexBuilder::writeIndex(llvm::BitstreamWriter &Stream) {
|
|
|
|
for (auto MapEntry : ImportedModuleFiles) {
|
|
|
|
auto *File = MapEntry.first;
|
|
|
|
ImportedModuleFileInfo &Info = MapEntry.second;
|
|
|
|
if (getModuleFileInfo(File).Signature) {
|
|
|
|
if (getModuleFileInfo(File).Signature != Info.StoredSignature)
|
|
|
|
// Verify Signature.
|
|
|
|
return true;
|
|
|
|
} else if (Info.StoredSize != File->getSize() ||
|
|
|
|
Info.StoredModTime != File->getModificationTime())
|
|
|
|
// Verify Size and ModTime.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-01-24 06:38:11 +08:00
|
|
|
using namespace llvm;
|
2019-12-11 19:49:42 +08:00
|
|
|
llvm::TimeTraceScope TimeScope("Module WriteIndex");
|
2018-02-26 23:16:42 +08:00
|
|
|
|
2013-01-24 06:38:11 +08:00
|
|
|
// Emit the file header.
|
|
|
|
Stream.Emit((unsigned)'B', 8);
|
|
|
|
Stream.Emit((unsigned)'C', 8);
|
|
|
|
Stream.Emit((unsigned)'G', 8);
|
|
|
|
Stream.Emit((unsigned)'I', 8);
|
|
|
|
|
|
|
|
// Write the block-info block, which describes the records in this bitcode
|
|
|
|
// file.
|
|
|
|
emitBlockInfoBlock(Stream);
|
|
|
|
|
|
|
|
Stream.EnterSubblock(GLOBAL_INDEX_BLOCK_ID, 3);
|
|
|
|
|
|
|
|
// Write the metadata.
|
|
|
|
SmallVector<uint64_t, 2> Record;
|
|
|
|
Record.push_back(CurrentVersion);
|
2013-01-25 09:03:03 +08:00
|
|
|
Stream.EmitRecord(INDEX_METADATA, Record);
|
2013-01-24 06:38:11 +08:00
|
|
|
|
|
|
|
// Write the set of known module files.
|
|
|
|
for (ModuleFilesMap::iterator M = ModuleFiles.begin(),
|
|
|
|
MEnd = ModuleFiles.end();
|
|
|
|
M != MEnd; ++M) {
|
|
|
|
Record.clear();
|
|
|
|
Record.push_back(M->second.ID);
|
|
|
|
Record.push_back(M->first->getSize());
|
|
|
|
Record.push_back(M->first->getModificationTime());
|
|
|
|
|
|
|
|
// File name
|
|
|
|
StringRef Name(M->first->getName());
|
|
|
|
Record.push_back(Name.size());
|
|
|
|
Record.append(Name.begin(), Name.end());
|
|
|
|
|
|
|
|
// Dependencies
|
|
|
|
Record.push_back(M->second.Dependencies.size());
|
|
|
|
Record.append(M->second.Dependencies.begin(), M->second.Dependencies.end());
|
|
|
|
Stream.EmitRecord(MODULE, Record);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the identifier -> module file mapping.
|
|
|
|
{
|
2014-04-19 03:57:06 +08:00
|
|
|
llvm::OnDiskChainedHashTableGenerator<IdentifierIndexWriterTrait> Generator;
|
2013-01-24 06:38:11 +08:00
|
|
|
IdentifierIndexWriterTrait Trait;
|
|
|
|
|
|
|
|
// Populate the hash table.
|
|
|
|
for (InterestingIdentifierMap::iterator I = InterestingIdentifiers.begin(),
|
|
|
|
IEnd = InterestingIdentifiers.end();
|
|
|
|
I != IEnd; ++I) {
|
|
|
|
Generator.insert(I->first(), I->second, Trait);
|
|
|
|
}
|
2018-02-26 23:16:42 +08:00
|
|
|
|
2013-01-24 06:38:11 +08:00
|
|
|
// Create the on-disk hash table in a buffer.
|
|
|
|
SmallString<4096> IdentifierTable;
|
|
|
|
uint32_t BucketOffset;
|
|
|
|
{
|
2014-03-29 06:03:19 +08:00
|
|
|
using namespace llvm::support;
|
2013-01-24 06:38:11 +08:00
|
|
|
llvm::raw_svector_ostream Out(IdentifierTable);
|
|
|
|
// Make sure that no bucket is at offset 0
|
2018-05-19 03:46:24 +08:00
|
|
|
endian::write<uint32_t>(Out, 0, little);
|
2013-01-24 06:38:11 +08:00
|
|
|
BucketOffset = Generator.Emit(Out, Trait);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a blob abbreviation
|
2017-01-05 06:36:43 +08:00
|
|
|
auto Abbrev = std::make_shared<BitCodeAbbrev>();
|
2013-01-24 06:38:11 +08:00
|
|
|
Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_INDEX));
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
|
|
|
|
Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
|
2017-01-05 06:36:43 +08:00
|
|
|
unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
|
2013-01-24 06:38:11 +08:00
|
|
|
|
|
|
|
// Write the identifier table
|
2015-09-10 09:46:39 +08:00
|
|
|
uint64_t Record[] = {IDENTIFIER_INDEX, BucketOffset};
|
2015-03-18 18:17:07 +08:00
|
|
|
Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
|
2013-01-24 06:38:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Stream.ExitBlock();
|
2017-03-14 02:45:08 +08:00
|
|
|
return false;
|
2013-01-24 06:38:11 +08:00
|
|
|
}
|
|
|
|
|
2019-06-27 03:50:12 +08:00
|
|
|
llvm::Error
|
2015-06-21 02:53:08 +08:00
|
|
|
GlobalModuleIndex::writeIndex(FileManager &FileMgr,
|
2015-07-17 09:19:54 +08:00
|
|
|
const PCHContainerReader &PCHContainerRdr,
|
2015-06-21 02:53:08 +08:00
|
|
|
StringRef Path) {
|
2013-01-24 06:38:11 +08:00
|
|
|
llvm::SmallString<128> IndexPath;
|
|
|
|
IndexPath += Path;
|
|
|
|
llvm::sys::path::append(IndexPath, IndexFileName);
|
|
|
|
|
|
|
|
// Coordinate building the global index file with other processes that might
|
|
|
|
// try to do the same.
|
|
|
|
llvm::LockFileManager Locked(IndexPath);
|
|
|
|
switch (Locked) {
|
|
|
|
case llvm::LockFileManager::LFS_Error:
|
2019-06-27 03:50:12 +08:00
|
|
|
return llvm::createStringError(std::errc::io_error, "LFS error");
|
2013-01-24 06:38:11 +08:00
|
|
|
|
|
|
|
case llvm::LockFileManager::LFS_Owned:
|
|
|
|
// We're responsible for building the index ourselves. Do so below.
|
|
|
|
break;
|
|
|
|
|
|
|
|
case llvm::LockFileManager::LFS_Shared:
|
|
|
|
// Someone else is responsible for building the index. We don't care
|
|
|
|
// when they finish, so we're done.
|
2019-06-27 03:50:12 +08:00
|
|
|
return llvm::createStringError(std::errc::device_or_resource_busy,
|
|
|
|
"someone else is building the index");
|
2013-01-24 06:38:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// The module index builder.
|
2015-07-17 09:19:54 +08:00
|
|
|
GlobalModuleIndexBuilder Builder(FileMgr, PCHContainerRdr);
|
2015-06-21 02:53:08 +08:00
|
|
|
|
2013-01-24 06:38:11 +08:00
|
|
|
// Load each of the module files.
|
2014-06-12 22:02:15 +08:00
|
|
|
std::error_code EC;
|
2013-01-24 06:38:11 +08:00
|
|
|
for (llvm::sys::fs::directory_iterator D(Path, EC), DEnd;
|
|
|
|
D != DEnd && !EC;
|
|
|
|
D.increment(EC)) {
|
|
|
|
// If this isn't a module file, we don't care.
|
|
|
|
if (llvm::sys::path::extension(D->path()) != ".pcm") {
|
|
|
|
// ... unless it's a .pcm.lock file, which indicates that someone is
|
|
|
|
// in the process of rebuilding a module. They'll rebuild the index
|
|
|
|
// at the end of that translation unit, so we don't have to.
|
|
|
|
if (llvm::sys::path::extension(D->path()) == ".pcm.lock")
|
2019-06-27 03:50:12 +08:00
|
|
|
return llvm::createStringError(std::errc::device_or_resource_busy,
|
|
|
|
"someone else is building the index");
|
2013-01-24 06:38:11 +08:00
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we can't find the module file, skip it.
|
2019-08-02 05:31:56 +08:00
|
|
|
auto ModuleFile = FileMgr.getFile(D->path());
|
2013-01-24 06:38:11 +08:00
|
|
|
if (!ModuleFile)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Load this module file.
|
2019-08-02 05:31:56 +08:00
|
|
|
if (llvm::Error Err = Builder.loadModuleFile(*ModuleFile))
|
2019-06-27 03:50:12 +08:00
|
|
|
return Err;
|
2013-01-24 06:38:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// The output buffer, into which the global index will be written.
|
2020-11-17 21:02:58 +08:00
|
|
|
SmallString<16> OutputBuffer;
|
2013-01-24 06:38:11 +08:00
|
|
|
{
|
|
|
|
llvm::BitstreamWriter OutputStream(OutputBuffer);
|
2017-03-14 02:45:08 +08:00
|
|
|
if (Builder.writeIndex(OutputStream))
|
2019-06-27 03:50:12 +08:00
|
|
|
return llvm::createStringError(std::errc::io_error,
|
|
|
|
"failed writing index");
|
2013-01-24 06:38:11 +08:00
|
|
|
}
|
|
|
|
|
2020-11-17 21:02:58 +08:00
|
|
|
return llvm::writeFileAtomically((IndexPath + "-%%%%%%%%").str(), IndexPath,
|
|
|
|
OutputBuffer);
|
2013-01-24 06:38:11 +08:00
|
|
|
}
|
2013-04-18 06:10:55 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class GlobalIndexIdentifierIterator : public IdentifierIterator {
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The current position within the identifier lookup table.
|
2013-04-18 06:10:55 +08:00
|
|
|
IdentifierIndexTable::key_iterator Current;
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// The end position within the identifier lookup table.
|
2013-04-18 06:10:55 +08:00
|
|
|
IdentifierIndexTable::key_iterator End;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GlobalIndexIdentifierIterator(IdentifierIndexTable &Idx) {
|
|
|
|
Current = Idx.key_begin();
|
|
|
|
End = Idx.key_end();
|
|
|
|
}
|
|
|
|
|
2014-03-13 10:13:41 +08:00
|
|
|
StringRef Next() override {
|
2013-04-18 06:10:55 +08:00
|
|
|
if (Current == End)
|
|
|
|
return StringRef();
|
|
|
|
|
|
|
|
StringRef Result = *Current;
|
|
|
|
++Current;
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2013-04-18 06:10:55 +08:00
|
|
|
|
|
|
|
IdentifierIterator *GlobalModuleIndex::createIdentifierIterator() const {
|
|
|
|
IdentifierIndexTable &Table =
|
|
|
|
*static_cast<IdentifierIndexTable *>(IdentifierIndex);
|
|
|
|
return new GlobalIndexIdentifierIterator(Table);
|
|
|
|
}
|