2011-08-26 04:47:51 +08:00
|
|
|
//===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the ModuleManager class, which manages a set of loaded
|
|
|
|
// modules for the ASTReader.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2015-06-21 02:53:08 +08:00
|
|
|
#include "clang/Frontend/PCHContainerOperations.h"
|
2014-04-15 02:00:01 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2013-03-19 08:28:20 +08:00
|
|
|
#include "clang/Lex/ModuleMap.h"
|
2013-01-26 07:32:03 +08:00
|
|
|
#include "clang/Serialization/GlobalModuleIndex.h"
|
2013-08-24 21:16:22 +08:00
|
|
|
#include "clang/Serialization/ModuleManager.h"
|
2011-08-26 04:47:51 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2013-06-12 06:15:02 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2011-08-26 04:47:51 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-06-13 01:19:42 +08:00
|
|
|
#include <system_error>
|
2011-08-26 04:47:51 +08:00
|
|
|
|
2011-10-12 03:27:55 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
#include "llvm/Support/GraphWriter.h"
|
|
|
|
#endif
|
|
|
|
|
2011-08-26 04:47:51 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace serialization;
|
|
|
|
|
2011-12-01 07:21:26 +08:00
|
|
|
ModuleFile *ModuleManager::lookup(StringRef Name) {
|
2013-02-09 05:27:45 +08:00
|
|
|
const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
|
|
|
|
/*cacheFailure=*/false);
|
2013-03-28 00:47:18 +08:00
|
|
|
if (Entry)
|
|
|
|
return lookup(Entry);
|
|
|
|
|
2014-05-22 13:54:18 +08:00
|
|
|
return nullptr;
|
2013-03-28 00:47:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ModuleFile *ModuleManager::lookup(const FileEntry *File) {
|
|
|
|
llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known
|
|
|
|
= Modules.find(File);
|
|
|
|
if (Known == Modules.end())
|
2014-05-22 13:54:18 +08:00
|
|
|
return nullptr;
|
2013-03-28 00:47:18 +08:00
|
|
|
|
|
|
|
return Known->second;
|
2011-08-26 04:47:51 +08:00
|
|
|
}
|
|
|
|
|
2014-08-19 03:16:31 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer>
|
|
|
|
ModuleManager::lookupBuffer(StringRef Name) {
|
2013-02-09 05:27:45 +08:00
|
|
|
const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
|
|
|
|
/*cacheFailure=*/false);
|
2014-08-19 03:16:31 +08:00
|
|
|
return std::move(InMemoryBuffers[Entry]);
|
2011-08-26 04:47:51 +08:00
|
|
|
}
|
|
|
|
|
2013-03-19 08:28:20 +08:00
|
|
|
ModuleManager::AddModuleResult
|
2012-12-01 03:28:05 +08:00
|
|
|
ModuleManager::addModule(StringRef FileName, ModuleKind Type,
|
|
|
|
SourceLocation ImportLoc, ModuleFile *ImportedBy,
|
2013-03-19 08:28:20 +08:00
|
|
|
unsigned Generation,
|
|
|
|
off_t ExpectedSize, time_t ExpectedModTime,
|
2014-10-24 02:05:36 +08:00
|
|
|
ASTFileSignature ExpectedSignature,
|
2015-03-24 12:43:52 +08:00
|
|
|
ASTFileSignatureReader ReadSignature,
|
2013-03-19 08:28:20 +08:00
|
|
|
ModuleFile *&Module,
|
|
|
|
std::string &ErrorStr) {
|
2014-05-22 13:54:18 +08:00
|
|
|
Module = nullptr;
|
2013-03-19 08:28:20 +08:00
|
|
|
|
|
|
|
// Look for the file entry. This only fails if the expected size or
|
|
|
|
// modification time differ.
|
|
|
|
const FileEntry *Entry;
|
2014-11-21 13:37:20 +08:00
|
|
|
if (Type == MK_ExplicitModule) {
|
|
|
|
// If we're not expecting to pull this file out of the module cache, it
|
|
|
|
// might have a different mtime due to being moved across filesystems in
|
|
|
|
// a distributed build. The size must still match, though. (As must the
|
|
|
|
// contents, but we can't check that.)
|
|
|
|
ExpectedModTime = 0;
|
|
|
|
}
|
2013-09-06 07:50:58 +08:00
|
|
|
if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
|
|
|
|
ErrorStr = "module file out of date";
|
2013-03-19 08:28:20 +08:00
|
|
|
return OutOfDate;
|
2013-09-06 07:50:58 +08:00
|
|
|
}
|
2013-03-19 08:28:20 +08:00
|
|
|
|
2011-08-26 04:47:51 +08:00
|
|
|
if (!Entry && FileName != "-") {
|
2013-09-06 07:50:58 +08:00
|
|
|
ErrorStr = "module file not found";
|
2013-03-19 08:28:20 +08:00
|
|
|
return Missing;
|
2011-08-26 04:47:51 +08:00
|
|
|
}
|
2013-03-19 08:28:20 +08:00
|
|
|
|
|
|
|
// Check whether we already loaded this module, before
|
2011-12-01 07:21:26 +08:00
|
|
|
ModuleFile *&ModuleEntry = Modules[Entry];
|
2011-08-26 04:47:51 +08:00
|
|
|
bool NewModule = false;
|
|
|
|
if (!ModuleEntry) {
|
|
|
|
// Allocate a new module.
|
2012-01-19 04:56:22 +08:00
|
|
|
ModuleFile *New = new ModuleFile(Type, Generation);
|
2013-01-22 04:07:12 +08:00
|
|
|
New->Index = Chain.size();
|
2011-08-26 04:47:51 +08:00
|
|
|
New->FileName = FileName.str();
|
2012-10-03 09:58:42 +08:00
|
|
|
New->File = Entry;
|
2012-12-01 03:28:05 +08:00
|
|
|
New->ImportLoc = ImportLoc;
|
2011-08-26 04:47:51 +08:00
|
|
|
Chain.push_back(New);
|
2015-07-22 10:08:40 +08:00
|
|
|
if (!New->isModule())
|
|
|
|
PCHChain.push_back(New);
|
2015-05-20 18:29:23 +08:00
|
|
|
if (!ImportedBy)
|
|
|
|
Roots.push_back(New);
|
2011-08-26 04:47:51 +08:00
|
|
|
NewModule = true;
|
|
|
|
ModuleEntry = New;
|
2012-12-01 03:28:05 +08:00
|
|
|
|
Add an option to allow Clang verify source files for a module only once during
the build
When Clang loads the module, it verifies the user source files that the module
was built from. If any file was changed, the module is rebuilt. There are two
problems with this:
1. correctness: we don't verify system files (there are too many of them, and
stat'ing all of them would take a lot of time);
2. performance: the same module file is verified again and again during a
single build.
This change allows the build system to optimize source file verification. The
idea is based on the fact that while the project is being built, the source
files don't change. This allows us to verify the module only once during a
single build session. The build system passes a flag,
-fbuild-session-timestamp=, to inform Clang of the time when the build started.
The build system also requests to enable this feature by passing
-fmodules-validate-once-per-build-session. If these flags are not passed, the
behavior is not changed. When Clang verifies the module the first time, it
writes out a timestamp file. Then, when Clang loads the module the second
time, it finds a timestamp file, so it can compare the verification timestamp
of the module with the time when the build started. If the verification
timestamp is too old, the module is verified again, and the timestamp file is
updated.
llvm-svn: 201224
2014-02-12 18:33:14 +08:00
|
|
|
New->InputFilesValidationTimestamp = 0;
|
2014-10-22 10:05:46 +08:00
|
|
|
if (New->Kind == MK_ImplicitModule) {
|
Add an option to allow Clang verify source files for a module only once during
the build
When Clang loads the module, it verifies the user source files that the module
was built from. If any file was changed, the module is rebuilt. There are two
problems with this:
1. correctness: we don't verify system files (there are too many of them, and
stat'ing all of them would take a lot of time);
2. performance: the same module file is verified again and again during a
single build.
This change allows the build system to optimize source file verification. The
idea is based on the fact that while the project is being built, the source
files don't change. This allows us to verify the module only once during a
single build session. The build system passes a flag,
-fbuild-session-timestamp=, to inform Clang of the time when the build started.
The build system also requests to enable this feature by passing
-fmodules-validate-once-per-build-session. If these flags are not passed, the
behavior is not changed. When Clang verifies the module the first time, it
writes out a timestamp file. Then, when Clang loads the module the second
time, it finds a timestamp file, so it can compare the verification timestamp
of the module with the time when the build started. If the verification
timestamp is too old, the module is verified again, and the timestamp file is
updated.
llvm-svn: 201224
2014-02-12 18:33:14 +08:00
|
|
|
std::string TimestampFilename = New->getTimestampFilename();
|
2014-02-21 05:59:23 +08:00
|
|
|
vfs::Status Status;
|
Add an option to allow Clang verify source files for a module only once during
the build
When Clang loads the module, it verifies the user source files that the module
was built from. If any file was changed, the module is rebuilt. There are two
problems with this:
1. correctness: we don't verify system files (there are too many of them, and
stat'ing all of them would take a lot of time);
2. performance: the same module file is verified again and again during a
single build.
This change allows the build system to optimize source file verification. The
idea is based on the fact that while the project is being built, the source
files don't change. This allows us to verify the module only once during a
single build session. The build system passes a flag,
-fbuild-session-timestamp=, to inform Clang of the time when the build started.
The build system also requests to enable this feature by passing
-fmodules-validate-once-per-build-session. If these flags are not passed, the
behavior is not changed. When Clang verifies the module the first time, it
writes out a timestamp file. Then, when Clang loads the module the second
time, it finds a timestamp file, so it can compare the verification timestamp
of the module with the time when the build started. If the verification
timestamp is too old, the module is verified again, and the timestamp file is
updated.
llvm-svn: 201224
2014-02-12 18:33:14 +08:00
|
|
|
// A cached stat value would be fine as well.
|
|
|
|
if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
|
|
|
|
New->InputFilesValidationTimestamp =
|
|
|
|
Status.getLastModificationTime().toEpochTime();
|
|
|
|
}
|
|
|
|
|
2011-08-26 04:47:51 +08:00
|
|
|
// Load the contents of the module
|
2014-08-19 03:16:31 +08:00
|
|
|
if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
|
2011-08-26 04:47:51 +08:00
|
|
|
// The buffer was already provided for us.
|
2014-08-19 03:16:31 +08:00
|
|
|
New->Buffer = std::move(Buffer);
|
2011-08-26 04:47:51 +08:00
|
|
|
} else {
|
|
|
|
// Open the AST file.
|
2014-10-27 06:44:13 +08:00
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf(
|
|
|
|
(std::error_code()));
|
2011-08-26 04:47:51 +08:00
|
|
|
if (FileName == "-") {
|
2014-10-27 06:44:13 +08:00
|
|
|
Buf = llvm::MemoryBuffer::getSTDIN();
|
2014-06-20 08:24:56 +08:00
|
|
|
} else {
|
|
|
|
// Leave the FileEntry open so if it gets read again by another
|
|
|
|
// ModuleManager it must be the same underlying file.
|
|
|
|
// FIXME: Because FileManager::getFile() doesn't guarantee that it will
|
|
|
|
// give us an open file, this may not be 100% reliable.
|
2014-10-27 06:44:13 +08:00
|
|
|
Buf = FileMgr.getBufferForFile(New->File,
|
|
|
|
/*IsVolatile=*/false,
|
|
|
|
/*ShouldClose=*/false);
|
2014-06-20 08:24:56 +08:00
|
|
|
}
|
2014-10-27 06:44:13 +08:00
|
|
|
|
|
|
|
if (!Buf) {
|
|
|
|
ErrorStr = Buf.getError().message();
|
2013-03-19 08:28:20 +08:00
|
|
|
return Missing;
|
2014-10-27 06:44:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
New->Buffer = std::move(*Buf);
|
2011-08-26 04:47:51 +08:00
|
|
|
}
|
2015-06-21 02:53:08 +08:00
|
|
|
|
|
|
|
// Initialize the stream.
|
2015-07-17 09:19:54 +08:00
|
|
|
PCHContainerRdr.ExtractPCH(New->Buffer->getMemBufferRef(), New->StreamFile);
|
2014-11-08 08:34:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ExpectedSignature) {
|
|
|
|
if (NewModule)
|
|
|
|
ModuleEntry->Signature = ReadSignature(ModuleEntry->StreamFile);
|
|
|
|
else
|
|
|
|
assert(ModuleEntry->Signature == ReadSignature(ModuleEntry->StreamFile));
|
2014-10-24 02:05:36 +08:00
|
|
|
|
2014-11-08 08:34:30 +08:00
|
|
|
if (ModuleEntry->Signature != ExpectedSignature) {
|
|
|
|
ErrorStr = ModuleEntry->Signature ? "signature mismatch"
|
|
|
|
: "could not read module signature";
|
2014-10-24 02:05:36 +08:00
|
|
|
|
2014-11-08 08:34:30 +08:00
|
|
|
if (NewModule) {
|
2014-10-24 02:05:36 +08:00
|
|
|
// Remove the module file immediately, since removeModules might try to
|
|
|
|
// invalidate the file cache for Entry, and that is not safe if this
|
|
|
|
// module is *itself* up to date, but has an out-of-date importer.
|
|
|
|
Modules.erase(Entry);
|
2015-05-20 18:29:23 +08:00
|
|
|
assert(Chain.back() == ModuleEntry);
|
2014-10-24 02:05:36 +08:00
|
|
|
Chain.pop_back();
|
2015-07-22 10:08:40 +08:00
|
|
|
if (!ModuleEntry->isModule())
|
|
|
|
PCHChain.pop_back();
|
2015-05-20 18:29:23 +08:00
|
|
|
if (Roots.back() == ModuleEntry)
|
|
|
|
Roots.pop_back();
|
|
|
|
else
|
|
|
|
assert(ImportedBy);
|
2014-11-08 08:34:30 +08:00
|
|
|
delete ModuleEntry;
|
2014-10-24 02:05:36 +08:00
|
|
|
}
|
2014-11-08 08:34:30 +08:00
|
|
|
return OutOfDate;
|
2014-10-24 02:05:36 +08:00
|
|
|
}
|
2013-03-19 08:28:20 +08:00
|
|
|
}
|
2011-08-26 04:47:51 +08:00
|
|
|
|
|
|
|
if (ImportedBy) {
|
|
|
|
ModuleEntry->ImportedBy.insert(ImportedBy);
|
|
|
|
ImportedBy->Imports.insert(ModuleEntry);
|
|
|
|
} else {
|
2012-12-01 03:28:05 +08:00
|
|
|
if (!ModuleEntry->DirectlyImported)
|
|
|
|
ModuleEntry->ImportLoc = ImportLoc;
|
|
|
|
|
2011-08-26 04:47:51 +08:00
|
|
|
ModuleEntry->DirectlyImported = true;
|
|
|
|
}
|
2013-03-19 08:28:20 +08:00
|
|
|
|
|
|
|
Module = ModuleEntry;
|
|
|
|
return NewModule? NewlyLoaded : AlreadyLoaded;
|
2011-08-26 04:47:51 +08:00
|
|
|
}
|
|
|
|
|
2014-06-20 08:24:56 +08:00
|
|
|
void ModuleManager::removeModules(
|
|
|
|
ModuleIterator first, ModuleIterator last,
|
|
|
|
llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
|
|
|
|
ModuleMap *modMap) {
|
2012-11-08 01:46:15 +08:00
|
|
|
if (first == last)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Collect the set of module file pointers that we'll be removing.
|
|
|
|
llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
|
|
|
|
|
2015-05-20 18:29:23 +08:00
|
|
|
auto IsVictim = [&](ModuleFile *MF) {
|
|
|
|
return victimSet.count(MF);
|
|
|
|
};
|
2012-11-08 01:46:15 +08:00
|
|
|
// Remove any references to the now-destroyed modules.
|
|
|
|
for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
|
2015-05-20 18:29:23 +08:00
|
|
|
Chain[i]->ImportedBy.remove_if(IsVictim);
|
2012-11-08 01:46:15 +08:00
|
|
|
}
|
2015-05-20 18:29:23 +08:00
|
|
|
Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim),
|
|
|
|
Roots.end());
|
2012-11-08 01:46:15 +08:00
|
|
|
|
2015-07-23 06:51:15 +08:00
|
|
|
// Remove the modules from the PCH chain.
|
|
|
|
for (auto I = first; I != last; ++I) {
|
|
|
|
if (!(*I)->isModule()) {
|
|
|
|
PCHChain.erase(std::find(PCHChain.begin(), PCHChain.end(), *I),
|
|
|
|
PCHChain.end());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-08 01:46:15 +08:00
|
|
|
// Delete the modules and erase them from the various structures.
|
|
|
|
for (ModuleIterator victim = first; victim != last; ++victim) {
|
2014-05-20 01:04:28 +08:00
|
|
|
Modules.erase((*victim)->File);
|
2013-03-19 08:28:20 +08:00
|
|
|
|
|
|
|
if (modMap) {
|
2014-04-15 02:00:01 +08:00
|
|
|
StringRef ModuleName = (*victim)->ModuleName;
|
2013-03-19 08:28:20 +08:00
|
|
|
if (Module *mod = modMap->findModule(ModuleName)) {
|
2014-05-22 13:54:18 +08:00
|
|
|
mod->setASTFile(nullptr);
|
2013-03-19 08:28:20 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-31 05:20:54 +08:00
|
|
|
|
2014-06-20 08:24:56 +08:00
|
|
|
// Files that didn't make it through ReadASTCore successfully will be
|
|
|
|
// rebuilt (or there was an error). Invalidate them so that we can load the
|
|
|
|
// new files that will be renamed over the old ones.
|
|
|
|
if (LoadedSuccessfully.count(*victim) == 0)
|
2014-05-31 05:20:54 +08:00
|
|
|
FileMgr.invalidateCache((*victim)->File);
|
|
|
|
|
2012-11-08 01:46:15 +08:00
|
|
|
delete *victim;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the modules from the chain.
|
|
|
|
Chain.erase(first, last);
|
|
|
|
}
|
|
|
|
|
2014-08-19 03:16:31 +08:00
|
|
|
void
|
|
|
|
ModuleManager::addInMemoryBuffer(StringRef FileName,
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buffer) {
|
|
|
|
|
|
|
|
const FileEntry *Entry =
|
|
|
|
FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
|
|
|
|
InMemoryBuffers[Entry] = std::move(Buffer);
|
2011-08-26 04:47:51 +08:00
|
|
|
}
|
|
|
|
|
2013-01-29 00:46:33 +08:00
|
|
|
ModuleManager::VisitState *ModuleManager::allocateVisitState() {
|
|
|
|
// Fast path: if we have a cached state, use it.
|
|
|
|
if (FirstVisitState) {
|
|
|
|
VisitState *Result = FirstVisitState;
|
|
|
|
FirstVisitState = FirstVisitState->NextState;
|
2014-05-22 13:54:18 +08:00
|
|
|
Result->NextState = nullptr;
|
2013-01-29 00:46:33 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate and return a new state.
|
|
|
|
return new VisitState(size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleManager::returnVisitState(VisitState *State) {
|
2014-05-22 13:54:18 +08:00
|
|
|
assert(State->NextState == nullptr && "Visited state is in list?");
|
2013-01-29 00:46:33 +08:00
|
|
|
State->NextState = FirstVisitState;
|
|
|
|
FirstVisitState = State;
|
|
|
|
}
|
|
|
|
|
2013-01-26 07:32:03 +08:00
|
|
|
void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
|
|
|
|
GlobalIndex = Index;
|
2013-03-23 02:50:14 +08:00
|
|
|
if (!GlobalIndex) {
|
|
|
|
ModulesInCommonWithGlobalIndex.clear();
|
|
|
|
return;
|
2013-03-19 08:28:20 +08:00
|
|
|
}
|
2013-03-23 02:50:14 +08:00
|
|
|
|
|
|
|
// Notify the global module index about all of the modules we've already
|
|
|
|
// loaded.
|
|
|
|
for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
|
|
|
|
if (!GlobalIndex->loadedModuleFile(Chain[I])) {
|
|
|
|
ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
|
|
|
|
if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ModulesInCommonWithGlobalIndex.push_back(MF);
|
2013-01-26 07:32:03 +08:00
|
|
|
}
|
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
ModuleManager::ModuleManager(FileManager &FileMgr,
|
2015-07-17 09:19:54 +08:00
|
|
|
const PCHContainerReader &PCHContainerRdr)
|
|
|
|
: FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr), GlobalIndex(),
|
2015-06-21 02:53:08 +08:00
|
|
|
FirstVisitState(nullptr) {}
|
2011-08-26 04:47:51 +08:00
|
|
|
|
|
|
|
ModuleManager::~ModuleManager() {
|
|
|
|
for (unsigned i = 0, e = Chain.size(); i != e; ++i)
|
|
|
|
delete Chain[e - i - 1];
|
2013-01-29 00:46:33 +08:00
|
|
|
delete FirstVisitState;
|
2011-08-26 04:47:51 +08:00
|
|
|
}
|
|
|
|
|
2015-07-25 20:14:04 +08:00
|
|
|
void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
|
|
|
|
llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
|
2013-01-26 07:32:03 +08:00
|
|
|
// If the visitation order vector is the wrong size, recompute the order.
|
2013-01-26 06:25:23 +08:00
|
|
|
if (VisitOrder.size() != Chain.size()) {
|
|
|
|
unsigned N = size();
|
|
|
|
VisitOrder.clear();
|
|
|
|
VisitOrder.reserve(N);
|
|
|
|
|
|
|
|
// Record the number of incoming edges for each module. When we
|
|
|
|
// encounter a module with no incoming edges, push it into the queue
|
|
|
|
// to seed the queue.
|
|
|
|
SmallVector<ModuleFile *, 4> Queue;
|
|
|
|
Queue.reserve(N);
|
|
|
|
llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
|
2015-07-22 09:28:05 +08:00
|
|
|
UnusedIncomingEdges.resize(size());
|
|
|
|
for (auto M = rbegin(), MEnd = rend(); M != MEnd; ++M) {
|
|
|
|
unsigned Size = (*M)->ImportedBy.size();
|
|
|
|
UnusedIncomingEdges[(*M)->Index] = Size;
|
|
|
|
if (!Size)
|
2013-01-26 06:25:23 +08:00
|
|
|
Queue.push_back(*M);
|
2013-01-22 04:07:12 +08:00
|
|
|
}
|
2013-01-26 06:25:23 +08:00
|
|
|
|
|
|
|
// Traverse the graph, making sure to visit a module before visiting any
|
|
|
|
// of its dependencies.
|
2015-07-22 09:28:05 +08:00
|
|
|
while (!Queue.empty()) {
|
|
|
|
ModuleFile *CurrentModule = Queue.pop_back_val();
|
2013-01-26 06:25:23 +08:00
|
|
|
VisitOrder.push_back(CurrentModule);
|
|
|
|
|
|
|
|
// For any module that this module depends on, push it on the
|
|
|
|
// stack (if it hasn't already been marked as visited).
|
2015-07-22 09:28:05 +08:00
|
|
|
for (auto M = CurrentModule->Imports.rbegin(),
|
|
|
|
MEnd = CurrentModule->Imports.rend();
|
2013-01-26 06:25:23 +08:00
|
|
|
M != MEnd; ++M) {
|
|
|
|
// Remove our current module as an impediment to visiting the
|
|
|
|
// module we depend on. If we were the last unvisited module
|
|
|
|
// that depends on this particular module, push it into the
|
|
|
|
// queue to be visited.
|
|
|
|
unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
|
|
|
|
if (NumUnusedEdges && (--NumUnusedEdges == 0))
|
|
|
|
Queue.push_back(*M);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(VisitOrder.size() == N && "Visitation order is wrong?");
|
2013-01-26 07:32:03 +08:00
|
|
|
|
2013-01-29 00:46:33 +08:00
|
|
|
delete FirstVisitState;
|
2014-05-22 13:54:18 +08:00
|
|
|
FirstVisitState = nullptr;
|
2011-08-26 04:47:51 +08:00
|
|
|
}
|
2013-01-22 04:07:12 +08:00
|
|
|
|
2013-01-29 00:46:33 +08:00
|
|
|
VisitState *State = allocateVisitState();
|
|
|
|
unsigned VisitNumber = State->NextVisitNumber++;
|
2013-01-26 06:25:23 +08:00
|
|
|
|
2013-01-26 07:32:03 +08:00
|
|
|
// If the caller has provided us with a hit-set that came from the global
|
|
|
|
// module index, mark every module file in common with the global module
|
|
|
|
// index that is *not* in that set as 'visited'.
|
|
|
|
if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
|
|
|
|
for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
|
|
|
|
{
|
|
|
|
ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
|
2013-03-19 08:28:20 +08:00
|
|
|
if (!ModuleFilesHit->count(M))
|
2013-01-29 00:46:33 +08:00
|
|
|
State->VisitNumber[M->Index] = VisitNumber;
|
2013-01-26 07:32:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-26 06:25:23 +08:00
|
|
|
for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
|
|
|
|
ModuleFile *CurrentModule = VisitOrder[I];
|
|
|
|
// Should we skip this module file?
|
2013-01-29 00:46:33 +08:00
|
|
|
if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
|
2011-08-26 04:47:51 +08:00
|
|
|
continue;
|
2013-01-26 06:25:23 +08:00
|
|
|
|
|
|
|
// Visit the module.
|
2013-01-29 00:46:33 +08:00
|
|
|
assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
|
|
|
|
State->VisitNumber[CurrentModule->Index] = VisitNumber;
|
2015-07-25 20:14:04 +08:00
|
|
|
if (!Visitor(*CurrentModule))
|
2013-01-26 06:25:23 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// The visitor has requested that cut off visitation of any
|
|
|
|
// module that the current module depends on. To indicate this
|
|
|
|
// behavior, we mark all of the reachable modules as having been visited.
|
|
|
|
ModuleFile *NextModule = CurrentModule;
|
|
|
|
do {
|
|
|
|
// For any module that this module depends on, push it on the
|
|
|
|
// stack (if it hasn't already been marked as visited).
|
|
|
|
for (llvm::SetVector<ModuleFile *>::iterator
|
|
|
|
M = NextModule->Imports.begin(),
|
|
|
|
MEnd = NextModule->Imports.end();
|
|
|
|
M != MEnd; ++M) {
|
2013-01-29 00:46:33 +08:00
|
|
|
if (State->VisitNumber[(*M)->Index] != VisitNumber) {
|
|
|
|
State->Stack.push_back(*M);
|
|
|
|
State->VisitNumber[(*M)->Index] = VisitNumber;
|
2011-08-26 04:47:51 +08:00
|
|
|
}
|
|
|
|
}
|
2013-01-26 06:25:23 +08:00
|
|
|
|
2013-01-29 00:46:33 +08:00
|
|
|
if (State->Stack.empty())
|
2013-01-26 06:25:23 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Pop the next module off the stack.
|
2013-08-24 00:11:15 +08:00
|
|
|
NextModule = State->Stack.pop_back_val();
|
2013-01-26 06:25:23 +08:00
|
|
|
} while (true);
|
2011-08-26 04:47:51 +08:00
|
|
|
}
|
2013-01-29 00:46:33 +08:00
|
|
|
|
|
|
|
returnVisitState(State);
|
2011-08-26 04:47:51 +08:00
|
|
|
}
|
|
|
|
|
2013-03-19 08:28:20 +08:00
|
|
|
bool ModuleManager::lookupModuleFile(StringRef FileName,
|
|
|
|
off_t ExpectedSize,
|
|
|
|
time_t ExpectedModTime,
|
|
|
|
const FileEntry *&File) {
|
2014-05-01 11:33:36 +08:00
|
|
|
// Open the file immediately to ensure there is no race between stat'ing and
|
|
|
|
// opening the file.
|
|
|
|
File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
|
2013-03-19 08:28:20 +08:00
|
|
|
|
|
|
|
if (!File && FileName != "-") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ExpectedSize && ExpectedSize != File->getSize()) ||
|
2014-05-04 13:20:54 +08:00
|
|
|
(ExpectedModTime && ExpectedModTime != File->getModificationTime()))
|
|
|
|
// Do not destroy File, as it may be referenced. If we need to rebuild it,
|
|
|
|
// it will be destroyed by removeModules.
|
2013-03-19 08:28:20 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-12 03:27:55 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
namespace llvm {
|
|
|
|
template<>
|
|
|
|
struct GraphTraits<ModuleManager> {
|
2011-12-01 07:21:26 +08:00
|
|
|
typedef ModuleFile NodeType;
|
|
|
|
typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
|
2011-10-12 03:27:55 +08:00
|
|
|
typedef ModuleManager::ModuleConstIterator nodes_iterator;
|
|
|
|
|
|
|
|
static ChildIteratorType child_begin(NodeType *Node) {
|
|
|
|
return Node->Imports.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
static ChildIteratorType child_end(NodeType *Node) {
|
|
|
|
return Node->Imports.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
static nodes_iterator nodes_begin(const ModuleManager &Manager) {
|
|
|
|
return Manager.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
static nodes_iterator nodes_end(const ModuleManager &Manager) {
|
|
|
|
return Manager.end();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
|
|
|
|
explicit DOTGraphTraits(bool IsSimple = false)
|
|
|
|
: DefaultDOTGraphTraits(IsSimple) { }
|
|
|
|
|
|
|
|
static bool renderGraphFromBottomUp() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-12-01 07:21:26 +08:00
|
|
|
std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
|
2014-04-15 02:00:01 +08:00
|
|
|
return M->ModuleName;
|
2011-10-12 03:27:55 +08:00
|
|
|
}
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2011-10-12 03:27:55 +08:00
|
|
|
|
|
|
|
void ModuleManager::viewGraph() {
|
|
|
|
llvm::ViewGraph(*this, "Modules");
|
|
|
|
}
|
|
|
|
#endif
|