xref: /llvm-project/clang-tools-extra/modularize/ModuleAssistant.cpp (revision bca3c9cb2bc177022f17422671790a0d5e701bc4)
1 //===--- ModuleAssistant.cpp - Module map generation manager --*- 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 // This file defines the module generation entry point function,
11 // createModuleMap, a Module class for representing a module,
12 // and various implementation functions for doing the underlying
13 // work, described below.
14 //
15 // The "Module" class represents a module, with members for storing the module
16 // name, associated header file names, and sub-modules, and an "output"
17 // function that recursively writes the module definitions.
18 //
19 // The "createModuleMap" function implements the top-level logic of the
20 // assistant mode.  It calls a loadModuleDescriptions function to walk
21 // the header list passed to it and creates a tree of Module objects
22 // representing the module hierarchy, represented by a "Module" object,
23 // the "RootModule".  This root module may or may not represent an actual
24 // module in the module map, depending on the "--root-module" option passed
25 // to modularize.  It then calls a writeModuleMap function to set up the
26 // module map file output and walk the module tree, outputting the module
27 // map file using a stream obtained and managed by an
28 // llvm::ToolOutputFile object.
29 //
30 //===----------------------------------------------------------------------===//
31 
32 #include "Modularize.h"
33 #include "llvm/ADT/SmallString.h"
34 #include "llvm/Support/FileSystem.h"
35 #include "llvm/Support/Path.h"
36 #include "llvm/Support/ToolOutputFile.h"
37 #include <vector>
38 
39 // Local definitions:
40 
41 namespace {
42 
43 // Internal class definitions:
44 
45 // Represents a module.
46 class Module {
47 public:
48   Module(llvm::StringRef Name, bool Problem);
49   ~Module();
50   bool output(llvm::raw_fd_ostream &OS, int Indent);
51   Module *findSubModule(llvm::StringRef SubName);
52 
53 public:
54   std::string Name;
55   std::vector<std::string> HeaderFileNames;
56   std::vector<Module *> SubModules;
57   bool IsProblem;
58 };
59 
60 } // end anonymous namespace.
61 
62 // Module functions:
63 
64 // Constructors.
65 Module::Module(llvm::StringRef Name, bool Problem)
66   : Name(Name), IsProblem(Problem) {}
67 
68 // Destructor.
69 Module::~Module() {
70   // Free submodules.
71   while (!SubModules.empty()) {
72     Module *last = SubModules.back();
73     SubModules.pop_back();
74     delete last;
75   }
76 }
77 
78 // Write a module hierarchy to the given output stream.
79 bool Module::output(llvm::raw_fd_ostream &OS, int Indent) {
80   // If this is not the nameless root module, start a module definition.
81   if (Name.size() != 0) {
82     OS.indent(Indent);
83     OS << "module " << Name << " {\n";
84     Indent += 2;
85   }
86 
87   // Output submodules.
88   for (auto I = SubModules.begin(), E = SubModules.end(); I != E; ++I) {
89     if (!(*I)->output(OS, Indent))
90       return false;
91   }
92 
93   // Output header files.
94   for (auto I = HeaderFileNames.begin(), E = HeaderFileNames.end(); I != E;
95        ++I) {
96     OS.indent(Indent);
97     if (IsProblem || strstr((*I).c_str(), ".inl"))
98       OS << "exclude header \"" << *I << "\"\n";
99     else
100       OS << "header \"" << *I << "\"\n";
101   }
102 
103   // If this module has header files, output export directive.
104   if (HeaderFileNames.size() != 0) {
105     OS.indent(Indent);
106     OS << "export *\n";
107   }
108 
109   // If this is not the nameless root module, close the module definition.
110   if (Name.size() != 0) {
111     Indent -= 2;
112     OS.indent(Indent);
113     OS << "}\n";
114   }
115 
116   return true;
117 }
118 
119 // Lookup a sub-module.
120 Module *Module::findSubModule(llvm::StringRef SubName) {
121   for (auto I = SubModules.begin(), E = SubModules.end(); I != E; ++I) {
122     if ((*I)->Name == SubName)
123       return *I;
124   }
125   return nullptr;
126 }
127 
128 // Implementation functions:
129 
130 // Reserved keywords in module.modulemap syntax.
131 // Keep in sync with keywords in module map parser in Lex/ModuleMap.cpp,
132 // such as in ModuleMapParser::consumeToken().
133 static const char *const ReservedNames[] = {
134   "config_macros", "export",   "module", "conflict", "framework",
135   "requires",      "exclude",  "header", "private",  "explicit",
136   "link",          "umbrella", "extern", "use",      nullptr // Flag end.
137 };
138 
139 // Convert module name to a non-keyword.
140 // Prepends a '_' to the name if and only if the name is a keyword.
141 static std::string
142 ensureNoCollisionWithReservedName(llvm::StringRef MightBeReservedName) {
143   std::string SafeName = MightBeReservedName;
144   for (int Index = 0; ReservedNames[Index] != nullptr; ++Index) {
145     if (MightBeReservedName == ReservedNames[Index]) {
146       SafeName.insert(0, "_");
147       break;
148     }
149   }
150   return SafeName;
151 }
152 
153 // Convert module name to a non-keyword.
154 // Prepends a '_' to the name if and only if the name is a keyword.
155 static std::string
156 ensureVaidModuleName(llvm::StringRef MightBeInvalidName) {
157   std::string SafeName = MightBeInvalidName;
158   std::replace(SafeName.begin(), SafeName.end(), '-', '_');
159   std::replace(SafeName.begin(), SafeName.end(), '.', '_');
160   if (isdigit(SafeName[0]))
161     SafeName = "_" + SafeName;
162   return SafeName;
163 }
164 
165 // Add one module, given a header file path.
166 static bool addModuleDescription(Module *RootModule,
167                                  llvm::StringRef HeaderFilePath,
168                                  llvm::StringRef HeaderPrefix,
169                                  DependencyMap &Dependencies,
170                                  bool IsProblemFile) {
171   Module *CurrentModule = RootModule;
172   DependentsVector &FileDependents = Dependencies[HeaderFilePath];
173   std::string FilePath;
174   // Strip prefix.
175   // HeaderFilePath should be compared to natively-canonicalized Prefix.
176   llvm::SmallString<256> NativePath, NativePrefix;
177   llvm::sys::path::native(HeaderFilePath, NativePath);
178   llvm::sys::path::native(HeaderPrefix, NativePrefix);
179   if (NativePath.startswith(NativePrefix))
180     FilePath = NativePath.substr(NativePrefix.size() + 1);
181   else
182     FilePath = HeaderFilePath;
183   int Count = FileDependents.size();
184   // Headers that go into modules must not depend on other files being
185   // included first.  If there are any dependents, warn user and omit.
186   if (Count != 0) {
187     llvm::errs() << "warning: " << FilePath
188                  << " depends on other headers being included first,"
189                     " meaning the module.modulemap won't compile."
190                     "  This header will be omitted from the module map.\n";
191     return true;
192   }
193   // Make canonical.
194   std::replace(FilePath.begin(), FilePath.end(), '\\', '/');
195   // Insert module into tree, using subdirectories as submodules.
196   for (llvm::sys::path::const_iterator I = llvm::sys::path::begin(FilePath),
197                                        E = llvm::sys::path::end(FilePath);
198        I != E; ++I) {
199     if ((*I)[0] == '.')
200       continue;
201     std::string Stem = llvm::sys::path::stem(*I);
202     Stem = ensureNoCollisionWithReservedName(Stem);
203     Stem = ensureVaidModuleName(Stem);
204     Module *SubModule = CurrentModule->findSubModule(Stem);
205     if (!SubModule) {
206       SubModule = new Module(Stem, IsProblemFile);
207       CurrentModule->SubModules.push_back(SubModule);
208     }
209     CurrentModule = SubModule;
210   }
211   // Add header file name to headers.
212   CurrentModule->HeaderFileNames.push_back(FilePath);
213   return true;
214 }
215 
216 // Create the internal module tree representation.
217 static Module *loadModuleDescriptions(
218     llvm::StringRef RootModuleName, llvm::ArrayRef<std::string> HeaderFileNames,
219     llvm::ArrayRef<std::string> ProblemFileNames,
220     DependencyMap &Dependencies, llvm::StringRef HeaderPrefix) {
221 
222   // Create root module.
223   auto *RootModule = new Module(RootModuleName, false);
224 
225   llvm::SmallString<256> CurrentDirectory;
226   llvm::sys::fs::current_path(CurrentDirectory);
227 
228   // If no header prefix, use current directory.
229   if (HeaderPrefix.size() == 0)
230     HeaderPrefix = CurrentDirectory;
231 
232   // Walk the header file names and output the module map.
233   for (llvm::ArrayRef<std::string>::iterator I = HeaderFileNames.begin(),
234                                              E = HeaderFileNames.end();
235        I != E; ++I) {
236     std::string Header(*I);
237     bool IsProblemFile = false;
238     for (auto &ProblemFile : ProblemFileNames) {
239       if (ProblemFile == Header) {
240         IsProblemFile = true;
241         break;
242       }
243     }
244     // Add as a module.
245     if (!addModuleDescription(RootModule, Header, HeaderPrefix, Dependencies, IsProblemFile))
246       return nullptr;
247   }
248 
249   return RootModule;
250 }
251 
252 // Kick off the writing of the module map.
253 static bool writeModuleMap(llvm::StringRef ModuleMapPath,
254                            llvm::StringRef HeaderPrefix, Module *RootModule) {
255   llvm::SmallString<256> HeaderDirectory(ModuleMapPath);
256   llvm::sys::path::remove_filename(HeaderDirectory);
257   llvm::SmallString<256> FilePath;
258 
259   // Get the module map file path to be used.
260   if ((HeaderDirectory.size() == 0) && (HeaderPrefix.size() != 0)) {
261     FilePath = HeaderPrefix;
262     // Prepend header file name prefix if it's not absolute.
263     llvm::sys::path::append(FilePath, ModuleMapPath);
264     llvm::sys::path::native(FilePath);
265   } else {
266     FilePath = ModuleMapPath;
267     llvm::sys::path::native(FilePath);
268   }
269 
270   // Set up module map output file.
271   std::error_code EC;
272   llvm::ToolOutputFile Out(FilePath, EC, llvm::sys::fs::F_Text);
273   if (EC) {
274     llvm::errs() << Argv0 << ": error opening " << FilePath << ":"
275                  << EC.message() << "\n";
276     return false;
277   }
278 
279   // Get output stream from tool output buffer/manager.
280   llvm::raw_fd_ostream &OS = Out.os();
281 
282   // Output file comment.
283   OS << "// " << ModuleMapPath << "\n";
284   OS << "// Generated by: " << CommandLine << "\n\n";
285 
286   // Write module hierarchy from internal representation.
287   if (!RootModule->output(OS, 0))
288     return false;
289 
290   // Tell ToolOutputFile that we want to keep the file.
291   Out.keep();
292 
293   return true;
294 }
295 
296 // Global functions:
297 
298 // Module map generation entry point.
299 bool createModuleMap(llvm::StringRef ModuleMapPath,
300                      llvm::ArrayRef<std::string> HeaderFileNames,
301                      llvm::ArrayRef<std::string> ProblemFileNames,
302                      DependencyMap &Dependencies, llvm::StringRef HeaderPrefix,
303                      llvm::StringRef RootModuleName) {
304   // Load internal representation of modules.
305   std::unique_ptr<Module> RootModule(
306     loadModuleDescriptions(
307       RootModuleName, HeaderFileNames, ProblemFileNames, Dependencies,
308       HeaderPrefix));
309   if (!RootModule.get())
310     return false;
311 
312   // Write module map file.
313   return writeModuleMap(ModuleMapPath, HeaderPrefix, RootModule.get());
314 }
315