2014-06-20 03:36:03 +08:00
|
|
|
//===--- ModuleDependencyCollector.cpp - Collect module dependencies ------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Collect the dependencies of a set of modules.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-04-07 09:12:18 +08:00
|
|
|
#include "clang/Basic/CharInfo.h"
|
2014-06-20 03:36:03 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2016-03-31 07:54:25 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2014-06-20 03:36:03 +08:00
|
|
|
#include "clang/Serialization/ASTReader.h"
|
2015-01-14 19:29:14 +08:00
|
|
|
#include "llvm/ADT/iterator_range.h"
|
2018-04-30 21:52:15 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2014-06-20 03:49:28 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2014-06-20 03:36:03 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
2016-03-31 07:54:25 +08:00
|
|
|
/// Private implementations for ModuleDependencyCollector
|
2014-06-20 03:36:03 +08:00
|
|
|
class ModuleDependencyListener : public ASTReaderListener {
|
|
|
|
ModuleDependencyCollector &Collector;
|
|
|
|
public:
|
|
|
|
ModuleDependencyListener(ModuleDependencyCollector &Collector)
|
|
|
|
: Collector(Collector) {}
|
|
|
|
bool needsInputFileVisitation() override { return true; }
|
|
|
|
bool needsSystemInputFileVisitation() override { return true; }
|
2015-08-14 01:57:10 +08:00
|
|
|
bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden,
|
2016-03-30 07:47:40 +08:00
|
|
|
bool IsExplicitModule) override {
|
|
|
|
Collector.addFile(Filename);
|
|
|
|
return true;
|
|
|
|
}
|
2014-06-20 03:36:03 +08:00
|
|
|
};
|
2016-03-31 07:54:25 +08:00
|
|
|
|
2016-12-11 12:27:31 +08:00
|
|
|
struct ModuleDependencyPPCallbacks : public PPCallbacks {
|
|
|
|
ModuleDependencyCollector &Collector;
|
|
|
|
SourceManager &SM;
|
|
|
|
ModuleDependencyPPCallbacks(ModuleDependencyCollector &Collector,
|
|
|
|
SourceManager &SM)
|
|
|
|
: Collector(Collector), SM(SM) {}
|
|
|
|
|
|
|
|
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
|
|
|
|
StringRef FileName, bool IsAngled,
|
|
|
|
CharSourceRange FilenameRange, const FileEntry *File,
|
|
|
|
StringRef SearchPath, StringRef RelativePath,
|
2018-05-11 03:05:36 +08:00
|
|
|
const Module *Imported,
|
|
|
|
SrcMgr::CharacteristicKind FileType) override {
|
2016-12-11 12:27:31 +08:00
|
|
|
if (!File)
|
|
|
|
return;
|
|
|
|
Collector.addFile(File->getName());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-31 07:54:25 +08:00
|
|
|
struct ModuleDependencyMMCallbacks : public ModuleMapCallbacks {
|
|
|
|
ModuleDependencyCollector &Collector;
|
|
|
|
ModuleDependencyMMCallbacks(ModuleDependencyCollector &Collector)
|
|
|
|
: Collector(Collector) {}
|
|
|
|
|
2016-05-07 07:21:50 +08:00
|
|
|
void moduleMapAddHeader(StringRef HeaderPath) override {
|
2016-03-31 07:54:25 +08:00
|
|
|
if (llvm::sys::path::is_absolute(HeaderPath))
|
|
|
|
Collector.addFile(HeaderPath);
|
|
|
|
}
|
2016-05-14 06:21:51 +08:00
|
|
|
void moduleMapAddUmbrellaHeader(FileManager *FileMgr,
|
|
|
|
const FileEntry *Header) override {
|
|
|
|
StringRef HeaderFilename = Header->getName();
|
|
|
|
moduleMapAddHeader(HeaderFilename);
|
|
|
|
// The FileManager can find and cache the symbolic link for a framework
|
|
|
|
// header before its real path, this means a module can have some of its
|
|
|
|
// headers to use other paths. Although this is usually not a problem, it's
|
|
|
|
// inconsistent, and not collecting the original path header leads to
|
|
|
|
// umbrella clashes while rebuilding modules in the crash reproducer. For
|
|
|
|
// example:
|
|
|
|
// ApplicationServices.framework/Frameworks/ImageIO.framework/ImageIO.h
|
|
|
|
// instead of:
|
|
|
|
// ImageIO.framework/ImageIO.h
|
|
|
|
//
|
|
|
|
// FIXME: this shouldn't be necessary once we have FileName instances
|
|
|
|
// around instead of FileEntry ones. For now, make sure we collect all
|
|
|
|
// that we need for the reproducer to work correctly.
|
|
|
|
StringRef UmbreallDirFromHeader =
|
|
|
|
llvm::sys::path::parent_path(HeaderFilename);
|
|
|
|
StringRef UmbrellaDir = Header->getDir()->getName();
|
|
|
|
if (!UmbrellaDir.equals(UmbreallDirFromHeader)) {
|
|
|
|
SmallString<128> AltHeaderFilename;
|
|
|
|
llvm::sys::path::append(AltHeaderFilename, UmbrellaDir,
|
|
|
|
llvm::sys::path::filename(HeaderFilename));
|
|
|
|
if (FileMgr->getFile(AltHeaderFilename))
|
|
|
|
moduleMapAddHeader(AltHeaderFilename);
|
|
|
|
}
|
|
|
|
}
|
2016-03-31 07:54:25 +08:00
|
|
|
};
|
|
|
|
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2014-06-20 03:36:03 +08:00
|
|
|
|
2018-04-28 04:29:57 +08:00
|
|
|
// TODO: move this to Support/Path.h and check for HAVE_REALPATH?
|
|
|
|
static bool real_path(StringRef SrcPath, SmallVectorImpl<char> &RealPath) {
|
|
|
|
#ifdef LLVM_ON_UNIX
|
|
|
|
char CanonicalPath[PATH_MAX];
|
|
|
|
|
|
|
|
// TODO: emit a warning in case this fails...?
|
|
|
|
if (!realpath(SrcPath.str().c_str(), CanonicalPath))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SmallString<256> RPath(CanonicalPath);
|
|
|
|
RealPath.swap(RPath);
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
// FIXME: Add support for systems without realpath.
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-06-20 03:36:03 +08:00
|
|
|
void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
|
2014-08-11 00:54:39 +08:00
|
|
|
R.addListener(llvm::make_unique<ModuleDependencyListener>(*this));
|
2014-06-20 03:36:03 +08:00
|
|
|
}
|
|
|
|
|
2016-03-31 07:54:25 +08:00
|
|
|
void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) {
|
2016-12-11 12:27:31 +08:00
|
|
|
PP.addPPCallbacks(llvm::make_unique<ModuleDependencyPPCallbacks>(
|
|
|
|
*this, PP.getSourceManager()));
|
2016-03-31 07:54:25 +08:00
|
|
|
PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
|
|
|
|
llvm::make_unique<ModuleDependencyMMCallbacks>(*this));
|
|
|
|
}
|
|
|
|
|
2016-04-07 08:00:57 +08:00
|
|
|
static bool isCaseSensitivePath(StringRef Path) {
|
2016-04-07 09:58:14 +08:00
|
|
|
SmallString<256> TmpDest = Path, UpperDest, RealDest;
|
2016-04-07 08:00:57 +08:00
|
|
|
// Remove component traversals, links, etc.
|
2018-04-28 04:29:57 +08:00
|
|
|
if (!real_path(Path, TmpDest))
|
2016-04-07 08:00:57 +08:00
|
|
|
return true; // Current default value in vfs.yaml
|
|
|
|
Path = TmpDest;
|
|
|
|
|
|
|
|
// Change path to all upper case and ask for its real path, if the latter
|
|
|
|
// exists and is equal to Path, it's not case sensitive. Default to case
|
2018-04-06 23:14:32 +08:00
|
|
|
// sensitive in the absence of realpath, since this is what the VFSWriter
|
2016-04-07 08:00:57 +08:00
|
|
|
// already expects when sensitivity isn't setup.
|
|
|
|
for (auto &C : Path)
|
2016-04-07 09:12:18 +08:00
|
|
|
UpperDest.push_back(toUppercase(C));
|
2018-04-28 04:29:57 +08:00
|
|
|
if (real_path(UpperDest, RealDest) && Path.equals(RealDest))
|
2016-04-07 08:00:57 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-20 03:36:03 +08:00
|
|
|
void ModuleDependencyCollector::writeFileMap() {
|
|
|
|
if (Seen.empty())
|
|
|
|
return;
|
|
|
|
|
2016-04-07 08:00:57 +08:00
|
|
|
StringRef VFSDir = getDest();
|
2014-06-20 03:36:03 +08:00
|
|
|
|
Reapply [2] [VFS] Add 'overlay-relative' field to YAML files
This reapplies r261552 and r263748. Fixed testcase to reapply.
The VFS overlay mapping between virtual paths and real paths is done through
the 'external-contents' entries in YAML files, which contains hardcoded paths
to the real files.
When a module compilation crashes, headers are dumped into <name>.cache/vfs
directory and are mapped via the <name>.cache/vfs/vfs.yaml. The script
generated for reproduction uses -ivfsoverlay pointing to file to gather the
mapping between virtual paths and files inside <name>.cache/vfs. Currently, we
are only capable of reproducing such crashes in the same machine as they
happen, because of the hardcoded paths in 'external-contents'.
To be able to reproduce a crash in another machine, this patch introduces a new
option in the VFS yaml file called 'overlay-relative'. When it's equal to
'true' it means that the provided path to the YAML file through the
-ivfsoverlay option should also be used to prefix the final path for every
'external-contents'.
Example, given the invocation snippet "... -ivfsoverlay
<name>.cache/vfs/vfs.yaml" and the following entry in the yaml file:
"overlay-relative": "true",
"roots": [
...
"type": "directory",
"name": "/usr/include",
"contents": [
{
"type": "file",
"name": "stdio.h",
"external-contents": "/usr/include/stdio.h"
},
...
Here, a file manager request for virtual "/usr/include/stdio.h", that will map
into real path "/<absolute_path_to>/<name>.cache/vfs/usr/include/stdio.h.
This is a useful feature for debugging module crashes in machines other than
the one where the error happened.
Differential Revision: http://reviews.llvm.org/D17457
rdar://problem/24499339
llvm-svn: 263893
2016-03-20 10:08:48 +08:00
|
|
|
// Default to use relative overlay directories in the VFS yaml file. This
|
|
|
|
// allows crash reproducer scripts to work across machines.
|
2016-04-07 08:00:57 +08:00
|
|
|
VFSWriter.setOverlayDir(VFSDir);
|
|
|
|
|
|
|
|
// Explicitly set case sensitivity for the YAML writer. For that, find out
|
|
|
|
// the sensitivity at the path where the headers all collected to.
|
|
|
|
VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir));
|
Reapply [2] [VFS] Add 'overlay-relative' field to YAML files
This reapplies r261552 and r263748. Fixed testcase to reapply.
The VFS overlay mapping between virtual paths and real paths is done through
the 'external-contents' entries in YAML files, which contains hardcoded paths
to the real files.
When a module compilation crashes, headers are dumped into <name>.cache/vfs
directory and are mapped via the <name>.cache/vfs/vfs.yaml. The script
generated for reproduction uses -ivfsoverlay pointing to file to gather the
mapping between virtual paths and files inside <name>.cache/vfs. Currently, we
are only capable of reproducing such crashes in the same machine as they
happen, because of the hardcoded paths in 'external-contents'.
To be able to reproduce a crash in another machine, this patch introduces a new
option in the VFS yaml file called 'overlay-relative'. When it's equal to
'true' it means that the provided path to the YAML file through the
-ivfsoverlay option should also be used to prefix the final path for every
'external-contents'.
Example, given the invocation snippet "... -ivfsoverlay
<name>.cache/vfs/vfs.yaml" and the following entry in the yaml file:
"overlay-relative": "true",
"roots": [
...
"type": "directory",
"name": "/usr/include",
"contents": [
{
"type": "file",
"name": "stdio.h",
"external-contents": "/usr/include/stdio.h"
},
...
Here, a file manager request for virtual "/usr/include/stdio.h", that will map
into real path "/<absolute_path_to>/<name>.cache/vfs/usr/include/stdio.h.
This is a useful feature for debugging module crashes in machines other than
the one where the error happened.
Differential Revision: http://reviews.llvm.org/D17457
rdar://problem/24499339
llvm-svn: 263893
2016-03-20 10:08:48 +08:00
|
|
|
|
2016-04-14 03:28:21 +08:00
|
|
|
// Do not rely on real path names when executing the crash reproducer scripts
|
|
|
|
// since we only want to actually use the files we have on the VFS cache.
|
|
|
|
VFSWriter.setUseExternalNames(false);
|
|
|
|
|
2014-08-26 02:17:04 +08:00
|
|
|
std::error_code EC;
|
2016-04-07 08:00:57 +08:00
|
|
|
SmallString<256> YAMLPath = VFSDir;
|
|
|
|
llvm::sys::path::append(YAMLPath, "vfs.yaml");
|
|
|
|
llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::F_Text);
|
2014-08-26 02:17:04 +08:00
|
|
|
if (EC) {
|
2016-03-30 07:47:40 +08:00
|
|
|
HasErrors = true;
|
2014-06-20 03:36:03 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
VFSWriter.write(OS);
|
|
|
|
}
|
|
|
|
|
2016-03-30 07:47:40 +08:00
|
|
|
bool ModuleDependencyCollector::getRealPath(StringRef SrcPath,
|
|
|
|
SmallVectorImpl<char> &Result) {
|
2016-03-17 10:20:43 +08:00
|
|
|
using namespace llvm::sys;
|
|
|
|
SmallString<256> RealPath;
|
|
|
|
StringRef FileName = path::filename(SrcPath);
|
|
|
|
std::string Dir = path::parent_path(SrcPath).str();
|
|
|
|
auto DirWithSymLink = SymLinkMap.find(Dir);
|
|
|
|
|
|
|
|
// Use real_path to fix any symbolic link component present in a path.
|
|
|
|
// Computing the real path is expensive, cache the search through the
|
|
|
|
// parent path directory.
|
|
|
|
if (DirWithSymLink == SymLinkMap.end()) {
|
2018-04-28 04:29:57 +08:00
|
|
|
if (!real_path(Dir, RealPath))
|
2016-03-17 10:20:43 +08:00
|
|
|
return false;
|
|
|
|
SymLinkMap[Dir] = RealPath.str();
|
|
|
|
} else {
|
|
|
|
RealPath = DirWithSymLink->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
path::append(RealPath, FileName);
|
|
|
|
Result.swap(RealPath);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:06:03 +08:00
|
|
|
std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src,
|
|
|
|
StringRef Dst) {
|
2014-06-20 03:36:03 +08:00
|
|
|
using namespace llvm::sys;
|
|
|
|
|
2016-05-07 07:58:58 +08:00
|
|
|
// We need an absolute src path to append to the root.
|
2014-06-20 03:36:03 +08:00
|
|
|
SmallString<256> AbsoluteSrc = Src;
|
|
|
|
fs::make_absolute(AbsoluteSrc);
|
2016-05-07 07:58:58 +08:00
|
|
|
// Canonicalize src to a native path to avoid mixed separator styles.
|
2014-12-13 07:12:27 +08:00
|
|
|
path::native(AbsoluteSrc);
|
2016-03-17 10:20:43 +08:00
|
|
|
// Remove redundant leading "./" pieces and consecutive separators.
|
|
|
|
AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
|
|
|
|
|
2016-05-07 07:58:58 +08:00
|
|
|
// Canonicalize the source path by removing "..", "." components.
|
2016-12-22 15:06:03 +08:00
|
|
|
SmallString<256> VirtualPath = AbsoluteSrc;
|
|
|
|
path::remove_dots(VirtualPath, /*remove_dot_dot=*/true);
|
2016-03-17 10:20:43 +08:00
|
|
|
|
|
|
|
// If a ".." component is present after a symlink component, remove_dots may
|
|
|
|
// lead to the wrong real destination path. Let the source be canonicalized
|
2016-05-07 07:58:58 +08:00
|
|
|
// like that but make sure we always use the real path for the destination.
|
2016-12-22 15:06:03 +08:00
|
|
|
SmallString<256> CopyFrom;
|
|
|
|
if (!getRealPath(AbsoluteSrc, CopyFrom))
|
|
|
|
CopyFrom = VirtualPath;
|
|
|
|
SmallString<256> CacheDst = getDest();
|
|
|
|
|
|
|
|
if (Dst.empty()) {
|
|
|
|
// The common case is to map the virtual path to the same path inside the
|
|
|
|
// cache.
|
|
|
|
path::append(CacheDst, path::relative_path(CopyFrom));
|
|
|
|
} else {
|
|
|
|
// When collecting entries from input vfsoverlays, copy the external
|
|
|
|
// contents into the cache but still map from the source.
|
|
|
|
if (!fs::exists(Dst))
|
|
|
|
return std::error_code();
|
|
|
|
path::append(CacheDst, Dst);
|
|
|
|
CopyFrom = Dst;
|
|
|
|
}
|
2014-06-20 03:36:03 +08:00
|
|
|
|
|
|
|
// Copy the file into place.
|
2016-12-22 15:06:03 +08:00
|
|
|
if (std::error_code EC = fs::create_directories(path::parent_path(CacheDst),
|
|
|
|
/*IgnoreExisting=*/true))
|
2014-06-20 03:36:03 +08:00
|
|
|
return EC;
|
2016-12-22 15:06:03 +08:00
|
|
|
if (std::error_code EC = fs::copy_file(CopyFrom, CacheDst))
|
2014-06-20 03:36:03 +08:00
|
|
|
return EC;
|
2016-03-17 10:20:43 +08:00
|
|
|
|
2016-05-07 07:58:58 +08:00
|
|
|
// Always map a canonical src path to its real path into the YAML, by doing
|
|
|
|
// this we map different virtual src paths to the same entry in the VFS
|
|
|
|
// overlay, which is a way to emulate symlink inside the VFS; this is also
|
2017-07-01 16:46:43 +08:00
|
|
|
// needed for correctness, not doing that can lead to module redefinition
|
2016-05-07 07:58:58 +08:00
|
|
|
// errors.
|
2016-12-22 15:06:03 +08:00
|
|
|
addFileMapping(VirtualPath, CacheDst);
|
2014-06-20 03:36:03 +08:00
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:06:03 +08:00
|
|
|
void ModuleDependencyCollector::addFile(StringRef Filename, StringRef FileDst) {
|
2016-03-30 07:47:40 +08:00
|
|
|
if (insertSeen(Filename))
|
2016-12-22 15:06:03 +08:00
|
|
|
if (copyToRoot(Filename, FileDst))
|
2016-03-30 07:47:40 +08:00
|
|
|
HasErrors = true;
|
2014-06-20 03:36:03 +08:00
|
|
|
}
|