mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Add frontend flag -import-module <name>.
This implicitly adds the named module as an import of every source file in the module being compiled. This is not intended to be used generally, but will be useful for playgrounds. rdar://problem/19605934 Swift SVN r24905
This commit is contained in:
@@ -72,6 +72,9 @@ public:
|
||||
/// by the frontend.
|
||||
std::string OutputFilename;
|
||||
|
||||
/// An arbitrary module to import and make implicitly visible.
|
||||
std::string ImplicitImportModuleName;
|
||||
|
||||
/// An Objective-C header to import and make implicitly visible.
|
||||
std::string ImplicitObjCHeaderPath;
|
||||
|
||||
|
||||
@@ -184,6 +184,9 @@ def enable_union_import :
|
||||
def enable_source_import : Flag<["-"], "enable-source-import">,
|
||||
HelpText<"Enable importing of Swift source files">;
|
||||
|
||||
def import_module : Separate<["-"], "import-module">,
|
||||
HelpText<"Implicitly import the specified module">;
|
||||
|
||||
def print_stats : Flag<["-"], "print-stats">,
|
||||
HelpText<"Print various statistics">;
|
||||
|
||||
|
||||
@@ -535,6 +535,9 @@ static bool ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
|
||||
!Opts.PrimaryInput && !Opts.ModuleOutputPath.empty();
|
||||
}
|
||||
|
||||
if (const Arg *A = Args.getLastArg(OPT_import_module))
|
||||
Opts.ImplicitImportModuleName = A->getValue();
|
||||
|
||||
for (const Arg *A : make_range(Args.filtered_begin(OPT_Xllvm),
|
||||
Args.filtered_end())) {
|
||||
Opts.LLVMArgs.push_back(A->getValue());
|
||||
|
||||
@@ -222,6 +222,7 @@ Module *CompilerInstance::getMainModule() {
|
||||
}
|
||||
|
||||
void CompilerInstance::performSema() {
|
||||
const FrontendOptions &options = Invocation.getFrontendOptions();
|
||||
const SourceFileKind Kind = Invocation.getInputKind();
|
||||
Module *MainModule = getMainModule();
|
||||
Context->LoadedModules[MainModule->Name] = MainModule;
|
||||
@@ -254,7 +255,7 @@ void CompilerInstance::performSema() {
|
||||
static_cast<ClangImporter *>(Context->getClangModuleLoader());
|
||||
|
||||
Module *underlying = nullptr;
|
||||
if (Invocation.getFrontendOptions().ImportUnderlyingModule) {
|
||||
if (options.ImportUnderlyingModule) {
|
||||
underlying = clangImporter->loadModule(SourceLoc(),
|
||||
std::make_pair(MainModule->Name,
|
||||
SourceLoc()));
|
||||
@@ -265,16 +266,26 @@ void CompilerInstance::performSema() {
|
||||
}
|
||||
|
||||
Module *importedHeaderModule = nullptr;
|
||||
StringRef implicitHeaderPath =
|
||||
Invocation.getFrontendOptions().ImplicitObjCHeaderPath;
|
||||
StringRef implicitHeaderPath = options.ImplicitObjCHeaderPath;
|
||||
if (!implicitHeaderPath.empty()) {
|
||||
clangImporter->importBridgingHeader(implicitHeaderPath, MainModule);
|
||||
importedHeaderModule = clangImporter->getImportedHeaderModule();
|
||||
assert(importedHeaderModule);
|
||||
}
|
||||
|
||||
Module *importModule = nullptr;
|
||||
if (!options.ImplicitImportModuleName.empty()) {
|
||||
if (Lexer::isIdentifier(options.ImplicitImportModuleName)) {
|
||||
auto moduleID = Context->getIdentifier(options.ImplicitImportModuleName);
|
||||
importModule = Context->getModule(std::make_pair(moduleID, SourceLoc()));
|
||||
} else {
|
||||
Diagnostics.diagnose(SourceLoc(), diag::error_bad_module_name,
|
||||
options.ImplicitImportModuleName, false);
|
||||
}
|
||||
}
|
||||
|
||||
auto addAdditionalInitialImports = [&](SourceFile *SF) {
|
||||
if (!underlying && !importedHeaderModule)
|
||||
if (!underlying && !importedHeaderModule && !importModule)
|
||||
return;
|
||||
|
||||
auto initialImports = SF->getImports(/*allowUnparsed=*/true);
|
||||
@@ -289,6 +300,10 @@ void CompilerInstance::performSema() {
|
||||
if (importedHeaderModule)
|
||||
initialImportsBuf.push_back({ { /*accessPath=*/{}, importedHeaderModule },
|
||||
/*exported=*/true });
|
||||
if (importModule)
|
||||
initialImportsBuf.push_back({ { /*accessPath=*/{}, importModule },
|
||||
/*exported=*/false });
|
||||
|
||||
SF->setImports(Context->AllocateCopy(initialImportsBuf));
|
||||
};
|
||||
|
||||
|
||||
9
test/NameBinding/import-command-line.swift
Normal file
9
test/NameBinding/import-command-line.swift
Normal file
@@ -0,0 +1,9 @@
|
||||
// RUN: %target-swift-frontend -parse %s -enable-source-import -I %S/Inputs -sdk "" -verify
|
||||
// RUN: %target-swift-frontend -parse %s -enable-source-import -I %S/Inputs -sdk "" -import-module abcde
|
||||
|
||||
// RUN: not %target-swift-frontend -parse %s -enable-source-import -I %S/Inputs -sdk "" -import-module 3333 2>&1 | FileCheck -check-prefix=NON-IDENT %s
|
||||
|
||||
// NON-IDENT: error: module name "3333" is not a valid identifier
|
||||
|
||||
var a: A? // expected-error {{use of undeclared type 'A'}}
|
||||
var qA: abcde.A? // expected-error {{use of undeclared type 'abcde'}}
|
||||
Reference in New Issue
Block a user