clang 24.0.0git
GlobalModuleIndex.cpp
Go to the documentation of this file.
1//===--- GlobalModuleIndex.cpp - Global Module Index ------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the GlobalModuleIndex class.
10//
11//===----------------------------------------------------------------------===//
12
14#include "ASTReaderInternals.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/MapVector.h"
21#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/Bitstream/BitstreamReader.h"
24#include "llvm/Bitstream/BitstreamWriter.h"
25#include "llvm/Support/DJB.h"
26#include "llvm/Support/FileSystem.h"
27#include "llvm/Support/IOSandbox.h"
28#include "llvm/Support/LockFileManager.h"
29#include "llvm/Support/MemoryBuffer.h"
30#include "llvm/Support/OnDiskHashTable.h"
31#include "llvm/Support/Path.h"
32#include "llvm/Support/TimeProfiler.h"
33#include "llvm/Support/raw_ostream.h"
34#include <cstdio>
35using namespace clang;
36using namespace serialization;
37
38//----------------------------------------------------------------------------//
39// Shared constants
40//----------------------------------------------------------------------------//
41namespace {
42 enum {
43 /// The block containing the index.
44 GLOBAL_INDEX_BLOCK_ID = llvm::bitc::FIRST_APPLICATION_BLOCKID
45 };
46
47 /// Describes the record types in the index.
48 enum IndexRecordTypes {
49 /// Contains version information and potentially other metadata,
50 /// used to determine if we can read this global index file.
51 INDEX_METADATA,
52 /// Describes a module, including its file name and dependencies.
53 MODULE,
54 /// The index for identifiers.
55 IDENTIFIER_INDEX
56 };
57}
58
59/// The name of the global index file.
60static const char * const IndexFileName = "modules.idx";
61
62/// The global index file version.
63static const unsigned CurrentVersion = 1;
64
65//----------------------------------------------------------------------------//
66// Global module index reader.
67//----------------------------------------------------------------------------//
68
69namespace {
70
71/// Trait used to read the identifier index from the on-disk hash
72/// table.
73class IdentifierIndexReaderTrait {
74public:
75 typedef StringRef external_key_type;
76 typedef StringRef internal_key_type;
77 typedef SmallVector<unsigned, 2> data_type;
78 typedef unsigned hash_value_type;
79 typedef unsigned offset_type;
80
81 static bool EqualKey(const internal_key_type& a, const internal_key_type& b) {
82 return a == b;
83 }
84
85 static hash_value_type ComputeHash(const internal_key_type& a) {
86 return llvm::djbHash(a);
87 }
88
89 static std::pair<unsigned, unsigned>
90 ReadKeyDataLength(const unsigned char*& d) {
91 using namespace llvm::support;
92 unsigned KeyLen = endian::readNext<uint16_t, llvm::endianness::little>(d);
93 unsigned DataLen = endian::readNext<uint16_t, llvm::endianness::little>(d);
94 return std::make_pair(KeyLen, DataLen);
95 }
96
97 static const internal_key_type&
98 GetInternalKey(const external_key_type& x) { return x; }
99
100 static const external_key_type&
101 GetExternalKey(const internal_key_type& x) { return x; }
102
103 static internal_key_type ReadKey(const unsigned char* d, unsigned n) {
104 return StringRef((const char *)d, n);
105 }
106
107 static data_type ReadData(const internal_key_type& k,
108 const unsigned char* d,
109 unsigned DataLen) {
110 using namespace llvm::support;
111
112 data_type Result;
113 while (DataLen > 0) {
114 unsigned ID = endian::readNext<uint32_t, llvm::endianness::little>(d);
115 Result.push_back(ID);
116 DataLen -= 4;
117 }
118
119 return Result;
120 }
121};
122
123typedef llvm::OnDiskIterableChainedHashTable<IdentifierIndexReaderTrait>
124 IdentifierIndexTable;
125
126}
127
128GlobalModuleIndex::GlobalModuleIndex(
129 std::unique_ptr<llvm::MemoryBuffer> IndexBuffer,
130 llvm::BitstreamCursor Cursor)
131 : Buffer(std::move(IndexBuffer)), IdentifierIndex(), NumIdentifierLookups(),
132 NumIdentifierLookupHits() {
133 auto Fail = [&](llvm::Error &&Err) {
134 report_fatal_error("Module index '" + Buffer->getBufferIdentifier() +
135 "' failed: " + toString(std::move(Err)));
136 };
137
138 llvm::TimeTraceScope TimeScope("Module LoadIndex");
139 // Read the global index.
140 bool InGlobalIndexBlock = false;
141 bool Done = false;
142 while (!Done) {
143 llvm::BitstreamEntry Entry;
144 if (Expected<llvm::BitstreamEntry> Res = Cursor.advance())
145 Entry = Res.get();
146 else
147 Fail(Res.takeError());
148
149 switch (Entry.Kind) {
150 case llvm::BitstreamEntry::Error:
151 return;
152
153 case llvm::BitstreamEntry::EndBlock:
154 if (InGlobalIndexBlock) {
155 InGlobalIndexBlock = false;
156 Done = true;
157 continue;
158 }
159 return;
160
161
162 case llvm::BitstreamEntry::Record:
163 // Entries in the global index block are handled below.
164 if (InGlobalIndexBlock)
165 break;
166
167 return;
168
169 case llvm::BitstreamEntry::SubBlock:
170 if (!InGlobalIndexBlock && Entry.ID == GLOBAL_INDEX_BLOCK_ID) {
171 if (llvm::Error Err = Cursor.EnterSubBlock(GLOBAL_INDEX_BLOCK_ID))
172 Fail(std::move(Err));
173 InGlobalIndexBlock = true;
174 } else if (llvm::Error Err = Cursor.SkipBlock())
175 Fail(std::move(Err));
176 continue;
177 }
178
180 StringRef Blob;
181 Expected<unsigned> MaybeIndexRecord =
182 Cursor.readRecord(Entry.ID, Record, &Blob);
183 if (!MaybeIndexRecord)
184 Fail(MaybeIndexRecord.takeError());
185 IndexRecordTypes IndexRecord =
186 static_cast<IndexRecordTypes>(MaybeIndexRecord.get());
187 switch (IndexRecord) {
188 case INDEX_METADATA:
189 // Make sure that the version matches.
190 if (Record.size() < 1 || Record[0] != CurrentVersion)
191 return;
192 break;
193
194 case MODULE: {
195 unsigned Idx = 0;
196 unsigned ID = Record[Idx++];
197
198 // Make room for this module's information.
199 if (ID == Modules.size())
200 Modules.push_back(ModuleInfo());
201 else
202 Modules.resize(ID + 1);
203
204 // Size/modification time for this module file at the time the
205 // global index was built.
206 Modules[ID].Size = Record[Idx++];
207 Modules[ID].ModTime = Record[Idx++];
208
209 // File name.
210 unsigned NameLen = Record[Idx++];
211 Modules[ID].FileName.assign(Record.begin() + Idx,
212 Record.begin() + Idx + NameLen);
213 Idx += NameLen;
214
215 // Dependencies
216 unsigned NumDeps = Record[Idx++];
217 Modules[ID].Dependencies.insert(Modules[ID].Dependencies.end(),
218 Record.begin() + Idx,
219 Record.begin() + Idx + NumDeps);
220 Idx += NumDeps;
221
222 // Make sure we're at the end of the record.
223 assert(Idx == Record.size() && "More module info?");
224
225 // Record this module as an unresolved module.
226 // FIXME: this doesn't work correctly for module names containing path
227 // separators.
228 StringRef ModuleName = llvm::sys::path::stem(Modules[ID].FileName);
229 // Remove the -<hash of ModuleMapPath>
230 ModuleName = ModuleName.rsplit('-').first;
231 UnresolvedModules[ModuleName] = ID;
232 break;
233 }
234
235 case IDENTIFIER_INDEX:
236 // Wire up the identifier index.
237 if (Record[0]) {
238 IdentifierIndex = IdentifierIndexTable::Create(
239 (const unsigned char *)Blob.data() + Record[0],
240 (const unsigned char *)Blob.data() + sizeof(uint32_t),
241 (const unsigned char *)Blob.data(), IdentifierIndexReaderTrait());
242 }
243 break;
244 }
245 }
246}
247
249 delete static_cast<IdentifierIndexTable *>(IdentifierIndex);
250}
251
252std::pair<GlobalModuleIndex *, llvm::Error>
254 // This is a compiler-internal input/output, let's bypass the sandbox.
255 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
256
257 // Load the index file, if it's there.
258 llvm::SmallString<128> IndexPath;
259 IndexPath += Path;
260 llvm::sys::path::append(IndexPath, IndexFileName);
261
262 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> BufferOrErr =
263 llvm::MemoryBuffer::getFile(IndexPath.c_str());
264 if (!BufferOrErr)
265 return std::make_pair(nullptr,
266 llvm::errorCodeToError(BufferOrErr.getError()));
267 std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(BufferOrErr.get());
268
269 /// The main bitstream cursor for the main block.
270 llvm::BitstreamCursor Cursor(*Buffer);
271
272 // Sniff for the signature.
273 for (unsigned char C : {'B', 'C', 'G', 'I'}) {
274 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = Cursor.Read(8)) {
275 if (Res.get() != C)
276 return std::make_pair(
277 nullptr, llvm::createStringError(std::errc::illegal_byte_sequence,
278 "expected signature BCGI"));
279 } else
280 return std::make_pair(nullptr, Res.takeError());
281 }
282
283 return std::make_pair(new GlobalModuleIndex(std::move(Buffer), std::move(Cursor)),
284 llvm::Error::success());
285}
286
288 ModuleFile *File,
289 SmallVectorImpl<ModuleFile *> &Dependencies) {
290 // Look for information about this module file.
291 llvm::DenseMap<ModuleFile *, unsigned>::iterator Known
292 = ModulesByFile.find(File);
293 if (Known == ModulesByFile.end())
294 return;
295
296 // Record dependencies.
297 Dependencies.clear();
298 ArrayRef<unsigned> StoredDependencies = Modules[Known->second].Dependencies;
299 for (unsigned I = 0, N = StoredDependencies.size(); I != N; ++I) {
300 if (ModuleFile *MF = Modules[I].File)
301 Dependencies.push_back(MF);
302 }
303}
304
305bool GlobalModuleIndex::lookupIdentifier(StringRef Name, HitSet &Hits) {
306 Hits.clear();
307
308 // If there's no identifier index, there is nothing we can do.
309 if (!IdentifierIndex)
310 return false;
311
312 // Look into the identifier index.
313 ++NumIdentifierLookups;
314 IdentifierIndexTable &Table
315 = *static_cast<IdentifierIndexTable *>(IdentifierIndex);
316 IdentifierIndexTable::iterator Known = Table.find(Name);
317 if (Known == Table.end()) {
318 return false;
319 }
320
321 for (unsigned ModuleID : *Known) {
322 if (ModuleFile *MF = Modules[ModuleID].File)
323 Hits.insert(MF);
324 }
325
326 ++NumIdentifierLookupHits;
327 return true;
328}
329
331 // Look for the module in the global module index based on the module name.
332 StringRef Name = File->ModuleName;
333 llvm::StringMap<unsigned>::iterator Known = UnresolvedModules.find(Name);
334 if (Known == UnresolvedModules.end()) {
335 return true;
336 }
337
338 // Rectify this module with the global module index.
339 ModuleInfo &Info = Modules[Known->second];
340
341 // If the size and modification time match what we expected, record this
342 // module file.
343 bool Failed = true;
344 if (File->Size == Info.Size && File->ModTime == Info.ModTime) {
345 Info.File = File;
346 ModulesByFile[File] = Known->second;
347
348 Failed = false;
349 }
350
351 // One way or another, we have resolved this module file.
352 UnresolvedModules.erase(Known);
353 return Failed;
354}
355
357 std::fprintf(stderr, "*** Global Module Index Statistics:\n");
358 if (NumIdentifierLookups) {
359 fprintf(stderr, " %u / %u identifier lookups succeeded (%f%%)\n",
360 NumIdentifierLookupHits, NumIdentifierLookups,
361 (double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
362 }
363 std::fprintf(stderr, "\n");
364}
365
366LLVM_DUMP_METHOD void GlobalModuleIndex::dump() {
367 llvm::errs() << "*** Global Module Index Dump:\n";
368 llvm::errs() << "Module files:\n";
369 for (auto &MI : Modules) {
370 llvm::errs() << "** " << MI.FileName << "\n";
371 if (MI.File)
372 MI.File->dump();
373 else
374 llvm::errs() << "\n";
375 }
376 llvm::errs() << "\n";
377}
378
379//----------------------------------------------------------------------------//
380// Global module index writer.
381//----------------------------------------------------------------------------//
382
383namespace {
384 /// Provides information about a specific module file.
385 struct ModuleFileInfo {
386 /// The numberic ID for this module file.
387 unsigned ID;
388
389 /// The set of modules on which this module depends. Each entry is
390 /// a module ID.
391 SmallVector<unsigned, 4> Dependencies;
392 ASTFileSignature Signature;
393 };
394
395 struct ImportedModuleFileInfo {
396 off_t StoredSize;
397 time_t StoredModTime;
398 ASTFileSignature StoredSignature;
399 ImportedModuleFileInfo(off_t Size, time_t ModTime, ASTFileSignature Sig)
400 : StoredSize(Size), StoredModTime(ModTime), StoredSignature(Sig) {}
401 };
402
403 /// Builder that generates the global module index file.
404 class GlobalModuleIndexBuilder {
405 FileManager &FileMgr;
406 const PCHContainerReader &PCHContainerRdr;
407
408 /// Mapping from files to module file information.
409 using ModuleFilesMap = llvm::MapVector<FileEntryRef, ModuleFileInfo>;
410
411 /// Information about each of the known module files.
412 ModuleFilesMap ModuleFiles;
413
414 /// Mapping from the imported module file to the imported
415 /// information.
416 using ImportedModuleFilesMap =
417 std::multimap<FileEntryRef, ImportedModuleFileInfo>;
418
419 /// Information about each importing of a module file.
420 ImportedModuleFilesMap ImportedModuleFiles;
421
422 /// Mapping from identifiers to the list of module file IDs that
423 /// consider this identifier to be interesting.
424 typedef llvm::StringMap<SmallVector<unsigned, 2> > InterestingIdentifierMap;
425
426 /// A mapping from all interesting identifiers to the set of module
427 /// files in which those identifiers are considered interesting.
428 InterestingIdentifierMap InterestingIdentifiers;
429
430 /// Write the block-info block for the global module index file.
431 void emitBlockInfoBlock(llvm::BitstreamWriter &Stream);
432
433 /// Retrieve the module file information for the given file.
434 ModuleFileInfo &getModuleFileInfo(FileEntryRef File) {
435 auto [It, Inserted] = ModuleFiles.try_emplace(File);
436 if (Inserted) {
437 unsigned NewID = ModuleFiles.size();
438 ModuleFileInfo &Info = It->second;
439 Info.ID = NewID;
440 }
441 return It->second;
442 }
443
444 public:
445 explicit GlobalModuleIndexBuilder(
446 FileManager &FileMgr, const PCHContainerReader &PCHContainerRdr)
447 : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr) {}
448
449 /// Load the contents of the given module file into the builder.
450 llvm::Error loadModuleFile(FileEntryRef File);
451
452 /// Write the index to the given bitstream.
453 /// \returns true if an error occurred, false otherwise.
454 bool writeIndex(llvm::BitstreamWriter &Stream);
455 };
456}
457
458static void emitBlockID(unsigned ID, const char *Name,
459 llvm::BitstreamWriter &Stream,
461 Record.clear();
462 Record.push_back(ID);
463 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
464
465 // Emit the block name if present.
466 if (!Name || Name[0] == 0) return;
467 Record.clear();
468 while (*Name)
469 Record.push_back(*Name++);
470 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
471}
472
473static void emitRecordID(unsigned ID, const char *Name,
474 llvm::BitstreamWriter &Stream,
476 Record.clear();
477 Record.push_back(ID);
478 while (*Name)
479 Record.push_back(*Name++);
480 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
481}
482
483void
484GlobalModuleIndexBuilder::emitBlockInfoBlock(llvm::BitstreamWriter &Stream) {
486 Stream.EnterBlockInfoBlock();
487
488#define BLOCK(X) emitBlockID(X ## _ID, #X, Stream, Record)
489#define RECORD(X) emitRecordID(X, #X, Stream, Record)
490 BLOCK(GLOBAL_INDEX_BLOCK);
491 RECORD(INDEX_METADATA);
492 RECORD(MODULE);
493 RECORD(IDENTIFIER_INDEX);
494#undef RECORD
495#undef BLOCK
496
497 Stream.ExitBlock();
498}
499
500namespace {
501 class InterestingASTIdentifierLookupTrait
503
504 public:
505 /// The identifier and whether it is "interesting".
506 typedef std::pair<StringRef, bool> data_type;
507
508 data_type ReadData(const internal_key_type& k,
509 const unsigned char* d,
510 unsigned DataLen) {
511 // The first bit indicates whether this identifier is interesting.
512 // That's all we care about.
513 using namespace llvm::support;
514 IdentifierID RawID =
515 endian::readNext<IdentifierID, llvm::endianness::little>(d);
516 bool IsInteresting = RawID & 0x01;
517 return std::make_pair(k, IsInteresting);
518 }
519 };
520}
521
522llvm::Error GlobalModuleIndexBuilder::loadModuleFile(FileEntryRef File) {
523 // Open the module file.
524
525 auto Buffer = FileMgr.getBufferForFile(File, /*isVolatile=*/true);
526 if (!Buffer)
527 return llvm::createStringError(Buffer.getError(),
528 "failed getting buffer for module file");
529
530 // Extract the AST file from the container.
531 std::unique_ptr<llvm::MemoryBuffer> ASTBuffer =
532 PCHContainerRdr.ExtractPCH(std::move(*Buffer));
533
534 // Initialize the input stream
535 llvm::BitstreamCursor InStream(ASTBuffer->getBuffer());
536
537 // Sniff for the signature.
538 for (unsigned char C : {'C', 'P', 'C', 'H'})
539 if (Expected<llvm::SimpleBitstreamCursor::word_t> Res = InStream.Read(8)) {
540 if (Res.get() != C)
541 return llvm::createStringError(std::errc::illegal_byte_sequence,
542 "expected signature CPCH");
543 } else
544 return Res.takeError();
545
546 // Record this module file and assign it a unique ID (if it doesn't have
547 // one already).
548 unsigned ID = getModuleFileInfo(File).ID;
549
550 // Search for the blocks and records we care about.
551 enum { Other, ControlBlock, ASTBlock, DiagnosticOptionsBlock } State = Other;
552 bool Done = false;
553 while (!Done) {
554 Expected<llvm::BitstreamEntry> MaybeEntry = InStream.advance();
555 if (!MaybeEntry)
556 return MaybeEntry.takeError();
557 llvm::BitstreamEntry Entry = MaybeEntry.get();
558
559 switch (Entry.Kind) {
560 case llvm::BitstreamEntry::Error:
561 Done = true;
562 continue;
563
564 case llvm::BitstreamEntry::Record:
565 // In the 'other' state, just skip the record. We don't care.
566 if (State == Other) {
567 if (llvm::Expected<unsigned> Skipped = InStream.skipRecord(Entry.ID))
568 continue;
569 else
570 return Skipped.takeError();
571 }
572
573 // Handle potentially-interesting records below.
574 break;
575
576 case llvm::BitstreamEntry::SubBlock:
577 if (Entry.ID == CONTROL_BLOCK_ID) {
578 if (llvm::Error Err = InStream.EnterSubBlock(CONTROL_BLOCK_ID))
579 return Err;
580
581 // Found the control block.
582 State = ControlBlock;
583 continue;
584 }
585
586 if (Entry.ID == AST_BLOCK_ID) {
587 if (llvm::Error Err = InStream.EnterSubBlock(AST_BLOCK_ID))
588 return Err;
589
590 // Found the AST block.
591 State = ASTBlock;
592 continue;
593 }
594
595 if (Entry.ID == UNHASHED_CONTROL_BLOCK_ID) {
596 if (llvm::Error Err = InStream.EnterSubBlock(UNHASHED_CONTROL_BLOCK_ID))
597 return Err;
598
599 // Found the Diagnostic Options block.
600 State = DiagnosticOptionsBlock;
601 continue;
602 }
603
604 if (llvm::Error Err = InStream.SkipBlock())
605 return Err;
606
607 continue;
608
609 case llvm::BitstreamEntry::EndBlock:
610 State = Other;
611 continue;
612 }
613
614 // Read the given record.
616 StringRef Blob;
617 Expected<unsigned> MaybeCode = InStream.readRecord(Entry.ID, Record, &Blob);
618 if (!MaybeCode)
619 return MaybeCode.takeError();
620 unsigned Code = MaybeCode.get();
621
622 // Handle module dependencies.
623 if (State == ControlBlock && Code == IMPORT) {
624 unsigned Idx = 0;
625 // Read information about the AST file.
626
627 // Skip the imported kind
628 ++Idx;
629
630 // Skip the import location
631 ++Idx;
632
633 // Skip the module name (currently this is only used for prebuilt
634 // modules while here we are only dealing with cached).
635 Blob = Blob.substr(Record[Idx++]);
636
637 ++Idx; // Standard C++ module
638 bool IsCXX20ModuleOrPrebuiltHeaderUnit = Record[Idx++];
639
640 // Skip C++20 modules and prebuilt header units.
641 if (IsCXX20ModuleOrPrebuiltHeaderUnit)
642 continue;
643
644 // Load stored size/modification time.
645 off_t StoredSize = (off_t)Record[Idx++];
646 time_t StoredModTime = (time_t)Record[Idx++];
647 (void)Record[Idx++]; // ImplicitModuleSuffixLength
648
649 // Skip the stored signature.
650 // FIXME: we could read the signature out of the import and validate it.
651 StringRef SignatureBytes = Blob.substr(0, ASTFileSignature::size);
652 auto StoredSignature = ASTFileSignature::create(SignatureBytes.begin(),
653 SignatureBytes.end());
654 Blob = Blob.substr(ASTFileSignature::size);
655
656 // Retrieve the imported file name.
657 unsigned Length = Record[Idx++];
658 StringRef ImportedFile = Blob.substr(0, Length);
659 Blob = Blob.substr(Length);
660
661 // Find the imported module file.
662 auto DependsOnFile =
663 FileMgr.getOptionalFileRef(ImportedFile, /*OpenFile=*/false,
664 /*CacheFailure=*/false);
665
666 if (!DependsOnFile)
667 return llvm::createStringError(std::errc::bad_file_descriptor,
668 "imported file \"%s\" not found",
669 std::string(ImportedFile).c_str());
670
671 // Save the information in ImportedModuleFileInfo so we can verify after
672 // loading all pcms.
673 ImportedModuleFiles.insert(std::make_pair(
674 *DependsOnFile, ImportedModuleFileInfo(StoredSize, StoredModTime,
675 StoredSignature)));
676
677 // Record the dependency.
678 unsigned DependsOnID = getModuleFileInfo(*DependsOnFile).ID;
679 getModuleFileInfo(File).Dependencies.push_back(DependsOnID);
680
681 continue;
682 }
683
684 // Handle the identifier table
685 if (State == ASTBlock && Code == IDENTIFIER_TABLE && Record[0] > 0) {
686 typedef llvm::OnDiskIterableChainedHashTable<
687 InterestingASTIdentifierLookupTrait> InterestingIdentifierTable;
688 std::unique_ptr<InterestingIdentifierTable> Table(
689 InterestingIdentifierTable::Create(
690 (const unsigned char *)Blob.data() + Record[0],
691 (const unsigned char *)Blob.data() + sizeof(uint32_t),
692 (const unsigned char *)Blob.data()));
693 for (InterestingIdentifierTable::data_iterator D = Table->data_begin(),
694 DEnd = Table->data_end();
695 D != DEnd; ++D) {
696 std::pair<StringRef, bool> Ident = *D;
697 if (Ident.second)
698 InterestingIdentifiers[Ident.first].push_back(ID);
699 else
700 (void)InterestingIdentifiers[Ident.first];
701 }
702 }
703
704 // Get Signature.
705 if (State == DiagnosticOptionsBlock && Code == SIGNATURE) {
706 auto Signature = ASTFileSignature::create(Blob.begin(), Blob.end());
707 assert(Signature != ASTFileSignature::createDummy() &&
708 "Dummy AST file signature not backpatched in ASTWriter.");
709 getModuleFileInfo(File).Signature = Signature;
710 }
711
712 // We don't care about this record.
713 }
714
715 return llvm::Error::success();
716}
717
718namespace {
719
720/// Trait used to generate the identifier index as an on-disk hash
721/// table.
722class IdentifierIndexWriterTrait {
723public:
724 typedef StringRef key_type;
725 typedef StringRef key_type_ref;
726 typedef SmallVector<unsigned, 2> data_type;
727 typedef const SmallVector<unsigned, 2> &data_type_ref;
728 typedef unsigned hash_value_type;
729 typedef unsigned offset_type;
730
731 static hash_value_type ComputeHash(key_type_ref Key) {
732 return llvm::djbHash(Key);
733 }
734
735 std::pair<unsigned,unsigned>
736 EmitKeyDataLength(raw_ostream& Out, key_type_ref Key, data_type_ref Data) {
737 using namespace llvm::support;
738 endian::Writer LE(Out, llvm::endianness::little);
739 unsigned KeyLen = Key.size();
740 unsigned DataLen = Data.size() * 4;
741 LE.write<uint16_t>(KeyLen);
742 LE.write<uint16_t>(DataLen);
743 return std::make_pair(KeyLen, DataLen);
744 }
745
746 void EmitKey(raw_ostream& Out, key_type_ref Key, unsigned KeyLen) {
747 Out.write(Key.data(), KeyLen);
748 }
749
750 void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref Data,
751 unsigned DataLen) {
752 using namespace llvm::support;
753 for (unsigned I = 0, N = Data.size(); I != N; ++I)
754 endian::write<uint32_t>(Out, Data[I], llvm::endianness::little);
755 }
756};
757
758}
759
760bool GlobalModuleIndexBuilder::writeIndex(llvm::BitstreamWriter &Stream) {
761 for (auto MapEntry : ImportedModuleFiles) {
762 auto File = MapEntry.first;
763 ImportedModuleFileInfo &Info = MapEntry.second;
764 if (getModuleFileInfo(File).Signature) {
765 if (getModuleFileInfo(File).Signature != Info.StoredSignature)
766 // Verify Signature.
767 return true;
768 } else if (Info.StoredSize != File.getSize() ||
769 Info.StoredModTime != File.getModificationTime())
770 // Verify Size and ModTime.
771 return true;
772 }
773
774 using namespace llvm;
775 llvm::TimeTraceScope TimeScope("Module WriteIndex");
776
777 // Emit the file header.
778 Stream.Emit((unsigned)'B', 8);
779 Stream.Emit((unsigned)'C', 8);
780 Stream.Emit((unsigned)'G', 8);
781 Stream.Emit((unsigned)'I', 8);
782
783 // Write the block-info block, which describes the records in this bitcode
784 // file.
785 emitBlockInfoBlock(Stream);
786
787 Stream.EnterSubblock(GLOBAL_INDEX_BLOCK_ID, 3);
788
789 // Write the metadata.
791 Record.push_back(CurrentVersion);
792 Stream.EmitRecord(INDEX_METADATA, Record);
793
794 // Write the set of known module files.
795 for (ModuleFilesMap::iterator M = ModuleFiles.begin(),
796 MEnd = ModuleFiles.end();
797 M != MEnd; ++M) {
798 Record.clear();
799 Record.push_back(M->second.ID);
800 Record.push_back(M->first.getSize());
801 Record.push_back(M->first.getModificationTime());
802
803 // File name
804 StringRef Name(M->first.getName());
805 Record.push_back(Name.size());
806 Record.append(Name.begin(), Name.end());
807
808 // Dependencies
809 Record.push_back(M->second.Dependencies.size());
810 Record.append(M->second.Dependencies.begin(), M->second.Dependencies.end());
811 Stream.EmitRecord(MODULE, Record);
812 }
813
814 // Write the identifier -> module file mapping.
815 {
816 llvm::OnDiskChainedHashTableGenerator<IdentifierIndexWriterTrait> Generator;
817 IdentifierIndexWriterTrait Trait;
818
819 // Populate the hash table.
820 for (InterestingIdentifierMap::iterator I = InterestingIdentifiers.begin(),
821 IEnd = InterestingIdentifiers.end();
822 I != IEnd; ++I) {
823 Generator.insert(I->first(), I->second, Trait);
824 }
825
826 // Create the on-disk hash table in a buffer.
828 uint32_t BucketOffset;
829 {
830 using namespace llvm::support;
831 llvm::raw_svector_ostream Out(IdentifierTable);
832 // Make sure that no bucket is at offset 0
833 endian::write<uint32_t>(Out, 0, llvm::endianness::little);
834 BucketOffset = Generator.Emit(Out, Trait);
835 }
836
837 // Create a blob abbreviation
838 auto Abbrev = std::make_shared<BitCodeAbbrev>();
839 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_INDEX));
840 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
841 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
842 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
843
844 // Write the identifier table
845 uint64_t Record[] = {IDENTIFIER_INDEX, BucketOffset};
846 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
847 }
848
849 Stream.ExitBlock();
850 return false;
851}
852
853llvm::Error
855 const PCHContainerReader &PCHContainerRdr,
856 StringRef Path) {
857 // This is a compiler-internal input/output, let's bypass the sandbox.
858 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
859
860 llvm::SmallString<128> IndexPath;
861 IndexPath += Path;
862 llvm::sys::path::append(IndexPath, IndexFileName);
863
864 // Coordinate building the global index file with other processes that might
865 // try to do the same.
866 llvm::LockFileManager Lock(IndexPath);
867 bool Owned;
868 if (llvm::Error Err = Lock.tryLock().moveInto(Owned)) {
869 llvm::consumeError(std::move(Err));
870 return llvm::createStringError(std::errc::io_error, "LFS error");
871 }
872 if (!Owned) {
873 // Someone else is responsible for building the index. We don't care
874 // when they finish, so we're done.
875 return llvm::createStringError(std::errc::device_or_resource_busy,
876 "someone else is building the index");
877 }
878
879 // We're responsible for building the index ourselves.
880
881 // The module index builder.
882 GlobalModuleIndexBuilder Builder(FileMgr, PCHContainerRdr);
883
884 // Load each of the module files.
885 std::error_code EC;
886 for (llvm::sys::fs::directory_iterator D(Path, EC), DEnd;
887 D != DEnd && !EC;
888 D.increment(EC)) {
889 // If this isn't a module file, we don't care.
890 if (llvm::sys::path::extension(D->path()) != ".pcm") {
891 // ... unless it's a .pcm.lock file, which indicates that someone is
892 // in the process of rebuilding a module. They'll rebuild the index
893 // at the end of that translation unit, so we don't have to.
894 if (StringRef(D->path()).ends_with(".pcm.lock"))
895 return llvm::createStringError(std::errc::device_or_resource_busy,
896 "someone else is building the index");
897
898 continue;
899 }
900
901 // If we can't find the module file, skip it.
902 auto ModuleFile = FileMgr.getOptionalFileRef(D->path());
903 if (!ModuleFile)
904 continue;
905
906 // Load this module file.
907 if (llvm::Error Err = Builder.loadModuleFile(*ModuleFile))
908 return Err;
909 }
910
911 // The output buffer, into which the global index will be written.
912 SmallString<16> OutputBuffer;
913 {
914 llvm::BitstreamWriter OutputStream(OutputBuffer);
915 if (Builder.writeIndex(OutputStream))
916 return llvm::createStringError(std::errc::io_error,
917 "failed writing index");
918 }
919
920 return llvm::writeToOutput(IndexPath, [&OutputBuffer](llvm::raw_ostream &OS) {
921 OS << OutputBuffer;
922 return llvm::Error::success();
923 });
924}
925
926namespace {
927 class GlobalIndexIdentifierIterator : public IdentifierIterator {
928 /// The current position within the identifier lookup table.
929 IdentifierIndexTable::key_iterator Current;
930
931 /// The end position within the identifier lookup table.
932 IdentifierIndexTable::key_iterator End;
933
934 public:
935 explicit GlobalIndexIdentifierIterator(IdentifierIndexTable &Idx) {
936 Current = Idx.key_begin();
937 End = Idx.key_end();
938 }
939
940 StringRef Next() override {
941 if (Current == End)
942 return StringRef();
943
944 StringRef Result = *Current;
945 ++Current;
946 return Result;
947 }
948 };
949}
950
952 IdentifierIndexTable &Table =
953 *static_cast<IdentifierIndexTable *>(IdentifierIndex);
954 return new GlobalIndexIdentifierIterator(Table);
955}
#define RECORD(CLASS, BASE)
Defines the clang::FileManager interface and associated types.
FormatToken * Next
The next token in the unwrapped line.
static void emitRecordID(unsigned ID, const char *Name, llvm::BitstreamWriter &Stream, SmallVectorImpl< uint64_t > &Record)
static const unsigned CurrentVersion
The global index file version.
static const char *const IndexFileName
The name of the global index file.
static void emitBlockID(unsigned ID, const char *Name, llvm::BitstreamWriter &Stream, SmallVectorImpl< uint64_t > &Record)
Result
Implement __builtin_bit_cast and related operations.
llvm::MachO::Record Record
Definition MachO.h:31
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
#define IMPORT(DERIVED, BASE)
Definition Template.h:637
#define BLOCK(DERIVED, BASE)
Definition Template.h:653
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition FileEntry.h:57
Implements support for file system lookup, file system caching, and directory search management.
Definition FileManager.h:57
bool loadedModuleFile(ModuleFile *File)
Note that the given module file has been loaded.
void printStats()
Print statistics to standard error.
llvm::SmallPtrSet< ModuleFile *, 4 > HitSet
A set of module files in which we found a result.
bool lookupIdentifier(llvm::StringRef Name, HitSet &Hits)
Look for all of the module files with information about the given identifier, e.g....
void getModuleDependencies(ModuleFile *File, llvm::SmallVectorImpl< ModuleFile * > &Dependencies)
Retrieve the set of module files on which the given module file directly depends.
IdentifierIterator * createIdentifierIterator() const
Returns an iterator for identifiers stored in the index table.
static std::pair< GlobalModuleIndex *, llvm::Error > readIndex(llvm::StringRef Path)
Read a global index file for the given directory.
void dump()
Print debugging view to standard error.
static llvm::Error writeIndex(FileManager &FileMgr, const PCHContainerReader &PCHContainerRdr, llvm::StringRef Path)
Write a global index into the given.
An iterator that walks over all of the known identifiers in the lookup table.
Implements an efficient mapping from strings to IdentifierInfo nodes.
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
virtual std::unique_ptr< llvm::MemoryBuffer > ExtractPCH(std::unique_ptr< llvm::MemoryBuffer > Buffer) const =0
Returns a buffer with the serialized AST inside the PCH container Buffer.
Base class for the trait describing the on-disk hash table for the identifiers in an AST file.
@ ModuleFileInfo
Dump information about a module file.
bool LE(InterpState &S, CodePtr OpPC)
Definition Interp.h:1512
@ AST_BLOCK_ID
The AST block, which acts as a container around the full AST block.
@ CONTROL_BLOCK_ID
The control block, which contains all of the information that needs to be validated prior to committi...
@ UNHASHED_CONTROL_BLOCK_ID
A block with unhashed content.
@ SIGNATURE
Record code for the signature that identifiers this AST file.
@ IDENTIFIER_TABLE
Record code for the identifier table.
unsigned ComputeHash(Selector Sel)
uint64_t IdentifierID
An ID number that refers to an identifier in an AST file.
Definition ASTBitCodes.h:63
std::shared_ptr< MatchComputation< T > > Generator
Definition RewriteRule.h:65
Top level wrappers for InstallAPI frontend operations.
int32_t uint32_t uint32_t __builtin_riscv_mhracc_i32 __builtin_riscv_mhraccu_u32 __builtin_riscv_mhraccsu_i32 __builtin_riscv_mhacc_h1_i32 __builtin_riscv_mhaccsu_h1_i32 __builtin_riscv_mulqr_i32 __builtin_riscv_mqrwacc_i64 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 __packed_splat4 uint16_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
__LIBC_ATTRS FILE * stderr
The signature of a module, which is a hash of the AST content.
Definition Module.h:198
static constexpr size_t size
Definition Module.h:201
static ASTFileSignature create(std::array< uint8_t, 20 > Bytes)
Definition Module.h:221
static ASTFileSignature createDummy()
Definition Module.h:231