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