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