1e5dd7070Spatrick //===--- ModuleDependencyCollector.cpp - Collect module dependencies ------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // Collect the dependencies of a set of modules.
10e5dd7070Spatrick //
11e5dd7070Spatrick //===----------------------------------------------------------------------===//
12e5dd7070Spatrick
13e5dd7070Spatrick #include "clang/Basic/CharInfo.h"
14e5dd7070Spatrick #include "clang/Frontend/Utils.h"
15e5dd7070Spatrick #include "clang/Lex/Preprocessor.h"
16e5dd7070Spatrick #include "clang/Serialization/ASTReader.h"
17e5dd7070Spatrick #include "llvm/ADT/iterator_range.h"
18e5dd7070Spatrick #include "llvm/Config/llvm-config.h"
19e5dd7070Spatrick #include "llvm/Support/FileSystem.h"
20e5dd7070Spatrick #include "llvm/Support/Path.h"
21e5dd7070Spatrick #include "llvm/Support/raw_ostream.h"
22e5dd7070Spatrick
23e5dd7070Spatrick using namespace clang;
24e5dd7070Spatrick
25e5dd7070Spatrick namespace {
26e5dd7070Spatrick /// Private implementations for ModuleDependencyCollector
27e5dd7070Spatrick class ModuleDependencyListener : public ASTReaderListener {
28e5dd7070Spatrick ModuleDependencyCollector &Collector;
29*12c85518Srobert FileManager &FileMgr;
30e5dd7070Spatrick public:
ModuleDependencyListener(ModuleDependencyCollector & Collector,FileManager & FileMgr)31*12c85518Srobert ModuleDependencyListener(ModuleDependencyCollector &Collector,
32*12c85518Srobert FileManager &FileMgr)
33*12c85518Srobert : Collector(Collector), FileMgr(FileMgr) {}
needsInputFileVisitation()34e5dd7070Spatrick bool needsInputFileVisitation() override { return true; }
needsSystemInputFileVisitation()35e5dd7070Spatrick bool needsSystemInputFileVisitation() override { return true; }
visitInputFile(StringRef Filename,bool IsSystem,bool IsOverridden,bool IsExplicitModule)36e5dd7070Spatrick bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden,
37e5dd7070Spatrick bool IsExplicitModule) override {
38*12c85518Srobert // Run this through the FileManager in order to respect 'use-external-name'
39*12c85518Srobert // in case we have a VFS overlay.
40*12c85518Srobert if (auto FE = FileMgr.getOptionalFileRef(Filename))
41*12c85518Srobert Filename = FE->getName();
42e5dd7070Spatrick Collector.addFile(Filename);
43e5dd7070Spatrick return true;
44e5dd7070Spatrick }
45e5dd7070Spatrick };
46e5dd7070Spatrick
47e5dd7070Spatrick struct ModuleDependencyPPCallbacks : public PPCallbacks {
48e5dd7070Spatrick ModuleDependencyCollector &Collector;
49e5dd7070Spatrick SourceManager &SM;
ModuleDependencyPPCallbacks__anone6f4b26c0111::ModuleDependencyPPCallbacks50e5dd7070Spatrick ModuleDependencyPPCallbacks(ModuleDependencyCollector &Collector,
51e5dd7070Spatrick SourceManager &SM)
52e5dd7070Spatrick : Collector(Collector), SM(SM) {}
53e5dd7070Spatrick
InclusionDirective__anone6f4b26c0111::ModuleDependencyPPCallbacks54e5dd7070Spatrick void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
55e5dd7070Spatrick StringRef FileName, bool IsAngled,
56*12c85518Srobert CharSourceRange FilenameRange,
57*12c85518Srobert OptionalFileEntryRef File, StringRef SearchPath,
58*12c85518Srobert StringRef RelativePath, const Module *Imported,
59e5dd7070Spatrick SrcMgr::CharacteristicKind FileType) override {
60e5dd7070Spatrick if (!File)
61e5dd7070Spatrick return;
62e5dd7070Spatrick Collector.addFile(File->getName());
63e5dd7070Spatrick }
64e5dd7070Spatrick };
65e5dd7070Spatrick
66e5dd7070Spatrick struct ModuleDependencyMMCallbacks : public ModuleMapCallbacks {
67e5dd7070Spatrick ModuleDependencyCollector &Collector;
ModuleDependencyMMCallbacks__anone6f4b26c0111::ModuleDependencyMMCallbacks68e5dd7070Spatrick ModuleDependencyMMCallbacks(ModuleDependencyCollector &Collector)
69e5dd7070Spatrick : Collector(Collector) {}
70e5dd7070Spatrick
moduleMapAddHeader__anone6f4b26c0111::ModuleDependencyMMCallbacks71e5dd7070Spatrick void moduleMapAddHeader(StringRef HeaderPath) override {
72e5dd7070Spatrick if (llvm::sys::path::is_absolute(HeaderPath))
73e5dd7070Spatrick Collector.addFile(HeaderPath);
74e5dd7070Spatrick }
moduleMapAddUmbrellaHeader__anone6f4b26c0111::ModuleDependencyMMCallbacks75e5dd7070Spatrick void moduleMapAddUmbrellaHeader(FileManager *FileMgr,
76e5dd7070Spatrick const FileEntry *Header) override {
77e5dd7070Spatrick StringRef HeaderFilename = Header->getName();
78e5dd7070Spatrick moduleMapAddHeader(HeaderFilename);
79e5dd7070Spatrick // The FileManager can find and cache the symbolic link for a framework
80e5dd7070Spatrick // header before its real path, this means a module can have some of its
81e5dd7070Spatrick // headers to use other paths. Although this is usually not a problem, it's
82e5dd7070Spatrick // inconsistent, and not collecting the original path header leads to
83e5dd7070Spatrick // umbrella clashes while rebuilding modules in the crash reproducer. For
84e5dd7070Spatrick // example:
85e5dd7070Spatrick // ApplicationServices.framework/Frameworks/ImageIO.framework/ImageIO.h
86e5dd7070Spatrick // instead of:
87e5dd7070Spatrick // ImageIO.framework/ImageIO.h
88e5dd7070Spatrick //
89e5dd7070Spatrick // FIXME: this shouldn't be necessary once we have FileName instances
90e5dd7070Spatrick // around instead of FileEntry ones. For now, make sure we collect all
91e5dd7070Spatrick // that we need for the reproducer to work correctly.
92e5dd7070Spatrick StringRef UmbreallDirFromHeader =
93e5dd7070Spatrick llvm::sys::path::parent_path(HeaderFilename);
94e5dd7070Spatrick StringRef UmbrellaDir = Header->getDir()->getName();
95e5dd7070Spatrick if (!UmbrellaDir.equals(UmbreallDirFromHeader)) {
96e5dd7070Spatrick SmallString<128> AltHeaderFilename;
97e5dd7070Spatrick llvm::sys::path::append(AltHeaderFilename, UmbrellaDir,
98e5dd7070Spatrick llvm::sys::path::filename(HeaderFilename));
99e5dd7070Spatrick if (FileMgr->getFile(AltHeaderFilename))
100e5dd7070Spatrick moduleMapAddHeader(AltHeaderFilename);
101e5dd7070Spatrick }
102e5dd7070Spatrick }
103e5dd7070Spatrick };
104e5dd7070Spatrick
105e5dd7070Spatrick }
106e5dd7070Spatrick
attachToASTReader(ASTReader & R)107e5dd7070Spatrick void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
108*12c85518Srobert R.addListener(
109*12c85518Srobert std::make_unique<ModuleDependencyListener>(*this, R.getFileManager()));
110e5dd7070Spatrick }
111e5dd7070Spatrick
attachToPreprocessor(Preprocessor & PP)112e5dd7070Spatrick void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) {
113e5dd7070Spatrick PP.addPPCallbacks(std::make_unique<ModuleDependencyPPCallbacks>(
114e5dd7070Spatrick *this, PP.getSourceManager()));
115e5dd7070Spatrick PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
116e5dd7070Spatrick std::make_unique<ModuleDependencyMMCallbacks>(*this));
117e5dd7070Spatrick }
118e5dd7070Spatrick
isCaseSensitivePath(StringRef Path)119e5dd7070Spatrick static bool isCaseSensitivePath(StringRef Path) {
120e5dd7070Spatrick SmallString<256> TmpDest = Path, UpperDest, RealDest;
121e5dd7070Spatrick // Remove component traversals, links, etc.
122e5dd7070Spatrick if (llvm::sys::fs::real_path(Path, TmpDest))
123e5dd7070Spatrick return true; // Current default value in vfs.yaml
124e5dd7070Spatrick Path = TmpDest;
125e5dd7070Spatrick
126e5dd7070Spatrick // Change path to all upper case and ask for its real path, if the latter
127e5dd7070Spatrick // exists and is equal to Path, it's not case sensitive. Default to case
128e5dd7070Spatrick // sensitive in the absence of realpath, since this is what the VFSWriter
129e5dd7070Spatrick // already expects when sensitivity isn't setup.
130e5dd7070Spatrick for (auto &C : Path)
131e5dd7070Spatrick UpperDest.push_back(toUppercase(C));
132e5dd7070Spatrick if (!llvm::sys::fs::real_path(UpperDest, RealDest) && Path.equals(RealDest))
133e5dd7070Spatrick return false;
134e5dd7070Spatrick return true;
135e5dd7070Spatrick }
136e5dd7070Spatrick
writeFileMap()137e5dd7070Spatrick void ModuleDependencyCollector::writeFileMap() {
138e5dd7070Spatrick if (Seen.empty())
139e5dd7070Spatrick return;
140e5dd7070Spatrick
141e5dd7070Spatrick StringRef VFSDir = getDest();
142e5dd7070Spatrick
143e5dd7070Spatrick // Default to use relative overlay directories in the VFS yaml file. This
144e5dd7070Spatrick // allows crash reproducer scripts to work across machines.
145e5dd7070Spatrick VFSWriter.setOverlayDir(VFSDir);
146e5dd7070Spatrick
147e5dd7070Spatrick // Explicitly set case sensitivity for the YAML writer. For that, find out
148e5dd7070Spatrick // the sensitivity at the path where the headers all collected to.
149e5dd7070Spatrick VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir));
150e5dd7070Spatrick
151e5dd7070Spatrick // Do not rely on real path names when executing the crash reproducer scripts
152e5dd7070Spatrick // since we only want to actually use the files we have on the VFS cache.
153e5dd7070Spatrick VFSWriter.setUseExternalNames(false);
154e5dd7070Spatrick
155e5dd7070Spatrick std::error_code EC;
156e5dd7070Spatrick SmallString<256> YAMLPath = VFSDir;
157e5dd7070Spatrick llvm::sys::path::append(YAMLPath, "vfs.yaml");
158a9ac8606Spatrick llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::OF_TextWithCRLF);
159e5dd7070Spatrick if (EC) {
160e5dd7070Spatrick HasErrors = true;
161e5dd7070Spatrick return;
162e5dd7070Spatrick }
163e5dd7070Spatrick VFSWriter.write(OS);
164e5dd7070Spatrick }
165e5dd7070Spatrick
copyToRoot(StringRef Src,StringRef Dst)166e5dd7070Spatrick std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src,
167e5dd7070Spatrick StringRef Dst) {
168e5dd7070Spatrick using namespace llvm::sys;
169a9ac8606Spatrick llvm::FileCollector::PathCanonicalizer::PathStorage Paths =
170a9ac8606Spatrick Canonicalizer.canonicalize(Src);
171e5dd7070Spatrick
172e5dd7070Spatrick SmallString<256> CacheDst = getDest();
173e5dd7070Spatrick
174e5dd7070Spatrick if (Dst.empty()) {
175e5dd7070Spatrick // The common case is to map the virtual path to the same path inside the
176e5dd7070Spatrick // cache.
177a9ac8606Spatrick path::append(CacheDst, path::relative_path(Paths.CopyFrom));
178e5dd7070Spatrick } else {
179e5dd7070Spatrick // When collecting entries from input vfsoverlays, copy the external
180e5dd7070Spatrick // contents into the cache but still map from the source.
181e5dd7070Spatrick if (!fs::exists(Dst))
182e5dd7070Spatrick return std::error_code();
183e5dd7070Spatrick path::append(CacheDst, Dst);
184a9ac8606Spatrick Paths.CopyFrom = Dst;
185e5dd7070Spatrick }
186e5dd7070Spatrick
187e5dd7070Spatrick // Copy the file into place.
188e5dd7070Spatrick if (std::error_code EC = fs::create_directories(path::parent_path(CacheDst),
189e5dd7070Spatrick /*IgnoreExisting=*/true))
190e5dd7070Spatrick return EC;
191a9ac8606Spatrick if (std::error_code EC = fs::copy_file(Paths.CopyFrom, CacheDst))
192e5dd7070Spatrick return EC;
193e5dd7070Spatrick
194e5dd7070Spatrick // Always map a canonical src path to its real path into the YAML, by doing
195e5dd7070Spatrick // this we map different virtual src paths to the same entry in the VFS
196e5dd7070Spatrick // overlay, which is a way to emulate symlink inside the VFS; this is also
197e5dd7070Spatrick // needed for correctness, not doing that can lead to module redefinition
198e5dd7070Spatrick // errors.
199a9ac8606Spatrick addFileMapping(Paths.VirtualPath, CacheDst);
200e5dd7070Spatrick return std::error_code();
201e5dd7070Spatrick }
202e5dd7070Spatrick
addFile(StringRef Filename,StringRef FileDst)203e5dd7070Spatrick void ModuleDependencyCollector::addFile(StringRef Filename, StringRef FileDst) {
204e5dd7070Spatrick if (insertSeen(Filename))
205e5dd7070Spatrick if (copyToRoot(Filename, FileDst))
206e5dd7070Spatrick HasErrors = true;
207e5dd7070Spatrick }
208