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 
makeInvocationForModuleBuildWithoutPaths(const ModuleDeps & Deps) const21 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>
serializeCompilerInvocation(const CompilerInvocation & CI)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 
getCanonicalCommandLine(std::function<StringRef (ModuleID)> LookupPCMPath,std::function<const ModuleDeps & (ModuleID)> LookupModuleDeps) const62 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>
getCanonicalCommandLineWithoutModulePaths() const81 ModuleDeps::getCanonicalCommandLineWithoutModulePaths() const {
82   return serializeCompilerInvocation(Invocation);
83 }
84 
collectPCMAndModuleMapPaths(llvm::ArrayRef<ModuleID> Modules,std::function<StringRef (ModuleID)> LookupPCMPath,std::function<const ModuleDeps & (ModuleID)> LookupModuleDeps,std::vector<std::string> & PCMPaths,std::vector<std::string> & ModMapPaths)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 
FileChanged(SourceLocation Loc,FileChangeReason Reason,SrcMgr::CharacteristicKind FileType,FileID PrevFID)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 
InclusionDirective(SourceLocation HashLoc,const Token & IncludeTok,StringRef FileName,bool IsAngled,CharSourceRange FilenameRange,const FileEntry * File,StringRef SearchPath,StringRef RelativePath,const Module * Imported,SrcMgr::CharacteristicKind FileType)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 
moduleImport(SourceLocation ImportLoc,ModuleIdPath Path,const Module * Imported)147 void ModuleDepCollectorPP::moduleImport(SourceLocation ImportLoc,
148                                         ModuleIdPath Path,
149                                         const Module *Imported) {
150   handleImport(Imported);
151 }
152 
handleImport(const Module * Imported)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 
EndOfMainFile()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     // A top-level module might not be actually imported as a module when
175     // -fmodule-name is used to compile a translation unit that imports this
176     // module. In that case it can be skipped. The appropriate header
177     // dependencies will still be reported as expected.
178     if (!M->getASTFile())
179       continue;
180     handleTopLevelModule(M);
181   }
182 
183   MDC.Consumer.handleDependencyOutputOpts(*MDC.Opts);
184 
185   for (auto &&I : MDC.ModularDeps)
186     MDC.Consumer.handleModuleDependency(I.second);
187 
188   for (auto &&I : MDC.FileDeps)
189     MDC.Consumer.handleFileDependency(I);
190 
191   for (auto &&I : DirectPrebuiltModularDeps)
192     MDC.Consumer.handlePrebuiltModuleDependency(PrebuiltModuleDep{I});
193 }
194 
handleTopLevelModule(const Module * M)195 ModuleID ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
196   assert(M == M->getTopLevelModule() && "Expected top level module!");
197 
198   // If this module has been handled already, just return its ID.
199   auto ModI = MDC.ModularDeps.insert({M, ModuleDeps{}});
200   if (!ModI.second)
201     return ModI.first->second.ID;
202 
203   ModuleDeps &MD = ModI.first->second;
204 
205   MD.ID.ModuleName = M->getFullModuleName();
206   MD.ImportedByMainFile = DirectModularDeps.contains(M);
207   MD.ImplicitModulePCMPath = std::string(M->getASTFile()->getName());
208   MD.IsSystem = M->IsSystem;
209 
210   const FileEntry *ModuleMap = Instance.getPreprocessor()
211                                    .getHeaderSearchInfo()
212                                    .getModuleMap()
213                                    .getModuleMapFileForUniquing(M);
214   MD.ClangModuleMapFile = std::string(ModuleMap ? ModuleMap->getName() : "");
215 
216   serialization::ModuleFile *MF =
217       MDC.Instance.getASTReader()->getModuleManager().lookup(M->getASTFile());
218   MDC.Instance.getASTReader()->visitInputFiles(
219       *MF, true, true, [&](const serialization::InputFile &IF, bool isSystem) {
220         // __inferred_module.map is the result of the way in which an implicit
221         // module build handles inferred modules. It adds an overlay VFS with
222         // this file in the proper directory and relies on the rest of Clang to
223         // handle it like normal. With explicitly built modules we don't need
224         // to play VFS tricks, so replace it with the correct module map.
225         if (IF.getFile()->getName().endswith("__inferred_module.map")) {
226           MD.FileDeps.insert(ModuleMap->getName());
227           return;
228         }
229         MD.FileDeps.insert(IF.getFile()->getName());
230       });
231 
232   // Add direct prebuilt module dependencies now, so that we can use them when
233   // creating a CompilerInvocation and computing context hash for this
234   // ModuleDeps instance.
235   addDirectPrebuiltModuleDeps(M, MD);
236 
237   MD.Invocation = MDC.makeInvocationForModuleBuildWithoutPaths(MD);
238   MD.ID.ContextHash = MD.Invocation.getModuleHash();
239 
240   llvm::DenseSet<const Module *> AddedModules;
241   addAllSubmoduleDeps(M, MD, AddedModules);
242 
243   return MD.ID;
244 }
245 
addDirectPrebuiltModuleDeps(const Module * M,ModuleDeps & MD)246 void ModuleDepCollectorPP::addDirectPrebuiltModuleDeps(const Module *M,
247                                                        ModuleDeps &MD) {
248   for (const Module *Import : M->Imports)
249     if (Import->getTopLevelModule() != M->getTopLevelModule())
250       if (MDC.isPrebuiltModule(Import))
251         MD.PrebuiltModuleDeps.emplace_back(Import);
252 }
253 
addAllSubmoduleDeps(const Module * M,ModuleDeps & MD,llvm::DenseSet<const Module * > & AddedModules)254 void ModuleDepCollectorPP::addAllSubmoduleDeps(
255     const Module *M, ModuleDeps &MD,
256     llvm::DenseSet<const Module *> &AddedModules) {
257   addModuleDep(M, MD, AddedModules);
258 
259   for (const Module *SubM : M->submodules())
260     addAllSubmoduleDeps(SubM, MD, AddedModules);
261 }
262 
addModuleDep(const Module * M,ModuleDeps & MD,llvm::DenseSet<const Module * > & AddedModules)263 void ModuleDepCollectorPP::addModuleDep(
264     const Module *M, ModuleDeps &MD,
265     llvm::DenseSet<const Module *> &AddedModules) {
266   for (const Module *Import : M->Imports) {
267     if (Import->getTopLevelModule() != M->getTopLevelModule() &&
268         !MDC.isPrebuiltModule(Import)) {
269       ModuleID ImportID = handleTopLevelModule(Import->getTopLevelModule());
270       if (AddedModules.insert(Import->getTopLevelModule()).second)
271         MD.ClangModuleDeps.push_back(ImportID);
272     }
273   }
274 }
275 
ModuleDepCollector(std::unique_ptr<DependencyOutputOptions> Opts,CompilerInstance & I,DependencyConsumer & C,CompilerInvocation && OriginalCI)276 ModuleDepCollector::ModuleDepCollector(
277     std::unique_ptr<DependencyOutputOptions> Opts, CompilerInstance &I,
278     DependencyConsumer &C, CompilerInvocation &&OriginalCI)
279     : Instance(I), Consumer(C), Opts(std::move(Opts)),
280       OriginalInvocation(std::move(OriginalCI)) {}
281 
attachToPreprocessor(Preprocessor & PP)282 void ModuleDepCollector::attachToPreprocessor(Preprocessor &PP) {
283   PP.addPPCallbacks(std::make_unique<ModuleDepCollectorPP>(Instance, *this));
284 }
285 
attachToASTReader(ASTReader & R)286 void ModuleDepCollector::attachToASTReader(ASTReader &R) {}
287 
isPrebuiltModule(const Module * M)288 bool ModuleDepCollector::isPrebuiltModule(const Module *M) {
289   std::string Name(M->getTopLevelModuleName());
290   const auto &PrebuiltModuleFiles =
291       Instance.getHeaderSearchOpts().PrebuiltModuleFiles;
292   auto PrebuiltModuleFileIt = PrebuiltModuleFiles.find(Name);
293   if (PrebuiltModuleFileIt == PrebuiltModuleFiles.end())
294     return false;
295   assert("Prebuilt module came from the expected AST file" &&
296          PrebuiltModuleFileIt->second == M->getASTFile()->getName());
297   return true;
298 }
299