1 //===- ModuleDepCollector.cpp - Callbacks to collect deps -------*- C++ -*-===// 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 #include "clang/Tooling/DependencyScanning/ModuleDepCollector.h" 10 11 #include "clang/Frontend/CompilerInstance.h" 12 #include "clang/Lex/Preprocessor.h" 13 #include "clang/Tooling/DependencyScanning/DependencyScanningWorker.h" 14 #include "llvm/Support/StringSaver.h" 15 16 using namespace clang; 17 using namespace tooling; 18 using namespace dependencies; 19 20 static void optimizeHeaderSearchOpts(HeaderSearchOptions &Opts, 21 ASTReader &Reader, 22 const serialization::ModuleFile &MF) { 23 // Only preserve search paths that were used during the dependency scan. 24 std::vector<HeaderSearchOptions::Entry> Entries = Opts.UserEntries; 25 Opts.UserEntries.clear(); 26 for (unsigned I = 0; I < Entries.size(); ++I) 27 if (MF.SearchPathUsage[I]) 28 Opts.UserEntries.push_back(Entries[I]); 29 } 30 31 CompilerInvocation ModuleDepCollector::makeInvocationForModuleBuildWithoutPaths( 32 const ModuleDeps &Deps, 33 llvm::function_ref<void(CompilerInvocation &)> Optimize) const { 34 // Make a deep copy of the original Clang invocation. 35 CompilerInvocation CI(OriginalInvocation); 36 37 CI.getLangOpts()->resetNonModularOptions(); 38 CI.getPreprocessorOpts().resetNonModularOptions(); 39 40 // Remove options incompatible with explicit module build. 41 CI.getFrontendOpts().Inputs.clear(); 42 CI.getFrontendOpts().OutputFile.clear(); 43 44 CI.getFrontendOpts().ProgramAction = frontend::GenerateModule; 45 CI.getLangOpts()->ModuleName = Deps.ID.ModuleName; 46 CI.getFrontendOpts().IsSystemModule = Deps.IsSystem; 47 48 CI.getLangOpts()->ImplicitModules = false; 49 50 // Report the prebuilt modules this module uses. 51 for (const auto &PrebuiltModule : Deps.PrebuiltModuleDeps) 52 CI.getFrontendOpts().ModuleFiles.push_back(PrebuiltModule.PCMFile); 53 54 Optimize(CI); 55 56 // The original invocation probably didn't have strict context hash enabled. 57 // We will use the context hash of this invocation to distinguish between 58 // multiple incompatible versions of the same module and will use it when 59 // reporting dependencies to the clients. Let's make sure we're using 60 // **strict** context hash in order to prevent accidental sharing of 61 // incompatible modules (e.g. with differences in search paths). 62 CI.getHeaderSearchOpts().ModulesStrictContextHash = true; 63 64 return CI; 65 } 66 67 static std::vector<std::string> 68 serializeCompilerInvocation(const CompilerInvocation &CI) { 69 // Set up string allocator. 70 llvm::BumpPtrAllocator Alloc; 71 llvm::StringSaver Strings(Alloc); 72 auto SA = [&Strings](const Twine &Arg) { return Strings.save(Arg).data(); }; 73 74 // Synthesize full command line from the CompilerInvocation, including "-cc1". 75 SmallVector<const char *, 32> Args{"-cc1"}; 76 CI.generateCC1CommandLine(Args, SA); 77 78 // Convert arguments to the return type. 79 return std::vector<std::string>{Args.begin(), Args.end()}; 80 } 81 82 std::vector<std::string> ModuleDeps::getCanonicalCommandLine( 83 std::function<StringRef(ModuleID)> LookupPCMPath, 84 std::function<const ModuleDeps &(ModuleID)> LookupModuleDeps) const { 85 CompilerInvocation CI(BuildInvocation); 86 FrontendOptions &FrontendOpts = CI.getFrontendOpts(); 87 88 InputKind ModuleMapInputKind(FrontendOpts.DashX.getLanguage(), 89 InputKind::Format::ModuleMap); 90 FrontendOpts.Inputs.emplace_back(ClangModuleMapFile, ModuleMapInputKind); 91 FrontendOpts.OutputFile = std::string(LookupPCMPath(ID)); 92 93 dependencies::detail::collectPCMAndModuleMapPaths( 94 ClangModuleDeps, LookupPCMPath, LookupModuleDeps, 95 FrontendOpts.ModuleFiles, FrontendOpts.ModuleMapFiles); 96 97 return serializeCompilerInvocation(CI); 98 } 99 100 std::vector<std::string> 101 ModuleDeps::getCanonicalCommandLineWithoutModulePaths() const { 102 return serializeCompilerInvocation(BuildInvocation); 103 } 104 105 void dependencies::detail::collectPCMAndModuleMapPaths( 106 llvm::ArrayRef<ModuleID> Modules, 107 std::function<StringRef(ModuleID)> LookupPCMPath, 108 std::function<const ModuleDeps &(ModuleID)> LookupModuleDeps, 109 std::vector<std::string> &PCMPaths, std::vector<std::string> &ModMapPaths) { 110 llvm::StringSet<> AlreadyAdded; 111 112 std::function<void(llvm::ArrayRef<ModuleID>)> AddArgs = 113 [&](llvm::ArrayRef<ModuleID> Modules) { 114 for (const ModuleID &MID : Modules) { 115 if (!AlreadyAdded.insert(MID.ModuleName + MID.ContextHash).second) 116 continue; 117 const ModuleDeps &M = LookupModuleDeps(MID); 118 // Depth first traversal. 119 AddArgs(M.ClangModuleDeps); 120 PCMPaths.push_back(LookupPCMPath(MID).str()); 121 if (!M.ClangModuleMapFile.empty()) 122 ModMapPaths.push_back(M.ClangModuleMapFile); 123 } 124 }; 125 126 AddArgs(Modules); 127 } 128 129 void ModuleDepCollectorPP::FileChanged(SourceLocation Loc, 130 FileChangeReason Reason, 131 SrcMgr::CharacteristicKind FileType, 132 FileID PrevFID) { 133 if (Reason != PPCallbacks::EnterFile) 134 return; 135 136 // This has to be delayed as the context hash can change at the start of 137 // `CompilerInstance::ExecuteAction`. 138 if (MDC.ContextHash.empty()) { 139 MDC.ContextHash = MDC.ScanInstance.getInvocation().getModuleHash(); 140 MDC.Consumer.handleContextHash(MDC.ContextHash); 141 } 142 143 SourceManager &SM = MDC.ScanInstance.getSourceManager(); 144 145 // Dependency generation really does want to go all the way to the 146 // file entry for a source location to find out what is depended on. 147 // We do not want #line markers to affect dependency generation! 148 if (Optional<StringRef> Filename = 149 SM.getNonBuiltinFilenameForID(SM.getFileID(SM.getExpansionLoc(Loc)))) 150 MDC.FileDeps.push_back( 151 std::string(llvm::sys::path::remove_leading_dotslash(*Filename))); 152 } 153 154 void ModuleDepCollectorPP::InclusionDirective( 155 SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, 156 bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, 157 StringRef SearchPath, StringRef RelativePath, const Module *Imported, 158 SrcMgr::CharacteristicKind FileType) { 159 if (!File && !Imported) { 160 // This is a non-modular include that HeaderSearch failed to find. Add it 161 // here as `FileChanged` will never see it. 162 MDC.FileDeps.push_back(std::string(FileName)); 163 } 164 handleImport(Imported); 165 } 166 167 void ModuleDepCollectorPP::moduleImport(SourceLocation ImportLoc, 168 ModuleIdPath Path, 169 const Module *Imported) { 170 handleImport(Imported); 171 } 172 173 void ModuleDepCollectorPP::handleImport(const Module *Imported) { 174 if (!Imported) 175 return; 176 177 const Module *TopLevelModule = Imported->getTopLevelModule(); 178 179 if (MDC.isPrebuiltModule(TopLevelModule)) 180 DirectPrebuiltModularDeps.insert(TopLevelModule); 181 else 182 DirectModularDeps.insert(TopLevelModule); 183 } 184 185 void ModuleDepCollectorPP::EndOfMainFile() { 186 FileID MainFileID = MDC.ScanInstance.getSourceManager().getMainFileID(); 187 MDC.MainFile = std::string(MDC.ScanInstance.getSourceManager() 188 .getFileEntryForID(MainFileID) 189 ->getName()); 190 191 if (!MDC.ScanInstance.getPreprocessorOpts().ImplicitPCHInclude.empty()) 192 MDC.FileDeps.push_back( 193 MDC.ScanInstance.getPreprocessorOpts().ImplicitPCHInclude); 194 195 for (const Module *M : DirectModularDeps) { 196 // A top-level module might not be actually imported as a module when 197 // -fmodule-name is used to compile a translation unit that imports this 198 // module. In that case it can be skipped. The appropriate header 199 // dependencies will still be reported as expected. 200 if (!M->getASTFile()) 201 continue; 202 handleTopLevelModule(M); 203 } 204 205 MDC.Consumer.handleDependencyOutputOpts(*MDC.Opts); 206 207 for (auto &&I : MDC.ModularDeps) 208 MDC.Consumer.handleModuleDependency(I.second); 209 210 for (auto &&I : MDC.FileDeps) 211 MDC.Consumer.handleFileDependency(I); 212 213 for (auto &&I : DirectPrebuiltModularDeps) 214 MDC.Consumer.handlePrebuiltModuleDependency(PrebuiltModuleDep{I}); 215 } 216 217 ModuleID ModuleDepCollectorPP::handleTopLevelModule(const Module *M) { 218 assert(M == M->getTopLevelModule() && "Expected top level module!"); 219 220 // If this module has been handled already, just return its ID. 221 auto ModI = MDC.ModularDeps.insert({M, ModuleDeps{}}); 222 if (!ModI.second) 223 return ModI.first->second.ID; 224 225 ModuleDeps &MD = ModI.first->second; 226 227 MD.ID.ModuleName = M->getFullModuleName(); 228 MD.ImportedByMainFile = DirectModularDeps.contains(M); 229 MD.ImplicitModulePCMPath = std::string(M->getASTFile()->getName()); 230 MD.IsSystem = M->IsSystem; 231 232 const FileEntry *ModuleMap = MDC.ScanInstance.getPreprocessor() 233 .getHeaderSearchInfo() 234 .getModuleMap() 235 .getModuleMapFileForUniquing(M); 236 MD.ClangModuleMapFile = std::string(ModuleMap ? ModuleMap->getName() : ""); 237 238 serialization::ModuleFile *MF = 239 MDC.ScanInstance.getASTReader()->getModuleManager().lookup( 240 M->getASTFile()); 241 MDC.ScanInstance.getASTReader()->visitInputFiles( 242 *MF, true, true, [&](const serialization::InputFile &IF, bool isSystem) { 243 // __inferred_module.map is the result of the way in which an implicit 244 // module build handles inferred modules. It adds an overlay VFS with 245 // this file in the proper directory and relies on the rest of Clang to 246 // handle it like normal. With explicitly built modules we don't need 247 // to play VFS tricks, so replace it with the correct module map. 248 if (IF.getFile()->getName().endswith("__inferred_module.map")) { 249 MD.FileDeps.insert(ModuleMap->getName()); 250 return; 251 } 252 MD.FileDeps.insert(IF.getFile()->getName()); 253 }); 254 255 // Add direct prebuilt module dependencies now, so that we can use them when 256 // creating a CompilerInvocation and computing context hash for this 257 // ModuleDeps instance. 258 llvm::DenseSet<const Module *> SeenModules; 259 addAllSubmodulePrebuiltDeps(M, MD, SeenModules); 260 261 MD.BuildInvocation = MDC.makeInvocationForModuleBuildWithoutPaths( 262 MD, [&](CompilerInvocation &BuildInvocation) { 263 if (MDC.OptimizeArgs) 264 optimizeHeaderSearchOpts(BuildInvocation.getHeaderSearchOpts(), 265 *MDC.ScanInstance.getASTReader(), *MF); 266 }); 267 MD.ID.ContextHash = MD.BuildInvocation.getModuleHash(); 268 269 llvm::DenseSet<const Module *> AddedModules; 270 addAllSubmoduleDeps(M, MD, AddedModules); 271 272 return MD.ID; 273 } 274 275 void ModuleDepCollectorPP::addAllSubmodulePrebuiltDeps( 276 const Module *M, ModuleDeps &MD, 277 llvm::DenseSet<const Module *> &SeenSubmodules) { 278 addModulePrebuiltDeps(M, MD, SeenSubmodules); 279 280 for (const Module *SubM : M->submodules()) 281 addAllSubmodulePrebuiltDeps(SubM, MD, SeenSubmodules); 282 } 283 284 void ModuleDepCollectorPP::addModulePrebuiltDeps( 285 const Module *M, ModuleDeps &MD, 286 llvm::DenseSet<const Module *> &SeenSubmodules) { 287 for (const Module *Import : M->Imports) 288 if (Import->getTopLevelModule() != M->getTopLevelModule()) 289 if (MDC.isPrebuiltModule(Import->getTopLevelModule())) 290 if (SeenSubmodules.insert(Import->getTopLevelModule()).second) 291 MD.PrebuiltModuleDeps.emplace_back(Import->getTopLevelModule()); 292 } 293 294 void ModuleDepCollectorPP::addAllSubmoduleDeps( 295 const Module *M, ModuleDeps &MD, 296 llvm::DenseSet<const Module *> &AddedModules) { 297 addModuleDep(M, MD, AddedModules); 298 299 for (const Module *SubM : M->submodules()) 300 addAllSubmoduleDeps(SubM, MD, AddedModules); 301 } 302 303 void ModuleDepCollectorPP::addModuleDep( 304 const Module *M, ModuleDeps &MD, 305 llvm::DenseSet<const Module *> &AddedModules) { 306 for (const Module *Import : M->Imports) { 307 if (Import->getTopLevelModule() != M->getTopLevelModule() && 308 !MDC.isPrebuiltModule(Import)) { 309 ModuleID ImportID = handleTopLevelModule(Import->getTopLevelModule()); 310 if (AddedModules.insert(Import->getTopLevelModule()).second) 311 MD.ClangModuleDeps.push_back(ImportID); 312 } 313 } 314 } 315 316 ModuleDepCollector::ModuleDepCollector( 317 std::unique_ptr<DependencyOutputOptions> Opts, 318 CompilerInstance &ScanInstance, DependencyConsumer &C, 319 CompilerInvocation &&OriginalCI, bool OptimizeArgs) 320 : ScanInstance(ScanInstance), Consumer(C), Opts(std::move(Opts)), 321 OriginalInvocation(std::move(OriginalCI)), OptimizeArgs(OptimizeArgs) {} 322 323 void ModuleDepCollector::attachToPreprocessor(Preprocessor &PP) { 324 PP.addPPCallbacks(std::make_unique<ModuleDepCollectorPP>(*this)); 325 } 326 327 void ModuleDepCollector::attachToASTReader(ASTReader &R) {} 328 329 bool ModuleDepCollector::isPrebuiltModule(const Module *M) { 330 std::string Name(M->getTopLevelModuleName()); 331 const auto &PrebuiltModuleFiles = 332 ScanInstance.getHeaderSearchOpts().PrebuiltModuleFiles; 333 auto PrebuiltModuleFileIt = PrebuiltModuleFiles.find(Name); 334 if (PrebuiltModuleFileIt == PrebuiltModuleFiles.end()) 335 return false; 336 assert("Prebuilt module came from the expected AST file" && 337 PrebuiltModuleFileIt->second == M->getASTFile()->getName()); 338 return true; 339 } 340