@@ -8534,22 +8534,26 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
8534
8534
}
8535
8535
8536
8536
namespace {
8537
- class PointerParamInfo {
8537
+ class PointerParamInfoPrinter {
8538
8538
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 << " )" ; }
8542
8547
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 ();
8553
8557
out << " ." ;
8554
8558
if (isSizedBy)
8555
8559
out << " sizedBy" ;
@@ -8565,7 +8569,6 @@ class CountedByParam : public PointerParamInfo {
8565
8569
out, {}, {ctx.getLangOpts ()}); // TODO: map clang::Expr to Swift Expr
8566
8570
out << " \" )" ;
8567
8571
}
8568
- virtual ~CountedByParam () {}
8569
8572
};
8570
8573
} // namespace
8571
8574
@@ -8575,43 +8578,31 @@ void ClangImporter::Implementation::importBoundsAttributes(
8575
8578
dyn_cast_or_null<clang::FunctionDecl>(MappedDecl->getClangDecl ());
8576
8579
if (!ClangDecl)
8577
8580
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))
8589
8582
return ;
8590
8583
8591
8584
llvm::SmallString<128 > MacroString;
8592
8585
{
8593
8586
llvm::raw_svector_ostream out (MacroString);
8594
8587
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) ;
8600
8593
}
8594
+ parameterIndex++;
8601
8595
}
8602
- out << " )" ;
8603
8596
}
8604
8597
8605
- // Dig out a buffer with the attribute text.
8606
- unsigned bufferID = getClangSwiftAttrSourceBuffer (MacroString);
8607
8598
8608
8599
// Dig out a source file we can use for parsing.
8609
8600
auto &sourceFile = getClangSwiftAttrSourceFile (
8610
- *MappedDecl->getDeclContext ()->getParentModule (), bufferID );
8601
+ *MappedDecl->getDeclContext ()->getParentModule (), MacroString );
8611
8602
8612
8603
// 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 );
8615
8606
// Prime the lexer.
8616
8607
parser.consumeTokenWithoutFeedingReceiver ();
8617
8608
0 commit comments