Skip to content

[cxx-interop] Use translation unit scope when doing lookups #66897

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 4 commits into from
Jun 23, 2023
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
2 changes: 1 addition & 1 deletion lib/ClangImporter/ClangAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ clang::TypedefNameDecl *importer::findSwiftNewtype(const clang::NamedDecl *decl,
clang::LookupResult lookupResult(clangSema, notificationName,
clang::SourceLocation(),
clang::Sema::LookupOrdinaryName);
if (!clangSema.LookupName(lookupResult, nullptr))
if (!clangSema.LookupName(lookupResult, clangSema.TUScope))
return nullptr;
auto nsDecl = lookupResult.getAsSingle<clang::TypedefNameDecl>();
if (!nsDecl)
Expand Down
2 changes: 1 addition & 1 deletion lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2757,7 +2757,7 @@ ClangImporter::Implementation::lookupTypedef(clang::DeclarationName name) {
clang::SourceLocation(),
clang::Sema::LookupOrdinaryName);

if (sema.LookupName(lookupResult, /*scope=*/nullptr)) {
if (sema.LookupName(lookupResult, sema.TUScope)) {
for (auto decl : lookupResult) {
if (auto typedefDecl =
dyn_cast<clang::TypedefNameDecl>(decl->getUnderlyingDecl()))
Expand Down
4 changes: 3 additions & 1 deletion lib/ClangImporter/ImportMacro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ static Optional<std::pair<llvm::APSInt, Type>>
}

// Macro identifier.
// TODO: for some reason when in C++ mode, "hasMacroDefinition" is often
// false: rdar://110071334
} else if (token.is(clang::tok::identifier) &&
token.getIdentifierInfo()->hasMacroDefinition()) {

Expand Down Expand Up @@ -422,7 +424,7 @@ static ValueDecl *importMacro(ClangImporter::Implementation &impl,
auto diagState = impl.getClangSema().DelayedDiagnostics.push(diagPool);
auto parsedType = impl.getClangSema().getTypeName(identifier,
clang::SourceLocation(),
/*scope*/nullptr);
impl.getClangSema().TUScope);
impl.getClangSema().DelayedDiagnostics.popWithoutEmitting(diagState);

if (parsedType && diagPool.empty()) {
Expand Down
2 changes: 2 additions & 0 deletions test/ClangImporter/cf.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// RUN: %target-swift-frontend -disable-objc-attr-requires-foundation-module -typecheck -verify -import-cf-types -I %S/Inputs/custom-modules %s

// RUN: %target-swift-frontend -disable-objc-attr-requires-foundation-module -typecheck -verify -import-cf-types -enable-experimental-cxx-interop -I %S/Inputs/custom-modules %s

// REQUIRES: objc_interop

import CoreCooling
Expand Down
3 changes: 3 additions & 0 deletions test/ClangImporter/macros.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-objc-interop -typecheck -verify %s

// Most of these don't pass: rdar://110071334
// %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-experimental-cxx-interop -enable-objc-interop -typecheck -verify %s

@_exported import macros

func circle_area(_ radius: CDouble) -> CDouble {
Expand Down
6 changes: 6 additions & 0 deletions test/Interop/Cxx/objc-correctness/Inputs/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ module CxxClassWithNSStringInit [extern_c] {
module NSOptionsMangling {
header "NSOptionsMangling.h"
}

module NSNofiticationBridging {
header "nsnotification-bridging.h"
requires objc
requires cplusplus
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#import <Foundation/Foundation.h>

extern NSString * const SpaceShipNotification;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %target-swift-ide-test -print-module -module-to-print=NSNofiticationBridging -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop -enable-objc-interop | %FileCheck %s

// REQUIRES: objc_interop

// CHECK: import Foundation

// CHECK: let SpaceShipNotification: String