clang 17.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/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
23using namespace clang;
24
25namespace {
26/// Private implementations for ModuleDependencyCollector
27class ModuleDependencyListener : public ASTReaderListener {
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 {
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 *Imported,
59 SrcMgr::CharacteristicKind FileType) override {
60 if (!File)
61 return;
62 Collector.addFile(File->getName());
63 }
64};
65
66struct ModuleDependencyMMCallbacks : public ModuleMapCallbacks {
68 ModuleDependencyMMCallbacks(ModuleDependencyCollector &Collector)
69 : Collector(Collector) {}
70
71 void moduleMapAddHeader(StringRef HeaderPath) override {
72 if (llvm::sys::path::is_absolute(HeaderPath))
73 Collector.addFile(HeaderPath);
74 }
76 const FileEntry *Header) override {
77 StringRef HeaderFilename = Header->getName();
78 moduleMapAddHeader(HeaderFilename);
79 // The FileManager can find and cache the symbolic link for a framework
80 // header before its real path, this means a module can have some of its
81 // headers to use other paths. Although this is usually not a problem, it's
82 // inconsistent, and not collecting the original path header leads to
83 // umbrella clashes while rebuilding modules in the crash reproducer. For
84 // example:
85 // ApplicationServices.framework/Frameworks/ImageIO.framework/ImageIO.h
86 // instead of:
87 // ImageIO.framework/ImageIO.h
88 //
89 // FIXME: this shouldn't be necessary once we have FileName instances
90 // around instead of FileEntry ones. For now, make sure we collect all
91 // that we need for the reproducer to work correctly.
92 StringRef UmbreallDirFromHeader =
93 llvm::sys::path::parent_path(HeaderFilename);
94 StringRef UmbrellaDir = Header->getDir()->getName();
95 if (!UmbrellaDir.equals(UmbreallDirFromHeader)) {
96 SmallString<128> AltHeaderFilename;
97 llvm::sys::path::append(AltHeaderFilename, UmbrellaDir,
98 llvm::sys::path::filename(HeaderFilename));
99 if (FileMgr->getFile(AltHeaderFilename))
100 moduleMapAddHeader(AltHeaderFilename);
101 }
102 }
103};
104
105}
106
108 R.addListener(
109 std::make_unique<ModuleDependencyListener>(*this, R.getFileManager()));
110}
111
113 PP.addPPCallbacks(std::make_unique<ModuleDependencyPPCallbacks>(
114 *this, PP.getSourceManager()));
116 std::make_unique<ModuleDependencyMMCallbacks>(*this));
117}
118
119static bool isCaseSensitivePath(StringRef Path) {
120 SmallString<256> TmpDest = Path, UpperDest, RealDest;
121 // Remove component traversals, links, etc.
122 if (llvm::sys::fs::real_path(Path, TmpDest))
123 return true; // Current default value in vfs.yaml
124 Path = TmpDest;
125
126 // Change path to all upper case and ask for its real path, if the latter
127 // exists and is equal to Path, it's not case sensitive. Default to case
128 // sensitive in the absence of realpath, since this is what the VFSWriter
129 // already expects when sensitivity isn't setup.
130 for (auto &C : Path)
131 UpperDest.push_back(toUppercase(C));
132 if (!llvm::sys::fs::real_path(UpperDest, RealDest) && Path.equals(RealDest))
133 return false;
134 return true;
135}
136
138 if (Seen.empty())
139 return;
140
141 StringRef VFSDir = getDest();
142
143 // Default to use relative overlay directories in the VFS yaml file. This
144 // allows crash reproducer scripts to work across machines.
145 VFSWriter.setOverlayDir(VFSDir);
146
147 // Explicitly set case sensitivity for the YAML writer. For that, find out
148 // the sensitivity at the path where the headers all collected to.
149 VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir));
150
151 // Do not rely on real path names when executing the crash reproducer scripts
152 // since we only want to actually use the files we have on the VFS cache.
153 VFSWriter.setUseExternalNames(false);
154
155 std::error_code EC;
156 SmallString<256> YAMLPath = VFSDir;
157 llvm::sys::path::append(YAMLPath, "vfs.yaml");
158 llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::OF_TextWithCRLF);
159 if (EC) {
160 HasErrors = true;
161 return;
162 }
163 VFSWriter.write(OS);
164}
165
166std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src,
167 StringRef Dst) {
168 using namespace llvm::sys;
169 llvm::FileCollector::PathCanonicalizer::PathStorage Paths =
170 Canonicalizer.canonicalize(Src);
171
172 SmallString<256> CacheDst = getDest();
173
174 if (Dst.empty()) {
175 // The common case is to map the virtual path to the same path inside the
176 // cache.
177 path::append(CacheDst, path::relative_path(Paths.CopyFrom));
178 } else {
179 // When collecting entries from input vfsoverlays, copy the external
180 // contents into the cache but still map from the source.
181 if (!fs::exists(Dst))
182 return std::error_code();
183 path::append(CacheDst, Dst);
184 Paths.CopyFrom = Dst;
185 }
186
187 // Copy the file into place.
188 if (std::error_code EC = fs::create_directories(path::parent_path(CacheDst),
189 /*IgnoreExisting=*/true))
190 return EC;
191 if (std::error_code EC = fs::copy_file(Paths.CopyFrom, CacheDst))
192 return EC;
193
194 // Always map a canonical src path to its real path into the YAML, by doing
195 // this we map different virtual src paths to the same entry in the VFS
196 // overlay, which is a way to emulate symlink inside the VFS; this is also
197 // needed for correctness, not doing that can lead to module redefinition
198 // errors.
199 addFileMapping(Paths.VirtualPath, CacheDst);
200 return std::error_code();
201}
202
203void ModuleDependencyCollector::addFile(StringRef Filename, StringRef FileDst) {
204 if (insertSeen(Filename))
205 if (copyToRoot(Filename, FileDst))
206 HasErrors = true;
207}
#define SM(sm)
Definition: Cuda.cpp:80
StringRef Filename
Definition: Format.cpp:2795
static bool isCaseSensitivePath(StringRef Path)
Defines the clang::Preprocessor interface.
Abstract interface for callback invocations by the ASTReader.
Definition: ASTReader.h:113
virtual bool needsInputFileVisitation()
Returns true if this ASTReaderListener wants to receive the input files of the AST file via visitInpu...
Definition: ASTReader.h:217
virtual bool visitInputFile(StringRef Filename, bool isSystem, bool isOverridden, bool isExplicitModule)
if needsInputFileVisitation returns true, this is called for each non-system input file of the AST Fi...
Definition: ASTReader.h:229
virtual bool needsSystemInputFileVisitation()
Returns true if this ASTReaderListener wants to receive the system input files of the AST file via vi...
Definition: ASTReader.h:221
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:366
void addListener(std::unique_ptr< ASTReaderListener > L)
Add an AST callback listener.
Definition: ASTReader.h:1676
FileManager & getFileManager() const
Definition: ASTReader.h:1588
Represents a character-granular source range.
StringRef getName() const
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:353
const DirectoryEntry * getDir() const
Return the directory the file lives in.
Definition: FileEntry.h:394
StringRef getName() const
Definition: FileEntry.h:384
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
OptionalFileEntryRef getOptionalFileRef(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Get a FileEntryRef if it exists, without doing anything on error.
Definition: FileManager.h:234
llvm::ErrorOr< const FileEntry * > getFile(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
ModuleMap & getModuleMap()
Retrieve the module map.
Definition: HeaderSearch.h:795
Record the location of an inclusion directive, such as an #include or #import statement.
Collects the dependencies for imported modules into a directory.
Definition: Utils.h:143
void attachToASTReader(ASTReader &R) override
virtual void addFileMapping(StringRef VPath, StringRef RPath)
Definition: Utils.h:161
void attachToPreprocessor(Preprocessor &PP) override
virtual void addFile(StringRef Filename, StringRef FileDst={})
virtual bool insertSeen(StringRef Filename)
Definition: Utils.h:158
A mechanism to observe the actions of the module map parser as it reads module map files.
Definition: ModuleMap.h:48
virtual void moduleMapAddUmbrellaHeader(FileManager *FileMgr, const FileEntry *Header)
Called when an umbrella header is added during module map parsing.
Definition: ModuleMap.h:72
virtual void moduleMapAddHeader(StringRef Filename)
Called when a header is added during module map parsing.
Definition: ModuleMap.h:66
void addModuleMapCallbacks(std::unique_ptr< ModuleMapCallbacks > Callback)
Add a module map callback.
Definition: ModuleMap.h:424
Describes a module or submodule.
Definition: Module.h:98
This interface provides a way to observe the actions of the preprocessor as it does its thing.
Definition: PPCallbacks.h:35
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:128
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
SourceManager & getSourceManager() const
HeaderSearch & getHeaderSearchInfo() const
Encodes a location in the source.
This class handles loading and caching of source files into memory.
Token - This structure provides full information about a lexed token.
Definition: Token.h:35
CharacteristicKind
Indicates whether a file or directory holds normal user code, system code, or system code which is im...
Definition: SourceManager.h:80
@ C
Languages that the frontend can parse and compile.
LLVM_READONLY char toUppercase(char c)
Converts the given ASCII character to its uppercase equivalent.
Definition: CharInfo.h:217