[interop][SwiftToCxx] avoid -Wshadow warning for C++ representation of enum member parameters

This commit is contained in:
Alex Lorenz
2023-02-08 12:33:07 -08:00
parent 8305c7d5de
commit 0b0cc99c98
6 changed files with 56 additions and 7 deletions

View File

@@ -639,6 +639,25 @@ static void printDirectReturnOrParamCType(
});
}
/// Make adjustments to the Swift parameter name in generated C++, to
/// avoid things like additional warnings.
static void renameCxxParameterIfNeeded(const AbstractFunctionDecl *FD,
std::string &paramName) {
if (paramName.empty())
return;
const auto *enumDecl = FD->getDeclContext()->getSelfEnumDecl();
if (!enumDecl)
return;
// Rename a parameter in an enum method that shadows an existing case name,
// to avoid a -Wshadow warning in Clang.
for (const auto *Case : enumDecl->getAllElements()) {
if (Case->getNameStr() == paramName) {
paramName = (llvm::Twine(paramName) + "_").str();
return;
}
}
}
ClangRepresentation DeclAndTypeClangFunctionPrinter::printFunctionSignature(
const AbstractFunctionDecl *FD, const LoweredFunctionSignature &signature,
StringRef name, Type resultTy, FunctionSignatureKind kind,
@@ -895,6 +914,7 @@ ClangRepresentation DeclAndTypeClangFunctionPrinter::printFunctionSignature(
param, param->getInterfaceType());
std::string paramName =
param->getName().empty() ? "" : param->getName().str().str();
renameCxxParameterIfNeeded(FD, paramName);
// Always emit a named parameter for the C++ inline thunk to ensure it
// can be referenced in the body.
if (kind == FunctionSignatureKind::CxxInlineThunk && paramName.empty()) {
@@ -1113,6 +1133,7 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
paramOS << "_" << paramIndex;
} else {
paramName = param.getName().str().str();
renameCxxParameterIfNeeded(FD, paramName);
}
++paramIndex;
printCxxToCFunctionParameterUse(param.getInterfaceType(), paramName,

View File

@@ -81,8 +81,7 @@ public:
using Index =
decltype(reinterpret_cast<Collection *>(0x123)->getStartIndex());
SWIFT_INLINE_THUNK CollectionIterator(const Collection &collection)
: collection(collection) {
SWIFT_INLINE_THUNK CollectionIterator(const Collection &c) : collection(c) {
index = collection.getStartIndex();
endIndex = collection.getEndIndex();
// FIXME: Begin read access.

View File

@@ -0,0 +1,31 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -typecheck -module-name Enums -clang-header-expose-decls=all-public -emit-clang-header-path %t/enums.h
// RUN: %FileCheck %s < %t/enums.h
// RUN: %check-interop-cxx-header-in-clang(%t/enums.h -Wno-unused-private-field -Wno-unused-function)
public enum E {
case a
case b(Int)
public init(_ b: Int) {
self = .b(b)
}
public func takeParamA(_ a: Int) {}
public static func takeParamB(_ b: Int) {}
}
// CHECK: static inline E init(swift::Int b_)
// CHECK: inline void takeParamA(swift::Int a_)
// CHECK: static inline void takeParamB(swift::Int b_)
// CHECK: E E::init(swift::Int b_) {
// CHECK: _impl::$s5Enums1EOyACSicfC(b_)
// CHECK: void E::takeParamA(swift::Int a_) const {
// CHECK: _impl::$s5Enums1EO10takeParamAyySiF(a_,
// CHECK: void E::takeParamB(swift::Int b_) {
// CHECK: return _impl::$s5Enums1EO10takeParamByySiFZ(b_);

View File

@@ -2,7 +2,7 @@
// RUN: %target-swift-frontend %s -typecheck -module-name Functions -enable-experimental-cxx-interop -emit-clang-header-path %t/functions.h
// RUN: %FileCheck %s < %t/functions.h
// RUN: %check-interop-cxx-header-in-clang(%t/functions.h -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY -Wno-shadow -Wno-unused-function)
// RUN: %check-interop-cxx-header-in-clang(%t/functions.h -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY -Wno-unused-function)
// CHECK-LABEL: namespace Functions __attribute__((swift_private)) SWIFT_SYMBOL_MODULE("Functions") {

View File

@@ -7,7 +7,7 @@
// RUN: cat %t/stdlib.h %t/stdlib2.h > %t/two_includes.h
// 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)
// RUN: %check-interop-cxx-header-in-clang(-DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY %t/two_includes.h -Wno-unused-private-field -Wno-unused-function)
@_expose(Cxx)
public func test() -> String {

View File

@@ -2,9 +2,7 @@
// 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
// RUN: %FileCheck %s < %t/Swift.h
// 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)
// FIXME: remove need for -Wno-shadow
// RUN: %check-interop-cxx-header-in-clang(%t/Swift.h -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY -Wno-unused-private-field -Wno-unused-function)
// CHECK: namespace Swift __attribute__((swift_private)) SWIFT_SYMBOL_MODULE("Swift") {