Skip to content

Commit d595213

Browse files
Merge pull request #28813 from adrian-prantl/57880844
Add an option to disable ClangImporter imports form source. …
2 parents a36fd6d + 046c849 commit d595213

File tree

6 files changed

+57
-10
lines changed

6 files changed

+57
-10
lines changed

include/swift/ClangImporter/ClangImporterOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ class ClangImporterOptions {
9999
/// When set, don't enforce warnings with -Werror.
100100
bool DebuggerSupport = false;
101101

102+
/// When set, ClangImporter is disabled, and all requests go to the
103+
/// DWARFImporter delegate.
104+
bool DisableSourceImport = false;
105+
102106
/// Return a hash code of any components from these options that should
103107
/// contribute to a Swift Bridging PCH hash.
104108
llvm::hash_code getPCHHashComponents() const {

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ def debug_crash_after_parse : Flag<["-"], "debug-crash-after-parse">,
268268
def debugger_support : Flag<["-"], "debugger-support">,
269269
HelpText<"Process swift code as if running in the debugger">;
270270

271+
def disable_clangimporter_source_import : Flag<["-"],
272+
"disable-clangimporter-source-import">,
273+
HelpText<"Disable ClangImporter and forward all requests straight the DWARF importer.">;
274+
271275
def disable_arc_opts : Flag<["-"], "disable-arc-opts">,
272276
HelpText<"Don't run SIL ARC optimization passes.">;
273277
def disable_ossa_opts : Flag<["-"], "disable-ossa-opts">,

lib/ClangImporter/ClangImporter.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,18 +1757,24 @@ ModuleDecl *ClangImporter::Implementation::loadModuleClang(
17571757
return finishLoadingClangModule(clangModule, /*preferOverlay=*/false);
17581758
}
17591759

1760-
ModuleDecl *ClangImporter::loadModule(
1761-
SourceLoc importLoc,
1762-
ArrayRef<std::pair<Identifier, SourceLoc>> path) {
1763-
ModuleDecl *MD = Impl.loadModuleClang(importLoc, path);
1760+
ModuleDecl *
1761+
ClangImporter::loadModule(SourceLoc importLoc,
1762+
ArrayRef<std::pair<Identifier, SourceLoc>> path) {
1763+
return Impl.loadModule(importLoc, path);
1764+
}
1765+
1766+
ModuleDecl *ClangImporter::Implementation::loadModule(
1767+
SourceLoc importLoc, ArrayRef<std::pair<Identifier, SourceLoc>> path) {
1768+
ModuleDecl *MD = nullptr;
1769+
if (!DisableSourceImport)
1770+
MD = loadModuleClang(importLoc, path);
17641771
if (!MD)
1765-
MD = Impl.loadModuleDWARF(importLoc, path);
1772+
MD = loadModuleDWARF(importLoc, path);
17661773
return MD;
17671774
}
17681775

17691776
ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
1770-
const clang::Module *clangModule,
1771-
bool findOverlay) {
1777+
const clang::Module *clangModule, bool findOverlay) {
17721778
assert(clangModule);
17731779

17741780
// Bump the generation count.
@@ -1954,6 +1960,7 @@ ClangImporter::Implementation::Implementation(
19541960
BridgingHeaderLookupTable(new SwiftLookupTable(nullptr)),
19551961
BuffersForDiagnostics(ctx.SourceMgr),
19561962
platformAvailability(ctx.LangOpts), nameImporter(),
1963+
DisableSourceImport(opts.DisableSourceImport),
19571964
DWARFImporter(dwarfImporterDelegate) {}
19581965

19591966
ClangImporter::Implementation::~Implementation() {
@@ -2613,7 +2620,8 @@ void ClangImporter::lookupTypeDecl(
26132620
clang::LookupResult lookupResult(sema, clangName, clang::SourceLocation(),
26142621
lookupKind);
26152622
bool foundViaClang = false;
2616-
if (sema.LookupName(lookupResult, /*Scope=*/nullptr)) {
2623+
if (!Impl.DisableSourceImport &&
2624+
sema.LookupName(lookupResult, /*Scope=*/nullptr)) {
26172625
for (auto clangDecl : lookupResult) {
26182626
if (!isa<clang::TypeDecl>(clangDecl) &&
26192627
!isa<clang::ObjCContainerDecl>(clangDecl) &&

lib/ClangImporter/ImporterImpl.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,10 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
605605
bool shouldIgnoreBridgeHeaderTopLevelDecl(clang::Decl *D);
606606

607607
private:
608+
/// When set, ClangImporter is disabled, and all requests go to the
609+
/// DWARFImporter delegate.
610+
bool DisableSourceImport;
611+
608612
/// The DWARF importer delegate, if installed.
609613
DWARFImporterDelegate *DWARFImporter = nullptr;
610614
public:
@@ -614,7 +618,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
614618
private:
615619
/// The list of Clang modules found in the debug info.
616620
llvm::DenseMap<Identifier, LoadedFile *> DWARFModuleUnits;
617-
public:
621+
618622
/// Load a module using the clang::CompilerInstance.
619623
ModuleDecl *loadModuleClang(SourceLoc importLoc,
620624
ArrayRef<std::pair<Identifier, SourceLoc>> path);
@@ -624,7 +628,11 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
624628
ModuleDecl *loadModuleDWARF(SourceLoc importLoc,
625629
ArrayRef<std::pair<Identifier, SourceLoc>> path);
626630

627-
631+
public:
632+
/// Load a module using either method.
633+
ModuleDecl *loadModule(SourceLoc importLoc,
634+
ArrayRef<std::pair<Identifier, SourceLoc>> path);
635+
628636
void recordImplicitUnwrapForDecl(ValueDecl *decl, bool isIUO) {
629637
if (!isIUO)
630638
return;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,12 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts,
638638

639639
if (Args.hasArg(OPT_warnings_as_errors))
640640
Opts.ExtraArgs.push_back("-Werror");
641+
641642
Opts.DebuggerSupport |= Args.hasArg(OPT_debugger_support);
643+
644+
Opts.DisableSourceImport |=
645+
Args.hasArg(OPT_disable_clangimporter_source_import);
646+
642647
return false;
643648
}
644649

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %empty-directory(%t.mcp)
2+
3+
// This should fail only if -disable-clangimporter-source-import is present.
4+
5+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) \
6+
// RUN: -enable-objc-interop -typecheck -I %S/Inputs/custom-modules \
7+
// RUN: -module-cache-path %t.mcp %s | %FileCheck --allow-empty %s
8+
9+
// RUN: not %target-swift-frontend(mock-sdk: %clang-importer-sdk) \
10+
// RUN: -enable-objc-interop -typecheck -o - -I %S/Inputs/custom-modules \
11+
// RUN: -module-cache-path %t.mcp -disable-clangimporter-source-import %s \
12+
// RUN: 2>&1 | %FileCheck --check-prefix=ERROR %s
13+
14+
import ExternIntX
15+
16+
public let y = x // ERROR: error
17+
18+
// CHECK-NOT: error

0 commit comments

Comments
 (0)