Skip to content

Add an option to disable ClangImporter imports form source. … #28813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/ClangImporter/ClangImporterOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ class ClangImporterOptions {
/// When set, don't enforce warnings with -Werror.
bool DebuggerSupport = false;

/// When set, ClangImporter is disabled, and all requests go to the
/// DWARFImporter delegate.
bool DisableSourceImport = false;

/// Return a hash code of any components from these options that should
/// contribute to a Swift Bridging PCH hash.
llvm::hash_code getPCHHashComponents() const {
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ def debug_crash_after_parse : Flag<["-"], "debug-crash-after-parse">,
def debugger_support : Flag<["-"], "debugger-support">,
HelpText<"Process swift code as if running in the debugger">;

def disable_clangimporter_source_import : Flag<["-"],
"disable-clangimporter-source-import">,
HelpText<"Disable ClangImporter and forward all requests straight the DWARF importer.">;

def disable_arc_opts : Flag<["-"], "disable-arc-opts">,
HelpText<"Don't run SIL ARC optimization passes.">;
def disable_ossa_opts : Flag<["-"], "disable-ossa-opts">,
Expand Down
24 changes: 16 additions & 8 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1757,18 +1757,24 @@ ModuleDecl *ClangImporter::Implementation::loadModuleClang(
return finishLoadingClangModule(clangModule, /*preferOverlay=*/false);
}

ModuleDecl *ClangImporter::loadModule(
SourceLoc importLoc,
ArrayRef<std::pair<Identifier, SourceLoc>> path) {
ModuleDecl *MD = Impl.loadModuleClang(importLoc, path);
ModuleDecl *
ClangImporter::loadModule(SourceLoc importLoc,
ArrayRef<std::pair<Identifier, SourceLoc>> path) {
return Impl.loadModule(importLoc, path);
}

ModuleDecl *ClangImporter::Implementation::loadModule(
SourceLoc importLoc, ArrayRef<std::pair<Identifier, SourceLoc>> path) {
ModuleDecl *MD = nullptr;
if (!DisableSourceImport)
MD = loadModuleClang(importLoc, path);
if (!MD)
MD = Impl.loadModuleDWARF(importLoc, path);
MD = loadModuleDWARF(importLoc, path);
return MD;
}

ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
const clang::Module *clangModule,
bool findOverlay) {
const clang::Module *clangModule, bool findOverlay) {
assert(clangModule);

// Bump the generation count.
Expand Down Expand Up @@ -1954,6 +1960,7 @@ ClangImporter::Implementation::Implementation(
BridgingHeaderLookupTable(new SwiftLookupTable(nullptr)),
BuffersForDiagnostics(ctx.SourceMgr),
platformAvailability(ctx.LangOpts), nameImporter(),
DisableSourceImport(opts.DisableSourceImport),
DWARFImporter(dwarfImporterDelegate) {}

ClangImporter::Implementation::~Implementation() {
Expand Down Expand Up @@ -2613,7 +2620,8 @@ void ClangImporter::lookupTypeDecl(
clang::LookupResult lookupResult(sema, clangName, clang::SourceLocation(),
lookupKind);
bool foundViaClang = false;
if (sema.LookupName(lookupResult, /*Scope=*/nullptr)) {
if (!Impl.DisableSourceImport &&
sema.LookupName(lookupResult, /*Scope=*/nullptr)) {
for (auto clangDecl : lookupResult) {
if (!isa<clang::TypeDecl>(clangDecl) &&
!isa<clang::ObjCContainerDecl>(clangDecl) &&
Expand Down
12 changes: 10 additions & 2 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,10 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
bool shouldIgnoreBridgeHeaderTopLevelDecl(clang::Decl *D);

private:
/// When set, ClangImporter is disabled, and all requests go to the
/// DWARFImporter delegate.
bool DisableSourceImport;

/// The DWARF importer delegate, if installed.
DWARFImporterDelegate *DWARFImporter = nullptr;
public:
Expand All @@ -614,7 +618,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
private:
/// The list of Clang modules found in the debug info.
llvm::DenseMap<Identifier, LoadedFile *> DWARFModuleUnits;
public:

/// Load a module using the clang::CompilerInstance.
ModuleDecl *loadModuleClang(SourceLoc importLoc,
ArrayRef<std::pair<Identifier, SourceLoc>> path);
Expand All @@ -624,7 +628,11 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
ModuleDecl *loadModuleDWARF(SourceLoc importLoc,
ArrayRef<std::pair<Identifier, SourceLoc>> path);


public:
/// Load a module using either method.
ModuleDecl *loadModule(SourceLoc importLoc,
ArrayRef<std::pair<Identifier, SourceLoc>> path);

void recordImplicitUnwrapForDecl(ValueDecl *decl, bool isIUO) {
if (!isIUO)
return;
Expand Down
5 changes: 5 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,12 @@ static bool ParseClangImporterArgs(ClangImporterOptions &Opts,

if (Args.hasArg(OPT_warnings_as_errors))
Opts.ExtraArgs.push_back("-Werror");

Opts.DebuggerSupport |= Args.hasArg(OPT_debugger_support);

Opts.DisableSourceImport |=
Args.hasArg(OPT_disable_clangimporter_source_import);

return false;
}

Expand Down
18 changes: 18 additions & 0 deletions test/ClangImporter/disable-source-import.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %empty-directory(%t.mcp)

// This should fail only if -disable-clangimporter-source-import is present.

// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) \
// RUN: -enable-objc-interop -typecheck -I %S/Inputs/custom-modules \
// RUN: -module-cache-path %t.mcp %s | %FileCheck --allow-empty %s

// RUN: not %target-swift-frontend(mock-sdk: %clang-importer-sdk) \
// RUN: -enable-objc-interop -typecheck -o - -I %S/Inputs/custom-modules \
// RUN: -module-cache-path %t.mcp -disable-clangimporter-source-import %s \
// RUN: 2>&1 | %FileCheck --check-prefix=ERROR %s

import ExternIntX

public let y = x // ERROR: error

// CHECK-NOT: error