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