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