32 #include "llvm/ADT/SmallString.h"
33 #include "llvm/Support/FileSystem.h"
34 #include "llvm/Support/Path.h"
35 #include "llvm/Support/ToolOutputFile.h"
49 bool output(llvm::raw_fd_ostream &
OS,
int Indent);
50 Module *findSubModule(llvm::StringRef SubName);
54 std::vector<std::string> HeaderFileNames;
55 std::vector<Module *> SubModules;
64 Module::Module(llvm::StringRef
Name,
bool Problem)
70 while (!SubModules.empty()) {
71 Module *last = SubModules.back();
72 SubModules.pop_back();
78 bool Module::output(llvm::raw_fd_ostream &
OS,
int Indent) {
80 if (
Name.size() != 0) {
82 OS <<
"module " <<
Name <<
" {\n";
87 for (
auto I = SubModules.begin(),
E = SubModules.end(); I !=
E; ++I) {
88 if (!(*I)->output(
OS, Indent))
93 for (
auto I = HeaderFileNames.begin(),
E = HeaderFileNames.end(); I !=
E;
96 if (IsProblem || strstr((*I).c_str(),
".inl"))
97 OS <<
"exclude header \"" << *I <<
"\"\n";
99 OS <<
"header \"" << *I <<
"\"\n";
103 if (HeaderFileNames.size() != 0) {
109 if (
Name.size() != 0) {
119 Module *Module::findSubModule(llvm::StringRef SubName) {
120 for (
auto I = SubModules.begin(),
E = SubModules.end(); I !=
E; ++I) {
121 if ((*I)->Name == SubName)
133 "config_macros",
"export",
"module",
"conflict",
"framework",
134 "requires",
"exclude",
"header",
"private",
"explicit",
135 "link",
"umbrella",
"extern",
"use",
nullptr
142 std::string SafeName(MightBeReservedName);
145 SafeName.insert(0,
"_");
156 std::string SafeName(MightBeInvalidName);
157 std::replace(SafeName.begin(), SafeName.end(),
'-',
'_');
158 std::replace(SafeName.begin(), SafeName.end(),
'.',
'_');
159 if (isdigit(SafeName[0]))
160 SafeName =
"_" + SafeName;
166 llvm::StringRef HeaderFilePath,
169 bool IsProblemFile) {
172 std::string FilePath;
175 llvm::SmallString<256> NativePath, NativePrefix;
176 llvm::sys::path::native(HeaderFilePath, NativePath);
178 if (NativePath.startswith(NativePrefix))
179 FilePath = std::string(NativePath.substr(NativePrefix.size() + 1));
181 FilePath = std::string(HeaderFilePath);
182 int Count = FileDependents.size();
186 llvm::errs() <<
"warning: " << FilePath
187 <<
" depends on other headers being included first,"
188 " meaning the module.modulemap won't compile."
189 " This header will be omitted from the module map.\n";
193 std::replace(FilePath.begin(), FilePath.end(),
'\\',
'/');
195 for (llvm::sys::path::const_iterator I = llvm::sys::path::begin(FilePath),
196 E = llvm::sys::path::end(FilePath);
200 std::string Stem(llvm::sys::path::stem(*I));
203 Module *SubModule = CurrentModule->findSubModule(Stem);
205 SubModule =
new Module(Stem, IsProblemFile);
206 CurrentModule->SubModules.push_back(SubModule);
208 CurrentModule = SubModule;
211 CurrentModule->HeaderFileNames.push_back(FilePath);
217 llvm::StringRef RootModuleName, llvm::ArrayRef<std::string> HeaderFileNames,
218 llvm::ArrayRef<std::string> ProblemFileNames,
222 auto *
RootModule =
new Module(RootModuleName,
false);
224 llvm::SmallString<256> CurrentDirectory;
225 llvm::sys::fs::current_path(CurrentDirectory);
232 for (llvm::ArrayRef<std::string>::iterator I = HeaderFileNames.begin(),
233 E = HeaderFileNames.end();
235 std::string Header(*I);
236 bool IsProblemFile =
false;
237 for (
auto &ProblemFile : ProblemFileNames) {
238 if (ProblemFile == Header) {
239 IsProblemFile =
true;
255 llvm::sys::path::remove_filename(HeaderDirectory);
256 llvm::SmallString<256> FilePath;
259 if ((HeaderDirectory.size() == 0) && (
HeaderPrefix.size() != 0)) {
263 llvm::sys::path::native(FilePath);
266 llvm::sys::path::native(FilePath);
271 llvm::ToolOutputFile
Out(FilePath, EC, llvm::sys::fs::OF_Text);
273 llvm::errs() <<
Argv0 <<
": error opening " << FilePath <<
":"
274 << EC.message() <<
"\n";
279 llvm::raw_fd_ostream &
OS =
Out.os();
299 llvm::ArrayRef<std::string> HeaderFileNames,
300 llvm::ArrayRef<std::string> ProblemFileNames,
302 llvm::StringRef RootModuleName) {
306 RootModuleName, HeaderFileNames, ProblemFileNames, Dependencies,