summaryrefslogtreecommitdiff
path: root/lib/Frontend/ModuleDependencyCollector.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2017-01-02 19:18:08 +0000
committerDimitry Andric <dim@FreeBSD.org>2017-01-02 19:18:08 +0000
commitbab175ec4b075c8076ba14c762900392533f6ee4 (patch)
tree01f4f29419a2cb10abe13c1e63cd2a66068b0137 /lib/Frontend/ModuleDependencyCollector.cpp
parent8b7a8012d223fac5d17d16a66bb39168a9a1dfc0 (diff)
Notes
Diffstat (limited to 'lib/Frontend/ModuleDependencyCollector.cpp')
-rw-r--r--lib/Frontend/ModuleDependencyCollector.cpp66
1 files changed, 51 insertions, 15 deletions
diff --git a/lib/Frontend/ModuleDependencyCollector.cpp b/lib/Frontend/ModuleDependencyCollector.cpp
index ca11f9b863bb9..9b34d42113532 100644
--- a/lib/Frontend/ModuleDependencyCollector.cpp
+++ b/lib/Frontend/ModuleDependencyCollector.cpp
@@ -15,7 +15,6 @@
#include "clang/Frontend/Utils.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Serialization/ASTReader.h"
-#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
@@ -39,6 +38,24 @@ public:
}
};
+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,
+ const Module *Imported) override {
+ if (!File)
+ return;
+ Collector.addFile(File->getName());
+ }
+};
+
struct ModuleDependencyMMCallbacks : public ModuleMapCallbacks {
ModuleDependencyCollector &Collector;
ModuleDependencyMMCallbacks(ModuleDependencyCollector &Collector)
@@ -103,6 +120,8 @@ void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
}
void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) {
+ PP.addPPCallbacks(llvm::make_unique<ModuleDependencyPPCallbacks>(
+ *this, PP.getSourceManager()));
PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
llvm::make_unique<ModuleDependencyMMCallbacks>(*this));
}
@@ -135,6 +154,10 @@ void ModuleDependencyCollector::writeFileMap() {
// allows crash reproducer scripts to work across machines.
VFSWriter.setOverlayDir(VFSDir);
+ // Do not ignore non existent contents otherwise we might skip something
+ // that should have been collected here.
+ VFSWriter.setIgnoreNonExistentContents(false);
+
// 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));
@@ -178,7 +201,8 @@ bool ModuleDependencyCollector::getRealPath(StringRef SrcPath,
return true;
}
-std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src) {
+std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src,
+ StringRef Dst) {
using namespace llvm::sys;
// We need an absolute src path to append to the root.
@@ -190,23 +214,35 @@ std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src) {
AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
// Canonicalize the source path by removing "..", "." components.
- SmallString<256> CanonicalPath = AbsoluteSrc;
- path::remove_dots(CanonicalPath, /*remove_dot_dot=*/true);
+ SmallString<256> VirtualPath = AbsoluteSrc;
+ path::remove_dots(VirtualPath, /*remove_dot_dot=*/true);
// If a ".." component is present after a symlink component, remove_dots may
// lead to the wrong real destination path. Let the source be canonicalized
// like that but make sure we always use the real path for the destination.
- SmallString<256> RealPath;
- if (!getRealPath(AbsoluteSrc, RealPath))
- RealPath = CanonicalPath;
- SmallString<256> Dest = getDest();
- path::append(Dest, path::relative_path(RealPath));
+ 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;
+ }
// Copy the file into place.
- if (std::error_code EC = fs::create_directories(path::parent_path(Dest),
- /*IgnoreExisting=*/true))
+ if (std::error_code EC = fs::create_directories(path::parent_path(CacheDst),
+ /*IgnoreExisting=*/true))
return EC;
- if (std::error_code EC = fs::copy_file(RealPath, Dest))
+ if (std::error_code EC = fs::copy_file(CopyFrom, CacheDst))
return EC;
// Always map a canonical src path to its real path into the YAML, by doing
@@ -214,12 +250,12 @@ std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src) {
// overlay, which is a way to emulate symlink inside the VFS; this is also
// needed for correctness, not doing that can lead to module redifinition
// errors.
- addFileMapping(CanonicalPath, Dest);
+ addFileMapping(VirtualPath, CacheDst);
return std::error_code();
}
-void ModuleDependencyCollector::addFile(StringRef Filename) {
+void ModuleDependencyCollector::addFile(StringRef Filename, StringRef FileDst) {
if (insertSeen(Filename))
- if (copyToRoot(Filename))
+ if (copyToRoot(Filename, FileDst))
HasErrors = true;
}