Skip to content

Commit

Permalink
Merge pull request #743 from kballard/enum-constant-prefix
Browse files Browse the repository at this point in the history
[ClangImporter] Ignore swift_name when calculating enum constant prefix
  • Loading branch information
jrose-apple committed Dec 23, 2015
2 parents 5b171b6 + f047c45 commit c217f01
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
14 changes: 4 additions & 10 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,7 @@ StringRef ClangImporter::Implementation::getEnumConstantNamePrefix(
}
}

// Compute th e common prefix.
// Compute the common prefix.
StringRef commonPrefix = (*ec)->getName();
bool followedByNonIdentifier = false;
for (++ec; ec != ecEnd; ++ec) {
Expand Down Expand Up @@ -1602,15 +1602,9 @@ StringRef ClangImporter::Implementation::getEnumConstantNamePrefix(
checkPrefix = checkPrefix.drop_front();
}

// Account for the enum being imported using
// __attribute__((swift_private)). This is a little ad hoc, but it's a
// rare case anyway.
Identifier enumName = importFullName(decl, None, nullptr, &sema).Imported
.getBaseName();
StringRef enumNameStr = enumName.str();
if (enumNameStr.startswith("__") && !checkPrefix.startswith("__"))
enumNameStr = enumNameStr.drop_front(2);

// Don't use importFullName() here, we want to ignore the swift_name
// and swift_private attributes.
StringRef enumNameStr = decl->getName();
StringRef commonWithEnum = getCommonPluralPrefix(checkPrefix,
enumNameStr);
size_t delta = commonPrefix.size() - checkPrefix.size();
Expand Down
31 changes: 31 additions & 0 deletions test/IDE/Inputs/print_clang_header_swift_name.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@import Foundation;

#define SWIFT_COMPILE_NAME(X) __attribute__((swift_name(X)))

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

typedef SWIFT_ENUM(NSInteger, Normal) {
NormalOne = 0,
NormalTwo,
NormalThree
};

// FIXME (#618): Use SWIFT_ENUM_NAMED() when support for that lands
#undef SWIFT_ENUM
#define SWIFT_ENUM(_type, _name) enum _name : _type _name SWIFT_COMPILE_NAME(SWIFT_ENUM_NAME); enum SWIFT_COMPILE_NAME(SWIFT_ENUM_NAME) SWIFT_ENUM_EXTRA _name : _type

#define SWIFT_ENUM_NAME "SwiftEnum"
typedef SWIFT_ENUM(NSInteger, ObjCEnum) {
ObjCEnumOne = 0,
ObjCEnumTwo,
ObjCEnumThree
};

#undef SWIFT_ENUM_NAME
#define SWIFT_ENUM_NAME "SwiftEnumTwo"
typedef SWIFT_ENUM(NSInteger, ObjCEnumTwo) {
// the following shouldn't have their prefixes stripped
SwiftEnumTwoA,
SwiftEnumTwoB,
SwiftEnumTwoC
};
25 changes: 25 additions & 0 deletions test/IDE/print_clang_header_swift_name.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: echo '#include "print_clang_header_swift_name.h"' > %t.m
// RUN: %target-swift-ide-test -source-filename %s -print-header -header-to-print \
// RUN: %S/Inputs/print_clang_header_swift_name.h --cc-args %target-cc-options \
// RUN: -isysroot %clang-importer-sdk-path -fsyntax-only %t.m -I %S/Inputs | FileCheck %s

// CHECK: enum Normal : Int {
// CHECK-NOT: {{^}}}
// CHECK: case One
// CHECK-NEXT: case Two
// CHECK-NEXT: case Three
// CHECK-NEXT: }

// CHECK: enum SwiftEnum : Int {
// CHECK-NOT: {{^}}}
// CHECK: case One
// CHECK-NEXT: case Two
// CHECK-NEXT: case Three
// CHECK-NEXT: }

// CHECK: enum SwiftEnumTwo : Int {
// CHECK-NOT: {{^}}}
// CHECK: case SwiftEnumTwoA
// CHECK-NEXT: case SwiftEnumTwoB
// CHECK-NEXT: case SwiftEnumTwoC
// CHECK-NEXT: }

0 comments on commit c217f01

Please sign in to comment.