Skip to content

Commit 9bcaf75

Browse files
committed
Mark functions with bounds attributes to process in...
importBoundsAttributes This allows an early exit without redundant processing or extra allocations.
1 parent e3651b6 commit 9bcaf75

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

lib/ClangImporter/ImportDecl.cpp

+25-32
Original file line numberDiff line numberDiff line change
@@ -8439,22 +8439,26 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
84398439
}
84408440

84418441
namespace {
8442-
class PointerParamInfo {
8442+
class PointerParamInfoPrinter {
84438443
public:
8444-
virtual void print(clang::ASTContext &ctx, llvm::raw_ostream &out) const = 0;
8445-
virtual ~PointerParamInfo() = default;
8446-
};
8444+
clang::ASTContext &ctx;
8445+
llvm::raw_ostream &out;
8446+
bool firstParam = true;
8447+
PointerParamInfoPrinter(clang::ASTContext &ctx, llvm::raw_ostream &out)
8448+
: ctx(ctx), out(out) {
8449+
out << "@PointerBounds(";
8450+
}
8451+
~PointerParamInfoPrinter() { out << ")"; }
84478452

8448-
class CountedByParam : public PointerParamInfo {
8449-
public:
8450-
size_t pointerIndex;
8451-
clang::Expr *countExpr;
8452-
bool isSizedBy;
8453-
CountedByParam(size_t idx, clang::Expr *E, bool sizedBy)
8454-
: pointerIndex(idx), countExpr(E), isSizedBy(sizedBy) {}
8455-
8456-
virtual void print(clang::ASTContext &ctx,
8457-
llvm::raw_ostream &out) const override {
8453+
void printCountedBy(const clang::CountAttributedType *CAT,
8454+
size_t pointerIndex) {
8455+
if (!firstParam) {
8456+
out << ", ";
8457+
} else {
8458+
firstParam = false;
8459+
}
8460+
clang::Expr *countExpr = CAT->getCountExpr();
8461+
bool isSizedBy = CAT->isCountInBytes();
84588462
out << ".";
84598463
if (isSizedBy)
84608464
out << "sizedBy";
@@ -8470,7 +8474,6 @@ class CountedByParam : public PointerParamInfo {
84708474
out, {}, {ctx.getLangOpts()}); // TODO: map clang::Expr to Swift Expr
84718475
out << "\")";
84728476
}
8473-
virtual ~CountedByParam() {}
84748477
};
84758478
} // namespace
84768479

@@ -8480,31 +8483,21 @@ void ClangImporter::Implementation::importBoundsAttributes(
84808483
dyn_cast_or_null<clang::FunctionDecl>(MappedDecl->getClangDecl());
84818484
if (!ClangDecl)
84828485
return;
8483-
8484-
SmallVector<PointerParamInfo *, 4> BoundsInfo;
8485-
size_t parameterIndex = 1;
8486-
for (auto param : ClangDecl->parameters()) {
8487-
if (auto CAT = param->getType()->getAs<clang::CountAttributedType>()) {
8488-
BoundsInfo.push_back(new CountedByParam(
8489-
parameterIndex, CAT->getCountExpr(), CAT->isCountInBytes()));
8490-
}
8491-
parameterIndex++;
8492-
}
8493-
if (BoundsInfo.empty())
8486+
if (!funcsWithPointerBounds.count(ClangDecl))
84948487
return;
84958488

84968489
llvm::SmallString<128> MacroString;
84978490
{
84988491
llvm::raw_svector_ostream out(MacroString);
84998492

8500-
out << "@PointerBounds(";
8501-
for (size_t i = 0; i < BoundsInfo.size(); i++) {
8502-
BoundsInfo[i]->print(getClangASTContext(), out);
8503-
if (i + 1 < BoundsInfo.size()) {
8504-
out << ", ";
8493+
size_t parameterIndex = 1;
8494+
PointerParamInfoPrinter printer(getClangASTContext(), out);
8495+
for (auto param : ClangDecl->parameters()) {
8496+
if (auto CAT = param->getType()->getAs<clang::CountAttributedType>()) {
8497+
printer.printCountedBy(CAT, parameterIndex);
85058498
}
8499+
parameterIndex++;
85068500
}
8507-
out << ")";
85088501
}
85098502

85108503
// Dig out a buffer with the attribute text.

lib/ClangImporter/ImportType.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -2428,6 +2428,9 @@ ClangImporter::Implementation::importParameterType(
24282428
// Treat as a normal pointer. importBoundsAttributes() will generate a safe
24292429
// overload later.
24302430
paramTy = CAT->desugar();
2431+
if (auto FuncD =
2432+
dyn_cast<clang::FunctionDecl>(param->getParentFunctionOrMethod()))
2433+
funcsWithPointerBounds.insert(FuncD);
24312434
} else if (isa<clang::PointerType>(paramTy) &&
24322435
isa<clang::TemplateTypeParmType>(paramTy->getPointeeType())) {
24332436
auto pointeeType = paramTy->getPointeeType();

lib/ClangImporter/ImporterImpl.h

+2
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,8 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
545545
/// used when parsing the attribute text.
546546
llvm::SmallDenseMap<unsigned, SourceFile *> ClangSwiftAttrSourceFiles;
547547

548+
llvm::SmallPtrSet<const clang::FunctionDecl *, 16> funcsWithPointerBounds;
549+
548550
public:
549551
/// The Swift lookup table for the bridging header.
550552
std::unique_ptr<SwiftLookupTable> BridgingHeaderLookupTable;

0 commit comments

Comments
 (0)