Skip to content

Commit 0b0cc99

Browse files
committed
[interop][SwiftToCxx] avoid -Wshadow warning for C++ representation of enum member parameters
1 parent 8305c7d commit 0b0cc99

File tree

6 files changed

+56
-7
lines changed

6 files changed

+56
-7
lines changed

Diff for: lib/PrintAsClang/PrintClangFunction.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,25 @@ static void printDirectReturnOrParamCType(
639639
});
640640
}
641641

642+
/// Make adjustments to the Swift parameter name in generated C++, to
643+
/// avoid things like additional warnings.
644+
static void renameCxxParameterIfNeeded(const AbstractFunctionDecl *FD,
645+
std::string &paramName) {
646+
if (paramName.empty())
647+
return;
648+
const auto *enumDecl = FD->getDeclContext()->getSelfEnumDecl();
649+
if (!enumDecl)
650+
return;
651+
// Rename a parameter in an enum method that shadows an existing case name,
652+
// to avoid a -Wshadow warning in Clang.
653+
for (const auto *Case : enumDecl->getAllElements()) {
654+
if (Case->getNameStr() == paramName) {
655+
paramName = (llvm::Twine(paramName) + "_").str();
656+
return;
657+
}
658+
}
659+
}
660+
642661
ClangRepresentation DeclAndTypeClangFunctionPrinter::printFunctionSignature(
643662
const AbstractFunctionDecl *FD, const LoweredFunctionSignature &signature,
644663
StringRef name, Type resultTy, FunctionSignatureKind kind,
@@ -895,6 +914,7 @@ ClangRepresentation DeclAndTypeClangFunctionPrinter::printFunctionSignature(
895914
param, param->getInterfaceType());
896915
std::string paramName =
897916
param->getName().empty() ? "" : param->getName().str().str();
917+
renameCxxParameterIfNeeded(FD, paramName);
898918
// Always emit a named parameter for the C++ inline thunk to ensure it
899919
// can be referenced in the body.
900920
if (kind == FunctionSignatureKind::CxxInlineThunk && paramName.empty()) {
@@ -1113,6 +1133,7 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
11131133
paramOS << "_" << paramIndex;
11141134
} else {
11151135
paramName = param.getName().str().str();
1136+
renameCxxParameterIfNeeded(FD, paramName);
11161137
}
11171138
++paramIndex;
11181139
printCxxToCFunctionParameterUse(param.getInterfaceType(), paramName,

Diff for: lib/PrintAsClang/_SwiftStdlibCxxOverlay.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ template <class Collection, class T> class CollectionIterator {
8181
using Index =
8282
decltype(reinterpret_cast<Collection *>(0x123)->getStartIndex());
8383

84-
SWIFT_INLINE_THUNK CollectionIterator(const Collection &collection)
85-
: collection(collection) {
84+
SWIFT_INLINE_THUNK CollectionIterator(const Collection &c) : collection(c) {
8685
index = collection.getStartIndex();
8786
endIndex = collection.getEndIndex();
8887
// FIXME: Begin read access.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %s -typecheck -module-name Enums -clang-header-expose-decls=all-public -emit-clang-header-path %t/enums.h
3+
// RUN: %FileCheck %s < %t/enums.h
4+
5+
// RUN: %check-interop-cxx-header-in-clang(%t/enums.h -Wno-unused-private-field -Wno-unused-function)
6+
7+
public enum E {
8+
case a
9+
case b(Int)
10+
11+
public init(_ b: Int) {
12+
self = .b(b)
13+
}
14+
15+
public func takeParamA(_ a: Int) {}
16+
17+
public static func takeParamB(_ b: Int) {}
18+
}
19+
20+
// CHECK: static inline E init(swift::Int b_)
21+
// CHECK: inline void takeParamA(swift::Int a_)
22+
// CHECK: static inline void takeParamB(swift::Int b_)
23+
24+
// CHECK: E E::init(swift::Int b_) {
25+
// CHECK: _impl::$s5Enums1EOyACSicfC(b_)
26+
27+
// CHECK: void E::takeParamA(swift::Int a_) const {
28+
// CHECK: _impl::$s5Enums1EO10takeParamAyySiF(a_,
29+
30+
// CHECK: void E::takeParamB(swift::Int b_) {
31+
// CHECK: return _impl::$s5Enums1EO10takeParamByySiFZ(b_);

Diff for: test/Interop/SwiftToCxx/functions/swift-functions-errors.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// RUN: %target-swift-frontend %s -typecheck -module-name Functions -enable-experimental-cxx-interop -emit-clang-header-path %t/functions.h
33
// RUN: %FileCheck %s < %t/functions.h
44

5-
// RUN: %check-interop-cxx-header-in-clang(%t/functions.h -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY -Wno-shadow -Wno-unused-function)
5+
// RUN: %check-interop-cxx-header-in-clang(%t/functions.h -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY -Wno-unused-function)
66

77
// CHECK-LABEL: namespace Functions __attribute__((swift_private)) SWIFT_SYMBOL_MODULE("Functions") {
88

Diff for: test/Interop/SwiftToCxx/stdlib/stdlib-dep-inline-in-cxx.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// RUN: cat %t/stdlib.h %t/stdlib2.h > %t/two_includes.h
99

10-
// RUN: %check-interop-cxx-header-in-clang(-DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY %t/two_includes.h -Wno-unused-private-field -Wno-unused-function -Wno-shadow)
10+
// RUN: %check-interop-cxx-header-in-clang(-DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY %t/two_includes.h -Wno-unused-private-field -Wno-unused-function)
1111

1212
@_expose(Cxx)
1313
public func test() -> String {

Diff for: test/Interop/SwiftToCxx/stdlib/swift-stdlib-in-cxx.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
// RUN: %target-swift-frontend -parse-as-library %platform-module-dir/Swift.swiftmodule/%module-target-triple.swiftinterface -enable-library-evolution -disable-objc-attr-requires-foundation-module -typecheck -module-name Swift -parse-stdlib -enable-experimental-cxx-interop -emit-clang-header-path %t/Swift.h -experimental-skip-all-function-bodies
33
// RUN: %FileCheck %s < %t/Swift.h
44

5-
// RUN: %check-interop-cxx-header-in-clang(%t/Swift.h -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY -Wno-unused-private-field -Wno-unused-function -Wno-shadow)
6-
7-
// FIXME: remove need for -Wno-shadow
5+
// RUN: %check-interop-cxx-header-in-clang(%t/Swift.h -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY -Wno-unused-private-field -Wno-unused-function)
86

97
// CHECK: namespace Swift __attribute__((swift_private)) SWIFT_SYMBOL_MODULE("Swift") {
108

0 commit comments

Comments
 (0)