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 14 #include "clang/Basic/CharInfo.h" 15 #include "clang/Frontend/Utils.h" 16 #include "clang/Lex/Preprocessor.h" 17 #include "clang/Serialization/ASTReader.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 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; 43 SourceManager &SM; 44 ModuleDependencyPPCallbacks(ModuleDependencyCollector &Collector, 45 SourceManager &SM) 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) override { 53 if (!File) 54 return; 55 Collector.addFile(File->getName()); 56 } 57 }; 58 59 struct ModuleDependencyMMCallbacks : public ModuleMapCallbacks { 60 ModuleDependencyCollector &Collector; 61 ModuleDependencyMMCallbacks(ModuleDependencyCollector &Collector) 62 : Collector(Collector) {} 63 64 void moduleMapAddHeader(StringRef HeaderPath) override { 65 if (llvm::sys::path::is_absolute(HeaderPath)) 66 Collector.addFile(HeaderPath); 67 } 68 void moduleMapAddUmbrellaHeader(FileManager *FileMgr, 69 const FileEntry *Header) override { 70 StringRef HeaderFilename = Header->getName(); 71 moduleMapAddHeader(HeaderFilename); 72 // The FileManager can find and cache the symbolic link for a framework 73 // header before its real path, this means a module can have some of its 74 // headers to use other paths. Although this is usually not a problem, it's 75 // inconsistent, and not collecting the original path header leads to 76 // umbrella clashes while rebuilding modules in the crash reproducer. For 77 // example: 78 // ApplicationServices.framework/Frameworks/ImageIO.framework/ImageIO.h 79 // instead of: 80 // ImageIO.framework/ImageIO.h 81 // 82 // FIXME: this shouldn't be necessary once we have FileName instances 83 // around instead of FileEntry ones. For now, make sure we collect all 84 // that we need for the reproducer to work correctly. 85 StringRef UmbreallDirFromHeader = 86 llvm::sys::path::parent_path(HeaderFilename); 87 StringRef UmbrellaDir = Header->getDir()->getName(); 88 if (!UmbrellaDir.equals(UmbreallDirFromHeader)) { 89 SmallString<128> AltHeaderFilename; 90 llvm::sys::path::append(AltHeaderFilename, UmbrellaDir, 91 llvm::sys::path::filename(HeaderFilename)); 92 if (FileMgr->getFile(AltHeaderFilename)) 93 moduleMapAddHeader(AltHeaderFilename); 94 } 95 } 96 }; 97 98 } 99 100 void ModuleDependencyCollector::attachToASTReader(ASTReader &R) { 101 R.addListener(llvm::make_unique<ModuleDependencyListener>(*this)); 102 } 103 104 void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) { 105 PP.addPPCallbacks(llvm::make_unique<ModuleDependencyPPCallbacks>( 106 *this, PP.getSourceManager())); 107 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks( 108 llvm::make_unique<ModuleDependencyMMCallbacks>(*this)); 109 } 110 111 static bool isCaseSensitivePath(StringRef Path) { 112 SmallString<256> TmpDest = Path, UpperDest, RealDest; 113 // Remove component traversals, links, etc. 114 if (llvm::sys::fs::real_path(Path, TmpDest)) 115 return true; // Current default value in vfs.yaml 116 Path = TmpDest; 117 118 // Change path to all upper case and ask for its real path, if the latter 119 // exists and is equal to Path, it's not case sensitive. Default to case 120 // sensitive in the absence of realpath, since this is what the VFSWriter 121 // already expects when sensitivity isn't setup. 122 for (auto &C : Path) 123 UpperDest.push_back(toUppercase(C)); 124 if (!llvm::sys::fs::real_path(UpperDest, RealDest) && Path.equals(RealDest)) 125 return false; 126 return true; 127 } 128 129 void ModuleDependencyCollector::writeFileMap() { 130 if (Seen.empty()) 131 return; 132 133 StringRef VFSDir = getDest(); 134 135 // Default to use relative overlay directories in the VFS yaml file. This 136 // allows crash reproducer scripts to work across machines. 137 VFSWriter.setOverlayDir(VFSDir); 138 139 // Do not ignore non existent contents otherwise we might skip something 140 // that should have been collected here. 141 VFSWriter.setIgnoreNonExistentContents(false); 142 143 // Explicitly set case sensitivity for the YAML writer. For that, find out 144 // the sensitivity at the path where the headers all collected to. 145 VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir)); 146 147 // Do not rely on real path names when executing the crash reproducer scripts 148 // since we only want to actually use the files we have on the VFS cache. 149 VFSWriter.setUseExternalNames(false); 150 151 std::error_code EC; 152 SmallString<256> YAMLPath = VFSDir; 153 llvm::sys::path::append(YAMLPath, "vfs.yaml"); 154 llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::F_Text); 155 if (EC) { 156 HasErrors = true; 157 return; 158 } 159 VFSWriter.write(OS); 160 } 161 162 bool ModuleDependencyCollector::getRealPath(StringRef SrcPath, 163 SmallVectorImpl<char> &Result) { 164 using namespace llvm::sys; 165 SmallString<256> RealPath; 166 StringRef FileName = path::filename(SrcPath); 167 std::string Dir = path::parent_path(SrcPath).str(); 168 auto DirWithSymLink = SymLinkMap.find(Dir); 169 170 // Use real_path to fix any symbolic link component present in a path. 171 // Computing the real path is expensive, cache the search through the 172 // parent path directory. 173 if (DirWithSymLink == SymLinkMap.end()) { 174 if (llvm::sys::fs::real_path(Dir, RealPath)) 175 return false; 176 SymLinkMap[Dir] = RealPath.str(); 177 } else { 178 RealPath = DirWithSymLink->second; 179 } 180 181 path::append(RealPath, FileName); 182 Result.swap(RealPath); 183 return true; 184 } 185 186 std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src, 187 StringRef Dst) { 188 using namespace llvm::sys; 189 190 // We need an absolute src path to append to the root. 191 SmallString<256> AbsoluteSrc = Src; 192 fs::make_absolute(AbsoluteSrc); 193 // Canonicalize src to a native path to avoid mixed separator styles. 194 path::native(AbsoluteSrc); 195 // Remove redundant leading "./" pieces and consecutive separators. 196 AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc); 197 198 // Canonicalize the source path by removing "..", "." components. 199 SmallString<256> VirtualPath = AbsoluteSrc; 200 path::remove_dots(VirtualPath, /*remove_dot_dot=*/true); 201 202 // If a ".." component is present after a symlink component, remove_dots may 203 // lead to the wrong real destination path. Let the source be canonicalized 204 // like that but make sure we always use the real path for the destination. 205 SmallString<256> CopyFrom; 206 if (!getRealPath(AbsoluteSrc, CopyFrom)) 207 CopyFrom = VirtualPath; 208 SmallString<256> CacheDst = getDest(); 209 210 if (Dst.empty()) { 211 // The common case is to map the virtual path to the same path inside the 212 // cache. 213 path::append(CacheDst, path::relative_path(CopyFrom)); 214 } else { 215 // When collecting entries from input vfsoverlays, copy the external 216 // contents into the cache but still map from the source. 217 if (!fs::exists(Dst)) 218 return std::error_code(); 219 path::append(CacheDst, Dst); 220 CopyFrom = Dst; 221 } 222 223 // Copy the file into place. 224 if (std::error_code EC = fs::create_directories(path::parent_path(CacheDst), 225 /*IgnoreExisting=*/true)) 226 return EC; 227 if (std::error_code EC = fs::copy_file(CopyFrom, CacheDst)) 228 return EC; 229 230 // Always map a canonical src path to its real path into the YAML, by doing 231 // this we map different virtual src paths to the same entry in the VFS 232 // overlay, which is a way to emulate symlink inside the VFS; this is also 233 // needed for correctness, not doing that can lead to module redefinition 234 // errors. 235 addFileMapping(VirtualPath, CacheDst); 236 return std::error_code(); 237 } 238 239 void ModuleDependencyCollector::addFile(StringRef Filename, StringRef FileDst) { 240 if (insertSeen(Filename)) 241 if (copyToRoot(Filename, FileDst)) 242 HasErrors = true; 243 } 244