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