clang 22.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/Path.h"
20#include "llvm/Support/raw_ostream.h"
21
22using namespace clang;
23
24namespace {
25/// Private implementations for ModuleDependencyCollector
26class ModuleDependencyListener : public ASTReaderListener {
27 ModuleDependencyCollector &Collector;
28 FileManager &FileMgr;
29public:
30 ModuleDependencyListener(ModuleDependencyCollector &Collector,
31 FileManager &FileMgr)
32 : Collector(Collector), FileMgr(FileMgr) {}
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 // Run this through the FileManager in order to respect 'use-external-name'
38 // in case we have a VFS overlay.
39 if (auto FE = FileMgr.getOptionalFileRef(Filename))
40 Filename = FE->getName();
41 Collector.addFile(Filename);
42 return true;
43 }
44};
45
46struct ModuleDependencyPPCallbacks : public PPCallbacks {
47 ModuleDependencyCollector &Collector;
48 SourceManager &SM;
49 ModuleDependencyPPCallbacks(ModuleDependencyCollector &Collector,
50 SourceManager &SM)
51 : Collector(Collector), SM(SM) {}
52
53 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
54 StringRef FileName, bool IsAngled,
55 CharSourceRange FilenameRange,
56 OptionalFileEntryRef File, StringRef SearchPath,
57 StringRef RelativePath, const Module *SuggestedModule,
58 bool ModuleImported,
60 if (!File)
61 return;
62 Collector.addFile(File->getName());
63 }
64};
65
66struct ModuleDependencyMMCallbacks : public ModuleMapCallbacks {
67 ModuleDependencyCollector &Collector;
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 }
75 void moduleMapAddUmbrellaHeader(FileEntryRef Header) override {
76 moduleMapAddHeader(Header.getNameAsRequested());
77 }
78};
79
80} // namespace
81
84 std::make_unique<ModuleDependencyListener>(*this, R.getFileManager()));
85}
86
88 PP.addPPCallbacks(std::make_unique<ModuleDependencyPPCallbacks>(
89 *this, PP.getSourceManager()));
91 std::make_unique<ModuleDependencyMMCallbacks>(*this));
92}
93
94static bool isCaseSensitivePath(StringRef Path) {
95 SmallString<256> TmpDest = Path, UpperDest, RealDest;
96 // Remove component traversals, links, etc.
97 if (llvm::sys::fs::real_path(Path, TmpDest))
98 return true; // Current default value in vfs.yaml
99 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 for (auto &C : Path)
106 UpperDest.push_back(toUppercase(C));
107 if (!llvm::sys::fs::real_path(UpperDest, RealDest) && Path == RealDest)
108 return false;
109 return true;
110}
111
113 if (Seen.empty())
114 return;
115
116 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 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 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 VFSWriter.setUseExternalNames(false);
129
130 std::error_code EC;
131 SmallString<256> YAMLPath = VFSDir;
132 llvm::sys::path::append(YAMLPath, "vfs.yaml");
133 llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::OF_TextWithCRLF);
134 if (EC) {
135 HasErrors = true;
136 return;
137 }
138 VFSWriter.write(OS);
139}
140
141std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src,
142 StringRef Dst) {
143 using namespace llvm::sys;
144 llvm::FileCollector::PathCanonicalizer::PathStorage Paths =
145 Canonicalizer.canonicalize(Src);
146
147 SmallString<256> CacheDst = getDest();
148
149 if (Dst.empty()) {
150 // The common case is to map the virtual path to the same path inside the
151 // cache.
152 path::append(CacheDst, path::relative_path(Paths.CopyFrom));
153 } else {
154 // When collecting entries from input vfsoverlays, copy the external
155 // contents into the cache but still map from the source.
156 if (!fs::exists(Dst))
157 return std::error_code();
158 path::append(CacheDst, Dst);
159 Paths.CopyFrom = Dst;
160 }
161
162 // Copy the file into place.
163 if (std::error_code EC = fs::create_directories(path::parent_path(CacheDst),
164 /*IgnoreExisting=*/true))
165 return EC;
166 if (std::error_code EC = fs::copy_file(Paths.CopyFrom, CacheDst))
167 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 addFileMapping(Paths.VirtualPath, CacheDst);
175 return std::error_code();
176}
177
178void ModuleDependencyCollector::addFile(StringRef Filename, StringRef FileDst) {
179 if (insertSeen(Filename))
180 if (copyToRoot(Filename, FileDst))
181 HasErrors = true;
182}
llvm::MachO::FileType FileType
Definition MachO.h:46
static bool isCaseSensitivePath(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:1912
FileManager & getFileManager() const
Definition ASTReader.h:1818
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:154
void attachToPreprocessor(Preprocessor &PP) override
virtual void addFile(StringRef Filename, StringRef FileDst={})
virtual bool insertSeen(StringRef Filename)
Definition Utils.h:151
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:413
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