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 
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::getFullCommandLine(
56     std::function<StringRef(ModuleID)> LookupPCMPath,
57     std::function<const ModuleDeps &(ModuleID)> LookupModuleDeps) const {
58   CompilerInvocation CI(makeInvocationForModuleBuildWithoutPaths(*this));
59 
60   dependencies::detail::collectPCMAndModuleMapPaths(
61       ClangModuleDeps, LookupPCMPath, LookupModuleDeps,
62       CI.getFrontendOpts().ModuleFiles, CI.getFrontendOpts().ModuleMapFiles);
63 
64   return serializeCompilerInvocation(CI);
65 }
66 
67 void dependencies::detail::collectPCMAndModuleMapPaths(
68     llvm::ArrayRef<ModuleID> Modules,
69     std::function<StringRef(ModuleID)> LookupPCMPath,
70     std::function<const ModuleDeps &(ModuleID)> LookupModuleDeps,
71     std::vector<std::string> &PCMPaths, std::vector<std::string> &ModMapPaths) {
72   llvm::StringSet<> AlreadyAdded;
73 
74   std::function<void(llvm::ArrayRef<ModuleID>)> AddArgs =
75       [&](llvm::ArrayRef<ModuleID> Modules) {
76         for (const ModuleID &MID : Modules) {
77           if (!AlreadyAdded.insert(MID.ModuleName + MID.ContextHash).second)
78             continue;
79           const ModuleDeps &M = LookupModuleDeps(MID);
80           // Depth first traversal.
81           AddArgs(M.ClangModuleDeps);
82           PCMPaths.push_back(LookupPCMPath(MID).str());
83           if (!M.ClangModuleMapFile.empty())
84             ModMapPaths.push_back(M.ClangModuleMapFile);
85         }
86       };
87 
88   AddArgs(Modules);
89 }
90 
91 void ModuleDepCollectorPP::FileChanged(SourceLocation Loc,
92                                        FileChangeReason Reason,
93                                        SrcMgr::CharacteristicKind FileType,
94                                        FileID PrevFID) {
95   if (Reason != PPCallbacks::EnterFile)
96     return;
97 
98   // This has to be delayed as the context hash can change at the start of
99   // `CompilerInstance::ExecuteAction`.
100   if (MDC.ContextHash.empty()) {
101     MDC.ContextHash = Instance.getInvocation().getModuleHash();
102     MDC.Consumer.handleContextHash(MDC.ContextHash);
103   }
104 
105   SourceManager &SM = Instance.getSourceManager();
106 
107   // Dependency generation really does want to go all the way to the
108   // file entry for a source location to find out what is depended on.
109   // We do not want #line markers to affect dependency generation!
110   if (Optional<StringRef> Filename =
111           SM.getNonBuiltinFilenameForID(SM.getFileID(SM.getExpansionLoc(Loc))))
112     MDC.FileDeps.push_back(
113         std::string(llvm::sys::path::remove_leading_dotslash(*Filename)));
114 }
115 
116 void ModuleDepCollectorPP::InclusionDirective(
117     SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
118     bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
119     StringRef SearchPath, StringRef RelativePath, const Module *Imported,
120     SrcMgr::CharacteristicKind FileType) {
121   if (!File && !Imported) {
122     // This is a non-modular include that HeaderSearch failed to find. Add it
123     // here as `FileChanged` will never see it.
124     MDC.FileDeps.push_back(std::string(FileName));
125   }
126   handleImport(Imported);
127 }
128 
129 void ModuleDepCollectorPP::moduleImport(SourceLocation ImportLoc,
130                                         ModuleIdPath Path,
131                                         const Module *Imported) {
132   handleImport(Imported);
133 }
134 
135 void ModuleDepCollectorPP::handleImport(const Module *Imported) {
136   if (!Imported)
137     return;
138 
139   const Module *TopLevelModule = Imported->getTopLevelModule();
140   MDC.ModularDeps[MDC.ContextHash + TopLevelModule->getFullModuleName()]
141       .ImportedByMainFile = true;
142   DirectModularDeps.insert(TopLevelModule);
143 }
144 
145 void ModuleDepCollectorPP::EndOfMainFile() {
146   FileID MainFileID = Instance.getSourceManager().getMainFileID();
147   MDC.MainFile = std::string(
148       Instance.getSourceManager().getFileEntryForID(MainFileID)->getName());
149 
150   for (const Module *M : DirectModularDeps)
151     handleTopLevelModule(M);
152 
153   for (auto &&I : MDC.ModularDeps)
154     MDC.Consumer.handleModuleDependency(I.second);
155 
156   for (auto &&I : MDC.FileDeps)
157     MDC.Consumer.handleFileDependency(*MDC.Opts, I);
158 }
159 
160 void ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
161   assert(M == M->getTopLevelModule() && "Expected top level module!");
162 
163   auto ModI = MDC.ModularDeps.insert(
164       std::make_pair(MDC.ContextHash + M->getFullModuleName(), ModuleDeps{}));
165 
166   if (!ModI.first->second.ID.ModuleName.empty())
167     return;
168 
169   ModuleDeps &MD = ModI.first->second;
170 
171   const FileEntry *ModuleMap = Instance.getPreprocessor()
172                                    .getHeaderSearchInfo()
173                                    .getModuleMap()
174                                    .getContainingModuleMapFile(M);
175 
176   MD.Invocation = Instance.getInvocationPtr();
177   MD.ClangModuleMapFile = std::string(ModuleMap ? ModuleMap->getName() : "");
178   MD.ID.ModuleName = M->getFullModuleName();
179   MD.ImplicitModulePCMPath = std::string(M->getASTFile()->getName());
180   MD.ID.ContextHash = MDC.ContextHash;
181   MD.IsSystem = M->IsSystem;
182   serialization::ModuleFile *MF =
183       MDC.Instance.getASTReader()->getModuleManager().lookup(M->getASTFile());
184   MDC.Instance.getASTReader()->visitInputFiles(
185       *MF, true, true, [&](const serialization::InputFile &IF, bool isSystem) {
186         MD.FileDeps.insert(IF.getFile()->getName());
187       });
188 
189   llvm::DenseSet<const Module *> AddedModules;
190   addAllSubmoduleDeps(M, MD, AddedModules);
191 }
192 
193 void ModuleDepCollectorPP::addAllSubmoduleDeps(
194     const Module *M, ModuleDeps &MD,
195     llvm::DenseSet<const Module *> &AddedModules) {
196   addModuleDep(M, MD, AddedModules);
197 
198   for (const Module *SubM : M->submodules())
199     addAllSubmoduleDeps(SubM, MD, AddedModules);
200 }
201 
202 void ModuleDepCollectorPP::addModuleDep(
203     const Module *M, ModuleDeps &MD,
204     llvm::DenseSet<const Module *> &AddedModules) {
205   for (const Module *Import : M->Imports) {
206     if (Import->getTopLevelModule() != M->getTopLevelModule()) {
207       if (AddedModules.insert(Import->getTopLevelModule()).second)
208         MD.ClangModuleDeps.push_back(
209             {std::string(Import->getTopLevelModuleName()),
210              Instance.getInvocation().getModuleHash()});
211       handleTopLevelModule(Import->getTopLevelModule());
212     }
213   }
214 }
215 
216 ModuleDepCollector::ModuleDepCollector(
217     std::unique_ptr<DependencyOutputOptions> Opts, CompilerInstance &I,
218     DependencyConsumer &C)
219     : Instance(I), Consumer(C), Opts(std::move(Opts)) {}
220 
221 void ModuleDepCollector::attachToPreprocessor(Preprocessor &PP) {
222   PP.addPPCallbacks(std::make_unique<ModuleDepCollectorPP>(Instance, *this));
223 }
224 
225 void ModuleDepCollector::attachToASTReader(ASTReader &R) {}
226