mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[interop][SwiftToCxx] avoid -Wshadow warning for C++ representation of enum member parameters
This commit is contained in:
@@ -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 ¶mName) {
|
||||||
|
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(
|
ClangRepresentation DeclAndTypeClangFunctionPrinter::printFunctionSignature(
|
||||||
const AbstractFunctionDecl *FD, const LoweredFunctionSignature &signature,
|
const AbstractFunctionDecl *FD, const LoweredFunctionSignature &signature,
|
||||||
StringRef name, Type resultTy, FunctionSignatureKind kind,
|
StringRef name, Type resultTy, FunctionSignatureKind kind,
|
||||||
@@ -895,6 +914,7 @@ ClangRepresentation DeclAndTypeClangFunctionPrinter::printFunctionSignature(
|
|||||||
param, param->getInterfaceType());
|
param, param->getInterfaceType());
|
||||||
std::string paramName =
|
std::string paramName =
|
||||||
param->getName().empty() ? "" : param->getName().str().str();
|
param->getName().empty() ? "" : param->getName().str().str();
|
||||||
|
renameCxxParameterIfNeeded(FD, paramName);
|
||||||
// Always emit a named parameter for the C++ inline thunk to ensure it
|
// Always emit a named parameter for the C++ inline thunk to ensure it
|
||||||
// can be referenced in the body.
|
// can be referenced in the body.
|
||||||
if (kind == FunctionSignatureKind::CxxInlineThunk && paramName.empty()) {
|
if (kind == FunctionSignatureKind::CxxInlineThunk && paramName.empty()) {
|
||||||
@@ -1113,6 +1133,7 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
|
|||||||
paramOS << "_" << paramIndex;
|
paramOS << "_" << paramIndex;
|
||||||
} else {
|
} else {
|
||||||
paramName = param.getName().str().str();
|
paramName = param.getName().str().str();
|
||||||
|
renameCxxParameterIfNeeded(FD, paramName);
|
||||||
}
|
}
|
||||||
++paramIndex;
|
++paramIndex;
|
||||||
printCxxToCFunctionParameterUse(param.getInterfaceType(), paramName,
|
printCxxToCFunctionParameterUse(param.getInterfaceType(), paramName,
|
||||||
|
|||||||
@@ -81,8 +81,7 @@ public:
|
|||||||
using Index =
|
using Index =
|
||||||
decltype(reinterpret_cast<Collection *>(0x123)->getStartIndex());
|
decltype(reinterpret_cast<Collection *>(0x123)->getStartIndex());
|
||||||
|
|
||||||
SWIFT_INLINE_THUNK CollectionIterator(const Collection &collection)
|
SWIFT_INLINE_THUNK CollectionIterator(const Collection &c) : collection(c) {
|
||||||
: collection(collection) {
|
|
||||||
index = collection.getStartIndex();
|
index = collection.getStartIndex();
|
||||||
endIndex = collection.getEndIndex();
|
endIndex = collection.getEndIndex();
|
||||||
// FIXME: Begin read access.
|
// FIXME: Begin read access.
|
||||||
|
|||||||
@@ -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_);
|
||||||
@@ -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: %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: %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") {
|
// CHECK-LABEL: namespace Functions __attribute__((swift_private)) SWIFT_SYMBOL_MODULE("Functions") {
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
// RUN: cat %t/stdlib.h %t/stdlib2.h > %t/two_includes.h
|
// 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)
|
@_expose(Cxx)
|
||||||
public func test() -> String {
|
public func test() -> String {
|
||||||
|
|||||||
@@ -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: %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: %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)
|
// RUN: %check-interop-cxx-header-in-clang(%t/Swift.h -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY -Wno-unused-private-field -Wno-unused-function)
|
||||||
|
|
||||||
// FIXME: remove need for -Wno-shadow
|
|
||||||
|
|
||||||
// CHECK: namespace Swift __attribute__((swift_private)) SWIFT_SYMBOL_MODULE("Swift") {
|
// CHECK: namespace Swift __attribute__((swift_private)) SWIFT_SYMBOL_MODULE("Swift") {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user