clang-tools 3.8.0

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

ModuleAssistant.cpp
Go to the documentation of this file.
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)
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 // Add one module, given a header file path.
161 static bool addModuleDescription(Module *RootModule,
162 llvm::StringRef HeaderFilePath,
163 llvm::StringRef HeaderPrefix,
164 DependencyMap &Dependencies,
165 bool IsProblemFile) {
166 Module *CurrentModule = RootModule;
167 DependentsVector &FileDependents = Dependencies[HeaderFilePath];
168 std::string FilePath;
169 // Strip prefix.
170 // HeaderFilePath should be compared to natively-canonicalized Prefix.
171 llvm::SmallString<256> NativePath, NativePrefix;
172 llvm::sys::path::native(HeaderFilePath, NativePath);
173 llvm::sys::path::native(HeaderPrefix, NativePrefix);
174 if (NativePath.startswith(NativePrefix))
175 FilePath = NativePath.substr(NativePrefix.size() + 1);
176 else
177 FilePath = HeaderFilePath;
178 int Count = FileDependents.size();
179 // Headers that go into modules must not depend on other files being
180 // included first. If there are any dependents, warn user and omit.
181 if (Count != 0) {
182 llvm::errs() << "warning: " << FilePath
183 << " depends on other headers being included first,"
184 " meaning the module.modulemap won't compile."
185 " This header will be omitted from the module map.\n";
186 return true;
187 }
188 // Make canonical.
189 std::replace(FilePath.begin(), FilePath.end(), '\\', '/');
190 // Insert module into tree, using subdirectories as submodules.
191 for (llvm::sys::path::const_iterator I = llvm::sys::path::begin(FilePath),
192 E = llvm::sys::path::end(FilePath);
193 I != E; ++I) {
194 if ((*I)[0] == '.')
195 continue;
196 std::string Stem = llvm::sys::path::stem(*I);
197 Stem = ensureNoCollisionWithReservedName(Stem);
198 Module *SubModule = CurrentModule->findSubModule(Stem);
199 if (!SubModule) {
200 SubModule = new Module(Stem, IsProblemFile);
201 CurrentModule->SubModules.push_back(SubModule);
202 }
203 CurrentModule = SubModule;
204 }
205 // Add header file name to headers.
206 CurrentModule->HeaderFileNames.push_back(FilePath);
207 return true;
208 }
209
210 // Create the internal module tree representation.
211 static Module *loadModuleDescriptions(
212 llvm::StringRef RootModuleName, llvm::ArrayRef<std::string> HeaderFileNames,
213 llvm::ArrayRef<std::string> ProblemFileNames,
214 DependencyMap &Dependencies, llvm::StringRef HeaderPrefix) {
215
216 // Create root module.
217 Module *RootModule = new Module(RootModuleName, false);
218
219 llvm::SmallString<256> CurrentDirectory;
220 llvm::sys::fs::current_path(CurrentDirectory);
221
222 // If no header prefix, use current directory.
223 if (HeaderPrefix.size() == 0)
224 HeaderPrefix = CurrentDirectory;
225
226 // Walk the header file names and output the module map.
227 for (llvm::ArrayRef<std::string>::iterator I = HeaderFileNames.begin(),
228 E = HeaderFileNames.end();
229 I != E; ++I) {
230 std::string Header(*I);
231 bool IsProblemFile = false;
232 for (auto &ProblemFile : ProblemFileNames) {
233 if (ProblemFile == Header) {
234 IsProblemFile = true;
235 break;
236 }
237 }
238 // Add as a module.
239 if (!addModuleDescription(RootModule, Header, HeaderPrefix, Dependencies, IsProblemFile))
240 return nullptr;
241 }
242
243 return RootModule;
244 }
245
246 // Kick off the writing of the module map.
247 static bool writeModuleMap(llvm::StringRef ModuleMapPath,
248 llvm::StringRef HeaderPrefix, Module *RootModule) {
249 llvm::SmallString<256> HeaderDirectory(ModuleMapPath);
250 llvm::sys::path::remove_filename(HeaderDirectory);
251 llvm::SmallString<256> FilePath;
252
253 // Get the module map file path to be used.
254 if ((HeaderDirectory.size() == 0) && (HeaderPrefix.size() != 0)) {
255 FilePath = HeaderPrefix;
256 // Prepend header file name prefix if it's not absolute.
257 llvm::sys::path::append(FilePath, ModuleMapPath);
258 llvm::sys::path::native(FilePath);
259 } else {
260 FilePath = ModuleMapPath;
261 llvm::sys::path::native(FilePath);
262 }
263
264 // Set up module map output file.
265 std::error_code EC;
266 llvm::tool_output_file Out(FilePath, EC, llvm::sys::fs::F_Text);
267 if (EC) {
268 llvm::errs() << Argv0 << ": error opening " << FilePath << ":"
269 << EC.message() << "\n";
270 return false;
271 }
272
273 // Get output stream from tool output buffer/manager.
274 llvm::raw_fd_ostream &OS = Out.os();
275
276 // Output file comment.
277 OS << "// " << ModuleMapPath << "\n";
278 OS << "// Generated by: " << CommandLine << "\n\n";
279
280 // Write module hierarchy from internal representation.
281 if (!RootModule->output(OS, 0))
282 return false;
283
284 // Tell tool_output_file that we want to keep the file.
285 Out.keep();
286
287 return true;
288 }
289
290 // Global functions:
291
292 // Module map generation entry point.
293 bool createModuleMap(llvm::StringRef ModuleMapPath,
294 llvm::ArrayRef<std::string> HeaderFileNames,
295 llvm::ArrayRef<std::string> ProblemFileNames,
296 DependencyMap &Dependencies, llvm::StringRef HeaderPrefix,
297 llvm::StringRef RootModuleName) {
298 // Load internal representation of modules.
299 std::unique_ptr<Module> RootModule(
300 loadModuleDescriptions(
301 RootModuleName, HeaderFileNames, ProblemFileNames, Dependencies,
302 HeaderPrefix));
303 if (!RootModule.get())
304 return false;
305
306 // Write module map file.
307 return writeModuleMap(ModuleMapPath, HeaderPrefix, RootModule.get());
308 }
Modularize.h
Common definitions for Modularize.
addModuleDescription
static bool addModuleDescription(Module *RootModule, llvm::StringRef HeaderFilePath, llvm::StringRef HeaderPrefix, DependencyMap &Dependencies, bool IsProblemFile)
Definition: ModuleAssistant.cpp:161
Name
StringHandle Name
Definition: PreprocessorTracker.cpp:525
DependentsVector
llvm::SmallVector< std::string, 4 > DependentsVector
Definition: Modularize.h:32
Argv0
const char * Argv0
Definition: Modularize.cpp:334
ReservedNames
static const char *const ReservedNames[]
Definition: ModuleAssistant.cpp:140
writeModuleMap
static bool writeModuleMap(llvm::StringRef ModuleMapPath, llvm::StringRef HeaderPrefix, Module *RootModule)
Definition: ModuleAssistant.cpp:247
HeaderPrefix
static cl::opt< std::string > HeaderPrefix("prefix", cl::init(""), cl::desc("Prepend header file paths with this prefix."" If not specified,"" the files are considered to be relative to the header list file."))
loadModuleDescriptions
static Module * loadModuleDescriptions(llvm::StringRef RootModuleName, llvm::ArrayRef< std::string > HeaderFileNames, llvm::ArrayRef< std::string > ProblemFileNames, DependencyMap &Dependencies, llvm::StringRef HeaderPrefix)
Definition: ModuleAssistant.cpp:211
ModuleMapPath
static cl::opt< std::string > ModuleMapPath("module-map-path", cl::init(""), cl::desc("Turn on module map output and specify output path or file name."" If no path is specified and if prefix option is specified,"" use prefix for file path."))
CommandLine
std::string CommandLine
Definition: Modularize.cpp:336
DependencyMap
llvm::StringMap< DependentsVector > DependencyMap
Definition: Modularize.h:33
createModuleMap
bool createModuleMap(llvm::StringRef ModuleMapPath, llvm::ArrayRef< std::string > HeaderFileNames, llvm::ArrayRef< std::string > ProblemFileNames, DependencyMap &Dependencies, llvm::StringRef HeaderPrefix, llvm::StringRef RootModuleName)
Create the module map file.
Definition: ModuleAssistant.cpp:293
RootModule
static cl::opt< std::string > RootModule("root-module", cl::init(""), cl::desc("Specify the name of the root module."))
ensureNoCollisionWithReservedName
static std::string ensureNoCollisionWithReservedName(llvm::StringRef MightBeReservedName)
Definition: ModuleAssistant.cpp:149