23 #include "llvm/ADT/STLExtras.h" 24 #include "llvm/ADT/SetVector.h" 25 #include "llvm/ADT/SmallPtrSet.h" 26 #include "llvm/ADT/SmallVector.h" 27 #include "llvm/ADT/StringRef.h" 28 #include "llvm/ADT/iterator.h" 29 #include "llvm/Support/Chrono.h" 30 #include "llvm/Support/DOTGraphTraits.h" 31 #include "llvm/Support/ErrorOr.h" 32 #include "llvm/Support/GraphWriter.h" 33 #include "llvm/Support/MemoryBuffer.h" 34 #include "llvm/Support/VirtualFileSystem.h" 39 #include <system_error> 41 using namespace clang;
42 using namespace serialization;
45 auto Entry = FileMgr.
getFile(Name,
false,
55 if (
const FileEntry *File = Mod->getASTFile())
62 auto Known = Modules.find(File);
63 if (Known == Modules.end())
69 std::unique_ptr<llvm::MemoryBuffer>
71 auto Entry = FileMgr.
getFile(Name,
false,
75 return std::move(InMemoryBuffers[*Entry]);
80 std::string &ErrorStr) {
81 if (!ExpectedSignature || Signature == ExpectedSignature)
85 Signature ?
"signature mismatch" :
"could not read module signature";
93 ImportedBy->
Imports.insert(&MF);
106 off_t ExpectedSize, time_t ExpectedModTime,
110 std::string &ErrorStr) {
126 ErrorStr =
"module file out of date";
130 if (!Entry && FileName !=
"-") {
131 ErrorStr =
"module file not found";
136 if (
ModuleFile *ModuleEntry = Modules.lookup(Entry)) {
138 if (
checkSignature(ModuleEntry->Signature, ExpectedSignature, ErrorStr))
141 Module = ModuleEntry;
147 auto NewModule = std::make_unique<ModuleFile>(Type, Generation);
148 NewModule->Index = Chain.size();
149 NewModule->FileName = FileName.str();
150 NewModule->File = Entry;
151 NewModule->ImportLoc = ImportLoc;
152 NewModule->InputFilesValidationTimestamp = 0;
155 std::string TimestampFilename = NewModule->getTimestampFilename();
156 llvm::vfs::Status Status;
159 NewModule->InputFilesValidationTimestamp =
160 llvm::sys::toTimeT(Status.getLastModificationTime());
164 if (std::unique_ptr<llvm::MemoryBuffer> Buffer =
lookupBuffer(FileName)) {
166 NewModule->Buffer = &ModuleCache->addBuiltPCM(FileName, std::move(Buffer));
171 }
else if (llvm::MemoryBuffer *Buffer =
173 NewModule->Buffer = Buffer;
183 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf((std::error_code()));
184 if (FileName ==
"-") {
185 Buf = llvm::MemoryBuffer::getSTDIN();
192 ErrorStr = Buf.getError().message();
200 NewModule->Data = PCHContainerRdr.
ExtractPCH(*NewModule->Buffer);
204 if (ExpectedSignature &&
checkSignature(ReadSignature(NewModule->Data),
205 ExpectedSignature, ErrorStr))
209 Module = Modules[Entry] = NewModule.get();
213 if (!NewModule->isModule())
214 PCHChain.push_back(NewModule.get());
216 Roots.push_back(NewModule.get());
218 Chain.push_back(std::move(NewModule));
231 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(
232 (llvm::pointer_iterator<ModuleIterator>(First)),
233 (llvm::pointer_iterator<ModuleIterator>(
Last)));
236 return victimSet.count(MF);
239 for (
auto I =
begin(); I != First; ++I) {
240 I->Imports.remove_if(IsVictim);
241 I->ImportedBy.remove_if(IsVictim);
243 Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim),
247 for (
auto I = First; I !=
Last; ++I) {
248 if (!I->isModule()) {
249 PCHChain.erase(llvm::find(PCHChain, &*I), PCHChain.end());
256 Modules.erase(victim->File);
259 StringRef ModuleName = victim->ModuleName;
261 mod->setASTFile(
nullptr);
267 Chain.erase(Chain.begin() + (First -
begin()), Chain.end());
272 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
275 InMemoryBuffers[Entry] = std::move(Buffer);
278 ModuleManager::VisitState *ModuleManager::allocateVisitState() {
280 if (FirstVisitState) {
281 VisitState *Result = FirstVisitState;
282 FirstVisitState = FirstVisitState->NextState;
283 Result->NextState =
nullptr;
288 return new VisitState(
size());
291 void ModuleManager::returnVisitState(VisitState *
State) {
292 assert(State->NextState ==
nullptr &&
"Visited state is in list?");
293 State->NextState = FirstVisitState;
294 FirstVisitState =
State;
300 ModulesInCommonWithGlobalIndex.clear();
308 ModulesInCommonWithGlobalIndex.push_back(&M);
315 ModulesInCommonWithGlobalIndex.push_back(MF);
322 : FileMgr(FileMgr), ModuleCache(&ModuleCache),
323 PCHContainerRdr(PCHContainerRdr), HeaderSearchInfo(HeaderSearchInfo) {}
328 llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
330 if (VisitOrder.size() != Chain.size()) {
333 VisitOrder.reserve(N);
341 UnusedIncomingEdges.resize(
size());
344 UnusedIncomingEdges[M.
Index] = Size;
351 while (!Queue.empty()) {
352 ModuleFile *CurrentModule = Queue.pop_back_val();
353 VisitOrder.push_back(CurrentModule);
357 for (
auto M = CurrentModule->
Imports.rbegin(),
358 MEnd = CurrentModule->
Imports.rend();
364 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
365 if (NumUnusedEdges && (--NumUnusedEdges == 0))
370 assert(VisitOrder.size() == N &&
"Visitation order is wrong?");
372 delete FirstVisitState;
373 FirstVisitState =
nullptr;
376 VisitState *
State = allocateVisitState();
377 unsigned VisitNumber = State->NextVisitNumber++;
382 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
383 for (
unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
385 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
386 if (!ModuleFilesHit->count(M))
387 State->VisitNumber[M->
Index] = VisitNumber;
391 for (
unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
394 if (State->VisitNumber[CurrentModule->
Index] == VisitNumber)
398 assert(State->VisitNumber[CurrentModule->
Index] == VisitNumber - 1);
399 State->VisitNumber[CurrentModule->
Index] = VisitNumber;
400 if (!Visitor(*CurrentModule))
410 for (llvm::SetVector<ModuleFile *>::iterator
411 M = NextModule->
Imports.begin(),
412 MEnd = NextModule->
Imports.end();
414 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
415 State->Stack.push_back(*M);
416 State->VisitNumber[(*M)->Index] = VisitNumber;
420 if (State->Stack.empty())
424 NextModule = State->Stack.pop_back_val();
428 returnVisitState(State);
433 time_t ExpectedModTime,
435 if (FileName ==
"-") {
442 auto FileOrErr = FileMgr.
getFile(FileName,
true,
450 if ((ExpectedSize && ExpectedSize != File->
getSize()) ||
500 llvm::ViewGraph(*
this,
"Modules");
Implements support for file system lookup, file system caching, and directory search management...
llvm::SetVector< ModuleFile * >::const_iterator ChildIteratorType
ModuleFile * lookup(const FileEntry *File) const
Returns the module associated with the given module file.
Defines the clang::FileManager interface and associated types.
time_t getModificationTime() const
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
static bool renderGraphFromBottomUp()
std::string ModuleName
The name of the module.
The module file is out-of-date.
The base class of the type hierarchy.
DOTGraphTraits(bool IsSimple=false)
unsigned Index
The index of this module in the list of modules.
ModuleFile * lookupByFileName(StringRef FileName) const
Returns the module associated with the given file name.
DOTGraphTraits(bool isSimple=false)
Manages the set of modules loaded by an AST reader.
void removeModules(ModuleIterator First, ModuleMap *modMap)
Remove the modules starting from First (to the end).
SourceLocation ImportLoc
The source location where this module was first imported.
AddModuleResult
The result of attempting to add a new module.
ModuleIterator begin()
Forward iterator to traverse all loaded modules.
void viewGraph()
View the graphviz representation of the module graph.
void visit(llvm::function_ref< bool(ModuleFile &M)> Visitor, llvm::SmallPtrSetImpl< ModuleFile *> *ModuleFilesHit=nullptr)
Visit each of the modules.
The signature of a module, which is a hash of the AST content.
void setGlobalIndex(GlobalModuleIndex *Index)
Set the global module index.
Describes a module or submodule.
virtual llvm::StringRef ExtractPCH(llvm::MemoryBufferRef Buffer) const =0
Returns the serialized AST inside the PCH container Buffer.
Module * findModule(StringRef Name) const
Retrieve a module with the given name.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
bool lookupModuleFile(StringRef FileName, off_t ExpectedSize, time_t ExpectedModTime, const FileEntry *&File)
Attempt to resolve the given module file name to a file entry.
ModuleIterator end()
Forward iterator end-point to traverse all loaded modules.
The module file is missing.
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
ASTFileSignature(*)(StringRef) ASTFileSignatureReader
ModuleKind
Specifies the kind of module that has been loaded.
File is from a prebuilt module path.
static nodes_iterator nodes_end(const ModuleManager &Manager)
llvm::MemoryBuffer & addPCM(llvm::StringRef Filename, std::unique_ptr< llvm::MemoryBuffer > Buffer)
Store the PCM under the Filename.
InMemoryModuleCache & getModuleCache() const
Information about a module that has been loaded by the ASTReader.
AddModuleResult addModule(StringRef FileName, ModuleKind Type, SourceLocation ImportLoc, ModuleFile *ImportedBy, unsigned Generation, off_t ExpectedSize, time_t ExpectedModTime, ASTFileSignature ExpectedSignature, ASTFileSignatureReader ReadSignature, ModuleFile *&Module, std::string &ErrorStr)
Attempts to create a new module and add it to the list of known modules.
In-memory cache for modules.
void addInMemoryBuffer(StringRef FileName, std::unique_ptr< llvm::MemoryBuffer > Buffer)
Add an in-memory buffer the list of known buffers.
static nodes_iterator nodes_begin(const ModuleManager &Manager)
Encodes a location in the source.
File is an implicitly-loaded module.
Cached information about one file (either on disk or in the virtual file system). ...
static ChildIteratorType child_begin(NodeRef Node)
std::error_code getNoncachedStatValue(StringRef Path, llvm::vfs::Status &Result)
Get the 'stat' information for the given Path.
const FileEntry * getVirtualFile(StringRef Filename, off_t Size, time_t ModificationTime)
Retrieve a file entry for a "virtual" file that acts as if there were a file with the given name on d...
llvm::SetVector< ModuleFile * > ImportedBy
List of modules which depend on this module.
A global index for a set of module files, providing information about the identifiers within those mo...
The module file was just loaded in response to this call.
pointer_iterator< ModuleManager::ModuleConstIterator > nodes_iterator
ast_type_traits::DynTypedNode Node
Dataflow Directional Tag Classes.
ModuleManager(FileManager &FileMgr, InMemoryModuleCache &ModuleCache, const PCHContainerReader &PCHContainerRdr, const HeaderSearch &HeaderSearchInfo)
File is an explicitly-loaded module.
ModuleFile * lookupByModuleName(StringRef ModName) const
Returns the module associated with the given module name.
static void updateModuleImports(ModuleFile &MF, ModuleFile *ImportedBy, SourceLocation ImportLoc)
void moduleFileAccepted(ModuleFile *MF)
Notification from the AST reader that the given module file has been "accepted", and will not (can no...
llvm::ErrorOr< const FileEntry * > getFile(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
std::string getNodeLabel(ModuleFile *M, const ModuleManager &)
unsigned size() const
Number of modules loaded.
The module file had already been loaded.
static ChildIteratorType child_end(NodeRef Node)
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(const FileEntry *Entry, bool isVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
llvm::pointee_iterator< SmallVectorImpl< std::unique_ptr< ModuleFile > >::iterator > ModuleIterator
bool DirectlyImported
Whether this module has been directly imported by the user.
llvm::SetVector< ModuleFile * > Imports
List of modules which this module depends on.
std::unique_ptr< llvm::MemoryBuffer > lookupBuffer(StringRef Name)
Returns the in-memory (virtual file) buffer with the given name.
bool loadedModuleFile(ModuleFile *File)
Note that the given module file has been loaded.
static bool checkSignature(ASTFileSignature Signature, ASTFileSignature ExpectedSignature, std::string &ErrorStr)