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