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