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
16using namespace clang;
17using namespace tooling;
18using namespace dependencies;
19
20std::vector<std::string> ModuleDeps::getFullCommandLine(
21    std::function<StringRef(ClangModuleDep)> LookupPCMPath,
22    std::function<const ModuleDeps &(ClangModuleDep)> LookupModuleDeps) const {
23  std::vector<std::string> Ret = NonPathCommandLine;
24
25  // TODO: Build full command line. That also means capturing the original
26  //       command line into NonPathCommandLine.
27
28  dependencies::detail::appendCommonModuleArguments(
29      ClangModuleDeps, LookupPCMPath, LookupModuleDeps, Ret);
30
31  return Ret;
32}
33
34void dependencies::detail::appendCommonModuleArguments(
35    llvm::ArrayRef<ClangModuleDep> Modules,
36    std::function<StringRef(ClangModuleDep)> LookupPCMPath,
37    std::function<const ModuleDeps &(ClangModuleDep)> LookupModuleDeps,
38    std::vector<std::string> &Result) {
39  llvm::StringSet<> AlreadyAdded;
40
41  std::function<void(llvm::ArrayRef<ClangModuleDep>)> AddArgs =
42      [&](llvm::ArrayRef<ClangModuleDep> Modules) {
43        for (const ClangModuleDep &CMD : Modules) {
44          if (!AlreadyAdded.insert(CMD.ModuleName + CMD.ContextHash).second)
45            continue;
46          const ModuleDeps &M = LookupModuleDeps(CMD);
47          // Depth first traversal.
48          AddArgs(M.ClangModuleDeps);
49          Result.push_back(("-fmodule-file=" + LookupPCMPath(CMD)).str());
50          if (!M.ClangModuleMapFile.empty()) {
51            Result.push_back("-fmodule-map-file=" + M.ClangModuleMapFile);
52          }
53        }
54      };
55
56  Result.push_back("-fno-implicit-modules");
57  Result.push_back("-fno-implicit-module-maps");
58  AddArgs(Modules);
59}
60
61void ModuleDepCollectorPP::FileChanged(SourceLocation Loc,
62                                       FileChangeReason Reason,
63                                       SrcMgr::CharacteristicKind FileType,
64                                       FileID PrevFID) {
65  if (Reason != PPCallbacks::EnterFile)
66    return;
67
68  // This has to be delayed as the context hash can change at the start of
69  // `CompilerInstance::ExecuteAction`.
70  if (MDC.ContextHash.empty()) {
71    MDC.ContextHash = Instance.getInvocation().getModuleHash();
72    MDC.Consumer.handleContextHash(MDC.ContextHash);
73  }
74
75  SourceManager &SM = Instance.getSourceManager();
76
77  // Dependency generation really does want to go all the way to the
78  // file entry for a source location to find out what is depended on.
79  // We do not want #line markers to affect dependency generation!
80  Optional<FileEntryRef> File =
81      SM.getFileEntryRefForID(SM.getFileID(SM.getExpansionLoc(Loc)));
82  if (!File)
83    return;
84
85  StringRef FileName =
86      llvm::sys::path::remove_leading_dotslash(File->getName());
87
88  MDC.MainDeps.push_back(std::string(FileName));
89}
90
91void ModuleDepCollectorPP::InclusionDirective(
92    SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName,
93    bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File,
94    StringRef SearchPath, StringRef RelativePath, const Module *Imported,
95    SrcMgr::CharacteristicKind FileType) {
96  if (!File && !Imported) {
97    // This is a non-modular include that HeaderSearch failed to find. Add it
98    // here as `FileChanged` will never see it.
99    MDC.MainDeps.push_back(std::string(FileName));
100  }
101  handleImport(Imported);
102}
103
104void ModuleDepCollectorPP::moduleImport(SourceLocation ImportLoc,
105                                        ModuleIdPath Path,
106                                        const Module *Imported) {
107  handleImport(Imported);
108}
109
110void ModuleDepCollectorPP::handleImport(const Module *Imported) {
111  if (!Imported)
112    return;
113
114  MDC.Deps[MDC.ContextHash + Imported->getTopLevelModule()->getFullModuleName()]
115      .ImportedByMainFile = true;
116  DirectDeps.insert(Imported->getTopLevelModule());
117}
118
119void ModuleDepCollectorPP::EndOfMainFile() {
120  FileID MainFileID = Instance.getSourceManager().getMainFileID();
121  MDC.MainFile = std::string(
122      Instance.getSourceManager().getFileEntryForID(MainFileID)->getName());
123
124  for (const Module *M : DirectDeps) {
125    handleTopLevelModule(M);
126  }
127
128  for (auto &&I : MDC.Deps)
129    MDC.Consumer.handleModuleDependency(I.second);
130
131  for (auto &&I : MDC.MainDeps)
132    MDC.Consumer.handleFileDependency(*MDC.Opts, I);
133}
134
135void ModuleDepCollectorPP::handleTopLevelModule(const Module *M) {
136  assert(M == M->getTopLevelModule() && "Expected top level module!");
137
138  auto ModI = MDC.Deps.insert(
139      std::make_pair(MDC.ContextHash + M->getFullModuleName(), ModuleDeps{}));
140
141  if (!ModI.first->second.ModuleName.empty())
142    return;
143
144  ModuleDeps &MD = ModI.first->second;
145
146  const FileEntry *ModuleMap = Instance.getPreprocessor()
147                                   .getHeaderSearchInfo()
148                                   .getModuleMap()
149                                   .getContainingModuleMapFile(M);
150
151  MD.ClangModuleMapFile = std::string(ModuleMap ? ModuleMap->getName() : "");
152  MD.ModuleName = M->getFullModuleName();
153  MD.ImplicitModulePCMPath = std::string(M->getASTFile()->getName());
154  MD.ContextHash = MDC.ContextHash;
155  serialization::ModuleFile *MF =
156      MDC.Instance.getASTReader()->getModuleManager().lookup(M->getASTFile());
157  MDC.Instance.getASTReader()->visitInputFiles(
158      *MF, true, true, [&](const serialization::InputFile &IF, bool isSystem) {
159        MD.FileDeps.insert(IF.getFile()->getName());
160      });
161
162  llvm::DenseSet<const Module *> AddedModules;
163  addAllSubmoduleDeps(M, MD, AddedModules);
164}
165
166void ModuleDepCollectorPP::addAllSubmoduleDeps(
167    const Module *M, ModuleDeps &MD,
168    llvm::DenseSet<const Module *> &AddedModules) {
169  addModuleDep(M, MD, AddedModules);
170
171  for (const Module *SubM : M->submodules())
172    addAllSubmoduleDeps(SubM, MD, AddedModules);
173}
174
175void ModuleDepCollectorPP::addModuleDep(
176    const Module *M, ModuleDeps &MD,
177    llvm::DenseSet<const Module *> &AddedModules) {
178  for (const Module *Import : M->Imports) {
179    if (Import->getTopLevelModule() != M->getTopLevelModule()) {
180      if (AddedModules.insert(Import->getTopLevelModule()).second)
181        MD.ClangModuleDeps.push_back(
182            {std::string(Import->getTopLevelModuleName()),
183             Instance.getInvocation().getModuleHash()});
184      handleTopLevelModule(Import->getTopLevelModule());
185    }
186  }
187}
188
189ModuleDepCollector::ModuleDepCollector(
190    std::unique_ptr<DependencyOutputOptions> Opts, CompilerInstance &I,
191    DependencyConsumer &C)
192    : Instance(I), Consumer(C), Opts(std::move(Opts)) {}
193
194void ModuleDepCollector::attachToPreprocessor(Preprocessor &PP) {
195  PP.addPPCallbacks(std::make_unique<ModuleDepCollectorPP>(Instance, *this));
196}
197
198void ModuleDepCollector::attachToASTReader(ASTReader &R) {}
199