Skip to content

Commit 2dc262f

Browse files
committed
Clang importer: Don't add duplicate declarations to the Swift lookup tables.
1 parent f798d53 commit 2dc262f

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

lib/ClangImporter/SwiftLookupTable.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ bool SwiftLookupTable::matchesContext(clang::DeclContext *foundContext,
4141
return false;
4242
}
4343

44+
/// Determine whether the new declarations matches an existing declaration.
45+
static bool matchesExistingDecl(clang::Decl *decl, clang::Decl *existingDecl) {
46+
// If the canonical declarations are equivalent, we have a match.
47+
if (decl->getCanonicalDecl() == existingDecl->getCanonicalDecl()) {
48+
return true;
49+
}
50+
51+
return false;
52+
}
53+
4454
void SwiftLookupTable::addEntry(DeclName name, clang::NamedDecl *decl) {
4555
clang::DeclContext *context
4656
= decl->getDeclContext()->getRedeclContext()->getPrimaryContext();
@@ -64,6 +74,11 @@ void SwiftLookupTable::addEntry(DeclName name, clang::NamedDecl *decl) {
6474
auto &fullEntries = knownFull->second;
6575
for (auto &fullEntry : fullEntries) {
6676
if (fullEntry.Context == context) {
77+
// Check whether this entry matches any existing entry.
78+
for (auto existingDecl : fullEntry.Decls) {
79+
if (matchesExistingDecl(decl, existingDecl)) return;
80+
}
81+
6782
fullEntry.Decls.push_back(decl);
6883
return;
6984
}

test/IDE/Inputs/swift_name.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
#define SWIFT_NAME(X) __attribute__((swift_name(#X)))
22

3+
#ifndef SWIFT_ENUM_EXTRA
4+
# define SWIFT_ENUM_EXTRA
5+
#endif
6+
7+
#ifndef SWIFT_ENUM
8+
# define SWIFT_ENUM(_type, _name) \
9+
enum _name : _type _name; \
10+
enum SWIFT_ENUM_EXTRA _name : _type
11+
#endif
12+
313
// Renaming global variables.
414
int SNFoo SWIFT_NAME(Bar);
515

@@ -16,6 +26,13 @@ struct SNSomeStruct SNMakeSomeStructForX(double X) SWIFT_NAME(makeSomeStruct(x:)
1626
// Renaming typedefs.
1727
typedef int SNIntegerType SWIFT_NAME(MyInt);
1828

29+
// Renaming enumerations.
30+
SWIFT_ENUM(unsigned char, SNColorChoice) {
31+
SNColorRed,
32+
SNColorGreen,
33+
SNColorBlue
34+
};
35+
1936
// swift_private attribute
2037
void SNTransposeInPlace(struct SNSomeStruct *value) __attribute__((swift_private));
2138

test/IDE/dump_swift_lookup_tables.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// CHECK-NEXT: Bar --> Bar
66
// CHECK-NEXT: MyInt --> MyInt
77
// CHECK-NEXT: Point --> Point
8+
// CHECK-NEXT: SNColorChoice --> SNColorChoice
89
// CHECK-NEXT: SomeStruct --> SomeStruct
910
// CHECK-NEXT: __SNTransposeInPlace --> __SNTransposeInPlace
1011
// CHECK-NEXT: makeSomeStruct --> makeSomeStruct(x:y:), makeSomeStruct(x:)
@@ -16,6 +17,8 @@
1617
// CHECK-NEXT: TU: SNIntegerType
1718
// CHECK-NEXT: Point:
1819
// CHECK-NEXT: TU: SNPoint
20+
// CHECK-NEXT: SNColorChoice:
21+
// CHECK-NEXT: TU: SNColorChoice, SNColorChoice{{$}}
1922
// CHECK-NEXT: SomeStruct:
2023
// CHECK-NEXT: TU: SNSomeStruct
2124
// CHECK-NEXT: __SNTransposeInPlace:

0 commit comments

Comments
 (0)