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   // Make a deep copy of the invocation.
24   CompilerInvocation CI(*Deps.Invocation);
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   CI.getHeaderSearchOpts().ImplicitModuleMaps = false;
36 
37   return CI;
38 }
39 
40 static std::vector<std::string>
41 serializeCompilerInvocation(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   SmallVector<const char *, 32> Args;
47 
48   // Synthesize full command line from the CompilerInvocation.
49   CI.generateCC1CommandLine(Args, SA);
50 
51   // Convert arguments to the return type.
52   std::vector<std::string> Ret;
53   Ret.reserve(Args.size());
54   for (const char *Arg : Args)
55     Ret.emplace_back(Arg);
56 
57   return Ret;
58 }
59 
60 std::vector<std::string> ModuleDeps::getFullCommandLine(
61     std::function<StringRef(ModuleID)> LookupPCMPath,
62     std::function<const ModuleDeps &(ModuleID)> LookupModuleDeps) const {
63   CompilerInvocation CI(makeInvocationForModuleBuildWithoutPaths(*this));
64 
65   dependencies::detail::collectPCMAndModuleMapPaths(
66       ClangModuleDeps, LookupPCMPath, LookupModuleDeps,
67       CI.getFrontendOpts().ModuleFiles, CI.getFrontendOpts().ModuleMapFiles);
68 
69   return serializeCompilerInvocation(CI);
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   MDC.ModularDeps[MDC.ContextHash + TopLevelModule->getFullModuleName()]
146       .ImportedByMainFile = true;
147   DirectModularDeps.insert(TopLevelModule);
148 }
149 
150 void ModuleDepCollectorPP::EndOfMainFile() {
151   FileID MainFileID = Instance.getSourceManager().getMainFileID();
152   MDC.MainFile = std::string(
153       Instance.getSourceManager().getFileEntryForID(MainFileID)->getName());
154 
155   for (const Module *M : DirectModularDeps)
156     handleTopLevelModule(M);
157 
158   for (auto &&I : MDC.ModularDeps)
159     MDC.Consumer.handleModuleDependency(I.second);
160 
161   for (auto &&I : MDC.FileDeps)
162     MDC.Consumer.handleFileDependency(*MDC.Opts, I);
163 }
164 
165 void ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
166   assert(M == M->getTopLevelModule() && "Expected top level module!");
167 
168   auto ModI = MDC.ModularDeps.insert(
169       std::make_pair(MDC.ContextHash + M->getFullModuleName(), ModuleDeps{}));
170 
171   if (!ModI.first->second.ID.ModuleName.empty())
172     return;
173 
174   ModuleDeps &MD = ModI.first->second;
175 
176   const FileEntry *ModuleMap = Instance.getPreprocessor()
177                                    .getHeaderSearchInfo()
178                                    .getModuleMap()
179                                    .getContainingModuleMapFile(M);
180 
181   MD.Invocation = Instance.getInvocationPtr();
182   MD.ClangModuleMapFile = std::string(ModuleMap ? ModuleMap->getName() : "");
183   MD.ID.ModuleName = M->getFullModuleName();
184   MD.ImplicitModulePCMPath = std::string(M->getASTFile()->getName());
185   MD.ID.ContextHash = MDC.ContextHash;
186   MD.IsSystem = M->IsSystem;
187   serialization::ModuleFile *MF =
188       MDC.Instance.getASTReader()->getModuleManager().lookup(M->getASTFile());
189   MDC.Instance.getASTReader()->visitInputFiles(
190       *MF, true, true, [&](const serialization::InputFile &IF, bool isSystem) {
191         MD.FileDeps.insert(IF.getFile()->getName());
192       });
193 
194   llvm::DenseSet<const Module *> AddedModules;
195   addAllSubmoduleDeps(M, MD, AddedModules);
196 }
197 
198 void ModuleDepCollectorPP::addAllSubmoduleDeps(
199     const Module *M, ModuleDeps &MD,
200     llvm::DenseSet<const Module *> &AddedModules) {
201   addModuleDep(M, MD, AddedModules);
202 
203   for (const Module *SubM : M->submodules())
204     addAllSubmoduleDeps(SubM, MD, AddedModules);
205 }
206 
207 void ModuleDepCollectorPP::addModuleDep(
208     const Module *M, ModuleDeps &MD,
209     llvm::DenseSet<const Module *> &AddedModules) {
210   for (const Module *Import : M->Imports) {
211     if (Import->getTopLevelModule() != M->getTopLevelModule()) {
212       if (AddedModules.insert(Import->getTopLevelModule()).second)
213         MD.ClangModuleDeps.push_back(
214             {std::string(Import->getTopLevelModuleName()),
215              Instance.getInvocation().getModuleHash()});
216       handleTopLevelModule(Import->getTopLevelModule());
217     }
218   }
219 }
220 
221 ModuleDepCollector::ModuleDepCollector(
222     std::unique_ptr<DependencyOutputOptions> Opts, CompilerInstance &I,
223     DependencyConsumer &C)
224     : Instance(I), Consumer(C), Opts(std::move(Opts)) {}
225 
226 void ModuleDepCollector::attachToPreprocessor(Preprocessor &PP) {
227   PP.addPPCallbacks(std::make_unique<ModuleDepCollectorPP>(Instance, *this));
228 }
229 
230 void ModuleDepCollector::attachToASTReader(ASTReader &R) {}
231