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