Skip to content

Commit

Permalink
[clang][modules] Build inferred modules
Browse files Browse the repository at this point in the history
This patch enables explicitly building inferred modules.

Effectively a cherry-pick of swiftlang#699 authored by @Bigcheese with libclang and dependency scanner changes omitted.

Contains the following changes:

1. [Clang] Fix the header paths in clang::Module for inferred modules.
  * The UmbrellaAsWritten and NameAsWritten fields in clang::Module are a lie for framework modules. For those they actually are the path to the header or umbrella relative to the clang::Module::Directory.
  * The exception to this case is for inferred modules. Here it actually is the name as written, because we print out the module and read it back in when implicitly building modules. This causes a problem when explicitly building an inferred module, as we skip the printing out step.
  * In order to fix this issue this patch adds a new field for the path we want to use in getInputBufferForModule. It also makes NameAsWritten actually be the name written in the module map file (or that would be, in the case of an inferred module).

2. [Clang] Allow explicitly building an inferred module.
  * Building the actual module still fails, but make sure it fails for the right reason.

Split from D100934.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D102491
  • Loading branch information
Bigcheese authored and jansvoboda11 committed May 17, 2021
1 parent 65936b9 commit d3676d4
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 31 deletions.
8 changes: 7 additions & 1 deletion clang/include/clang/Basic/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ class Module {
/// The name of the umbrella entry, as written in the module map.
std::string UmbrellaAsWritten;

// The path to the umbrella entry relative to the root module's \c Directory.
std::string UmbrellaRelativeToRootModuleDirectory;

/// The module through which entities defined in this module will
/// eventually be exposed, for use in "private" modules.
std::string ExportAsModule;
Expand Down Expand Up @@ -188,6 +191,7 @@ class Module {
/// file.
struct Header {
std::string NameAsWritten;
std::string PathRelativeToRootModuleDirectory;
const FileEntry *Entry;

explicit operator bool() { return Entry; }
Expand All @@ -197,6 +201,7 @@ class Module {
/// file.
struct DirectoryName {
std::string NameAsWritten;
std::string PathRelativeToRootModuleDirectory;
const DirectoryEntry *Entry;

explicit operator bool() { return Entry; }
Expand Down Expand Up @@ -545,7 +550,8 @@ class Module {
/// module.
Header getUmbrellaHeader() const {
if (auto *FE = Umbrella.dyn_cast<const FileEntry *>())
return Header{UmbrellaAsWritten, FE};
return Header{UmbrellaAsWritten, UmbrellaRelativeToRootModuleDirectory,
FE};
return Header{};
}

Expand Down
6 changes: 4 additions & 2 deletions clang/include/clang/Lex/ModuleMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -649,12 +649,14 @@ class ModuleMap {
/// Sets the umbrella header of the given module to the given
/// header.
void setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader,
Twine NameAsWritten);
const Twine &NameAsWritten,
const Twine &PathRelativeToRootModuleDirectory);

/// Sets the umbrella directory of the given module to the given
/// directory.
void setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir,
Twine NameAsWritten);
const Twine &NameAsWritten,
const Twine &PathRelativeToRootModuleDirectory);

/// Adds this header to the given module.
/// \param Role The role of the header wrt the module.
Expand Down
5 changes: 3 additions & 2 deletions clang/lib/Basic/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,10 @@ bool Module::fullModuleNameIs(ArrayRef<StringRef> nameParts) const {

Module::DirectoryName Module::getUmbrellaDir() const {
if (Header U = getUmbrellaHeader())
return {"", U.Entry->getDir()};
return {"", "", U.Entry->getDir()};

return {UmbrellaAsWritten, Umbrella.dyn_cast<const DirectoryEntry *>()};
return {UmbrellaAsWritten, UmbrellaRelativeToRootModuleDirectory,
Umbrella.dyn_cast<const DirectoryEntry *>()};
}

void Module::addTopHeader(const FileEntry *File) {
Expand Down
16 changes: 9 additions & 7 deletions clang/lib/Frontend/FrontendAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ static std::error_code collectModuleHeaderIncludes(
// file relative to the module build directory (the directory containing
// the module map file) so this will find the same file that we found
// while parsing the module map.
addHeaderInclude(H.NameAsWritten, Includes, LangOpts, Module->IsExternC);
addHeaderInclude(H.PathRelativeToRootModuleDirectory, Includes, LangOpts,
Module->IsExternC);
}
}
// Note that Module->PrivateHeaders will not be a TopHeader.
Expand All @@ -351,8 +352,8 @@ static std::error_code collectModuleHeaderIncludes(
Module->addTopHeader(UmbrellaHeader.Entry);
if (Module->Parent)
// Include the umbrella header for submodules.
addHeaderInclude(UmbrellaHeader.NameAsWritten, Includes, LangOpts,
Module->IsExternC);
addHeaderInclude(UmbrellaHeader.PathRelativeToRootModuleDirectory,
Includes, LangOpts, Module->IsExternC);
} else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) {
// Add all of the headers we find in this subdirectory.
std::error_code EC;
Expand Down Expand Up @@ -386,7 +387,8 @@ static std::error_code collectModuleHeaderIncludes(
auto PathIt = llvm::sys::path::rbegin(Dir->path());
for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt)
Components.push_back(*PathIt);
SmallString<128> RelativeHeader(UmbrellaDir.NameAsWritten);
SmallString<128> RelativeHeader(
UmbrellaDir.PathRelativeToRootModuleDirectory);
for (auto It = Components.rbegin(), End = Components.rend(); It != End;
++It)
llvm::sys::path::append(RelativeHeader, *It);
Expand Down Expand Up @@ -470,7 +472,7 @@ static Module *prepareToBuildModule(CompilerInstance &CI,
// Dig out the module definition.
HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
Module *M = HS.lookupModule(CI.getLangOpts().CurrentModule,
/*AllowSearch=*/false);
/*AllowSearch=*/true);
if (!M) {
CI.getDiagnostics().Report(diag::err_missing_module)
<< CI.getLangOpts().CurrentModule << ModuleMapFilename;
Expand Down Expand Up @@ -528,8 +530,8 @@ getInputBufferForModule(CompilerInstance &CI, Module *M) {
SmallString<256> HeaderContents;
std::error_code Err = std::error_code();
if (Module::Header UmbrellaHeader = M->getUmbrellaHeader())
addHeaderInclude(UmbrellaHeader.NameAsWritten, HeaderContents,
CI.getLangOpts(), M->IsExternC);
addHeaderInclude(UmbrellaHeader.PathRelativeToRootModuleDirectory,
HeaderContents, CI.getLangOpts(), M->IsExternC);
Err = collectModuleHeaderIncludes(
CI.getLangOpts(), FileMgr, CI.getDiagnostics(),
CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), M,
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Frontend/FrontendActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ bool GenerateHeaderModuleAction::BeginSourceFileAction(
<< Name;
continue;
}
Headers.push_back({std::string(Name), &FE->getFileEntry()});
Headers.push_back(
{std::string(Name), std::string(Name), &FE->getFileEntry()});
}
HS.getModuleMap().createHeaderModule(CI.getLangOpts().CurrentModule, Headers);

Expand Down
34 changes: 22 additions & 12 deletions clang/lib/Lex/ModuleMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,10 @@ void ModuleMap::resolveHeader(Module *Mod,
<< UmbrellaMod->getFullModuleName();
else
// Record this umbrella header.
setUmbrellaHeader(Mod, *File, RelativePathName.str());
setUmbrellaHeader(Mod, *File, Header.FileName, RelativePathName.str());
} else {
Module::Header H = {std::string(RelativePathName.str()), *File};
Module::Header H = {Header.FileName, std::string(RelativePathName.str()),
*File};
if (Header.Kind == Module::HK_Excluded)
excludeHeader(Mod, H);
else
Expand Down Expand Up @@ -305,7 +306,7 @@ bool ModuleMap::resolveAsBuiltinHeader(
return false;

auto Role = headerKindToRole(Header.Kind);
Module::Header H = {std::string(Path.str()), *File};
Module::Header H = {Header.FileName, std::string(Path.str()), *File};
addHeader(Mod, H, Role);
return true;
}
Expand Down Expand Up @@ -1038,11 +1039,13 @@ Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir,
Result->NoUndeclaredIncludes |= Attrs.NoUndeclaredIncludes;
Result->Directory = FrameworkDir;

// Chop off the first framework bit, as that is implied.
StringRef RelativePath = UmbrellaName.str().substr(
Result->getTopLevelModule()->Directory->getName().size());
RelativePath = llvm::sys::path::relative_path(RelativePath);

// umbrella header "umbrella-header-name"
//
// The "Headers/" component of the name is implied because this is
// a framework module.
setUmbrellaHeader(Result, *UmbrellaHeader, ModuleName + ".h");
setUmbrellaHeader(Result, *UmbrellaHeader, ModuleName + ".h", RelativePath);

// export *
Result->Exports.push_back(Module::ExportDecl(nullptr, true));
Expand Down Expand Up @@ -1121,11 +1124,14 @@ Module *ModuleMap::createShadowedModule(StringRef Name, bool IsFramework,
return Result;
}

void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader,
Twine NameAsWritten) {
void ModuleMap::setUmbrellaHeader(
Module *Mod, const FileEntry *UmbrellaHeader, const Twine &NameAsWritten,
const Twine &PathRelativeToRootModuleDirectory) {
Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader));
Mod->Umbrella = UmbrellaHeader;
Mod->UmbrellaAsWritten = NameAsWritten.str();
Mod->UmbrellaRelativeToRootModuleDirectory =
PathRelativeToRootModuleDirectory.str();
UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;

// Notify callbacks that we just added a new header.
Expand All @@ -1134,9 +1140,12 @@ void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader,
}

void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir,
Twine NameAsWritten) {
const Twine &NameAsWritten,
const Twine &PathRelativeToRootModuleDirectory) {
Mod->Umbrella = UmbrellaDir;
Mod->UmbrellaAsWritten = NameAsWritten.str();
Mod->UmbrellaRelativeToRootModuleDirectory =
PathRelativeToRootModuleDirectory.str();
UmbrellaDirs[UmbrellaDir] = Mod;
}

Expand Down Expand Up @@ -2405,6 +2414,7 @@ void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
}

std::string DirName = std::string(Tok.getString());
std::string DirNameAsWritten = DirName;
SourceLocation DirNameLoc = consumeToken();

// Check whether we already have an umbrella.
Expand Down Expand Up @@ -2446,7 +2456,7 @@ void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
for (llvm::vfs::recursive_directory_iterator I(FS, Dir->getName(), EC), E;
I != E && !EC; I.increment(EC)) {
if (auto FE = SourceMgr.getFileManager().getFile(I->path())) {
Module::Header Header = {std::string(I->path()), *FE};
Module::Header Header = {"", std::string(I->path()), *FE};
Headers.push_back(std::move(Header));
}
}
Expand All @@ -2467,7 +2477,7 @@ void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
}

// Record this umbrella directory.
Map.setUmbrellaDir(ActiveModule, Dir, DirName);
Map.setUmbrellaDir(ActiveModule, Dir, DirNameAsWritten, DirName);
}

