ModuleDependencyCollector.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //===--- ModuleDependencyCollector.cpp - Collect module dependencies ------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Collect the dependencies of a set of modules.
  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. using namespace clang;
  23. namespace {
  24. /// Private implementations for ModuleDependencyCollector
  25. class ModuleDependencyListener : public ASTReaderListener {
  26. ModuleDependencyCollector &Collector;
  27. public:
  28. ModuleDependencyListener(ModuleDependencyCollector &Collector)
  29. : Collector(Collector) {}
  30. bool needsInputFileVisitation() override { return true; }
  31. bool needsSystemInputFileVisitation() override { return true; }
  32. bool visitInputFile(StringRef Filename, bool IsSystem, bool IsOverridden,
  33. bool IsExplicitModule) override {
  34. Collector.addFile(Filename);
  35. return true;
  36. }
  37. };
  38. struct ModuleDependencyPPCallbacks : public PPCallbacks {
  39. ModuleDependencyCollector &Collector;
  40. SourceManager &SM;
  41. ModuleDependencyPPCallbacks(ModuleDependencyCollector &Collector,
  42. SourceManager &SM)
  43. : Collector(Collector), SM(SM) {}
  44. void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
  45. StringRef FileName, bool IsAngled,
  46. CharSourceRange FilenameRange, const FileEntry *File,
  47. StringRef SearchPath, StringRef RelativePath,
  48. const Module *Imported) override {
  49. if (!File)
  50. return;
  51. Collector.addFile(File->getName());
  52. }
  53. };
  54. struct ModuleDependencyMMCallbacks : public ModuleMapCallbacks {
  55. ModuleDependencyCollector &Collector;
  56. ModuleDependencyMMCallbacks(ModuleDependencyCollector &Collector)
  57. : Collector(Collector) {}
  58. void moduleMapAddHeader(StringRef HeaderPath) override {
  59. if (llvm::sys::path::is_absolute(HeaderPath))
  60. Collector.addFile(HeaderPath);
  61. }
  62. void moduleMapAddUmbrellaHeader(FileManager *FileMgr,
  63. const FileEntry *Header) override {
  64. StringRef HeaderFilename = Header->getName();
  65. moduleMapAddHeader(HeaderFilename);
  66. // The FileManager can find and cache the symbolic link for a framework
  67. // header before its real path, this means a module can have some of its
  68. // headers to use other paths. Although this is usually not a problem, it's
  69. // inconsistent, and not collecting the original path header leads to
  70. // umbrella clashes while rebuilding modules in the crash reproducer. For
  71. // example:
  72. // ApplicationServices.framework/Frameworks/ImageIO.framework/ImageIO.h
  73. // instead of:
  74. // ImageIO.framework/ImageIO.h
  75. //
  76. // FIXME: this shouldn't be necessary once we have FileName instances
  77. // around instead of FileEntry ones. For now, make sure we collect all
  78. // that we need for the reproducer to work correctly.
  79. StringRef UmbreallDirFromHeader =
  80. llvm::sys::path::parent_path(HeaderFilename);
  81. StringRef UmbrellaDir = Header->getDir()->getName();
  82. if (!UmbrellaDir.equals(UmbreallDirFromHeader)) {
  83. SmallString<128> AltHeaderFilename;
  84. llvm::sys::path::append(AltHeaderFilename, UmbrellaDir,
  85. llvm::sys::path::filename(HeaderFilename));
  86. if (FileMgr->getFile(AltHeaderFilename))
  87. moduleMapAddHeader(AltHeaderFilename);
  88. }
  89. }
  90. };
  91. }
  92. // TODO: move this to Support/Path.h and check for HAVE_REALPATH?
  93. static bool real_path(StringRef SrcPath, SmallVectorImpl<char> &RealPath) {
  94. #ifdef LLVM_ON_UNIX
  95. char CanonicalPath[PATH_MAX];
  96. // TODO: emit a warning in case this fails...?
  97. if (!realpath(SrcPath.str().c_str(), CanonicalPath))
  98. return false;
  99. SmallString<256> RPath(CanonicalPath);
  100. RealPath.swap(RPath);
  101. return true;
  102. #else
  103. // FIXME: Add support for systems without realpath.
  104. return false;
  105. #endif
  106. }
  107. void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
  108. R.addListener(llvm::make_unique<ModuleDependencyListener>(*this));
  109. }
  110. void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) {
  111. PP.addPPCallbacks(llvm::make_unique<ModuleDependencyPPCallbacks>(
  112. *this, PP.getSourceManager()));
  113. PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
  114. llvm::make_unique<ModuleDependencyMMCallbacks>(*this));
  115. }
  116. static bool isCaseSensitivePath(StringRef Path) {
  117. SmallString<256> TmpDest = Path, UpperDest, RealDest;
  118. // Remove component traversals, links, etc.
  119. if (!real_path(Path, TmpDest))
  120. return true; // Current default value in vfs.yaml
  121. Path = TmpDest;
  122. // Change path to all upper case and ask for its real path, if the latter
  123. // exists and is equal to Path, it's not case sensitive. Default to case
  124. // sensitive in the absence of realpath, since this is what the VFSWriter
  125. // already expects when sensitivity isn't setup.
  126. for (auto &C : Path)
  127. UpperDest.push_back(toUppercase(C));
  128. if (real_path(UpperDest, RealDest) && Path.equals(RealDest))
  129. return false;
  130. return true;
  131. }
  132. void ModuleDependencyCollector::writeFileMap() {
  133. if (Seen.empty())
  134. return;
  135. StringRef VFSDir = getDest();
  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. // Do not ignore non existent contents otherwise we might skip something
  140. // that should have been collected here.
  141. VFSWriter.setIgnoreNonExistentContents(false);
  142. // Explicitly set case sensitivity for the YAML writer. For that, find out
  143. // the sensitivity at the path where the headers all collected to.
  144. VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir));
  145. // Do not rely on real path names when executing the crash reproducer scripts
  146. // since we only want to actually use the files we have on the VFS cache.
  147. VFSWriter.setUseExternalNames(false);
  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::F_Text);
  152. if (EC) {
  153. HasErrors = true;
  154. return;
  155. }
  156. VFSWriter.write(OS);
  157. }
  158. bool ModuleDependencyCollector::getRealPath(StringRef SrcPath,
  159. SmallVectorImpl<char> &Result) {
  160. using namespace llvm::sys;
  161. SmallString<256> RealPath;
  162. StringRef FileName = path::filename(SrcPath);
  163. std::string Dir = path::parent_path(SrcPath).str();
  164. auto DirWithSymLink = SymLinkMap.find(Dir);
  165. // Use real_path to fix any symbolic link component present in a path.
  166. // Computing the real path is expensive, cache the search through the
  167. // parent path directory.
  168. if (DirWithSymLink == SymLinkMap.end()) {
  169. if (!real_path(Dir, RealPath))
  170. return false;
  171. SymLinkMap[Dir] = RealPath.str();
  172. } else {
  173. RealPath = DirWithSymLink->second;
  174. }
  175. path::append(RealPath, FileName);
  176. Result.swap(RealPath);
  177. return true;
  178. }
  179. std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src,
  180. StringRef Dst) {
  181. using namespace llvm::sys;
  182. // We need an absolute src path to append to the root.
  183. SmallString<256> AbsoluteSrc = Src;
  184. fs::make_absolute(AbsoluteSrc);
  185. // Canonicalize src to a native path to avoid mixed separator styles.
  186. path::native(AbsoluteSrc);
  187. // Remove redundant leading "./" pieces and consecutive separators.
  188. AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
  189. // Canonicalize the source path by removing "..", "." components.
  190. SmallString<256> VirtualPath = AbsoluteSrc;
  191. path::remove_dots(VirtualPath, /*remove_dot_dot=*/true);
  192. // If a ".." component is present after a symlink component, remove_dots may
  193. // lead to the wrong real destination path. Let the source be canonicalized
  194. // like that but make sure we always use the real path for the destination.
  195. SmallString<256> CopyFrom;
  196. if (!getRealPath(AbsoluteSrc, CopyFrom))
  197. CopyFrom = VirtualPath;
  198. SmallString<256> CacheDst = getDest();
  199. if (Dst.empty()) {
  200. // The common case is to map the virtual path to the same path inside the
  201. // cache.
  202. path::append(CacheDst, path::relative_path(CopyFrom));
  203. } else {
  204. // When collecting entries from input vfsoverlays, copy the external
  205. // contents into the cache but still map from the source.
  206. if (!fs::exists(Dst))
  207. return std::error_code();
  208. path::append(CacheDst, Dst);
  209. CopyFrom = Dst;
  210. }
  211. // Copy the file into place.
  212. if (std::error_code EC = fs::create_directories(path::parent_path(CacheDst),
  213. /*IgnoreExisting=*/true))
  214. return EC;
  215. if (std::error_code EC = fs::copy_file(CopyFrom, CacheDst))
  216. return EC;
  217. // Always map a canonical src path to its real path into the YAML, by doing
  218. // this we map different virtual src paths to the same entry in the VFS
  219. // overlay, which is a way to emulate symlink inside the VFS; this is also
  220. // needed for correctness, not doing that can lead to module redefinition
  221. // errors.
  222. addFileMapping(VirtualPath, CacheDst);
  223. return std::error_code();
  224. }
  225. void ModuleDependencyCollector::addFile(StringRef Filename, StringRef FileDst) {
  226. if (insertSeen(Filename))
  227. if (copyToRoot(Filename, FileDst))
  228. HasErrors = true;
  229. }