Merge remote-tracking branch 'origin/main' into rebranch

This commit is contained in:
swift-ci
2025-05-09 11:19:56 -07:00
5 changed files with 138 additions and 4 deletions

View File

@@ -60,7 +60,9 @@
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjCCommon.h"
#include "clang/AST/Expr.h"
#include "clang/AST/PrettyPrinter.h"
#include "clang/AST/StmtVisitor.h"
#include "clang/AST/RecordLayout.h"
#include "clang/AST/Type.h"
#include "clang/Basic/Specifiers.h"
@@ -9140,6 +9142,51 @@ private:
};
} // namespace
namespace {
struct CountedByExpressionValidator
: clang::ConstStmtVisitor<CountedByExpressionValidator, bool> {
bool VisitDeclRefExpr(const clang::DeclRefExpr *e) { return true; }
bool VisitIntegerLiteral(const clang::IntegerLiteral *) { return true; }
bool VisitImplicitCastExpr(const clang::ImplicitCastExpr *c) {
return this->Visit(c->getSubExpr());
}
bool VisitParenExpr(const clang::ParenExpr *p) {
return this->Visit(p->getSubExpr());
}
#define SUPPORTED_UNOP(UNOP) \
bool VisitUnary ## UNOP(const clang::UnaryOperator *unop) { \
return this->Visit(unop->getSubExpr()); \
}
SUPPORTED_UNOP(Plus)
SUPPORTED_UNOP(Minus)
SUPPORTED_UNOP(Not)
#undef SUPPORTED_UNOP
#define SUPPORTED_BINOP(BINOP) \
bool VisitBin ## BINOP(const clang::BinaryOperator *binop) { \
return this->Visit(binop->getLHS()) && this->Visit(binop->getRHS()); \
}
SUPPORTED_BINOP(Add)
SUPPORTED_BINOP(Sub)
SUPPORTED_BINOP(Div)
SUPPORTED_BINOP(Mul)
SUPPORTED_BINOP(Rem)
SUPPORTED_BINOP(Shl)
SUPPORTED_BINOP(Shr)
SUPPORTED_BINOP(And)
SUPPORTED_BINOP(Xor)
SUPPORTED_BINOP(Or)
#undef SUPPORTED_BINOP
bool VisitStmt(const clang::Stmt *) { return false; }
};
} // namespace
static bool SwiftifiableCAT(const clang::CountAttributedType *CAT) {
return CAT && CountedByExpressionValidator().Visit(CAT->getCountExpr());
}
void ClangImporter::Implementation::swiftify(FuncDecl *MappedDecl) {
if (!SwiftContext.LangOpts.hasFeature(Feature::SafeInteropWrappers))
return;
@@ -9175,8 +9222,8 @@ void ClangImporter::Implementation::swiftify(FuncDecl *MappedDecl) {
SwiftifyInfoPrinter printer(getClangASTContext(), SwiftContext, out);
bool returnIsStdSpan = registerStdSpanTypeMapping(
MappedDecl->getResultInterfaceType(), ClangDecl->getReturnType());
if (auto CAT =
ClangDecl->getReturnType()->getAs<clang::CountAttributedType>()) {
auto *CAT = ClangDecl->getReturnType()->getAs<clang::CountAttributedType>();
if (SwiftifiableCAT(CAT)) {
printer.printCountedBy(CAT, SwiftifyInfoPrinter::RETURN_VALUE_INDEX);
attachMacro = true;
}
@@ -9191,7 +9238,8 @@ void ClangImporter::Implementation::swiftify(FuncDecl *MappedDecl) {
auto clangParamTy = clangParam->getType();
auto swiftParam = MappedDecl->getParameters()->get(index);
bool paramHasBoundsInfo = false;
if (auto CAT = clangParamTy->getAs<clang::CountAttributedType>()) {
auto *CAT = clangParamTy->getAs<clang::CountAttributedType>();
if (SwiftifiableCAT(CAT)) {
printer.printCountedBy(CAT, index);
attachMacro = paramHasBoundsInfo = true;
}