clang 23.0.0git
ModuleDependencyCollector.cpp
Go to the documentation of this file.
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
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
23using namespace clang;
24
25namespace {
26/// Private implementations for ModuleDependencyCollector
27class ModuleDependencyListener : public ASTReaderListener {
28 ModuleDependencyCollector &Collector;
29 FileManager &FileMgr;
30public:
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
47struct 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,
61 if (!File)
62 return;
63 Collector.addFile(File->getName());
64 }
65};
66
67struct 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
85 std::make_unique<ModuleDependencyListener>(*this, R.getFileManager()));
86}
87
89 PP.addPPCallbacks(std::make_unique<ModuleDependencyPPCallbacks>(
90 *this, PP.getSourceManager()));
92 std::make_unique<ModuleDependencyMMCallbacks>(*this));
93}
94
95static 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
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
143std::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
185void ModuleDependencyCollector::addFile(StringRef Filename, StringRef FileDst) {
186 if (insertSeen(Filename))
187 if (copyToRoot(Filename, FileDst))
188 HasErrors = true;
189}
llvm::MachO::FileType FileType
Definition MachO.h:46
static bool isCaseSensitivePath(llvm::vfs::FileSystem &VFS, StringRef Path)
Defines the clang::Preprocessor interface.
Abstract interface for callback invocations by the ASTReader.
Definition ASTReader.h:117
Reads an AST files chain containing the contents of a translation unit.
Definition ASTReader.h:430
void addListener(std::unique_ptr< ASTReaderListener > L)
Add an AST callback listener.
Definition ASTReader.h:1913
FileManager & getFileManager() const
Definition ASTReader.h:1819
StringRef getNameAsRequested() const
The name of this FileEntry, as originally requested without applying any remappings for VFS 'use-exte...
Definition FileEntry.h:68
ModuleMap & getModuleMap()
Retrieve the module map.
void attachToASTReader(ASTReader &R) override
virtual void addFileMapping(StringRef VPath, StringRef RPath)
Definition Utils.h:155
void attachToPreprocessor(Preprocessor &PP) override
virtual void addFile(StringRef Filename, StringRef FileDst={})
virtual bool insertSeen(StringRef Filename)
Definition Utils.h:152
A mechanism to observe the actions of the module map loader as it reads module map files.
Definition ModuleMap.h:49
void addModuleMapCallbacks(std::unique_ptr< ModuleMapCallbacks > Callback)
Add a module map callback.
Definition ModuleMap.h:417
This interface provides a way to observe the actions of the preprocessor as it does its thing.
Definition PPCallbacks.h:37
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
SourceManager & getSourceManager() const
HeaderSearch & getHeaderSearchInfo() const
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
The JSON file list parser is used to communicate input to InstallAPI.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition FileEntry.h:208
@ Module
Module linkage, which indicates that the entity can be referred to from other translation units withi...
Definition Linkage.h:54
LLVM_READONLY char toUppercase(char c)
Converts the given ASCII character to its uppercase equivalent.
Definition CharInfo.h:233