Skip to content

Commit be1db64

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 ced7d04 commit be1db64

File tree

3 files changed

+33
-37
lines changed

3 files changed

+33
-37
lines changed

lib/ClangImporter/ImportDecl.cpp

+28-37
Original file line numberDiff line numberDiff line change
@@ -8534,22 +8534,26 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
85348534
}
85358535

85368536
namespace {
8537-
class PointerParamInfo {
8537+
class PointerParamInfoPrinter {
85388538
public:
8539-
virtual void print(clang::ASTContext &ctx, llvm::raw_ostream &out) const = 0;
8540-
virtual ~PointerParamInfo() = default;
8541-
};
8539+
clang::ASTContext &ctx;
8540+
llvm::raw_ostream &out;
8541+
bool firstParam = true;
8542+
PointerParamInfoPrinter(clang::ASTContext &ctx, llvm::raw_ostream &out)
8543+
: ctx(ctx), out(out) {
8544+
out << "@PointerBounds(";
8545+
}
8546+
~PointerParamInfoPrinter() { out << ")"; }
85428547

8543-
class CountedByParam : public PointerParamInfo {
8544-
public:
8545-
size_t pointerIndex;
8546-
clang::Expr *countExpr;
8547-
bool isSizedBy;
8548-
CountedByParam(size_t idx, clang::Expr *E, bool sizedBy)
8549-
: pointerIndex(idx), countExpr(E), isSizedBy(sizedBy) {}
8550-
8551-
virtual void print(clang::ASTContext &ctx,
8552-
llvm::raw_ostream &out) const override {
8548+
void printCountedBy(const clang::CountAttributedType *CAT,
8549+
size_t pointerIndex) {
8550+
if (!firstParam) {
8551+
out << ", ";
8552+
} else {
8553+
firstParam = false;
8554+
}
8555+
clang::Expr *countExpr = CAT->getCountExpr();
8556+
bool isSizedBy = CAT->isCountInBytes();
85538557
out << ".";
85548558
if (isSizedBy)
85558559
out << "sizedBy";
@@ -8565,7 +8569,6 @@ class CountedByParam : public PointerParamInfo {
85658569
out, {}, {ctx.getLangOpts()}); // TODO: map clang::Expr to Swift Expr
85668570
out << "\")";
85678571
}
8568-
virtual ~CountedByParam() {}
85698572
};
85708573
} // namespace
85718574

@@ -8575,43 +8578,31 @@ void ClangImporter::Implementation::importBoundsAttributes(
85758578
dyn_cast_or_null<clang::FunctionDecl>(MappedDecl->getClangDecl());
85768579
if (!ClangDecl)
85778580
return;
8578-
8579-
SmallVector<PointerParamInfo *, 4> BoundsInfo;
8580-
size_t parameterIndex = 1;
8581-
for (auto param : ClangDecl->parameters()) {
8582-
if (auto CAT = param->getType()->getAs<clang::CountAttributedType>()) {
8583-
BoundsInfo.push_back(new CountedByParam(
8584-
parameterIndex, CAT->getCountExpr(), CAT->isCountInBytes()));
8585-
}
8586-
parameterIndex++;
8587-
}
8588-
if (BoundsInfo.empty())
8581+
if (!funcsWithPointerBounds.count(ClangDecl))
85898582
return;
85908583

85918584
llvm::SmallString<128> MacroString;
85928585
{
85938586
llvm::raw_svector_ostream out(MacroString);
85948587

8595-
out << "@PointerBounds(";
8596-
for (size_t i = 0; i < BoundsInfo.size(); i++) {
8597-
BoundsInfo[i]->print(getClangASTContext(), out);
8598-
if (i + 1 < BoundsInfo.size()) {
8599-
out << ", ";
8588+
size_t parameterIndex = 1;
8589+
PointerParamInfoPrinter printer(getClangASTContext(), out);
8590+
for (auto param : ClangDecl->parameters()) {
8591+
if (auto CAT = param->getType()->getAs<clang::CountAttributedType>()) {
8592+
printer.printCountedBy(CAT, parameterIndex);
86008593
}
8594+
parameterIndex++;
86018595
}
8602-
out << ")";
86038596
}
86048597

8605-
// Dig out a buffer with the attribute text.
8606-
unsigned bufferID = getClangSwiftAttrSourceBuffer(MacroString);
86078598

86088599
// Dig out a source file we can use for parsing.
86098600
auto &sourceFile = getClangSwiftAttrSourceFile(
8610-
*MappedDecl->getDeclContext()->getParentModule(), bufferID);
8601+
*MappedDecl->getDeclContext()->getParentModule(), MacroString);
86118602

86128603
// Spin up a parser.
8613-
swift::Parser parser(bufferID, sourceFile, &SwiftContext.Diags, nullptr,
8614-
nullptr);
8604+
swift::Parser parser(sourceFile.getBufferID(), sourceFile,
8605+
&SwiftContext.Diags, nullptr, nullptr);
86158606
// Prime the lexer.
86168607
parser.consumeTokenWithoutFeedingReceiver();
86178608

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
@@ -544,6 +544,8 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
544544
/// These are re-used when parsing the Swift attributes on import.
545545
llvm::StringMap<llvm::TinyPtrVector<SourceFile *>> ClangSwiftAttrSourceFiles;
546546

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

0 commit comments

Comments
 (0)