Skip to content

Commit 44783e7

Browse files
committed
[Frontend] Add support for implicit import of _Backtracing
Once the API has gone through Swift Evolution, we will want to implicitly import the _Backtracing module. Add code to do that, but set it to off by default for now. rdar://105394140
1 parent a2fc8c3 commit 44783e7

File tree

22 files changed

+196
-23
lines changed

22 files changed

+196
-23
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,10 @@ option(SWIFT_IMPLICIT_CONCURRENCY_IMPORT
575575
"Implicitly import the Swift concurrency module"
576576
TRUE)
577577

578+
option(SWIFT_IMPLICIT_BACKTRACING_IMPORT
579+
"Implicitly import the Swift backtracing module"
580+
FALSE)
581+
578582
option(SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY
579583
"Enable build of the Swift concurrency module"
580584
FALSE)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Test if the Swift compiler supports -disable-implicit-<module>-module-import
2+
function(swift_supports_implicit_module module_name out_var)
3+
file(WRITE "${CMAKE_BINARY_DIR}/tmp/empty-check-${module_name}.swift" "")
4+
execute_process(
5+
COMMAND
6+
"${CMAKE_Swift_COMPILER}"
7+
-Xfrontend -disable-implicit-${module_name}-module-import
8+
-c - -o /dev/null
9+
INPUT_FILE
10+
"${CMAKE_BINARY_DIR}/tmp/empty-check-${module_name}.swift"
11+
OUTPUT_QUIET ERROR_QUIET
12+
RESULT_VARIABLE
13+
result
14+
)
15+
if(NOT result)
16+
set("${out_var}" "TRUE" PARENT_SCOPE)
17+
else()
18+
set("${out_var}" "FALSE" PARENT_SCOPE)
19+
endif()
20+
endfunction()

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ WARNING(warn_implicit_concurrency_import_failed,none,
172172
"unable to perform implicit import of \"_Concurrency\" module: no such module found", ())
173173
REMARK(warn_implicit_string_processing_import_failed,none,
174174
"unable to perform implicit import of \"_StringProcessing\" module: no such module found", ())
175+
REMARK(warn_implicit_backtracing_import_failed,none,
176+
"unable to perform implicit import of \"_Backtracing\" module: no such module found", ())
175177

