Skip to content

Commit f719237

Browse files
committed
Add -testable-import-module frontend flag
This is just like -import-module, but it does an @testable import. Simple, right? Fixes rdar://66544654.
1 parent bf074b0 commit f719237

File tree

7 files changed

+27
-13
lines changed

7 files changed

+27
-13
lines changed

include/swift/Frontend/FrontendOptions.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class FrontendOptions {
3434
friend class ArgsToFrontendOptionsConverter;
3535

3636
/// A list of arbitrary modules to import and make implicitly visible.
37-
std::vector<std::string> ImplicitImportModuleNames;
37+
std::vector<std::pair<std::string, bool /*testable*/>>
38+
ImplicitImportModuleNames;
3839

3940
public:
4041
FrontendInputsAndOutputs InputsAndOutputs;
@@ -351,7 +352,8 @@ class FrontendOptions {
351352

352353
/// Retrieves the list of arbitrary modules to import and make implicitly
353354
/// visible.
354-
ArrayRef<std::string> getImplicitImportModuleNames() const {
355+
ArrayRef<std::pair<std::string, bool /*testable*/>>
356+
getImplicitImportModuleNames() const {
355357
return ImplicitImportModuleNames;
356358
}
357359

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,9 @@ def enable_throw_without_try : Flag<["-"], "enable-throw-without-try">,
512512
def import_module : Separate<["-"], "import-module">,
513513
HelpText<"Implicitly import the specified module">;
514514

515+
def testable_import_module : Separate<["-"], "testable-import-module">,
516+
HelpText<"Implicitly import the specified module with @testable">;
517+
515518
def print_stats : Flag<["-"], "print-stats">,
516519
HelpText<"Print various statistics">;
517520

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ bool ArgsToFrontendOptionsConverter::convert(
215215
Opts.DisableBuildingInterface = Args.hasArg(OPT_disable_building_interface);
216216

217217
computeImportObjCHeaderOptions();
218-
computeImplicitImportModuleNames();
218+
computeImplicitImportModuleNames(OPT_import_module, /*isTestable=*/false);
219+
computeImplicitImportModuleNames(OPT_testable_import_module, /*isTestable=*/true);
219220
computeLLVMArgs();
220221

221222
return false;
@@ -586,16 +587,17 @@ void ArgsToFrontendOptionsConverter::computeImportObjCHeaderOptions() {
586587
Opts.SerializeBridgingHeader |= !Opts.InputsAndOutputs.hasPrimaryInputs();
587588
}
588589
}
589-
void ArgsToFrontendOptionsConverter::computeImplicitImportModuleNames() {
590+
void ArgsToFrontendOptionsConverter::
591+
computeImplicitImportModuleNames(OptSpecifier id, bool isTestable) {
590592
using namespace options;
591-
for (const Arg *A : Args.filtered(OPT_import_module)) {
593+
for (const Arg *A : Args.filtered(id)) {
592594
auto *moduleStr = A->getValue();
593595
if (!Lexer::isIdentifier(moduleStr)) {
594596
Diags.diagnose(SourceLoc(), diag::error_bad_module_name, moduleStr,
595597
/*suggestModuleNameFlag*/ false);
596598
continue;
597599
}
598-
Opts.ImplicitImportModuleNames.push_back(moduleStr);
600+
Opts.ImplicitImportModuleNames.emplace_back(moduleStr, isTestable);
599601
}
600602
}
601603
void ArgsToFrontendOptionsConverter::computeLLVMArgs() {

lib/Frontend/ArgsToFrontendOptionsConverter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class ArgsToFrontendOptionsConverter {
4040
bool computeMainAndSupplementaryOutputFilenames();
4141
void computeDumpScopeMapLocations();
4242
void computeHelpOptions();
43-
void computeImplicitImportModuleNames();
43+
void computeImplicitImportModuleNames(llvm::opt::OptSpecifier id,
44+
bool isTestable);
4445
void computeImportObjCHeaderOptions();
4546
void computeLLVMArgs();
4647
void computePlaygroundOptions();

lib/Frontend/Frontend.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -740,15 +740,18 @@ ImplicitImportInfo CompilerInstance::getImplicitImportInfo() const {
740740
ImplicitImportInfo imports;
741741
imports.StdlibKind = Invocation.getImplicitStdlibKind();
742742

743-
auto pushImport = [&](StringRef moduleStr) {
743+
auto pushImport = [&](StringRef moduleStr,
744+
ImportOptions options = ImportOptions()) {
744745
ImportPath::Builder importPath(Context->getIdentifier(moduleStr));
745746
UnloadedImportedModule import(importPath.copyTo(*Context),
746747
/*isScoped=*/false);
747-
imports.AdditionalUnloadedImports.emplace_back(import, ImportOptions());
748+
imports.AdditionalUnloadedImports.emplace_back(import, options);
748749
};
749750

750-
for (auto &moduleStr : frontendOpts.getImplicitImportModuleNames()) {
751-
pushImport(moduleStr);
751+
for (auto &moduleStrAndTestable : frontendOpts.getImplicitImportModuleNames()) {
752+
pushImport(moduleStrAndTestable.first,
753+
moduleStrAndTestable.second ? ImportFlags::Testable
754+
: ImportOptions());
752755
}
753756

754757
if (Invocation.shouldImportSwiftONoneSupport()) {

test/ImportResolution/import-command-line.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@
77
// RUN: not %target-swift-frontend -typecheck %s -enable-source-import -I %S/Inputs -sdk "" -import-module NON_EXISTENT 2>&1 | %FileCheck -check-prefix=NON-EXISTENT %s
88
// NON-EXISTENT: error: no such module 'NON_EXISTENT'
99

10+
// RUN: not %target-swift-frontend -typecheck %s -enable-source-import -I %S/Inputs -sdk "" -testable-import-module abcde 2>&1 | %FileCheck -check-prefix=NON-TESTABLE %s
11+
// NON-TESTABLE: error: module 'abcde' was not compiled for testing
12+
1013
var a: A? // expected-error {{cannot find type 'A' in scope}}
1114
var qA: abcde.A? // expected-error {{cannot find type 'abcde' in scope}}

tools/SourceKit/lib/SwiftLang/CodeCompletionOrganizer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,9 @@ ImportDepth::ImportDepth(ASTContext &context,
347347
// Imports from -import-name such as Playground auxiliary sources are treated
348348
// specially by applying import depth 0.
349349
llvm::StringSet<> auxImports;
350-
for (StringRef moduleName :
350+
for (const auto &pair :
351351
invocation.getFrontendOptions().getImplicitImportModuleNames())
352-
auxImports.insert(moduleName);
352+
auxImports.insert(pair.first);
353353

354354
// Private imports from this module.
355355
// FIXME: only the private imports from the current source file.

0 commit comments

Comments
 (0)