/// Parse a module export declaration.
Expand Down
13 changes: 7 additions & 6 deletions clang/lib/Serialization/ASTReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1921,10 +1921,9 @@ HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
std::string Filename = std::string(key.Filename);
if (key.Imported)
Reader.ResolveImportedPath(M, Filename);
// FIXME: This is not always the right filename-as-written, but we're not
// going to use this information to rebuild the module, so it doesn't make
// a lot of difference.
Module::Header H = {std::string(key.Filename), *FileMgr.getFile(Filename)};
// FIXME: NameAsWritten
Module::Header H = {std::string(key.Filename), "",
*FileMgr.getFile(Filename)};
ModMap.addHeader(Mod, H, HeaderRole, /*Imported*/true);
HFI.isModuleHeader |= !(HeaderRole & ModuleMap::TextualHeader);
}
Expand Down Expand Up @@ -5618,7 +5617,8 @@ ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
ResolveImportedPath(F, Filename);
if (auto Umbrella = PP.getFileManager().getFile(Filename)) {
if (!CurrentModule->getUmbrellaHeader())
ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob);
// FIXME: NameAsWritten
ModMap.setUmbrellaHeader(CurrentModule, *Umbrella, Blob, "");
else if (CurrentModule->getUmbrellaHeader().Entry != *Umbrella) {
if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
Error("mismatched umbrella headers in submodule");
Expand Down Expand Up @@ -5651,7 +5651,8 @@ ASTReader::ReadSubmoduleBlock(ModuleFile &F, unsigned ClientLoadCapabilities) {
ResolveImportedPath(F, Dirname);
if (auto Umbrella = PP.getFileManager().getDirectory(Dirname)) {
if (!CurrentModule->getUmbrellaDir())
ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob);
// FIXME: NameAsWritten
ModMap.setUmbrellaDir(CurrentModule, *Umbrella, Blob, "");
else if (CurrentModule->getUmbrellaDir().Entry != *Umbrella) {
if ((ClientLoadCapabilities & ARR_OutOfDate) == 0)
Error("mismatched umbrella directories in submodule");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
typedef int inferred;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
framework module * {}
13 changes: 13 additions & 0 deletions clang/test/Modules/explicit-build-inferred.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: rm -rf %t && mkdir %t
//
// RUN: %clang_cc1 -fmodules -fno-implicit-modules -fimplicit-module-maps \
// RUN: -emit-module -x c++ %S/Inputs/explicit-build-inferred/frameworks/module.modulemap \
// RUN: -fmodule-name=Inferred -o %t/Inferred.pcm -F %S/Inputs/explicit-build-inferred/frameworks
//
// RUN: %clang_cc1 -fmodules -fno-implicit-modules -fsyntax-only %s \
// RUN: -fmodule-map-file=%S/Inputs/explicit-build-inferred/frameworks/module.modulemap \
// RUN: -fmodule-file=%t/Inferred.pcm -F %S/Inputs/explicit-build-inferred/frameworks

#include <Inferred/Inferred.h>

inferred a = 0;

0 comments on commit d3676d4

Please sign in to comment.