33 #include "llvm/ADT/SmallString.h" 34 #include "llvm/Support/FileSystem.h" 35 #include "llvm/Support/Path.h" 36 #include "llvm/Support/ToolOutputFile.h" 50 bool output(llvm::raw_fd_ostream &OS,
int Indent);
51 Module *findSubModule(llvm::StringRef SubName);
55 std::vector<std::string> HeaderFileNames;
56 std::vector<Module *> SubModules;
65 Module::Module(llvm::StringRef
Name,
bool Problem)
66 : Name(Name), IsProblem(Problem) {}
71 while (!SubModules.empty()) {
72 Module *last = SubModules.back();
73 SubModules.pop_back();
79 bool Module::output(llvm::raw_fd_ostream &OS,
int Indent) {
81 if (
Name.size() != 0) {
83 OS <<
"module " <<
Name <<
" {\n";
88 for (
auto I = SubModules.begin(), E = SubModules.end(); I != E; ++I) {
89 if (!(*I)->output(OS, Indent))
97 if (IsProblem || strstr((*I).c_str(),
".inl"))
98 OS <<
"exclude header \"" << *I <<
"\"\n";
100 OS <<
"header \"" << *I <<
"\"\n";
110 if (
Name.size() != 0) {
120 Module *Module::findSubModule(llvm::StringRef SubName) {
121 for (
auto I = SubModules.begin(), E = SubModules.end(); I != E; ++I) {
122 if ((*I)->Name == SubName)
134 "config_macros",
"export",
"module",
"conflict",
"framework",
135 "requires",
"exclude",
"header",
"private",
"explicit",
136 "link",
"umbrella",
"extern",
"use",
nullptr 143 std::string SafeName = MightBeReservedName;
145 if (MightBeReservedName == ReservedNames[
Index]) {
146 SafeName.insert(0,
"_");
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;
167 llvm::StringRef HeaderFilePath,
170 bool IsProblemFile) {
173 std::string FilePath;
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);
182 FilePath = HeaderFilePath;
183 int Count = FileDependents.size();
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";
194 std::replace(FilePath.begin(), FilePath.end(),
'\\',
'/');
196 for (llvm::sys::path::const_iterator I = llvm::sys::path::begin(FilePath),
197 E = llvm::sys::path::end(FilePath);
201 std::string Stem = llvm::sys::path::stem(*I);
204 Module *SubModule = CurrentModule->findSubModule(Stem);
206 SubModule =
new Module(Stem, IsProblemFile);
207 CurrentModule->SubModules.push_back(SubModule);
209 CurrentModule = SubModule;
212 CurrentModule->HeaderFileNames.push_back(FilePath);
218 llvm::StringRef RootModuleName, llvm::ArrayRef<std::string>
HeaderFileNames,
223 auto *
RootModule =
new Module(RootModuleName,
false);
225 llvm::SmallString<256> CurrentDirectory;
226 llvm::sys::fs::current_path(CurrentDirectory);
229 if (HeaderPrefix.size() == 0)
230 HeaderPrefix = CurrentDirectory;
233 for (llvm::ArrayRef<std::string>::iterator I = HeaderFileNames.begin(),
234 E = HeaderFileNames.end();
236 std::string Header(*I);
237 bool IsProblemFile =
false;
238 for (
auto &ProblemFile : ProblemFileNames) {
239 if (ProblemFile == Header) {
240 IsProblemFile =
true;
255 llvm::SmallString<256> HeaderDirectory(ModuleMapPath);
256 llvm::sys::path::remove_filename(HeaderDirectory);
257 llvm::SmallString<256> FilePath;
260 if ((HeaderDirectory.size() == 0) && (HeaderPrefix.size() != 0)) {
263 llvm::sys::path::append(FilePath, ModuleMapPath);
264 llvm::sys::path::native(FilePath);
267 llvm::sys::path::native(FilePath);
272 llvm::ToolOutputFile Out(FilePath, EC, llvm::sys::fs::F_Text);
274 llvm::errs() <<
Argv0 <<
": error opening " << FilePath <<
":" 275 << EC.message() <<
"\n";
280 llvm::raw_fd_ostream &OS = Out.os();
283 OS <<
"// " << ModuleMapPath <<
"\n";
284 OS <<
"// Generated by: " <<
CommandLine <<
"\n\n";
287 if (!RootModule->output(OS, 0))
303 llvm::StringRef RootModuleName) {
307 RootModuleName, HeaderFileNames, ProblemFileNames, Dependencies,
309 if (!RootModule.get())
313 return writeModuleMap(ModuleMapPath, HeaderPrefix, RootModule.get());
DependencyMap Dependencies
Map of top-level header file dependencies.
Common definitions for Modularize.
static bool addModuleDescription(Module *RootModule, llvm::StringRef HeaderFilePath, llvm::StringRef HeaderPrefix, DependencyMap &Dependencies, bool IsProblemFile)
llvm::SmallVector< std::string, 4 > DependentsVector
static const char *const ReservedNames[]
static bool writeModuleMap(llvm::StringRef ModuleMapPath, llvm::StringRef HeaderPrefix, Module *RootModule)
static Module * loadModuleDescriptions(llvm::StringRef RootModuleName, llvm::ArrayRef< std::string > HeaderFileNames, llvm::ArrayRef< std::string > ProblemFileNames, DependencyMap &Dependencies, llvm::StringRef HeaderPrefix)
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."))
static std::string ensureVaidModuleName(llvm::StringRef MightBeInvalidName)
static constexpr llvm::StringLiteral Name
llvm::SmallVector< std::string, 32 > ProblemFileNames
List of header files with problems.
llvm::StringMap< DependentsVector > DependencyMap
llvm::SmallVector< std::string, 32 > HeaderFileNames
List of top-level header files.
llvm::StringRef HeaderPrefix
The header prefix.
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.
static cl::opt< std::string > RootModule("root-module", cl::init(""), cl::desc("Specify the name of the root module."))
static std::string ensureNoCollisionWithReservedName(llvm::StringRef MightBeReservedName)
const SymbolIndex * Index