176178
ERROR(error_module_name_required,none, "-module-name is required", ())
177179
ERROR(error_bad_module_name,none,

include/swift/AST/KnownIdentifiers.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ IDENTIFIER(version)
263263
IDENTIFIER_(StringProcessing)
264264
IDENTIFIER(RegexBuilder)
265265

266+
// Backtracing
267+
IDENTIFIER_(Backtracing)
268+
266269
// Distributed actors
267270
IDENTIFIER(ActorID)
268271
IDENTIFIER(ActorSystem)

include/swift/Basic/LangOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,10 @@ namespace swift {
352352
/// Disable the implicit import of the _StringProcessing module.
353353
bool DisableImplicitStringProcessingModuleImport = false;
354354

355+
/// Disable the implicit import of the _Backtracing module.
356+
bool DisableImplicitBacktracingModuleImport =
357+
!SWIFT_IMPLICIT_BACKTRACING_IMPORT;
358+
355359
/// Should we check the target OSs of serialized modules to see that they're
356360
/// new enough?
357361
bool EnableTargetOSChecking = true;

include/swift/Config.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#cmakedefine01 SWIFT_IMPLICIT_CONCURRENCY_IMPORT
1212

13+
#cmakedefine01 SWIFT_IMPLICIT_BACKTRACING_IMPORT
14+
1315
#cmakedefine01 SWIFT_ENABLE_EXPERIMENTAL_DISTRIBUTED
1416

1517
#cmakedefine01 SWIFT_ENABLE_GLOBAL_ISEL_ARM64

include/swift/Frontend/Frontend.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ class CompilerInvocation {
380380
/// imported.
381381
bool shouldImportSwiftStringProcessing() const;
382382

383+
/// Whether the Swift Backtracing support library should be implicitly
384+
/// imported.
385+
bool shouldImportSwiftBacktracing() const;
386+
383387
/// Performs input setup common to these tools:
384388
/// sil-opt, sil-func-extractor, sil-llvm-gen, and sil-nm.
385389
/// Return value includes the buffer so caller can keep it alive.
@@ -575,6 +579,14 @@ class CompilerInstance {
575579
/// i.e. if it can be found.
576580
bool canImportSwiftStringProcessing() const;
577581

582+
/// Verify that if an implicit import of the `Backtracing` module if
583+
/// expected, it can actually be imported. Emit a warning, otherwise.
584+
void verifyImplicitBacktracingImport();
585+
586+
/// Whether the Swift Backtracing support library can be imported
587+
/// i.e. if it can be found.
588+
bool canImportSwiftBacktracing() const;
589+
578590
/// Whether the CxxShim library can be imported
579591
/// i.e. if it can be found.
580592
bool canImportCxxShim() const;

include/swift/Option/FrontendOptions.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,14 @@ def disable_implicit_string_processing_module_import : Flag<["-"],
441441
"disable-implicit-string-processing-module-import">,
442442
HelpText<"Disable the implicit import of the _StringProcessing module.">;
443443

444+
def enable_implicit_backtracing_module_import : Flag<["-"],
445+
"enable-implicit-backtracing-module-import">,
446+
HelpText<"Enable the implicit import of the _Backtracing module.">;
447+
448+
def disable_implicit_backtracing_module_import : Flag<["-"],
449+
"disable-implicit-backtracing-module-import">,
450+
HelpText<"Disable the implicit import of the _Backtracing module.">;
451+
444452
def disable_arc_opts : Flag<["-"], "disable-arc-opts">,
445453
HelpText<"Don't run SIL ARC optimization passes.">;
446454
def disable_ossa_opts : Flag<["-"], "disable-ossa-opts">,

include/swift/Strings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ constexpr static const StringLiteral SWIFT_CONCURRENCY_SHIMS_NAME = "_SwiftConcu
3030
constexpr static const StringLiteral SWIFT_DISTRIBUTED_NAME = "Distributed";
3131
/// The name of the StringProcessing module, which supports that extension.
3232
constexpr static const StringLiteral SWIFT_STRING_PROCESSING_NAME = "_StringProcessing";
33+
/// The name of the Backtracing module, which supports that extension.
34+
constexpr static const StringLiteral SWIFT_BACKTRACING_NAME = "_Backtracing";
3335
/// The name of the SwiftShims module, which contains private stdlib decls.
3436
constexpr static const StringLiteral SWIFT_SHIMS_NAME = "SwiftShims";
3537
/// The name of the CxxShim module, which contains a cxx casting utility.

lib/AST/NameLookup.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,22 @@ static void recordShadowedDeclsAfterTypeMatch(
662662
}
663663
}
664664

665+
// Next, prefer any other module over the _Backtracing module.
666+
if (auto spModule = ctx.getLoadedModule(ctx.Id_Backtracing)) {
667+
if ((firstModule == spModule) != (secondModule == spModule)) {
668+
// If second module is _StringProcessing, then it is shadowed by
669+
// first.
670+
if (secondModule == spModule) {
671+
shadowed.insert(secondDecl);
672+
continue;
673+
}
674+
675+
// Otherwise, the first declaration is shadowed by the second.
676+
shadowed.insert(firstDecl);
677+
break;
678+
}
679+
}
680+
665681
// The Foundation overlay introduced Data.withUnsafeBytes, which is
666682
// treated as being ambiguous with SwiftNIO's Data.withUnsafeBytes
667683
// extension. Apply a special-case name shadowing rule to use the

0 commit comments

Comments
 (0)