ModuleDependencyCollector.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/Frontend/Utils.h"
  14. #include "clang/Serialization/ASTReader.h"
  15. #include "llvm/ADT/iterator_range.h"
  16. #include "llvm/ADT/StringSet.h"
  17. #include "llvm/Support/FileSystem.h"
  18. #include "llvm/Support/Path.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. using namespace clang;
  21. namespace {
  22. /// Private implementation for ModuleDependencyCollector
  23. class ModuleDependencyListener : public ASTReaderListener {
  24. ModuleDependencyCollector &Collector;
  25. std::error_code copyToRoot(StringRef Src);
  26. public:
  27. ModuleDependencyListener(ModuleDependencyCollector &Collector)
  28. : Collector(Collector) {}
  29. bool needsInputFileVisitation() override { return true; }
  30. bool needsSystemInputFileVisitation() override { return true; }
  31. bool visitInputFile(StringRef Filename, bool IsSystem,
  32. bool IsOverridden) override;
  33. };
  34. }
  35. void ModuleDependencyCollector::attachToASTReader(ASTReader &R) {
  36. R.addListener(llvm::make_unique<ModuleDependencyListener>(*this));
  37. }
  38. void ModuleDependencyCollector::writeFileMap() {
  39. if (Seen.empty())
  40. return;
  41. SmallString<256> Dest = getDest();
  42. llvm::sys::path::append(Dest, "vfs.yaml");
  43. std::error_code EC;
  44. llvm::raw_fd_ostream OS(Dest, EC, llvm::sys::fs::F_Text);
  45. if (EC) {
  46. setHasErrors();
  47. return;
  48. }
  49. VFSWriter.write(OS);
  50. }
  51. /// Remove traversal (ie, . or ..) from the given absolute path.
  52. static void removePathTraversal(SmallVectorImpl<char> &Path) {
  53. using namespace llvm::sys;
  54. SmallVector<StringRef, 16> ComponentStack;
  55. StringRef P(Path.data(), Path.size());
  56. // Skip the root path, then look for traversal in the components.
  57. StringRef Rel = path::relative_path(P);
  58. for (StringRef C : llvm::make_range(path::begin(Rel), path::end(Rel))) {
  59. if (C == ".")
  60. continue;
  61. if (C == "..") {
  62. assert(ComponentStack.size() && "Path traverses out of parent");
  63. ComponentStack.pop_back();
  64. } else
  65. ComponentStack.push_back(C);
  66. }
  67. // The stack is now the path without any directory traversal.
  68. SmallString<256> Buffer = path::root_path(P);
  69. for (StringRef C : ComponentStack)
  70. path::append(Buffer, C);
  71. // Put the result in Path.
  72. Path.swap(Buffer);
  73. }
  74. std::error_code ModuleDependencyListener::copyToRoot(StringRef Src) {
  75. using namespace llvm::sys;
  76. // We need an absolute path to append to the root.
  77. SmallString<256> AbsoluteSrc = Src;
  78. fs::make_absolute(AbsoluteSrc);
  79. removePathTraversal(AbsoluteSrc);
  80. // Build the destination path.
  81. SmallString<256> Dest = Collector.getDest();
  82. path::append(Dest, path::relative_path(AbsoluteSrc));
  83. // Copy the file into place.
  84. if (std::error_code EC = fs::create_directories(path::parent_path(Dest),
  85. /*IgnoreExisting=*/true))
  86. return EC;
  87. if (std::error_code EC = fs::copy_file(AbsoluteSrc.str(), Dest.str()))
  88. return EC;
  89. // Use the absolute path under the root for the file mapping.
  90. Collector.addFileMapping(AbsoluteSrc.str(), Dest.str());
  91. return std::error_code();
  92. }
  93. bool ModuleDependencyListener::visitInputFile(StringRef Filename, bool IsSystem,
  94. bool IsOverridden) {
  95. if (Collector.insertSeen(Filename))
  96. if (copyToRoot(Filename))
  97. Collector.setHasErrors();
  98. return true;
  99. }