Skip to content

Commit f798d53

Browse files
committed
Clang importer: convert more importName callers over to importFullName. NFC
The sole remaining caller to importName is for enumerators, which may have prefixes that need stripping. That refactor will come in a subsequent commit.
1 parent 3caf703 commit f798d53

File tree

6 files changed

+42
-17
lines changed

6 files changed

+42
-17
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,8 +743,7 @@ bool ClangImporter::Implementation::importHeader(
743743

744744
if (UseSwiftLookupTables) {
745745
if (auto named = dyn_cast<clang::NamedDecl>(D)) {
746-
bool hasCustomName;
747-
if (DeclName name = importFullName(named, hasCustomName))
746+
if (DeclName name = importFullName(named))
748747
BridgingHeaderLookupTable.addEntry(name, named);
749748
}
750749
}

lib/ClangImporter/ImportDecl.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,12 +1064,16 @@ makeBitFieldAccessors(ClangImporter::Implementation &Impl,
10641064
/// generated name will most likely be unique.
10651065
static Identifier getClangDeclName(ClangImporter::Implementation &Impl,
10661066
const clang::TagDecl *decl) {
1067-
if (decl->getDeclName() || decl->hasAttr<clang::SwiftNameAttr>())
1068-
return Impl.importName(decl);
1069-
else if (auto *typedefForAnon = decl->getTypedefNameForAnonDecl())
1070-
return Impl.importName(typedefForAnon);
1071-
1072-
Identifier name;
1067+
// Import the name of this declaration.
1068+
Identifier name = Impl.importFullName(decl).getBaseName();
1069+
if (!name.empty()) return name;
1070+
1071+
// If that didn't succeed, check whether this is an anonymous tag declaration
1072+
// with a corresponding typedef-name declaration.
1073+
if (decl->getDeclName().isEmpty()) {
1074+
if (auto *typedefForAnon = decl->getTypedefNameForAnonDecl())
1075+
return Impl.importFullName(typedefForAnon).getBaseName();
1076+
}
10731077

10741078
if (!decl->isRecord())
10751079
return name;
@@ -1518,7 +1522,7 @@ namespace {
15181522
}
15191523

15201524
Decl *VisitTypedefNameDecl(const clang::TypedefNameDecl *Decl) {
1521-
auto Name = Impl.importName(Decl);
1525+
auto Name = Impl.importFullName(Decl).getBaseName();
15221526
if (Name.empty())
15231527
return nullptr;
15241528

@@ -2684,7 +2688,7 @@ namespace {
26842688
return nullptr;
26852689
}
26862690
}
2687-
auto name = Impl.importName(decl);
2691+
auto name = Impl.importFullName(decl).getBaseName();
26882692
if (name.empty())
26892693
return nullptr;
26902694

@@ -2790,7 +2794,7 @@ namespace {
27902794

27912795
Decl *VisitFieldDecl(const clang::FieldDecl *decl) {
27922796
// Fields are imported as variables.
2793-
auto name = Impl.importName(decl);
2797+
auto name = Impl.importFullName(decl).getBaseName();
27942798
if (name.empty())
27952799
return nullptr;
27962800

@@ -2836,7 +2840,7 @@ namespace {
28362840
return nullptr;
28372841

28382842
// Variables are imported as... variables.
2839-
auto name = Impl.importName(decl);
2843+
auto name = Impl.importFullName(decl).getBaseName();
28402844
if (name.empty())
28412845
return nullptr;
28422846

@@ -5019,7 +5023,7 @@ namespace {
50195023
}
50205024

50215025
Decl *VisitObjCProtocolDecl(const clang::ObjCProtocolDecl *decl) {
5022-
Identifier name = Impl.importName(decl);
5026+
Identifier name = Impl.importFullName(decl).getBaseName();
50235027
if (name.empty())
50245028
return nullptr;
50255029

@@ -5173,7 +5177,7 @@ namespace {
51735177
}
51745178

51755179
Decl *VisitObjCInterfaceDecl(const clang::ObjCInterfaceDecl *decl) {
5176-
auto name = Impl.importName(decl);
5180+
auto name = Impl.importFullName(decl).getBaseName();
51775181
if (name.empty())
51785182
return nullptr;
51795183

@@ -5389,7 +5393,7 @@ namespace {
53895393

53905394
Decl *VisitObjCPropertyDecl(const clang::ObjCPropertyDecl *decl,
53915395
DeclContext *dc) {
5392-
auto name = Impl.importName(decl);
5396+
auto name = Impl.importFullName(decl).getBaseName();
53935397
if (name.empty())
53945398
return nullptr;
53955399

lib/ClangImporter/ImportType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,7 @@ Type ClangImporter::Implementation::importFunctionType(
14251425
}
14261426

14271427
// Figure out the name for this parameter.
1428-
Identifier bodyName = importName(param);
1428+
Identifier bodyName = importFullName(param).getBaseName();
14291429

14301430
// Retrieve the argument name.
14311431
Identifier name;
@@ -2427,7 +2427,7 @@ Type ClangImporter::Implementation::importMethodType(
24272427
}
24282428

24292429
// Figure out the name for this parameter.
2430-
Identifier bodyName = importName(param);
2430+
Identifier bodyName = importFullName(param).getBaseName();
24312431

24322432
// Figure out the name for this argument, which comes from the method name.
24332433
Identifier name;

lib/ClangImporter/ImporterImpl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,15 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
718718
/// \brief Converts the given Swift identifier for Clang.
719719
clang::DeclarationName exportName(Identifier name);
720720

721+
/// Imports the full name of the given Clang declaration into Swift.
722+
///
723+
/// Note that this may result in a name very different from the Clang name,
724+
/// so it should not be used when referencing Clang symbols.
725+
DeclName importFullName(const clang::NamedDecl *D) {
726+
bool hasCustomName;
727+
return importFullName(D, hasCustomName);
728+
}
729+
721730
/// Imports the full name of the given Clang declaration into Swift.
722731
///
723732
/// Note that this may result in a name very different from the Clang name,

test/IDE/Inputs/swift_name.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,12 @@ struct SNSomeStruct SNMakeSomeStruct(double X, double Y) SWIFT_NAME(makeSomeStru
1313

1414
struct SNSomeStruct SNMakeSomeStructForX(double X) SWIFT_NAME(makeSomeStruct(x:));
1515

16+
// Renaming typedefs.
17+
typedef int SNIntegerType SWIFT_NAME(MyInt);
18+
1619
// swift_private attribute
1720
void SNTransposeInPlace(struct SNSomeStruct *value) __attribute__((swift_private));
21+
22+
typedef struct {
23+
double x, y, z;
24+
} SNPoint SWIFT_NAME(Point);

test/IDE/dump_swift_lookup_tables.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33

44
// CHECK: Base -> full name mappings:
55
// CHECK-NEXT: Bar --> Bar
6+
// CHECK-NEXT: MyInt --> MyInt
7+
// CHECK-NEXT: Point --> Point
68
// CHECK-NEXT: SomeStruct --> SomeStruct
79
// CHECK-NEXT: __SNTransposeInPlace --> __SNTransposeInPlace
810
// CHECK-NEXT: makeSomeStruct --> makeSomeStruct(x:y:), makeSomeStruct(x:)
911

1012
// CHECK: Full name -> entry mappings:
1113
// CHECK-NEXT: Bar:
1214
// CHECK-NEXT: TU: SNFoo
15+
// CHECK-NEXT: MyInt:
16+
// CHECK-NEXT: TU: SNIntegerType
17+
// CHECK-NEXT: Point:
18+
// CHECK-NEXT: TU: SNPoint
1319
// CHECK-NEXT: SomeStruct:
1420
// CHECK-NEXT: TU: SNSomeStruct
1521
// CHECK-NEXT: __SNTransposeInPlace:

0 commit comments

Comments
 (0)