186d1259cSJustin Bogner //===--- ModuleDependencyCollector.cpp - Collect module dependencies ------===//
286d1259cSJustin Bogner //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
686d1259cSJustin Bogner //
786d1259cSJustin Bogner //===----------------------------------------------------------------------===//
886d1259cSJustin Bogner //
986d1259cSJustin Bogner // Collect the dependencies of a set of modules.
1086d1259cSJustin Bogner //
1186d1259cSJustin Bogner //===----------------------------------------------------------------------===//
1286d1259cSJustin Bogner
134775fcfaSBruno Cardoso Lopes #include "clang/Basic/CharInfo.h"
1486d1259cSJustin Bogner #include "clang/Frontend/Utils.h"
15e62cfd7cSBruno Cardoso Lopes #include "clang/Lex/Preprocessor.h"
1686d1259cSJustin Bogner #include "clang/Serialization/ASTReader.h"
170d9593ddSChandler Carruth #include "llvm/ADT/iterator_range.h"
18d637c059SNico Weber #include "llvm/Config/llvm-config.h"
19cbda32fbSJustin Bogner #include "llvm/Support/FileSystem.h"
2086d1259cSJustin Bogner #include "llvm/Support/Path.h"
2186d1259cSJustin Bogner #include "llvm/Support/raw_ostream.h"
2286d1259cSJustin Bogner
2386d1259cSJustin Bogner using namespace clang;
2486d1259cSJustin Bogner
2586d1259cSJustin Bogner namespace {
26e62cfd7cSBruno Cardoso Lopes /// Private implementations for ModuleDependencyCollector
2786d1259cSJustin Bogner class ModuleDependencyListener : public ASTReaderListener {
2886d1259cSJustin Bogner ModuleDependencyCollector &Collector;
2986d1259cSJustin Bogner public:
ModuleDependencyListener(ModuleDependencyCollector & Collector)3086d1259cSJustin Bogner ModuleDependencyListener(ModuleDependencyCollector &Collector)
3186d1259cSJustin Bogner : Collector(Collector) {}
needsInputFileVisitation()3286d1259cSJustin Bogner bool needsInputFileVisitation() override { return true; }
needsSystemInputFileVisitation()3386d1259cSJustin Bogner bool needsSystemInputFileVisitation() override { return true; }
visitInputFile(StringRef Filename,bool IsSystem,bool IsOverridden,bool IsExplicitModule)34216a3bd7SRichard Smith bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden,
35b1631d91SBruno Cardoso Lopes bool IsExplicitModule) override {
36b1631d91SBruno Cardoso Lopes Collector.addFile(Filename);
37b1631d91SBruno Cardoso Lopes return true;
38b1631d91SBruno Cardoso Lopes }
3986d1259cSJustin Bogner };
40e62cfd7cSBruno Cardoso Lopes
4166e9627bSBruno Cardoso Lopes struct ModuleDependencyPPCallbacks : public PPCallbacks {
4266e9627bSBruno Cardoso Lopes ModuleDependencyCollector &Collector;
4366e9627bSBruno Cardoso Lopes SourceManager &SM;
ModuleDependencyPPCallbacks__anonddb9f1e50111::ModuleDependencyPPCallbacks4466e9627bSBruno Cardoso Lopes ModuleDependencyPPCallbacks(ModuleDependencyCollector &Collector,
4566e9627bSBruno Cardoso Lopes SourceManager &SM)
4666e9627bSBruno Cardoso Lopes : Collector(Collector), SM(SM) {}
4766e9627bSBruno Cardoso Lopes
InclusionDirective__anonddb9f1e50111::ModuleDependencyPPCallbacks4866e9627bSBruno Cardoso Lopes void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
4966e9627bSBruno Cardoso Lopes StringRef FileName, bool IsAngled,
50*d79ad2f1SJan Svoboda CharSourceRange FilenameRange,
51*d79ad2f1SJan Svoboda Optional<FileEntryRef> File, StringRef SearchPath,
52*d79ad2f1SJan Svoboda StringRef RelativePath, const Module *Imported,
5396fbe58bSJulie Hockett SrcMgr::CharacteristicKind FileType) override {
5466e9627bSBruno Cardoso Lopes if (!File)
5566e9627bSBruno Cardoso Lopes return;
5666e9627bSBruno Cardoso Lopes Collector.addFile(File->getName());
5766e9627bSBruno Cardoso Lopes }
5866e9627bSBruno Cardoso Lopes };
5966e9627bSBruno Cardoso Lopes
60e62cfd7cSBruno Cardoso Lopes struct ModuleDependencyMMCallbacks : public ModuleMapCallbacks {
61e62cfd7cSBruno Cardoso Lopes ModuleDependencyCollector &Collector;
ModuleDependencyMMCallbacks__anonddb9f1e50111::ModuleDependencyMMCallbacks62e62cfd7cSBruno Cardoso Lopes ModuleDependencyMMCallbacks(ModuleDependencyCollector &Collector)
63e62cfd7cSBruno Cardoso Lopes : Collector(Collector) {}
64e62cfd7cSBruno Cardoso Lopes
moduleMapAddHeader__anonddb9f1e50111::ModuleDependencyMMCallbacks65f0841790SBruno Cardoso Lopes void moduleMapAddHeader(StringRef HeaderPath) override {
66e62cfd7cSBruno Cardoso Lopes if (llvm::sys::path::is_absolute(HeaderPath))
67e62cfd7cSBruno Cardoso Lopes Collector.addFile(HeaderPath);
68e62cfd7cSBruno Cardoso Lopes }
moduleMapAddUmbrellaHeader__anonddb9f1e50111::ModuleDependencyMMCallbacks69b3a0fa48SBruno Cardoso Lopes void moduleMapAddUmbrellaHeader(FileManager *FileMgr,
70b3a0fa48SBruno Cardoso Lopes const FileEntry *Header) override {
71b3a0fa48SBruno Cardoso Lopes StringRef HeaderFilename = Header->getName();
72b3a0fa48SBruno Cardoso Lopes moduleMapAddHeader(HeaderFilename);
73b3a0fa48SBruno Cardoso Lopes // The FileManager can find and cache the symbolic link for a framework
74b3a0fa48SBruno Cardoso Lopes // header before its real path, this means a module can have some of its
75b3a0fa48SBruno Cardoso Lopes // headers to use other paths. Although this is usually not a problem, it's
76b3a0fa48SBruno Cardoso Lopes // inconsistent, and not collecting the original path header leads to
77b3a0fa48SBruno Cardoso Lopes // umbrella clashes while rebuilding modules in the crash reproducer. For
78b3a0fa48SBruno Cardoso Lopes // example:
79b3a0fa48SBruno Cardoso Lopes // ApplicationServices.framework/Frameworks/ImageIO.framework/ImageIO.h
80b3a0fa48SBruno Cardoso Lopes // instead of:
81b3a0fa48SBruno Cardoso Lopes // ImageIO.framework/ImageIO.h
82b3a0fa48SBruno Cardoso Lopes //
83b3a0fa48SBruno Cardoso Lopes // FIXME: this shouldn't be necessary once we have FileName instances
84b3a0fa48SBruno Cardoso Lopes // around instead of FileEntry ones. For now, make sure we collect all
85b3a0fa48SBruno Cardoso Lopes // that we need for the reproducer to work correctly.
86b3a0fa48SBruno Cardoso Lopes StringRef UmbreallDirFromHeader =
87b3a0fa48SBruno Cardoso Lopes llvm::sys::path::parent_path(HeaderFilename);
88b3a0fa48SBruno Cardoso Lopes StringRef UmbrellaDir = Header->getDir()->getName();
89b3a0fa48SBruno Cardoso Lopes if (!UmbrellaDir.equals(UmbreallDirFromHeader)) {
90b3a0fa48SBruno Cardoso Lopes SmallString<128> AltHeaderFilename;
91b3a0fa48SBruno Cardoso Lopes llvm::sys::path::append(AltHeaderFilename, UmbrellaDir,
92b3a0fa48SBruno Cardoso Lopes llvm::sys::path::filename(HeaderFilename));
93b3a0fa48SBruno Cardoso Lopes if (FileMgr->getFile(AltHeaderFilename))
94b3a0fa48SBruno Cardoso Lopes moduleMapAddHeader(AltHeaderFilename);
95b3a0fa48SBruno Cardoso Lopes }
96b3a0fa48SBruno Cardoso Lopes }
97e62cfd7cSBruno Cardoso Lopes };
98e62cfd7cSBruno Cardoso Lopes
99ab9db510SAlexander Kornienko }
10086d1259cSJustin Bogner
attachToASTReader(ASTReader & R)10186d1259cSJustin Bogner void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
1022b3d49b6SJonas Devlieghere R.addListener(std::make_unique<ModuleDependencyListener>(*this));
10386d1259cSJustin Bogner }
10486d1259cSJustin Bogner
attachToPreprocessor(Preprocessor & PP)105e62cfd7cSBruno Cardoso Lopes void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) {
1062b3d49b6SJonas Devlieghere PP.addPPCallbacks(std::make_unique<ModuleDependencyPPCallbacks>(
10766e9627bSBruno Cardoso Lopes *this, PP.getSourceManager()));
108e62cfd7cSBruno Cardoso Lopes PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
1092b3d49b6SJonas Devlieghere std::make_unique<ModuleDependencyMMCallbacks>(*this));
110e62cfd7cSBruno Cardoso Lopes }
111e62cfd7cSBruno Cardoso Lopes
isCaseSensitivePath(StringRef Path)1124c20bef1SBruno Cardoso Lopes static bool isCaseSensitivePath(StringRef Path) {
11372af4725SSean Silva SmallString<256> TmpDest = Path, UpperDest, RealDest;
1144c20bef1SBruno Cardoso Lopes // Remove component traversals, links, etc.
11577bc7355SJonas Devlieghere if (llvm::sys::fs::real_path(Path, TmpDest))
1164c20bef1SBruno Cardoso Lopes return true; // Current default value in vfs.yaml
1174c20bef1SBruno Cardoso Lopes Path = TmpDest;
1184c20bef1SBruno Cardoso Lopes
1194c20bef1SBruno Cardoso Lopes // Change path to all upper case and ask for its real path, if the latter
1204c20bef1SBruno Cardoso Lopes // exists and is equal to Path, it's not case sensitive. Default to case
1212a8c18d9SAlexander Kornienko // sensitive in the absence of realpath, since this is what the VFSWriter
1224c20bef1SBruno Cardoso Lopes // already expects when sensitivity isn't setup.
1234c20bef1SBruno Cardoso Lopes for (auto &C : Path)
1244775fcfaSBruno Cardoso Lopes UpperDest.push_back(toUppercase(C));
12577bc7355SJonas Devlieghere if (!llvm::sys::fs::real_path(UpperDest, RealDest) && Path.equals(RealDest))
1264c20bef1SBruno Cardoso Lopes return false;
1274c20bef1SBruno Cardoso Lopes return true;
1284c20bef1SBruno Cardoso Lopes }
1294c20bef1SBruno Cardoso Lopes
writeFileMap()13086d1259cSJustin Bogner void ModuleDependencyCollector::writeFileMap() {
13186d1259cSJustin Bogner if (Seen.empty())
13286d1259cSJustin Bogner return;
13386d1259cSJustin Bogner
1344c20bef1SBruno Cardoso Lopes StringRef VFSDir = getDest();
13586d1259cSJustin Bogner
136d878e28eSBruno Cardoso Lopes // Default to use relative overlay directories in the VFS yaml file. This
137d878e28eSBruno Cardoso Lopes // allows crash reproducer scripts to work across machines.
1384c20bef1SBruno Cardoso Lopes VFSWriter.setOverlayDir(VFSDir);
1394c20bef1SBruno Cardoso Lopes
1404c20bef1SBruno Cardoso Lopes // Explicitly set case sensitivity for the YAML writer. For that, find out
1414c20bef1SBruno Cardoso Lopes // the sensitivity at the path where the headers all collected to.
1424c20bef1SBruno Cardoso Lopes VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir));
143d878e28eSBruno Cardoso Lopes
144fc8644cdSBruno Cardoso Lopes // Do not rely on real path names when executing the crash reproducer scripts
145fc8644cdSBruno Cardoso Lopes // since we only want to actually use the files we have on the VFS cache.
146fc8644cdSBruno Cardoso Lopes VFSWriter.setUseExternalNames(false);
147fc8644cdSBruno Cardoso Lopes
148dae941a6SRafael Espindola std::error_code EC;
1494c20bef1SBruno Cardoso Lopes SmallString<256> YAMLPath = VFSDir;
1504c20bef1SBruno Cardoso Lopes llvm::sys::path::append(YAMLPath, "vfs.yaml");
15182b3e28eSAbhina Sreeskantharajan llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::OF_TextWithCRLF);
152dae941a6SRafael Espindola if (EC) {
153b1631d91SBruno Cardoso Lopes HasErrors = true;
15486d1259cSJustin Bogner return;
15586d1259cSJustin Bogner }
15686d1259cSJustin Bogner VFSWriter.write(OS);
15786d1259cSJustin Bogner }
15886d1259cSJustin Bogner
copyToRoot(StringRef Src,StringRef Dst)15982ec4fdeSBruno Cardoso Lopes std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src,
16082ec4fdeSBruno Cardoso Lopes StringRef Dst) {
16186d1259cSJustin Bogner using namespace llvm::sys;
162080952a9SDuncan P. N. Exon Smith llvm::FileCollector::PathCanonicalizer::PathStorage Paths =
163080952a9SDuncan P. N. Exon Smith Canonicalizer.canonicalize(Src);
16486d1259cSJustin Bogner
16582ec4fdeSBruno Cardoso Lopes SmallString<256> CacheDst = getDest();
16682ec4fdeSBruno Cardoso Lopes
16782ec4fdeSBruno Cardoso Lopes if (Dst.empty()) {
16882ec4fdeSBruno Cardoso Lopes // The common case is to map the virtual path to the same path inside the
16982ec4fdeSBruno Cardoso Lopes // cache.
170080952a9SDuncan P. N. Exon Smith path::append(CacheDst, path::relative_path(Paths.CopyFrom));
17182ec4fdeSBruno Cardoso Lopes } else {
17282ec4fdeSBruno Cardoso Lopes // When collecting entries from input vfsoverlays, copy the external
17382ec4fdeSBruno Cardoso Lopes // contents into the cache but still map from the source.
17482ec4fdeSBruno Cardoso Lopes if (!fs::exists(Dst))
17582ec4fdeSBruno Cardoso Lopes return std::error_code();
17682ec4fdeSBruno Cardoso Lopes path::append(CacheDst, Dst);
177080952a9SDuncan P. N. Exon Smith Paths.CopyFrom = Dst;
17882ec4fdeSBruno Cardoso Lopes }
17986d1259cSJustin Bogner
18086d1259cSJustin Bogner // Copy the file into place.
18182ec4fdeSBruno Cardoso Lopes if (std::error_code EC = fs::create_directories(path::parent_path(CacheDst),
18286d1259cSJustin Bogner /*IgnoreExisting=*/true))
18386d1259cSJustin Bogner return EC;
184080952a9SDuncan P. N. Exon Smith if (std::error_code EC = fs::copy_file(Paths.CopyFrom, CacheDst))
18586d1259cSJustin Bogner return EC;
186b76c0277SBruno Cardoso Lopes
1870df3e044SBruno Cardoso Lopes // Always map a canonical src path to its real path into the YAML, by doing
1880df3e044SBruno Cardoso Lopes // this we map different virtual src paths to the same entry in the VFS
1890df3e044SBruno Cardoso Lopes // overlay, which is a way to emulate symlink inside the VFS; this is also
1903170de0eSHiroshi Inoue // needed for correctness, not doing that can lead to module redefinition
1910df3e044SBruno Cardoso Lopes // errors.
192080952a9SDuncan P. N. Exon Smith addFileMapping(Paths.VirtualPath, CacheDst);
19386d1259cSJustin Bogner return std::error_code();
19486d1259cSJustin Bogner }
19586d1259cSJustin Bogner
addFile(StringRef Filename,StringRef FileDst)19682ec4fdeSBruno Cardoso Lopes void ModuleDependencyCollector::addFile(StringRef Filename, StringRef FileDst) {
197b1631d91SBruno Cardoso Lopes if (insertSeen(Filename))
19882ec4fdeSBruno Cardoso Lopes if (copyToRoot(Filename, FileDst))
199b1631d91SBruno Cardoso Lopes HasErrors = true;
20086d1259cSJustin Bogner }
201