[modules] Flatten -fmodule-name= and -fmodule-implementation-of= into a single

option. Previously these options could both be used to specify that you were
compiling the implementation file of a module, with a different set of minor
bugs in each case.

This change removes -fmodule-implementation-of, and instead tracks a flag to
determine whether we're currently building a module. -fmodule-name now behaves
the same way that -fmodule-implementation-of previously did.

llvm-svn: 261372
This commit is contained in:
Richard Smith 2016-02-19 22:25:36 +00:00
parent a8f1f2efaf
commit 7e82e019c6
22 changed files with 67 additions and 104 deletions

View File

@ -210,10 +210,6 @@ def err_test_module_file_extension_version : Error<
"test module file extension '%0' has different version (%1.%2) than expected "
"(%3.%4)">;
def err_conflicting_module_names : Error<
"conflicting module names specified: '-fmodule-name=%0' and "
"'-fmodule-implementation-of %1'">;
def err_missing_vfs_overlay_file : Error<
"virtual filesystem overlay file '%0' not found">, DefaultFatal;
def err_invalid_vfs_overlay : Error<

View File

@ -127,6 +127,7 @@ BENIGN_LANGOPT(EmitAllDecls , 1, 0, "support for emitting all declarations"
LANGOPT(MathErrno , 1, 1, "errno support for math functions")
BENIGN_LANGOPT(HeinousExtensions , 1, 0, "Extensions that we really don't like and may be ripped out at any time")
LANGOPT(Modules , 1, 0, "modules extension to C")
BENIGN_LANGOPT(CompilingModule, 1, 0, "compiling a module interface")
COMPATIBLE_LANGOPT(ModulesDeclUse , 1, 0, "require declaration of module uses")
LANGOPT(ModulesSearchAll , 1, 1, "search even non-imported modules to find unresolved references")
COMPATIBLE_LANGOPT(ModulesStrictDeclUse, 1, 0, "require declaration of module uses and all headers to be in modules")

View File

@ -92,14 +92,12 @@ public:
/// If none is specified, abort (GCC-compatible behaviour).
std::string OverflowHandler;
/// \brief The name of the current module.
/// \brief The name of the current module, of which the main source file
/// is a part. If CompilingModule is set, we are compiling the interface
/// of this module, otherwise we are compiling an implementation file of
/// it.
std::string CurrentModule;
/// \brief The name of the module that the translation unit is an
/// implementation of. Prevents semantic imports, but does not otherwise
/// treat this as the CurrentModule.
std::string ImplementationOfModule;
/// \brief The names of any features to enable in module 'requires' decls
/// in addition to the hard-coded list in Module.cpp and the target features.
///

View File

@ -382,9 +382,6 @@ def fno_modules_global_index : Flag<["-"], "fno-modules-global-index">,
HelpText<"Do not automatically generate or update the global module index">;
def fno_modules_error_recovery : Flag<["-"], "fno-modules-error-recovery">,
HelpText<"Do not automatically import modules for error recovery">;
def fmodule_implementation_of : Separate<["-"], "fmodule-implementation-of">,
MetaVarName<"<name>">,
HelpText<"Specify the name of the module whose implementation file this is">;
def fmodule_map_file_home_is_cwd : Flag<["-"], "fmodule-map-file-home-is-cwd">,
HelpText<"Use the current working directory as the home directory of "
"module maps specified by -fmodule-map-file=<FILE>">;

View File

@ -786,9 +786,10 @@ def fimplicit_module_maps : Flag <["-"], "fimplicit-module-maps">, Group<f_Group
Flags<[DriverOption, CC1Option]>,
HelpText<"Implicitly search the file system for module map files.">;
def fmodule_maps : Flag <["-"], "fmodule-maps">, Alias<fimplicit_module_maps>;
def fmodule_name : JoinedOrSeparate<["-"], "fmodule-name=">, Group<f_Group>,
def fmodule_name_EQ : Joined<["-"], "fmodule-name=">, Group<f_Group>,
Flags<[DriverOption,CC1Option]>, MetaVarName<"<name>">,
HelpText<"Specify the name of the module to build">;
def fmodule_name : Separate<["-"], "fmodule-name">, Alias<fmodule_name_EQ>;
def fmodule_map_file : Joined<["-"], "fmodule-map-file=">,
Group<f_Group>, Flags<[DriverOption,CC1Option]>, MetaVarName<"<file>">,
HelpText<"Load this module map file">;

View File

@ -70,15 +70,10 @@ class ModuleMap {
/// These are always simple C language options.
LangOptions MMapLangOpts;
// The module that we are building; related to \c LangOptions::CurrentModule.
Module *CompilingModule;
public:
// The module that the .cc source file is associated with.
// The module that the main source file is associated with (the module
// named LangOpts::CurrentModule, if we've loaded it).
Module *SourceModule;
std::string SourceModuleName;
private:
/// \brief The top-level modules that are known.
llvm::StringMap<Module *> Modules;

View File

@ -34,7 +34,6 @@ void LangOptions::resetNonModularOptions() {
SanitizerBlacklistFiles.clear();
CurrentModule.clear();
ImplementationOfModule.clear();
}
bool LangOptions::isNoBuiltinFunc(const char *Name) const {

View File

@ -4909,7 +4909,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
// -fmodule-name specifies the module that is currently being built (or
// used for header checking by -fmodule-maps).
Args.AddLastArg(CmdArgs, options::OPT_fmodule_name);
Args.AddLastArg(CmdArgs, options::OPT_fmodule_name_EQ);
// -fmodule-map-file can be used to specify files containing module
// definitions.

View File

@ -2814,7 +2814,7 @@ const FileEntry *ASTUnit::getPCHFile() {
}
bool ASTUnit::isModuleFile() {
return isMainFileAST() && !ASTFileLangOpts.CurrentModule.empty();
return isMainFileAST() && ASTFileLangOpts.CompilingModule;
}
void ASTUnit::PreambleData::countLines() const {

View File

@ -1386,8 +1386,7 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
// when both the preprocessor and parser see the same import declaration.
if (ImportLoc.isValid() && LastModuleImportLoc == ImportLoc) {
// Make the named module visible.
if (LastModuleImportResult && ModuleName != getLangOpts().CurrentModule &&
ModuleName != getLangOpts().ImplementationOfModule)
if (LastModuleImportResult && ModuleName != getLangOpts().CurrentModule)
ModuleManager->makeModuleVisible(LastModuleImportResult, Visibility,
ImportLoc);
return LastModuleImportResult;
@ -1401,8 +1400,7 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
if (Known != KnownModules.end()) {
// Retrieve the cached top-level module.
Module = Known->second;
} else if (ModuleName == getLangOpts().CurrentModule ||
ModuleName == getLangOpts().ImplementationOfModule) {
} else if (ModuleName == getLangOpts().CurrentModule) {
// This is the module we're building.
Module = PP->getHeaderSearchInfo().lookupModule(ModuleName);
Known = KnownModules.insert(std::make_pair(Path[0].first, Module)).first;
@ -1580,10 +1578,6 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
}
}
// Don't make the module visible if we are in the implementation.
if (ModuleName == getLangOpts().ImplementationOfModule)
return ModuleLoadResult(Module, false);
// Make the named module visible, if it's not already part of the module
// we are parsing.
if (ModuleName != getLangOpts().CurrentModule) {

View File

@ -1763,10 +1763,8 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
Opts.DebuggerCastResultToId = Args.hasArg(OPT_fdebugger_cast_result_to_id);
Opts.DebuggerObjCLiteral = Args.hasArg(OPT_fdebugger_objc_literal);
Opts.ApplePragmaPack = Args.hasArg(OPT_fapple_pragma_pack);
Opts.CurrentModule = Args.getLastArgValue(OPT_fmodule_name);
Opts.CurrentModule = Args.getLastArgValue(OPT_fmodule_name_EQ);
Opts.AppExt = Args.hasArg(OPT_fapplication_extension);
Opts.ImplementationOfModule =
Args.getLastArgValue(OPT_fmodule_implementation_of);
Opts.ModuleFeatures = Args.getAllArgValues(OPT_fmodule_feature);
std::sort(Opts.ModuleFeatures.begin(), Opts.ModuleFeatures.end());
Opts.NativeHalfType |= Args.hasArg(OPT_fnative_half_type);
@ -1784,12 +1782,6 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
Args.hasFlag(OPT_fdeclspec, OPT_fno_declspec,
(Opts.MicrosoftExt || Opts.Borland || Opts.CUDA));
if (!Opts.CurrentModule.empty() && !Opts.ImplementationOfModule.empty() &&
Opts.CurrentModule != Opts.ImplementationOfModule) {
Diags.Report(diag::err_conflicting_module_names)
<< Opts.CurrentModule << Opts.ImplementationOfModule;
}
// For now, we only support local submodule visibility in C++ (because we
// heavily depend on the ODR for merging redefinitions).
if (Opts.ModulesLocalVisibility && !Opts.CPlusPlus)

View File

@ -270,6 +270,8 @@ collectModuleHeaderIncludes(const LangOptions &LangOpts, FileManager &FileMgr,
bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI,
StringRef Filename) {
CI.getLangOpts().CompilingModule = true;
// Find the module map file.
const FileEntry *ModuleMap =
CI.getFileManager().getFile(Filename, /*openFile*/true);

View File

@ -89,7 +89,7 @@ ModuleMap::ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags,
HeaderSearch &HeaderInfo)
: SourceMgr(SourceMgr), Diags(Diags), LangOpts(LangOpts), Target(Target),
HeaderInfo(HeaderInfo), BuiltinIncludeDir(nullptr),
CompilingModule(nullptr), SourceModule(nullptr), NumCreatedModules(0) {
SourceModule(nullptr), NumCreatedModules(0) {
MMapLangOpts.LineComment = true;
}
@ -343,8 +343,8 @@ ModuleMap::KnownHeader ModuleMap::findModuleForHeader(const FileEntry *File) {
ModuleMap::KnownHeader Result;
// Iterate over all modules that 'File' is part of to find the best fit.
for (KnownHeader &H : Known->second) {
// Prefer a header from the current module over all others.
if (H.getModule()->getTopLevelModule() == CompilingModule)
// Prefer a header from the source module over all others.
if (H.getModule()->getTopLevelModule() == SourceModule)
return MakeResult(H);
if (!Result || isBetterKnownHeader(H, Result))
Result = H;
@ -556,16 +556,10 @@ ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
// Create a new module with this name.
Module *Result = new Module(Name, SourceLocation(), Parent,
IsFramework, IsExplicit, NumCreatedModules++);
if (LangOpts.CurrentModule == Name) {
SourceModule = Result;
SourceModuleName = Name;
}
if (!Parent) {
if (LangOpts.CurrentModule == Name)
SourceModule = Result;
Modules[Name] = Result;
if (!LangOpts.CurrentModule.empty() && !CompilingModule &&
Name == LangOpts.CurrentModule) {
CompilingModule = Result;
}
}
return std::make_pair(Result, true);
}
@ -693,9 +687,10 @@ Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir,
NumCreatedModules++);
InferredModuleAllowedBy[Result] = ModuleMapFile;
Result->IsInferred = true;
if (LangOpts.CurrentModule == ModuleName) {
SourceModule = Result;
SourceModuleName = ModuleName;
if (!Parent) {
if (LangOpts.CurrentModule == ModuleName)
SourceModule = Result;
Modules[ModuleName] = Result;
}
Result->IsSystem |= Attrs.IsSystem;
@ -703,9 +698,6 @@ Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir,
Result->ConfigMacrosExhaustive |= Attrs.IsExhaustive;
Result->Directory = FrameworkDir;
if (!Parent)
Modules[ModuleName] = Result;
// umbrella header "umbrella-header-name"
//
// The "Headers/" component of the name is implied because this is
@ -812,7 +804,8 @@ void ModuleMap::addHeader(Module *Mod, Module::Header Header,
HeaderList.push_back(KH);
Mod->Headers[headerRoleToKind(Role)].push_back(std::move(Header));
bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule;
bool isCompilingModuleHeader =
LangOpts.CompilingModule && Mod->getTopLevelModule() == SourceModule;
if (!Imported || isCompilingModuleHeader) {
// When we import HeaderFileInfo, the external source is expected to
// set the isModuleHeader flag itself.

View File

@ -573,23 +573,23 @@ void Preprocessor::PTHSkipExcludedConditionalBlock() {
}
Module *Preprocessor::getModuleForLocation(SourceLocation Loc) {
ModuleMap &ModMap = HeaderInfo.getModuleMap();
if (SourceMgr.isInMainFile(Loc)) {
if (Module *CurMod = getCurrentModule())
return CurMod; // Compiling a module.
return HeaderInfo.getModuleMap().SourceModule; // Compiling a source.
}
// Try to determine the module of the include directive.
// FIXME: Look into directly passing the FileEntry from LookupFile instead.
FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc));
if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
// The include comes from a file.
return ModMap.findModuleForHeader(EntryOfIncl).getModule();
} else {
// The include does not come from a file,
// so it is probably a module compilation.
return getCurrentModule();
if (!SourceMgr.isInMainFile(Loc)) {
// Try to determine the module of the include directive.
// FIXME: Look into directly passing the FileEntry from LookupFile instead.
FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc));
if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
// The include comes from an included file.
return HeaderInfo.getModuleMap()
.findModuleForHeader(EntryOfIncl)
.getModule();
}
}
// This is either in the main file or not in a file at all. It belongs
// to the current module, if there is one.
return getLangOpts().CurrentModule.empty()
? nullptr
: HeaderInfo.lookupModule(getLangOpts().CurrentModule);
}
Module *Preprocessor::getModuleContainingLocation(SourceLocation Loc) {
@ -1668,10 +1668,7 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
// are processing this module textually (because we're building the module).
if (File && SuggestedModule && getLangOpts().Modules &&
SuggestedModule.getModule()->getTopLevelModuleName() !=
getLangOpts().CurrentModule &&
SuggestedModule.getModule()->getTopLevelModuleName() !=
getLangOpts().ImplementationOfModule) {
getLangOpts().CurrentModule) {
// If this include corresponds to a module but that module is
// unavailable, diagnose the situation and bail out.
if (!SuggestedModule.getModule()->isAvailable()) {

View File

@ -1438,8 +1438,9 @@ static bool EvaluateBuildingModule(Token &Tok,
return false;
}
bool Result
= Tok.getIdentifierInfo()->getName() == PP.getLangOpts().CurrentModule;
bool Result =
PP.getLangOpts().CompilingModule &&
Tok.getIdentifierInfo()->getName() == PP.getLangOpts().CurrentModule;
// Get ')'.
PP.LexNonComment(Tok);

View File

@ -477,7 +477,7 @@ void Preprocessor::CreateString(StringRef Str, Token &Tok,
}
Module *Preprocessor::getCurrentModule() {
if (getLangOpts().CurrentModule.empty())
if (!getLangOpts().CompilingModule)
return nullptr;
return getHeaderSearchInfo().lookupModule(getLangOpts().CurrentModule);

View File

@ -14752,11 +14752,10 @@ DeclResult Sema::ActOnModuleImport(SourceLocation AtLoc,
// of the same top-level module. Until we do, make it an error rather than
// silently ignoring the import.
if (Mod->getTopLevelModuleName() == getLangOpts().CurrentModule)
Diag(ImportLoc, diag::err_module_self_import)
Diag(ImportLoc, getLangOpts().CompilingModule
? diag::err_module_self_import
: diag::err_module_import_in_implementation)
<< Mod->getFullModuleName() << getLangOpts().CurrentModule;
else if (Mod->getTopLevelModuleName() == getLangOpts().ImplementationOfModule)
Diag(ImportLoc, diag::err_module_import_in_implementation)
<< Mod->getFullModuleName() << getLangOpts().ImplementationOfModule;
SmallVector<SourceLocation, 2> IdentifierLocs;
Module *ModCheck = Mod;
@ -14790,11 +14789,13 @@ void Sema::ActOnModuleInclude(SourceLocation DirectiveLoc, Module *Mod) {
TUKind == TU_Module &&
getSourceManager().isWrittenInMainFile(DirectiveLoc);
// Similarly, if this module is specified by -fmodule-implementation-of
// don't actually synthesize an illegal module import.
bool ShouldAddImport = !IsInModuleIncludes &&
(getLangOpts().ImplementationOfModule.empty() ||
getLangOpts().ImplementationOfModule != Mod->getTopLevelModuleName());
// Similarly, if we're in the implementation of a module, don't
// synthesize an illegal module import. FIXME: Why not?
bool ShouldAddImport =
!IsInModuleIncludes &&
(getLangOpts().CompilingModule ||
getLangOpts().CurrentModule.empty() ||
getLangOpts().CurrentModule != Mod->getTopLevelModuleName());
// If this module import was due to an inclusion directive, create an
// implicit import declaration to capture it in the AST.

View File

@ -3171,7 +3171,7 @@ void Sema::addMethodToGlobalList(ObjCMethodList *List,
ObjCMethodList *Previous = List;
for (; List; Previous = List, List = List->getNext()) {
// If we are building a module, keep all of the methods.
if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty())
if (getLangOpts().CompilingModule)
continue;
if (!MatchTwoMethodDeclarations(Method, List->getMethod())) {

View File

@ -1,4 +1,4 @@
#if !__building_module(a)
#if !__building_module(a) && !BUILDING_A_PCH
#error "should only get here when building module a"
#endif

View File

@ -143,7 +143,7 @@
// -------------------------------
// Try to import a PCH with -fmodule-file=
// RUN: %clang_cc1 -x c++ -std=c++11 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -Rmodule-build -fno-modules-error-recovery \
// RUN: -fmodule-name=a -emit-pch %S/Inputs/explicit-build/a.h -o %t/a.pch \
// RUN: -fmodule-name=a -emit-pch %S/Inputs/explicit-build/a.h -o %t/a.pch -DBUILDING_A_PCH \
// RUN: 2>&1 | FileCheck --check-prefix=CHECK-NO-IMPLICIT-BUILD %s --allow-empty
//
// RUN: not %clang_cc1 -x c++ -std=c++11 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -Rmodule-build -fno-modules-error-recovery \

View File

@ -1,22 +1,18 @@
// RUN: not %clang_cc1 -fmodule-implementation-of Foo -fmodule-name=Bar %s 2>&1 \
// RUN: | FileCheck -check-prefix=CHECK-IMPL-OF-ERR %s
// CHECK-IMPL-OF-ERR: conflicting module names specified: '-fmodule-name=Bar' and '-fmodule-implementation-of Foo'
// RUN: rm -rf %t
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -w -Werror=auto-import %s -I %S/Inputs \
// RUN: -fmodule-implementation-of category_right -fsyntax-only
// RUN: -fmodule-name=category_right -fsyntax-only
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -w -Werror=auto-import %s -I %S/Inputs \
// RUN: -fmodule-implementation-of category_right -dM -E -o - 2>&1 | FileCheck %s
// RUN: -fmodule-name=category_right -dM -E -o - 2>&1 | FileCheck %s
// CHECK-NOT: __building_module
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -w -Werror=auto-import %s -I %S/Inputs \
// RUN: -fmodule-implementation-of category_left -verify
// RUN: -fmodule-name=category_left -verify
// RUN: %clang_cc1 -x objective-c-header -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -w -Werror=auto-import %s -I %S/Inputs \
// RUN: -fmodule-implementation-of category_right -emit-pch -o %t.pch
// RUN: -fmodule-name=category_right -emit-pch -o %t.pch
// RUN: %clang_cc1 -x objective-c-header -fmodules -fimplicit-module-maps -fmodules-cache-path=%t -w -Werror=auto-import %s -I %S/Inputs \
// RUN: -DWITH_PREFIX -fmodules-ignore-macro=WITH_PREFIX -include-pch %t.pch -fmodule-implementation-of category_right
// RUN: -DWITH_PREFIX -fmodules-ignore-macro=WITH_PREFIX -include-pch %t.pch -fmodule-name=category_right
#ifndef WITH_PREFIX

View File

@ -6,6 +6,6 @@
// RUN: not %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t \
// RUN: -I %S/Inputs/submodules -fmodule-name=import_self %s \
// RUN: 2>&1 | FileCheck -check-prefix=CHECK-fmodule-name %s
// CHECK-fmodule-name: import of module 'import_self.b' appears within same top-level module 'import_self'
// CHECK-fmodule-name: @import of module 'import_self.b' in implementation of 'import_self'
@import import_self.b;