Skip to content

Commit

Permalink
Clang importer: Don't add duplicate declarations to the Swift lookup …
Browse files Browse the repository at this point in the history
…tables.
  • Loading branch information
DougGregor committed Dec 3, 2015
1 parent f798d53 commit 2dc262f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/ClangImporter/SwiftLookupTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ bool SwiftLookupTable::matchesContext(clang::DeclContext *foundContext,
return false;
}

/// Determine whether the new declarations matches an existing declaration.
static bool matchesExistingDecl(clang::Decl *decl, clang::Decl *existingDecl) {
// If the canonical declarations are equivalent, we have a match.
if (decl->getCanonicalDecl() == existingDecl->getCanonicalDecl()) {
return true;
}

return false;
}

void SwiftLookupTable::addEntry(DeclName name, clang::NamedDecl *decl) {
clang::DeclContext *context
= decl->getDeclContext()->getRedeclContext()->getPrimaryContext();
Expand All @@ -64,6 +74,11 @@ void SwiftLookupTable::addEntry(DeclName name, clang::NamedDecl *decl) {
auto &fullEntries = knownFull->second;
for (auto &fullEntry : fullEntries) {
if (fullEntry.Context == context) {
// Check whether this entry matches any existing entry.
for (auto existingDecl : fullEntry.Decls) {
if (matchesExistingDecl(decl, existingDecl)) return;
}

fullEntry.Decls.push_back(decl);
return;
}
Expand Down
17 changes: 17 additions & 0 deletions test/IDE/Inputs/swift_name.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#define SWIFT_NAME(X) __attribute__((swift_name(#X)))

#ifndef SWIFT_ENUM_EXTRA
# define SWIFT_ENUM_EXTRA
#endif

#ifndef SWIFT_ENUM
# define SWIFT_ENUM(_type, _name) \
enum _name : _type _name; \
enum SWIFT_ENUM_EXTRA _name : _type
#endif

// Renaming global variables.
int SNFoo SWIFT_NAME(Bar);

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

// Renaming enumerations.
SWIFT_ENUM(unsigned char, SNColorChoice) {
SNColorRed,
SNColorGreen,
SNColorBlue
};

// swift_private attribute
void SNTransposeInPlace(struct SNSomeStruct *value) __attribute__((swift_private));

Expand Down
3 changes: 3 additions & 0 deletions test/IDE/dump_swift_lookup_tables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// CHECK-NEXT: Bar --> Bar
// CHECK-NEXT: MyInt --> MyInt
// CHECK-NEXT: Point --> Point
// CHECK-NEXT: SNColorChoice --> SNColorChoice
// CHECK-NEXT: SomeStruct --> SomeStruct
// CHECK-NEXT: __SNTransposeInPlace --> __SNTransposeInPlace
// CHECK-NEXT: makeSomeStruct --> makeSomeStruct(x:y:), makeSomeStruct(x:)
Expand All @@ -16,6 +17,8 @@
// CHECK-NEXT: TU: SNIntegerType
// CHECK-NEXT: Point:
// CHECK-NEXT: TU: SNPoint
// CHECK-NEXT: SNColorChoice:
// CHECK-NEXT: TU: SNColorChoice, SNColorChoice{{$}}
// CHECK-NEXT: SomeStruct:
// CHECK-NEXT: TU: SNSomeStruct
// CHECK-NEXT: __SNTransposeInPlace:
Expand Down

0 comments on commit 2dc262f

Please sign in to comment.