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