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" 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 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, 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 101 // TODO: move this to Support/Path.h and check for HAVE_REALPATH? 102 static bool real_path(StringRef SrcPath, SmallVectorImpl<char> &RealPath) { 103 #ifdef LLVM_ON_UNIX 104 char CanonicalPath[PATH_MAX]; 105 106 // TODO: emit a warning in case this fails...? 107 if (!realpath(SrcPath.str().c_str(), CanonicalPath)) 108 return false; 109 110 SmallString<256> RPath(CanonicalPath); 111 RealPath.swap(RPath); 112 return true; 113 #else 114 // FIXME: Add support for systems without realpath. 115 return false; 116 #endif 117 } 118 119 void ModuleDependencyCollector::attachToASTReader(ASTReader &R) { 120 R.addListener(llvm::make_unique<ModuleDependencyListener>(*this)); 121 } 122 123 void ModuleDependencyCollector::attachToPreprocessor(Preprocessor &PP) { 124 PP.addPPCallbacks(llvm::make_unique<ModuleDependencyPPCallbacks>( 125 *this, PP.getSourceManager())); 126 PP.getHeaderSearchInfo().getModuleMap().addModuleMapCallbacks( 127 llvm::make_unique<ModuleDependencyMMCallbacks>(*this)); 128 } 129 130 static bool isCaseSensitivePath(StringRef Path) { 131 SmallString<256> TmpDest = Path, UpperDest, RealDest; 132 // Remove component traversals, links, etc. 133 if (!real_path(Path, TmpDest)) 134 return true; // Current default value in vfs.yaml 135 Path = TmpDest; 136 137 // Change path to all upper case and ask for its real path, if the latter 138 // exists and is equal to Path, it's not case sensitive. Default to case 139 // sensitive in the absence of realpath, since this is what the VFSWriter 140 // already expects when sensitivity isn't setup. 141 for (auto &C : Path) 142 UpperDest.push_back(toUppercase(C)); 143 if (real_path(UpperDest, RealDest) && Path.equals(RealDest)) 144 return false; 145 return true; 146 } 147 148 void ModuleDependencyCollector::writeFileMap() { 149 if (Seen.empty()) 150 return; 151 152 StringRef VFSDir = getDest(); 153 154 // Default to use relative overlay directories in the VFS yaml file. This 155 // allows crash reproducer scripts to work across machines. 156 VFSWriter.setOverlayDir(VFSDir); 157 158 // Explicitly set case sensitivity for the YAML writer. For that, find out 159 // the sensitivity at the path where the headers all collected to. 160 VFSWriter.setCaseSensitivity(isCaseSensitivePath(VFSDir)); 161 162 // Do not rely on real path names when executing the crash reproducer scripts 163 // since we only want to actually use the files we have on the VFS cache. 164 VFSWriter.setUseExternalNames(false); 165 166 std::error_code EC; 167 SmallString<256> YAMLPath = VFSDir; 168 llvm::sys::path::append(YAMLPath, "vfs.yaml"); 169 llvm::raw_fd_ostream OS(YAMLPath, EC, llvm::sys::fs::F_Text); 170 if (EC) { 171 HasErrors = true; 172 return; 173 } 174 VFSWriter.write(OS); 175 } 176 177 bool ModuleDependencyCollector::getRealPath(StringRef SrcPath, 178 SmallVectorImpl<char> &Result) { 179 using namespace llvm::sys; 180 SmallString<256> RealPath; 181 StringRef FileName = path::filename(SrcPath); 182 std::string Dir = path::parent_path(SrcPath).str(); 183 auto DirWithSymLink = SymLinkMap.find(Dir); 184 185 // Use real_path to fix any symbolic link component present in a path. 186 // Computing the real path is expensive, cache the search through the 187 // parent path directory. 188 if (DirWithSymLink == SymLinkMap.end()) { 189 if (!real_path(Dir, RealPath)) 190 return false; 191 SymLinkMap[Dir] = RealPath.str(); 192 } else { 193 RealPath = DirWithSymLink->second; 194 } 195 196 path::append(RealPath, FileName); 197 Result.swap(RealPath); 198 return true; 199 } 200 201 std::error_code ModuleDependencyCollector::copyToRoot(StringRef Src, 202 StringRef Dst) { 203 using namespace llvm::sys; 204 205 // We need an absolute src path to append to the root. 206 SmallString<256> AbsoluteSrc = Src; 207 fs::make_absolute(AbsoluteSrc); 208 // Canonicalize src to a native path to avoid mixed separator styles. 209 path::native(AbsoluteSrc); 210 // Remove redundant leading "./" pieces and consecutive separators. 211 AbsoluteSrc = path::remove_leading_dotslash(AbsoluteSrc); 212 213 // Canonicalize the source path by removing "..", "." components. 214 SmallString<256> VirtualPath = AbsoluteSrc; 215 path::remove_dots(VirtualPath, /*remove_dot_dot=*/true); 216 217 // If a ".." component is present after a symlink component, remove_dots may 218 // lead to the wrong real destination path. Let the source be canonicalized 219 // like that but make sure we always use the real path for the destination. 220 SmallString<256> CopyFrom; 221 if (!getRealPath(AbsoluteSrc, CopyFrom)) 222 CopyFrom = VirtualPath; 223 SmallString<256> CacheDst = getDest(); 224 225 if (Dst.empty()) { 226 // The common case is to map the virtual path to the same path inside the 227 // cache. 228 path::append(CacheDst, path::relative_path(CopyFrom)); 229 } else { 230 // When collecting entries from input vfsoverlays, copy the external 231 // contents into the cache but still map from the source. 232 if (!fs::exists(Dst)) 233 return std::error_code(); 234 path::append(CacheDst, Dst); 235 CopyFrom = Dst; 236 } 237 238 // Copy the file into place. 239 if (std::error_code EC = fs::create_directories(path::parent_path(CacheDst), 240 /*IgnoreExisting=*/true)) 241 return EC; 242 if (std::error_code EC = fs::copy_file(CopyFrom, CacheDst)) 243 return EC; 244 245 // Always map a canonical src path to its real path into the YAML, by doing 246 // this we map different virtual src paths to the same entry in the VFS 247 // overlay, which is a way to emulate symlink inside the VFS; this is also 248 // needed for correctness, not doing that can lead to module redefinition 249 // errors. 250 addFileMapping(VirtualPath, CacheDst); 251 return std::error_code(); 252 } 253 254 void ModuleDependencyCollector::addFile(StringRef Filename, StringRef FileDst) { 255 if (insertSeen(Filename)) 256 if (copyToRoot(Filename, FileDst)) 257 HasErrors = true; 258 } 259