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;
55 if (
const FileEntry *File = Mod->getASTFile())
62 auto Known = Modules.find(File);
63 if (Known == Modules.end())
69 std::unique_ptr<llvm::MemoryBuffer>
73 return std::move(InMemoryBuffers[Entry]);
78 std::string &ErrorStr) {
79 if (!ExpectedSignature || Signature == ExpectedSignature)
83 Signature ?
"signature mismatch" :
"could not read module signature";
91 ImportedBy->
Imports.insert(&MF);
104 off_t ExpectedSize, time_t ExpectedModTime,
108 std::string &ErrorStr) {
124 ErrorStr =
"module file out of date";
128 if (!Entry && FileName !=
"-") {
129 ErrorStr =
"module file not found";
134 if (
ModuleFile *ModuleEntry = Modules.lookup(Entry)) {
136 if (
checkSignature(ModuleEntry->Signature, ExpectedSignature, ErrorStr))
139 Module = ModuleEntry;
145 auto NewModule = llvm::make_unique<ModuleFile>(Type, Generation);
146 NewModule->Index = Chain.size();
147 NewModule->FileName = FileName.str();
148 NewModule->File = Entry;
149 NewModule->ImportLoc = ImportLoc;
150 NewModule->InputFilesValidationTimestamp = 0;
153 std::string TimestampFilename = NewModule->getTimestampFilename();
154 llvm::vfs::Status Status;
157 NewModule->InputFilesValidationTimestamp =
158 llvm::sys::toTimeT(Status.getLastModificationTime());
162 if (std::unique_ptr<llvm::MemoryBuffer> Buffer =
lookupBuffer(FileName)) {
164 NewModule->Buffer = &ModuleCache->addBuiltPCM(FileName, std::move(Buffer));
169 }
else if (llvm::MemoryBuffer *Buffer =
171 NewModule->Buffer = Buffer;
181 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf((std::error_code()));
182 if (FileName ==
"-") {
183 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)) {
214 Module = Modules[Entry] = NewModule.get();
218 if (!NewModule->isModule())
219 PCHChain.push_back(NewModule.get());
221 Roots.push_back(NewModule.get());
223 Chain.push_back(std::move(NewModule));
229 llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
239 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(
240 (llvm::pointer_iterator<ModuleIterator>(First)),
241 (llvm::pointer_iterator<ModuleIterator>(
Last)));
244 return victimSet.count(MF);
247 for (
auto I =
begin(); I != First; ++I) {
248 I->Imports.remove_if(IsVictim);
249 I->ImportedBy.remove_if(IsVictim);
251 Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim),
255 for (
auto I = First; I !=
Last; ++I) {
256 if (!I->isModule()) {
257 PCHChain.erase(llvm::find(PCHChain, &*I), PCHChain.end());
264 Modules.erase(victim->File);
267 StringRef ModuleName = victim->ModuleName;
269 mod->setASTFile(
nullptr);
275 Chain.erase(Chain.begin() + (First -
begin()), Chain.end());
280 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
283 InMemoryBuffers[Entry] = std::move(Buffer);
286 ModuleManager::VisitState *ModuleManager::allocateVisitState() {
288 if (FirstVisitState) {
289 VisitState *Result = FirstVisitState;
290 FirstVisitState = FirstVisitState->NextState;
291 Result->NextState =
nullptr;
296 return new VisitState(
size());
299 void ModuleManager::returnVisitState(VisitState *
State) {
300 assert(State->NextState ==
nullptr &&
"Visited state is in list?");
301 State->NextState = FirstVisitState;
302 FirstVisitState =
State;
308 ModulesInCommonWithGlobalIndex.clear();
316 ModulesInCommonWithGlobalIndex.push_back(&M);
323 ModulesInCommonWithGlobalIndex.push_back(MF);
330 : FileMgr(FileMgr), ModuleCache(&ModuleCache),
331 PCHContainerRdr(PCHContainerRdr), HeaderSearchInfo(HeaderSearchInfo) {}
336 llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
338 if (VisitOrder.size() != Chain.size()) {
341 VisitOrder.reserve(N);
349 UnusedIncomingEdges.resize(
size());
352 UnusedIncomingEdges[M.
Index] = Size;
359 while (!Queue.empty()) {
360 ModuleFile *CurrentModule = Queue.pop_back_val();
361 VisitOrder.push_back(CurrentModule);
365 for (
auto M = CurrentModule->
Imports.rbegin(),
366 MEnd = CurrentModule->
Imports.rend();
372 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
373 if (NumUnusedEdges && (--NumUnusedEdges == 0))
378 assert(VisitOrder.size() == N &&
"Visitation order is wrong?");
380 delete FirstVisitState;
381 FirstVisitState =
nullptr;
384 VisitState *
State = allocateVisitState();
385 unsigned VisitNumber = State->NextVisitNumber++;
390 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
391 for (
unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
393 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
394 if (!ModuleFilesHit->count(M))
395 State->VisitNumber[M->
Index] = VisitNumber;
399 for (
unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
402 if (State->VisitNumber[CurrentModule->
Index] == VisitNumber)
406 assert(State->VisitNumber[CurrentModule->
Index] == VisitNumber - 1);
407 State->VisitNumber[CurrentModule->
Index] = VisitNumber;
408 if (!Visitor(*CurrentModule))
418 for (llvm::SetVector<ModuleFile *>::iterator
419 M = NextModule->
Imports.begin(),
420 MEnd = NextModule->
Imports.end();
422 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
423 State->Stack.push_back(*M);
424 State->VisitNumber[(*M)->Index] = VisitNumber;
428 if (State->Stack.empty())
432 NextModule = State->Stack.pop_back_val();
436 returnVisitState(State);
441 time_t ExpectedModTime,
443 if (FileName ==
"-") {
450 File = FileMgr.
getFile(FileName,
true,
false);
454 if ((ExpectedSize && ExpectedSize != File->
getSize()) ||
504 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.
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...
const FileEntry * getFile(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
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)
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.
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(const FileEntry *Entry, bool isVolatile=false, bool ShouldCloseOpenFile=true)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
static void updateModuleImports(ModuleFile &MF, ModuleFile *ImportedBy, SourceLocation ImportLoc)
bool getNoncachedStatValue(StringRef Path, llvm::vfs::Status &Result)
Get the 'stat' information for the given Path.
void removeModules(ModuleIterator First, llvm::SmallPtrSetImpl< ModuleFile *> &LoadedSuccessfully, ModuleMap *modMap)
Remove the modules starting from First (to the end).
void moduleFileAccepted(ModuleFile *MF)
Notification from the AST reader that the given module file has been "accepted", and will not (can no...
std::string getNodeLabel(ModuleFile *M, const ModuleManager &)
unsigned size() const
Number of modules loaded.
The module file had already been loaded.
void invalidateCache(const FileEntry *Entry)
Remove the real file Entry from the cache.
static ChildIteratorType child_end(NodeRef Node)
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)