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