Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 1 | //===--- GlobalModuleIndex.cpp - Global Module Index ------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the GlobalModuleIndex class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 13 | |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 14 | #include "ASTReaderInternals.h" |
| 15 | #include "clang/Basic/FileManager.h" |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 16 | #include "clang/Lex/HeaderSearch.h" |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 17 | #include "clang/Serialization/ASTBitCodes.h" |
| 18 | #include "clang/Serialization/GlobalModuleIndex.h" |
Douglas Gregor | 603cd86 | 2013-03-22 18:50:14 +0000 | [diff] [blame] | 19 | #include "clang/Serialization/Module.h" |
Richard Trieu | f3b0046 | 2018-12-12 02:53:59 +0000 | [diff] [blame] | 20 | #include "clang/Serialization/PCHContainerOperations.h" |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseMap.h" |
| 22 | #include "llvm/ADT/MapVector.h" |
| 23 | #include "llvm/ADT/SmallString.h" |
Francis Visoiu Mistrih | e030827 | 2019-07-03 22:40:07 +0000 | [diff] [blame] | 24 | #include "llvm/Bitstream/BitstreamReader.h" |
| 25 | #include "llvm/Bitstream/BitstreamWriter.h" |
Jonas Devlieghere | 560ce2c | 2018-02-26 15:16:42 +0000 | [diff] [blame] | 26 | #include "llvm/Support/DJB.h" |
Douglas Gregor | 8ec343c | 2013-01-23 22:45:24 +0000 | [diff] [blame] | 27 | #include "llvm/Support/FileSystem.h" |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 28 | #include "llvm/Support/LockFileManager.h" |
| 29 | #include "llvm/Support/MemoryBuffer.h" |
Justin Bogner | bb094f0 | 2014-04-18 19:57:06 +0000 | [diff] [blame] | 30 | #include "llvm/Support/OnDiskHashTable.h" |
Rafael Espindola | 552c169 | 2013-06-11 22:15:02 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Path.h" |
Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 32 | #include "llvm/Support/TimeProfiler.h" |
NAKAMURA Takumi | f0add23 | 2013-01-25 01:47:07 +0000 | [diff] [blame] | 33 | #include <cstdio> |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | using namespace serialization; |
| 36 | |
| 37 | //----------------------------------------------------------------------------// |
| 38 | // Shared constants |
| 39 | //----------------------------------------------------------------------------// |
| 40 | namespace { |
| 41 | enum { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 42 | /// The block containing the index. |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 43 | GLOBAL_INDEX_BLOCK_ID = llvm::bitc::FIRST_APPLICATION_BLOCKID |
| 44 | }; |
| 45 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 46 | /// Describes the record types in the index. |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 47 | enum IndexRecordTypes { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 48 | /// Contains version information and potentially other metadata, |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 49 | /// used to determine if we can read this global index file. |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 50 | INDEX_METADATA, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 51 | /// Describes a module, including its file name and dependencies. |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 52 | MODULE, |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 53 | /// The index for identifiers. |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 54 | IDENTIFIER_INDEX |
| 55 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 56 | } |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 57 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 58 | /// The name of the global index file. |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 59 | static const char * const IndexFileName = "modules.idx"; |
| 60 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 61 | /// The global index file version. |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 62 | static const unsigned CurrentVersion = 1; |
| 63 | |
| 64 | //----------------------------------------------------------------------------// |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 65 | // Global module index reader. |
| 66 | //----------------------------------------------------------------------------// |
| 67 | |
| 68 | namespace { |
| 69 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 70 | /// Trait used to read the identifier index from the on-disk hash |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 71 | /// table. |
| 72 | class IdentifierIndexReaderTrait { |
| 73 | public: |
| 74 | typedef StringRef external_key_type; |
| 75 | typedef StringRef internal_key_type; |
| 76 | typedef SmallVector<unsigned, 2> data_type; |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 77 | typedef unsigned hash_value_type; |
| 78 | typedef unsigned offset_type; |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 79 | |
| 80 | static bool EqualKey(const internal_key_type& a, const internal_key_type& b) { |
| 81 | return a == b; |
| 82 | } |
| 83 | |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 84 | static hash_value_type ComputeHash(const internal_key_type& a) { |
Jonas Devlieghere | 560ce2c | 2018-02-26 15:16:42 +0000 | [diff] [blame] | 85 | return llvm::djbHash(a); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static std::pair<unsigned, unsigned> |
| 89 | ReadKeyDataLength(const unsigned char*& d) { |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 90 | using namespace llvm::support; |
| 91 | unsigned KeyLen = endian::readNext<uint16_t, little, unaligned>(d); |
| 92 | unsigned DataLen = endian::readNext<uint16_t, little, unaligned>(d); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 93 | return std::make_pair(KeyLen, DataLen); |
| 94 | } |
| 95 | |
| 96 | static const internal_key_type& |
| 97 | GetInternalKey(const external_key_type& x) { return x; } |
| 98 | |
| 99 | static const external_key_type& |
| 100 | GetExternalKey(const internal_key_type& x) { return x; } |
| 101 | |
| 102 | static internal_key_type ReadKey(const unsigned char* d, unsigned n) { |
| 103 | return StringRef((const char *)d, n); |
| 104 | } |
| 105 | |
| 106 | static data_type ReadData(const internal_key_type& k, |
| 107 | const unsigned char* d, |
| 108 | unsigned DataLen) { |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 109 | using namespace llvm::support; |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 110 | |
| 111 | data_type Result; |
| 112 | while (DataLen > 0) { |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 113 | unsigned ID = endian::readNext<uint32_t, little, unaligned>(d); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 114 | Result.push_back(ID); |
| 115 | DataLen -= 4; |
| 116 | } |
| 117 | |
| 118 | return Result; |
| 119 | } |
| 120 | }; |
| 121 | |
Justin Bogner | bb094f0 | 2014-04-18 19:57:06 +0000 | [diff] [blame] | 122 | typedef llvm::OnDiskIterableChainedHashTable<IdentifierIndexReaderTrait> |
Justin Bogner | da4e650 | 2014-04-14 16:34:29 +0000 | [diff] [blame] | 123 | IdentifierIndexTable; |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 124 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 125 | } |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 126 | |
David Blaikie | afa10d3 | 2014-08-11 18:47:26 +0000 | [diff] [blame] | 127 | GlobalModuleIndex::GlobalModuleIndex(std::unique_ptr<llvm::MemoryBuffer> Buffer, |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 128 | llvm::BitstreamCursor Cursor) |
David Blaikie | afa10d3 | 2014-08-11 18:47:26 +0000 | [diff] [blame] | 129 | : Buffer(std::move(Buffer)), IdentifierIndex(), NumIdentifierLookups(), |
| 130 | NumIdentifierLookupHits() { |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 131 | auto Fail = [&Buffer](llvm::Error &&Err) { |
| 132 | report_fatal_error("Module index '" + Buffer->getBufferIdentifier() + |
| 133 | "' failed: " + toString(std::move(Err))); |
| 134 | }; |
| 135 | |
Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 136 | llvm::TimeTraceScope TimeScope("Module LoadIndex", StringRef("")); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 137 | // Read the global index. |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 138 | bool InGlobalIndexBlock = false; |
| 139 | bool Done = false; |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 140 | while (!Done) { |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 141 | llvm::BitstreamEntry Entry; |
| 142 | if (Expected<llvm::BitstreamEntry> Res = Cursor.advance()) |
| 143 | Entry = Res.get(); |
| 144 | else |
| 145 | Fail(Res.takeError()); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 146 | |
| 147 | switch (Entry.Kind) { |
| 148 | case llvm::BitstreamEntry::Error: |
| 149 | return; |
| 150 | |
| 151 | case llvm::BitstreamEntry::EndBlock: |
| 152 | if (InGlobalIndexBlock) { |
| 153 | InGlobalIndexBlock = false; |
| 154 | Done = true; |
| 155 | continue; |
| 156 | } |
| 157 | return; |
| 158 | |
| 159 | |
| 160 | case llvm::BitstreamEntry::Record: |
| 161 | // Entries in the global index block are handled below. |
| 162 | if (InGlobalIndexBlock) |
| 163 | break; |
| 164 | |
| 165 | return; |
| 166 | |
| 167 | case llvm::BitstreamEntry::SubBlock: |
| 168 | if (!InGlobalIndexBlock && Entry.ID == GLOBAL_INDEX_BLOCK_ID) { |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 169 | if (llvm::Error Err = Cursor.EnterSubBlock(GLOBAL_INDEX_BLOCK_ID)) |
| 170 | Fail(std::move(Err)); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 171 | InGlobalIndexBlock = true; |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 172 | } else if (llvm::Error Err = Cursor.SkipBlock()) |
| 173 | Fail(std::move(Err)); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 174 | continue; |
| 175 | } |
| 176 | |
| 177 | SmallVector<uint64_t, 64> Record; |
| 178 | StringRef Blob; |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 179 | Expected<unsigned> MaybeIndexRecord = |
| 180 | Cursor.readRecord(Entry.ID, Record, &Blob); |
| 181 | if (!MaybeIndexRecord) |
| 182 | Fail(MaybeIndexRecord.takeError()); |
| 183 | IndexRecordTypes IndexRecord = |
| 184 | static_cast<IndexRecordTypes>(MaybeIndexRecord.get()); |
| 185 | switch (IndexRecord) { |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 186 | case INDEX_METADATA: |
| 187 | // Make sure that the version matches. |
| 188 | if (Record.size() < 1 || Record[0] != CurrentVersion) |
| 189 | return; |
| 190 | break; |
| 191 | |
| 192 | case MODULE: { |
| 193 | unsigned Idx = 0; |
| 194 | unsigned ID = Record[Idx++]; |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 195 | |
| 196 | // Make room for this module's information. |
| 197 | if (ID == Modules.size()) |
| 198 | Modules.push_back(ModuleInfo()); |
| 199 | else |
| 200 | Modules.resize(ID + 1); |
| 201 | |
| 202 | // Size/modification time for this module file at the time the |
| 203 | // global index was built. |
| 204 | Modules[ID].Size = Record[Idx++]; |
| 205 | Modules[ID].ModTime = Record[Idx++]; |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 206 | |
| 207 | // File name. |
| 208 | unsigned NameLen = Record[Idx++]; |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 209 | Modules[ID].FileName.assign(Record.begin() + Idx, |
| 210 | Record.begin() + Idx + NameLen); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 211 | Idx += NameLen; |
| 212 | |
| 213 | // Dependencies |
| 214 | unsigned NumDeps = Record[Idx++]; |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 215 | Modules[ID].Dependencies.insert(Modules[ID].Dependencies.end(), |
| 216 | Record.begin() + Idx, |
| 217 | Record.begin() + Idx + NumDeps); |
| 218 | Idx += NumDeps; |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 219 | |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 220 | // Make sure we're at the end of the record. |
| 221 | assert(Idx == Record.size() && "More module info?"); |
Douglas Gregor | 603cd86 | 2013-03-22 18:50:14 +0000 | [diff] [blame] | 222 | |
| 223 | // Record this module as an unresolved module. |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 224 | // FIXME: this doesn't work correctly for module names containing path |
| 225 | // separators. |
| 226 | StringRef ModuleName = llvm::sys::path::stem(Modules[ID].FileName); |
| 227 | // Remove the -<hash of ModuleMapPath> |
| 228 | ModuleName = ModuleName.rsplit('-').first; |
| 229 | UnresolvedModules[ModuleName] = ID; |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 230 | break; |
| 231 | } |
| 232 | |
| 233 | case IDENTIFIER_INDEX: |
| 234 | // Wire up the identifier index. |
| 235 | if (Record[0]) { |
| 236 | IdentifierIndex = IdentifierIndexTable::Create( |
Justin Bogner | da4e650 | 2014-04-14 16:34:29 +0000 | [diff] [blame] | 237 | (const unsigned char *)Blob.data() + Record[0], |
| 238 | (const unsigned char *)Blob.data() + sizeof(uint32_t), |
| 239 | (const unsigned char *)Blob.data(), IdentifierIndexReaderTrait()); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 240 | } |
| 241 | break; |
| 242 | } |
| 243 | } |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Nico Weber | e68b847 | 2014-04-25 19:45:23 +0000 | [diff] [blame] | 246 | GlobalModuleIndex::~GlobalModuleIndex() { |
| 247 | delete static_cast<IdentifierIndexTable *>(IdentifierIndex); |
| 248 | } |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 249 | |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 250 | std::pair<GlobalModuleIndex *, llvm::Error> |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 251 | GlobalModuleIndex::readIndex(StringRef Path) { |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 252 | // Load the index file, if it's there. |
| 253 | llvm::SmallString<128> IndexPath; |
| 254 | IndexPath += Path; |
| 255 | llvm::sys::path::append(IndexPath, IndexFileName); |
| 256 | |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 257 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> BufferOrErr = |
| 258 | llvm::MemoryBuffer::getFile(IndexPath.c_str()); |
| 259 | if (!BufferOrErr) |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 260 | return std::make_pair(nullptr, |
| 261 | llvm::errorCodeToError(BufferOrErr.getError())); |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 262 | std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(BufferOrErr.get()); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 263 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 264 | /// The main bitstream cursor for the main block. |
Peter Collingbourne | 77c89b6 | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 265 | llvm::BitstreamCursor Cursor(*Buffer); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 266 | |
| 267 | // Sniff for the signature. |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 268 | for (unsigned char C : {'B', 'C', 'G', 'I'}) { |
| 269 | if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Cursor.Read(8)) { |
| 270 | if (Res.get() != C) |
| 271 | return std::make_pair( |
| 272 | nullptr, llvm::createStringError(std::errc::illegal_byte_sequence, |
| 273 | "expected signature BCGI")); |
| 274 | } else |
| 275 | return std::make_pair(nullptr, Res.takeError()); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 276 | } |
Ahmed Charles | 9a16beb | 2014-03-07 19:33:25 +0000 | [diff] [blame] | 277 | |
David Blaikie | afa10d3 | 2014-08-11 18:47:26 +0000 | [diff] [blame] | 278 | return std::make_pair(new GlobalModuleIndex(std::move(Buffer), Cursor), |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 279 | llvm::Error::success()); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 282 | void |
| 283 | GlobalModuleIndex::getKnownModules(SmallVectorImpl<ModuleFile *> &ModuleFiles) { |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 284 | ModuleFiles.clear(); |
| 285 | for (unsigned I = 0, N = Modules.size(); I != N; ++I) { |
Douglas Gregor | 603cd86 | 2013-03-22 18:50:14 +0000 | [diff] [blame] | 286 | if (ModuleFile *MF = Modules[I].File) |
| 287 | ModuleFiles.push_back(MF); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
| 291 | void GlobalModuleIndex::getModuleDependencies( |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 292 | ModuleFile *File, |
| 293 | SmallVectorImpl<ModuleFile *> &Dependencies) { |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 294 | // Look for information about this module file. |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 295 | llvm::DenseMap<ModuleFile *, unsigned>::iterator Known |
| 296 | = ModulesByFile.find(File); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 297 | if (Known == ModulesByFile.end()) |
| 298 | return; |
| 299 | |
| 300 | // Record dependencies. |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 301 | Dependencies.clear(); |
| 302 | ArrayRef<unsigned> StoredDependencies = Modules[Known->second].Dependencies; |
| 303 | for (unsigned I = 0, N = StoredDependencies.size(); I != N; ++I) { |
Douglas Gregor | 603cd86 | 2013-03-22 18:50:14 +0000 | [diff] [blame] | 304 | if (ModuleFile *MF = Modules[I].File) |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 305 | Dependencies.push_back(MF); |
| 306 | } |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Douglas Gregor | 7211ac1 | 2013-01-25 23:32:03 +0000 | [diff] [blame] | 309 | bool GlobalModuleIndex::lookupIdentifier(StringRef Name, HitSet &Hits) { |
| 310 | Hits.clear(); |
Jonas Devlieghere | 560ce2c | 2018-02-26 15:16:42 +0000 | [diff] [blame] | 311 | |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 312 | // If there's no identifier index, there is nothing we can do. |
| 313 | if (!IdentifierIndex) |
| 314 | return false; |
| 315 | |
| 316 | // Look into the identifier index. |
| 317 | ++NumIdentifierLookups; |
| 318 | IdentifierIndexTable &Table |
| 319 | = *static_cast<IdentifierIndexTable *>(IdentifierIndex); |
| 320 | IdentifierIndexTable::iterator Known = Table.find(Name); |
| 321 | if (Known == Table.end()) { |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | SmallVector<unsigned, 2> ModuleIDs = *Known; |
| 326 | for (unsigned I = 0, N = ModuleIDs.size(); I != N; ++I) { |
Douglas Gregor | 603cd86 | 2013-03-22 18:50:14 +0000 | [diff] [blame] | 327 | if (ModuleFile *MF = Modules[ModuleIDs[I]].File) |
| 328 | Hits.insert(MF); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | ++NumIdentifierLookupHits; |
| 332 | return true; |
| 333 | } |
| 334 | |
Douglas Gregor | 603cd86 | 2013-03-22 18:50:14 +0000 | [diff] [blame] | 335 | bool GlobalModuleIndex::loadedModuleFile(ModuleFile *File) { |
| 336 | // Look for the module in the global module index based on the module name. |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 337 | StringRef Name = File->ModuleName; |
Douglas Gregor | 603cd86 | 2013-03-22 18:50:14 +0000 | [diff] [blame] | 338 | llvm::StringMap<unsigned>::iterator Known = UnresolvedModules.find(Name); |
| 339 | if (Known == UnresolvedModules.end()) { |
| 340 | return true; |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Douglas Gregor | 603cd86 | 2013-03-22 18:50:14 +0000 | [diff] [blame] | 343 | // Rectify this module with the global module index. |
| 344 | ModuleInfo &Info = Modules[Known->second]; |
| 345 | |
| 346 | // If the size and modification time match what we expected, record this |
| 347 | // module file. |
| 348 | bool Failed = true; |
| 349 | if (File->File->getSize() == Info.Size && |
| 350 | File->File->getModificationTime() == Info.ModTime) { |
| 351 | Info.File = File; |
| 352 | ModulesByFile[File] = Known->second; |
| 353 | |
| 354 | Failed = false; |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Douglas Gregor | 603cd86 | 2013-03-22 18:50:14 +0000 | [diff] [blame] | 357 | // One way or another, we have resolved this module file. |
| 358 | UnresolvedModules.erase(Known); |
| 359 | return Failed; |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 362 | void GlobalModuleIndex::printStats() { |
| 363 | std::fprintf(stderr, "*** Global Module Index Statistics:\n"); |
| 364 | if (NumIdentifierLookups) { |
| 365 | fprintf(stderr, " %u / %u identifier lookups succeeded (%f%%)\n", |
| 366 | NumIdentifierLookupHits, NumIdentifierLookups, |
| 367 | (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups); |
| 368 | } |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 369 | std::fprintf(stderr, "\n"); |
| 370 | } |
| 371 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 372 | LLVM_DUMP_METHOD void GlobalModuleIndex::dump() { |
John Thompson | a39baf1 | 2014-04-17 17:06:13 +0000 | [diff] [blame] | 373 | llvm::errs() << "*** Global Module Index Dump:\n"; |
| 374 | llvm::errs() << "Module files:\n"; |
John Thompson | 4f52d44 | 2014-04-17 18:17:36 +0000 | [diff] [blame] | 375 | for (auto &MI : Modules) { |
John Thompson | a39baf1 | 2014-04-17 17:06:13 +0000 | [diff] [blame] | 376 | llvm::errs() << "** " << MI.FileName << "\n"; |
| 377 | if (MI.File) |
| 378 | MI.File->dump(); |
John Thompson | bcdcc92 | 2014-04-16 21:03:41 +0000 | [diff] [blame] | 379 | else |
John Thompson | a39baf1 | 2014-04-17 17:06:13 +0000 | [diff] [blame] | 380 | llvm::errs() << "\n"; |
John Thompson | bcdcc92 | 2014-04-16 21:03:41 +0000 | [diff] [blame] | 381 | } |
John Thompson | a39baf1 | 2014-04-17 17:06:13 +0000 | [diff] [blame] | 382 | llvm::errs() << "\n"; |
John Thompson | bcdcc92 | 2014-04-16 21:03:41 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 385 | //----------------------------------------------------------------------------// |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 386 | // Global module index writer. |
| 387 | //----------------------------------------------------------------------------// |
| 388 | |
| 389 | namespace { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 390 | /// Provides information about a specific module file. |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 391 | struct ModuleFileInfo { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 392 | /// The numberic ID for this module file. |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 393 | unsigned ID; |
| 394 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 395 | /// The set of modules on which this module depends. Each entry is |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 396 | /// a module ID. |
| 397 | SmallVector<unsigned, 4> Dependencies; |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 398 | ASTFileSignature Signature; |
| 399 | }; |
| 400 | |
| 401 | struct ImportedModuleFileInfo { |
| 402 | off_t StoredSize; |
| 403 | time_t StoredModTime; |
| 404 | ASTFileSignature StoredSignature; |
| 405 | ImportedModuleFileInfo(off_t Size, time_t ModTime, ASTFileSignature Sig) |
| 406 | : StoredSize(Size), StoredModTime(ModTime), StoredSignature(Sig) {} |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 407 | }; |
| 408 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 409 | /// Builder that generates the global module index file. |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 410 | class GlobalModuleIndexBuilder { |
| 411 | FileManager &FileMgr; |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 412 | const PCHContainerReader &PCHContainerRdr; |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 413 | |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 414 | /// Mapping from files to module file information. |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 415 | typedef llvm::MapVector<const FileEntry *, ModuleFileInfo> ModuleFilesMap; |
| 416 | |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 417 | /// Information about each of the known module files. |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 418 | ModuleFilesMap ModuleFiles; |
| 419 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 420 | /// Mapping from the imported module file to the imported |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 421 | /// information. |
| 422 | typedef std::multimap<const FileEntry *, ImportedModuleFileInfo> |
| 423 | ImportedModuleFilesMap; |
| 424 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 425 | /// Information about each importing of a module file. |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 426 | ImportedModuleFilesMap ImportedModuleFiles; |
| 427 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 428 | /// Mapping from identifiers to the list of module file IDs that |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 429 | /// consider this identifier to be interesting. |
| 430 | typedef llvm::StringMap<SmallVector<unsigned, 2> > InterestingIdentifierMap; |
| 431 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 432 | /// A mapping from all interesting identifiers to the set of module |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 433 | /// files in which those identifiers are considered interesting. |
| 434 | InterestingIdentifierMap InterestingIdentifiers; |
Jonas Devlieghere | 560ce2c | 2018-02-26 15:16:42 +0000 | [diff] [blame] | 435 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 436 | /// Write the block-info block for the global module index file. |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 437 | void emitBlockInfoBlock(llvm::BitstreamWriter &Stream); |
| 438 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 439 | /// Retrieve the module file information for the given file. |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 440 | ModuleFileInfo &getModuleFileInfo(const FileEntry *File) { |
| 441 | llvm::MapVector<const FileEntry *, ModuleFileInfo>::iterator Known |
| 442 | = ModuleFiles.find(File); |
| 443 | if (Known != ModuleFiles.end()) |
| 444 | return Known->second; |
| 445 | |
| 446 | unsigned NewID = ModuleFiles.size(); |
| 447 | ModuleFileInfo &Info = ModuleFiles[File]; |
| 448 | Info.ID = NewID; |
| 449 | return Info; |
| 450 | } |
| 451 | |
| 452 | public: |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 453 | explicit GlobalModuleIndexBuilder( |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 454 | FileManager &FileMgr, const PCHContainerReader &PCHContainerRdr) |
| 455 | : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr) {} |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 456 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 457 | /// Load the contents of the given module file into the builder. |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 458 | llvm::Error loadModuleFile(const FileEntry *File); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 459 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 460 | /// Write the index to the given bitstream. |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 461 | /// \returns true if an error occurred, false otherwise. |
| 462 | bool writeIndex(llvm::BitstreamWriter &Stream); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 463 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 464 | } |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 465 | |
| 466 | static void emitBlockID(unsigned ID, const char *Name, |
| 467 | llvm::BitstreamWriter &Stream, |
| 468 | SmallVectorImpl<uint64_t> &Record) { |
| 469 | Record.clear(); |
| 470 | Record.push_back(ID); |
| 471 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record); |
| 472 | |
| 473 | // Emit the block name if present. |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 474 | if (!Name || Name[0] == 0) return; |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 475 | Record.clear(); |
| 476 | while (*Name) |
| 477 | Record.push_back(*Name++); |
| 478 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record); |
| 479 | } |
| 480 | |
| 481 | static void emitRecordID(unsigned ID, const char *Name, |
| 482 | llvm::BitstreamWriter &Stream, |
| 483 | SmallVectorImpl<uint64_t> &Record) { |
| 484 | Record.clear(); |
| 485 | Record.push_back(ID); |
| 486 | while (*Name) |
| 487 | Record.push_back(*Name++); |
| 488 | Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record); |
| 489 | } |
| 490 | |
| 491 | void |
| 492 | GlobalModuleIndexBuilder::emitBlockInfoBlock(llvm::BitstreamWriter &Stream) { |
| 493 | SmallVector<uint64_t, 64> Record; |
Peter Collingbourne | d3a6c70 | 2016-11-01 01:18:57 +0000 | [diff] [blame] | 494 | Stream.EnterBlockInfoBlock(); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 495 | |
| 496 | #define BLOCK(X) emitBlockID(X ## _ID, #X, Stream, Record) |
| 497 | #define RECORD(X) emitRecordID(X, #X, Stream, Record) |
| 498 | BLOCK(GLOBAL_INDEX_BLOCK); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 499 | RECORD(INDEX_METADATA); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 500 | RECORD(MODULE); |
| 501 | RECORD(IDENTIFIER_INDEX); |
| 502 | #undef RECORD |
| 503 | #undef BLOCK |
| 504 | |
| 505 | Stream.ExitBlock(); |
| 506 | } |
| 507 | |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 508 | namespace { |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 509 | class InterestingASTIdentifierLookupTrait |
| 510 | : public serialization::reader::ASTIdentifierLookupTraitBase { |
| 511 | |
| 512 | public: |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 513 | /// The identifier and whether it is "interesting". |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 514 | typedef std::pair<StringRef, bool> data_type; |
| 515 | |
| 516 | data_type ReadData(const internal_key_type& k, |
| 517 | const unsigned char* d, |
| 518 | unsigned DataLen) { |
| 519 | // The first bit indicates whether this identifier is interesting. |
| 520 | // That's all we care about. |
Justin Bogner | 57ba0b2 | 2014-03-28 22:03:24 +0000 | [diff] [blame] | 521 | using namespace llvm::support; |
| 522 | unsigned RawID = endian::readNext<uint32_t, little, unaligned>(d); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 523 | bool IsInteresting = RawID & 0x01; |
| 524 | return std::make_pair(k, IsInteresting); |
| 525 | } |
| 526 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 527 | } |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 528 | |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 529 | llvm::Error GlobalModuleIndexBuilder::loadModuleFile(const FileEntry *File) { |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 530 | // Open the module file. |
Rafael Espindola | 6406f7b | 2014-08-26 19:54:40 +0000 | [diff] [blame] | 531 | |
Benjamin Kramer | a885796 | 2014-10-26 22:44:13 +0000 | [diff] [blame] | 532 | auto Buffer = FileMgr.getBufferForFile(File, /*isVolatile=*/true); |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 533 | if (!Buffer) |
| 534 | return llvm::createStringError(Buffer.getError(), |
| 535 | "failed getting buffer for module file"); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 536 | |
| 537 | // Initialize the input stream |
Peter Collingbourne | 77c89b6 | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 538 | llvm::BitstreamCursor InStream(PCHContainerRdr.ExtractPCH(**Buffer)); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 539 | |
| 540 | // Sniff for the signature. |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 541 | for (unsigned char C : {'C', 'P', 'C', 'H'}) |
| 542 | if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = InStream.Read(8)) { |
| 543 | if (Res.get() != C) |
| 544 | return llvm::createStringError(std::errc::illegal_byte_sequence, |
| 545 | "expected signature CPCH"); |
| 546 | } else |
| 547 | return Res.takeError(); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 548 | |
| 549 | // Record this module file and assign it a unique ID (if it doesn't have |
| 550 | // one already). |
| 551 | unsigned ID = getModuleFileInfo(File).ID; |
| 552 | |
| 553 | // Search for the blocks and records we care about. |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 554 | enum { Other, ControlBlock, ASTBlock, DiagnosticOptionsBlock } State = Other; |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 555 | bool Done = false; |
| 556 | while (!Done) { |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 557 | Expected<llvm::BitstreamEntry> MaybeEntry = InStream.advance(); |
| 558 | if (!MaybeEntry) |
| 559 | return MaybeEntry.takeError(); |
| 560 | llvm::BitstreamEntry Entry = MaybeEntry.get(); |
| 561 | |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 562 | switch (Entry.Kind) { |
| 563 | case llvm::BitstreamEntry::Error: |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 564 | Done = true; |
| 565 | continue; |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 566 | |
| 567 | case llvm::BitstreamEntry::Record: |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 568 | // In the 'other' state, just skip the record. We don't care. |
| 569 | if (State == Other) { |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 570 | if (llvm::Expected<unsigned> Skipped = InStream.skipRecord(Entry.ID)) |
| 571 | continue; |
| 572 | else |
| 573 | return Skipped.takeError(); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | // Handle potentially-interesting records below. |
| 577 | break; |
| 578 | |
| 579 | case llvm::BitstreamEntry::SubBlock: |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 580 | if (Entry.ID == CONTROL_BLOCK_ID) { |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 581 | if (llvm::Error Err = InStream.EnterSubBlock(CONTROL_BLOCK_ID)) |
| 582 | return Err; |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 583 | |
| 584 | // Found the control block. |
| 585 | State = ControlBlock; |
| 586 | continue; |
| 587 | } |
| 588 | |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 589 | if (Entry.ID == AST_BLOCK_ID) { |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 590 | if (llvm::Error Err = InStream.EnterSubBlock(AST_BLOCK_ID)) |
| 591 | return Err; |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 592 | |
| 593 | // Found the AST block. |
| 594 | State = ASTBlock; |
| 595 | continue; |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 598 | if (Entry.ID == UNHASHED_CONTROL_BLOCK_ID) { |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 599 | if (llvm::Error Err = InStream.EnterSubBlock(UNHASHED_CONTROL_BLOCK_ID)) |
| 600 | return Err; |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 601 | |
| 602 | // Found the Diagnostic Options block. |
| 603 | State = DiagnosticOptionsBlock; |
| 604 | continue; |
| 605 | } |
| 606 | |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 607 | if (llvm::Error Err = InStream.SkipBlock()) |
| 608 | return Err; |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 609 | |
| 610 | continue; |
| 611 | |
| 612 | case llvm::BitstreamEntry::EndBlock: |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 613 | State = Other; |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 614 | continue; |
| 615 | } |
| 616 | |
| 617 | // Read the given record. |
| 618 | SmallVector<uint64_t, 64> Record; |
| 619 | StringRef Blob; |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 620 | Expected<unsigned> MaybeCode = InStream.readRecord(Entry.ID, Record, &Blob); |
| 621 | if (!MaybeCode) |
| 622 | return MaybeCode.takeError(); |
| 623 | unsigned Code = MaybeCode.get(); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 624 | |
| 625 | // Handle module dependencies. |
| 626 | if (State == ControlBlock && Code == IMPORTS) { |
| 627 | // Load each of the imported PCH files. |
| 628 | unsigned Idx = 0, N = Record.size(); |
| 629 | while (Idx < N) { |
| 630 | // Read information about the AST file. |
| 631 | |
| 632 | // Skip the imported kind |
| 633 | ++Idx; |
| 634 | |
| 635 | // Skip the import location |
| 636 | ++Idx; |
| 637 | |
Jonas Devlieghere | 560ce2c | 2018-02-26 15:16:42 +0000 | [diff] [blame] | 638 | // Load stored size/modification time. |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 639 | off_t StoredSize = (off_t)Record[Idx++]; |
| 640 | time_t StoredModTime = (time_t)Record[Idx++]; |
| 641 | |
Ben Langmuir | 487ea14 | 2014-10-23 18:05:36 +0000 | [diff] [blame] | 642 | // Skip the stored signature. |
| 643 | // FIXME: we could read the signature out of the import and validate it. |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 644 | ASTFileSignature StoredSignature = { |
| 645 | {{(uint32_t)Record[Idx++], (uint32_t)Record[Idx++], |
| 646 | (uint32_t)Record[Idx++], (uint32_t)Record[Idx++], |
| 647 | (uint32_t)Record[Idx++]}}}; |
Ben Langmuir | 487ea14 | 2014-10-23 18:05:36 +0000 | [diff] [blame] | 648 | |
Boris Kolpackov | d30446f | 2017-08-31 06:26:43 +0000 | [diff] [blame] | 649 | // Skip the module name (currently this is only used for prebuilt |
| 650 | // modules while here we are only dealing with cached). |
| 651 | Idx += Record[Idx] + 1; |
| 652 | |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 653 | // Retrieve the imported file name. |
| 654 | unsigned Length = Record[Idx++]; |
| 655 | SmallString<128> ImportedFile(Record.begin() + Idx, |
| 656 | Record.begin() + Idx + Length); |
| 657 | Idx += Length; |
| 658 | |
| 659 | // Find the imported module file. |
Douglas Gregor | dadd85d | 2013-02-08 21:27:45 +0000 | [diff] [blame] | 660 | const FileEntry *DependsOnFile |
| 661 | = FileMgr.getFile(ImportedFile, /*openFile=*/false, |
| 662 | /*cacheFailure=*/false); |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 663 | |
| 664 | if (!DependsOnFile) |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 665 | return llvm::createStringError(std::errc::bad_file_descriptor, |
| 666 | "imported file \"%s\" not found", |
| 667 | ImportedFile.c_str()); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 668 | |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 669 | // Save the information in ImportedModuleFileInfo so we can verify after |
| 670 | // loading all pcms. |
| 671 | ImportedModuleFiles.insert(std::make_pair( |
| 672 | DependsOnFile, ImportedModuleFileInfo(StoredSize, StoredModTime, |
| 673 | StoredSignature))); |
| 674 | |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 675 | // Record the dependency. |
| 676 | unsigned DependsOnID = getModuleFileInfo(DependsOnFile).ID; |
| 677 | getModuleFileInfo(File).Dependencies.push_back(DependsOnID); |
| 678 | } |
| 679 | |
| 680 | continue; |
| 681 | } |
| 682 | |
| 683 | // Handle the identifier table |
| 684 | if (State == ASTBlock && Code == IDENTIFIER_TABLE && Record[0] > 0) { |
Justin Bogner | bb094f0 | 2014-04-18 19:57:06 +0000 | [diff] [blame] | 685 | typedef llvm::OnDiskIterableChainedHashTable< |
| 686 | InterestingASTIdentifierLookupTrait> InterestingIdentifierTable; |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 687 | std::unique_ptr<InterestingIdentifierTable> Table( |
| 688 | InterestingIdentifierTable::Create( |
| 689 | (const unsigned char *)Blob.data() + Record[0], |
Justin Bogner | da4e650 | 2014-04-14 16:34:29 +0000 | [diff] [blame] | 690 | (const unsigned char *)Blob.data() + sizeof(uint32_t), |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 691 | (const unsigned char *)Blob.data())); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 692 | for (InterestingIdentifierTable::data_iterator D = Table->data_begin(), |
| 693 | DEnd = Table->data_end(); |
| 694 | D != DEnd; ++D) { |
| 695 | std::pair<StringRef, bool> Ident = *D; |
| 696 | if (Ident.second) |
| 697 | InterestingIdentifiers[Ident.first].push_back(ID); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 698 | else |
| 699 | (void)InterestingIdentifiers[Ident.first]; |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 703 | // Get Signature. |
| 704 | if (State == DiagnosticOptionsBlock && Code == SIGNATURE) |
| 705 | getModuleFileInfo(File).Signature = { |
| 706 | {{(uint32_t)Record[0], (uint32_t)Record[1], (uint32_t)Record[2], |
| 707 | (uint32_t)Record[3], (uint32_t)Record[4]}}}; |
| 708 | |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 709 | // We don't care about this record. |
| 710 | } |
| 711 | |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 712 | return llvm::Error::success(); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | namespace { |
| 716 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 717 | /// Trait used to generate the identifier index as an on-disk hash |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 718 | /// table. |
| 719 | class IdentifierIndexWriterTrait { |
| 720 | public: |
| 721 | typedef StringRef key_type; |
| 722 | typedef StringRef key_type_ref; |
| 723 | typedef SmallVector<unsigned, 2> data_type; |
| 724 | typedef const SmallVector<unsigned, 2> &data_type_ref; |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 725 | typedef unsigned hash_value_type; |
| 726 | typedef unsigned offset_type; |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 727 | |
Justin Bogner | 25463f1 | 2014-04-18 20:27:24 +0000 | [diff] [blame] | 728 | static hash_value_type ComputeHash(key_type_ref Key) { |
Jonas Devlieghere | 560ce2c | 2018-02-26 15:16:42 +0000 | [diff] [blame] | 729 | return llvm::djbHash(Key); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | std::pair<unsigned,unsigned> |
| 733 | EmitKeyDataLength(raw_ostream& Out, key_type_ref Key, data_type_ref Data) { |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 734 | using namespace llvm::support; |
Peter Collingbourne | e3f6529 | 2018-05-18 19:46:24 +0000 | [diff] [blame] | 735 | endian::Writer LE(Out, little); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 736 | unsigned KeyLen = Key.size(); |
| 737 | unsigned DataLen = Data.size() * 4; |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 738 | LE.write<uint16_t>(KeyLen); |
| 739 | LE.write<uint16_t>(DataLen); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 740 | return std::make_pair(KeyLen, DataLen); |
| 741 | } |
Jonas Devlieghere | 560ce2c | 2018-02-26 15:16:42 +0000 | [diff] [blame] | 742 | |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 743 | void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) { |
| 744 | Out.write(Key.data(), KeyLen); |
| 745 | } |
| 746 | |
| 747 | void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data, |
| 748 | unsigned DataLen) { |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 749 | using namespace llvm::support; |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 750 | for (unsigned I = 0, N = Data.size(); I != N; ++I) |
Peter Collingbourne | e3f6529 | 2018-05-18 19:46:24 +0000 | [diff] [blame] | 751 | endian::write<uint32_t>(Out, Data[I], little); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 752 | } |
| 753 | }; |
| 754 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 755 | } |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 756 | |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 757 | bool GlobalModuleIndexBuilder::writeIndex(llvm::BitstreamWriter &Stream) { |
| 758 | for (auto MapEntry : ImportedModuleFiles) { |
| 759 | auto *File = MapEntry.first; |
| 760 | ImportedModuleFileInfo &Info = MapEntry.second; |
| 761 | if (getModuleFileInfo(File).Signature) { |
| 762 | if (getModuleFileInfo(File).Signature != Info.StoredSignature) |
| 763 | // Verify Signature. |
| 764 | return true; |
| 765 | } else if (Info.StoredSize != File->getSize() || |
| 766 | Info.StoredModTime != File->getModificationTime()) |
| 767 | // Verify Size and ModTime. |
| 768 | return true; |
| 769 | } |
| 770 | |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 771 | using namespace llvm; |
Anton Afanasyev | d880de2 | 2019-03-30 08:42:48 +0000 | [diff] [blame] | 772 | llvm::TimeTraceScope TimeScope("Module WriteIndex", StringRef("")); |
Jonas Devlieghere | 560ce2c | 2018-02-26 15:16:42 +0000 | [diff] [blame] | 773 | |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 774 | // Emit the file header. |
| 775 | Stream.Emit((unsigned)'B', 8); |
| 776 | Stream.Emit((unsigned)'C', 8); |
| 777 | Stream.Emit((unsigned)'G', 8); |
| 778 | Stream.Emit((unsigned)'I', 8); |
| 779 | |
| 780 | // Write the block-info block, which describes the records in this bitcode |
| 781 | // file. |
| 782 | emitBlockInfoBlock(Stream); |
| 783 | |
| 784 | Stream.EnterSubblock(GLOBAL_INDEX_BLOCK_ID, 3); |
| 785 | |
| 786 | // Write the metadata. |
| 787 | SmallVector<uint64_t, 2> Record; |
| 788 | Record.push_back(CurrentVersion); |
Douglas Gregor | e060e57 | 2013-01-25 01:03:03 +0000 | [diff] [blame] | 789 | Stream.EmitRecord(INDEX_METADATA, Record); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 790 | |
| 791 | // Write the set of known module files. |
| 792 | for (ModuleFilesMap::iterator M = ModuleFiles.begin(), |
| 793 | MEnd = ModuleFiles.end(); |
| 794 | M != MEnd; ++M) { |
| 795 | Record.clear(); |
| 796 | Record.push_back(M->second.ID); |
| 797 | Record.push_back(M->first->getSize()); |
| 798 | Record.push_back(M->first->getModificationTime()); |
| 799 | |
| 800 | // File name |
| 801 | StringRef Name(M->first->getName()); |
| 802 | Record.push_back(Name.size()); |
| 803 | Record.append(Name.begin(), Name.end()); |
| 804 | |
| 805 | // Dependencies |
| 806 | Record.push_back(M->second.Dependencies.size()); |
| 807 | Record.append(M->second.Dependencies.begin(), M->second.Dependencies.end()); |
| 808 | Stream.EmitRecord(MODULE, Record); |
| 809 | } |
| 810 | |
| 811 | // Write the identifier -> module file mapping. |
| 812 | { |
Justin Bogner | bb094f0 | 2014-04-18 19:57:06 +0000 | [diff] [blame] | 813 | llvm::OnDiskChainedHashTableGenerator<IdentifierIndexWriterTrait> Generator; |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 814 | IdentifierIndexWriterTrait Trait; |
| 815 | |
| 816 | // Populate the hash table. |
| 817 | for (InterestingIdentifierMap::iterator I = InterestingIdentifiers.begin(), |
| 818 | IEnd = InterestingIdentifiers.end(); |
| 819 | I != IEnd; ++I) { |
| 820 | Generator.insert(I->first(), I->second, Trait); |
| 821 | } |
Jonas Devlieghere | 560ce2c | 2018-02-26 15:16:42 +0000 | [diff] [blame] | 822 | |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 823 | // Create the on-disk hash table in a buffer. |
| 824 | SmallString<4096> IdentifierTable; |
| 825 | uint32_t BucketOffset; |
| 826 | { |
Justin Bogner | e1c147c | 2014-03-28 22:03:19 +0000 | [diff] [blame] | 827 | using namespace llvm::support; |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 828 | llvm::raw_svector_ostream Out(IdentifierTable); |
| 829 | // Make sure that no bucket is at offset 0 |
Peter Collingbourne | e3f6529 | 2018-05-18 19:46:24 +0000 | [diff] [blame] | 830 | endian::write<uint32_t>(Out, 0, little); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 831 | BucketOffset = Generator.Emit(Out, Trait); |
| 832 | } |
| 833 | |
| 834 | // Create a blob abbreviation |
David Blaikie | b44f0bf | 2017-01-04 22:36:43 +0000 | [diff] [blame] | 835 | auto Abbrev = std::make_shared<BitCodeAbbrev>(); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 836 | Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_INDEX)); |
| 837 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); |
| 838 | Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); |
David Blaikie | b44f0bf | 2017-01-04 22:36:43 +0000 | [diff] [blame] | 839 | unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev)); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 840 | |
| 841 | // Write the identifier table |
Mehdi Amini | 57a4191 | 2015-09-10 01:46:39 +0000 | [diff] [blame] | 842 | uint64_t Record[] = {IDENTIFIER_INDEX, BucketOffset}; |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 843 | Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 846 | Stream.ExitBlock(); |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 847 | return false; |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 848 | } |
| 849 | |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 850 | llvm::Error |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 851 | GlobalModuleIndex::writeIndex(FileManager &FileMgr, |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 852 | const PCHContainerReader &PCHContainerRdr, |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 853 | StringRef Path) { |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 854 | llvm::SmallString<128> IndexPath; |
| 855 | IndexPath += Path; |
| 856 | llvm::sys::path::append(IndexPath, IndexFileName); |
| 857 | |
| 858 | // Coordinate building the global index file with other processes that might |
| 859 | // try to do the same. |
| 860 | llvm::LockFileManager Locked(IndexPath); |
| 861 | switch (Locked) { |
| 862 | case llvm::LockFileManager::LFS_Error: |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 863 | return llvm::createStringError(std::errc::io_error, "LFS error"); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 864 | |
| 865 | case llvm::LockFileManager::LFS_Owned: |
| 866 | // We're responsible for building the index ourselves. Do so below. |
| 867 | break; |
| 868 | |
| 869 | case llvm::LockFileManager::LFS_Shared: |
| 870 | // Someone else is responsible for building the index. We don't care |
| 871 | // when they finish, so we're done. |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 872 | return llvm::createStringError(std::errc::device_or_resource_busy, |
| 873 | "someone else is building the index"); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | // The module index builder. |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 877 | GlobalModuleIndexBuilder Builder(FileMgr, PCHContainerRdr); |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 878 | |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 879 | // Load each of the module files. |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 880 | std::error_code EC; |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 881 | for (llvm::sys::fs::directory_iterator D(Path, EC), DEnd; |
| 882 | D != DEnd && !EC; |
| 883 | D.increment(EC)) { |
| 884 | // If this isn't a module file, we don't care. |
| 885 | if (llvm::sys::path::extension(D->path()) != ".pcm") { |
| 886 | // ... unless it's a .pcm.lock file, which indicates that someone is |
| 887 | // in the process of rebuilding a module. They'll rebuild the index |
| 888 | // at the end of that translation unit, so we don't have to. |
| 889 | if (llvm::sys::path::extension(D->path()) == ".pcm.lock") |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 890 | return llvm::createStringError(std::errc::device_or_resource_busy, |
| 891 | "someone else is building the index"); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 892 | |
| 893 | continue; |
| 894 | } |
| 895 | |
| 896 | // If we can't find the module file, skip it. |
| 897 | const FileEntry *ModuleFile = FileMgr.getFile(D->path()); |
| 898 | if (!ModuleFile) |
| 899 | continue; |
| 900 | |
| 901 | // Load this module file. |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 902 | if (llvm::Error Err = Builder.loadModuleFile(ModuleFile)) |
| 903 | return Err; |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | // The output buffer, into which the global index will be written. |
| 907 | SmallVector<char, 16> OutputBuffer; |
| 908 | { |
| 909 | llvm::BitstreamWriter OutputStream(OutputBuffer); |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 910 | if (Builder.writeIndex(OutputStream)) |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 911 | return llvm::createStringError(std::errc::io_error, |
| 912 | "failed writing index"); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | // Write the global index file to a temporary file. |
| 916 | llvm::SmallString<128> IndexTmpPath; |
| 917 | int TmpFD; |
Rafael Espindola | 1862711 | 2013-07-05 21:13:58 +0000 | [diff] [blame] | 918 | if (llvm::sys::fs::createUniqueFile(IndexPath + "-%%%%%%%%", TmpFD, |
| 919 | IndexTmpPath)) |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 920 | return llvm::createStringError(std::errc::io_error, |
| 921 | "failed creating unique file"); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 922 | |
| 923 | // Open the temporary global index file for output. |
NAKAMURA Takumi | e00c986 | 2013-01-24 08:20:11 +0000 | [diff] [blame] | 924 | llvm::raw_fd_ostream Out(TmpFD, true); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 925 | if (Out.has_error()) |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 926 | return llvm::createStringError(Out.error(), "failed outputting to stream"); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 927 | |
| 928 | // Write the index. |
| 929 | Out.write(OutputBuffer.data(), OutputBuffer.size()); |
| 930 | Out.close(); |
| 931 | if (Out.has_error()) |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 932 | return llvm::createStringError(Out.error(), "failed writing to stream"); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 933 | |
| 934 | // Remove the old index file. It isn't relevant any more. |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 935 | llvm::sys::fs::remove(IndexPath); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 936 | |
| 937 | // Rename the newly-written index file to the proper name. |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 938 | if (std::error_code Err = llvm::sys::fs::rename(IndexTmpPath, IndexPath)) { |
| 939 | // Remove the file on failure, don't check whether removal succeeded. |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 940 | llvm::sys::fs::remove(IndexTmpPath); |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 941 | return llvm::createStringError(Err, "failed renaming file \"%s\" to \"%s\"", |
| 942 | IndexTmpPath.c_str(), IndexPath.c_str()); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 943 | } |
| 944 | |
JF Bastien | 0e82895 | 2019-06-26 19:50:12 +0000 | [diff] [blame] | 945 | return llvm::Error::success(); |
Douglas Gregor | 5e306b1 | 2013-01-23 22:38:11 +0000 | [diff] [blame] | 946 | } |
Argyrios Kyrtzidis | 9aca3c6 | 2013-04-17 22:10:55 +0000 | [diff] [blame] | 947 | |
| 948 | namespace { |
| 949 | class GlobalIndexIdentifierIterator : public IdentifierIterator { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 950 | /// The current position within the identifier lookup table. |
Argyrios Kyrtzidis | 9aca3c6 | 2013-04-17 22:10:55 +0000 | [diff] [blame] | 951 | IdentifierIndexTable::key_iterator Current; |
| 952 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 953 | /// The end position within the identifier lookup table. |
Argyrios Kyrtzidis | 9aca3c6 | 2013-04-17 22:10:55 +0000 | [diff] [blame] | 954 | IdentifierIndexTable::key_iterator End; |
| 955 | |
| 956 | public: |
| 957 | explicit GlobalIndexIdentifierIterator(IdentifierIndexTable &Idx) { |
| 958 | Current = Idx.key_begin(); |
| 959 | End = Idx.key_end(); |
| 960 | } |
| 961 | |
Craig Topper | 3e89dfe | 2014-03-13 02:13:41 +0000 | [diff] [blame] | 962 | StringRef Next() override { |
Argyrios Kyrtzidis | 9aca3c6 | 2013-04-17 22:10:55 +0000 | [diff] [blame] | 963 | if (Current == End) |
| 964 | return StringRef(); |
| 965 | |
| 966 | StringRef Result = *Current; |
| 967 | ++Current; |
| 968 | return Result; |
| 969 | } |
| 970 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 971 | } |
Argyrios Kyrtzidis | 9aca3c6 | 2013-04-17 22:10:55 +0000 | [diff] [blame] | 972 | |
| 973 | IdentifierIterator *GlobalModuleIndex::createIdentifierIterator() const { |
| 974 | IdentifierIndexTable &Table = |
| 975 | *static_cast<IdentifierIndexTable *>(IdentifierIndex); |
| 976 | return new GlobalIndexIdentifierIterator(Table); |
| 977 | } |