Debian Codegraph @

clang/lib/Frontend/ModuleDependencyCollector.cpp

raw
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/Config/llvm-config.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/IOSandbox.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/raw_ostream.h"
22
23 using namespace clang;
24
25 namespace {
26 /// Private implementations for ModuleDependencyCollector
27 class ModuleDependencyListener : public ASTReaderListener {
28 ModuleDependencyCollector &Collector;
29 FileManager &FileMgr;
30 public:
31 ModuleDependencyListener(ModuleDependencyCollector &Collector,
32 FileManager &FileMgr)
33 : Collector(Collector), FileMgr(FileMgr) {}
34 bool needsInputFileVisitation() override { return true; }
35 bool needsSystemInputFileVisitation() override { return true; }
36 bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden,
37 bool IsExplicitModule) override {
38 // Run this through the FileManager in order to respect 'use-external-name'
39 // in case we have a VFS overlay.
40 if (auto FE = FileMgr.getOptionalFileRef(Filename))
41 Filename = FE->getName();
42 Collector.addFile(Filename);
43 return true;
44 }
45 };
46
47 struct ModuleDependencyPPCallbacks : public PPCallbacks {
48 ModuleDependencyCollector &Collector;
49 SourceManager &SM;
50 ModuleDependencyPPCallbacks(ModuleDependencyCollector &Collector,
51 SourceManager &SM)
52 : Collector(Collector), SM(SM) {}
53
54 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
55 StringRef FileName, bool IsAngled,
56 CharSourceRange FilenameRange,
57 OptionalFileEntryRef File, StringRef SearchPath,
58 StringRef RelativePath, const Module *SuggestedModule,
59 bool ModuleImported,
60 SrcMgr::CharacteristicKind FileType) override {
61 if (!File)
62 return;
63 Collector.addFile(File->getName());
64 }
65 };
66
67 struct ModuleDependencyMMCallbacks : public ModuleMapCallbacks {
68 ModuleDependencyCollector &Collector;
69 ModuleDependencyMMCallbacks(ModuleDependencyCollector &Collector)
70 : Collector(Collector) {}
71
72 void moduleMapAddHeader(StringRef HeaderPath) override {
73 if (llvm::sys::path::is_absolute(HeaderPath))
74 Collector.addFile(HeaderPath);
75 }
76 void moduleMapAddUmbrellaHeader(FileEntryRef Header) override {
77 moduleMapAddHeader(Header.getNameAsRequested());
78 }
79 };
80
81 } // namespace
82
83 void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
84 R.addListener(
85 std::make_unique<ModuleDependencyListener>(*this, R.getFileManager()));
86 }
87
88 void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) {
89 PP.addPPCallbacks(std::make_unique<ModuleDependencyPPCallbacks>(
90 *this, PP.getSourceManager()));
91 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
92 std::make_unique<ModuleDependencyMMCallbacks>(*this));
93 }
94
95 static bool isCaseSensitivePath(llvm::vfs::FileSystem &VFS, StringRef Path) {
96 SmallString<256> TmpDest = Path, UpperDest, RealDest;
97 // Remove component traversals, links, etc.
98 if (VFS.getRealPath(Path, TmpDest))
99 return true; // Current default value in vfs.yaml
100 Path = TmpDest;
101
102 // Change path to all upper case and ask for its real path, if the latter
103 // exists and is equal to Path, it's not case sensitive. Default to case
104 // sensitive in the absence of realpath, since this is what the VFSWriter
105 // already expects when sensitivity isn't setup.
106 for (auto &C : Path)
107 UpperDest.push_back(toUppercase(C));
108 if (!VFS.getRealPath(UpperDest, RealDest) && Path == RealDest)
109 return false;
110 return true;
111 }
112
113 void ModuleDependencyCollector::writeFileMap() {
114 if (Seen.empty())
115 return;
116
117 StringRef VFSDir = getDest();
118
119 // Default to use relative overlay directories in the VFS yaml file. This
120 // allows crash reproducer scripts to work across machines.
121 VFSWriter.setOverlayDir(VFSDir);
122
123 // Explicitly set case sensitivity for the YAML writer. For that, find out
124 // the sensitivity at the path where the headers all collected to.
125 VFSWriter.setCaseSensitivity(
126 isCaseSensitivePath(Canonicalizer.getFileSystem(), VFSDir));
127
128 // Do not rely on real path names when executing the crash reproducer scripts
129 // since we only want to actually use the files we have on the VFS cache.
130 VFSWriter.setUseExternalNames(false);
131
132 std::error_code EC;
133 SmallString<256> YAMLPath = VFSDir;
134 llvm::sys::path::append(YAMLPath, "vfs.yaml");
135 llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::OF_TextWithCRLF);
136 if (EC) {
137 HasErrors = true;
138 return;
139 }
140 VFSWriter.write(OS);
141 }
142
143 std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src,
144 StringRef Dst) {
145 using namespace llvm::sys;
146 llvm::FileCollector::PathCanonicalizer::PathStorage Paths =
147 Canonicalizer.canonicalize(Src);
148
149 SmallString<256> CacheDst = getDest();
150
151 if (Dst.empty()) {
152 // The common case is to map the virtual path to the same path inside the
153 // cache.
154 path::append(CacheDst, path::relative_path(Paths.CopyFrom));
155 } else {
156 // When collecting entries from input vfsoverlays, copy the external
157 // contents into the cache but still map from the source.
158 if (!Canonicalizer.getFileSystem().exists(Dst))
159 return std::error_code();
160 path::append(CacheDst, Dst);
161 Paths.CopyFrom = Dst;
162 }
163
164 // Copy the file into place.
165 {
166 // FIXME(sandboxing): Implement this via vfs::{FileSystem,OutputBackend}.
167 auto BypassSandbox = sandbox::scopedDisable();
168
169 if (std::error_code EC = fs::create_directories(path::parent_path(CacheDst),
170 /*IgnoreExisting=*/true))
171 return EC;
172 if (std::error_code EC = fs::copy_file(Paths.CopyFrom, CacheDst))
173 return EC;
174 }
175
176 // Always map a canonical src path to its real path into the YAML, by doing
177 // this we map different virtual src paths to the same entry in the VFS
178 // overlay, which is a way to emulate symlink inside the VFS; this is also
179 // needed for correctness, not doing that can lead to module redefinition
180 // errors.
181 addFileMapping(Paths.VirtualPath, CacheDst);
182 return std::error_code();
183 }
184
185 void ModuleDependencyCollector::addFile(StringRef Filename, StringRef FileDst) {
186 if (insertSeen(Filename))
187 if (copyToRoot(Filename, FileDst))
188 HasErrors = true;
189 }