blob: 8e4402b8eda736a5a5f35f3100cfec193352c3e3 [file] [log] [blame]
Justin Bogner86d12592014-06-19 19:36:03 +00001//===--- ModuleDependencyCollector.cpp - Collect module dependencies ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Collect the dependencies of a set of modules.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/Utils.h"
15#include "clang/Serialization/ASTReader.h"
16#include "llvm/ADT/iterator_range.h"
17#include "llvm/ADT/StringSet.h"
18#include "llvm/Support/Filesystem.h"
19#include "llvm/Support/Path.h"
20#include "llvm/Support/raw_ostream.h"
21
22using namespace clang;
23
24namespace {
25/// Private implementation for ModuleDependencyCollector
26class ModuleDependencyListener : public ASTReaderListener {
27 ModuleDependencyCollector &Collector;
28
29 std::error_code copyToRoot(StringRef Src);
30public:
31 ModuleDependencyListener(ModuleDependencyCollector &Collector)
32 : Collector(Collector) {}
33 bool needsInputFileVisitation() override { return true; }
34 bool needsSystemInputFileVisitation() override { return true; }
35 bool visitInputFile(StringRef Filename, bool IsSystem,
36 bool IsOverridden) override;
37};
38}
39
40void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
41 R.addListener(new ModuleDependencyListener(*this));
42}
43
44void ModuleDependencyCollector::writeFileMap() {
45 if (Seen.empty())
46 return;
47
48 SmallString<256> Dest = getDest();
49 llvm::sys::path::append(Dest, "vfs.yaml");
50
51 std::string ErrorInfo;
52 llvm::raw_fd_ostream OS(Dest.c_str(), ErrorInfo, llvm::sys::fs::F_Text);
53 if (!ErrorInfo.empty()) {
54 setHasErrors();
55 return;
56 }
57 VFSWriter.write(OS);
58}
59
60/// Append the absolute path in Nested to the path given by Root. This will
61/// remove directory traversal from the resulting nested path.
62static void appendNestedPath(SmallVectorImpl<char> &Root, StringRef Nested) {
63 using namespace llvm::sys;
64 SmallVector<StringRef, 16> ComponentStack;
65
66 StringRef Rel = path::relative_path(Nested);
67 for (StringRef C : llvm::make_range(path::begin(Rel), path::end(Rel))) {
68 if (C == ".")
69 continue;
70 if (C == "..") {
71 assert(ComponentStack.size() && "Path traverses out of parent");
72 ComponentStack.pop_back();
73 } else
74 ComponentStack.push_back(C);
75 }
76 // The stack is now the path without any directory traversal.
77 for (StringRef C : ComponentStack)
78 path::append(Root, C);
79}
80
81std::error_code ModuleDependencyListener::copyToRoot(StringRef Src) {
82 using namespace llvm::sys;
83
84 // We need an absolute path to append to the root.
85 SmallString<256> AbsoluteSrc = Src;
86 fs::make_absolute(AbsoluteSrc);
87 // Build the destination path.
88 SmallString<256> Dest = Collector.getDest();
89 size_t RootLen = Dest.size();
90 appendNestedPath(Dest, AbsoluteSrc);
91
92 // Copy the file into place.
93 if (std::error_code EC = fs::create_directories(path::parent_path(Dest),
94 /*IgnoreExisting=*/true))
95 return EC;
96 if (std::error_code EC = fs::copy_file(AbsoluteSrc.str(), Dest.str()))
97 return EC;
98 // Use the absolute path under the root for the file mapping.
99 Collector.addFileMapping(Dest.substr(RootLen), Dest.str());
100 return std::error_code();
101}
102
103bool ModuleDependencyListener::visitInputFile(StringRef Filename, bool IsSystem,
104 bool IsOverridden) {
105 if (Collector.insertSeen(Filename))
106 if (copyToRoot(Filename))
107 Collector.setHasErrors();
108 return true;
109}