ModuleDependencyCollector.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/StringMap.h"
  18. #include "llvm/ADT/iterator_range.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 ModuleDependencyMMCallbacks : public ModuleMapCallbacks {
  39. ModuleDependencyCollector &Collector;
  40. ModuleDependencyMMCallbacks(ModuleDependencyCollector &Collector)
  41. : Collector(Collector) {}
  42. void moduleMapAddHeader(StringRef HeaderPath) override {
  43. if (llvm::sys::path::is_absolute(HeaderPath))
  44. Collector.addFile(HeaderPath);
  45. }
  46. };
  47. }
  48. // TODO: move this to Support/Path.h and check for HAVE_REALPATH?
  49. static bool real_path(StringRef SrcPath, SmallVectorImpl<char> &RealPath) {
  50. #ifdef LLVM_ON_UNIX
  51. char CanonicalPath[PATH_MAX];
  52. // TODO: emit a warning in case this fails...?
  53. if (!realpath(SrcPath.str().c_str(), CanonicalPath))
  54. return false;
  55. SmallString<256> RPath(CanonicalPath);
  56. RealPath.swap(RPath);
  57. return true;
  58. #else
  59. // FIXME: Add support for systems without realpath.
  60. return false;
  61. #endif
  62. }
  63. void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
  64. R.addListener(llvm::make_unique<ModuleDependencyListener>(*this));
  65. }
  66. void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) {
  67. PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks(
  68. llvm::make_unique<ModuleDependencyMMCallbacks>(*this));
  69. }
  70. static bool isCaseSensitivePath(StringRef Path) {
  71. SmallString<256> TmpDest = Path, UpperDest, RealDest;
  72. // Remove component traversals, links, etc.
  73. if (!real_path(Path, TmpDest))
  74. return true; // Current default value in vfs.yaml
  75. Path = TmpDest;
  76. // Change path to all upper case and ask for its real path, if the latter
  77. // exists and is equal to Path, it's not case sensitive. Default to case
  78. // sensitive in the absense of realpath, since this is what the VFSWriter
  79. // already expects when sensitivity isn't setup.
  80. for (auto &C : Path)
  81. UpperDest.push_back(toUppercase(C));
  82. if (real_path(UpperDest, RealDest) && Path.equals(RealDest))
  83. return false;
  84. return true;
  85. }
  86. void ModuleDependencyCollector::writeFileMap() {
  87. if (Seen.empty())
  88. return;
  89. StringRef VFSDir = getDest();
  90. // Default to use relative overlay directories in the VFS yaml file. This
  91. // allows crash reproducer scripts to work across machines.
  92. VFSWriter.setOverlayDir(VFSDir);
  93. // Explicitly set case sensitivity for the YAML writer. For that, find out
  94. // the sensitivity at the path where the headers all collected to.
  95. VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir));
  96. // Do not rely on real path names when executing the crash reproducer scripts
  97. // since we only want to actually use the files we have on the VFS cache.
  98. VFSWriter.setUseExternalNames(false);
  99. std::error_code EC;
  100. SmallString<256> YAMLPath = VFSDir;
  101. llvm::sys::path::append(YAMLPath, "vfs.yaml");
  102. llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::F_Text);
  103. if (EC) {
  104. HasErrors = true;
  105. return;
  106. }
  107. VFSWriter.write(OS);
  108. }
  109. bool ModuleDependencyCollector::getRealPath(StringRef SrcPath,
  110. SmallVectorImpl<char> &Result) {
  111. using namespace llvm::sys;
  112. SmallString<256> RealPath;
  113. StringRef FileName = path::filename(SrcPath);
  114. std::string Dir = path::parent_path(SrcPath).str();
  115. auto DirWithSymLink = SymLinkMap.find(Dir);
  116. // Use real_path to fix any symbolic link component present in a path.
  117. // Computing the real path is expensive, cache the search through the
  118. // parent path directory.
  119. if (DirWithSymLink == SymLinkMap.end()) {
  120. if (!real_path(Dir, RealPath))
  121. return false;
  122. SymLinkMap[Dir] = RealPath.str();
  123. } else {
  124. RealPath = DirWithSymLink->second;
  125. }
  126. path::append(RealPath, FileName);
  127. Result.swap(RealPath);
  128. return true;
  129. }
  130. std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src) {
  131. using namespace llvm::sys;
  132. // We need an absolute src path to append to the root.
  133. SmallString<256> AbsoluteSrc = Src;
  134. fs::make_absolute(AbsoluteSrc);
  135. // Canonicalize src to a native path to avoid mixed separator styles.
  136. path::native(AbsoluteSrc);
  137. // Remove redundant leading "./" pieces and consecutive separators.
  138. AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc);
  139. // Canonicalize the source path by removing "..", "." components.
  140. SmallString<256> CanonicalPath = AbsoluteSrc;
  141. path::remove_dots(CanonicalPath, /*remove_dot_dot=*/true);
  142. // If a ".." component is present after a symlink component, remove_dots may
  143. // lead to the wrong real destination path. Let the source be canonicalized
  144. // like that but make sure we always use the real path for the destination.
  145. SmallString<256> RealPath;
  146. if (!getRealPath(AbsoluteSrc, RealPath))
  147. RealPath = CanonicalPath;
  148. SmallString<256> Dest = getDest();
  149. path::append(Dest, path::relative_path(RealPath));
  150. // Copy the file into place.
  151. if (std::error_code EC = fs::create_directories(path::parent_path(Dest),
  152. /*IgnoreExisting=*/true))
  153. return EC;
  154. if (std::error_code EC = fs::copy_file(RealPath, Dest))
  155. return EC;
  156. // Always map a canonical src path to its real path into the YAML, by doing
  157. // this we map different virtual src paths to the same entry in the VFS
  158. // overlay, which is a way to emulate symlink inside the VFS; this is also
  159. // needed for correctness, not doing that can lead to module redifinition
  160. // errors.
  161. addFileMapping(CanonicalPath, Dest);
  162. return std::error_code();
  163. }
  164. void ModuleDependencyCollector::addFile(StringRef Filename) {
  165. if (insertSeen(Filename))
  166. if (copyToRoot(Filename))
  167. HasErrors = true;
  168. }