1 //===--- ModuleDependencyCollector.cpp - Collect module dependencies ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Collect the dependencies of a set of modules.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Basic/CharInfo.h"
15 #include "clang/Frontend/Utils.h"
16 #include "clang/Lex/Preprocessor.h"
17 #include "clang/Serialization/ASTReader.h"
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/Config/llvm-config.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Support/raw_ostream.h"
24 using namespace clang;
27 /// Private implementations for ModuleDependencyCollector
28 class ModuleDependencyListener : public ASTReaderListener {
29 ModuleDependencyCollector &Collector;
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, bool IsOverridden,
36 bool IsExplicitModule) override {
37 Collector.addFile(Filename);
42 struct ModuleDependencyPPCallbacks : public PPCallbacks {
43 ModuleDependencyCollector &Collector;
45 ModuleDependencyPPCallbacks(ModuleDependencyCollector &Collector,
47 : Collector(Collector), SM(SM) {}
49 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
50 StringRef FileName, bool IsAngled,
51 CharSourceRange FilenameRange, const FileEntry *File,
52 StringRef SearchPath, StringRef RelativePath,
53 const Module *Imported,
54 SrcMgr::CharacteristicKind FileType) override {
57 Collector.addFile(File->getName());
61 struct ModuleDependencyMMCallbacks : public ModuleMapCallbacks {
62 ModuleDependencyCollector &Collector;
63 ModuleDependencyMMCallbacks(ModuleDependencyCollector &Collector)
64 : Collector(Collector) {}
66 void moduleMapAddHeader(StringRef HeaderPath) override {
67 if (llvm::sys::path::is_absolute(HeaderPath))
68 Collector.addFile(HeaderPath);
70 void moduleMapAddUmbrellaHeader(FileManager *FileMgr,
71 const FileEntry *Header) override {
72 StringRef HeaderFilename = Header->getName();
73 moduleMapAddHeader(HeaderFilename);
74 // The FileManager can find and cache the symbolic link for a framework
75 // header before its real path, this means a module can have some of its
76 // headers to use other paths. Although this is usually not a problem, it's
77 // inconsistent, and not collecting the original path header leads to
78 // umbrella clashes while rebuilding modules in the crash reproducer. For
80 // ApplicationServices.framework/Frameworks/ImageIO.framework/ImageIO.h
82 // ImageIO.framework/ImageIO.h
84 // FIXME: this shouldn't be necessary once we have FileName instances
85 // around instead of FileEntry ones. For now, make sure we collect all
86 // that we need for the reproducer to work correctly.
87 StringRef UmbreallDirFromHeader =
88 llvm::sys::path::parent_path(HeaderFilename);
89 StringRef UmbrellaDir = Header->getDir()->getName();
90 if (!UmbrellaDir.equals(UmbreallDirFromHeader)) {
91 SmallString<128> AltHeaderFilename;
92 llvm::sys::path::append(AltHeaderFilename, UmbrellaDir,
93 llvm::sys::path::filename(HeaderFilename));
94 if (FileMgr->getFile(AltHeaderFilename))
95 moduleMapAddHeader(AltHeaderFilename);
102 // TODO: move this to Support/Path.h and check for HAVE_REALPATH?
103 static bool real_path(StringRef SrcPath, SmallVectorImpl<char> &RealPath) {
105 char CanonicalPath[PATH_MAX];
107 // TODO: emit a warning in case this fails...?
108 if (!realpath(SrcPath.str().c_str(), CanonicalPath))
111 SmallString<256> RPath(CanonicalPath);
112 RealPath.swap(RPath);
115 // FIXME: Add support for systems without realpath.
120 void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
121 R.addListener(llvm::make_unique<ModuleDependencyListener>(*this));
124 void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) {
125 PP.addPPCallbacks(llvm::make_unique<ModuleDependencyPPCallbacks>(
126 *this, PP.getSourceManager()));
127 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
128 llvm::make_unique<ModuleDependencyMMCallbacks>(*this));
131 static bool isCaseSensitivePath(StringRef Path) {
132 SmallString<256> TmpDest = Path, UpperDest, RealDest;
133 // Remove component traversals, links, etc.
134 if (!real_path(Path, TmpDest))
135 return true; // Current default value in vfs.yaml
138 // Change path to all upper case and ask for its real path, if the latter
139 // exists and is equal to Path, it's not case sensitive. Default to case
140 // sensitive in the absence of realpath, since this is what the VFSWriter
141 // already expects when sensitivity isn't setup.
143 UpperDest.push_back(toUppercase(C));
144 if (real_path(UpperDest, RealDest) && Path.equals(RealDest))
149 void ModuleDependencyCollector::writeFileMap() {
153 StringRef VFSDir = getDest();
155 // Default to use relative overlay directories in the VFS yaml file. This
156 // allows crash reproducer scripts to work across machines.
157 VFSWriter.setOverlayDir(VFSDir);
159 // Explicitly set case sensitivity for the YAML writer. For that, find out
160 // the sensitivity at the path where the headers all collected to.
161 VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir));
163 // Do not rely on real path names when executing the crash reproducer scripts
164 // since we only want to actually use the files we have on the VFS cache.
165 VFSWriter.setUseExternalNames(false);
168 SmallString<256> YAMLPath = VFSDir;
169 llvm::sys::path::append(YAMLPath, "vfs.yaml");
170 llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::F_Text);
178 bool ModuleDependencyCollector::getRealPath(StringRef SrcPath,
179 SmallVectorImpl<char> &Result) {
180 using namespace llvm::sys;
181 SmallString<256> RealPath;
182 StringRef FileName = path::filename(SrcPath);
183 std::string Dir = path::parent_path(SrcPath).str();
184 auto DirWithSymLink = SymLinkMap.find(Dir);
186 // Use real_path to fix any symbolic link component present in a path.
187 // Computing the real path is expensive, cache the search through the
188 // parent path directory.
189 if (DirWithSymLink == SymLinkMap.end()) {
190 if (!real_path(Dir, RealPath))
192 SymLinkMap[Dir] = RealPath.str();
194 RealPath = DirWithSymLink->second;
197 path::append(RealPath, FileName);
198 Result.swap(RealPath);
202 std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src,
204 using namespace llvm::sys;
206 // We need an absolute src path to append to the root.
207 SmallString<256> AbsoluteSrc = Src;
208 fs::make_absolute(AbsoluteSrc);
209 // Canonicalize src to a native path to avoid mixed separator styles.
210 path::native(AbsoluteSrc);
211 // Remove redundant leading "./" pieces and consecutive separators.
212 AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
214 // Canonicalize the source path by removing "..", "." components.
215 SmallString<256> VirtualPath = AbsoluteSrc;
216 path::remove_dots(VirtualPath, /*remove_dot_dot=*/true);
218 // If a ".." component is present after a symlink component, remove_dots may
219 // lead to the wrong real destination path. Let the source be canonicalized
220 // like that but make sure we always use the real path for the destination.
221 SmallString<256> CopyFrom;
222 if (!getRealPath(AbsoluteSrc, CopyFrom))
223 CopyFrom = VirtualPath;
224 SmallString<256> CacheDst = getDest();
227 // The common case is to map the virtual path to the same path inside the
229 path::append(CacheDst, path::relative_path(CopyFrom));
231 // When collecting entries from input vfsoverlays, copy the external
232 // contents into the cache but still map from the source.
233 if (!fs::exists(Dst))
234 return std::error_code();
235 path::append(CacheDst, Dst);
239 // Copy the file into place.
240 if (std::error_code EC = fs::create_directories(path::parent_path(CacheDst),
241 /*IgnoreExisting=*/true))
243 if (std::error_code EC = fs::copy_file(CopyFrom, CacheDst))
246 // Always map a canonical src path to its real path into the YAML, by doing
247 // this we map different virtual src paths to the same entry in the VFS
248 // overlay, which is a way to emulate symlink inside the VFS; this is also
249 // needed for correctness, not doing that can lead to module redefinition
251 addFileMapping(VirtualPath, CacheDst);
252 return std::error_code();
255 void ModuleDependencyCollector::addFile(StringRef Filename, StringRef FileDst) {
256 if (insertSeen(Filename))
257 if (copyToRoot(Filename, FileDst))