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