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